Code

Added some fixes for iframe scrolling
[gosa.git] / plugins / admin / systems / class_terminalStartup.inc
1 <?php
2 class termstartup 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();
12   var $gotoBootKernel= "default-inherit";
13   var $gotoKernelParameters= "";
14   var $gotoLdapServer= "";
15   var $gotoModules= array();
16   var $gotoTerminalPath= "";
17   var $gotoBootKernels= array();
19   /* Share */
20   var $gotoShares         = array();// Currently Share Option
21   var $gotoShare          = "";     // currently selected Share Option
22   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
23   var $gotoAvailableShares= array();// Available Shares for this account
26   /* attribute list for save action */
27   var $attributes= array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters");
28   var $objectclasses= array("GOhard");
30   /* Helper */
31   var $customParameters= "";
32   var $orig_dn= "";
33   var $ignore_account= TRUE;
35   function termstartup ($config, $dn= NULL, $parent= NULL)
36   {
37     plugin::plugin ($config, $dn, $parent);
39     $this->gotoBootKernels = array("default-inherit"=>"["._("inherited")."]");
41     /* Get arrays */
42     foreach (array("gotoModules") as $val){
43       if (isset($this->attrs["$val"]["count"])){
44         for ($i= 0; $i<$this->attrs["count"]; $i++){
45           if (isset($this->attrs["$val"][$i])){
46             array_push($this->$val, $this->attrs["$val"][$i]);
47           }
48         }
49       }
50       sort ($this->$val);
51       $this->$val= array_unique($this->$val);
52     }
54     /* Parse Kernel Parameters to decide what boot mode is enabled */
55     if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
56       $this->bootmode= "G";
57     } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
58       $this->bootmode= "D";
59     } elseif ($this->gotoKernelParameters == "") {
60       $this->bootmode= "G";
61     } else {
62       $this->bootmode= "T";
63     }
64     if (preg_match("/ o /", $this->gotoKernelParameters)){
65       $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
66     } else {
67       $this->customParameters= "";
68     }
70     /* Prepare Shares */
71     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
72       unset($this->attrs['gotoShare']['count']);
73       foreach($this->attrs['gotoShare'] as $share){
74         $tmp = $tmp2 = array();
75         $tmp = split("\|",$share);
76         $tmp2['server']      =$tmp[0];
77         $tmp2['name']        =$tmp[1];
78         $tmp2['mountPoint']  =$tmp[2];
79         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
80       }
81     }
83     $this->gotoShareSelections= $config->getShareList(true);
84     $this->gotoAvailableShares= $config->getShareList(false);
86     $this->orig_dn= $this->dn;
88     /* Get list of boot kernels */
89     if (isset($this->config->data['TABS'])){
90       $command= search_config($this->config->data['TABS'], get_class($this), "KERNELS");
92       if (!check_command($command)){
93         $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
94             get_class($this));
95       } else {
96         $fh= popen($command, "r");
97         while (!feof($fh)) {
98           $buffer= trim(fgets($fh, 256));
99           
100           if(!empty($buffer)){
101           
102             $name=$value = $buffer;
104             if(preg_match("/:/",$buffer)){
105               $name = preg_replace("/:.*$/","",$buffer);
106               $value= preg_replace("/^.*:/","",$buffer);
107               $this->gotoBootKernels[$name]= $name.":".$value;
108             }else{
109               $this->gotoBootKernels[$name]= $value;
110             }
111           }
112         }
113         pclose($fh);
114       }
116     }
117     $tmp = $this->config->data['SERVERS']['LDAP'];  
118     foreach($tmp as $server){
119       $visible = $server;
120       if($server == "default-inherit"){
121         $visible = "["._("inherited")."]";
122       }
123       $this->goLdapServerList[$server] = $visible;
124     }
126      /* Load hardware list */
127     $ldap= $this->config->get_ldap_link();
128     $ldap->cd($this->config->current['BASE']);
129     $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))");
130     if ($ldap->count() == 1){
131       $map= array("gotoLdapServer");
132       $attrs= $ldap->fetch();
134       foreach ($map as $name){
135         if (!isset($attrs[$name][0])){
136           continue;
137         }
139         switch ($name){
140           case 'gotoLdapServer':
141             $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
142             break;
143         }
144       }
145     }
146   }
148   function execute()
149   {
150     /* Call parent execute */
151     plugin::execute();
153     /* Do we need to flip is_account state? */
154     if (isset($_POST['modify_state'])){
155       $this->is_account= !$this->is_account;
156     }
158     /* Do we represent a valid terminal? */
159     if (!$this->is_account && $this->parent == NULL){
160       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
161         _("This 'dn' has no terminal features.")."</b>";
162       return ($display);
163     }
165     /* Add module */
166     if (isset ($_POST['add_module'])){
167       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
168         $this->add_list ($this->gotoModules, $_POST['module']);
169       }
170     }
172     /* Delete module */
173     if (isset ($_POST['delete_module'])){
174       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
175         $this->del_list ($this->gotoModules, $_POST['modules_list']);
176       }
177     }
179     /* Show main page */
180     $smarty= get_smarty();
182     /* Assign acls */
183     $tmp = $this->plInfo();
184     foreach($tmp['plProvidedAcls'] as $name => $translation){
185       $smarty->assign($name."ACL",$this->getacl($name));
186     }
189        /* In this section server shares will be defined
190      * A user can select one of the given shares and a mount point
191      *  and attach this combination to his setup.
192      */
193     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
194     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
195     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
197     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
198      * This entry will be, a combination of mountPoint and sharedefinitions
199      */
200     if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
201       /* We assign a share to this user, if we don't know where to mount the share */
202       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
203         print_red(_("You must specify a valid mount point."));
204       }else{
205         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
206         $s_mount = $_POST['gotoShareMountPoint'];
207         /* Preparing the new assignment */
208         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
209         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
210       }
211     }
213     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
214      * If there is no defined share selected, we will abort the deletion without any message
215      */
216     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
217       unset($this->gotoShares[$_POST['gotoShare']]);
218     }
220     $smarty->assign("gotoShares",$this->printOutAssignedShares());
221     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
223     /* Create divSelectBox for ldap server selection
224      */
225     $SelectBoxLdapServer = new divSelectBox("LdapServer");
226     $SelectBoxLdapServer->SetHeight(80);
228     /* Set first entry as selected, if $this->gotoLdapServer is empty
229      *  or given entry is no longer available ...
230      */
231     $found = false;
232     foreach($this->goLdapServerList as $server => $name){
233       if($this->gotoLdapServer==$server){
234         $found = true;
235       }
236     }
237     
238     /* Add Entries
239      */
240     foreach($this->goLdapServerList as $server => $visible){
241       $use ="";
242       if(($this->gotoLdapServer == $server) || ($found == false)) {
243         $found = true;
244         $use = " checked ";
245       };
247       $SelectBoxLdapServer->AddEntry(
248           array(
249             array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
250                   "attach"=>"style='border-left:0px;'"),
251             array("string"=>$visible)
252             ));
253     }
255     $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
257     foreach (array("gotoModules" ) as $val){
258       $smarty->assign("$val", $this->$val);
259     }
261     /* Values */
262     foreach(array("gotoBootKernel", "customParameters") as $val){
263       $smarty->assign($val, $this->$val);
264     }
266     /* Radio button group */
267     if (preg_match("/G/", $this->bootmode)) {
268       $smarty->assign("graphicalbootup", "checked");
269     } else {
270       $smarty->assign("graphicalbootup", "");
271     }
272     if (preg_match("/T/", $this->bootmode)) {
273       $smarty->assign("textbootup", "checked");
274     } else {
275       $smarty->assign("textbootup", "");
276     }
277     if (preg_match("/D/", $this->bootmode)) {
278       $smarty->assign("debugbootup", "checked");
279     } else {
280       $smarty->assign("debugbootup", "");
281     }
283     /* Show main page */
284     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
285   }
287   function remove_from_parent()
288   {
289     if($this->acl_is_removeable()){
290       $this->handle_post_events("remove");
291     }
292   }
295   /* Save data to object */
296   function save_object()
297   {
298     plugin::save_object();
300     /* Save group radio buttons */
301     if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
302       $this->bootmode= $_POST["bootmode"];
303     }
305     /* Save kernel parameters */
306     if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
307       $this->customParameters= $_POST["customParameters"];
308     }
309   }
312   /* Save to LDAP */
313   function save()
314   {
315     /* Find proper terminal path for tftp configuration
316        FIXME: This is suboptimal when the default has changed to
317        another location! */
318     if ($this->gotoTerminalPath == "default-inherit"){
319       $ldap= $this->config->get_ldap_link();
321       /* Strip relevant part from dn, keep trailing ',' */
322       $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
323       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
325       /* Walk from top to base and try to load default values for
326          'gotoTerminalPath'. Abort when an entry is found. */
327       while (TRUE){
328         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
330         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
331             $this->config->current['BASE'], array('gotoTerminalPath'));
332         $attrs= $ldap->fetch();
333         if (isset($attrs['gotoTerminalPath'])){
334           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
335           break;
336         }
338         /* Nothing left? */
339         if ($tmp == ""){
340           break;
341         }
342       }
343     }
345     /* Add semi automatic values */
346     // FIXME: LDAP Server may not be set here...
347     $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
348       $this->gotoTerminalPath.
349       ",ro,hard,nolock,fg,rsize=8192 ".
350       "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
352     switch ($this->bootmode){
353       case "D":
354         $this->gotoKernelParameters.= " debug";
355       break;
356       case "G":
357         $this->gotoKernelParameters.= " splash=silent";
358       break;
359     }
360     if ($this->customParameters != ""){
361       $this->gotoKernelParameters.= " o ".$this->customParameters;
362     }
364     plugin::save();
366     /* Add missing arrays */
367     foreach (array("gotoModules") as $val){
368       if (isset ($this->$val) && count ($this->$val) != 0){
369     
370         $this->attrs["$val"]= array_unique($this->$val);
371       }
372       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
373     }
375     /* Strip out 'default' values */
376     foreach(array("gotoBootKernel","gotoLdapServer") as $value){
377       if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
378         $this->attrs[$value] = array();
379       } 
380     }
382      /* prepare share settings */
383     $tmp = array();
384     foreach($this->gotoShares as $name => $settings){
385       $tmp2 = split("\|",$name);
386       $name = $tmp2[0];
387       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
388     }
389     $this->attrs['gotoShare']=$tmp;
391     /* Write back to ldap */
392     $ldap= $this->config->get_ldap_link();
393     $ldap->cd($this->dn);
394     $this->cleanup();
395     $ldap->modify ($this->attrs); 
397     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
398     $this->handle_post_events("modify");
399   }
401   /* Add value to array, check if unique */
402   function add_list (&$array, $value)
403   {
404     if ($value != ""){
405       $array[]= $value;
406       sort($array);
407       array_unique ($array);
408     }
409   }
412   /* Delete value to array, check if unique */
413   function del_list (&$array, $list)
414   {
415     $tmp= array();
416     foreach ($array as $mod){
417       if (!in_array($mod, $list)){
418         $tmp[]= $mod;
419       }
420     }
421     $array= $tmp;
422   }
424    /* Generate ListBox frindly output for the defined shares
425    * Possibly Add or remove an attribute here,
426    */
427   function printOutAssignedShares()
428   {
429     $a_return = array();
430     if(is_array($this->gotoShares)){
431       foreach($this->gotoShares as $share){
432         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
433       }
434     }
435     return($a_return);
436   }
438   /* Return plugin informations for acl handling */
439   function plInfo()
440   {
441     return (array(
442           "plShortName"   => _("Startup"),
443           "plDescription" => _("Terminal startup"),
444           "plSelfModify"  => FALSE,
445           "plDepends"     => array(),
446           "plPriority"    => 5,
447           "plSection"     => array("administration"),
448           "plCategory"    => array("terminal"),
450           "plProvidedAcls"=> array(
451             "gotoLdapServer"      => _("Ldap server"),
452             "gotoShare"           => _("Shares"),
453             "gotoModules"         => _("Kernel modules"),
454             "gotoBootKernel"      => _("Boot kernel"), 
455             "gotoKernelParameters"=> _("Kernel parameter"))
456           ));
457   }
462 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
463 ?>