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 }else{
86 print_red(_("Unknown type of FAI source information. This is not a profile, workstation nor a ogroup."));
87 }
89 /* Append workstation class && LAST */
90 if(isset($this->parent->by_object['workgeneric']->cn)){
91 $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
92 }
93 }
95 $tmp = array_flip($this->getBranches());
97 $this->Classes[] = "LAST";
99 /* You need full access to complete collection of fai acls to view this summary */
100 $acl = $this->ui->has_complete_category_acls($this->base,"fai");
101 if(!preg_match("/r/",$acl)){
102 $this->Classes = array();
103 }
105 /* Set all available releases */
106 $this->Releases = $tmp;
107 $this->usedClasses = array();
109 /* Check the configuration */
110 if(count($this->Classes) && (!empty($this->Release))){
111 foreach($this->Classes as $class){
112 $this->resolveObject($class);
113 }
114 }
115 }
118 /* Capture posts, and call opnen close tag */
119 function save_object()
120 {
121 $once = true;
123 if(isset($_GET['expand']) && isset($_GET['id'])){
124 if(isset($this->Result[$_GET['id']])){
125 $this->Result[$_GET['id']]['Open'] =true ;
126 }
127 }
129 if(isset($_GET['compress']) && isset($_GET['id'])){
130 if(isset($this->Result[$_GET['id']])){
131 unset($this->Result[$_GET['id']]['Open']);
132 }
133 }
135 foreach($_POST as $name => $value ){
137 /* check for open tag request */
138 if(preg_match("/^open_/",$name) && $once){
139 $once = false;
140 $value = preg_replace("/^open_/","",$name);
141 $value = preg_replace("/_.*$/","",$value);
142 $this->Result[$value]['Open'] =true ;
143 }
144 /* Check for close tag request */
145 if(preg_match("/^close_/",$name) && $once){
146 $once = false;
147 $value = preg_replace("/^close_/","",$name);
148 $value = preg_replace("/_.*$/","",$value);
149 unset($this->Result[$value]['Open']);
150 }
151 if(preg_match("/^reloadList/",$name)){
152 $this->InitCalled = false;
153 }
154 }
155 }
158 /* returns an array with all possibly release types */
159 function getBranches($base = false,$prefix = "")
160 {
161 if(!$base){
162 $base = $this->base;
163 }
164 return(get_all_releases_from_base($base,true));
165 }
168 /* This function calls all nescessary functions to generate the fai class summary tree */
169 function execute()
170 {
171 /* Check if we must (re)init the this tab */
172 if(!$this->InitCalled){
173 $this->init();
174 }
176 if(isset($_GET['show'])){
177 $ldap = $this->config->get_ldap_link();
178 $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplate'));
179 $attrs = $ldap->fetch();
180 if($_GET['type'] == "FAIscript"){
181 $str = $attrs['FAIscript'][0];
182 }elseif($_GET['type'] == "FAIhook"){
183 $str = $attrs['FAIscript'][0];
184 }elseif($_GET['type'] == "FAItemplate"){
185 $str = $attrs['FAItemplateFile'][0];
186 }
187 echo nl2br($str);
188 exit();
189 }
191 /* Get smarty class & assign created summary results */
192 $smarty = get_smarty();
193 $acl = $this->ui->has_complete_category_acls($this->base,"fai");
194 $smarty->assign("readable", preg_match("/r/",$acl));
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>
220 <td colspan=2>
221 <a href='?plug=".$_GET['plug']."&expand&id=".$key."'>
222 <img border=0 class='center' src='images/expand.png' alt='"._("Open")."'> ";
223 $str .= $image." ";
224 $str .= " <b>".$this->objs[$key]['Name']."</b></a></td></tr>";
225 }else{
226 $str .= "<tr><td colspan=2>
227 <a href='?plug=".$_GET['plug']."&compress&id=".$key."'>
228 <img border=0 class='center' src='images/sort_down.png' alt='"._("Close")."'> ";
229 $str .= $image." ";
230 $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
232 /* Display FAItemplate FAIhook FAIscript entries */
233 if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
235 $nums = array();
236 $tmp = array();
237 if($key == "FAIscript"){
238 if(is_array($entry['Entries'])){
239 foreach($entry['Entries'] as $scripts){
240 foreach($scripts as $script){
242 if(!isset($nums[$script['cn'][0]])){
243 $nums[$script['cn'][0]]= 0;
244 }
245 $nums[$script['cn'][0]] ++;
247 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
248 }
249 }
250 }
251 krsort($tmp);;
253 $entry['Entries'] = $tmp;
254 }else{
255 $tmp = array();
256 foreach($entry['Entries'] as $script){
257 $tmp[$script['cn'][0].$script['CLASS']] = $script;
258 if(!isset($nums[$script['cn'][0]])){
259 $nums[$script['cn'][0]]= 0;
260 }
261 $nums[$script['cn'][0]] ++;
262 }
263 ksort($tmp);
264 $entry['Entries'] = $tmp;
265 }
267 foreach($entry['Entries'] as $cn => $data){
269 if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
270 $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
271 }else{
272 $str .="<tr><td style='padding-left:20px;'>";
273 }
275 $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
276 $str .= $image." ";
277 if(isset($data['FAIpriority'][0])){
278 $str .= "(".$data['FAIpriority'][0].")";
279 }
281 $str .= " ".$data['cn'][0];
282 if(isset($data['description'][0])){
283 $str .= " [".$data['description'][0]."]";
284 }
285 $str .= "</a>";
286 $str .= "</td><td><i>".$data['CLASS']."</i>";
287 $str .= "</td></tr>";
288 }
289 }
291 /* Create variable entries */
292 if(in_array($key,array("FAIvariable"))) {
293 foreach($entry['Entries'] as $cn => $data){
294 $str .="<tr><td style='padding-left:20px;'>";
295 $str .= $image." ".$data['cn'][0];
296 if(isset($data['description'][0])){
297 $str .= " [".$data['description'][0]."] ";
298 }
299 /* Only display FAIvariableContent if description dosn't contain [*] */
300 if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
301 $str .=" = '".$data['FAIvariableContent'][0]."'";
302 }
303 $str .= "</td><td><i>".$data['CLASS']."</i>";
304 $str .="</td></tr>";
305 }
306 }
308 /* Create packagelist entries */
309 if(in_array($key,array("FAIpackageList"))) {
310 ksort($entry['Entries']);
311 foreach($entry['Entries'] as $data){
312 $str .="<tr><td style='padding-left:20px;'>";
313 $str .= $image." ".$data['cn'][0];
314 $str .= "</td><td><i>".$data['CLASS']."</i>";
315 $str .= "</td></tr>";;
316 }
317 }
319 /* Create partition table entries */
320 if(in_array($key,array("FAIpartitionTable"))) {
321 foreach($entry['Entries'] as $cn => $data){
322 $str .= "<tr><td style='padding-left:20px;'>";
323 $str .= $image." ".$data['name'];
324 $str .= "</td><td><i>".$data['CLASS']."</i>";
325 $str .= "</td></tr>";
326 ksort($data['partitions']);
328 $str .= "<tr><td colspan=2 style='padding-left:20px;'>";
329 $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
330 <tr style='background-color:#DBDBDB;'>
331 <td class='tbhead'>"._("No.")."</td>
332 <td class='tbhead'>"._("Name")."</td>
333 <td class='tbhead'>"._("FS options")."</td>
334 <td class='tbhead'>"._("Mount options")."</td>
335 <td class='tbhead'>"._("Size in MB")."</td>
336 <td class='tbhead'>"._("Mount point")."</td>
337 <td class='tbrhead'>"._("Type")."</td>
338 </tr>";
340 $i = 0;
341 foreach($data['partitions'] as $key => $part){
342 $i ++;
344 if($i%2 == 1){
345 $c = " class='rowxp1'";
346 $d = " class='list1'";
347 }else{
348 $c = " class='rowxp0'";
349 $d = " class='list0'";
350 }
352 $str.="<tr $c style='height:10px;'>
353 <td $d>".$i."</td>
354 <td $d>".$part['cn'][0]."</td>
355 <td $d>".$part['FAIfsType'][0]."</td>
356 <td $d>".$part['FAIfsOptions'][0]."</td>
357 <td $d>".$part['FAIpartitionSize'][0]."</td>
358 <td $d>".$part['FAImountPoint'][0]."</td>
359 <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td>
360 </tr>";
361 }
362 $str .="</table>";
363 }
364 }
365 }
366 }
367 $str .="</table>";
368 return($str);
369 }
372 /* resolve specified object to append it to our object tree */
373 function resolveObject($class)
374 {
375 $ldap = $this->config->get_ldap_link();
376 $dn = $this->Releases [$this->Release];
377 $resolvedClasses = get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
379 /* Try to fetch all types of fai objects with the given cn */
380 foreach($resolvedClasses as $obj){
382 $dn = $obj['dn'];
383 $ldap->cat($dn);
385 while($attrs = $ldap->fetch()){
387 foreach($this-> objs as $key => $rest){
388 if(in_array($key,$attrs['objectClass'])){
390 if(!isset($this->usedClasses[$key][$class])){
391 $this->usedClasses[$key][$class] = true;
392 switch($key){
393 case "FAIprofile": $this->prepare_FAIprofile($attrs);break;
394 case "FAIscript": $this->prepare_FAIscript($attrs);break;
395 case "FAIhook": $this->prepare_FAIhook($attrs);break;
396 case "FAIvariable": $this->prepare_FAIvariable($attrs);break;
397 case "FAItemplate": $this->prepare_FAItemplate($attrs);break;
398 case "FAIpackageList": $this->prepare_FAIpackageList($attrs);break;
399 case "FAIpartitionTable": $this->prepare_FAIpartitionTable($attrs);break;
400 }
401 }
403 }
404 }
405 }
406 }
407 }
409 /* Prepare fai script */
410 function prepare_FAIscript($data)
411 {
412 if(isset($this->Result['FAIscript']['Entries'])){
413 $current = $this->Result['FAIscript']['Entries'];
414 }else{
415 $current = array();
416 }
417 $ldap = $this->config->get_ldap_link();
418 $ldap->cd($data['dn']);
419 $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
420 while($attrs = $ldap->fetch()){
421 $attrs['CLASS'] = $data['cn'][0];
422 $current[$attrs['FAIpriority'][0]][] = $attrs;
423 }
424 $this->Result['FAIscript']['Entries'] = $current ;
425 }
428 /* Prepare fai script */
429 function prepare_FAIpartitionTable($data)
430 {
431 if(isset($this->Result['FAIpartitionTable']['Entries'])){
432 $current = $this->Result['FAIpartitionTable']['Entries'];
433 }else{
434 $current = array();
435 }
437 /* get subentries */
438 $ldap = $this->config->get_ldap_link();
439 $ldap->cd($data['dn']);
440 $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
441 $disks = array();
443 /* Create an array which is sortable by priority */
444 while($attrs = $ldap->fetch()){
445 $attrs['CLASS'] = $data['cn'][0];
446 $disks[$attrs['cn'][0]]= $attrs;
447 if(isset($attrs['description'][0])){
448 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
449 }else{
450 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
451 }
452 $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
453 $disks[$attrs['cn'][0]]['partitions'] = array();
454 }
456 /* Sort by priority */
457 foreach($disks as $key => $disk){
458 $ldap->cd($disk['dn']);
459 $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
460 while($attrs = $ldap->fetch()){
461 if(!isset($attrs['FAIfsOptions'][0])){
462 $attrs['FAIfsOptions'][0] = "";
463 }
464 $attrs['CLASS'] = $data['cn'][0];
465 $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
466 }
467 }
468 $this->Result['FAIpartitionTable']['Entries'] = $disks;
469 }
472 /* Create template entry */
473 function prepare_FAItemplate($data)
474 {
475 $current = array();
476 if(isset($this->Result['FAItemplate']['Entries'])){
477 $current = $this->Result['FAItemplate']['Entries'];
478 }
480 $ldap = $this->config->get_ldap_link();
481 $ldap->cd($data['dn']);
482 $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
483 while($attrs = $ldap->fetch()){
484 $attrs['CLASS'] = $data['cn'][0];
485 $current[] = $attrs;
486 }
487 $this->Result['FAItemplate']['Entries'] = $current ;
488 }
491 /* Create a package list */
492 function prepare_FAIpackageList($data)
493 {
494 $current = array();
495 if(isset($this->Result['FAIpackageList']['Entries'])){
496 $current = $this->Result['FAIpackageList']['Entries'];
497 }
498 if(isset($data['FAIpackage'])){
499 unset($data['FAIpackage']['count']);
500 foreach($data['FAIpackage'] as $pkg){
501 $attrs['CLASS'] = $data['cn'][0];
502 $attrs['cn'][0] = $pkg;
503 $current[$pkg] = $attrs;
504 }
505 }
506 $this->Result['FAIpackageList']['Entries'] = $current ;
507 }
510 /* Create a variable entry */
511 function prepare_FAIvariable($data)
512 {
513 $current = array();
514 if(isset($this->Result['FAIvariable']['Entries'])){
515 $current = $this->Result['FAIvariable']['Entries'];
516 }
517 $ldap = $this->config->get_ldap_link();
518 $ldap->cd($data['dn']);
519 $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
520 while($attrs = $ldap->fetch()){
521 $attrs['CLASS'] = $data['cn'][0];
522 $current[] = $attrs;
523 }
524 $this->Result['FAIvariable']['Entries'] = $current ;
525 }
527 /* Create a hook entry */
528 function prepare_FAIhook($data)
529 {
530 $current = array();
531 if(isset($this->Result['FAIhook']['Entries'])){
532 $current = $this->Result['FAIhook']['Entries'];
533 }
534 $ldap = $this->config->get_ldap_link();
535 $ldap->cd($data['dn']);
536 $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
537 while($attrs = $ldap->fetch()){
538 $attrs['CLASS'] = $data['cn'][0];
539 $current[$attrs['cn'][0]] = $attrs;
540 }
541 $this->Result['FAIhook']['Entries'] = $current ;
542 }
545 /* Create a new Profile entry */
546 function prepare_FAIprofile($data)
547 {
548 $classes = split("\ ",$data['FAIclass'][0]);
549 foreach($classes as $class){
550 $class = trim($class);
551 $this->resolveObject($class);
552 }
553 }
555 /* Return plugin informations for acl handling * /
556 function plInfo()
557 {
558 return (array(
559 "plShortName" => _("Summary"),
560 "plDescription" => _("FAI summary"),
561 "plSelfModify" => FALSE,
562 "plDepends" => array(),
563 "plPriority" => 15,
564 "plSection" => array("administration"),
565 "plCategory" => array("workstation","server","fai","ogroups") ,
566 "plProvidedAcls"=> array(
567 "readable" => _("Viewable")),
568 ));
569 }
570 */
571 }
572 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
573 ?>