Code

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