Code

Added logging to some classes
[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     @log::log("view","terminal/".get_class($this),$this->dn);
148   }
150   function execute()
151   {
152     /* Call parent execute */
153     plugin::execute();
155     /* Do we need to flip is_account state? */
156     if (isset($_POST['modify_state'])){
157       $this->is_account= !$this->is_account;
158     }
160     /* Do we represent a valid terminal? */
161     if (!$this->is_account && $this->parent == NULL){
162       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
163         _("This 'dn' has no terminal features.")."</b>";
164       return ($display);
165     }
167     /* Add module */
168     if (isset ($_POST['add_module'])){
169       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
170         $this->add_list ($this->gotoModules, $_POST['module']);
171       }
172     }
174     /* Delete module */
175     if (isset ($_POST['delete_module'])){
176       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
177         $this->del_list ($this->gotoModules, $_POST['modules_list']);
178       }
179     }
181     /* Show main page */
182     $smarty= get_smarty();
184     /* Assign acls */
185     $tmp = $this->plInfo();
186     foreach($tmp['plProvidedAcls'] as $name => $translation){
187       $smarty->assign($name."ACL",$this->getacl($name));
188     }
191        /* In this section server shares will be defined
192      * A user can select one of the given shares and a mount point
193      *  and attach this combination to his setup.
194      */
195     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
196     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
197     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
199     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
200      * This entry will be, a combination of mountPoint and sharedefinitions
201      */
202     if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
203       /* We assign a share to this user, if we don't know where to mount the share */
204       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
205         print_red(_("You must specify a valid mount point."));
206       }else{
207         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
208         $s_mount = $_POST['gotoShareMountPoint'];
209         /* Preparing the new assignment */
210         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
211         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
212       }
213     }
215     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
216      * If there is no defined share selected, we will abort the deletion without any message
217      */
218     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
219       unset($this->gotoShares[$_POST['gotoShare']]);
220     }
222     $smarty->assign("gotoShares",$this->printOutAssignedShares());
223     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
225     /* Create divSelectBox for ldap server selection
226      */
227     $SelectBoxLdapServer = new divSelectBox("LdapServer");
228     $SelectBoxLdapServer->SetHeight(80);
230     /* Set first entry as selected, if $this->gotoLdapServer is empty
231      *  or given entry is no longer available ...
232      */
233     $found = false;
234     foreach($this->goLdapServerList as $server => $name){
235       if($this->gotoLdapServer==$server){
236         $found = true;
237       }
238     }
239     
240     /* Add Entries
241      */
242     foreach($this->goLdapServerList as $server => $visible){
243       $use ="";
244       if(($this->gotoLdapServer == $server) || ($found == false)) {
245         $found = true;
246         $use = " checked ";
247       };
249       $SelectBoxLdapServer->AddEntry(
250           array(
251             array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
252                   "attach"=>"style='border-left:0px;'"),
253             array("string"=>$visible)
254             ));
255     }
257     $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
259     foreach (array("gotoModules" ) as $val){
260       $smarty->assign("$val", $this->$val);
261     }
263     /* Values */
264     foreach(array("gotoBootKernel", "customParameters") as $val){
265       $smarty->assign($val, $this->$val);
266     }
268     /* Radio button group */
269     if (preg_match("/G/", $this->bootmode)) {
270       $smarty->assign("graphicalbootup", "checked");
271     } else {
272       $smarty->assign("graphicalbootup", "");
273     }
274     if (preg_match("/T/", $this->bootmode)) {
275       $smarty->assign("textbootup", "checked");
276     } else {
277       $smarty->assign("textbootup", "");
278     }
279     if (preg_match("/D/", $this->bootmode)) {
280       $smarty->assign("debugbootup", "checked");
281     } else {
282       $smarty->assign("debugbootup", "");
283     }
285     /* Show main page */
286     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
287   }
289   function remove_from_parent()
290   {
291     if($this->acl_is_removeable()){
292       $this->handle_post_events("remove");
293       @log::log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
294     }
295   }
298   /* Save data to object */
299   function save_object()
300   {
301     plugin::save_object();
303     /* Save group radio buttons */
304     if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
305       $this->bootmode= $_POST["bootmode"];
306     }
308     /* Save kernel parameters */
309     if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
310       $this->customParameters= $_POST["customParameters"];
311     }
312   }
315   /* Save to LDAP */
316   function save()
317   {
318     /* Find proper terminal path for tftp configuration
319        FIXME: This is suboptimal when the default has changed to
320        another location! */
321     if ($this->gotoTerminalPath == "default-inherit"){
322       $ldap= $this->config->get_ldap_link();
324       /* Strip relevant part from dn, keep trailing ',' */
325       $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
326       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
328       /* Walk from top to base and try to load default values for
329          'gotoTerminalPath'. Abort when an entry is found. */
330       while (TRUE){
331         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
333         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
334             $this->config->current['BASE'], array('gotoTerminalPath'));
335         $attrs= $ldap->fetch();
336         if (isset($attrs['gotoTerminalPath'])){
337           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
338           break;
339         }
341         /* Nothing left? */
342         if ($tmp == ""){
343           break;
344         }
345       }
346     }
348     /* Add semi automatic values */
349     // FIXME: LDAP Server may not be set here...
350     $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
351       $this->gotoTerminalPath.
352       ",ro,hard,nolock,fg,rsize=8192 ".
353       "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
355     switch ($this->bootmode){
356       case "D":
357         $this->gotoKernelParameters.= " debug";
358       break;
359       case "G":
360         $this->gotoKernelParameters.= " splash=silent";
361       break;
362     }
363     if ($this->customParameters != ""){
364       $this->gotoKernelParameters.= " o ".$this->customParameters;
365     }
367     plugin::save();
369     /* Add missing arrays */
370     foreach (array("gotoModules") as $val){
371       if (isset ($this->$val) && count ($this->$val) != 0){
372     
373         $this->attrs["$val"]= array_unique($this->$val);
374       }
375       if(!isset($this->attrs["$val"])){
376        $this->attrs["$val"]=array();
377       }
378     }
380     /* Strip out 'default' values */
381     foreach(array("gotoBootKernel","gotoLdapServer") as $value){
382       if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
383         $this->attrs[$value] = array();
384       } 
385     }
387      /* prepare share settings */
388     $tmp = array();
389     foreach($this->gotoShares as $name => $settings){
390       $tmp2 = split("\|",$name);
391       $name = $tmp2[0];
392       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
393     }
394     $this->attrs['gotoShare']=$tmp;
396     /* Write back to ldap */
397     $ldap= $this->config->get_ldap_link();
398     $ldap->cd($this->dn);
399     
400     $this->cleanup();
401     $ldap->modify ($this->attrs); 
403     @log::log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
405     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
406     $this->handle_post_events("modify");
407   }
409   /* Add value to array, check if unique */
410   function add_list (&$array, $value)
411   {
412     if ($value != ""){
413       $array[]= $value;
414       sort($array);
415       array_unique ($array);
416     }
417   }
420   /* Delete value to array, check if unique */
421   function del_list (&$array, $list)
422   {
423     $tmp= array();
424     foreach ($array as $mod){
425       if (!in_array($mod, $list)){
426         $tmp[]= $mod;
427       }
428     }
429     $array= $tmp;
430   }
432    /* Generate ListBox frindly output for the defined shares
433    * Possibly Add or remove an attribute here,
434    */
435   function printOutAssignedShares()
436   {
437     $a_return = array();
438     if(is_array($this->gotoShares)){
439       foreach($this->gotoShares as $share){
440         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
441       }
442     }
443     return($a_return);
444   }
446   /* Return plugin informations for acl handling */
447   function plInfo()
448   {
449     return (array(
450           "plShortName"   => _("Startup"),
451           "plDescription" => _("Terminal startup"),
452           "plSelfModify"  => FALSE,
453           "plDepends"     => array(),
454           "plPriority"    => 5,
455           "plSection"     => array("administration"),
456           "plCategory"    => array("terminal"),
458           "plProvidedAcls"=> array(
459             "gotoLdapServer"      => _("Ldap server"),
460             "gotoShare"           => _("Shares"),
461             "gotoModules"         => _("Kernel modules"),
462             "gotoBootKernel"      => _("Boot kernel"), 
463             "gotoKernelParameters"=> _("Kernel parameter"))
464           ));
465   }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>