Code

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