Code

Execute parents execute() method child classes of plugin
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiSummaryTab.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) Fabian Hickert 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class faiSummaryTab extends plugin{
23   var $Classes        = array();
24   var $Release        = "";
25   var $Releases       = array();
26   var $Result         = array();
27   var $objs               = array();
28   var $ObjectList     = "";
29   var $is_dialog      = false;
30   var $InitCalled     = false;
31   var $usedClasses    = array();
32   var $base           = ""; 
33   var $parent         = NULL; 
34   var $ui             = NULL;
36   function faiSummaryTab(&$config,$dn)//,$parent)
37   {
38     plugin::plugin($config,$dn);//,$parent);
40     $this->ui = get_userinfo();
42     /* Base object configuration */
43     $this->objs = array(        
44         "FAIscript"                 => array(   "Image" =>"plugins/fai/images/fai_script.png",        "Name"=>_("Scripts")      ,"Tree" =>get_ou('faiScriptRDN')) ,
45         "FAIhook"                           => array(   "Image" =>"plugins/fai/images/fai_hook.png" ,         "Name"=>_("Hooks")        ,"Tree" =>get_ou('faiHookRDN')),
46         "FAIvariable"             => array(     "Image" =>"plugins/fai/images/fai_variable.png",      "Name"=>_("Variables")    ,"Tree" =>get_ou('faiVariableRDN')) ,
47         "FAItemplate"             => array(     "Image" =>"plugins/fai/images/fai_template.png",      "Name"=>_("Templates")    ,"Tree" =>get_ou('faiTemplateRDN')) ,
48         "FAIpartitionTable" => array(   "Image" =>"plugins/fai/images/fai_partitionTable.png","Name"=>_("Partition table")  ,"Tree"     =>get_ou('faiPartitionRDN')) ,
49         "FAIpackageList"          => array(     "Image" =>"plugins/fai/images/fai_packages.png",      "Name"=>_("Package list") ,"Tree" =>get_ou('faiPackageRDN')) ,
50         "FAIprofile"                => array(   "Image" =>"plugins/fai/images/fai_profile.png",       "Name"=>_("Profiles")     ,"Tree" =>get_ou('faiProfileRDN')));
51   }
54   /* Init the fai tree with objects.
55      If this method is called, all assigned classes and the release name 
56       will be read from parent object. */
57   function init()
58   {
59     $this->InitCalled = true;
60     $this->Result = array();
61   
62     /* Get classes & release name 
63        There are 2 different tabs which use the summary tab
64        faiProfile / Workstations */
65     
66     /* Check if given parent object is fai profile tabs */
67     if(isset($this->parent->by_name['faiProfile'])){
68       
69       /* Get Classes and release */
70       $this->Classes = $this->parent->by_object['faiProfile']->FAIclasses;
71       $this->base    = $this->parent->by_object['faiProfile']->parent->parent->fai_release;
72       $str = preg_replace("/^.*".preg_quote(get_ou('faiProfileRDN'), '/')."/i","",$this->dn);
73       $tmp  = $this->parent->by_object['faiProfile']->parent->parent->getBranches();
74       $this->Release = $tmp[$this->base];
75   
76     /* Check if parent tab is work tab */
77     }elseif(isset($this->parent->by_name['workstartup'])){
78       if($this->parent->by_object['workstartup']->FAIdebianMirror == "inherited"){
79         $this->Classes = $this->parent->by_object['workstartup']->InheritedFAIclass;
80         $this->Release = $this->parent->by_object['workstartup']->InheritedFAIrelease;
81       }else{
82         $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
83         $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
84       }
86       /* Check if this is a workstation or an object group */
87       if(isset($this->parent->by_object['workgeneric'])){
88         $this->base    = $this->parent->by_object['workgeneric']->base;
89       }elseif(isset($this->parent->by_object['ogroup'])){
90         $this->base    = $this->parent->by_object['ogroup']->base;
91       }elseif(isset($this->parent->by_object['servgeneric'])){
92         $this->base    = $this->parent->by_object['servgeneric']->base;
93       }else{
94         msg_dialog::display(_("Error"), _("Unknown FAI information source!"), ERROR_DIALOG);
95       }
96       
97       /* Append workstation class && LAST */
98       if(isset($this->parent->by_object['workgeneric']->cn)){
99         $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
100       }
101     }
103     $tmp = array_flip($this->getBranches());
105     $this->Classes[] = "LAST";
107     /* You need full access to complete collection of fai acls to view this summary */
108     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
109     if(!preg_match("/r/",$acl)){
110       $this->Classes = array();
111     }
113     /* Set all available releases */
114     $this->Releases = $tmp;
115     $this->usedClasses = array();
117     /* Check the configuration */
118     if(count($this->Classes) && (!empty($this->Release))){
119       foreach($this->Classes as $class){
120         $this->resolveObject($class);
121       }
122     }
123   }
126   /* Capture posts, and call opnen close tag */
127   function save_object()
128   {
129     $once = true;
131     if(isset($_GET['expand']) && isset($_GET['id'])){
132       if(isset($this->Result[$_GET['id']])){
133         $this->Result[$_GET['id']]['Open'] =true  ;
134       }
135     }
137     if(isset($_GET['compress']) && isset($_GET['id'])){
138       if(isset($this->Result[$_GET['id']])){
139         unset($this->Result[$_GET['id']]['Open']);
140       }
141     }
143     foreach($_POST as $name => $value ){
145       /* check for open tag request */
146       if(preg_match("/^open_/",$name) && $once){
147         $once  = false;
148         $value = preg_replace("/^open_/","",$name);
149         $value = preg_replace("/_.*$/","",$value); 
150         $this->Result[$value]['Open'] =true  ;
151       }
152       /* Check for close tag request */
153       if(preg_match("/^close_/",$name) && $once){
154         $once  = false;
155         $value = preg_replace("/^close_/","",$name);
156         $value = preg_replace("/_.*$/","",$value); 
157         unset($this->Result[$value]['Open']);
158       }
159       if(preg_match("/^reloadList/",$name)){
160         $this->InitCalled = false;
161       }
162     }
163   }
166   /* returns an array with all possibly release types */
167   function getBranches($base = false,$prefix = "")
168   {
169     if(!$base){
170       $base = $this->base;
171     }
172     return(FAI::get_all_releases_from_base($base,true));
173   }
176   /* This function calls all nescessary functions to generate the fai class summary tree */
177   function execute() 
178   {
179     plugin::execute();
181     /* Skip this if fai is deactivated */
182     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
183     if(empty($tmp)){
184       $str = "<h3>"._("You can't use this plugin until FAI is activated.")."</h3>";
185       return $str;
186     }    
188     /* Check if we must (re)init the this tab */
189     if(!$this->InitCalled){
190       $this->init();
191     }
193     if(isset($_GET['show'])){
194       $ldap = $this->config->get_ldap_link();
195       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplateFile'));
196       $attrs = $ldap->fetch();
197       if($_GET['type'] == "FAIscript"){
198         $str = $attrs['FAIscript'][0];
199       }elseif($_GET['type'] == "FAIhook"){
200         $str = $attrs['FAIscript'][0];
201       }elseif($_GET['type'] == "FAItemplate"){
202         $str = $attrs['FAItemplateFile'][0];
203       }
204       echo "<pre>".$str."</pre>";
205       exit();
206     }
208     /* Get smarty class & assign created summary results */
209     $smarty = get_smarty();
210     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
211     $smarty->assign("readable", preg_match("/r/",$acl));
213     $this->ObjectList = $this->createSummary($this->Result);
214     $smarty->assign("objectList",$this->ObjectList);
215     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
216   }
219   /* Create output of from resolved fai objects */
220   function createSummary($data)
221   {
222     /* Don't generate any output, if there is no result */ 
223     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
224     if(!count($this->Result)){
225       $str.="<tr><td>"; 
226       $str.= (_("This object has no FAI classes assigned."));
227       $str.= "</tr></td></table>";
228       return($str);
229     }
231     /* walk through all classes */      
232     foreach($data as $key => $entry){
233       
234       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
235       if(!isset($entry['Open'])){
236         $str .= "<tr>
237                   <td colspan=2>
238                    <a href='?plug=".$_GET['plug']."&amp;expand&amp;id=".$key."'>
239                     <img border=0 class='center' src='images/lists/expand.png' alt='"._("Open")."'>&nbsp;";
240         $str .=     $image."&nbsp;";
241         $str .= "   <b>".$this->objs[$key]['Name']."</b></a></td></tr>";
242       }else{
243         $str .= "<tr><td colspan=2>
244                     <a href='?plug=".$_GET['plug']."&amp;compress&amp;id=".$key."'>
245                    <img border=0 class='center' src='images/lists/sort-down.png' alt='"._("Close")."'>&nbsp;";
246         $str .= $image."&nbsp;";
247         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
248    
249         /* Display FAItemplate FAIhook FAIscript entries */ 
250         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
252           $nums = array();          
253           $tmp  = array();
254           if($key == "FAIscript"){
255             if(is_array($entry['Entries'])){
256               foreach($entry['Entries'] as $scripts){
257                 foreach($scripts as $script){
259                   if(!isset($nums[$script['cn'][0]])){
260                     $nums[$script['cn'][0]]= 0;
261                   } 
262                   $nums[$script['cn'][0]] ++;
264                   $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
265                 }
266               }
267             }
268             krsort($tmp);;
269             
270             $entry['Entries'] = $tmp;
271           }else{
272             $tmp = array();
273             foreach($entry['Entries'] as $script){
274               $tmp[$script['cn'][0].$script['CLASS']] = $script;
275               if(!isset($nums[$script['cn'][0]])){
276                 $nums[$script['cn'][0]]= 0;
277               } 
278               $nums[$script['cn'][0]] ++;
279             }
280              ksort($tmp);
281             $entry['Entries'] = $tmp;
282           }
284           foreach($entry['Entries'] as $cn => $data){
286             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
287               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
288             }else{
289               $str .="<tr><td style='padding-left:20px;'>";
290             }
292             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&amp;show&amp;type=".$key."&amp;id=".base64_encode($data['dn'])."'>";
293             $str .= $image."&nbsp;";
294             if(isset($data['FAIpriority'][0])){
295               $str .= "(".$data['FAIpriority'][0].")";
296             }
298             $str .= "&nbsp;".$data['cn'][0];
299             if(isset($data['description'][0])){
300               $str .= " [".$data['description'][0]."]";
301             }
302             $str .= "</a>";
303             $str .= "</td><td><i>".$data['CLASS']."</i>";
304             $str .= "</td></tr>";
305           }
306         }
308         /* Create variable entries */
309         if(in_array($key,array("FAIvariable"))) {
310           foreach($entry['Entries'] as $cn => $data){
311             $str .="<tr><td style='padding-left:20px;'>";
312             $str .= $image."&nbsp;".$data['cn'][0];
313             if(isset($data['description'][0])){
314               $str .= " [".$data['description'][0]."] ";
315             }
316             /* Only display FAIvariableContent if description dosn't contain [*] */ 
317             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
318               $str .=" = '".$data['FAIvariableContent'][0]."'";
319             }
320             $str .= "</td><td><i>".$data['CLASS']."</i>";
321             $str .="</td></tr>";
322           }
323         }
325         /* Create packagelist entries */ 
326         if(in_array($key,array("FAIpackageList"))) {
327           ksort($entry['Entries']);
328           foreach($entry['Entries'] as $data){
329             $str .="<tr><td style='padding-left:20px;'>";
330             $str .= $image."&nbsp;".$data['cn'][0];
331             $str .= "</td><td><i>".$data['CLASS']."</i>";
332             $str .= "</td></tr>";;
333           }
334         }
336         /* Create partition table entries */
337         if(in_array($key,array("FAIpartitionTable"))) {
338           foreach($entry['Entries'] as $cn => $data){
339             $str .= "<tr><td style='padding-left:20px;'>";
340             $str .= $image."&nbsp;".$data['name'];
341             $str .= "</td><td><i>".$data['CLASS']."</i>";
342             $str .= "</td></tr>";
343             ksort($data['partitions']);
345             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
346             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
347                       <tr style='background-color:#DBDBDB;'>
348                        <td class='tbhead'>"._("No.")."</td>
349                        <td class='tbhead'>"._("Name")."</td>
350                        <td class='tbhead'>"._("FS options")."</td>
351                        <td class='tbhead'>"._("Mount options")."</td>
352                        <td class='tbhead'>"._("Size in MB")."</td>
353                        <td class='tbhead'>"._("Mount point")."</td>
354                        <td class='tbrhead'>"._("Type")."</td>
355                       </tr>";
357             $i = 0;
358             foreach($data['partitions'] as $key => $part){
359               $i ++;
361               if($i%2 == 1){
362                 $c = " class='rowxp1'";
363                 $d = " class='list1'";
364               }else{
365                 $c = " class='rowxp0'";
366                 $d = " class='list0'";
367               }
369               $str.="<tr $c style='height:10px;'>
370                       <td $d>".$i."</td> 
371                       <td $d>".$part['cn'][0]."</td> 
372                       <td $d>".$part['FAIfsType'][0]."</td> 
373                       <td $d>".$part['FAIfsOptions'][0]."</td> 
374                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
375                       <td $d>".$part['FAImountPoint'][0]."</td> 
376                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
377                     </tr>";
378             }
379             $str .="</table>";
380           }
381         }
382       }
383     }
384     $str .="</table>";  
385     return($str);
386   }
389   /* resolve specified object to append it to our object tree */
390   function resolveObject($class)
391   {
392     $ldap                 = $this->config->get_ldap_link();
393     if(isset($this->Releases[$this->Release])){
394       $dn                   = $this->Releases[$this->Release];
395     }else{
396       return(FALSE);
397     }
398   
399     $resolvedClasses  = FAI::get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
401     /* Try to fetch all types of fai objects with the given cn  */
402     foreach($resolvedClasses as $obj){
404       $dn = $obj['dn'];
405       $ldap->cat($dn);
407       while($attrs = $ldap->fetch()){
409         foreach($this-> objs as $key => $rest){
410           if(in_array($key,$attrs['objectClass'])){
412             if(!isset($this->usedClasses[$key][$class])){
413               $this->usedClasses[$key][$class] = true;
414               switch($key){
415                 case "FAIprofile":                      $this->prepare_FAIprofile($attrs);break;
416                 case "FAIscript":                       $this->prepare_FAIscript($attrs);break;
417                 case "FAIhook":                           $this->prepare_FAIhook($attrs);break;
418                 case "FAIvariable":                     $this->prepare_FAIvariable($attrs);break;
419                 case "FAItemplate":                     $this->prepare_FAItemplate($attrs);break;
420                 case "FAIpackageList":        $this->prepare_FAIpackageList($attrs);break;
421                 case "FAIpartitionTable":           $this->prepare_FAIpartitionTable($attrs);break;
422               }
423             }
425           }
426         }
427       }
428     }
429   }
431   /* Prepare fai script */
432   function prepare_FAIscript($data)
433   {
434     if(isset($this->Result['FAIscript']['Entries'])){
435       $current = $this->Result['FAIscript']['Entries'];
436     }else{
437       $current = array();
438     }
439     $ldap = $this->config->get_ldap_link();
440     $ldap->cd($data['dn']);
441     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
442     while($attrs = $ldap->fetch()){
443       $attrs['CLASS'] = $data['cn'][0];
444       $current[$attrs['FAIpriority'][0]][] = $attrs;
445     }
446     $this->Result['FAIscript']['Entries'] = $current ;
447   }
450   /* Prepare fai script */
451   function prepare_FAIpartitionTable($data)
452   {
453     if(isset($this->Result['FAIpartitionTable']['Entries'])){
454       $current = $this->Result['FAIpartitionTable']['Entries'];
455     }else{
456       $current = array();
457     }
459     /* get subentries */
460     $ldap = $this->config->get_ldap_link();
461     $ldap->cd($data['dn']);
462     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
463     $disks = array();
465     /* Create an array which is sortable by priority */
466     while($attrs = $ldap->fetch()){
467       $attrs['CLASS'] = $data['cn'][0];
468       $disks[$attrs['cn'][0]]= $attrs;
469       if(isset($attrs['description'][0])){
470         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
471       }else{
472         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
473       }
474       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
475       $disks[$attrs['cn'][0]]['partitions'] = array();
476     }
478     /* Sort by priority */
479     foreach($disks as $key => $disk){
480       $ldap->cd($disk['dn']);
481       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
482       while($attrs = $ldap->fetch()){
483         if(!isset($attrs['FAIfsOptions'][0])){
484           $attrs['FAIfsOptions'][0] = "";
485         }
486         $attrs['CLASS'] = $data['cn'][0];
487         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
488       }
489     }
490     $this->Result['FAIpartitionTable']['Entries'] = $disks;
491   }
494   /* Create template entry */   
495   function prepare_FAItemplate($data)
496   {
497     $current = array();
498     if(isset($this->Result['FAItemplate']['Entries'])){
499       $current = $this->Result['FAItemplate']['Entries'];
500     }
502     $ldap = $this->config->get_ldap_link();
503     $ldap->cd($data['dn']);
504     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
505     while($attrs = $ldap->fetch()){
506       $attrs['CLASS'] = $data['cn'][0];
507       $current[] = $attrs;
508     }
509     $this->Result['FAItemplate']['Entries'] = $current ;
510   }
513   /* Create a package list  */  
514   function prepare_FAIpackageList($data)
515   {
516     $current = array();
517     if(isset($this->Result['FAIpackageList']['Entries'])){
518       $current = $this->Result['FAIpackageList']['Entries'];
519     }
520     if(isset($data['FAIpackage'])){
521       unset($data['FAIpackage']['count']);
522       foreach($data['FAIpackage'] as $pkg){
523         $attrs['CLASS'] = $data['cn'][0];
524         $attrs['cn'][0] = $pkg;
525         $current[$pkg] = $attrs;
526       }
527     }
528     $this->Result['FAIpackageList']['Entries'] = $current ;
529   }
532   /* Create a variable entry */ 
533   function prepare_FAIvariable($data)
534   {
535     $current = array();
536     if(isset($this->Result['FAIvariable']['Entries'])){
537       $current = $this->Result['FAIvariable']['Entries'];
538     }
539     $ldap = $this->config->get_ldap_link();
540     $ldap->cd($data['dn']);
541     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
542     while($attrs = $ldap->fetch()){
543       $attrs['CLASS'] = $data['cn'][0];
544       $current[] = $attrs;
545     }
546     $this->Result['FAIvariable']['Entries'] = $current ;
547   }
549   /* Create a hook entry */     
550   function prepare_FAIhook($data)
551   {
552     $current = array();
553     if(isset($this->Result['FAIhook']['Entries'])){
554       $current = $this->Result['FAIhook']['Entries'];
555     }
556     $ldap = $this->config->get_ldap_link();
557     $ldap->cd($data['dn']);
558     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
559     while($attrs = $ldap->fetch()){
560       $attrs['CLASS'] = $data['cn'][0];
561       $current[$attrs['cn'][0]] = $attrs;
562     }
563     $this->Result['FAIhook']['Entries'] = $current ;
564   }
567   /* Create a new Profile entry */
568   function prepare_FAIprofile($data)
569   {
570     $classes = explode(" ",$data['FAIclass'][0]);
571     foreach($classes as $class){
572       $class = trim($class);
573       $this->resolveObject($class);
574     }
575   }
577   /* Return plugin informations for acl handling * /
578   static function plInfo()
579   {
580     return (array(
581           "plShortName"   => _("Summary"),
582           "plDescription" => _("FAI summary"),
583           "plSelfModify"  => FALSE,
584           "plDepends"     => array(),
585           "plPriority"    => 15,
586           "plSection"     => array("administration"),
587           "plCategory"    => array("workstation","server","fai","ogroups") ,
588           "plProvidedAcls"=> array(
589             "readable" => _("Viewable")),
590           ));
591   }
592   */
594 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
595 ?>