Code

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