Code

c39eaf2963b67b51a9ed9e6db8fe982fb8810b09
[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("faiScript", "faiScriptRDN")) ,
45         "FAIhook"                           => array(   "Image" =>"plugins/fai/images/fai_hook.png" ,         "Name"=>_("Hooks")        ,"Tree" =>get_ou("faiHook", "faiHookRDN")),
46         "FAIvariable"             => array(     "Image" =>"plugins/fai/images/fai_variable.png",      "Name"=>_("Variables")    ,"Tree" =>get_ou("faiVariable", "faiVariableRDN")) ,
47         "FAItemplate"             => array(     "Image" =>"plugins/fai/images/fai_template.png",      "Name"=>_("Templates")    ,"Tree" =>get_ou("faiTemplate", "faiTemplateRDN")) ,
48         "FAIpartitionTable" => array(   "Image" =>"plugins/fai/images/fai_partitionTable.png","Name"=>_("Partition table")  ,"Tree"     =>get_ou("faiPartitionTable", "faiPartitionRDN")) ,
49         "FAIpackageList"          => array(     "Image" =>"plugins/fai/images/fai_packages.png",      "Name"=>_("Package list") ,"Tree" =>get_ou("faiPackage", "faiPackageRDN")) ,
50         "FAIprofile"                => array(   "Image" =>"plugins/fai/images/fai_profile.png",       "Name"=>_("Profiles")     ,"Tree" =>get_ou("faiProfile", "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("faiProfile", "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     if(!$this->config->pluginEnabled("faiManagement")){
188       $str = "<h3>"._("You can't use this plug-in until FAI is activated.")."</h3>";
189       return $str;
190     }    
192     /* Check if we must (re)init the this tab */
193     if(!$this->InitCalled){
194       $this->init();
195     }
197     if(isset($_GET['show'])){
198       $ldap = $this->config->get_ldap_link();
199       $ldap->cat(postDecode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplateFile'));
200       $attrs = $ldap->fetch();
201       if($_GET['type'] == "FAIscript"){
202         $str = $attrs['FAIscript'][0];
203       }elseif($_GET['type'] == "FAIhook"){
204         $str = $attrs['FAIscript'][0];
205       }elseif($_GET['type'] == "FAItemplate"){
206         $str = $attrs['FAItemplateFile'][0];
207       }
208       echo "<pre>".$str."</pre>";
209       exit();
210     }
212     /* Get smarty class & assign created summary results */
213     $smarty = get_smarty();
214     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
215     $smarty->assign("readable", preg_match("/r/",$acl));
217     $this->ObjectList = $this->createSummary($this->Result);
218     $smarty->assign("objectList",$this->ObjectList);
219     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
220   }
223   /* Create output of from resolved fai objects */
224   function createSummary($data)
225   {
226     /* Don't generate any output, if there is no result */ 
227     if(!count($this->Result)){
228       return("<h3>"._("This object has no FAI classes assigned.")."</h3>");
229     }
231     $str ="<table style='width:100%;' summary='"._("FAI summary")."'> ";
232     /* walk through all classes */      
233     foreach($data as $key => $entry){
234       
235       $image = image($this->objs[$key]['Image']);
236       if(!isset($entry['Open'])){
237         $str .= "<tr>
238           <td colspan=2>".
239           image('images/lists/expand.png').$image."&nbsp;".
240           "<a href='?plug=".$_GET['plug']."&amp;expand&amp;id=".$key."'>".
241           "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
242       }else{
243         $str .= "<tr><td colspan=2>".
244           image('images/lists/sort-down.png').$image."&nbsp;".
245           "<a href='?plug=".$_GET['plug']."&amp;compress&amp;id=".$key."'>".
246           "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
248         /* Display FAItemplate FAIhook FAIscript entries */ 
249         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
251           $nums = array();          
252           $tmp  = array();
253           if($key == "FAIscript"){
254             if(is_array($entry['Entries'])){
255               foreach($entry['Entries'] as $scripts){
256                 foreach($scripts as $script){
258                   if(!isset($nums[$script['cn'][0]])){
259                     $nums[$script['cn'][0]]= 0;
260                   } 
261                   $nums[$script['cn'][0]] ++;
263                   $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
264                 }
265               }
266             }
267             krsort($tmp);;
268             
269             $entry['Entries'] = $tmp;
270           }else{
271             $tmp = array();
272             foreach($entry['Entries'] as $script){
273               $tmp[$script['cn'][0].$script['CLASS']] = $script;
274               if(!isset($nums[$script['cn'][0]])){
275                 $nums[$script['cn'][0]]= 0;
276               } 
277               $nums[$script['cn'][0]] ++;
278             }
279              ksort($tmp);
280             $entry['Entries'] = $tmp;
281           }
283           foreach($entry['Entries'] as $cn => $data){
285             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
286               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
287             }else{
288               $str .="<tr><td style='padding-left:20px;'>";
289             }
291             $str .= $image."&nbsp;";
292             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&amp;show&amp;type=".$key."&amp;id=".postEncode($data['dn'])."'>";
293             if(isset($data['FAIpriority'][0])){
294               $str .= "(".$data['FAIpriority'][0].")";
295             }
297             $str .= "&nbsp;".$data['cn'][0];
298             if(isset($data['description'][0])){
299               $str .= " [".$data['description'][0]."]";
300             }
301             $str .= "</a>";
302             $str .= "</td><td><i>".$data['CLASS']."</i>";
303             $str .= "</td></tr>";
304           }
305         }
307         /* Create variable entries */
308         if(in_array($key,array("FAIvariable"))) {
309           foreach($entry['Entries'] as $cn => $data){
310             $str .="<tr><td style='padding-left:20px;'>";
311             $str .= $image."&nbsp;".$data['cn'][0];
312             if(isset($data['description'][0])){
313               $str .= " [".$data['description'][0]."] ";
314             }
315             /* Only display FAIvariableContent if description dosn't contain [*] */ 
316             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
317               $str .=" = '".$data['FAIvariableContent'][0]."'";
318             }
319             $str .= "</td><td><i>".$data['CLASS']."</i>";
320             $str .="</td></tr>";
321           }
322         }
324         /* Create packagelist entries */ 
325         if(in_array($key,array("FAIpackageList"))) {
326           ksort($entry['Entries']);
327           foreach($entry['Entries'] as $data){
328             $str .="<tr><td style='padding-left:20px;'>";
329             $str .= $image."&nbsp;".$data['cn'][0];
330             $str .= "</td><td><i>".$data['CLASS']."</i>";
331             $str .= "</td></tr>";;
332           }
333         }
335         /* Create partition table entries */
336         if(in_array($key,array("FAIpartitionTable"))) {
337           foreach($entry['Entries'] as $cn => $data){
338             $str .= "<tr><td style='padding-left:20px;'>";
339             $str .= $image."&nbsp;".$data['name'];
340             $str .= "</td><td><i>".$data['CLASS']."</i>";
341             $str .= "</td></tr>";
342             ksort($data['partitions']);
344             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
345             $str .= "<table \""._("Summary of fai classes")."\"
346                         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'>"._("File system 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 ?>