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