Code

9517edfd49dc25ce8d50c7b4989909240c9e790c
[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)(objectClass=FAIprofile)
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']   = 'Pl';
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       if(in_array('FAIprofile',$attr['objectClass'])){
150         $tmp2[$cn]['FAIprofile']['obj']= 'FAIprofile'; 
151         $tmp2[$cn]['FAIprofile']['kzl']= 'P'; 
152         $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
153       }
154     }
156     $this->FAIclassInfo = $tmp2;
158     if(isset($this->attrs['FAIclass'])){
159       $tmp = array();
160       $tmp2 = $this->attrs['FAIclass'];
161       unset($tmp2['count']);
162       foreach($tmp2 as $class){
163         $tmp[$class] = $class;
164       }
165       $this->FAIclass = $tmp;
166     }
168     if(!is_array($this->FAIclass)){
169       $this->FAIclass = array();
170     }
173     $this->orig_dn= $this->dn;
174   }
176   /* Create array to display available classes/profiles in a selectbox */
177   function selectFriendlyClasses(){
178     $tmp=array();
179     foreach($this->FAIclasses as $class){
180       $str    = "";
181       $skip = false;
182       if(isset($this->FAIclassInfo[$class])){
183         foreach($this->FAIclassInfo[$class] as $objs){
184           $str .= $objs['kzl']." "; 
185         }
186       }
187       $tmp[$class] = $class."&nbsp; [".$str."]";
188     }
189     return($tmp);
190   }
192   function check()
193   {
194     $messages = array();
195     /* If there are packages selected, but no mirror show error */   
196     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
197       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
198     }
199     return($messages);
200   }
202   function execute()
203   {
204         /* Call parent execute */
205         plugin::execute();
206     /* Do we need to flip is_account state? */
207     if (isset($_POST['modify_state'])){
208       $this->is_account= !$this->is_account;
209     }
211     /* Do we represent a valid terminal? */
212     if (!$this->is_account && $this->parent == NULL){
213       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
214         _("This 'dn' has no terminal features.")."</b>";
215       return ($display);
216     }
218     /* Add module */
219     if (isset ($_POST['add_module'])){
220       if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
221         $this->add_list ($this->gotoModules, $_POST['module']);
222       }
223     }
225     /* Delete module */
226     if (isset ($_POST['delete_module'])){
227       if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
228         $this->del_list ($this->gotoModules, $_POST['modules_list']);
229       }
230     }
232     /* FAI class management */
233     if((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))){
234       $found = 0 ; 
236       /* If this new class/profile will attach a second partition table
237        * to our list of classes, abort and show a message.
238        */
239       foreach($this->FAIclass as $name){
240         if(isset($this->FAIclassInfo[$name])){
241           foreach($this->FAIclassInfo[$name] as $atr){
242             if($atr['obj'] == "FAIpartitionTable"){
243               $found ++ ; 
244             }
245           }
246         }
247       }
249       if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpackageList']))&&($found>0)){
250         print_red(_("There is already a profile in your selection that contain partition table configuratons."));
251       }else{
252         $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
253       }
254     }
256     /* Delete selected class from our list */
257     if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
258       if(isset($this->FAIclass[$_POST['FAIclassSel']])){
259         unset($this->FAIclass[$_POST['FAIclassSel']]);
260       }
261     }
263     /* Show main page */
264     $smarty= get_smarty();
266     /* In this section server shares will be defined
267      * A user can select one of the given shares and a mount point
268      *  and attach this combination to his setup.
269      */
270     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
271     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
273     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
274      * This entry will be, a combination of mountPoint and sharedefinitions
275      */
276     if(isset($_POST['gotoShareAdd'])){
277       /* We assign a share to this user, if we don't know where to mount the share */
278       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
279         print_red(_("You must specify a valid mount point."));
280       }else{
281         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
282         $s_mount = $_POST['gotoShareMountPoint'];
283         /* Preparing the new assignment */
284         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
285         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
286       }
287     }
289     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
290      * If there is no defined share selected, we will abort the deletion without any message
291      */
292     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
293       unset($this->gotoShares[$_POST['gotoShare']]);
294     }
296     $smarty->assign("gotoShares",$this->printOutAssignedShares());
297     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
299     /* Arrays */
300     $smarty->assign("ldapservers", $this->config->data['SERVERS']['LDAP']);
301     $smarty->assign("gotoLdapServer_select", $this->gotoLdapServer);
302     $smarty->assign("gotoLdapServerACL", chkacl($this->acl, "gotoLdapServer"));
303     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
304       $smarty->assign("$val", $this->$val);
305     }
307     /* Values */
308     foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror") as $val){
309       $smarty->assign($val, $this->$val);
310       $smarty->assign($val."ACL", chkacl($this->acl, $val));
311     }
312   
313     $smarty->assign("FAIdebianMirrors",$this->FAIdebianMirrors);
314     $smarty->assign("FAIclasses",$this->selectFriendlyClasses());
315     $smarty->assign("FAIclassesKeys",array_flip($this->selectFriendlyClasses()));
316     $smarty->assign("FAIclassKeys",$this->FAIclass);
318     /* Radio button group */
319     if (preg_match("/G/", $this->bootmode)) {
320       $smarty->assign("graphicalbootup", "checked");
321     } else {
322       $smarty->assign("graphicalbootup", "");
323     }
324     if (preg_match("/T/", $this->bootmode)) {
325       $smarty->assign("textbootup", "checked");
326     } else {
327       $smarty->assign("textbootup", "");
328     }
329     if (preg_match("/D/", $this->bootmode)) {
330       $smarty->assign("debugbootup", "checked");
331     } else {
332       $smarty->assign("debugbootup", "");
333     }
335     /* ACL's */
336     foreach (array("gotoKernelParameters", "gotoModules", "gotoFilesystem","FAIclass") as $value){
337       $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
338     }
340     /* Show main page */
341     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
342   }
344   function remove_from_parent()
345   {
346     /*
347     $ldap= $this->config->get_ldap_link();
348     $ldap->rmdir($this->dn);
349     show_ldap_error($ldap->get_error());
350     $this->handle_post_events("remove");
351     */
354     /* Depending on the baseobject (Ogroup / WS) we
355      *  use another set of objectClasses
356      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
357      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
358      */
359     if(isset($this->parent->by_object['ogroup'])){
360       $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
361     }elseif(isset($this->parent->by_object['workgeneric'])){
362       $this->objectclasses = array("GOhard", "FAIobject");
363     }else{
364       print "unknown";
365       exit();
366     }
368     $ldap= $this->config->get_ldap_link();
369     plugin::remove_from_parent();
370    
371     /* Remove additional attributes.*/ 
372     foreach(array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $attr){
373       $this->attrs [$attr] = array();
374     }
376     $ldap->cd($this->dn);
377     $ldap->modify($this->attrs);
378     show_ldap_error($ldap->get_error());
380   }
383   /* Save data to object */
384   function save_object()
385   {
386     plugin::save_object();
388     /* Save group radio buttons */
389     if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
390       $this->bootmode= $_POST["bootmode"];
391     }
393     /* Save kernel parameters */
394     if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
395       $this->customParameters= $_POST["customParameters"];
396     }
397   }
400   /* Save to LDAP */
401   function save()
402   {
404     /* Depending on the baseobject (Ogroup / WS) we
405      *  use another set of objectClasses
406      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
407      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
408      */
409     if(isset($this->parent->by_object['ogroup'])){
410       $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
411     }elseif(isset($this->parent->by_object['workgeneric'])){
412       $this->objectclasses = array("GOhard", "FAIobject");
413     }else{
414       print "unknown";
415       exit();
416     }
418     /* Find proper terminal path for tftp configuration
419        FIXME: This is suboptimal when the default has changed to
420        another location! */
421     if ($this->gotoTerminalPath == "default"){
422       $ldap= $this->config->get_ldap_link();
424       /* Strip relevant part from dn, keep trailing ',' */
425       $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
426       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
428       /* Walk from top to base and try to load default values for
429          'gotoTerminalPath'. Abort when an entry is found. */
430       while (TRUE){
431         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
433         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
434             $this->config->current['BASE']);
435         $attrs= $ldap->fetch();
436         if (isset($attrs['gotoTerminalPath'])){
437           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
438           break;
439         }
441         /* Nothing left? */
442         if ($tmp == ""){
443           break;
444         }
445       }
446     }
448     /* Add semi automatic values */
449     // FIXME: LDAP Server may not be set here...
450     $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
451       $this->gotoTerminalPath.
452       ",ro,hard,nolock,fg,rsize=8192 ".
453       "ip=::::::dhcp LDAP=".$this->gotoLdapServer;
455     switch ($this->bootmode){
456       case "D":
457         $this->gotoKernelParameters.= " debug";
458       break;
459       case "G":
460         $this->gotoKernelParameters.= " splash=silent";
461       break;
462     }
463     if ($this->customParameters != ""){
464       $this->gotoKernelParameters.= " o ".$this->customParameters;
465     }
467     /* Create numeric index for our classes */
468     $tmp = array();
469     foreach($this->FAIclass as $class){
470       $tmp[] = $class;
471     }
472     $this->FAIclass = $tmp;
474     plugin::save();
476     /* Add missing arrays */
477     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
478       if (isset ($this->$val) && count ($this->$val) != 0){
479     
480         $this->attrs["$val"]= array_unique($this->$val);
481       }
482       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
483     }
484     /* Strip out 'default' values */
485     if ($this->attrs['gotoLdapServer'] == "default"){
486       unset ($this->attrs['gotoLdapServer']);
487     }
489     $tmp = array();
490     foreach($this->FAIclass as $class){
491       $tmp[] = $class;
492     }
493     $this->attrs['FAIclass'] = $tmp;
494     /* Write back to ldap */
496     /* if mirror == none stop saving this attribute */
497     if($this->FAIdebianMirror == "none"){
498       $this->FAIdebianMirror = "";
499     }
500    
501     if((count($this->attrs['FAIclass'])==0)&&(empty($this->FAIdebianMirror))){
502       $tmp = array();
503       foreach($this->attrs['objectClass'] as $class){
504         if($class != "FAIobject"){
505           $tmp[] = $class;
506         }
507       }
508       $this->attrs['objectClass']     = $tmp;
509       $this->attrs['FAIclass']        = array();
510       $this->attrs['FAIdebianMirror'] = array();
511     }
513     /* prepare share settings */
514     $tmp = array();
515     foreach($this->gotoShares as $name => $settings){
516       $tmp2= split("\|",$name);
517       $name = $tmp2[0];
518       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
519     }
520     $this->attrs['gotoShare']=$tmp;
522     $ldap= $this->config->get_ldap_link();
523     $ldap->cd($this->dn);
524     $ldap->modify($this->attrs);
525     show_ldap_error($ldap->get_error());
526     $this->handle_post_events("modify");
527   }
529   /* Add value to array, check if unique */
530   function add_list (&$array, $value)
531   {
532     if ($value != ""){
533       $array[]= $value;
534       sort($array);
535       array_unique ($array);
536     }
537   }
540   /* Delete value to array, check if unique */
541   function del_list (&$array, $list)
542   {
543     $tmp= array();
544     foreach ($array as $mod){
545       if (!in_array($mod, $list)){
546         $tmp[]= $mod;
547       }
548     }
549     $array= $tmp;
550   }
552   /* Generate ListBox frindly output for the defined shares
553    * Possibly Add or remove an attribute here,
554    */
555   function printOutAssignedShares()
556   {
557     $a_return = array();
558     if(is_array($this->gotoShares)){
559       foreach($this->gotoShares as $share){
560         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
561       }
562     }
563     return($a_return);
564   }
568 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
569 ?>