Code

Updated work startup
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_workstationStartup.inc
1 <?php
2 class workstartup extends plugin
3 {
4   /* Ldap server list */
5   var $gotoLdapServers    = array();
6   var $gotoLdapServerList = array();
7   var $gotoLdap_inherit   = FALSE;
9   /* Generic terminal attributes */
10   var $bootmode             = "G";
11   var $gotoBootKernel       = "default-inherited";
12   var $gotoKernelParameters = "";
13   var $gotoLdapServer       = "default-inherited";
14   var $gotoModules          = array();
15   var $gotoAutoFs           = array();
16   var $gotoFilesystem       = array();
17   var $gotoTerminalPath     = "";
18   var $gotoBootKernels      = array();
20   /* attribute list for save action */
21   var $attributes           = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", 
22                                     "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror", "FAIrelease");
23   var $objectclasses        = array("GOhard", "FAIobject");
25   /* Share */
26   var $gotoShares         = array();// Currently Share Option
27   var $gotoShare          = "";     // currently selected Share Option
28   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
29   var $gotoAvailableShares= array();// Available Shares for this account
31   /* Helper */
32   var $customParameters   = "";
33   var $orig_dn            = "";
34   var $ignore_account     = TRUE;
35  
36   /* FAI class selection */ 
37   var $FAIclass           = array();  // The currently selected classes 
38   var $FAIrelease         = "";
39   var $FAIdebianMirror    = "auto";
40   var $si_fai_action_failed = FALSE;
42   var $cache              = array(); // Used as cache in fai mehtods
44   var $FAIstatus          = "";
45   var $FAIclasses         = array();
47   var $view_logged        = FALSE;
48   
49   /* FAI class selection */
50   var $InheritedFAIclass       = array();
51   var $InheritedFAIrelease     = "";
52   var $InheritedFAIdebianMirror= "auto";
54   var $CopyPasteVars    = array("gotoModules","gotoShares");
55   var $fai_activated    = FALSE;
56   var $o_group_dn       = "";
57   var $member_of_ogroup = FALSE;
59   function workstartup (&$config, $dn= NULL, $parent= NULL)
60   {
61     /* Check if FAI is active */
62     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
63     if(!empty($tmp)){
64       $this->fai_activated = TRUE;
65     }else{
66       $this->attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "gotoShare");
67       $this->objectclasses  = array("GOhard");
68     }
70     plugin::plugin ($config, $dn, $parent);
72     /* Check object group membership */
73     if(!isset($this->parent->by_object['ogroup'])){
74       $ldap = $this->config->get_ldap_link();
75       $ldap->cd ($this->config->current['BASE']);
76       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn","dn"));
77       if($ldap->count()){
78         $this->member_of_ogroup = TRUE;
79         $attrs = $ldap->fetch();
80         $this->o_group_dn = $attrs['dn'];
81       }
82     }
84     /* Creating a list of valid Mirrors 
85      * none will not be saved to ldap.
86      */
87     $ldap   = $this->config->get_ldap_link();
88     $ldap->cd($this->config->current['BASE']);
89     foreach($this->config->data['SERVERS']['LDAP'] as $server) {
90       $this->gotoLdapServerList[]= $server; 
91     }
93     /* Get list of assigned ldap servers 
94      */ 
95     if(isset($this->attrs['gotoLdapServer'])){
96       unset($this->attrs['gotoLdapServer']['count']);
97       sort($this->attrs['gotoLdapServer']);
98       foreach($this->attrs['gotoLdapServer'] as $value){
99         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
100       }
101     }
102     if(!count($this->gotoLdapServers) && $this->member_of_ogroup){ 
103       $this->gotoLdap_inherit = TRUE;
104     }
106     /* FAI Initialization
107        Skip this if FAI is not activated 
108      */
109     if($this->fai_activated) {
111       $this->update_fai_cache(TRUE);
113       /* Parse used FAIclasses (stored as string).
114        * The single classes are seperated by ' '.
115        * There is also the release type given, after first
116        *  occurrence of ':'.
117        */
118       $this->FAIclass =array();
119       if(isset($this->attrs['FAIclass'][0])){
120         $tmp = split(" ",$this->attrs['FAIclass'][0]);
121         $tmp2 =array();  
123         foreach($tmp as $class){
124           if( ":" == $class[0] ) {
125             $this->FAIrelease = trim(substr($class, 1));
126           }else{
127             $tmp2[$class] = $class;
128           }
129         }
130         $this->FAIclass = $tmp2;
131       }
132     }
134     /* Get arrays */
135     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
136       if (isset($this->attrs["$val"]["count"])){
137         for ($i= 0; $i<$this->attrs["count"]; $i++){
138           if (isset($this->attrs["$val"][$i])){
139             array_push($this->$val, $this->attrs["$val"][$i]);
140           }
141         }
142       }
143       sort ($this->$val);
144       $this->$val= array_unique($this->$val);
145     }
147     /* Parse Kernel Parameters to decide what boot mode is enabled */
148     if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
149       $this->bootmode= "G";
150     } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
151       $this->bootmode= "D";
152     } elseif ($this->gotoKernelParameters == "") {
153       $this->bootmode= "G";
154     } else {
155       $this->bootmode= "T";
156     }
157     if (preg_match("/ o /", $this->gotoKernelParameters)){
158       $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
159     } else {
160       $this->customParameters= "";
161     }
163     /* Prepare Shares */
164     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
165       unset($this->attrs['gotoShare']['count']);
166       foreach($this->attrs['gotoShare'] as $share){
167         $tmp = $tmp2 = array();
168         $tmp = split("\|",$share);
169         $tmp2['server']      =$tmp[0];
170         $tmp2['name']        =$tmp[1];
171         $tmp2['mountPoint']  =$tmp[2];
172         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
173       }
174     }
176     $this->gotoShareSelections= $config->getShareList(true);
177     $this->gotoAvailableShares= $config->getShareList(false);
178     $tmp2 = array();
179   
181     $this->orig_dn= $this->dn;
183     /* Handle inheritance value "default" */
184     if ($this->member_of_ogroup){
185       $this->gotoBootKernels= array("default-inherited" => '['._("inherited").']'); 
186     }
188     /* If we are member in an object group,
189      *  we have to handle inherited values.
190      * So you can see what is inherited.
191      */
192     if ($this->member_of_ogroup){
194       if(count($this->FAIclass)==0 && $this->FAIrelease == ""){
195         $this->FAIdebianMirror = "inherited";
196       }
198       if($this->fai_activated){
199         $map= array("gotoBootKernel","FAIclass","FAIdebianMirror");
200       }else{
201         $map= array("gotoBootKernel");
202       }
204       $ldap = $this->config->get_ldap_link();
205       $ldap->cat($this->o_group_dn);
206       $attrs= $ldap->fetch();
208       foreach ($map as $name){
209         if (!isset($attrs[$name][0])){
210           continue;
211         }
213         switch ($name){
214           case 'gotoBootKernel':
215             $this->gotoBootKernels['default-inherited']=  _("inherited").' ['.$attrs[$name][0].']' ;
216             break;
218           case 'FAIclass':
219             $str = split(":",$attrs[$name][0]);
220             $this->InheritedFAIclass    = split("\ ",trim($str[0]));
221             $this->InheritedFAIrelease  = trim($str[1]);
222             break;
224           case 'FAIdebianMirror':
225             $this->InheritedFAIdebianMirror = $attrs[$name][0];
226             break;
227         }
228       }
229     }
232     if($this->fai_activated && !$this->si_fai_action_failed){
234       /* Check if the current mirror is available 
235        */
236       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
237         if(count($this->FAIclass)){
238           msg_dialog::display(_("Error"), sprintf(_("FAI mirror '%s' is not available - setting to mirror 'auto'!"), $this->FAIdebianMirror), ERROR_DIALOG);
239         }
240         $this->FAIdebianMirror = "auto";
241         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
242         $this->cache =array();
243         $this->update_fai_cache();
244         
245       }
246   
247       /* Check if the current mirror is available 
248        */
249       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
250         $new_release = key($this->cache['SERVERS'][$this->FAIdebianMirror]); 
251         if(count($this->FAIclass)){
252           msg_dialog::display(_("Error"), sprintf(_("FAI release '%s' is not available on mirror '%s' - setting to release '%s'!"), $this->FAIrelease, $this->FAIdebianmirror), ERROR_DIALOG);
253         }
254         $this->FAIrelease = $new_release;
255         $this->cache =array();
256         $this->update_fai_cache();
257       }
258     }
260     /* Get list of boot kernels */
261     if (isset($this->config->data['TABS'])){
262       $command= $this->config->search(get_class($this), "KERNELS",array('tabs'));
263       if (!check_command($command)){
264         $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
265             get_class($this));
266       } else {
267         $fh= popen($command, "r");
268         while (!feof($fh)) {
269           $buffer= trim(fgets($fh, 256));
270           if(!empty($buffer)){
271             $name=$value = $buffer;
272             if(preg_match("/:/",$buffer)){
273               $name = preg_replace("/:.*$/","",$buffer);
274               $value= preg_replace("/^.*:/","",$buffer);
275               $this->gotoBootKernels[$name]= $name.":".$value;
276             }else{
277               $this->gotoBootKernels[$name]= $value;
278             }
279           }
280         }
281         pclose($fh);
282       }
283     }
285     /* Turn to default, if we've nothing to inherit */
286     if (!isset($this->gotoBootKernels['default-inherited']) && $this->gotoBootKernel == "default-inherited"){
287       $this->gotoBootKernel= "default";
288     }
289   }
291   
292   function check()
293   {
294     $messages = array();
295     
296     /* Call common method to give check the hook */
297     $messages= plugin::check();
299     /* If there are packages selected, but no mirror show error */   
300     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
301       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
302     }
304     return($messages);
305   }
307   function execute()
308   {
309         /* Call parent execute */
310         plugin::execute();
312     if($this->is_account && !$this->view_logged){
313       $this->view_logged = TRUE;
314       new log("view","workstation/".get_class($this),$this->dn);
315     }
317     /* Do we need to flip is_account state? */
318     if(isset($_POST['modify_state'])){
319       if($this->is_account && $this->acl_is_removeable()){
320         $this->is_account= FALSE;
321       }elseif(!$this->is_account && $this->acl_is_createable()){
322         $this->is_account= TRUE;
323       }
324     }
326     /* Do we represent a valid terminal? */
327     if (!$this->is_account && $this->parent === NULL){
328       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
329         msgPool::noValidExtension(_("workstation"))."</b>";
330       return ($display);
331     }
333     /* Add module */
334     if (isset ($_POST['add_module'])){
335       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoModule")){
336         $this->add_list ($this->gotoModules, $_POST['module']);
337       }
338     }
340     /* Delete module */
341     if (isset ($_POST['delete_module'])){
342       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoModule")){
343         $this->del_list ($this->gotoModules, $_POST['modules_list']);
344       }
345     }
347     /* FAI class management */
348     if($this->fai_activated){
349       if(((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))) && ($this->acl_is_writeable("FAIclass"))){
350         $found = 0 ; 
352         /* If this new class/profile will attach a second partition table
353          * to our list of classes, abort and show a message.
354          */
355         foreach($this->FAIclass as $name){
356           if(isset($this->FAIclassInfo[$name])){
357             foreach($this->FAIclassInfo[$name] as $atr){
358               if(isset($atr['obj'])){
359                 if($atr['obj'] == "FAIpartitionTable"){
360                   $found ++ ; 
361                 }
362               }
363             }
364           }
365         }
367         if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
368           msg_dialog(_("Error"), _("There is already a profile containing a partition table in your configuration!") , ERROR_DIALOG);
369         }else{
370           $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
371         }
372       }
374       $sort = false;
376       /* Move one used class class one position up or down */
377       if($this->acl_is_writeable("FAIclass")){
378         foreach($_POST as $name => $val){
380           $sort_type = false;
381           if((preg_match("/sort_up/",$name))&&(!$sort)){
382             $sort_type = "sort_up_";
383           }
384           if((preg_match("/sort_down/",$name))&&(!$sort)){
385             $sort_type = "sort_down_";
386           }
388           if(($sort_type)&&(!$sort)){
389             $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name)));
390             $sort = true;
392             $last = -1;
393             $change_down  = -1;
395             /* Create array with numeric index */ 
396             $tmp = array();
397             foreach($this->FAIclass as $class){
398               $tmp [] = $class;
399             }
401             /* Walk trough array */
402             foreach($tmp as $key => $faiName){
403               if($faiName == $value){
404                 if($sort_type == "sort_up_"){
405                   if($last != -1){
406                     $change_down= $last;
407                   }
408                 }else{
409                   if(isset($tmp[$key+1])){
410                     $change_down = $key;
411                   }
412                 }
413               }
414               $last = $key;
415             }
417             $tmp2 = array();
418             $skip = false;    
420             foreach($tmp as $ky => $vl){
422               if($ky == $change_down){
423                 $skip = $vl;
424               }else{
425                 $tmp2[$vl] = $vl;
426               }
427               if(($skip != false)&&($ky != $change_down)){
428                 $tmp2[$skip]  = $skip;
429                 $skip =false;
430               }
431             }   
432             $this->FAIclass = $tmp2; 
433           }
435           if(preg_match("/fai_remove/i",$name)){
436             $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/fai_remove_/i","",$name)));
437             unset($this->FAIclass[$value]);
438           }
439         }
440       }
442       /* Delete selected class from our list */
443       if($this->acl_is_writeable("FAIclass")){
444         if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
445           if(isset($this->FAIclass[$_POST['FAIclassSel']])){
446             unset($this->FAIclass[$_POST['FAIclassSel']]);
447           }
448         }
449       }
450     }// END fai handling
452     /* Show main page */
453     $smarty= get_smarty();
455     /* Assign ACLs to smarty */
456     $tmp = $this->plInfo();
457     foreach($tmp['plProvidedAcls'] as $name => $translation){
458       $smarty->assign($name."ACL",$this->getacl($name));
459     } 
461     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
463     /* In this section server shares will be defined
464      * A user can select one of the given shares and a mount point
465      *  and attach this combination to his setup.
466      */
467     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
468     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
470     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
471      * This entry will be, a combination of mountPoint and sharedefinitions
472      */
473     if((isset($_POST['gotoShareAdd'])) && ($this->acl_is_writeable("gotoShare"))) {
474       /* We assign a share to this user, if we don't know where to mount the share */
475       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
476         msg_dialog(_("Error"), msgPool::required(_("Mount point")), ERROR_DIALOG);
477       }else{
478         if(count($this->gotoAvailableShares)){
479           $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
480           $s_mount = $_POST['gotoShareMountPoint'];
481           /* Preparing the new assignment */
482           $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
483           $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
484         }
485       }
486     }
488     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
489      * If there is no defined share selected, we will abort the deletion without any message
490      */
491     if(($this->acl_is_writeable("gotoShare"))&& (isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
492       unset($this->gotoShares[$_POST['gotoShare']]);
493     }
495     $smarty->assign("gotoShares",$this->printOutAssignedShares());
496     $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
497     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
498     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
500     /* Create divSelectBox for ldap server selection
501      */
502     $SelectBoxLdapServer = new divSelectBox("LdapServer");
503     $SelectBoxLdapServer->SetHeight(130);
505     /* Add new ldap server to the list */
506     if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
507       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
508         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
509         if(!in_array($to_add,$this->gotoLdapServers)){
510           $this->gotoLdapServers[] = $to_add;
511         }
512       }
513     }
514     
515     /* Move ldap servers up and down */
516     if(!$this->gotoLdap_inherit){
517       foreach($_POST as $name => $value){
518         if(preg_match("/sort_ldap_up_/",$name)){
519           $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
520           $from =  $id;  
521           $to   =  $id -1;
522           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
523           if($tmp){
524             $this->gotoLdapServers = $tmp;
525           }
526           break;
527         }
528         if(preg_match("/sort_ldap_down_/",$name)){
529           $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
530           $from =  $id;  
531           $to   =  $id +1;
532           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
533           if($tmp){
534             $this->gotoLdapServers = $tmp;
535           }
536           break;
537         }
538         if(preg_match("/gotoLdapRemove_/",$name)){
539           $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
540           $value = $this->gotoLdapServers[$id];
541           $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
542           break;
543         }
544       } 
545     }
546   
547     /* Add Entries */
548     foreach($this->gotoLdapServers as $key => $server){
549       /* Announce missing entries */
550       if(!in_array($server,$this->gotoLdapServerList)){
551         $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
552       }
554       /* Convert old style entry */
555       if (!preg_match('%:ldap://%', $server)){
556         $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
558       /* Beautify new style entries */
559       } else {
560         $server= preg_replace("/^[^:]+:/", "", $server);
561       }
563       $SelectBoxLdapServer->AddEntry(
564           array(array("string" => $server),
565             array("string" => 
566               "<input class='center' type='image' src='images/sort_up.png' name='sort_ldap_up_".$key."'>&nbsp;".
567               "<input class='center' type='image' src='images/sort_down.png' name='sort_ldap_down_".$key."'>&nbsp;".
568               "<input class='center' type='image' src='images/edittrash.png' name='gotoLdapRemove_".$key."'>",
569               "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
570     }    
572     if($this->gotoLdap_inherit){
573       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
574     }else{
575       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
576     }
577     
578     $list = array();
579     foreach($this->gotoLdapServerList as $key => $entry){
580       if(!in_array($entry,$this->gotoLdapServers)){
582         /* Convert old style entry */
583         if (!preg_match('%:ldap://%', $entry)){
584           $entry= "ldap://".preg_replace('/^([^:]+):/', '\1/', $entry);
586         /* Beautify new style entries */
587         } else {
588           $entry= preg_replace("/^[^:]+:/", "", $entry);
589         }
591         $list[$key] = $entry;
592       }
593     }
594     $smarty->assign("gotoLdapServers",    $SelectBoxLdapServer->DrawList());
595     $smarty->assign("gotoLdapServerList", $list);
596     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
597     $smarty->assign("JS",  session::get('js'));
599     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
600       $smarty->assign("$val", $this->$val);
601     }
603     /* Values */
604     foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
605       $smarty->assign($val, $this->$val);
606     }
608     $smarty->assign("fai_activated",$this->fai_activated);
610     /* Create FAI output */
611     if($this->si_fai_action_failed){
612       $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
613     }elseif($this->fai_activated){
615       $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
616       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
617       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
618       $smarty->assign("FAIrelease"  , $this->FAIrelease);
619       $smarty->assign("FAIclasses"  , $this->selectable_classes());
621       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
623       $div = new divSelectBox("WSFAIscriptClasses");
624       $div -> SetHeight("110");
625       $str_up     = " &nbsp;<input type='image' src='images/sort_up.png'    name='sort_up_%s'    value='%s'>";
626       $str_down   = " &nbsp;<input type='image' src='images/sort_down.png'  name='sort_down_%s'  value='%s'>";
627       $str_remove = " &nbsp;<input type='image' src='images/edittrash.png'  name='fai_remove_%s' value='%s'>";
628       $str_empty  = " &nbsp;<img src='images/empty.png' alt=\"\" width='7'>"; 
630       /* Get classes */
631       if($this->FAIdebianMirror == "inherited"){
632         $tmp = $this->InheritedFAIclass;
633       }else{
634         $tmp = $this->FAIclass;
635       }
637       /* Get invalid classes */
638       $invalid = $this->get_invalid_classes($tmp);
640       /* Draw every single entry */
641       $i = 1;
642       foreach($tmp as $class){
644         /* Mark invalid classes. (Not in selected release)
645          */
646         $marker = "";
647         if(in_array_ics($class,$invalid)){
648           $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
649         }
651         /* Create up/down priority icons  
652          * Skip this, if we have inherited the FAI classes.
653          */
654         if($this->FAIdebianMirror == "inherited"){
655           $str = "";
656         }else{
657           if($i==1){
658             $str = $str_empty.$str_down.$str_remove;
659           }elseif($i == count($this->FAIclass)){
660             $str = $str_up.$str_empty.$str_remove;
661           }else{
662             $str = $str_up.$str_down.$str_remove;
663           }
664         }
665         $i ++ ; 
667         /* Get Description tag 
668          *  There may be several FAI objects with the same class name, 
669          *   use the description from FAIprofile, if possible.
670          */  
671         $desc = ""; 
672         if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
673           foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
674             if(isset($types['Desc'])){
675               $desc= $types['Desc'];
676               if($types['Type'] == "FAIprofile"){
677                 break;
678               }
679             }
680           }
681         }
682         if(!empty($desc)){
683           $desc = "&nbsp;[".trim($desc)."]";
684         }        
686         $div->AddEntry(array(
687               array("string"=>$class.$desc.$marker),
688               array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
689               ));
690       }  
691       $smarty->assign("FAIScriptlist",$div->DrawList()); 
692     }// END FAI output generation 
694     /* Radio button group */
695     if (preg_match("/G/", $this->bootmode)) {
696       $smarty->assign("graphicalbootup", "checked");
697     } else {
698       $smarty->assign("graphicalbootup", "");
699     }
700     if (preg_match("/T/", $this->bootmode)) {
701       $smarty->assign("textbootup", "checked");
702     } else {
703       $smarty->assign("textbootup", "");
704     }
705     if (preg_match("/D/", $this->bootmode)) {
706       $smarty->assign("debugbootup", "checked");
707     } else {
708       $smarty->assign("debugbootup", "");
709     }
711     /* Show main page */
712     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
713   }
716   function remove_from_parent()
717   {
718     $this->handle_post_events("remove");
719     new log("remove","workstation/".get_class($this),$this->dn);
720   }
723   /* Save data to object */
724   function save_object()
725   {
726     $old_mirror  = $this->FAIdebianMirror;
727     plugin::save_object();
729     /* Update release */
730     if($old_mirror != $this->FAIdebianMirror){
731       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
732         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
733       }
734     }
736     if(isset($_POST['WorkstationStarttabPosted'])){
737       if(isset($_POST['gotoLdap_inherit'])){
738         $this->gotoLdap_inherit = TRUE;
739       }else{
740         $this->gotoLdap_inherit = FALSE;
741       }
743       /* Save group radio buttons */
744       if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
745         $this->bootmode= $_POST["bootmode"];
746       }
748       /* Save kernel parameters */
749       if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
750         $this->customParameters= $_POST["customParameters"];
751       }
752     }
753   }
756   /* Save to LDAP */
757   function save()
758   {
760     /* Depending on the baseobject (Ogroup / WS) we
761      *  use another set of objectClasses
762      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
763      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
764      */
765     if(isset($this->parent->by_object['ogroup'])){
766       $this->objectclasses = array("gotoWorkstationTemplate");
767     }elseif(isset($this->parent->by_object['workgeneric'])){
768       $this->objectclasses = array("GOhard");
769     }elseif(isset($this->parent->by_object['servgeneric'])){
770       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
771     }else{
772       print "Object Type Configuration : unknown";
773       exit();
774     }
776     /* Append FAI class */
777     if($this->fai_activated){
778       $this->objectclasses[]  = "FAIobject";
779     }
781     /* Find proper terminal path for tftp configuration
782        FIXME: This is suboptimal when the default has changed to
783        another location! */
784     if (($this->gotoTerminalPath == "default")){
785       $ldap= $this->config->get_ldap_link();
787       /* Strip relevant part from dn, keep trailing ',' */
788       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalou')."/i", "", $this->dn);
789       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
791       /* Walk from top to base and try to load default values for
792          'gotoTerminalPath'. Abort when an entry is found. */
793       while (TRUE){
794         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
796         $ldap->cat("cn=default,".get_ou('terminalou').$tmp.
797             $this->config->current['BASE'], array('gotoTerminalPath'));
798         $attrs= $ldap->fetch();
799         if (isset($attrs['gotoTerminalPath'])){
800           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
801           break;
802         }
804         /* Nothing left? */
805         if ($tmp == ""){
806           break;
807         }
808       }
809     }
811     /* Add semi automatic values */
812     // FIXME: LDAP Server may not be set here...
813     $this->gotoKernelParameters= "ldap=".base64_encode($this->gotoLdapServer);
815     switch ($this->bootmode){
816       case "D":
817         $this->gotoKernelParameters.= " debug";
818       break;
819       case "G":
820         $this->gotoKernelParameters.= " splash=silent";
821       break;
822     }
823     if ($this->customParameters != ""){
824       $this->gotoKernelParameters.= " o ".$this->customParameters;
825     }
827     plugin::save();
829     unset( $this->attrs['FAIrelease'] );
830     $str = "";
832     /* Skip FAI attribute handling if not necessary */
833     if($this->fai_activated){
834       if($this->FAIdebianMirror == "inherited"){
835         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
836       }else{
837         foreach($this->FAIclass as $class){
838           $str .= $class." ";
839         }
840         $str = trim($str);
841         if(empty($this->attrs['FAIclass'])){
842           $this->attrs['FAIclass'] = array();
843         }else{
844           $this->attrs['FAIclass']= $str.":".$this->FAIrelease;
845         }
846       }
847     }
849     /* Add missing arrays */
850     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
851       if (isset ($this->$val) && count ($this->$val) != 0){
852     
853         $this->attrs["$val"]= array_unique($this->$val);
854       }
855       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
856     }
858     /* Prepare list of ldap servers */
859     $this->attrs['gotoLdapServer'] = array();
860     if(!$this->gotoLdap_inherit){
861       $i = 0;
862       foreach($this->gotoLdapServers as $server){
863         $i ++;
864         $this->attrs['gotoLdapServer'][] = $i.":".$server;
865       }
866     }
868     /* Check if LDAP server has changed */
869     $ldap_changed= false;
870     if (isset($this->saved_attributes['gotoLdapServer'])){
871       if (isset($this->attrs['gotoLdapServer']) && $this->attrs['gotoLdapServer'] != $this->saved_attributes['gotoLdapServer']){
872         $ldap_changed= true;
873       }
874     } else {
875       if (isset($this->attrs['gotoLdapServer'])){
876         $ldap_changed= true;
877       }
878     }
880     if (($this->attrs['gotoBootKernel'] == "default-inherited") || ($this->attrs['gotoBootKernel'] == "%default%")){
881       $this->attrs['gotoBootKernel']= array();
882     }
884     /* if mirror == none stop saving this attribute */
885     if($this->FAIdebianMirror == "none"){
886       $this->FAIdebianMirror = "";
887     }
888    
889     /* Get FAIstate from object, the generic tab could have changed it during execute */
890     $ldap= $this->config->get_ldap_link();
891     $ldap->cd($this->dn);
894     /* Skip FAI attribute handling if not necessary */
895     if($this->fai_activated){
896       $ldap->cat($this->dn,array("FAIstate"));
897       $checkFAIstate = $ldap->fetch();
899       /* Remove FAI objects if no FAI class is selected */ 
900       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
901         $this->attrs['FAIclass']        = array();
902         $this->attrs['FAIdebianMirror'] = array();
903       }
904     }
907     /* prepare share settings */
908     $tmp = array();
909     foreach($this->gotoShares as $name => $settings){
910       $tmp2= split("\|",$name);
911       $name = $tmp2[0];
912       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
913     }
914     $this->attrs['gotoShare']=$tmp;
916     $this->cleanup();
917     $ldap->modify ($this->attrs); 
918     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
920     if (!$ldap->success()){
921       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
922     }
923     $this->handle_post_events("modify");
925     /* Check if LDAP server has changed */
926     if ($ldap_changed && class_available("DaemonEvent")){
927       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
928       $o_queue = new gosaSupportDaemon();
929       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
930         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
931         $macs = array();
932     
933         /* Get list of macAddresses 
934          */
935         if(isset($this->parent->by_object['ogroup'])){
936         
937           /* If we are an object group, add all member macs 
938            */
939           $p = $this->parent->by_object['ogroup'];
940           foreach($p->memberList as $dn => $obj){
941             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
942               $macs[] = $p->objcache[$dn]['macAddress'];
943             }
944           }
945         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
947           /* We are a workstation. Add current mac.
948            */
949           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
950           if(!empty($mac)){
951             $macs[] = $mac;
952           }          
953         }
955         /* Trigger event for all member objects 
956          */
957         foreach($macs as $mac){
958           $tmp = new $evt['CLASS_NAME']($this->config);
959           $tmp->set_type(TRIGGERED_EVENT);
960           $tmp->add_targets(array($mac));
961           if(!$o_queue->append($tmp)){
962             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
963           }
964         }
965       }
966     }
967   }
970   /* Add value to array, check if unique */
971   function add_list (&$array, $value)
972   {
973     if ($value != ""){
974       $array[]= $value;
975       sort($array);
976       array_unique ($array);
977     }
978   }
981   /* Delete value to array, check if unique */
982   function del_list (&$array, $list)
983   {
984     $tmp= array();
985     foreach ($array as $mod){
986       if (!in_array($mod, $list)){
987         $tmp[]= $mod;
988       }
989     }
990     $array= $tmp;
991   }
993   /* Generate ListBox frindly output for the defined shares
994    * Possibly Add or remove an attribute here,
995    */
996   function printOutAssignedShares()
997   {
998     $a_return = array();
999     if(is_array($this->gotoShares)){
1000       foreach($this->gotoShares as $share){
1001         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
1002       }
1003     }
1004     return($a_return);
1005   }
1009   function PrepareForCopyPaste($source)
1010   {
1011     plugin::PrepareForCopyPaste($source);    
1012     $source_o = new workstartup ($this->config, $source['dn']);
1013     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
1014           "gotoKernelParameters","gotoShares","customParameters") as $attr){
1015       $this->$attr = $source_o->$attr;
1016     }
1017   }
1019   
1020   function array_switch_item($ar,$from,$to)
1021   {
1022     if(!is_array($ar)){
1023       return(false);
1024     }
1025     if(!isset($ar[$from])){
1026       return(false);
1027     }
1028     if(!isset($ar[$to])){
1029       return(false);
1030     }
1032     $tmp = $ar[$from];
1033     $ar[$from] = $ar[$to];    
1034     $ar[$to] = $tmp;    
1035     return($ar);
1036   }
1039   /* Return plugin informations for acl handling */ 
1040   static function plInfo()
1041   {
1042     return (array( 
1043           "plShortName"   => _("Startup"),
1044           "plDescription" => _("System startup"),
1045           "plSelfModify"  => FALSE,
1046           "plDepends"     => array(),
1047           "plPriority"    => 9,
1048           "plSection"     => array("administration"),           
1049           "plCategory"    => array("workstation","server","ogroups"),
1051           "plProvidedAcls"=> array(
1052             "gotoLdapServer"        => _("Ldap server"),
1053             "gotoBootKernel"        => _("Boot kernel"),
1054             "gotoKernelParameters"  => _("Kernel parameter"),
1056             "gotoModules"           => _("Kernel modules"),
1057             "gotoShare"             => _("Shares"),
1059             "FAIclass"              => _("FAI classes"),
1060             "FAIdebianMirror"       => _("Debian mirror"),
1061             "FAIrelease"            => _("Debian release"),
1063             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
1064           ));
1065   }
1068   /* Updates release dns 
1069    *  and reads all classes for the current release, 
1070    *  if not already done ($this->cache).
1071    */
1072   function update_fai_cache($first_call = FALSE)
1073   {
1074     $force = FALSE;
1075     $this->si_fai_action_failed = false;
1076   
1077     $start = microtime(TRUE);  
1079     /* Get the list of available servers and their releases. 
1080      */
1081     if($force || !isset($this->cache['SERVERS'])){
1083       /* Only add inherit option, if we are part in an object group
1084        */
1085       if($this->member_of_ogroup){
1086         $this->cache['SERVERS']['inherited']=array();
1087       }
1089       $o_queue = new gosaSupportDaemon();
1090       $tmp = $o_queue->FAI_get_server(); 
1091       if($o_queue->is_error()){
1092         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1093         $this->si_fai_action_failed = TRUE;
1094         return;
1095       }else{
1096         foreach($tmp as $entry){
1097           $rel = $entry['RELEASE'];
1098           $this->cache['SERVERS']['auto'][$rel] = $rel;
1099           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
1100         }
1101       }
1102     }
1104     /* Build up arrays, without checks */
1105     if(!$first_call){
1107       /* Check if the selected mirror is available */
1108       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1109         $this->FAIdebianMirror = "auto";
1110         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1111         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1112       }
1114       /* Check if the selected release is available */
1115       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1116         trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1117         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1118       }
1119     }
1121     /* Get classes for release from cache. 
1122      * Or build cache
1123      */
1124     if($this->FAIdebianMirror == "inherited"){
1125       $release = $this->InheritedFAIrelease;
1126     }else{
1127       $release = $this->FAIrelease;
1128     }
1130     if($force || !isset($this->cache['CLASSES'][$release])){
1132       /* Get the list of available servers and their releases.
1133        */
1134       $o_queue = new gosaSupportDaemon();
1135       $tmp = $o_queue->FAI_get_classes($release);
1136       $this->cache['CLASSES'][$release] = array();
1137       if($o_queue->is_error()){
1138         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1139         $this->si_fai_action_failed = TRUEi;
1140         return;
1141       }else{
1142         foreach($tmp as $entry){
1143           $class = $entry['CLASS'];
1144           $this->cache['CLASSES'][$release][$class] = $this->analyse_fai_object($entry); 
1145         }
1146       }
1148       /* Add object caught from external hook
1149        */
1150       $lines= $this->GetHookElements();
1151       foreach ($lines as $hline){
1152         $entries= split(";", $hline);
1153         $server = $entries['0'];
1154         $url    = $entries['1'];
1155         if (!empty($url)){
1157           /* Split releases */
1158           if (isset($entries[2])){
1159             $releases= split(",", $entries[2]);
1161             foreach ($releases as $release_data){
1162               $release_c  = preg_replace('/:.*$/', '', $release_data);
1163               $sections_c = split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1164               $classes_c  = split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1166               if($release_c == $release){
1167                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1168                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1169                 foreach ($classes_c as $class){
1170                   if ($class != ""){
1171                     $this->cache['CLASSES'][$release_c][$class]= array();
1172                   }
1173                 }
1174               }
1175             }
1176           }
1177         }
1178       }
1179     }
1180 #    echo sprintf("Took %.6f <br> ",microtime(1) - $start);
1181   }
1184   /* This function return an array containing all 
1185    *  invalid classes for the selected server/release
1186    */
1187   function get_invalid_classes($classes)
1188   {
1189     $this->update_fai_cache();
1190     if($this->FAIdebianMirror == "inherited"){
1191       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1192     }else{
1193       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1194     }
1197     /* Detect all classes that are not valid 
1198      *  for the selected release 
1199      */
1200     $NA = array();
1201     foreach($classes as $class){
1202       if(!isset($release_classes[$class])){
1203         $NA[] = $class;
1204       }
1205     }
1206     return($NA);
1207   }  
1209   
1210   /* Get all selectable classes for the ui select box
1211    */
1212   function selectable_classes()
1213   {
1214     $this->update_fai_cache();
1216     if($this->FAIdebianMirror == "inherited"){
1217       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1218     }else{
1219       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1220     }
1222     $Abbr ="";
1223     $ret= array();
1224     foreach($classes as $class_name => $class_types){
1225       if(!in_array($class_name,$this->FAIclass)){
1226         foreach($class_types as $type){
1227           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1228             $Abbr .= $type['Abbr']." ";
1229           }
1230         }
1231         $ret[$class_name] = trim($Abbr);
1232       }
1233     }
1234     uksort($ret, 'strnatcasecmp');
1235     return($ret);
1236   }
1239   /* Analyse FAI object and return an array with usefull informations like 
1240    *  FAIobject type.
1241    */
1242   function analyse_fai_object($attr)
1243   {
1244     $tmp = array();
1245     switch($attr['TYPE']){
1247       case 'FAIpackageList':
1248         $tmp["Type"]= 'FAIpackageList';
1249         $tmp["Abbr"]= 'Pl';
1250         break;
1251       case 'FAItemplate': 
1252         $tmp["Type"]= 'FAItemplate'; 
1253         $tmp["Abbr"]= 'T'; 
1254         break;
1255       case 'FAIvariable':
1256         $tmp["Type"]= 'FAIvariable'; 
1257         $tmp["Abbr"]= 'V'; 
1258         break;
1259       case 'FAIscript':
1260         $tmp["Type"]= 'FAIscript'; 
1261         $tmp["Abbr"]= 'S'; 
1262         break;
1263       case 'FAIhook':
1264         $tmp["Type"]= 'FAIhook'; 
1265         $tmp["Abbr"]= 'H'; 
1266         break;
1267       case 'FAIpartitionTable':
1268         $tmp["Type"]= 'FAIpartitionTable'; 
1269         $tmp["Abbr"]= 'Pt'; 
1270         break;
1271       case 'FAIprofile':
1272         $tmp["Type"]= 'FAIprofile'; 
1273         $tmp["Abbr"]= 'P'; 
1274         break;
1275       default: trigger_error("Unknown FAI object type!");;
1276     }
1277     return($tmp);
1278   }
1281   /* Return repository hook output, if possible.
1282    */
1283   function GetHookElements()
1284   {
1285     $ret = array();
1286     $cmd= $this->config->search("servrepository", "REPOSITORY_HOOK",array('tabs'));
1287     if(!empty($cmd)){
1288       $res = shell_exec($cmd);
1289       $res2 = trim($res);
1290       if((!$res)){
1291         msg_dialog(_("Configuration error"), msgPool::cmdexecfailed("REPOSITORY_HOOK", $cmd), ERROR_DIALOG);
1292       }elseif(empty($res2)){
1293         msg_dialog(_("Configuration error"), _("REPOSITORY_HOOK returned no result!"), ERROR_DIALOG);
1294       }else{
1295         $tmp = split("\n",$res);
1296         foreach($tmp as $line){
1297           if(empty($line)) continue;
1298           $ret[]= $line;
1299         }
1300       }
1301     }
1302     return($ret);
1303   }
1306   /* This function creates the release name out of a dn 
1307    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1308    */
1309   function dn_to_release_name($dn)
1310   {
1311     $relevant = preg_replace("/,".normalizePreg(get_ou("faiou")).".*$/","",$dn);
1312     $parts    = array_reverse(split("\,",$relevant));
1313     $str ="";
1314     foreach($parts as $part){
1315       $str .= preg_replace("/^ou=/","",$part)."/";
1316     }
1317     return(preg_replace("/\/$/","",$str)); 
1318   }
1321 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1322 ?>