Code

Updated workstation 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,$new_release), 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     $this->update_fai_cache();
612     if($this->si_fai_action_failed){
613       $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
614     }elseif($this->fai_activated){
616       $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
617       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
618       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
619       $smarty->assign("FAIrelease"  , $this->FAIrelease);
620       $smarty->assign("FAIclasses"  , $this->selectable_classes());
622       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
624       $div = new divSelectBox("WSFAIscriptClasses");
625       $div -> SetHeight("110");
626       $str_up     = " &nbsp;<input type='image' src='images/sort_up.png'    name='sort_up_%s'    value='%s'>";
627       $str_down   = " &nbsp;<input type='image' src='images/sort_down.png'  name='sort_down_%s'  value='%s'>";
628       $str_remove = " &nbsp;<input type='image' src='images/edittrash.png'  name='fai_remove_%s' value='%s'>";
629       $str_empty  = " &nbsp;<img src='images/empty.png' alt=\"\" width='7'>"; 
631       /* Get classes */
632       if($this->FAIdebianMirror == "inherited"){
633         $tmp = $this->InheritedFAIclass;
634       }else{
635         $tmp = $this->FAIclass;
636       }
638       /* Get invalid classes */
639       $invalid = $this->get_invalid_classes($tmp);
641       /* Draw every single entry */
642       $i = 1;
643       foreach($tmp as $class){
645         /* Mark invalid classes. (Not in selected release)
646          */
647         $marker = "";
648         if(in_array_ics($class,$invalid)){
649           $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
650         }
652         /* Create up/down priority icons  
653          * Skip this, if we have inherited the FAI classes.
654          */
655         if($this->FAIdebianMirror == "inherited"){
656           $str = "";
657         }else{
658           if($i==1){
659             $str = $str_empty.$str_down.$str_remove;
660           }elseif($i == count($this->FAIclass)){
661             $str = $str_up.$str_empty.$str_remove;
662           }else{
663             $str = $str_up.$str_down.$str_remove;
664           }
665         }
666         $i ++ ; 
668         /* Get Description tag 
669          *  There may be several FAI objects with the same class name, 
670          *   use the description from FAIprofile, if possible.
671          */  
672         $desc = ""; 
673         if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
674           foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
675             if(isset($types['Desc'])){
676               $desc= $types['Desc'];
677               if($types['Type'] == "FAIprofile"){
678                 break;
679               }
680             }
681           }
682         }
683         if(!empty($desc)){
684           $desc = "&nbsp;[".trim($desc)."]";
685         }        
687         $div->AddEntry(array(
688               array("string"=>$class.$desc.$marker),
689               array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
690               ));
691       }  
692       $smarty->assign("FAIScriptlist",$div->DrawList()); 
693     }// END FAI output generation 
695     /* Radio button group */
696     if (preg_match("/G/", $this->bootmode)) {
697       $smarty->assign("graphicalbootup", "checked");
698     } else {
699       $smarty->assign("graphicalbootup", "");
700     }
701     if (preg_match("/T/", $this->bootmode)) {
702       $smarty->assign("textbootup", "checked");
703     } else {
704       $smarty->assign("textbootup", "");
705     }
706     if (preg_match("/D/", $this->bootmode)) {
707       $smarty->assign("debugbootup", "checked");
708     } else {
709       $smarty->assign("debugbootup", "");
710     }
712     /* Show main page */
713     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
714   }
717   function remove_from_parent()
718   {
719     $this->handle_post_events("remove");
720     new log("remove","workstation/".get_class($this),$this->dn);
721   }
724   /* Save data to object */
725   function save_object()
726   {
727     $old_mirror  = $this->FAIdebianMirror;
728     plugin::save_object();
730     /* Update release */
731     if($old_mirror != $this->FAIdebianMirror){
732       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
733         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
734       }
735     }
737     if(isset($_POST['WorkstationStarttabPosted'])){
738       if(isset($_POST['gotoLdap_inherit'])){
739         $this->gotoLdap_inherit = TRUE;
740       }else{
741         $this->gotoLdap_inherit = FALSE;
742       }
744       /* Save group radio buttons */
745       if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
746         $this->bootmode= $_POST["bootmode"];
747       }
749       /* Save kernel parameters */
750       if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
751         $this->customParameters= $_POST["customParameters"];
752       }
753     }
754   }
757   /* Save to LDAP */
758   function save()
759   {
761     /* Depending on the baseobject (Ogroup / WS) we
762      *  use another set of objectClasses
763      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
764      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
765      */
766     if(isset($this->parent->by_object['ogroup'])){
767       $this->objectclasses = array("gotoWorkstationTemplate");
768     }elseif(isset($this->parent->by_object['workgeneric'])){
769       $this->objectclasses = array("GOhard");
770     }elseif(isset($this->parent->by_object['servgeneric'])){
771       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
772     }else{
773       print "Object Type Configuration : unknown";
774       exit();
775     }
777     /* Append FAI class */
778     if($this->fai_activated){
779       $this->objectclasses[]  = "FAIobject";
780     }
782     /* Find proper terminal path for tftp configuration
783        FIXME: This is suboptimal when the default has changed to
784        another location! */
785     if (($this->gotoTerminalPath == "default")){
786       $ldap= $this->config->get_ldap_link();
788       /* Strip relevant part from dn, keep trailing ',' */
789       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalou')."/i", "", $this->dn);
790       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
792       /* Walk from top to base and try to load default values for
793          'gotoTerminalPath'. Abort when an entry is found. */
794       while (TRUE){
795         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
797         $ldap->cat("cn=default,".get_ou('terminalou').$tmp.
798             $this->config->current['BASE'], array('gotoTerminalPath'));
799         $attrs= $ldap->fetch();
800         if (isset($attrs['gotoTerminalPath'])){
801           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
802           break;
803         }
805         /* Nothing left? */
806         if ($tmp == ""){
807           break;
808         }
809       }
810     }
812     /* Add semi automatic values */
813     // FIXME: LDAP Server may not be set here...
814     $this->gotoKernelParameters= "ldap=".base64_encode($this->gotoLdapServer);
816     switch ($this->bootmode){
817       case "D":
818         $this->gotoKernelParameters.= " debug";
819       break;
820       case "G":
821         $this->gotoKernelParameters.= " splash=silent";
822       break;
823     }
824     if ($this->customParameters != ""){
825       $this->gotoKernelParameters.= " o ".$this->customParameters;
826     }
828     plugin::save();
830     unset( $this->attrs['FAIrelease'] );
831     $str = "";
833     /* Skip FAI attribute handling if not necessary */
834     if($this->fai_activated && !$this->si_fai_action_failed){
835       if($this->FAIdebianMirror == "inherited"){
836         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
837       }else{
838         foreach($this->FAIclass as $class){
839           $str .= $class." ";
840         }
841         $str = trim($str);
842         if(empty($this->attrs['FAIclass'])){
843           $this->attrs['FAIclass'] = array();
844         }else{
845           $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
846         }
847       }
848     }
850     /* Add missing arrays */
851     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
852       if (isset ($this->$val) && count ($this->$val) != 0){
853     
854         $this->attrs["$val"]= array_unique($this->$val);
855       }
856       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
857     }
859     /* Prepare list of ldap servers */
860     $this->attrs['gotoLdapServer'] = array();
861     if(!$this->gotoLdap_inherit){
862       $i = 0;
863       foreach($this->gotoLdapServers as $server){
864         $i ++;
865         $this->attrs['gotoLdapServer'][] = $i.":".$server;
866       }
867     }
869     /* Check if LDAP server has changed */
870     $ldap_changed= false;
871     if (isset($this->saved_attributes['gotoLdapServer'])){
872       if (isset($this->attrs['gotoLdapServer']) && $this->attrs['gotoLdapServer'] != $this->saved_attributes['gotoLdapServer']){
873         $ldap_changed= true;
874       }
875     } else {
876       if (isset($this->attrs['gotoLdapServer'])){
877         $ldap_changed= true;
878       }
879     }
881     if (($this->attrs['gotoBootKernel'] == "default-inherited") || ($this->attrs['gotoBootKernel'] == "%default%")){
882       $this->attrs['gotoBootKernel']= array();
883     }
885     /* if mirror == none stop saving this attribute */
886     if($this->FAIdebianMirror == "none"){
887       $this->FAIdebianMirror = "";
888     }
889    
890     /* Get FAIstate from object, the generic tab could have changed it during execute */
891     $ldap= $this->config->get_ldap_link();
892     $ldap->cd($this->dn);
895     /* Skip FAI attribute handling if not necessary */
896     if($this->fai_activated && !$this->si_fai_action_failed){
897       $ldap->cat($this->dn,array("FAIstate"));
898       $checkFAIstate = $ldap->fetch();
900       /* Remove FAI objects if no FAI class is selected */ 
901       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
902         $this->attrs['FAIclass']        = array();
903         $this->attrs['FAIdebianMirror'] = array();
904       }
905     }
908     /* prepare share settings */
909     $tmp = array();
910     foreach($this->gotoShares as $name => $settings){
911       $tmp2= split("\|",$name);
912       $name = $tmp2[0];
913       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
914     }
915     $this->attrs['gotoShare']=$tmp;
917     $this->cleanup();
918     $ldap->modify ($this->attrs); 
919     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
921     if (!$ldap->success()){
922       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
923     }
924     $this->handle_post_events("modify");
926     /* Check if LDAP server has changed */
927     if ($ldap_changed && class_available("DaemonEvent")){
928       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
929       $o_queue = new gosaSupportDaemon();
930       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
931         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
932         $macs = array();
933     
934         /* Get list of macAddresses 
935          */
936         if(isset($this->parent->by_object['ogroup'])){
937         
938           /* If we are an object group, add all member macs 
939            */
940           $p = $this->parent->by_object['ogroup'];
941           foreach($p->memberList as $dn => $obj){
942             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
943               $macs[] = $p->objcache[$dn]['macAddress'];
944             }
945           }
946         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
948           /* We are a workstation. Add current mac.
949            */
950           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
951           if(!empty($mac)){
952             $macs[] = $mac;
953           }          
954         }
956         /* Trigger event for all member objects 
957          */
958         foreach($macs as $mac){
959           $tmp = new $evt['CLASS_NAME']($this->config);
960           $tmp->set_type(TRIGGERED_EVENT);
961           $tmp->add_targets(array($mac));
962           if(!$o_queue->append($tmp)){
963             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
964           }
965         }
966       }
967     }
968   }
971   /* Add value to array, check if unique */
972   function add_list (&$array, $value)
973   {
974     if ($value != ""){
975       $array[]= $value;
976       sort($array);
977       array_unique ($array);
978     }
979   }
982   /* Delete value to array, check if unique */
983   function del_list (&$array, $list)
984   {
985     $tmp= array();
986     foreach ($array as $mod){
987       if (!in_array($mod, $list)){
988         $tmp[]= $mod;
989       }
990     }
991     $array= $tmp;
992   }
994   /* Generate ListBox frindly output for the defined shares
995    * Possibly Add or remove an attribute here,
996    */
997   function printOutAssignedShares()
998   {
999     $a_return = array();
1000     if(is_array($this->gotoShares)){
1001       foreach($this->gotoShares as $share){
1002         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
1003       }
1004     }
1005     return($a_return);
1006   }
1010   function PrepareForCopyPaste($source)
1011   {
1012     plugin::PrepareForCopyPaste($source);    
1013     $source_o = new workstartup ($this->config, $source['dn']);
1014     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
1015           "gotoKernelParameters","gotoShares","customParameters") as $attr){
1016       $this->$attr = $source_o->$attr;
1017     }
1018   }
1020   
1021   function array_switch_item($ar,$from,$to)
1022   {
1023     if(!is_array($ar)){
1024       return(false);
1025     }
1026     if(!isset($ar[$from])){
1027       return(false);
1028     }
1029     if(!isset($ar[$to])){
1030       return(false);
1031     }
1033     $tmp = $ar[$from];
1034     $ar[$from] = $ar[$to];    
1035     $ar[$to] = $tmp;    
1036     return($ar);
1037   }
1040   /* Return plugin informations for acl handling */ 
1041   static function plInfo()
1042   {
1043     return (array( 
1044           "plShortName"   => _("Startup"),
1045           "plDescription" => _("System startup"),
1046           "plSelfModify"  => FALSE,
1047           "plDepends"     => array(),
1048           "plPriority"    => 9,
1049           "plSection"     => array("administration"),           
1050           "plCategory"    => array("workstation","server","ogroups"),
1052           "plProvidedAcls"=> array(
1053             "gotoLdapServer"        => _("Ldap server"),
1054             "gotoBootKernel"        => _("Boot kernel"),
1055             "gotoKernelParameters"  => _("Kernel parameter"),
1057             "gotoModules"           => _("Kernel modules"),
1058             "gotoShare"             => _("Shares"),
1060             "FAIclass"              => _("FAI classes"),
1061             "FAIdebianMirror"       => _("Debian mirror"),
1062             "FAIrelease"            => _("Debian release"),
1064             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
1065           ));
1066   }
1069   /* Updates release dns 
1070    *  and reads all classes for the current release, 
1071    *  if not already done ($this->cache).
1072    */
1073   function update_fai_cache($first_call = FALSE)
1074   {
1075     $force = FALSE;
1076     $this->si_fai_action_failed = FALSE;
1077   
1078     $start = microtime(TRUE);  
1080     /* Get the list of available servers and their releases. 
1081      */
1082     if($force || !isset($this->cache['SERVERS'])){
1084       /* Only add inherit option, if we are part in an object group
1085        */
1086       if($this->member_of_ogroup){
1087         $this->cache['SERVERS']['inherited']=array();
1088       }
1090       $o_queue = new gosaSupportDaemon();
1091       $tmp = $o_queue->FAI_get_server(); 
1092       if($o_queue->is_error()){
1093         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1094         $this->si_fai_action_failed = TRUE;
1095         $this->cache = array();
1096         return;
1097       }else{
1098         foreach($tmp as $entry){
1099           $rel = $entry['RELEASE'];
1100           $this->cache['SERVERS']['auto'][$rel] = $rel;
1101           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
1102         }
1103       }
1104     }
1106     /* Build up arrays, without checks */
1107     if(!$first_call){
1109       /* Check if the selected mirror is available */
1110       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1111         $this->FAIdebianMirror = "auto";
1112         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1113         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1114       }
1116       /* Check if the selected release is available */
1117       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1118         trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1119         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1120       }
1121     }
1123     /* Get classes for release from cache. 
1124      * Or build cache
1125      */
1126     if($this->FAIdebianMirror == "inherited"){
1127       $release = $this->InheritedFAIrelease;
1128     }else{
1129       $release = $this->FAIrelease;
1130     }
1132     if($force || !isset($this->cache['CLASSES'][$release])){
1134       /* Get the list of available servers and their releases.
1135        */
1136       $o_queue = new gosaSupportDaemon();
1137       $tmp = $o_queue->FAI_get_classes($release);
1138       $this->cache['CLASSES'][$release] = array();
1139       if($o_queue->is_error()){
1140         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1141         $this->si_fai_action_failed = TRUE;
1142         $this->cache=array();
1143         return;
1144       }else{
1145         foreach($tmp as $entry){
1146           $class = $entry['CLASS'];
1147           $this->cache['CLASSES'][$release][$class] = $this->analyse_fai_object($entry); 
1148         }
1149       }
1151       /* Add object caught from external hook
1152        */
1153       $lines= $this->GetHookElements();
1154       foreach ($lines as $hline){
1155         $entries= split(";", $hline);
1156         $server = $entries['0'];
1157         $url    = $entries['1'];
1158         if (!empty($url)){
1160           /* Split releases */
1161           if (isset($entries[2])){
1162             $releases= split(",", $entries[2]);
1164             foreach ($releases as $release_data){
1165               $release_c  = preg_replace('/:.*$/', '', $release_data);
1166               $sections_c = split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1167               $classes_c  = split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1169               if($release_c == $release){
1170                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1171                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1172                 foreach ($classes_c as $class){
1173                   if ($class != ""){
1174                     $this->cache['CLASSES'][$release_c][$class]= array();
1175                   }
1176                 }
1177               }
1178             }
1179           }
1180         }
1181       }
1182     }
1183 #    echo sprintf("Took %.6f <br> ",microtime(1) - $start);
1184   }
1187   /* This function return an array containing all 
1188    *  invalid classes for the selected server/release
1189    */
1190   function get_invalid_classes($classes)
1191   {
1192     $this->update_fai_cache();
1193     if($this->FAIdebianMirror == "inherited"){
1194       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1195     }else{
1196       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1197     }
1200     /* Detect all classes that are not valid 
1201      *  for the selected release 
1202      */
1203     $NA = array();
1204     foreach($classes as $class){
1205       if(!isset($release_classes[$class])){
1206         $NA[] = $class;
1207       }
1208     }
1209     return($NA);
1210   }  
1212   
1213   /* Get all selectable classes for the ui select box
1214    */
1215   function selectable_classes()
1216   {
1217     $this->update_fai_cache();
1219     if($this->FAIdebianMirror == "inherited"){
1220       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1221     }else{
1222       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1223     }
1225     $Abbr ="";
1226     $ret= array();
1227     foreach($classes as $class_name => $class_types){
1228       if(!in_array($class_name,$this->FAIclass)){
1229         foreach($class_types as $type){
1230           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1231             $Abbr .= $type['Abbr']." ";
1232           }
1233         }
1234         $ret[$class_name] = trim($Abbr);
1235       }
1236     }
1237     uksort($ret, 'strnatcasecmp');
1238     return($ret);
1239   }
1242   /* Analyse FAI object and return an array with usefull informations like 
1243    *  FAIobject type.
1244    */
1245   function analyse_fai_object($attr)
1246   {
1247     $tmp = array();
1248     switch($attr['TYPE']){
1250       case 'FAIpackageList':
1251         $tmp["Type"]= 'FAIpackageList';
1252         $tmp["Abbr"]= 'Pl';
1253         break;
1254       case 'FAItemplate': 
1255         $tmp["Type"]= 'FAItemplate'; 
1256         $tmp["Abbr"]= 'T'; 
1257         break;
1258       case 'FAIvariable':
1259         $tmp["Type"]= 'FAIvariable'; 
1260         $tmp["Abbr"]= 'V'; 
1261         break;
1262       case 'FAIscript':
1263         $tmp["Type"]= 'FAIscript'; 
1264         $tmp["Abbr"]= 'S'; 
1265         break;
1266       case 'FAIhook':
1267         $tmp["Type"]= 'FAIhook'; 
1268         $tmp["Abbr"]= 'H'; 
1269         break;
1270       case 'FAIpartitionTable':
1271         $tmp["Type"]= 'FAIpartitionTable'; 
1272         $tmp["Abbr"]= 'Pt'; 
1273         break;
1274       case 'FAIprofile':
1275         $tmp["Type"]= 'FAIprofile'; 
1276         $tmp["Abbr"]= 'P'; 
1277         break;
1278       default: trigger_error("Unknown FAI object type!");;
1279     }
1280     return($tmp);
1281   }
1284   /* Return repository hook output, if possible.
1285    */
1286   function GetHookElements()
1287   {
1288     $ret = array();
1289     $cmd= $this->config->search("servrepository", "REPOSITORY_HOOK",array('tabs'));
1290     if(!empty($cmd)){
1291       $res = shell_exec($cmd);
1292       $res2 = trim($res);
1293       if((!$res)){
1294         msg_dialog(_("Configuration error"), msgPool::cmdexecfailed("REPOSITORY_HOOK", $cmd), ERROR_DIALOG);
1295       }elseif(empty($res2)){
1296         msg_dialog(_("Configuration error"), _("REPOSITORY_HOOK returned no result!"), ERROR_DIALOG);
1297       }else{
1298         $tmp = split("\n",$res);
1299         foreach($tmp as $line){
1300           if(empty($line)) continue;
1301           $ret[]= $line;
1302         }
1303       }
1304     }
1305     return($ret);
1306   }
1309   /* This function creates the release name out of a dn 
1310    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1311    */
1312   function dn_to_release_name($dn)
1313   {
1314     $relevant = preg_replace("/,".normalizePreg(get_ou("faiou")).".*$/","",$dn);
1315     $parts    = array_reverse(split("\,",$relevant));
1316     $str ="";
1317     foreach($parts as $part){
1318       $str .= preg_replace("/^ou=/","",$part)."/";
1319     }
1320     return(preg_replace("/\/$/","",$str)); 
1321   }
1324 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1325 ?>