Code

a5cfa2063def19c0e042c88d59850b4557337528
[gosa.git] / plugins / admin / systems / 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   /* Generic terminal attributes */
10   var $bootmode             = "G";
11   var $goLdapServerList     = array("default");
12   var $gotoBootKernel       = "default";
13   var $gotoKernelParameters = "";
14   var $gotoLdapServer       = "";
15   var $gotoModules          = array();
16   var $gotoAutoFs           = array();
17   var $gotoFilesystem       = array();
18   var $gotoTerminalPath     = "";
19   var $FAIstatus            = "";
20   var $FAIclass             = array();
21   var $FAIclasses           = array();
22   var $FAIclassInfo         = array();
23   var $FAIdebianMirror      = array();
26   /* attribute list for save action */
27   var $attributes     = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror");
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   var $FAIdebianMirrors   = array();
42   function workstartup ($config, $dn= NULL)
43   {
44     plugin::plugin ($config, $dn);
46     /* Creating a list of valid Mirrors 
47      * none will not be saved to ldap.
48      */
49     $this->FAIdebianMirrors   = array();
50     $ldap   = $this->config->get_ldap_link();
51     $ldap->cd($this->config->current['BASE']);
52     $ldap->search("(objectClass=FAIrepository)",array("FAIdebianMirror"));
53     
54     /* attach all attributes with "index => cn" to avoid multiple entries */
55     $this->FAIdebianMirrors['auto']=_("automatic");
56     while($attr = $ldap->fetch()){
57       if(isset($attr['FAIdebianMirror'])){
58         $this->FAIdebianMirrors[$attr['FAIdebianMirror'][0]]=$attr['FAIdebianMirror'][0];
59       }
60     } 
61  
62     /* Get arrays */
63     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
64       if (isset($this->attrs["$val"]["count"])){
65         for ($i= 0; $i<$this->attrs["count"]; $i++){
66           if (isset($this->attrs["$val"][$i])){
67             array_push($this->$val, $this->attrs["$val"][$i]);
68           }
69         }
70       }
71       sort ($this->$val);
72       $this->$val= array_unique($this->$val);
73     }
75     /* Parse Kernel Parameters to decide what boot mode is enabled */
76     if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
77       $this->bootmode= "G";
78     } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
79       $this->bootmode= "D";
80     } elseif ($this->gotoKernelParameters == "") {
81       $this->bootmode= "G";
82     } else {
83       $this->bootmode= "T";
84     }
85     if (preg_match("/ o /", $this->gotoKernelParameters)){
86       $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
87     } else {
88       $this->customParameters= "";
89     }
91     /* Prepare Shares */
92     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
93       unset($this->attrs['gotoShare']['count']);
94       foreach($this->attrs['gotoShare'] as $share){
95         $tmp = $tmp2 = array();
96         $tmp = split("\|",$share);
97         $tmp2['server']      =$tmp[0];
98         $tmp2['name']        =$tmp[1];
99         $tmp2['mountPoint']  =$tmp[2];
100         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
101       }
102     }
104     $this->gotoShareSelections= $config->getShareList(true);
105     $this->gotoAvailableShares= $config->getShareList(false);
106     $tmp2 = array();
107   
108     $ldap   = $this->config->get_ldap_link();
109     $ldap->cd($this->config->current['BASE']);
111     /* Search all FAI objects */
112     $ldap->search("(| (objectClass=FAIpackageList)(objectClass=FAItemplate)
113                       (objectClass=FAIvariable)(objectClass=FAIscript)(objectClass=FAIhook)(objectClass=FAIprofile)
114                       (objectClass=FAIpartitionTable))",array("objectClass","cn"),true);
115     /* Sort all entries, and attach elementtype.
116      * To be able to show the types in the listbox.
117      */
118     while($attr = $ldap->fetch()){
119       $cn = $attr['cn'][0];
120       if(in_array('FAIpackageList',$attr['objectClass'])){
121         $tmp2[$cn]['FAIpackageList']['obj']   = 'FAIpackageList'; 
122         $tmp2[$cn]['FAIpackageList']['kzl']   = 'Pl';
123         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
124       }
125       if(in_array('FAItemplate',$attr['objectClass'])){
126         $tmp2[$cn]['FAItemplate']['obj']      = 'FAItemplate'; 
127         $tmp2[$cn]['FAItemplate']['kzl']      = 'T'; 
128         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
129       }
130       if(in_array('FAIvariable',$attr['objectClass'])){
131         $tmp2[$cn]['FAIvariable']['obj']      = 'FAIvariable'; 
132         $tmp2[$cn]['FAIvariable']['kzl']      = 'V'; 
133         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
134       }
135       if(in_array('FAIscript',$attr['objectClass'])){
136         $tmp2[$cn]['FAIscript']['obj']        = 'FAIscript'; 
137         $tmp2[$cn]['FAIscript']['kzl']        = 'S'; 
138         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
139       }
140       if(in_array('FAIhook',$attr['objectClass'])){
141         $tmp2[$cn]['FAIhook']['obj']          = 'FAIhook'; 
142         $tmp2[$cn]['FAIhook']['kzl']          = 'H'; 
143         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
144       }
145       if(in_array('FAIpartitionTable',$attr['objectClass'])){
146         $tmp2[$cn]['FAIpartitionTable']['obj']= 'FAIpartitionTable'; 
147         $tmp2[$cn]['FAIpartitionTable']['kzl']= 'Pt'; 
148         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
149       }
150       if(in_array('FAIprofile',$attr['objectClass'])){
151         $tmp2[$cn]['FAIprofile']['obj']= 'FAIprofile'; 
152         $tmp2[$cn]['FAIprofile']['kzl']= 'P'; 
153         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
154       }
155     }
157     $this->FAIclassInfo = $tmp2;
159     if((isset($this->FAIclass))&&(!is_array($this->FAIclass))){
160       $tmp = array();
161       $tmp = split(" ",$this->FAIclass);
162       $tmp2 =array();  
163     
164       foreach($tmp as $class){
165         $tmp2[$class] = $class;
166       }
167       $this->FAIclass = $tmp2;
168     }
170     if(!is_array($this->FAIclass)){
171       $this->FAIclass =array();
172     }
174     $this->orig_dn= $this->dn;
175   }
177   /* Create array to display available classes/profiles in a selectbox */
178   function selectFriendlyClasses(){
179     $tmp=array();
180     foreach($this->FAIclasses as $class){
181       $str    = "";
182       $skip = false;
183       if(isset($this->FAIclassInfo[$class])){
184         foreach($this->FAIclassInfo[$class] as $objs){
185           $str .= $objs['kzl']." "; 
186         }
187       }
188       $tmp[$class] = $class."&nbsp; [".$str."]";
189     }
190     return($tmp);
191   }
193   function check()
194   {
195     $messages = array();
196     /* If there are packages selected, but no mirror show error */   
197     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
198       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
199     }
200     return($messages);
201   }
203   function execute()
204   {
205         /* Call parent execute */
206         plugin::execute();
208     /* Do we need to flip is_account state? */
209     if (isset($_POST['modify_state'])){
210       $this->is_account= !$this->is_account;
211     }
213     /* Do we represent a valid terminal? */
214     if (!$this->is_account && $this->parent == NULL){
215       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
216         _("This 'dn' has no terminal features.")."</b>";
217       return ($display);
218     }
220     /* Add module */
221     if (isset ($_POST['add_module'])){
222       if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
223         $this->add_list ($this->gotoModules, $_POST['module']);
224       }
225     }
227     /* Delete module */
228     if (isset ($_POST['delete_module'])){
229       if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
230         $this->del_list ($this->gotoModules, $_POST['modules_list']);
231       }
232     }
234     /* FAI class management */
235     if((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))){
236       $found = 0 ; 
238       /* If this new class/profile will attach a second partition table
239        * to our list of classes, abort and show a message.
240        */
241       foreach($this->FAIclass as $name){
242         if(isset($this->FAIclassInfo[$name])){
243           foreach($this->FAIclassInfo[$name] as $atr){
244             if($atr['obj'] == "FAIpartitionTable"){
245               $found ++ ; 
246             }
247           }
248         }
249       }
251       if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
252         print_red(_("There is already a profile in your selection that contain partition table configurations."));
253       }else{
254         $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
255       }
256     }
258     $sort = false;
259     foreach($_POST as $name => $val){
260       
261       $sort_type = false;
262       if((preg_match("/sort_up/",$name))&&(!$sort)){
263         $sort_type = "sort_up_";
264       }
265       if((preg_match("/sort_down/",$name))&&(!$sort)){
266         $sort_type = "sort_down_";
267       }
268     
269       if(($sort_type)&&(!$sort)){
270         $value = preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name));
271         $sort = true;
272         
273         $last = -1;
274         $change_down  = -1;
275  
276         /* Create array with numeric index */ 
277         $tmp = array();
278         foreach($this->FAIclass as $class){
279           $tmp [] = $class;
280         }
282         /* Walk trough array */
283         foreach($tmp as $key => $faiName){
284           if($faiName == $value){
285             if($sort_type == "sort_up_"){
286               if($last != -1){
287                  $change_down= $last;
288               }
289             }else{
290               if(isset($tmp[$key+1])){
291                 $change_down = $key;
292               }
293             }
294           }
295           $last = $key;
296         }
297  
298         $tmp2 = array();
299         $skip = false;    
300   
301         foreach($tmp as $ky => $vl){
303           if($ky == $change_down){
304             $skip = $vl;
305           }else{
306             $tmp2[$vl] = $vl;
307           }
308           if(($skip != false)&&($ky != $change_down)){
309             $tmp2[$skip]  = $skip;
310             $skip =false;
311           }
312         }   
313         $this->FAIclass = $tmp2; 
314       }
315   
317       if(preg_match("/fai_remove/i",$name)){
318         $value = preg_replace("/_.*$/i","",preg_replace("/fai_remove_/i","",$name));
319         unset($this->FAIclass[$value]);
320       }
321     }
323     /* Delete selected class from our list */
324     if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
325       if(isset($this->FAIclass[$_POST['FAIclassSel']])){
326         unset($this->FAIclass[$_POST['FAIclassSel']]);
327       }
328     }
330     /* Show main page */
331     $smarty= get_smarty();
333     /* In this section server shares will be defined
334      * A user can select one of the given shares and a mount point
335      *  and attach this combination to his setup.
336      */
337     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
338     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
340     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
341      * This entry will be, a combination of mountPoint and sharedefinitions
342      */
343     if(isset($_POST['gotoShareAdd'])){
344       /* We assign a share to this user, if we don't know where to mount the share */
345       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
346         print_red(_("You must specify a valid mount point."));
347       }else{
348         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
349         $s_mount = $_POST['gotoShareMountPoint'];
350         /* Preparing the new assignment */
351         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
352         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
353       }
354     }
356     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
357      * If there is no defined share selected, we will abort the deletion without any message
358      */
359     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
360       unset($this->gotoShares[$_POST['gotoShare']]);
361     }
363     $smarty->assign("gotoShares",$this->printOutAssignedShares());
364     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
366     /* Arrays */
367     $smarty->assign("ldapservers", $this->config->data['SERVERS']['LDAP']);
368     $smarty->assign("gotoLdapServer_select", $this->gotoLdapServer);
369     $smarty->assign("gotoLdapServerACL", chkacl($this->acl, "gotoLdapServer"));
370     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
371       $smarty->assign("$val", $this->$val);
372     }
374     /* Values */
375     foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror") as $val){
376       $smarty->assign($val, $this->$val);
377       $smarty->assign($val."ACL", chkacl($this->acl, $val));
378     }
380     $div = new divSelectBox("WSFAIscriptClasses");
381     
382     $div -> SetHeight("110");
384     $str_up     = " &nbsp;<input type='image' src='images/sort_up.png'    name='sort_up_%s'    value='%s'>";
385     $str_down   = " &nbsp;<input type='image' src='images/sort_down.png'  name='sort_down_%s'  value='%s'>";
386     $str_remove = " &nbsp;<input type='image' src='images/edittrash.png'  name='fai_remove_%s' value='%s'>";
387     $str_empty  = " &nbsp;<img src='images/empty.png'i width='7'>"; 
389     $i = 1;
390     foreach($this->FAIclass as $class){
391       if($i==1){
392         $str = $str_empty.$str_down.$str_remove;
393       }elseif($i == count($this->FAIclass)){
394         $str = $str_up.$str_empty.$str_remove;
395       }else{
396         $str = $str_up.$str_down.$str_remove;
397       }
398       $i ++ ; 
399     
400       $div->AddEntry(array(
401                             array("string"=>$class),
402                             array("string"=>preg_replace("/\%s/",$class,$str),"attach"=>"style='width:50px;border-right:none;'")
403                           ));
404     }  
406     $smarty->assign("FAIScriptlist",$div->DrawList()); 
407     $smarty->assign("FAIdebianMirrors",$this->FAIdebianMirrors);
408     $smarty->assign("FAIclasses",$this->selectFriendlyClasses());
409     $smarty->assign("FAIclassesKeys",array_flip($this->selectFriendlyClasses()));
410     $smarty->assign("FAIclassKeys",$this->FAIclass);
412     /* Radio button group */
413     if (preg_match("/G/", $this->bootmode)) {
414       $smarty->assign("graphicalbootup", "checked");
415     } else {
416       $smarty->assign("graphicalbootup", "");
417     }
418     if (preg_match("/T/", $this->bootmode)) {
419       $smarty->assign("textbootup", "checked");
420     } else {
421       $smarty->assign("textbootup", "");
422     }
423     if (preg_match("/D/", $this->bootmode)) {
424       $smarty->assign("debugbootup", "checked");
425     } else {
426       $smarty->assign("debugbootup", "");
427     }
429     /* ACL's */
430     foreach (array("gotoKernelParameters", "gotoModules", "gotoFilesystem","FAIclass") as $value){
431       $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
432     }
434     /* Show main page */
435     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
436   }
438   function remove_from_parent()
439   {
440     $this->handle_post_events("remove");
441   }
444   /* Save data to object */
445   function save_object()
446   {
447     plugin::save_object();
449     /* Save group radio buttons */
450     if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
451       $this->bootmode= $_POST["bootmode"];
452     }
454     /* Save kernel parameters */
455     if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
456       $this->customParameters= $_POST["customParameters"];
457     }
458   }
461   /* Save to LDAP */
462   function save()
463   {
465     /* Depending on the baseobject (Ogroup / WS) we
466      *  use another set of objectClasses
467      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
468      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
469      */
470     if(isset($this->parent->by_object['ogroup'])){
471       $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
472     }elseif(isset($this->parent->by_object['workgeneric'])){
473       $this->objectclasses = array("GOhard", "FAIobject");
474     }else{
475       print "unknown";
476       exit();
477     }
479     /* Find proper terminal path for tftp configuration
480        FIXME: This is suboptimal when the default has changed to
481        another location! */
482     if ($this->gotoTerminalPath == "default"){
483       $ldap= $this->config->get_ldap_link();
485       /* Strip relevant part from dn, keep trailing ',' */
486       $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
487       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
489       /* Walk from top to base and try to load default values for
490          'gotoTerminalPath'. Abort when an entry is found. */
491       while (TRUE){
492         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
494         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
495             $this->config->current['BASE']);
496         $attrs= $ldap->fetch();
497         if (isset($attrs['gotoTerminalPath'])){
498           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
499           break;
500         }
502         /* Nothing left? */
503         if ($tmp == ""){
504           break;
505         }
506       }
507     }
509     /* Add semi automatic values */
510     // FIXME: LDAP Server may not be set here...
511     $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
512       $this->gotoTerminalPath.
513       ",ro,hard,nolock,fg,rsize=8192 ".
514       "ip=::::::dhcp LDAP=".$this->gotoLdapServer;
516     switch ($this->bootmode){
517       case "D":
518         $this->gotoKernelParameters.= " debug";
519       break;
520       case "G":
521         $this->gotoKernelParameters.= " splash=silent";
522       break;
523     }
524     if ($this->customParameters != ""){
525       $this->gotoKernelParameters.= " o ".$this->customParameters;
526     }
528     plugin::save();
529     
530     $str = "";
531     foreach($this->FAIclass as $class){
532       $str .= $class." ";
533     }
534     $this->attrs['FAIclass']= "";
535     $this->attrs['FAIclass']= trim($str);
537     if(empty($this->attrs['FAIclass'])){
538       $this->attrs['FAIclass'] = array();
539     }
541     /* Add missing arrays */
542     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
543       if (isset ($this->$val) && count ($this->$val) != 0){
544     
545         $this->attrs["$val"]= array_unique($this->$val);
546       }
547       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
548     }
549     /* Strip out 'default' values */
550     if ($this->attrs['gotoLdapServer'] == "default"){
551       unset ($this->attrs['gotoLdapServer']);
552     }
554     /* if mirror == none stop saving this attribute */
555     if($this->FAIdebianMirror == "none"){
556       $this->FAIdebianMirror = "";
557     }
558     
559     if((count($this->attrs['FAIclass'])==0)&&(empty($this->FAIdebianMirror))){
560       $tmp = array();
561       foreach($this->attrs['objectClass'] as $class){
562         if($class != "FAIobject"){
563           $tmp[] = $class;
564         }
565       }
566       $this->attrs['objectClass']     = $tmp;
567       $this->attrs['FAIclass']        = array();
568       $this->attrs['FAIdebianMirror'] = array();
569     }
571     /* prepare share settings */
572     $tmp = array();
573     foreach($this->gotoShares as $name => $settings){
574       $tmp2= split("\|",$name);
575       $name = $tmp2[0];
576       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
577     }
578     $this->attrs['gotoShare']=$tmp;
580     $ldap= $this->config->get_ldap_link();
581     $ldap->cd($this->dn);
582     $ldap->modify($this->attrs);
583     show_ldap_error($ldap->get_error());
584     $this->handle_post_events("modify");
585   }
587   /* Add value to array, check if unique */
588   function add_list (&$array, $value)
589   {
590     if ($value != ""){
591       $array[]= $value;
592       sort($array);
593       array_unique ($array);
594     }
595   }
598   /* Delete value to array, check if unique */
599   function del_list (&$array, $list)
600   {
601     $tmp= array();
602     foreach ($array as $mod){
603       if (!in_array($mod, $list)){
604         $tmp[]= $mod;
605       }
606     }
607     $array= $tmp;
608   }
610   /* Generate ListBox frindly output for the defined shares
611    * Possibly Add or remove an attribute here,
612    */
613   function printOutAssignedShares()
614   {
615     $a_return = array();
616     if(is_array($this->gotoShares)){
617       foreach($this->gotoShares as $share){
618         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
619       }
620     }
621     return($a_return);
622   }
626 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
627 ?>