Code

Updated teminal template
[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     if (($this->attrs['gotoBootKernel'] == "default-inherited") || ($this->attrs['gotoBootKernel'] == "%default%")){
857       $this->attrs['gotoBootKernel']= array();
858     }
860     /* if mirror == none stop saving this attribute */
861     if($this->FAIdebianMirror == "none"){
862       $this->FAIdebianMirror = "";
863     }
864    
865     /* Get FAIstate from object, the generic tab could have changed it during execute */
866     $ldap= $this->config->get_ldap_link();
867     $ldap->cd($this->dn);
870     /* Skip FAI attribute handling if not necessary */
871     if($this->fai_activated){
872       $ldap->cat($this->dn,array("FAIstate"));
873       $checkFAIstate = $ldap->fetch();
875       /* Remove FAI objects if no FAI class is selected */ 
876       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
877         $this->attrs['FAIclass']        = array();
878         $this->attrs['FAIdebianMirror'] = array();
879       }
880     }
883     /* prepare share settings */
884     $tmp = array();
885     foreach($this->gotoShares as $name => $settings){
886       $tmp2= split("\|",$name);
887       $name = $tmp2[0];
888       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
889     }
890     $this->attrs['gotoShare']=$tmp;
892     $this->cleanup();
893     $ldap->modify ($this->attrs); 
894     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
896     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/startup with dn '%s' failed."),$this->dn));
897     $this->handle_post_events("modify");
898   }
901   /* Add value to array, check if unique */
902   function add_list (&$array, $value)
903   {
904     if ($value != ""){
905       $array[]= $value;
906       sort($array);
907       array_unique ($array);
908     }
909   }
912   /* Delete value to array, check if unique */
913   function del_list (&$array, $list)
914   {
915     $tmp= array();
916     foreach ($array as $mod){
917       if (!in_array($mod, $list)){
918         $tmp[]= $mod;
919       }
920     }
921     $array= $tmp;
922   }
924   /* Generate ListBox frindly output for the defined shares
925    * Possibly Add or remove an attribute here,
926    */
927   function printOutAssignedShares()
928   {
929     $a_return = array();
930     if(is_array($this->gotoShares)){
931       foreach($this->gotoShares as $share){
932         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
933       }
934     }
935     return($a_return);
936   }
940   function PrepareForCopyPaste($source)
941   {
942     plugin::PrepareForCopyPaste($source);    
943     $source_o = new workstartup ($this->config, $source['dn']);
944     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
945           "gotoKernelParameters","gotoShares","customParameters") as $attr){
946       $this->$attr = $source_o->$attr;
947     }
948   }
950   
951   function array_switch_item($ar,$from,$to)
952   {
953     if(!is_array($ar)){
954       return(false);
955     }
956     if(!isset($ar[$from])){
957       return(false);
958     }
959     if(!isset($ar[$to])){
960       return(false);
961     }
963     $tmp = $ar[$from];
964     $ar[$from] = $ar[$to];    
965     $ar[$to] = $tmp;    
966     return($ar);
967   }
970   /* Return plugin informations for acl handling */ 
971   static function plInfo()
972   {
973     return (array( 
974           "plShortName"   => _("Startup"),
975           "plDescription" => _("System startup"),
976           "plSelfModify"  => FALSE,
977           "plDepends"     => array(),
978           "plPriority"    => 9,
979           "plSection"     => array("administration"),           
980           "plCategory"    => array("workstation","server","ogroups"),
982           "plProvidedAcls"=> array(
983             "gotoLdapServer"        => _("Ldap server"),
984             "gotoBootKernel"        => _("Boot kernel"),
985             "gotoKernelParameters"  => _("Kernel parameter"),
987             "gotoModules"           => _("Kernel modules"),
988             "gotoShare"             => _("Shares"),
990             "FAIclass"              => _("FAI classes"),
991             "FAIdebianMirror"       => _("Debian mirror"),
992             "FAIrelease"            => _("Debian release"),
994             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
995           ));
996   }
999   /* Updates release dns 
1000    *  and reads all classes for the current release, 
1001    *  if not already done ($this->cache).
1002    */
1003   function update_fai_cache($first_call = FALSE)
1004   {
1005     $force = FALSE;
1007     /* Get the list of available servers and their releases. 
1008      */
1009     if($force || !isset($this->cache['SERVERS'])){
1010       $ldap = $this->config->get_ldap_link();
1011       $ldap->cd($this->config->current['BASE']);
1012       $ldap->search("(&(FAIrepository=*)(objectClass=FAIrepositoryServer))",array("FAIrepository"));
1013       $this->cache['SERVERS'] = array();
1015       /* Only add inherit option, if we are part in an object group
1016        */
1017       if($this->member_of_ogroup){
1018         $this->cache['SERVERS']['inherited']=array();
1019       }
1021       /* Add auto value  
1022        */
1023       $this->cache['SERVERS']['auto'] = array();
1024       $sort_by = array("auto");
1025       $server_tmp = array("auto"=>array());
1026       while($attr = $ldap->fetch()){
1027         if(isset($attr['FAIrepository'])){
1028           for($i = 0 ; $i < $attr['FAIrepository']['count'] ; $i ++ ){
1029             $rep = $attr['FAIrepository'][$i];
1030             $tmp = split("\|",$rep);
1031             if(count($tmp)==4){
1032               $sections = split(",",$tmp[3]);
1033               $release  = $tmp[2];
1034               $server   = $tmp[1];
1035               $url      = $tmp[0];
1036               $server_tmp[$url][$release]=$release;
1037               $server_tmp['auto'][$release]=$release;
1038               $sort_by[$url] = $url;
1039             }
1040           }
1041         }
1042       }
1043       natcasesort($sort_by);
1044       foreach($sort_by as $name){
1045         $releases = $server_tmp[$name];
1046         natcasesort($releases);
1047         $this->cache['SERVERS'][$name] = $releases; 
1048       }
1049     }
1051     /* Build up arrays, without checks */
1052     if(!$first_call){
1054       /* Check if the selected mirror is available */
1055       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1056         $this->FAIdebianMirror = "auto";
1057         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1058         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1059       }
1061       /* Check if the selected release is available */
1062       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1063         trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1064         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1065       }
1066     }
1068     /* Get classes for release from cache. 
1069      * Or build cache
1070      */
1071     if($this->FAIdebianMirror == "inherited"){
1072       $release = $this->InheritedFAIrelease;
1073     }else{
1074       $release = $this->FAIrelease;
1075     }
1076     if($force || !isset($this->cache['CLASSES'][$release])){
1078       /*  Create a list of available releases.
1079        *  $this->cache['RELEASE_DNS'][ou=siga...,c=de]         = "siga";
1080        *  $this->cache['RELEASE_DNS'][ou=siga,ou=rc1,...,c=de] = "siga/rc1";
1081        */
1082       if($force || !isset($this->cache['RELEASE_DNS'])){
1083         $this->cache['RELEASE_DNS'] = array();
1084         $fai_ou_parts = preg_replace("/\/.*$/","",$this->FAIrelease);
1085         $ldap = $this->config->get_ldap_link();
1086         $ldap->cd($this->config->current['BASE']);
1087         $ldap->search("(objectClass=FAIbranch)",array("ou"));
1088         while($attrs = $ldap->fetch()){
1089           if(preg_match("/".normalizePreg(get_ou("faiou"))."/",$attrs['dn'])){
1090             $this->cache['RELEASE_DNS'][$attrs['dn']] = $this->dn_to_release_name($attrs['dn']);
1091           }
1092         }
1093       }
1095       /* Create list of available classes for the currenlty selected release.
1096        */
1097       $base = array_search($release,$this->cache['RELEASE_DNS']);
1098       $this->cache['CLASSES'][$release] = array();
1100       if(class_exists("FAI")){
1101         if(!empty($base)){
1102           $filter = "(|(objectClass=FAIpackageList)(objectClass=FAItemplate)(objectClass=FAIvariable)".
1103             "(objectClass=FAIscript)(objectClass=FAIhook)(objectClass=FAIprofile)".
1104             "(objectClass=FAIpartitionTable))";
1105           $list = FAI::get_all_objects_for_given_base($base,$filter,TRUE);
1106           foreach($list as $attrs){
1107             $info = $this->analyse_fai_object($attrs);
1108             if(count($info)){
1109               $this->cache['CLASSES'][$release][$attrs['cn'][0]][] = $info;
1110             }
1111           }
1112         }
1113       }else{
1114         print_red(_("Could not load available classes, the fai extension plugin seems not to be installed."));
1115       }
1117       /* Add object caught from external hook
1118        */
1119       $lines= $this->GetHookElements();
1120       foreach ($lines as $hline){
1121         $entries= split(";", $hline);
1122         $server = $entries['0'];
1123         $url    = $entries['1'];
1124         if (!empty($url)){
1125           
1126           /* Split releases */
1127           if (isset($entries[2])){
1128             $releases= split(",", $entries[2]);
1130             foreach ($releases as $release_data){
1131               $release= preg_replace('/:.*$/', '', $release_data);
1132               $sections = split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1133               $classes  = split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1134               $this->cache['SERVERS'][$url][$release]=$release;
1135               $this->cache['SERVERS']['auto'][$release]=$release; 
1136               foreach ($classes as $class){
1137                 if ($class != ""){
1138                   $this->cache['CLASSES'][$release][$class]= array();
1139                 }
1140               }
1141             }
1142           }
1143         }
1144       }
1145     }
1146   }
1149   /* This function return an array containing all 
1150    *  invalid classes for the selected server/release
1151    */
1152   function get_invalid_classes($classes)
1153   {
1154     $this->update_fai_cache();
1155     if($this->FAIdebianMirror == "inherited"){
1156       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1157     }else{
1158       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1159     }
1162     /* Detect all classes that are not valid 
1163      *  for the selected release 
1164      */
1165     $NA = array();
1166     foreach($classes as $class){
1167       if(!isset($release_classes[$class])){
1168         $NA[] = $class;
1169       }
1170     }
1171     return($NA);
1172   }  
1174   
1175   /* Get all selectable classes for the ui select box
1176    */
1177   function selectable_classes()
1178   {
1179     $this->update_fai_cache();
1181     if($this->FAIdebianMirror == "inherited"){
1182       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1183     }else{
1184       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1185     }
1187     $Abbr ="";
1188     $ret= array();
1189     foreach($classes as $class_name => $class_types){
1190       if(!in_array($class_name,$this->FAIclass)){
1191         foreach($class_types as $type){
1192           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1193             $Abbr .= $type['Abbr']." ";
1194           }
1195         }
1196         $ret[$class_name] = trim($Abbr);
1197       }
1198     }
1199     uksort($ret, 'strnatcasecmp');
1200     return($ret);
1201   }
1204   /* Analyse FAI object and return an array with usefull informations like 
1205    *  FAIobject type.
1206    */
1207   function analyse_fai_object($attr)
1208   {
1209     $tmp2 = array();
1210     if(!isset($attr['description'])){
1211       $attr['description'][0] ="";
1212     }
1213     if(in_array('FAIpackageList',$attr['objectClass'])){
1214       $tmp2["Type"]   = 'FAIpackageList'; 
1215       $tmp2["Abbr"]   = 'Pl';
1216       $tmp2["Desc"]  = $attr['description'][0];
1217     }
1218     if(in_array('FAItemplate',$attr['objectClass'])){
1219       $tmp2["Type"]      = 'FAItemplate'; 
1220       $tmp2["Abbr"]      = 'T'; 
1221       $tmp2["Desc"]  = $attr['description'][0];
1222     }
1223     if(in_array('FAIvariable',$attr['objectClass'])){
1224       $tmp2["Type"]      = 'FAIvariable'; 
1225       $tmp2["Abbr"]      = 'V'; 
1226       $tmp2["Desc"]  = $attr['description'][0];
1227     }
1228     if(in_array('FAIscript',$attr['objectClass'])){
1229       $tmp2["Type"]        = 'FAIscript'; 
1230       $tmp2["Abbr"]        = 'S'; 
1231       $tmp2["Desc"]  = $attr['description'][0];
1232     }
1233     if(in_array('FAIhook',$attr['objectClass'])){
1234       $tmp2["Type"]          = 'FAIhook'; 
1235       $tmp2["Abbr"]          = 'H'; 
1236       $tmp2["Desc"]  = $attr['description'][0];
1237     }
1238     if(in_array('FAIpartitionTable',$attr['objectClass'])){
1239       $tmp2["Type"]= 'FAIpartitionTable'; 
1240       $tmp2["Abbr"]= 'Pt'; 
1241       $tmp2["Desc"]  = $attr['description'][0];
1242     }
1243     if(in_array('FAIprofile',$attr['objectClass'])){
1244       $tmp2["Type"]= 'FAIprofile'; 
1245       $tmp2["Abbr"]= 'P'; 
1246       $tmp2["Desc"]  = $attr['description'][0];
1247     }
1248     return($tmp2);
1249   }
1252   /* Return repository hook output, if possible.
1253    */
1254   function GetHookElements()
1255   {
1256     $ret = array();
1257     $cmd= $this->config->search("servrepository", "REPOSITORY_HOOK",array('tabs'));
1258     if(!empty($cmd)){
1259       $res = shell_exec($cmd);
1260       $res2 = trim($res);
1261       if((!$res)){
1262         print_red(sprintf(_("Can't execute specified REPOSITORY_HOOK '%s' please check your gosa.conf."),$cmd));
1263       }elseif(empty($res2)){
1264         print_red(sprintf(_("The specified REPOSITORY_HOOK '%s', specified in your gosa.conf, returns an empty string."),$cmd));
1265       }else{
1266         $tmp = split("\n",$res);
1267         foreach($tmp as $line){
1268           if(empty($line)) continue;
1269           $ret[]= $line;
1270         }
1271       }
1272     }
1273     return($ret);
1274   }
1277   /* This function creates the release name out of a dn 
1278    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1279    */
1280   function dn_to_release_name($dn)
1281   {
1282     $relevant = preg_replace("/,".normalizePreg(get_ou("faiou")).".*$/","",$dn);
1283     $parts    = array_reverse(split("\,",$relevant));
1284     $str ="";
1285     foreach($parts as $part){
1286       $str .= preg_replace("/^ou=/","",$part)."/";
1287     }
1288     return(preg_replace("/\/$/","",$str)); 
1289   }
1292 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1293 ?>