Code

Added branches container for old stuff
[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('faiscriptou')) ,
45         "FAIhook"                           => array(   "Image" =>"plugins/fai/images/fai_hook.png" ,         "Name"=>_("Hooks")        ,"Tree" =>get_ou('faihookou')),
46         "FAIvariable"             => array(     "Image" =>"plugins/fai/images/fai_variable.png",      "Name"=>_("Variables")    ,"Tree" =>get_ou('faivariableou')) ,
47         "FAItemplate"             => array(     "Image" =>"plugins/fai/images/fai_template.png",      "Name"=>_("Templates")    ,"Tree" =>get_ou('faitemplateou')) ,
48         "FAIpartitionTable" => array(   "Image" =>"plugins/fai/images/fai_partitionTable.png","Name"=>_("Partition table")  ,"Tree"     =>get_ou('faipartitionou')) ,
49         "FAIpackageList"          => array(     "Image" =>"plugins/fai/images/fai_packages.png",      "Name"=>_("Package list") ,"Tree" =>get_ou('faipackageou')) ,
50         "FAIprofile"                => array(   "Image" =>"plugins/fai/images/fai_profile.png",       "Name"=>_("Profiles")     ,"Tree" =>get_ou('faiprofileou')));
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("/^.*".normalizePreg(get_ou('faiprofileou'))."/","",$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       $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
79       $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
81       /* Check if this is a workstation or an object group */
82       if(isset($this->parent->by_object['workgeneric'])){
83         $this->base    = $this->parent->by_object['workgeneric']->base;
84       }elseif(isset($this->parent->by_object['ogroup'])){
85         $this->base    = $this->parent->by_object['ogroup']->base;
86       }elseif(isset($this->parent->by_object['servgeneric'])){
87         $this->base    = $this->parent->by_object['servgeneric']->base;
88       }else{
89         msg_dialog::display(_("Error"), _("Unknown FAI information source!"), ERROR_DIALOG);
90       }
91       
92       /* Append workstation class && LAST */
93       if(isset($this->parent->by_object['workgeneric']->cn)){
94         $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
95       }
96     }
98     $tmp = array_flip($this->getBranches());
100     $this->Classes[] = "LAST";
102     /* You need full access to complete collection of fai acls to view this summary */
103     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
104     if(!preg_match("/r/",$acl)){
105       $this->Classes = array();
106     }
108     /* Set all available releases */
109     $this->Releases = $tmp;
110     $this->usedClasses = array();
112     /* Check the configuration */
113     if(count($this->Classes) && (!empty($this->Release))){
114       foreach($this->Classes as $class){
115         $this->resolveObject($class);
116       }
117     }
118   }
121   /* Capture posts, and call opnen close tag */
122   function save_object()
123   {
124     $once = true;
126     if(isset($_GET['expand']) && isset($_GET['id'])){
127       if(isset($this->Result[$_GET['id']])){
128         $this->Result[$_GET['id']]['Open'] =true  ;
129       }
130     }
132     if(isset($_GET['compress']) && isset($_GET['id'])){
133       if(isset($this->Result[$_GET['id']])){
134         unset($this->Result[$_GET['id']]['Open']);
135       }
136     }
138     foreach($_POST as $name => $value ){
140       /* check for open tag request */
141       if(preg_match("/^open_/",$name) && $once){
142         $once  = false;
143         $value = preg_replace("/^open_/","",$name);
144         $value = preg_replace("/_.*$/","",$value); 
145         $this->Result[$value]['Open'] =true  ;
146       }
147       /* Check for close tag request */
148       if(preg_match("/^close_/",$name) && $once){
149         $once  = false;
150         $value = preg_replace("/^close_/","",$name);
151         $value = preg_replace("/_.*$/","",$value); 
152         unset($this->Result[$value]['Open']);
153       }
154       if(preg_match("/^reloadList/",$name)){
155         $this->InitCalled = false;
156       }
157     }
158   }
161   /* returns an array with all possibly release types */
162   function getBranches($base = false,$prefix = "")
163   {
164     if(!$base){
165       $base = $this->base;
166     }
167     return(FAI::get_all_releases_from_base($base,true));
168   }
171   /* This function calls all nescessary functions to generate the fai class summary tree */
172   function execute() 
173   {
175     /* Skip this if fai is deactivated */
176     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
177     if(empty($tmp)){
178       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
179       return $str;
180     }    
182     /* Check if we must (re)init the this tab */
183     if(!$this->InitCalled){
184       $this->init();
185     }
187     if(isset($_GET['show'])){
188       $ldap = $this->config->get_ldap_link();
189       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplateFile'));
190       $attrs = $ldap->fetch();
191       if($_GET['type'] == "FAIscript"){
192         $str = $attrs['FAIscript'][0];
193       }elseif($_GET['type'] == "FAIhook"){
194         $str = $attrs['FAIscript'][0];
195       }elseif($_GET['type'] == "FAItemplate"){
196         $str = $attrs['FAItemplateFile'][0];
197       }
198       echo "<pre>".$str."</pre>";
199       exit();
200     }
202     /* Get smarty class & assign created summary results */
203     $smarty = get_smarty();
204     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
205     $smarty->assign("readable", preg_match("/r/",$acl));
207     $this->ObjectList = $this->createSummary($this->Result);
208     $smarty->assign("objectList",$this->ObjectList);
209     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
210   }
213   /* Create output of from resolved fai objects */
214   function createSummary($data)
215   {
216     /* Don't generate any output, if there is no result */ 
217     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
218     if(!count($this->Result)){
219       $str.="<tr><td>"; 
220       $str.= (_("This object has no FAI classes assigned."));
221       $str.= "</tr></td></table>";
222       return($str);
223     }
225     /* walk through all classes */      
226     foreach($data as $key => $entry){
227       
228       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
229       if(!isset($entry['Open'])){
230         $str .= "<tr>
231                   <td colspan=2>
232                    <a href='?plug=".$_GET['plug']."&amp;expand&amp;id=".$key."'>
233                     <img border=0 class='center' src='images/lists/expand.png' alt='"._("Open")."'>&nbsp;";
234         $str .=     $image."&nbsp;";
235         $str .= "   <b>".$this->objs[$key]['Name']."</b></a></td></tr>";
236       }else{
237         $str .= "<tr><td colspan=2>
238                     <a href='?plug=".$_GET['plug']."&amp;compress&amp;id=".$key."'>
239                    <img border=0 class='center' src='images/lists/sort-down.png' alt='"._("Close")."'>&nbsp;";
240         $str .= $image."&nbsp;";
241         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
242    
243         /* Display FAItemplate FAIhook FAIscript entries */ 
244         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
246           $nums = array();          
247           $tmp  = array();
248           if($key == "FAIscript"){
249             if(is_array($entry['Entries'])){
250               foreach($entry['Entries'] as $scripts){
251                 foreach($scripts as $script){
253                   if(!isset($nums[$script['cn'][0]])){
254                     $nums[$script['cn'][0]]= 0;
255                   } 
256                   $nums[$script['cn'][0]] ++;
258                   $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
259                 }
260               }
261             }
262             krsort($tmp);;
263             
264             $entry['Entries'] = $tmp;
265           }else{
266             $tmp = array();
267             foreach($entry['Entries'] as $script){
268               $tmp[$script['cn'][0].$script['CLASS']] = $script;
269               if(!isset($nums[$script['cn'][0]])){
270                 $nums[$script['cn'][0]]= 0;
271               } 
272               $nums[$script['cn'][0]] ++;
273             }
274              ksort($tmp);
275             $entry['Entries'] = $tmp;
276           }
278           foreach($entry['Entries'] as $cn => $data){
280             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
281               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
282             }else{
283               $str .="<tr><td style='padding-left:20px;'>";
284             }
286             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&amp;show&amp;type=".$key."&amp;id=".base64_encode($data['dn'])."'>";
287             $str .= $image."&nbsp;";
288             if(isset($data['FAIpriority'][0])){
289               $str .= "(".$data['FAIpriority'][0].")";
290             }
292             $str .= "&nbsp;".$data['cn'][0];
293             if(isset($data['description'][0])){
294               $str .= " [".$data['description'][0]."]";
295             }
296             $str .= "</a>";
297             $str .= "</td><td><i>".$data['CLASS']."</i>";
298             $str .= "</td></tr>";
299           }
300         }
302         /* Create variable entries */
303         if(in_array($key,array("FAIvariable"))) {
304           foreach($entry['Entries'] as $cn => $data){
305             $str .="<tr><td style='padding-left:20px;'>";
306             $str .= $image."&nbsp;".$data['cn'][0];
307             if(isset($data['description'][0])){
308               $str .= " [".$data['description'][0]."] ";
309             }
310             /* Only display FAIvariableContent if description dosn't contain [*] */ 
311             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
312               $str .=" = '".$data['FAIvariableContent'][0]."'";
313             }
314             $str .= "</td><td><i>".$data['CLASS']."</i>";
315             $str .="</td></tr>";
316           }
317         }
319         /* Create packagelist entries */ 
320         if(in_array($key,array("FAIpackageList"))) {
321           ksort($entry['Entries']);
322           foreach($entry['Entries'] as $data){
323             $str .="<tr><td style='padding-left:20px;'>";
324             $str .= $image."&nbsp;".$data['cn'][0];
325             $str .= "</td><td><i>".$data['CLASS']."</i>";
326             $str .= "</td></tr>";;
327           }
328         }
330         /* Create partition table entries */
331         if(in_array($key,array("FAIpartitionTable"))) {
332           foreach($entry['Entries'] as $cn => $data){
333             $str .= "<tr><td style='padding-left:20px;'>";
334             $str .= $image."&nbsp;".$data['name'];
335             $str .= "</td><td><i>".$data['CLASS']."</i>";
336             $str .= "</td></tr>";
337             ksort($data['partitions']);
339             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
340             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
341                       <tr style='background-color:#DBDBDB;'>
342                        <td class='tbhead'>"._("No.")."</td>
343                        <td class='tbhead'>"._("Name")."</td>
344                        <td class='tbhead'>"._("FS options")."</td>
345                        <td class='tbhead'>"._("Mount options")."</td>
346                        <td class='tbhead'>"._("Size in MB")."</td>
347                        <td class='tbhead'>"._("Mount point")."</td>
348                        <td class='tbrhead'>"._("Type")."</td>
349                       </tr>";
351             $i = 0;
352             foreach($data['partitions'] as $key => $part){
353               $i ++;
355               if($i%2 == 1){
356                 $c = " class='rowxp1'";
357                 $d = " class='list1'";
358               }else{
359                 $c = " class='rowxp0'";
360                 $d = " class='list0'";
361               }
363               $str.="<tr $c style='height:10px;'>
364                       <td $d>".$i."</td> 
365                       <td $d>".$part['cn'][0]."</td> 
366                       <td $d>".$part['FAIfsType'][0]."</td> 
367                       <td $d>".$part['FAIfsOptions'][0]."</td> 
368                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
369                       <td $d>".$part['FAImountPoint'][0]."</td> 
370                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
371                     </tr>";
372             }
373             $str .="</table>";
374           }
375         }
376       }
377     }
378     $str .="</table>";  
379     return($str);
380   }
383   /* resolve specified object to append it to our object tree */
384   function resolveObject($class)
385   {
386     $ldap                 = $this->config->get_ldap_link();
387     if(isset($this->Releases[$this->Release])){
388       $dn                   = $this->Releases[$this->Release];
389     }else{
390       return(FALSE);
391     }
392   
393     $resolvedClasses  = FAI::get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
395     /* Try to fetch all types of fai objects with the given cn  */
396     foreach($resolvedClasses as $obj){
398       $dn = $obj['dn'];
399       $ldap->cat($dn);
401       while($attrs = $ldap->fetch()){
403         foreach($this-> objs as $key => $rest){
404           if(in_array($key,$attrs['objectClass'])){
406             if(!isset($this->usedClasses[$key][$class])){
407               $this->usedClasses[$key][$class] = true;
408               switch($key){
409                 case "FAIprofile":                      $this->prepare_FAIprofile($attrs);break;
410                 case "FAIscript":                       $this->prepare_FAIscript($attrs);break;
411                 case "FAIhook":                           $this->prepare_FAIhook($attrs);break;
412                 case "FAIvariable":                     $this->prepare_FAIvariable($attrs);break;
413                 case "FAItemplate":                     $this->prepare_FAItemplate($attrs);break;
414                 case "FAIpackageList":        $this->prepare_FAIpackageList($attrs);break;
415                 case "FAIpartitionTable":           $this->prepare_FAIpartitionTable($attrs);break;
416               }
417             }
419           }
420         }
421       }
422     }
423   }
425   /* Prepare fai script */
426   function prepare_FAIscript($data)
427   {
428     if(isset($this->Result['FAIscript']['Entries'])){
429       $current = $this->Result['FAIscript']['Entries'];
430     }else{
431       $current = array();
432     }
433     $ldap = $this->config->get_ldap_link();
434     $ldap->cd($data['dn']);
435     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
436     while($attrs = $ldap->fetch()){
437       $attrs['CLASS'] = $data['cn'][0];
438       $current[$attrs['FAIpriority'][0]][] = $attrs;
439     }
440     $this->Result['FAIscript']['Entries'] = $current ;
441   }
444   /* Prepare fai script */
445   function prepare_FAIpartitionTable($data)
446   {
447     if(isset($this->Result['FAIpartitionTable']['Entries'])){
448       $current = $this->Result['FAIpartitionTable']['Entries'];
449     }else{
450       $current = array();
451     }
453     /* get subentries */
454     $ldap = $this->config->get_ldap_link();
455     $ldap->cd($data['dn']);
456     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
457     $disks = array();
459     /* Create an array which is sortable by priority */
460     while($attrs = $ldap->fetch()){
461       $attrs['CLASS'] = $data['cn'][0];
462       $disks[$attrs['cn'][0]]= $attrs;
463       if(isset($attrs['description'][0])){
464         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
465       }else{
466         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
467       }
468       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
469       $disks[$attrs['cn'][0]]['partitions'] = array();
470     }
472     /* Sort by priority */
473     foreach($disks as $key => $disk){
474       $ldap->cd($disk['dn']);
475       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
476       while($attrs = $ldap->fetch()){
477         if(!isset($attrs['FAIfsOptions'][0])){
478           $attrs['FAIfsOptions'][0] = "";
479         }
480         $attrs['CLASS'] = $data['cn'][0];
481         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
482       }
483     }
484     $this->Result['FAIpartitionTable']['Entries'] = $disks;
485   }
488   /* Create template entry */   
489   function prepare_FAItemplate($data)
490   {
491     $current = array();
492     if(isset($this->Result['FAItemplate']['Entries'])){
493       $current = $this->Result['FAItemplate']['Entries'];
494     }
496     $ldap = $this->config->get_ldap_link();
497     $ldap->cd($data['dn']);
498     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
499     while($attrs = $ldap->fetch()){
500       $attrs['CLASS'] = $data['cn'][0];
501       $current[] = $attrs;
502     }
503     $this->Result['FAItemplate']['Entries'] = $current ;
504   }
507   /* Create a package list  */  
508   function prepare_FAIpackageList($data)
509   {
510     $current = array();
511     if(isset($this->Result['FAIpackageList']['Entries'])){
512       $current = $this->Result['FAIpackageList']['Entries'];
513     }
514     if(isset($data['FAIpackage'])){
515       unset($data['FAIpackage']['count']);
516       foreach($data['FAIpackage'] as $pkg){
517         $attrs['CLASS'] = $data['cn'][0];
518         $attrs['cn'][0] = $pkg;
519         $current[$pkg] = $attrs;
520       }
521     }
522     $this->Result['FAIpackageList']['Entries'] = $current ;
523   }
526   /* Create a variable entry */ 
527   function prepare_FAIvariable($data)
528   {
529     $current = array();
530     if(isset($this->Result['FAIvariable']['Entries'])){
531       $current = $this->Result['FAIvariable']['Entries'];
532     }
533     $ldap = $this->config->get_ldap_link();
534     $ldap->cd($data['dn']);
535     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
536     while($attrs = $ldap->fetch()){
537       $attrs['CLASS'] = $data['cn'][0];
538       $current[] = $attrs;
539     }
540     $this->Result['FAIvariable']['Entries'] = $current ;
541   }
543   /* Create a hook entry */     
544   function prepare_FAIhook($data)
545   {
546     $current = array();
547     if(isset($this->Result['FAIhook']['Entries'])){
548       $current = $this->Result['FAIhook']['Entries'];
549     }
550     $ldap = $this->config->get_ldap_link();
551     $ldap->cd($data['dn']);
552     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
553     while($attrs = $ldap->fetch()){
554       $attrs['CLASS'] = $data['cn'][0];
555       $current[$attrs['cn'][0]] = $attrs;
556     }
557     $this->Result['FAIhook']['Entries'] = $current ;
558   }
561   /* Create a new Profile entry */
562   function prepare_FAIprofile($data)
563   {
564     $classes = split("\ ",$data['FAIclass'][0]);
565     foreach($classes as $class){
566       $class = trim($class);
567       $this->resolveObject($class);
568     }
569   }
571   /* Return plugin informations for acl handling * /
572   static function plInfo()
573   {
574     return (array(
575           "plShortName"   => _("Summary"),
576           "plDescription" => _("FAI summary"),
577           "plSelfModify"  => FALSE,
578           "plDepends"     => array(),
579           "plPriority"    => 15,
580           "plSection"     => array("administration"),
581           "plCategory"    => array("workstation","server","fai","ogroups") ,
582           "plProvidedAcls"=> array(
583             "readable" => _("Viewable")),
584           ));
585   }
586   */
588 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
589 ?>