Code

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