Code

Added a first set of reference changes - nearly untested
[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   {
172     /* Skip this if fai is deactivated */
173     $tmp = search_config($this->config->data,"faiManagement","CLASS");
174     if(empty($tmp)){
175       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
176       return $str;
177     }    
179     /* Check if we must (re)init the this tab */
180     if(!$this->InitCalled){
181       $this->init();
182     }
184     if(isset($_GET['show'])){
185       $ldap = $this->config->get_ldap_link();
186       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplate'));
187       $attrs = $ldap->fetch();
188       if($_GET['type'] == "FAIscript"){
189         $str = $attrs['FAIscript'][0];
190       }elseif($_GET['type'] == "FAIhook"){
191         $str = $attrs['FAIscript'][0];
192       }elseif($_GET['type'] == "FAItemplate"){
193         $str = $attrs['FAItemplateFile'][0];
194       }
195       echo nl2br($str);
196       exit();
197     }
199     /* Get smarty class & assign created summary results */
200     $smarty = get_smarty();
201     $acl = $this->ui->has_complete_category_acls($this->base,"fai");
202     $smarty->assign("readable", preg_match("/r/",$acl));
204     $this->ObjectList = $this->createSummary($this->Result);
205     $smarty->assign("objectList",$this->ObjectList);
206     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
207   }
210   /* Create output of from resolved fai objects */
211   function createSummary($data)
212   {
213     /* Don't generate any output, if there is no result */ 
214     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
215     if(!count($this->Result)){
216       $str.="<tr><td>"; 
217       $str.= (_("This object has no FAI classes assigned."));
218       $str.= "</tr></td></table>";
219       return($str);
220     }
222     /* walk through all classes */      
223     foreach($data as $key => $entry){
224       
225       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
226       if(!isset($entry['Open'])){
227         $str .= "<tr>
228                   <td colspan=2>
229                    <a href='?plug=".$_GET['plug']."&amp;expand&amp;id=".$key."'>
230                     <img border=0 class='center' src='images/expand.png' alt='"._("Open")."'>&nbsp;";
231         $str .=     $image."&nbsp;";
232         $str .= "   <b>".$this->objs[$key]['Name']."</b></a></td></tr>";
233       }else{
234         $str .= "<tr><td colspan=2>
235                     <a href='?plug=".$_GET['plug']."&amp;compress&amp;id=".$key."'>
236                    <img border=0 class='center' src='images/sort_down.png' alt='"._("Close")."'>&nbsp;";
237         $str .= $image."&nbsp;";
238         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
239    
240         /* Display FAItemplate FAIhook FAIscript entries */ 
241         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
243           $nums = array();          
244           $tmp  = array();
245           if($key == "FAIscript"){
246             if(is_array($entry['Entries'])){
247               foreach($entry['Entries'] as $scripts){
248                 foreach($scripts as $script){
250                   if(!isset($nums[$script['cn'][0]])){
251                     $nums[$script['cn'][0]]= 0;
252                   } 
253                   $nums[$script['cn'][0]] ++;
255                   $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
256                 }
257               }
258             }
259             krsort($tmp);;
260             
261             $entry['Entries'] = $tmp;
262           }else{
263             $tmp = array();
264             foreach($entry['Entries'] as $script){
265               $tmp[$script['cn'][0].$script['CLASS']] = $script;
266               if(!isset($nums[$script['cn'][0]])){
267                 $nums[$script['cn'][0]]= 0;
268               } 
269               $nums[$script['cn'][0]] ++;
270             }
271              ksort($tmp);
272             $entry['Entries'] = $tmp;
273           }
275           foreach($entry['Entries'] as $cn => $data){
277             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
278               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
279             }else{
280               $str .="<tr><td style='padding-left:20px;'>";
281             }
283             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&amp;show&amp;type=".$key."&amp;id=".base64_encode($data['dn'])."'>";
284             $str .= $image."&nbsp;";
285             if(isset($data['FAIpriority'][0])){
286               $str .= "(".$data['FAIpriority'][0].")";
287             }
289             $str .= "&nbsp;".$data['cn'][0];
290             if(isset($data['description'][0])){
291               $str .= " [".$data['description'][0]."]";
292             }
293             $str .= "</a>";
294             $str .= "</td><td><i>".$data['CLASS']."</i>";
295             $str .= "</td></tr>";
296           }
297         }
299         /* Create variable entries */
300         if(in_array($key,array("FAIvariable"))) {
301           foreach($entry['Entries'] as $cn => $data){
302             $str .="<tr><td style='padding-left:20px;'>";
303             $str .= $image."&nbsp;".$data['cn'][0];
304             if(isset($data['description'][0])){
305               $str .= " [".$data['description'][0]."] ";
306             }
307             /* Only display FAIvariableContent if description dosn't contain [*] */ 
308             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
309               $str .=" = '".$data['FAIvariableContent'][0]."'";
310             }
311             $str .= "</td><td><i>".$data['CLASS']."</i>";
312             $str .="</td></tr>";
313           }
314         }
316         /* Create packagelist entries */ 
317         if(in_array($key,array("FAIpackageList"))) {
318           ksort($entry['Entries']);
319           foreach($entry['Entries'] as $data){
320             $str .="<tr><td style='padding-left:20px;'>";
321             $str .= $image."&nbsp;".$data['cn'][0];
322             $str .= "</td><td><i>".$data['CLASS']."</i>";
323             $str .= "</td></tr>";;
324           }
325         }
327         /* Create partition table entries */
328         if(in_array($key,array("FAIpartitionTable"))) {
329           foreach($entry['Entries'] as $cn => $data){
330             $str .= "<tr><td style='padding-left:20px;'>";
331             $str .= $image."&nbsp;".$data['name'];
332             $str .= "</td><td><i>".$data['CLASS']."</i>";
333             $str .= "</td></tr>";
334             ksort($data['partitions']);
336             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
337             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
338                       <tr style='background-color:#DBDBDB;'>
339                        <td class='tbhead'>"._("No.")."</td>
340                        <td class='tbhead'>"._("Name")."</td>
341                        <td class='tbhead'>"._("FS options")."</td>
342                        <td class='tbhead'>"._("Mount options")."</td>
343                        <td class='tbhead'>"._("Size in MB")."</td>
344                        <td class='tbhead'>"._("Mount point")."</td>
345                        <td class='tbrhead'>"._("Type")."</td>
346                       </tr>";
348             $i = 0;
349             foreach($data['partitions'] as $key => $part){
350               $i ++;
352               if($i%2 == 1){
353                 $c = " class='rowxp1'";
354                 $d = " class='list1'";
355               }else{
356                 $c = " class='rowxp0'";
357                 $d = " class='list0'";
358               }
360               $str.="<tr $c style='height:10px;'>
361                       <td $d>".$i."</td> 
362                       <td $d>".$part['cn'][0]."</td> 
363                       <td $d>".$part['FAIfsType'][0]."</td> 
364                       <td $d>".$part['FAIfsOptions'][0]."</td> 
365                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
366                       <td $d>".$part['FAImountPoint'][0]."</td> 
367                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
368                     </tr>";
369             }
370             $str .="</table>";
371           }
372         }
373       }
374     }
375     $str .="</table>";  
376     return($str);
377   }
380   /* resolve specified object to append it to our object tree */
381   function resolveObject($class)
382   {
383     $ldap                 = $this->config->get_ldap_link();
384     $dn                     = $this->Releases [$this->Release];
385     $resolvedClasses  = get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
387     /* Try to fetch all types of fai objects with the given cn  */
388     foreach($resolvedClasses as $obj){
390       $dn = $obj['dn'];
391       $ldap->cat($dn);
393       while($attrs = $ldap->fetch()){
395         foreach($this-> objs as $key => $rest){
396           if(in_array($key,$attrs['objectClass'])){
398             if(!isset($this->usedClasses[$key][$class])){
399               $this->usedClasses[$key][$class] = true;
400               switch($key){
401                 case "FAIprofile":                      $this->prepare_FAIprofile($attrs);break;
402                 case "FAIscript":                       $this->prepare_FAIscript($attrs);break;
403                 case "FAIhook":                           $this->prepare_FAIhook($attrs);break;
404                 case "FAIvariable":                     $this->prepare_FAIvariable($attrs);break;
405                 case "FAItemplate":                     $this->prepare_FAItemplate($attrs);break;
406                 case "FAIpackageList":        $this->prepare_FAIpackageList($attrs);break;
407                 case "FAIpartitionTable":           $this->prepare_FAIpartitionTable($attrs);break;
408               }
409             }
411           }
412         }
413       }
414     }
415   }
417   /* Prepare fai script */
418   function prepare_FAIscript($data)
419   {
420     if(isset($this->Result['FAIscript']['Entries'])){
421       $current = $this->Result['FAIscript']['Entries'];
422     }else{
423       $current = array();
424     }
425     $ldap = $this->config->get_ldap_link();
426     $ldap->cd($data['dn']);
427     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
428     while($attrs = $ldap->fetch()){
429       $attrs['CLASS'] = $data['cn'][0];
430       $current[$attrs['FAIpriority'][0]][] = $attrs;
431     }
432     $this->Result['FAIscript']['Entries'] = $current ;
433   }
436   /* Prepare fai script */
437   function prepare_FAIpartitionTable($data)
438   {
439     if(isset($this->Result['FAIpartitionTable']['Entries'])){
440       $current = $this->Result['FAIpartitionTable']['Entries'];
441     }else{
442       $current = array();
443     }
445     /* get subentries */
446     $ldap = $this->config->get_ldap_link();
447     $ldap->cd($data['dn']);
448     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
449     $disks = array();
451     /* Create an array which is sortable by priority */
452     while($attrs = $ldap->fetch()){
453       $attrs['CLASS'] = $data['cn'][0];
454       $disks[$attrs['cn'][0]]= $attrs;
455       if(isset($attrs['description'][0])){
456         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
457       }else{
458         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
459       }
460       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
461       $disks[$attrs['cn'][0]]['partitions'] = array();
462     }
464     /* Sort by priority */
465     foreach($disks as $key => $disk){
466       $ldap->cd($disk['dn']);
467       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
468       while($attrs = $ldap->fetch()){
469         if(!isset($attrs['FAIfsOptions'][0])){
470           $attrs['FAIfsOptions'][0] = "";
471         }
472         $attrs['CLASS'] = $data['cn'][0];
473         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
474       }
475     }
476     $this->Result['FAIpartitionTable']['Entries'] = $disks;
477   }
480   /* Create template entry */   
481   function prepare_FAItemplate($data)
482   {
483     $current = array();
484     if(isset($this->Result['FAItemplate']['Entries'])){
485       $current = $this->Result['FAItemplate']['Entries'];
486     }
488     $ldap = $this->config->get_ldap_link();
489     $ldap->cd($data['dn']);
490     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
491     while($attrs = $ldap->fetch()){
492       $attrs['CLASS'] = $data['cn'][0];
493       $current[] = $attrs;
494     }
495     $this->Result['FAItemplate']['Entries'] = $current ;
496   }
499   /* Create a package list  */  
500   function prepare_FAIpackageList($data)
501   {
502     $current = array();
503     if(isset($this->Result['FAIpackageList']['Entries'])){
504       $current = $this->Result['FAIpackageList']['Entries'];
505     }
506     if(isset($data['FAIpackage'])){
507       unset($data['FAIpackage']['count']);
508       foreach($data['FAIpackage'] as $pkg){
509         $attrs['CLASS'] = $data['cn'][0];
510         $attrs['cn'][0] = $pkg;
511         $current[$pkg] = $attrs;
512       }
513     }
514     $this->Result['FAIpackageList']['Entries'] = $current ;
515   }
518   /* Create a variable entry */ 
519   function prepare_FAIvariable($data)
520   {
521     $current = array();
522     if(isset($this->Result['FAIvariable']['Entries'])){
523       $current = $this->Result['FAIvariable']['Entries'];
524     }
525     $ldap = $this->config->get_ldap_link();
526     $ldap->cd($data['dn']);
527     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
528     while($attrs = $ldap->fetch()){
529       $attrs['CLASS'] = $data['cn'][0];
530       $current[] = $attrs;
531     }
532     $this->Result['FAIvariable']['Entries'] = $current ;
533   }
535   /* Create a hook entry */     
536   function prepare_FAIhook($data)
537   {
538     $current = array();
539     if(isset($this->Result['FAIhook']['Entries'])){
540       $current = $this->Result['FAIhook']['Entries'];
541     }
542     $ldap = $this->config->get_ldap_link();
543     $ldap->cd($data['dn']);
544     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
545     while($attrs = $ldap->fetch()){
546       $attrs['CLASS'] = $data['cn'][0];
547       $current[$attrs['cn'][0]] = $attrs;
548     }
549     $this->Result['FAIhook']['Entries'] = $current ;
550   }
553   /* Create a new Profile entry */
554   function prepare_FAIprofile($data)
555   {
556     $classes = split("\ ",$data['FAIclass'][0]);
557     foreach($classes as $class){
558       $class = trim($class);
559       $this->resolveObject($class);
560     }
561   }
563   /* Return plugin informations for acl handling * /
564   function plInfo()
565   {
566     return (array(
567           "plShortName"   => _("Summary"),
568           "plDescription" => _("FAI summary"),
569           "plSelfModify"  => FALSE,
570           "plDepends"     => array(),
571           "plPriority"    => 15,
572           "plSection"     => array("administration"),
573           "plCategory"    => array("workstation","server","fai","ogroups") ,
574           "plProvidedAcls"=> array(
575             "readable" => _("Viewable")),
576           ));
577   }
578   */
580 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
581 ?>