Code

c60e83b7c92efa7e8c126ade467fe118c7da9455
[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("none"=>_("none"));
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     while($attr = $ldap->fetch()){
56       if(isset($attr['FAIdebianMirror'])){
57         $this->FAIdebianMirrors[$attr['FAIdebianMirror'][0]]=$attr['FAIdebianMirror'][0];
58       }
59     } 
60  
61     /* Get arrays */
62     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
63       if (isset($this->attrs["$val"]["count"])){
64         for ($i= 0; $i<$this->attrs["count"]; $i++){
65           if (isset($this->attrs["$val"][$i])){
66             array_push($this->$val, $this->attrs["$val"][$i]);
67           }
68         }
69       }
70       sort ($this->$val);
71       $this->$val= array_unique($this->$val);
72     }
74     /* Parse Kernel Parameters to decide what boot mode is enabled */
75     if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
76       $this->bootmode= "G";
77     } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
78       $this->bootmode= "D";
79     } elseif ($this->gotoKernelParameters == "") {
80       $this->bootmode= "G";
81     } else {
82       $this->bootmode= "T";
83     }
84     if (preg_match("/ o /", $this->gotoKernelParameters)){
85       $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
86     } else {
87       $this->customParameters= "";
88     }
90     /* Prepare Shares */
91     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
92       unset($this->attrs['gotoShare']['count']);
93       foreach($this->attrs['gotoShare'] as $share){
94         $tmp = $tmp2 = array();
95         $tmp = split("\|",$share);
96         $tmp2['server']      =$tmp[0];
97         $tmp2['name']        =$tmp[1];
98         $tmp2['mountPoint']  =$tmp[2];
99         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
100       }
101     }
103     $this->gotoShareSelections= $config->getShareList(true);
104     $this->gotoAvailableShares= $config->getShareList(false);
105     $tmp2 = array();
106   
107     $ldap   = $this->config->get_ldap_link();
108     $ldap->cd($this->config->current['BASE']);
110     /* Search all FAI objects */
111     $ldap->search("(| (objectClass=FAIpackageList)(objectClass=FAItemplate)
112                       (objectClass=FAIvariable)(objectClass=FAIscript')(objectClass=FAIhook)
113                       (objectClass=FAIpartitionTable))",array("objectClass","cn"),true);
114     /* Sort all entries, and attach elementtype.
115      * To be able to show the types in the listbox.
116      */
117     while($attr = $ldap->fetch()){
118       $cn = $attr['cn'][0];
119       if(in_array('FAIpackageList',$attr['objectClass'])){
120         $tmp2[$cn]['FAIpackageList']['obj']   = 'FAIpackageList'; 
121         $tmp2[$cn]['FAIpackageList']['kzl']   = 'P';
122         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
123       }
124       if(in_array('FAItemplate',$attr['objectClass'])){
125         $tmp2[$cn]['FAItemplate']['obj']      = 'FAItemplate'; 
126         $tmp2[$cn]['FAItemplate']['kzl']      = 'T'; 
127         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
128       }
129       if(in_array('FAIvariable',$attr['objectClass'])){
130         $tmp2[$cn]['FAIvariable']['obj']      = 'FAIvariable'; 
131         $tmp2[$cn]['FAIvariable']['kzl']      = 'V'; 
132         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
133       }
134       if(in_array('FAIscript',$attr['objectClass'])){
135         $tmp2[$cn]['FAIscript']['obj']        = 'FAIscript'; 
136         $tmp2[$cn]['FAIscript']['kzl']        = 'S'; 
137         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
138       }
139       if(in_array('FAIhook',$attr['objectClass'])){
140         $tmp2[$cn]['FAIhook']['obj']          = 'FAIhook'; 
141         $tmp2[$cn]['FAIhook']['kzl']          = 'H'; 
142         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
143       }
144       if(in_array('FAIpartitionTable',$attr['objectClass'])){
145         $tmp2[$cn]['FAIpartitionTable']['obj']= 'FAIpartitionTable'; 
146         $tmp2[$cn]['FAIpartitionTable']['kzl']= 'Pt'; 
147         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
148       }
149     }
151     $this->FAIclassInfo = $tmp2;
153     if(isset($this->attrs['FAIclass'])){
154       $tmp = array();
155       $tmp2 = $this->attrs['FAIclass'];
156       unset($tmp2['count']);
157       foreach($tmp2 as $class){
158         $tmp[$class] = $class;
159       }
160       $this->FAIclass = $tmp;
161     }
163     if(!is_array($this->FAIclass)){
164       $this->FAIclass = array();
165     }
168     $this->orig_dn= $this->dn;
169   }
171   /* Create array to display available classes/profiles in a selectbox */
172   function selectFriendlyClasses(){
173     $tmp=array();
174     foreach($this->FAIclasses as $class){
175       $str    = "";
176       $skip = false;
177       if(isset($this->FAIclassInfo[$class])){
178         foreach($this->FAIclassInfo[$class] as $objs){
179           $str .= $objs['kzl']." "; 
180         }
181       }
182       $tmp[$class] = $class."&nbsp; [".$str."]";
183     }
184     return($tmp);
185   }
187   function check()
188   {
189     $messages = array();
190     /* If there are packages selected, but no mirror show error */   
191     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
192       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
193     }
194     return($messages);
195   }
197   function execute()
198   {
199     /* Do we need to flip is_account state? */
200     if (isset($_POST['modify_state'])){
201       $this->is_account= !$this->is_account;
202     }
204     /* Do we represent a valid terminal? */
205     if (!$this->is_account && $this->parent == NULL){
206       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
207         _("This 'dn' has no terminal features.")."</b>";
208       return ($display);
209     }
211     /* Add module */
212     if (isset ($_POST['add_module'])){
213       if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
214         $this->add_list ($this->gotoModules, $_POST['module']);
215       }
216     }
218     /* Delete module */
219     if (isset ($_POST['delete_module'])){
220       if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
221         $this->del_list ($this->gotoModules, $_POST['modules_list']);
222       }
223     }
225     /* FAI class management */
226     if((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))){
227       $found = 0 ; 
229       /* If this new class/profile will attach a second partition table
230        * to our list of classes, abort and show a message.
231        */
232       foreach($this->FAIclass as $name){
233         if(isset($this->FAIclassInfo[$name])){
234           foreach($this->FAIclassInfo[$name] as $atr){
235             if($atr['obj'] == "FAIpartitionTable"){
236               $found ++ ; 
237             }
238           }
239         }
240       }
242       if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpackageList']))&&($found>0)){
243         print_red(_("There is already a profile in your selection that contain partition table configuratons."));
244       }else{
245         $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
246       }
247     }
249     /* Delete selected class from our list */
250     if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
251       if(isset($this->FAIclass[$_POST['FAIclassSel']])){
252         unset($this->FAIclass[$_POST['FAIclassSel']]);
253       }
254     }
256     /* Show main page */
257     $smarty= get_smarty();
259     /* In this section server shares will be defined
260      * A user can select one of the given shares and a mount point
261      *  and attach this combination to his setup.
262      */
263     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
264     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
266     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
267      * This entry will be, a combination of mountPoint and sharedefinitions
268      */
269     if(isset($_POST['gotoShareAdd'])){
270       /* We assign a share to this user, if we don't know where to mount the share */
271       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
272         print_red(_("You must specify a valid mount point."));
273       }else{
274         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
275         $s_mount = $_POST['gotoShareMountPoint'];
276         /* Preparing the new assignment */
277         $this->gotoShares[$a_share['name']]=$a_share;
278         $this->gotoShares[$a_share['name']]['mountPoint']=$s_mount;
279       }
280     }
282     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
283      * If there is no defined share selected, we will abort the deletion without any message
284      */
285     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
286       unset($this->gotoShares[$_POST['gotoShare']]);
287     }
289     $smarty->assign("gotoShares",$this->printOutAssignedShares());
290     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
292     /* Arrays */
293     $smarty->assign("ldapservers", $this->config->data['SERVERS']['LDAP']);
294     $smarty->assign("gotoLdapServer_select", $this->gotoLdapServer);
295     $smarty->assign("gotoLdapServerACL", chkacl($this->acl, "gotoLdapServer"));
296     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
297       $smarty->assign("$val", $this->$val);
298     }
300     /* Values */
301     foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror") as $val){
302       $smarty->assign($val, $this->$val);
303       $smarty->assign($val."ACL", chkacl($this->acl, $val));
304     }
305   
306     $smarty->assign("FAIdebianMirrors",$this->FAIdebianMirrors);
307     $smarty->assign("FAIclasses",$this->selectFriendlyClasses());
308     $smarty->assign("FAIclassesKeys",array_flip($this->selectFriendlyClasses()));
309     $smarty->assign("FAIclassKeys",$this->FAIclass);
311     /* Radio button group */
312     if (preg_match("/G/", $this->bootmode)) {
313       $smarty->assign("graphicalbootup", "checked");
314     } else {
315       $smarty->assign("graphicalbootup", "");
316     }
317     if (preg_match("/T/", $this->bootmode)) {
318       $smarty->assign("textbootup", "checked");
319     } else {
320       $smarty->assign("textbootup", "");
321     }
322     if (preg_match("/D/", $this->bootmode)) {
323       $smarty->assign("debugbootup", "checked");
324     } else {
325       $smarty->assign("debugbootup", "");
326     }
328     /* ACL's */
329     foreach (array("gotoKernelParameters", "gotoModules", "gotoFilesystem","FAIclass") as $value){
330       $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
331     }
333     /* Show main page */
334     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
335   }
337   function remove_from_parent()
338   {
339     /*
340     $ldap= $this->config->get_ldap_link();
341     $ldap->rmdir($this->dn);
342     show_ldap_error($ldap->get_error());
343     $this->handle_post_events("remove");
344     */
347     /* Depending on the baseobject (Ogroup / WS) we
348      *  use another set of objectClasses
349      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
350      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
351      */
352     if(isset($this->parent->by_object['ogroup'])){
353       $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
354     }elseif(isset($this->parent->by_object['workgeneric'])){
355       $this->objectclasses = array("GOhard", "FAIobject");
356     }else{
357       print "unknown";
358       exit();
359     }
361     $ldap= $this->config->get_ldap_link();
362     plugin::remove_from_parent();
363    
364     /* Remove additional attributes.*/ 
365     foreach(array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $attr){
366       $this->attrs [$attr] = array();
367     }
369     $ldap->cd($this->dn);
370     $ldap->modify($this->attrs);
371     show_ldap_error($ldap->get_error());
373   }
376   /* Save data to object */
377   function save_object()
378   {
379     plugin::save_object();
381     /* Save group radio buttons */
382     if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
383       $this->bootmode= $_POST["bootmode"];
384     }
386     /* Save kernel parameters */
387     if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
388       $this->customParameters= $_POST["customParameters"];
389     }
390   }
393   /* Save to LDAP */
394   function save()
395   {
397     /* Depending on the baseobject (Ogroup / WS) we
398      *  use another set of objectClasses
399      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
400      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
401      */
402     if(isset($this->parent->by_object['ogroup'])){
403       $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
404     }elseif(isset($this->parent->by_object['workgeneric'])){
405       $this->objectclasses = array("GOhard", "FAIobject");
406     }else{
407       print "unknown";
408       exit();
409     }
411     /* Find proper terminal path for tftp configuration
412        FIXME: This is suboptimal when the default has changed to
413        another location! */
414     if ($this->gotoTerminalPath == "default"){
415       $ldap= $this->config->get_ldap_link();
417       /* Strip relevant part from dn, keep trailing ',' */
418       $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
419       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
421       /* Walk from top to base and try to load default values for
422          'gotoTerminalPath'. Abort when an entry is found. */
423       while (TRUE){
424         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
426         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
427             $this->config->current['BASE']);
428         $attrs= $ldap->fetch();
429         if (isset($attrs['gotoTerminalPath'])){
430           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
431           break;
432         }
434         /* Nothing left? */
435         if ($tmp == ""){
436           break;
437         }
438       }
439     }
441     /* Add semi automatic values */
442     // FIXME: LDAP Server may not be set here...
443     $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
444       $this->gotoTerminalPath.
445       ",ro,hard,nolock,fg,rsize=8192 ".
446       "ip=::::::dhcp LDAP=".$this->gotoLdapServer;
448     switch ($this->bootmode){
449       case "D":
450         $this->gotoKernelParameters.= " debug";
451       break;
452       case "G":
453         $this->gotoKernelParameters.= " splash=silent";
454       break;
455     }
456     if ($this->customParameters != ""){
457       $this->gotoKernelParameters.= " o ".$this->customParameters;
458     }
460     /* Create numeric index for our classes */
461     $tmp = array();
462     foreach($this->FAIclass as $class){
463       $tmp[] = $class;
464     }
465     $this->FAIclass = $tmp;
467     plugin::save();
469     /* Add missing arrays */
470     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
471       if (isset ($this->$val) && count ($this->$val) != 0){
472     
473         $this->attrs["$val"]= array_unique($this->$val);
474       }
475       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
476     }
477     /* Strip out 'default' values */
478     if ($this->attrs['gotoLdapServer'] == "default"){
479       unset ($this->attrs['gotoLdapServer']);
480     }
482     $tmp = array();
483     foreach($this->FAIclass as $class){
484       $tmp[] = $class;
485     }
486     $this->attrs['FAIclass'] = $tmp;
487     /* Write back to ldap */
489     /* if mirror == none stop saving this attribute */
490     if($this->FAIdebianMirror == "none"){
491       $this->FAIdebianMirror = "";
492     }
493    
494     if((count($this->attrs['FAIclass'])==0)&&(empty($this->FAIdebianMirror))){
495       $tmp = array();
496       foreach($this->attrs['objectClass'] as $class){
497         if($class != "FAIobject"){
498           $tmp[] = $class;
499         }
500       }
501       $this->attrs['objectClass']     = $tmp;
502       $this->attrs['FAIclass']        = array();
503       $this->attrs['FAIdebianMirror'] = array();
504     }
506     /* prepare share settings */
507     $tmp = array();
508     foreach($this->gotoShares as $name => $settings){
509       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
510     }
511     $this->attrs['gotoShare']=$tmp;
513     $ldap= $this->config->get_ldap_link();
514     $ldap->cd($this->dn);
515     $ldap->modify($this->attrs);
516     show_ldap_error($ldap->get_error());
517     $this->handle_post_events("modify");
518   }
520   /* Add value to array, check if unique */
521   function add_list (&$array, $value)
522   {
523     if ($value != ""){
524       $array[]= $value;
525       sort($array);
526       array_unique ($array);
527     }
528   }
531   /* Delete value to array, check if unique */
532   function del_list (&$array, $list)
533   {
534     $tmp= array();
535     foreach ($array as $mod){
536       if (!in_array($mod, $list)){
537         $tmp[]= $mod;
538       }
539     }
540     $array= $tmp;
541   }
543   /* Generate ListBox frindly output for the defined shares
544    * Possibly Add or remove an attribute here,
545    */
546   function printOutAssignedShares()
547   {
548     $a_return = array();
549     if(is_array($this->gotoShares)){
550       foreach($this->gotoShares as $share){
551         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
552       }
553     }
554     return($a_return);
555   }
559 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
560 ?>