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(base64_decode($_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 .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
293 $str .= $image." ";
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 cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
347 <tr style='background-color:#DBDBDB;'>
348 <td class='tbhead'>"._("No.")."</td>
349 <td class='tbhead'>"._("Name")."</td>
350 <td class='tbhead'>"._("FS options")."</td>
351 <td class='tbhead'>"._("Mount options")."</td>
352 <td class='tbhead'>"._("Size in MB")."</td>
353 <td class='tbhead'>"._("Mount point")."</td>
354 <td class='tbrhead'>"._("Type")."</td>
355 </tr>";
357 $i = 0;
358 foreach($data['partitions'] as $key => $part){
359 $i ++;
361 if($i%2 == 1){
362 $c = " class='rowxp1'";
363 $d = " class='list1'";
364 }else{
365 $c = " class='rowxp0'";
366 $d = " class='list0'";
367 }
369 $str.="<tr $c style='height:10px;'>
370 <td $d>".$i."</td>
371 <td $d>".$part['cn'][0]."</td>
372 <td $d>".$part['FAIfsType'][0]."</td>
373 <td $d>".$part['FAIfsOptions'][0]."</td>
374 <td $d>".$part['FAIpartitionSize'][0]."</td>
375 <td $d>".$part['FAImountPoint'][0]."</td>
376 <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td>
377 </tr>";
378 }
379 $str .="</table>";
380 }
381 }
382 }
383 }
384 $str .="</table>";
385 return($str);
386 }
389 /* resolve specified object to append it to our object tree */
390 function resolveObject($class)
391 {
392 $ldap = $this->config->get_ldap_link();
393 if(isset($this->Releases[$this->Release])){
394 $dn = $this->Releases[$this->Release];
395 }else{
396 return(FALSE);
397 }
399 $resolvedClasses = FAI::get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
401 /* Try to fetch all types of fai objects with the given cn */
402 foreach($resolvedClasses as $obj){
404 $dn = $obj['dn'];
405 $ldap->cat($dn);
407 while($attrs = $ldap->fetch()){
409 foreach($this-> objs as $key => $rest){
410 if(in_array($key,$attrs['objectClass'])){
412 if(!isset($this->usedClasses[$key][$class])){
413 $this->usedClasses[$key][$class] = true;
414 switch($key){
415 case "FAIprofile": $this->prepare_FAIprofile($attrs);break;
416 case "FAIscript": $this->prepare_FAIscript($attrs);break;
417 case "FAIhook": $this->prepare_FAIhook($attrs);break;
418 case "FAIvariable": $this->prepare_FAIvariable($attrs);break;
419 case "FAItemplate": $this->prepare_FAItemplate($attrs);break;
420 case "FAIpackageList": $this->prepare_FAIpackageList($attrs);break;
421 case "FAIpartitionTable": $this->prepare_FAIpartitionTable($attrs);break;
422 }
423 }
425 }
426 }
427 }
428 }
429 }
431 /* Prepare fai script */
432 function prepare_FAIscript($data)
433 {
434 if(isset($this->Result['FAIscript']['Entries'])){
435 $current = $this->Result['FAIscript']['Entries'];
436 }else{
437 $current = array();
438 }
439 $ldap = $this->config->get_ldap_link();
440 $ldap->cd($data['dn']);
441 $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
442 while($attrs = $ldap->fetch()){
443 $attrs['CLASS'] = $data['cn'][0];
444 $current[$attrs['FAIpriority'][0]][] = $attrs;
445 }
446 $this->Result['FAIscript']['Entries'] = $current ;
447 }
450 /* Prepare fai script */
451 function prepare_FAIpartitionTable($data)
452 {
453 if(isset($this->Result['FAIpartitionTable']['Entries'])){
454 $current = $this->Result['FAIpartitionTable']['Entries'];
455 }else{
456 $current = array();
457 }
459 /* get subentries */
460 $ldap = $this->config->get_ldap_link();
461 $ldap->cd($data['dn']);
462 $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
463 $disks = array();
465 /* Create an array which is sortable by priority */
466 while($attrs = $ldap->fetch()){
467 $attrs['CLASS'] = $data['cn'][0];
468 $disks[$attrs['cn'][0]]= $attrs;
469 if(isset($attrs['description'][0])){
470 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
471 }else{
472 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
473 }
474 $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
475 $disks[$attrs['cn'][0]]['partitions'] = array();
476 }
478 /* Sort by priority */
479 foreach($disks as $key => $disk){
480 $ldap->cd($disk['dn']);
481 $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
482 while($attrs = $ldap->fetch()){
483 if(!isset($attrs['FAIfsOptions'][0])){
484 $attrs['FAIfsOptions'][0] = "";
485 }
486 $attrs['CLASS'] = $data['cn'][0];
487 $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
488 }
489 }
490 $this->Result['FAIpartitionTable']['Entries'] = $disks;
491 }
494 /* Create template entry */
495 function prepare_FAItemplate($data)
496 {
497 $current = array();
498 if(isset($this->Result['FAItemplate']['Entries'])){
499 $current = $this->Result['FAItemplate']['Entries'];
500 }
502 $ldap = $this->config->get_ldap_link();
503 $ldap->cd($data['dn']);
504 $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
505 while($attrs = $ldap->fetch()){
506 $attrs['CLASS'] = $data['cn'][0];
507 $current[] = $attrs;
508 }
509 $this->Result['FAItemplate']['Entries'] = $current ;
510 }
513 /* Create a package list */
514 function prepare_FAIpackageList($data)
515 {
516 $current = array();
517 if(isset($this->Result['FAIpackageList']['Entries'])){
518 $current = $this->Result['FAIpackageList']['Entries'];
519 }
520 if(isset($data['FAIpackage'])){
521 unset($data['FAIpackage']['count']);
522 foreach($data['FAIpackage'] as $pkg){
523 $attrs['CLASS'] = $data['cn'][0];
524 $attrs['cn'][0] = $pkg;
525 $current[$pkg] = $attrs;
526 }
527 }
528 $this->Result['FAIpackageList']['Entries'] = $current ;
529 }
532 /* Create a variable entry */
533 function prepare_FAIvariable($data)
534 {
535 $current = array();
536 if(isset($this->Result['FAIvariable']['Entries'])){
537 $current = $this->Result['FAIvariable']['Entries'];
538 }
539 $ldap = $this->config->get_ldap_link();
540 $ldap->cd($data['dn']);
541 $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
542 while($attrs = $ldap->fetch()){
543 $attrs['CLASS'] = $data['cn'][0];
544 $current[] = $attrs;
545 }
546 $this->Result['FAIvariable']['Entries'] = $current ;
547 }
549 /* Create a hook entry */
550 function prepare_FAIhook($data)
551 {
552 $current = array();
553 if(isset($this->Result['FAIhook']['Entries'])){
554 $current = $this->Result['FAIhook']['Entries'];
555 }
556 $ldap = $this->config->get_ldap_link();
557 $ldap->cd($data['dn']);
558 $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
559 while($attrs = $ldap->fetch()){
560 $attrs['CLASS'] = $data['cn'][0];
561 $current[$attrs['cn'][0]] = $attrs;
562 }
563 $this->Result['FAIhook']['Entries'] = $current ;
564 }
567 /* Create a new Profile entry */
568 function prepare_FAIprofile($data)
569 {
570 $classes = explode(" ",$data['FAIclass'][0]);
571 foreach($classes as $class){
572 $class = trim($class);
573 $this->resolveObject($class);
574 }
575 }
577 /* Return plugin informations for acl handling * /
578 static function plInfo()
579 {
580 return (array(
581 "plShortName" => _("Summary"),
582 "plDescription" => _("FAI summary"),
583 "plSelfModify" => FALSE,
584 "plDepends" => array(),
585 "plPriority" => 15,
586 "plSection" => array("administration"),
587 "plCategory" => array("workstation","server","fai","ogroups") ,
588 "plProvidedAcls"=> array(
589 "readable" => _("Viewable")),
590 ));
591 }
592 */
593 }
594 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
595 ?>