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