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']));
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>";
192 $str.="<tr><td>";
193 if(!isset($entry['Open'])){
194 $str .= "<input type='image' class='center' src='images/expand.png' name='open_".$key."'> ";
195 $str .= $image." ";
196 $str .= "<b>".$this->objs[$key]['Name']."</b>";
197 }else{
198 $str .= "<input type='image' class='center' src='images/sort_down.png' name='close_".$key."'> ";
199 $str .= $image." ";
200 $str .= "<b>".$this->objs[$key]['Name']."</b>";
202 /* Display FAItemplate FAIhook FAIscript entries */
203 if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
205 if($key == "FAIscript"){
206 krsort($entry['Entries']);
207 foreach($entry['Entries'] as $scripts){
208 ksort($scripts);
209 foreach($scripts as $script){
210 $script['cn'][0] = "(".$script['FAIpriority'][0].") ".$script['cn'][0];
211 $tmp[$script['cn'][0]] = $script;
212 }
213 }
214 $entry['Entries'] = $tmp;
215 }
217 foreach($entry['Entries'] as $cn => $data){
218 $str .="<td></tr><tr><td style='padding-left:20px;'>";
219 $str .= "<a target='_blank'href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
220 $str .= $image." ".$data['cn'][0];
221 if(isset($data['description'][0])){
222 $str .= " [".$data['description'][0]."]";
223 }
224 $str .="</a>";
225 }
226 }
228 /* Create variable entries */
229 if(in_array($key,array("FAIvariable"))) {
230 foreach($entry['Entries'] as $cn => $data){
231 $str .="<td></tr><tr><td style='padding-left:20px;'>";
232 $str .= $image." ".$data['cn'][0];
233 if(isset($data['description'][0])){
234 $str .= " [".$data['description'][0]."] ";
235 }
236 $str .=" = '".$data['FAIvariableContent'][0]."'";
237 }
238 }
240 /* Create packagelist entries */
241 if(in_array($key,array("FAIpackageList"))) {
242 ksort($entry['Entries']);
243 foreach($entry['Entries'] as $cn => $data){
244 $str .="<td></tr><tr><td style='padding-left:20px;'>";
245 $str .= $image." ".$data;
246 }
247 }
249 /* Create partition table entries */
250 if(in_array($key,array("FAIpartitionTable"))) {
251 foreach($entry['Entries'] as $cn => $data){
252 $str .="<td></tr><tr><td style='padding-left:20px;'>";
253 $str .= $image." ".$data['name'];
254 ksort($data['partitions']);
255 $str .= "<table cellspacing=1 style='background:#FFFFFF;'>
256 <tr style='background-color:#BBBBBB;'>
257 <td>"._("No.")."</td>
258 <td>"._("Name")."</td>
259 <td>"._("FS options")."</td>
260 <td>"._("Mount options")."</td>
261 <td>"._("Size in MB")."</td>
262 <td>"._("Mount point")."</td>
263 <td>"._("Type")."</td>
264 </tr>";
266 $i = 0;
267 foreach($data['partitions'] as $key => $part){
268 $i ++;
270 if($i%2 == 1){
271 $c = " class='rowxp1'";
272 $d = " class='list1' style='border:0px;'";
273 }else{
274 $c = " class='rowxp0'";
275 $d = " class='list0' style='border:0px;'";
276 }
278 $str.="<tr $c style='height:10px;'>
279 <td $d>".$i."</td>
280 <td $d>".$part['cn'][0]."</td>
281 <td $d>".$part['FAIfsType'][0]."</td>
282 <td $d>".$part['FAIfsOptions'][0]."</td>
283 <td $d>".$part['FAIpartitionSize'][0]."</td>
284 <td $d>".$part['FAImountPoint'][0]."</td>
285 <td $d>".$part['FAIpartitionType'][0]."</td>
286 </tr>";
287 }
288 $str .="</table>";
289 }
290 }
291 }
292 $str.="</td></tr>" ;
293 }
294 $str .="</table>";
295 return($str);
296 }
299 /* resolve specified object to append it to our object tree */
300 function resolveObject($class)
301 {
302 $ldap = $this->config->get_ldap_link();
303 $dn = $this->Releases [$this->Release];
304 $res = array();
306 /* Try to fetch all types of fai objects with the given cn */
307 foreach($this->objs as $key => $obj){
309 /* Check for objects with cn=$class
310 and add them to our list */
311 $ldap->ls("(&(objectClass=".$key.")(cn=".$class."))",$obj['Tree'].$dn,array("*"));
312 while($attrs = $ldap->fetch()){
314 if(!isset($this->usedClasses[$key][$class])){
315 $this->usedClasses[$key][$class] = true;
316 switch($key){
317 case "FAIprofile": $this->prepare_FAIprofile($attrs);break;
318 case "FAIscript": $this->prepare_FAIscript($attrs);break;
319 case "FAIhook": $this->prepare_FAIhook($attrs);break;
320 case "FAIvariable": $this->prepare_FAIvariable($attrs);break;
321 case "FAItemplate": $this->prepare_FAItemplate($attrs);break;
322 case "FAIpackageList": $this->prepare_FAIpackageList($attrs);break;
323 case "FAIpartitionTable": $this->prepare_FAIpartitionTable($attrs);break;
324 }
325 }
326 }
327 }
328 return($res);
329 }
332 /* Prepare fai script */
333 function prepare_FAIscript($data)
334 {
335 if(isset($this->Result['FAIscript']['Entries'])){
336 $current = $this->Result['FAIscript']['Entries'];
337 }else{
338 $current = array();
339 }
340 $ldap = $this->config->get_ldap_link();
341 $ldap->cd($data['dn']);
342 $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
343 while($attrs = $ldap->fetch()){
344 $current[$attrs['FAIpriority'][0]][$attrs['cn'][0]] = $attrs;
345 }
346 $this->Result['FAIscript']['Entries'] = $current ;
347 }
350 /* Prepare fai script */
351 function prepare_FAIpartitionTable($data)
352 {
353 if(isset($this->Result['FAIpartitionTable']['Entries'])){
354 $current = $this->Result['FAIpartitionTable']['Entries'];
355 }else{
356 $current = array();
357 }
359 /* get subentries */
360 $ldap = $this->config->get_ldap_link();
361 $ldap->cd($data['dn']);
362 $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
363 $disks = array();
365 /* Create an array which is sortable by priority */
366 while($attrs = $ldap->fetch()){
367 if(isset($attrs['description'][0])){
368 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
369 }else{
370 $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
371 }
372 $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
373 $disks[$attrs['cn'][0]]['partitions'] = array();
374 }
376 /* Sort by priority */
377 foreach($disks as $key => $disk){
378 $ldap->cd($disk['dn']);
379 $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
380 while($attrs = $ldap->fetch()){
381 if(!isset($attrs['FAIfsOptions'][0])){
382 $attrs['FAIfsOptions'][0] = "";
383 }
384 $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
385 }
386 }
387 $this->Result['FAIpartitionTable']['Entries'] = $disks;
388 }
391 /* Create template entry */
392 function prepare_FAItemplate($data)
393 {
394 $current = array();
395 if(isset($this->Result['FAItemplate']['Entries'])){
396 $current = $this->Result['FAItemplate']['Entries'];
397 }
399 $ldap = $this->config->get_ldap_link();
400 $ldap->cd($data['dn']);
401 $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
402 while($attrs = $ldap->fetch()){
403 $current[$attrs['cn'][0]] = $attrs;
404 }
405 $this->Result['FAItemplate']['Entries'] = $current ;
406 }
409 /* Create a package list */
410 function prepare_FAIpackageList($data)
411 {
412 $current = array();
413 if(isset($this->Result['FAIpackageList']['Entries'])){
414 $current = $this->Result['FAIpackageList']['Entries'];
415 }
416 if(isset($data['FAIpackage'])){
417 unset($data['FAIpackage']['count']);
418 foreach($data['FAIpackage'] as $pkg){
419 $current[$pkg] = $pkg;
420 }
421 }
422 $this->Result['FAIpackageList']['Entries'] = $current ;
423 }
426 /* Create a variable entry */
427 function prepare_FAIvariable($data)
428 {
429 $current = array();
430 if(isset($this->Result['FAIvariable']['Entries'])){
431 $current = $this->Result['FAIvariable']['Entries'];
432 }
433 $ldap = $this->config->get_ldap_link();
434 $ldap->cd($data['dn']);
435 $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
436 while($attrs = $ldap->fetch()){
437 $current[$attrs['cn'][0]] = $attrs;
438 }
439 $this->Result['FAIvariable']['Entries'] = $current ;
440 }
442 /* Create a hook entry */
443 function prepare_FAIhook($data)
444 {
445 $current = array();
446 if(isset($this->Result['FAIhook']['Entries'])){
447 $current = $this->Result['FAIhook']['Entries'];
448 }
449 $ldap = $this->config->get_ldap_link();
450 $ldap->cd($data['dn']);
451 $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
452 while($attrs = $ldap->fetch()){
453 $current[$attrs['cn'][0]] = $attrs;
454 }
455 $this->Result['FAIhook']['Entries'] = $current ;
456 }
459 /* Create a new Profile entry */
460 function prepare_FAIprofile($data)
461 {
462 $classes = split("\ ",$data['FAIclass'][0]);
463 foreach($classes as $class){
464 $class = trim($class);
465 $this->resolveObject($class);
466 }
467 }
468 }
469 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
470 ?>