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'), '/')."/","",$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'])){
78 if($this->parent->by_object['workstartup']->FAIdebianMirror == "inherited"){
79 $this->Classes = $this->parent->by_object['workstartup']->InheritedFAIclass;
80 $this->Release = $this->parent->by_object['workstartup']->InheritedFAIrelease;
81 }else{
82 $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
83 $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
84 }
86 /* Check if this is a workstation or an object group */
87 if(isset($this->parent->by_object['workgeneric'])){
88 $this->base = $this->parent->by_object['workgeneric']->base;
89 }elseif(isset($this->parent->by_object['ogroup'])){
90 $this->base = $this->parent->by_object['ogroup']->base;
91 }elseif(isset($this->parent->by_object['servgeneric'])){
92 $this->base = $this->parent->by_object['servgeneric']->base;
93 }else{
94 msg_dialog::display(_("Error"), _("Unknown FAI information source!"), ERROR_DIALOG);
95 }
97 /* Append workstation class && LAST */
98 if(isset($this->parent->by_object['workgeneric']->cn)){
99 $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
100 }
101 }
103 $tmp = array_flip($this->getBranches());
105 $this->Classes[] = "LAST";
107 /* You need full access to complete collection of fai acls to view this summary */
108 $acl = $this->ui->has_complete_category_acls($this->base,"fai");
109 if(!preg_match("/r/",$acl)){
110 $this->Classes = array();
111 }
113 /* Set all available releases */
114 $this->Releases = $tmp;
115 $this->usedClasses = array();
117 /* Check the configuration */
118 if(count($this->Classes) && (!empty($this->Release))){
119 foreach($this->Classes as $class){
120 $this->resolveObject($class);
121 }
122 }
123 }
126 /* Capture posts, and call opnen close tag */
127 function save_object()
128 {
129 $once = true;
131 if(isset($_GET['expand']) && isset($_GET['id'])){
132 if(isset($this->Result[$_GET['id']])){
133 $this->Result[$_GET['id']]['Open'] =true ;
134 }
135 }
137 if(isset($_GET['compress']) && isset($_GET['id'])){
138 if(isset($this->Result[$_GET['id']])){
139 unset($this->Result[$_GET['id']]['Open']);
140 }
141 }
143 foreach($_POST as $name => $value ){
145 /* check for open tag request */
146 if(preg_match("/^open_/",$name) && $once){
147 $once = false;
148 $value = preg_replace("/^open_/","",$name);
149 $value = preg_replace("/_.*$/","",$value);
150 $this->Result[$value]['Open'] =true ;
151 }
152 /* Check for close tag request */
153 if(preg_match("/^close_/",$name) && $once){
154 $once = false;
155 $value = preg_replace("/^close_/","",$name);
156 $value = preg_replace("/_.*$/","",$value);
157 unset($this->Result[$value]['Open']);
158 }
159 if(preg_match("/^reloadList/",$name)){
160 $this->InitCalled = false;
161 }
162 }
163 }
166 /* returns an array with all possibly release types */
167 function getBranches($base = false,$prefix = "")
168 {
169 if(!$base){
170 $base = $this->base;
171 }
172 return(FAI::get_all_releases_from_base($base,true));
173 }
176 /* This function calls all nescessary functions to generate the fai class summary tree */
177 function execute()
178 {
180 /* Skip this if fai is deactivated */
181 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
182 if(empty($tmp)){
183 $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
184 return $str;
185 }
187 /* Check if we must (re)init the this tab */
188 if(!$this->InitCalled){
189 $this->init();
190 }
192 if(isset($_GET['show'])){
193 $ldap = $this->config->get_ldap_link();
194 $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplateFile'));
195 $attrs = $ldap->fetch();
196 if($_GET['type'] == "FAIscript"){
197 $str = $attrs['FAIscript'][0];
198 }elseif($_GET['type'] == "FAIhook"){
199 $str = $attrs['FAIscript'][0];
200 }elseif($_GET['type'] == "FAItemplate"){
201 $str = $attrs['FAItemplateFile'][0];
202 }
203 echo "<pre>".$str."</pre>";
204 exit();
205 }
207 /* Get smarty class & assign created summary results */
208 $smarty = get_smarty();
209 $acl = $this->ui->has_complete_category_acls($this->base,"fai");
210 $smarty->assign("readable", preg_match("/r/",$acl));
212 $this->ObjectList = $this->createSummary($this->Result);
213 $smarty->assign("objectList",$this->ObjectList);
214 return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
215 }
218 /* Create output of from resolved fai objects */
219 function createSummary($data)
220 {
221 /* Don't generate any output, if there is no result */
222 $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
223 if(!count($this->Result)){
224 $str.="<tr><td>";
225 $str.= (_("This object has no FAI classes assigned."));
226 $str.= "</tr></td></table>";
227 return($str);
228 }
230 /* walk through all classes */
231 foreach($data as $key => $entry){
233 $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
234 if(!isset($entry['Open'])){
235 $str .= "<tr>
236 <td colspan=2>
237 <a href='?plug=".$_GET['plug']."&expand&id=".$key."'>
238 <img border=0 class='center' src='images/lists/expand.png' alt='"._("Open")."'> ";
239 $str .= $image." ";
240 $str .= " <b>".$this->objs[$key]['Name']."</b></a></td></tr>";
241 }else{
242 $str .= "<tr><td colspan=2>
243 <a href='?plug=".$_GET['plug']."&compress&id=".$key."'>
244 <img border=0 class='center' src='images/lists/sort-down.png' alt='"._("Close")."'> ";
245 $str .= $image." ";
246 $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
248 /* Display FAItemplate FAIhook FAIscript entries */
249 if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
251 $nums = array();
252 $tmp = array();
253 if($key == "FAIscript"){
254 if(is_array($entry['Entries'])){
255 foreach($entry['Entries'] as $scripts){
256 foreach($scripts as $script){
258 if(!isset($nums[$script['cn'][0]])){
259 $nums[$script['cn'][0]]= 0;
260 }
261 $nums[$script['cn'][0]] ++;
263 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
264 }
265 }
266 }
267 krsort($tmp);;
269 $entry['Entries'] = $tmp;
270 }else{
271 $tmp = array();
272 foreach($entry['Entries'] as $script){
273 $tmp[$script['cn'][0].$script['CLASS']] = $script;
274 if(!isset($nums[$script['cn'][0]])){
275 $nums[$script['cn'][0]]= 0;
276 }
277 $nums[$script['cn'][0]] ++;
278 }
279 ksort($tmp);
280 $entry['Entries'] = $tmp;
281 }
283 foreach($entry['Entries'] as $cn => $data){
285 if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
286 $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
287 }else{
288 $str .="<tr><td style='padding-left:20px;'>";
289 }
291 $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
292 $str .= $image." ";
293 if(isset($data['FAIpriority'][0])){
294 $str .= "(".$data['FAIpriority'][0].")";
295 }
297 $str .= " ".$data['cn'][0];
298 if(isset($data['description'][0])){
299 $str .= " [".$data['description'][0]."]";
300 }
301 $str .= "</a>";
302 $str .= "</td><td><i>".$data['CLASS']."</i>";
303 $str .= "</td></tr>";
304 }
305 }
307 /* Create variable entries */
308 if(in_array($key,array("FAIvariable"))) {
309 foreach($entry['Entries'] as $cn => $data){
310 $str .="<tr><td style='padding-left:20px;'>";
311 $str .= $image." ".$data['cn'][0];
312 if(isset($data['description'][0])){
313 $str .= " [".$data['description'][0]."] ";
314 }
315 /* Only display FAIvariableContent if description dosn't contain [*] */
316 if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
317 $str .=" = '".$data['FAIvariableContent'][0]."'";
318 }
319 $str .= "</td><td><i>".$data['CLASS']."</i>";
320 $str .="</td></tr>";
321 }
322 }
324 /* Create packagelist entries */
325 if(in_array($key,array("FAIpackageList"))) {
326 ksort($entry['Entries']);
327 foreach($entry['Entries'] as $data){
328 $str .="<tr><td style='padding-left:20px;'>";
329 $str .= $image." ".$data['cn'][0];
330 $str .= "</td><td><i>".$data['CLASS']."</i>";
331 $str .= "</td></tr>";;
332 }
333 }
335 /* Create partition table entries */
336 if(in_array($key,array("FAIpartitionTable"))) {
337 foreach($entry['Entries'] as $cn => $data){
338 $str .= "<tr><td style='padding-left:20px;'>";
339 $str .= $image." ".$data['name'];
340 $str .= "</td><td><i>".$data['CLASS']."</i>";
341 $str .= "</td></tr>";
342 ksort($data['partitions']);
344 $str .= "<tr><td colspan=2 style='padding-left:20px;'>";
345 $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
346 <tr style='background-color:#DBDBDB;'>
347 <td class='tbhead'>"._("No.")."</td>
348 <td class='tbhead'>"._("Name")."</td>
349 <td class='tbhead'>"._("FS options")."</td>
350 <td class='tbhead'>"._("Mount options")."</td>
351 <td class='tbhead'>"._("Size in MB")."</td>
352 <td class='tbhead'>"._("Mount point")."</td>
353 <td class='tbrhead'>"._("Type")."</td>
354 </tr>";
356 $i = 0;
357 foreach($data['partitions'] as $key => $part){
358 $i ++;
360 if($i%2 == 1){
361 $c = " class='rowxp1'";
362 $d = " class='list1'";
363 }else{
364 $c = " class='rowxp0'";
365 $d = " class='list0'";
366 }
368 $str.="<tr $c style='height:10px;'>
369 <td $d>".$i."</td>
370 <td $d>".$part['cn'][0]."</td>
371 <td $d>".$part['FAIfsType'][0]."</td>
372 <td $d>".$part['FAIfsOptions'][0]."</td>
373 <td $d>".$part['FAIpartitionSize'][0]."</td>
374 <td $d>".$part['FAImountPoint'][0]."</td>
375 <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td>
376 </tr>";
377 }
378 $str .="</table>";
379 }
380 }
381 }
382 }
383 $str .="</table>";
384 return($str);
385 }
388 /* resolve specified object to append it to our object tree */
389 function resolveObject($class)
390 {
391 $ldap = $this->config->get_ldap_link();
392 if(isset($this->Releases[$this->Release])){
393 $dn = $this->Releases[$this->Release];
394 }else{
395 return(FALSE);
396 }
398 $resolvedClasses = FAI::get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
400 /* Try to fetch all types of fai objects with the given cn */
401 foreach($resolvedClasses as $obj){
403 $dn = $obj['dn'];
404 $ldap->cat($dn);
406 while($attrs = $ldap->fetch()){
408 foreach($this-> objs as $key => $rest){
409 if(in_array($key,$attrs['objectClass'])){
411 if(!isset($this->usedClasses[$key][$class])){
412 $this->usedClasses[$key][$class] = true;
413 switch($key){
414 case "FAIprofile": $this->prepare_FAIprofile($attrs);break;
415 case "FAIscript": $this->prepare_FAIscript($attrs);break;
416 case "FAIhook": $this->prepare_FAIhook($attrs);break;
417 case "FAIvariable": $this->prepare_FAIvariable($attrs);break;
418 case "FAItemplate": $this->prepare_FAItemplate($attrs);break;
419 case "FAIpackageList": $this->prepare_FAIpackageList($attrs);break;
420 case "FAIpartitionTable": $this->prepare_FAIpartitionTable($attrs);break;
421 }
422 }
424 }
425 }
426 }
427 }
428 }
430 /* Prepare fai script */
431 function prepare_FAIscript($data)
432 {
433 if(isset($this->Result['FAIscript']['Entries'])){
434 $current = $this->Result['FAIscript']['Entries'];
435 }else{
436 $current = array();
437 }
438 $ldap = $this->config->get_ldap_link();
439 $ldap->cd($data['dn']);
440 $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
441 while($attrs = $ldap->fetch()){
442 $attrs['CLASS'] = $data['cn'][0];
443 $current[$attrs['FAIpriority'][0]][] = $attrs;
444 }
445 $this->Result['FAIscript']['Entries'] = $current ;
446 }
449 /* Prepare fai script */
450 function prepare_FAIpartitionTable($data)
451 {
452 if(isset($this->Result['FAIpartitionTable']['Entries'])){
453 $current = $this->Result['FAIpartitionTable']['Entries'];
454 }else{
455 $current = array();
456 }
458 /* get subentries */
459 $ldap = $this->config->get_ldap_link();
460 $ldap->cd($data['dn']);
461 $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
462 $disks = array();
464 /* Create an array which is sortable by priority */
465 while($attrs = $ldap->fetch()){
466 $attrs['CLASS'] = $data['cn'][0];
467 $disks[$attrs['cn'][0]]= $attrs;
468 if(isset($attrs['description'][0])){
469 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
470 }else{
471 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
472 }
473 $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
474 $disks[$attrs['cn'][0]]['partitions'] = array();
475 }
477 /* Sort by priority */
478 foreach($disks as $key => $disk){
479 $ldap->cd($disk['dn']);
480 $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
481 while($attrs = $ldap->fetch()){
482 if(!isset($attrs['FAIfsOptions'][0])){
483 $attrs['FAIfsOptions'][0] = "";
484 }
485 $attrs['CLASS'] = $data['cn'][0];
486 $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
487 }
488 }
489 $this->Result['FAIpartitionTable']['Entries'] = $disks;
490 }
493 /* Create template entry */
494 function prepare_FAItemplate($data)
495 {
496 $current = array();
497 if(isset($this->Result['FAItemplate']['Entries'])){
498 $current = $this->Result['FAItemplate']['Entries'];
499 }
501 $ldap = $this->config->get_ldap_link();
502 $ldap->cd($data['dn']);
503 $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
504 while($attrs = $ldap->fetch()){
505 $attrs['CLASS'] = $data['cn'][0];
506 $current[] = $attrs;
507 }
508 $this->Result['FAItemplate']['Entries'] = $current ;
509 }
512 /* Create a package list */
513 function prepare_FAIpackageList($data)
514 {
515 $current = array();
516 if(isset($this->Result['FAIpackageList']['Entries'])){
517 $current = $this->Result['FAIpackageList']['Entries'];
518 }
519 if(isset($data['FAIpackage'])){
520 unset($data['FAIpackage']['count']);
521 foreach($data['FAIpackage'] as $pkg){
522 $attrs['CLASS'] = $data['cn'][0];
523 $attrs['cn'][0] = $pkg;
524 $current[$pkg] = $attrs;
525 }
526 }
527 $this->Result['FAIpackageList']['Entries'] = $current ;
528 }
531 /* Create a variable entry */
532 function prepare_FAIvariable($data)
533 {
534 $current = array();
535 if(isset($this->Result['FAIvariable']['Entries'])){
536 $current = $this->Result['FAIvariable']['Entries'];
537 }
538 $ldap = $this->config->get_ldap_link();
539 $ldap->cd($data['dn']);
540 $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
541 while($attrs = $ldap->fetch()){
542 $attrs['CLASS'] = $data['cn'][0];
543 $current[] = $attrs;
544 }
545 $this->Result['FAIvariable']['Entries'] = $current ;
546 }
548 /* Create a hook entry */
549 function prepare_FAIhook($data)
550 {
551 $current = array();
552 if(isset($this->Result['FAIhook']['Entries'])){
553 $current = $this->Result['FAIhook']['Entries'];
554 }
555 $ldap = $this->config->get_ldap_link();
556 $ldap->cd($data['dn']);
557 $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
558 while($attrs = $ldap->fetch()){
559 $attrs['CLASS'] = $data['cn'][0];
560 $current[$attrs['cn'][0]] = $attrs;
561 }
562 $this->Result['FAIhook']['Entries'] = $current ;
563 }
566 /* Create a new Profile entry */
567 function prepare_FAIprofile($data)
568 {
569 $classes = split("\ ",$data['FAIclass'][0]);
570 foreach($classes as $class){
571 $class = trim($class);
572 $this->resolveObject($class);
573 }
574 }
576 /* Return plugin informations for acl handling * /
577 static function plInfo()
578 {
579 return (array(
580 "plShortName" => _("Summary"),
581 "plDescription" => _("FAI summary"),
582 "plSelfModify" => FALSE,
583 "plDepends" => array(),
584 "plPriority" => 15,
585 "plSection" => array("administration"),
586 "plCategory" => array("workstation","server","fai","ogroups") ,
587 "plProvidedAcls"=> array(
588 "readable" => _("Viewable")),
589 ));
590 }
591 */
592 }
593 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
594 ?>