Code

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