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