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" =>"plugins/fai/images/fai_script.png", "Name"=>_("Scripts") ,"Tree" =>get_ou('faiScriptRDN')) ,
45 "FAIhook" => array( "Image" =>"plugins/fai/images/fai_hook.png" , "Name"=>_("Hooks") ,"Tree" =>get_ou('faiHookRDN')),
46 "FAIvariable" => array( "Image" =>"plugins/fai/images/fai_variable.png", "Name"=>_("Variables") ,"Tree" =>get_ou('faiVariableRDN')) ,
47 "FAItemplate" => array( "Image" =>"plugins/fai/images/fai_template.png", "Name"=>_("Templates") ,"Tree" =>get_ou('faiTemplateRDN')) ,
48 "FAIpartitionTable" => array( "Image" =>"plugins/fai/images/fai_partitionTable.png","Name"=>_("Partition table") ,"Tree" =>get_ou('faiPartitionRDN')) ,
49 "FAIpackageList" => array( "Image" =>"plugins/fai/images/fai_packages.png", "Name"=>_("Package list") ,"Tree" =>get_ou('faiPackageRDN')) ,
50 "FAIprofile" => array( "Image" =>"plugins/fai/images/fai_profile.png", "Name"=>_("Profiles") ,"Tree" =>get_ou('faiProfileRDN')));
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();
62 /* Get classes & release name
63 There are 2 different tabs which use the summary tab
64 faiProfile / Workstations */
66 /* Check if given parent object is fai profile tabs */
67 if(isset($this->parent->by_name['faiProfile'])){
69 /* Get Classes and release */
70 $this->Classes = $this->parent->by_object['faiProfile']->FAIclasses;
71 $this->base = $this->parent->by_object['faiProfile']->parent->parent->fai_release;
72 $str = preg_replace("/^.*".preg_quote(get_ou('faiProfileRDN'), '/')."/i","",$this->dn);
73 $tmp = $this->parent->by_object['faiProfile']->parent->parent->getBranches();
74 $this->Release = $tmp[$this->base];
76 /* Check if parent tab is work tab */
77 }elseif(isset($this->parent->by_name['workstartup'])){
79 if($this->parent->by_object['workstartup']->FAIdebianMirror == "inherited"){
80 $this->Classes = $this->parent->by_object['workstartup']->InheritedFAIclass;
81 $this->Release = $this->parent->by_object['workstartup']->InheritedFAIrelease;
82 }else{
83 $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
84 $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
85 }
87 /* Check if this is a workstation or an object group */
88 if(isset($this->parent->by_object['workgeneric'])){
89 $this->base = $this->parent->by_object['workgeneric']->base;
90 }elseif(isset($this->parent->by_object['ogroup'])){
91 $this->base = $this->parent->by_object['ogroup']->base;
92 }elseif(isset($this->parent->by_object['servgeneric'])){
93 $this->base = $this->parent->by_object['servgeneric']->base;
94 }else{
95 msg_dialog::display(_("Error"), _("Unknown FAI information source!"), ERROR_DIALOG);
96 }
98 /* Append workstation class && LAST */
99 if(isset($this->parent->by_object['workgeneric']->cn)){
100 $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
101 }
102 }
104 // All FAI stuff is located on the ldap base, isn't it?
105 $this->base = $this->config->current['BASE'];
107 $tmp = array_flip($this->getBranches());
109 $this->Classes[] = "LAST";
111 /* You need full access to complete collection of fai acls to view this summary */
112 $acl = $this->ui->has_complete_category_acls($this->base,"fai");
113 if(!preg_match("/r/",$acl)){
114 $this->Classes = array();
115 }
117 /* Set all available releases */
118 $this->Releases = $tmp;
119 $this->usedClasses = array();
122 /* Check the configuration */
123 if(count($this->Classes) && (!empty($this->Release))){
124 foreach($this->Classes as $class){
125 $this->resolveObject($class);
126 }
127 }
128 }
131 /* Capture posts, and call opnen close tag */
132 function save_object()
133 {
134 $once = true;
136 if(isset($_GET['expand']) && isset($_GET['id'])){
137 if(isset($this->Result[$_GET['id']])){
138 $this->Result[$_GET['id']]['Open'] =true ;
139 }
140 }
142 if(isset($_GET['compress']) && isset($_GET['id'])){
143 if(isset($this->Result[$_GET['id']])){
144 unset($this->Result[$_GET['id']]['Open']);
145 }
146 }
148 foreach($_POST as $name => $value ){
150 /* check for open tag request */
151 if(preg_match("/^open_/",$name) && $once){
152 $once = false;
153 $value = preg_replace("/^open_/","",$name);
154 $value = preg_replace("/_.*$/","",$value);
155 $this->Result[$value]['Open'] =true ;
156 }
157 /* Check for close tag request */
158 if(preg_match("/^close_/",$name) && $once){
159 $once = false;
160 $value = preg_replace("/^close_/","",$name);
161 $value = preg_replace("/_.*$/","",$value);
162 unset($this->Result[$value]['Open']);
163 }
164 }
165 if(isset($_POST['reloadList'])){
166 $this->InitCalled = false;
167 }
168 }
171 /* returns an array with all possibly release types */
172 function getBranches($base = false,$prefix = "")
173 {
174 if(!$base){
175 $base = $this->base;
176 }
177 return(FAI::get_all_releases_from_base($base,true));
178 }
181 /* This function calls all nescessary functions to generate the fai class summary tree */
182 function execute()
183 {
184 plugin::execute();
186 /* Skip this if fai is deactivated */
187 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
188 if(empty($tmp)){
189 $str = "<h3>"._("You can't use this plugin until FAI is activated.")."</h3>";
190 return $str;
191 }
193 /* Check if we must (re)init the this tab */
194 if(!$this->InitCalled){
195 $this->init();
196 }
198 if(isset($_GET['show'])){
199 $ldap = $this->config->get_ldap_link();
200 $ldap->cat(postDecode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplateFile'));
201 $attrs = $ldap->fetch();
202 if($_GET['type'] == "FAIscript"){
203 $str = $attrs['FAIscript'][0];
204 }elseif($_GET['type'] == "FAIhook"){
205 $str = $attrs['FAIscript'][0];
206 }elseif($_GET['type'] == "FAItemplate"){
207 $str = $attrs['FAItemplateFile'][0];
208 }
209 echo "<pre>".$str."</pre>";
210 exit();
211 }
213 /* Get smarty class & assign created summary results */
214 $smarty = get_smarty();
215 $acl = $this->ui->has_complete_category_acls($this->base,"fai");
216 $smarty->assign("readable", preg_match("/r/",$acl));
218 $this->ObjectList = $this->createSummary($this->Result);
219 $smarty->assign("objectList",$this->ObjectList);
220 return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
221 }
224 /* Create output of from resolved fai objects */
225 function createSummary($data)
226 {
227 /* Don't generate any output, if there is no result */
228 if(!count($this->Result)){
229 return("<h3>"._("This object has no FAI classes assigned.")."</h3>");
230 }
232 $str ="<table style='width:100%;' summary='"._("FAI summary")."'> ";
233 /* walk through all classes */
234 foreach($data as $key => $entry){
236 $image = image($this->objs[$key]['Image']);
237 if(!isset($entry['Open'])){
238 $str .= "<tr>
239 <td colspan=2>".
240 image('images/lists/expand.png').$image." ".
241 "<a href='?plug=".$_GET['plug']."&expand&id=".$key."'>".
242 "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
243 }else{
244 $str .= "<tr><td colspan=2>".
245 image('images/lists/sort-down.png').$image." ".
246 "<a href='?plug=".$_GET['plug']."&compress&id=".$key."'>".
247 "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
249 /* Display FAItemplate FAIhook FAIscript entries */
250 if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
252 $nums = array();
253 $tmp = array();
254 if($key == "FAIscript"){
255 if(is_array($entry['Entries'])){
256 foreach($entry['Entries'] as $scripts){
257 foreach($scripts as $script){
259 if(!isset($nums[$script['cn'][0]])){
260 $nums[$script['cn'][0]]= 0;
261 }
262 $nums[$script['cn'][0]] ++;
264 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
265 }
266 }
267 }
268 krsort($tmp);;
270 $entry['Entries'] = $tmp;
271 }else{
272 $tmp = array();
273 foreach($entry['Entries'] as $script){
274 $tmp[$script['cn'][0].$script['CLASS']] = $script;
275 if(!isset($nums[$script['cn'][0]])){
276 $nums[$script['cn'][0]]= 0;
277 }
278 $nums[$script['cn'][0]] ++;
279 }
280 ksort($tmp);
281 $entry['Entries'] = $tmp;
282 }
284 foreach($entry['Entries'] as $cn => $data){
286 if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
287 $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
288 }else{
289 $str .="<tr><td style='padding-left:20px;'>";
290 }
292 $str .= $image." ";
293 $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".postEncode($data['dn'])."'>";
294 if(isset($data['FAIpriority'][0])){
295 $str .= "(".$data['FAIpriority'][0].")";
296 }
298 $str .= " ".$data['cn'][0];
299 if(isset($data['description'][0])){
300 $str .= " [".$data['description'][0]."]";
301 }
302 $str .= "</a>";
303 $str .= "</td><td><i>".$data['CLASS']."</i>";
304 $str .= "</td></tr>";
305 }
306 }
308 /* Create variable entries */
309 if(in_array($key,array("FAIvariable"))) {
310 foreach($entry['Entries'] as $cn => $data){
311 $str .="<tr><td style='padding-left:20px;'>";
312 $str .= $image." ".$data['cn'][0];
313 if(isset($data['description'][0])){
314 $str .= " [".$data['description'][0]."] ";
315 }
316 /* Only display FAIvariableContent if description dosn't contain [*] */
317 if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
318 $str .=" = '".$data['FAIvariableContent'][0]."'";
319 }
320 $str .= "</td><td><i>".$data['CLASS']."</i>";
321 $str .="</td></tr>";
322 }
323 }
325 /* Create packagelist entries */
326 if(in_array($key,array("FAIpackageList"))) {
327 ksort($entry['Entries']);
328 foreach($entry['Entries'] as $data){
329 $str .="<tr><td style='padding-left:20px;'>";
330 $str .= $image." ".$data['cn'][0];
331 $str .= "</td><td><i>".$data['CLASS']."</i>";
332 $str .= "</td></tr>";;
333 }
334 }
336 /* Create partition table entries */
337 if(in_array($key,array("FAIpartitionTable"))) {
338 foreach($entry['Entries'] as $cn => $data){
339 $str .= "<tr><td style='padding-left:20px;'>";
340 $str .= $image." ".$data['name'];
341 $str .= "</td><td><i>".$data['CLASS']."</i>";
342 $str .= "</td></tr>";
343 ksort($data['partitions']);
345 $str .= "<tr><td colspan=2 style='padding-left:20px;'>";
346 $str .= "<table \""._("Summary of fai classes")."\"
347 cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
348 <tr style='background-color:#DBDBDB;'>
349 <td class='tbhead'>"._("No.")."</td>
350 <td class='tbhead'>"._("Name")."</td>
351 <td class='tbhead'>"._("FS options")."</td>
352 <td class='tbhead'>"._("Mount options")."</td>
353 <td class='tbhead'>"._("Size in MB")."</td>
354 <td class='tbhead'>"._("Mount point")."</td>
355 <td class='tbrhead'>"._("Type")."</td>
356 </tr>";
358 $i = 0;
359 foreach($data['partitions'] as $key => $part){
360 $i ++;
362 if($i%2 == 1){
363 $c = " class='rowxp1'";
364 $d = " class='list1'";
365 }else{
366 $c = " class='rowxp0'";
367 $d = " class='list0'";
368 }
370 $str.="<tr $c style='height:10px;'>
371 <td $d>".$i."</td>
372 <td $d>".$part['cn'][0]."</td>
373 <td $d>".$part['FAIfsType'][0]."</td>
374 <td $d>".$part['FAIfsOptions'][0]."</td>
375 <td $d>".$part['FAIpartitionSize'][0]."</td>
376 <td $d>".$part['FAImountPoint'][0]."</td>
377 <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td>
378 </tr>";
379 }
380 $str .="</table>";
381 }
382 }
383 }
384 }
385 $str .="</table>";
386 return($str);
387 }
390 /* resolve specified object to append it to our object tree */
391 function resolveObject($class)
392 {
393 $ldap = $this->config->get_ldap_link();
394 if(isset($this->Releases[$this->Release])){
395 $dn = $this->Releases[$this->Release];
396 }else{
397 return(FALSE);
398 }
400 $resolvedClasses = FAI::get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
402 /* Try to fetch all types of fai objects with the given cn */
403 foreach($resolvedClasses as $obj){
405 $dn = $obj['dn'];
406 $ldap->cat($dn);
408 while($attrs = $ldap->fetch()){
410 foreach($this-> objs as $key => $rest){
411 if(in_array($key,$attrs['objectClass'])){
413 if(!isset($this->usedClasses[$key][$class])){
414 $this->usedClasses[$key][$class] = true;
415 switch($key){
416 case "FAIprofile": $this->prepare_FAIprofile($attrs);break;
417 case "FAIscript": $this->prepare_FAIscript($attrs);break;
418 case "FAIhook": $this->prepare_FAIhook($attrs);break;
419 case "FAIvariable": $this->prepare_FAIvariable($attrs);break;
420 case "FAItemplate": $this->prepare_FAItemplate($attrs);break;
421 case "FAIpackageList": $this->prepare_FAIpackageList($attrs);break;
422 case "FAIpartitionTable": $this->prepare_FAIpartitionTable($attrs);break;
423 }
424 }
426 }
427 }
428 }
429 }
430 }
432 /* Prepare fai script */
433 function prepare_FAIscript($data)
434 {
435 if(isset($this->Result['FAIscript']['Entries'])){
436 $current = $this->Result['FAIscript']['Entries'];
437 }else{
438 $current = array();
439 }
440 $ldap = $this->config->get_ldap_link();
441 $ldap->cd($data['dn']);
442 $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
443 while($attrs = $ldap->fetch()){
444 $attrs['CLASS'] = $data['cn'][0];
445 $current[$attrs['FAIpriority'][0]][] = $attrs;
446 }
447 $this->Result['FAIscript']['Entries'] = $current ;
448 }
451 /* Prepare fai script */
452 function prepare_FAIpartitionTable($data)
453 {
454 if(isset($this->Result['FAIpartitionTable']['Entries'])){
455 $current = $this->Result['FAIpartitionTable']['Entries'];
456 }else{
457 $current = array();
458 }
460 /* get subentries */
461 $ldap = $this->config->get_ldap_link();
462 $ldap->cd($data['dn']);
463 $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
464 $disks = array();
466 /* Create an array which is sortable by priority */
467 while($attrs = $ldap->fetch()){
468 $attrs['CLASS'] = $data['cn'][0];
469 $disks[$attrs['cn'][0]]= $attrs;
470 if(isset($attrs['description'][0])){
471 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
472 }else{
473 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
474 }
475 $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
476 $disks[$attrs['cn'][0]]['partitions'] = array();
477 }
479 /* Sort by priority */
480 foreach($disks as $key => $disk){
481 $ldap->cd($disk['dn']);
482 $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
483 while($attrs = $ldap->fetch()){
484 if(!isset($attrs['FAIfsOptions'][0])){
485 $attrs['FAIfsOptions'][0] = "";
486 }
487 $attrs['CLASS'] = $data['cn'][0];
488 $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
489 }
490 }
491 $this->Result['FAIpartitionTable']['Entries'] = $disks;
492 }
495 /* Create template entry */
496 function prepare_FAItemplate($data)
497 {
498 $current = array();
499 if(isset($this->Result['FAItemplate']['Entries'])){
500 $current = $this->Result['FAItemplate']['Entries'];
501 }
503 $ldap = $this->config->get_ldap_link();
504 $ldap->cd($data['dn']);
505 $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
506 while($attrs = $ldap->fetch()){
507 $attrs['CLASS'] = $data['cn'][0];
508 $current[] = $attrs;
509 }
510 $this->Result['FAItemplate']['Entries'] = $current ;
511 }
514 /* Create a package list */
515 function prepare_FAIpackageList($data)
516 {
517 $current = array();
518 if(isset($this->Result['FAIpackageList']['Entries'])){
519 $current = $this->Result['FAIpackageList']['Entries'];
520 }
521 if(isset($data['FAIpackage'])){
522 unset($data['FAIpackage']['count']);
523 foreach($data['FAIpackage'] as $pkg){
524 $attrs['CLASS'] = $data['cn'][0];
525 $attrs['cn'][0] = $pkg;
526 $current[$pkg] = $attrs;
527 }
528 }
529 $this->Result['FAIpackageList']['Entries'] = $current ;
530 }
533 /* Create a variable entry */
534 function prepare_FAIvariable($data)
535 {
536 $current = array();
537 if(isset($this->Result['FAIvariable']['Entries'])){
538 $current = $this->Result['FAIvariable']['Entries'];
539 }
540 $ldap = $this->config->get_ldap_link();
541 $ldap->cd($data['dn']);
542 $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
543 while($attrs = $ldap->fetch()){
544 $attrs['CLASS'] = $data['cn'][0];
545 $current[] = $attrs;
546 }
547 $this->Result['FAIvariable']['Entries'] = $current ;
548 }
550 /* Create a hook entry */
551 function prepare_FAIhook($data)
552 {
553 $current = array();
554 if(isset($this->Result['FAIhook']['Entries'])){
555 $current = $this->Result['FAIhook']['Entries'];
556 }
557 $ldap = $this->config->get_ldap_link();
558 $ldap->cd($data['dn']);
559 $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
560 while($attrs = $ldap->fetch()){
561 $attrs['CLASS'] = $data['cn'][0];
562 $current[$attrs['cn'][0]] = $attrs;
563 }
564 $this->Result['FAIhook']['Entries'] = $current ;
565 }
568 /* Create a new Profile entry */
569 function prepare_FAIprofile($data)
570 {
571 $classes = explode(" ",$data['FAIclass'][0]);
572 foreach($classes as $class){
573 $class = trim($class);
574 $this->resolveObject($class);
575 }
576 }
578 /* Return plugin informations for acl handling * /
579 static function plInfo()
580 {
581 return (array(
582 "plShortName" => _("Summary"),
583 "plDescription" => _("FAI summary"),
584 "plSelfModify" => FALSE,
585 "plDepends" => array(),
586 "plPriority" => 15,
587 "plSection" => array("administration"),
588 "plCategory" => array("workstation","server","fai","ogroups") ,
589 "plProvidedAcls"=> array(
590 "readable" => _("Viewable")),
591 ));
592 }
593 */
594 }
595 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
596 ?>