Code

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