Code

40d3981977bfa489e07a77fb858bc4159c235664
[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   
33   function faiSummaryTab($config,$dn)
34   {
35     plugin::plugin($config,$dn);
37     /* Base object configuration */
38     $this->objs = array(        
39         "FAIscript"                 => array(   "Image" =>"images/fai_script.png",        "Name"=>_("Scripts")      ,"Tree"     =>"ou=scripts,") ,
40         "FAIhook"                           => array(   "Image" =>"images/fai_hook.png" ,         "Name"=>_("Hooks")        ,"Tree"     =>"ou=hooks,"),
41         "FAIvariable"             => array(     "Image" =>"images/fai_variable.png",      "Name"=>_("Variables")    ,"Tree"     =>"ou=variables,") ,
42         "FAItemplate"             => array(     "Image" =>"images/fai_template.png",      "Name"=>_("Templates")    ,"Tree"     =>"ou=templates,") ,
43         "FAIpartitionTable" => array(   "Image" =>"images/fai_partitionTable.png","Name"=>_("Partition table")  ,"Tree" =>"ou=disk,") ,
44         "FAIpackageList"          => array(     "Image" =>"images/fai_packages.png",      "Name"=>_("Package list") ,"Tree"     =>"ou=packages,") ,
45         "FAIprofile"                => array(   "Image" =>"images/fai_profile.png",       "Name"=>_("Profiles")     ,"Tree"     =>"ou=profiles,"));
46   }
49   /* Init the fai tree with objects.
50      If this method is called, all assigned classes and the release name 
51       will be read from parent object. */
52   function init()
53   {
54     $this->InitCalled = true;
55     $this->Result = array();
56     $_SESSION['faiSummaryTab']['UniqueID'] = 0; 
57     $tmp = $this->getBranches();
58     
59     /* Get classes & release name 
60        There are 2 different tabs which use the summary tab
61        faiProfile / Workstations */
62     
63     /* Check if given parent object is fai profile tabs */
64     if(isset($this->parent->by_name['faiProfile'])){
65       
66       /* Get Classes and release */
67       $this->Classes = $this->parent->by_object['faiProfile']->FAIclasses;
68       $str = preg_replace("/^.*ou=profiles,/","",$this->dn);
69       if(isset($tmp[$str])){
70         $this->Release = $tmp[$str];
71       }
72   
73     /* Check if parent tab is work tab */
74     }elseif(isset($this->parent->by_name['workstartup'])){
75       $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
76       $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
77       $tmp = array_flip($tmp);
78       if(!isset($tmp[$this->Release])) {
79         $this->Release = "";
80       }
81     }
83     /* Set all available releases */
84     $this->Releases = array_flip($this->getBranches());
85     $this->usedClasses = array();
87     /* Check the configuration */
88     if(count($this->Classes) && (!empty($this->Release))){
89       foreach($this->Classes as $class){
90         $this->resolveObject($class);
91       }
92     }
93   }
96   /* Capture posts, and call opnen close tag */
97   function save_object()
98   {
99     $once = true;
101     if(isset($_GET['expand']) && isset($_GET['id'])){
102       if(isset($this->Result[$_GET['id']])){
103         $this->Result[$_GET['id']]['Open'] =true  ;
104       }
105     }
107     if(isset($_GET['compress']) && isset($_GET['id'])){
108       if(isset($this->Result[$_GET['id']])){
109         unset($this->Result[$_GET['id']]['Open']);
110       }
111     }
113     foreach($_POST as $name => $value ){
115       /* check for open tag request */
116       if(preg_match("/^open_/",$name) && $once){
117         $once  = false;
118         $value = preg_replace("/^open_/","",$name);
119         $value = preg_replace("/_.*$/","",$value); 
120         $this->Result[$value]['Open'] =true  ;
121       }
122       /* Check for close tag request */
123       if(preg_match("/^close_/",$name) && $once){
124         $once  = false;
125         $value = preg_replace("/^close_/","",$name);
126         $value = preg_replace("/_.*$/","",$value); 
127         unset($this->Result[$value]['Open']);
128       }
129       if(preg_match("/^reloadList/",$name)){
130         $this->InitCalled = false;
131       }
132     }
133   }
136   /* returns an array with all possibly release types */
137   function getBranches($base = false,$prefix = "")
138   {
139     $ret = array("main"=>"/");
140     $ldap = $this->config->get_ldap_link();
141     $ldap->cd($this->config->current['BASE']);
142     if(!$base){
143       $base= "ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
144     }
145     $ldap->cd($base);
146     $ldap->ls("(objectClass=FAIbranch)",$base);
147     while($attrs = $ldap->fetch()){
148       if((isset($attrs['ou']))&&($attrs['dn']!=$base)){
149         $ret[$attrs['dn']]= $prefix.$attrs['ou'][0];
150         $ret = array_merge($ret,$this->getBranches($attrs['dn'],"".$prefix.$attrs['ou'][0]."/"));
151       }
152     }
153     return($ret);
154   }
157   /* This function calls all nescessary functions to generate the fai class summary tree */
158   function execute() 
159   {
160     /* Check if we must (re)init the this tab */
161     if(!$this->InitCalled){
162       $this->init();
163     }
165     if(isset($_GET['show'])){
166       $ldap = $this->config->get_ldap_link();
167       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplate'));
168       $attrs = $ldap->fetch();
169       if($_GET['type'] == "FAIscript"){
170         $str = $attrs['FAIscript'][0];
171       }elseif($_GET['type'] == "FAIhook"){
172         $str = $attrs['FAIscript'][0];
173       }elseif($_GET['type'] == "FAItemplate"){
174         $str = $attrs['FAItemplateFile'][0];
175       }
176       echo nl2br($str);
177       exit();
178     }
180     /* Get smarty class & assign created summary results */
181     $smarty = get_smarty();
182     $this->ObjectList = $this->createSummary($this->Result);
183     $smarty->assign("objectList",$this->ObjectList);
184     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
185   }
188   /* Create output of from resolved fai objects */
189   function createSummary($data)
190   {
191     /* Don't generate any output, if there is no result */ 
192     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
193     if(!count($this->Result)){
194       $str.="<tr><td>"; 
195       $str.= (_("This object has no FAI classes assigned."));
196       $str.= "</tr></td></table>";
197       return($str);
198     }
200     /* walk through all classes */      
201     foreach($data as $key => $entry){
202       
203       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
204       if(!isset($entry['Open'])){
205         $str .= "<tr><td colspan=2>
206                     <a href='?plug=".$_GET['plug']."&expand&id=".$key."'>
207                   <img border=0 class='center' src='images/expand.png' alt='"._("Open")."'>&nbsp;";
208         $str .= $image."&nbsp;";
209         $str .= "<b>".$this->objs[$key]['Name']."</b></b></td></tr>";
210       }else{
211         $str .= "<tr><td colspan=2>
212                     <a href='?plug=".$_GET['plug']."&compress&id=".$key."'>
213                    <img border=0 class='center' src='images/sort_down.png' alt='"._("Close")."'>&nbsp;";
214         $str .= $image."&nbsp;";
215         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
216    
217         /* Display FAItemplate FAIhook FAIscript entries */ 
218         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
220           $nums = array();          
222           if($key == "FAIscript"){
223             foreach($entry['Entries'] as $scripts){
224               foreach($scripts as $script){
225                 
226                 if(!isset($nums[$script['cn'][0]])){
227                   $nums[$script['cn'][0]]= 0;
228                 } 
229                 $nums[$script['cn'][0]] ++;
230             
231                 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
232               }
233             }
234             krsort($tmp);;
235             
236             $entry['Entries'] = $tmp;
237           }else{
238             $tmp = array();
239             foreach($entry['Entries'] as $script){
240               $tmp[$script['cn'][0].$script['CLASS']] = $script;
241               if(!isset($nums[$script['cn'][0]])){
242                 $nums[$script['cn'][0]]= 0;
243               } 
244               $nums[$script['cn'][0]] ++;
245             }
246              ksort($tmp);
247             $entry['Entries'] = $tmp;
248           }
250           foreach($entry['Entries'] as $cn => $data){
252             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
253               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
254             }else{
255               $str .="<tr><td style='padding-left:20px;'>";
256             }
258             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
259             $str .= $image."&nbsp;";
260             if(isset($data['FAIpriority'][0])){
261               $str .= "(".$data['FAIpriority'][0].")";
262             }
264             $str .= "&nbsp;".$data['cn'][0];
265             if(isset($data['description'][0])){
266               $str .= " [".$data['description'][0]."]";
267             }
268             $str .= "</a>";
269             $str .= "</td><td><i>".$data['CLASS']."</i>";
270             $str .= "</td></tr>";
271           }
272         }
274         /* Create variable entries */
275         if(in_array($key,array("FAIvariable"))) {
276           foreach($entry['Entries'] as $cn => $data){
277             $str .="<tr><td style='padding-left:20px;'>";
278             $str .= $image."&nbsp;".$data['cn'][0];
279             if(isset($data['description'][0])){
280               $str .= " [".$data['description'][0]."] ";
281             }
282             /* Only display FAIvariableContent if description dosn't contain [*] */ 
283             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
284               $str .=" = '".$data['FAIvariableContent'][0]."'";
285             }
286             $str .= "</td><td><i>".$data['CLASS']."</i>";
287             $str .="</td></tr>";
288           }
289         }
291         /* Create packagelist entries */ 
292         if(in_array($key,array("FAIpackageList"))) {
293           ksort($entry['Entries']);
294           foreach($entry['Entries'] as $data){
295             $str .="<tr><td style='padding-left:20px;'>";
296             $str .= $image."&nbsp;".$data['cn'][0];
297             $str .= "</td><td><i>".$data['CLASS']."</i>";
298             $str .= "</td></tr>";;
299           }
300         }
302         /* Create partition table entries */
303         if(in_array($key,array("FAIpartitionTable"))) {
304           foreach($entry['Entries'] as $cn => $data){
305             $str .= "<tr><td style='padding-left:20px;'>";
306             $str .= $image."&nbsp;".$data['name'];
307             $str .= "</td><td><i>".$data['CLASS']."</i>";
308             $str .= "</td></tr>";
309             ksort($data['partitions']);
311             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
312             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
313                       <tr style='background-color:#DBDBDB;'>
314                        <td class='tbhead'>"._("No.")."</td>
315                        <td class='tbhead'>"._("Name")."</td>
316                        <td class='tbhead'>"._("FS options")."</td>
317                        <td class='tbhead'>"._("Mount options")."</td>
318                        <td class='tbhead'>"._("Size in MB")."</td>
319                        <td class='tbhead'>"._("Mount point")."</td>
320                        <td class='tbrhead'>"._("Type")."</td>
321                       </tr>";
323             $i = 0;
324             foreach($data['partitions'] as $key => $part){
325               $i ++;
327               if($i%2 == 1){
328                 $c = " class='rowxp1'";
329                 $d = " class='list1'";
330               }else{
331                 $c = " class='rowxp0'";
332                 $d = " class='list0'";
333               }
335               $str.="<tr $c style='height:10px;'>
336                       <td $d>".$i."</td> 
337                       <td $d>".$part['cn'][0]."</td> 
338                       <td $d>".$part['FAIfsType'][0]."</td> 
339                       <td $d>".$part['FAIfsOptions'][0]."</td> 
340                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
341                       <td $d>".$part['FAImountPoint'][0]."</td> 
342                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
343                     </tr>";
344             }
345             $str .="</table>";
346           }
347         }
348       }
349     }
350     $str .="</table>";  
351     return($str);
352   }
355   /* resolve specified object to append it to our object tree */
356   function resolveObject($class)
357   {
358     $ldap       = $this->config->get_ldap_link();
359     $dn           = $this->Releases [$this->Release];
360     $res    = array();
361  
362     /* Try to fetch all types of fai objects with the given cn  */
363     foreach($this->objs as $key =>  $obj){
364       
365       /* Check for objects with cn=$class 
366           and add them to our list */
367       $ldap->ls("(&(objectClass=".$key.")(cn=".$class."))",$obj['Tree'].$dn,array("*"));
368       while($attrs = $ldap->fetch()){
370         if(!isset($this->usedClasses[$key][$class])){
371           $this->usedClasses[$key][$class] = true;
372           switch($key){
373             case "FAIprofile":                  $this->prepare_FAIprofile($attrs);break;
374             case "FAIscript":                   $this->prepare_FAIscript($attrs);break;
375             case "FAIhook":                       $this->prepare_FAIhook($attrs);break;
376             case "FAIvariable":                 $this->prepare_FAIvariable($attrs);break;
377             case "FAItemplate":                 $this->prepare_FAItemplate($attrs);break;
378             case "FAIpackageList":            $this->prepare_FAIpackageList($attrs);break;
379             case "FAIpartitionTable":       $this->prepare_FAIpartitionTable($attrs);break;
380           }
381         }
382       }
383     }
384     return($res);
385   }
388   /* Prepare fai script */
389   function prepare_FAIscript($data)
390   {
391     if(isset($this->Result['FAIscript']['Entries'])){
392       $current = $this->Result['FAIscript']['Entries'];
393     }else{
394       $current = array();
395     }
396     $ldap = $this->config->get_ldap_link();
397     $ldap->cd($data['dn']);
398     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
399     while($attrs = $ldap->fetch()){
400       $attrs['CLASS'] = $data['cn'][0];
401       $current[$attrs['FAIpriority'][0]][] = $attrs;
402     }
403     $this->Result['FAIscript']['Entries'] = $current ;
404   }
407   /* Prepare fai script */
408   function prepare_FAIpartitionTable($data)
409   {
410     if(isset($this->Result['FAIpartitionTable']['Entries'])){
411       $current = $this->Result['FAIpartitionTable']['Entries'];
412     }else{
413       $current = array();
414     }
416     /* get subentries */
417     $ldap = $this->config->get_ldap_link();
418     $ldap->cd($data['dn']);
419     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
420     $disks = array();
422     /* Create an array which is sortable by priority */
423     while($attrs = $ldap->fetch()){
424       $attrs['CLASS'] = $data['cn'][0];
425       $disks[$attrs['cn'][0]]= $attrs;
426       if(isset($attrs['description'][0])){
427         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
428       }else{
429         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
430       }
431       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
432       $disks[$attrs['cn'][0]]['partitions'] = array();
433     }
435     /* Sort by priority */
436     foreach($disks as $key => $disk){
437       $ldap->cd($disk['dn']);
438       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
439       while($attrs = $ldap->fetch()){
440         if(!isset($attrs['FAIfsOptions'][0])){
441           $attrs['FAIfsOptions'][0] = "";
442         }
443         $attrs['CLASS'] = $data['cn'][0];
444         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
445       }
446     }
447     $this->Result['FAIpartitionTable']['Entries'] = $disks;
448   }
451   /* Create template entry */   
452   function prepare_FAItemplate($data)
453   {
454     $current = array();
455     if(isset($this->Result['FAItemplate']['Entries'])){
456       $current = $this->Result['FAItemplate']['Entries'];
457     }
459     $ldap = $this->config->get_ldap_link();
460     $ldap->cd($data['dn']);
461     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
462     while($attrs = $ldap->fetch()){
463       $attrs['CLASS'] = $data['cn'][0];
464       $current[] = $attrs;
465     }
466     $this->Result['FAItemplate']['Entries'] = $current ;
467   }
470   /* Create a package list  */  
471   function prepare_FAIpackageList($data)
472   {
473     $current = array();
474     if(isset($this->Result['FAIpackageList']['Entries'])){
475       $current = $this->Result['FAIpackageList']['Entries'];
476     }
477     if(isset($data['FAIpackage'])){
478       unset($data['FAIpackage']['count']);
479       foreach($data['FAIpackage'] as $pkg){
480         $attrs['CLASS'] = $data['cn'][0];
481         $attrs['cn'][0] = $pkg;
482         $current[$pkg] = $attrs;
483       }
484     }
485     $this->Result['FAIpackageList']['Entries'] = $current ;
486   }
489   /* Create a variable entry */ 
490   function prepare_FAIvariable($data)
491   {
492     $current = array();
493     if(isset($this->Result['FAIvariable']['Entries'])){
494       $current = $this->Result['FAIvariable']['Entries'];
495     }
496     $ldap = $this->config->get_ldap_link();
497     $ldap->cd($data['dn']);
498     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
499     while($attrs = $ldap->fetch()){
500       $attrs['CLASS'] = $data['cn'][0];
501       $current[] = $attrs;
502     }
503     $this->Result['FAIvariable']['Entries'] = $current ;
504   }
506   /* Create a hook entry */     
507   function prepare_FAIhook($data)
508   {
509     $current = array();
510     if(isset($this->Result['FAIhook']['Entries'])){
511       $current = $this->Result['FAIhook']['Entries'];
512     }
513     $ldap = $this->config->get_ldap_link();
514     $ldap->cd($data['dn']);
515     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
516     while($attrs = $ldap->fetch()){
517       $attrs['CLASS'] = $data['cn'][0];
518       $current[$attrs['cn'][0]] = $attrs;
519     }
520     $this->Result['FAIhook']['Entries'] = $current ;
521   }
524   /* Create a new Profile entry */
525   function prepare_FAIprofile($data)
526   {
527     $classes = split("\ ",$data['FAIclass'][0]);
528     foreach($classes as $class){
529       $class = trim($class);
530       $this->resolveObject($class);
531     }
532   }
534 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
535 ?>