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