Code

Added speed optimizations
[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'] != "" && chkacl ($this->acl, "gotoModule") == ""){
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']) && chkacl ($this->acl, "gotoModule") == ""){
175         $this->del_list ($this->gotoModules, $_POST['modules_list']);
176       }
177     }
179     /* Show main page */
180     $smarty= get_smarty();
183        /* In this section server shares will be defined
184      * A user can select one of the given shares and a mount point
185      *  and attach this combination to his setup.
186      */
187     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
188     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
189     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
191     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
192      * This entry will be, a combination of mountPoint and sharedefinitions
193      */
194     if(isset($_POST['gotoShareAdd'])){
195       /* We assign a share to this user, if we don't know where to mount the share */
196       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
197         print_red(_("You must specify a valid mount point."));
198       }else{
199         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
200         $s_mount = $_POST['gotoShareMountPoint'];
201         /* Preparing the new assignment */
202         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
203         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
204       }
205     }
207     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
208      * If there is no defined share selected, we will abort the deletion without any message
209      */
210     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
211       unset($this->gotoShares[$_POST['gotoShare']]);
212     }
214     $smarty->assign("gotoShares",$this->printOutAssignedShares());
215     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
217     /* Create divSelectBox for ldap server selection
218      */
219     $SelectBoxLdapServer = new divSelectBox("LdapServer");
220     $SelectBoxLdapServer->SetHeight(80);
222     /* Set first entry as selected, if $this->gotoLdapServer is empty
223      *  or given entry is no longer available ...
224      */
225     $found = false;
226     foreach($this->goLdapServerList as $server => $name){
227       if($this->gotoLdapServer==$server){
228         $found = true;
229       }
230     }
231     
232     /* Add Entries
233      */
234     foreach($this->goLdapServerList as $server => $visible){
235       $use ="";
236       if(($this->gotoLdapServer == $server) || ($found == false)) {
237         $found = true;
238         $use = " checked ";
239       };
241       $SelectBoxLdapServer->AddEntry(
242           array(
243             array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
244                   "attach"=>"style='border-left:0px;'"),
245             array("string"=>$visible)
246             ));
247     }
249     $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
251     $smarty->assign("gotoShareACL", chkacl($this->acl, "gotoShareACL"));
252     foreach (array("gotoModules" ) as $val){
253       $smarty->assign("$val", $this->$val);
254     }
256     /* Values */
257     foreach(array("gotoBootKernel", "customParameters") as $val){
258       $smarty->assign($val, $this->$val);
259       $smarty->assign($val."ACL", chkacl($this->acl, $val));
260     }
262     /* Radio button group */
263     if (preg_match("/G/", $this->bootmode)) {
264       $smarty->assign("graphicalbootup", "checked");
265     } else {
266       $smarty->assign("graphicalbootup", "");
267     }
268     if (preg_match("/T/", $this->bootmode)) {
269       $smarty->assign("textbootup", "checked");
270     } else {
271       $smarty->assign("textbootup", "");
272     }
273     if (preg_match("/D/", $this->bootmode)) {
274       $smarty->assign("debugbootup", "checked");
275     } else {
276       $smarty->assign("debugbootup", "");
277     }
279     /* ACL's */
280     foreach (array("gotoKernelParameters", "gotoModules") as $value){
281       $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
282     }
284     /* Show main page */
285     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
286   }
288   function remove_from_parent()
289   {
290       $this->handle_post_events("remove");
291   }
294   /* Save data to object */
295   function save_object()
296   {
297     plugin::save_object();
299     /* Save group radio buttons */
300     if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
301       $this->bootmode= $_POST["bootmode"];
302     }
304     /* Save kernel parameters */
305     if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
306       $this->customParameters= $_POST["customParameters"];
307     }
308   }
311   /* Save to LDAP */
312   function save()
313   {
314     /* Find proper terminal path for tftp configuration
315        FIXME: This is suboptimal when the default has changed to
316        another location! */
317     if ($this->gotoTerminalPath == "default-inherit"){
318       $ldap= $this->config->get_ldap_link();
320       /* Strip relevant part from dn, keep trailing ',' */
321       $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
322       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
324       /* Walk from top to base and try to load default values for
325          'gotoTerminalPath'. Abort when an entry is found. */
326       while (TRUE){
327         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
329         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
330             $this->config->current['BASE'], array('gotoTerminalPath'));
331         $attrs= $ldap->fetch();
332         if (isset($attrs['gotoTerminalPath'])){
333           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
334           break;
335         }
337         /* Nothing left? */
338         if ($tmp == ""){
339           break;
340         }
341       }
342     }
344     /* Add semi automatic values */
345     // FIXME: LDAP Server may not be set here...
346     $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
347       $this->gotoTerminalPath.
348       ",ro,hard,nolock,fg,rsize=8192 ".
349       "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
351     switch ($this->bootmode){
352       case "D":
353         $this->gotoKernelParameters.= " debug";
354       break;
355       case "G":
356         $this->gotoKernelParameters.= " splash=silent";
357       break;
358     }
359     if ($this->customParameters != ""){
360       $this->gotoKernelParameters.= " o ".$this->customParameters;
361     }
363     plugin::save();
365     /* Add missing arrays */
366     foreach (array("gotoModules") as $val){
367       if (isset ($this->$val) && count ($this->$val) != 0){
368     
369         $this->attrs["$val"]= array_unique($this->$val);
370       }
371       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
372     }
374     /* Strip out 'default' values */
375     foreach(array("gotoBootKernel","gotoLdapServer") as $value){
376       if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
377         $this->attrs[$value] = array();
378       } 
379     }
381      /* prepare share settings */
382     $tmp = array();
383     foreach($this->gotoShares as $name => $settings){
384       $tmp2 = split("\|",$name);
385       $name = $tmp2[0];
386       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
387     }
388     $this->attrs['gotoShare']=$tmp;
390     /* Write back to ldap */
391     $ldap= $this->config->get_ldap_link();
392     $ldap->cd($this->dn);
393     $this->cleanup();
394     $ldap->modify ($this->attrs); 
396     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
397     $this->handle_post_events("modify");
398   }
400   /* Add value to array, check if unique */
401   function add_list (&$array, $value)
402   {
403     if ($value != ""){
404       $array[]= $value;
405       sort($array);
406       array_unique ($array);
407     }
408   }
411   /* Delete value to array, check if unique */
412   function del_list (&$array, $list)
413   {
414     $tmp= array();
415     foreach ($array as $mod){
416       if (!in_array($mod, $list)){
417         $tmp[]= $mod;
418       }
419     }
420     $array= $tmp;
421   }
423    /* Generate ListBox frindly output for the defined shares
424    * Possibly Add or remove an attribute here,
425    */
426   function printOutAssignedShares()
427   {
428     $a_return = array();
429     if(is_array($this->gotoShares)){
430       foreach($this->gotoShares as $share){
431         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
432       }
433     }
434     return($a_return);
435   }
437   /* Return plugin informations for acl handling */
438   function plInfo()
439   {
440     return (array(
441           "plShortName"   => _("Startup"),
442           "plDescription" => _("Terminal startup"),
443           "plSelfModify"  => FALSE,
444           "plDepends"     => array(),
445           "plPriority"    => 0,
446           "plSection"     => array("administration"),
447           "plCategory"    => array("terminal"),
449           "plProvidedAcls"=> array(
450             "gotoLdapServer"      => _("Ldap server"), 
451             "gotoBootKernel"      => _("Boot kernel"), 
452             "gotoKernelParameters"=> _("Kernel parameter"))
453           ));
454   }
459 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
460 ?>