Code

162236676e18903cb4add01832570c66a59ec66d
[gosa.git] / plugins / 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" =>"images/fai_script.png",        "Name"=>_("Scripts")      ,"Tree"     =>"ou=scripts,") ,
45         "FAIhook"                           => array(   "Image" =>"images/fai_hook.png" ,         "Name"=>_("Hooks")        ,"Tree"     =>"ou=hooks,"),
46         "FAIvariable"             => array(     "Image" =>"images/fai_variable.png",      "Name"=>_("Variables")    ,"Tree"     =>"ou=variables,") ,
47         "FAItemplate"             => array(     "Image" =>"images/fai_template.png",      "Name"=>_("Templates")    ,"Tree"     =>"ou=templates,") ,
48         "FAIpartitionTable" => array(   "Image" =>"images/fai_partitionTable.png","Name"=>_("Partition table")  ,"Tree" =>"ou=disk,") ,
49         "FAIpackageList"          => array(     "Image" =>"images/fai_packages.png",      "Name"=>_("Package list") ,"Tree"     =>"ou=packages,") ,
50         "FAIprofile"                => array(   "Image" =>"images/fai_profile.png",       "Name"=>_("Profiles")     ,"Tree"     =>"ou=profiles,"));
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     $_SESSION['faiSummaryTab']['UniqueID'] = 0; 
62   
63     /* Get classes & release name 
64        There are 2 different tabs which use the summary tab
65        faiProfile / Workstations */
66     
67     /* Check if given parent object is fai profile tabs */
68     if(isset($this->parent->by_name['faiProfile'])){
69       
70       /* Get Classes and release */
71       $this->Classes = $this->parent->by_object['faiProfile']->FAIclasses;
72       $this->base    = $this->parent->by_object['faiProfile']->base;
73       $str = preg_replace("/^.*ou=profiles,/","",$this->dn);
74   
75     /* Check if parent tab is work tab */
76     }elseif(isset($this->parent->by_name['workstartup'])){
77       $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
78       $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
80       /* Check if this is a workstation or an object group */
81       if(isset($this->parent->by_object['workgeneric'])){
82         $this->base    = $this->parent->by_object['workgeneric']->base;
83       }elseif(isset($this->parent->by_object['ogroup'])){
84         $this->base    = $this->parent->by_object['ogroup']->base;
85       }else{
86         print_red(_("Unknown type of FAI source information. This is not a profile, workstation nor a ogroup."));
87       }
88       
89       /* Append workstation class && LAST */
90       if(isset($this->parent->by_object['workgeneric']->cn)){
91         $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
92       }
93     }
95     $tmp = array_flip($this->getBranches());
97     $this->Classes[] = "LAST";
99     /* You need full access to complete collection of fai acls to view this summary */
100     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
101     if(!preg_match("/r/",$acl)){
102       $this->Classes = array();
103     }
105     /* Set all available releases */
106     $this->Releases = $tmp;
107     $this->usedClasses = array();
109     /* Check the configuration */
110     if(count($this->Classes) && (!empty($this->Release))){
111       foreach($this->Classes as $class){
112         $this->resolveObject($class);
113       }
114     }
115   }
118   /* Capture posts, and call opnen close tag */
119   function save_object()
120   {
121     $once = true;
123     if(isset($_GET['expand']) && isset($_GET['id'])){
124       if(isset($this->Result[$_GET['id']])){
125         $this->Result[$_GET['id']]['Open'] =true  ;
126       }
127     }
129     if(isset($_GET['compress']) && isset($_GET['id'])){
130       if(isset($this->Result[$_GET['id']])){
131         unset($this->Result[$_GET['id']]['Open']);
132       }
133     }
135     foreach($_POST as $name => $value ){
137       /* check for open tag request */
138       if(preg_match("/^open_/",$name) && $once){
139         $once  = false;
140         $value = preg_replace("/^open_/","",$name);
141         $value = preg_replace("/_.*$/","",$value); 
142         $this->Result[$value]['Open'] =true  ;
143       }
144       /* Check for close tag request */
145       if(preg_match("/^close_/",$name) && $once){
146         $once  = false;
147         $value = preg_replace("/^close_/","",$name);
148         $value = preg_replace("/_.*$/","",$value); 
149         unset($this->Result[$value]['Open']);
150       }
151       if(preg_match("/^reloadList/",$name)){
152         $this->InitCalled = false;
153       }
154     }
155   }
158   /* returns an array with all possibly release types */
159   function getBranches($base = false,$prefix = "")
160   {
161     if(!$base){
162       $base = $this->base;
163     }
164     return(get_all_releases_from_base($base,true));
165   }
168   /* This function calls all nescessary functions to generate the fai class summary tree */
169   function execute() 
170   {
171     /* Check if we must (re)init the this tab */
172     if(!$this->InitCalled){
173       $this->init();
174     }
176     if(isset($_GET['show'])){
177       $ldap = $this->config->get_ldap_link();
178       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplate'));
179       $attrs = $ldap->fetch();
180       if($_GET['type'] == "FAIscript"){
181         $str = $attrs['FAIscript'][0];
182       }elseif($_GET['type'] == "FAIhook"){
183         $str = $attrs['FAIscript'][0];
184       }elseif($_GET['type'] == "FAItemplate"){
185         $str = $attrs['FAItemplateFile'][0];
186       }
187       echo nl2br($str);
188       exit();
189     }
191     /* Get smarty class & assign created summary results */
192     $smarty = get_smarty();
193     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
194     $smarty->assign("readable", preg_match("/r/",$acl));
196     $this->ObjectList = $this->createSummary($this->Result);
197     $smarty->assign("objectList",$this->ObjectList);
198     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
199   }
202   /* Create output of from resolved fai objects */
203   function createSummary($data)
204   {
205     /* Don't generate any output, if there is no result */ 
206     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
207     if(!count($this->Result)){
208       $str.="<tr><td>"; 
209       $str.= (_("This object has no FAI classes assigned."));
210       $str.= "</tr></td></table>";
211       return($str);
212     }
214     /* walk through all classes */      
215     foreach($data as $key => $entry){
216       
217       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
218       if(!isset($entry['Open'])){
219         $str .= "<tr>
220                   <td colspan=2>
221                    <a href='?plug=".$_GET['plug']."&amp;expand&amp;id=".$key."'>
222                     <img border=0 class='center' src='images/expand.png' alt='"._("Open")."'>&nbsp;";
223         $str .=     $image."&nbsp;";
224         $str .= "   <b>".$this->objs[$key]['Name']."</b></a></td></tr>";
225       }else{
226         $str .= "<tr><td colspan=2>
227                     <a href='?plug=".$_GET['plug']."&amp;compress&amp;id=".$key."'>
228                    <img border=0 class='center' src='images/sort_down.png' alt='"._("Close")."'>&nbsp;";
229         $str .= $image."&nbsp;";
230         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
231    
232         /* Display FAItemplate FAIhook FAIscript entries */ 
233         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
235           $nums = array();          
236           $tmp  = array();
237           if($key == "FAIscript"){
238             if(is_array($entry['Entries'])){
239               foreach($entry['Entries'] as $scripts){
240                 foreach($scripts as $script){
242                   if(!isset($nums[$script['cn'][0]])){
243                     $nums[$script['cn'][0]]= 0;
244                   } 
245                   $nums[$script['cn'][0]] ++;
247                   $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
248                 }
249               }
250             }
251             krsort($tmp);;
252             
253             $entry['Entries'] = $tmp;
254           }else{
255             $tmp = array();
256             foreach($entry['Entries'] as $script){
257               $tmp[$script['cn'][0].$script['CLASS']] = $script;
258               if(!isset($nums[$script['cn'][0]])){
259                 $nums[$script['cn'][0]]= 0;
260               } 
261               $nums[$script['cn'][0]] ++;
262             }
263              ksort($tmp);
264             $entry['Entries'] = $tmp;
265           }
267           foreach($entry['Entries'] as $cn => $data){
269             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
270               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
271             }else{
272               $str .="<tr><td style='padding-left:20px;'>";
273             }
275             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&amp;show&amp;type=".$key."&amp;id=".base64_encode($data['dn'])."'>";
276             $str .= $image."&nbsp;";
277             if(isset($data['FAIpriority'][0])){
278               $str .= "(".$data['FAIpriority'][0].")";
279             }
281             $str .= "&nbsp;".$data['cn'][0];
282             if(isset($data['description'][0])){
283               $str .= " [".$data['description'][0]."]";
284             }
285             $str .= "</a>";
286             $str .= "</td><td><i>".$data['CLASS']."</i>";
287             $str .= "</td></tr>";
288           }
289         }
291         /* Create variable entries */
292         if(in_array($key,array("FAIvariable"))) {
293           foreach($entry['Entries'] as $cn => $data){
294             $str .="<tr><td style='padding-left:20px;'>";
295             $str .= $image."&nbsp;".$data['cn'][0];
296             if(isset($data['description'][0])){
297               $str .= " [".$data['description'][0]."] ";
298             }
299             /* Only display FAIvariableContent if description dosn't contain [*] */ 
300             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
301               $str .=" = '".$data['FAIvariableContent'][0]."'";
302             }
303             $str .= "</td><td><i>".$data['CLASS']."</i>";
304             $str .="</td></tr>";
305           }
306         }
308         /* Create packagelist entries */ 
309         if(in_array($key,array("FAIpackageList"))) {
310           ksort($entry['Entries']);
311           foreach($entry['Entries'] as $data){
312             $str .="<tr><td style='padding-left:20px;'>";
313             $str .= $image."&nbsp;".$data['cn'][0];
314             $str .= "</td><td><i>".$data['CLASS']."</i>";
315             $str .= "</td></tr>";;
316           }
317         }
319         /* Create partition table entries */
320         if(in_array($key,array("FAIpartitionTable"))) {
321           foreach($entry['Entries'] as $cn => $data){
322             $str .= "<tr><td style='padding-left:20px;'>";
323             $str .= $image."&nbsp;".$data['name'];
324             $str .= "</td><td><i>".$data['CLASS']."</i>";
325             $str .= "</td></tr>";
326             ksort($data['partitions']);
328             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
329             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
330                       <tr style='background-color:#DBDBDB;'>
331                        <td class='tbhead'>"._("No.")."</td>
332                        <td class='tbhead'>"._("Name")."</td>
333                        <td class='tbhead'>"._("FS options")."</td>
334                        <td class='tbhead'>"._("Mount options")."</td>
335                        <td class='tbhead'>"._("Size in MB")."</td>
336                        <td class='tbhead'>"._("Mount point")."</td>
337                        <td class='tbrhead'>"._("Type")."</td>
338                       </tr>";
340             $i = 0;
341             foreach($data['partitions'] as $key => $part){
342               $i ++;
344               if($i%2 == 1){
345                 $c = " class='rowxp1'";
346                 $d = " class='list1'";
347               }else{
348                 $c = " class='rowxp0'";
349                 $d = " class='list0'";
350               }
352               $str.="<tr $c style='height:10px;'>
353                       <td $d>".$i."</td> 
354                       <td $d>".$part['cn'][0]."</td> 
355                       <td $d>".$part['FAIfsType'][0]."</td> 
356                       <td $d>".$part['FAIfsOptions'][0]."</td> 
357                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
358                       <td $d>".$part['FAImountPoint'][0]."</td> 
359                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
360                     </tr>";
361             }
362             $str .="</table>";
363           }
364         }
365       }
366     }
367     $str .="</table>";  
368     return($str);
369   }
372   /* resolve specified object to append it to our object tree */
373   function resolveObject($class)
374   {
375     $ldap                 = $this->config->get_ldap_link();
376     $dn                     = $this->Releases [$this->Release];
377     $resolvedClasses  = get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
379     /* Check acls -> are we allowed to view the config summary ? */
380     if(!$this->acl_is_readable("readable")) {
381       return;
382     }
384     /* Try to fetch all types of fai objects with the given cn  */
385     foreach($resolvedClasses as $obj){
387       $dn = $obj['dn'];
388       $ldap->cat($dn);
390       while($attrs = $ldap->fetch()){
392         foreach($this-> objs as $key => $rest){
393           if(in_array($key,$attrs['objectClass'])){
395             if(!isset($this->usedClasses[$key][$class])){
396               $this->usedClasses[$key][$class] = true;
397               switch($key){
398                 case "FAIprofile":                      $this->prepare_FAIprofile($attrs);break;
399                 case "FAIscript":                       $this->prepare_FAIscript($attrs);break;
400                 case "FAIhook":                           $this->prepare_FAIhook($attrs);break;
401                 case "FAIvariable":                     $this->prepare_FAIvariable($attrs);break;
402                 case "FAItemplate":                     $this->prepare_FAItemplate($attrs);break;
403                 case "FAIpackageList":        $this->prepare_FAIpackageList($attrs);break;
404                 case "FAIpartitionTable":           $this->prepare_FAIpartitionTable($attrs);break;
405               }
406             }
408           }
409         }
410       }
411     }
412   }
414   /* Prepare fai script */
415   function prepare_FAIscript($data)
416   {
417     if(isset($this->Result['FAIscript']['Entries'])){
418       $current = $this->Result['FAIscript']['Entries'];
419     }else{
420       $current = array();
421     }
422     $ldap = $this->config->get_ldap_link();
423     $ldap->cd($data['dn']);
424     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
425     while($attrs = $ldap->fetch()){
426       $attrs['CLASS'] = $data['cn'][0];
427       $current[$attrs['FAIpriority'][0]][] = $attrs;
428     }
429     $this->Result['FAIscript']['Entries'] = $current ;
430   }
433   /* Prepare fai script */
434   function prepare_FAIpartitionTable($data)
435   {
436     if(isset($this->Result['FAIpartitionTable']['Entries'])){
437       $current = $this->Result['FAIpartitionTable']['Entries'];
438     }else{
439       $current = array();
440     }
442     /* get subentries */
443     $ldap = $this->config->get_ldap_link();
444     $ldap->cd($data['dn']);
445     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
446     $disks = array();
448     /* Create an array which is sortable by priority */
449     while($attrs = $ldap->fetch()){
450       $attrs['CLASS'] = $data['cn'][0];
451       $disks[$attrs['cn'][0]]= $attrs;
452       if(isset($attrs['description'][0])){
453         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
454       }else{
455         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
456       }
457       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
458       $disks[$attrs['cn'][0]]['partitions'] = array();
459     }
461     /* Sort by priority */
462     foreach($disks as $key => $disk){
463       $ldap->cd($disk['dn']);
464       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
465       while($attrs = $ldap->fetch()){
466         if(!isset($attrs['FAIfsOptions'][0])){
467           $attrs['FAIfsOptions'][0] = "";
468         }
469         $attrs['CLASS'] = $data['cn'][0];
470         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
471       }
472     }
473     $this->Result['FAIpartitionTable']['Entries'] = $disks;
474   }
477   /* Create template entry */   
478   function prepare_FAItemplate($data)
479   {
480     $current = array();
481     if(isset($this->Result['FAItemplate']['Entries'])){
482       $current = $this->Result['FAItemplate']['Entries'];
483     }
485     $ldap = $this->config->get_ldap_link();
486     $ldap->cd($data['dn']);
487     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
488     while($attrs = $ldap->fetch()){
489       $attrs['CLASS'] = $data['cn'][0];
490       $current[] = $attrs;
491     }
492     $this->Result['FAItemplate']['Entries'] = $current ;
493   }
496   /* Create a package list  */  
497   function prepare_FAIpackageList($data)
498   {
499     $current = array();
500     if(isset($this->Result['FAIpackageList']['Entries'])){
501       $current = $this->Result['FAIpackageList']['Entries'];
502     }
503     if(isset($data['FAIpackage'])){
504       unset($data['FAIpackage']['count']);
505       foreach($data['FAIpackage'] as $pkg){
506         $attrs['CLASS'] = $data['cn'][0];
507         $attrs['cn'][0] = $pkg;
508         $current[$pkg] = $attrs;
509       }
510     }
511     $this->Result['FAIpackageList']['Entries'] = $current ;
512   }
515   /* Create a variable entry */ 
516   function prepare_FAIvariable($data)
517   {
518     $current = array();
519     if(isset($this->Result['FAIvariable']['Entries'])){
520       $current = $this->Result['FAIvariable']['Entries'];
521     }
522     $ldap = $this->config->get_ldap_link();
523     $ldap->cd($data['dn']);
524     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
525     while($attrs = $ldap->fetch()){
526       $attrs['CLASS'] = $data['cn'][0];
527       $current[] = $attrs;
528     }
529     $this->Result['FAIvariable']['Entries'] = $current ;
530   }
532   /* Create a hook entry */     
533   function prepare_FAIhook($data)
534   {
535     $current = array();
536     if(isset($this->Result['FAIhook']['Entries'])){
537       $current = $this->Result['FAIhook']['Entries'];
538     }
539     $ldap = $this->config->get_ldap_link();
540     $ldap->cd($data['dn']);
541     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
542     while($attrs = $ldap->fetch()){
543       $attrs['CLASS'] = $data['cn'][0];
544       $current[$attrs['cn'][0]] = $attrs;
545     }
546     $this->Result['FAIhook']['Entries'] = $current ;
547   }
550   /* Create a new Profile entry */
551   function prepare_FAIprofile($data)
552   {
553     $classes = split("\ ",$data['FAIclass'][0]);
554     foreach($classes as $class){
555       $class = trim($class);
556       $this->resolveObject($class);
557     }
558   }
560   /* Return plugin informations for acl handling * /
561   function plInfo()
562   {
563     return (array(
564           "plShortName"   => _("Summary"),
565           "plDescription" => _("FAI summary"),
566           "plSelfModify"  => FALSE,
567           "plDepends"     => array(),
568           "plPriority"    => 15,
569           "plSection"     => array("administration"),
570           "plCategory"    => array("workstation","server","fai","ogroups") ,
571           "plProvidedAcls"=> array(
572             "readable" => _("Viewable")),
573           ));
574   }
575   */
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>