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