Code

Updated filtering again
[gosa.git] / plugins / admin / systems / class_terminalStartup.inc
1 <?php
2 class termstartup extends plugin
3 {
4   /* Generic terminal attributes */
5   var $bootmode= "G";
6   var $goLdapServerList= array();
7   var $gotoBootKernel= "";
8   var $gotoKernelParameters= "";
9   var $gotoLdapServer= "";
10   var $gotoModules= array();
11   var $gotoTerminalPath= "";
12   var $gotoBootKernels= array();
14   /* Share */
15   var $gotoShares         = array();// Currently Share Option
16   var $gotoShare          = "";     // currently selected Share Option
17   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
18   var $gotoAvailableShares= array();// Available Shares for this account
21   /* attribute list for save action */
22   var $attributes= array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters","gotoModules");
23   var $objectclasses= array("GOhard");
25   /* Helper */
26   var $customParameters= "";
27   var $orig_dn= "";
28   var $ignore_account= TRUE;
30   function termstartup ($config, $dn= NULL, $parent= NULL)
31   {
32     plugin::plugin ($config, $dn, $parent);
34     /* Get arrays */
35     foreach (array("gotoModules") as $val){
36       $tmp =array();
37       if (isset($this->attrs[$val]["count"])){
38         for ($i= 0; $i<$this->attrs[$val]["count"]; $i++){
39           if (isset($this->attrs[$val][$i])){
40             $tmp[] = $this->attrs["$val"][$i];
41           }
42         }
43       }
44       $this->$val = $tmp;
45       sort ($this->$val);
46       $this->$val= array_unique($this->$val);
47     }
49     /* Parse Kernel Parameters to decide what boot mode is enabled */
50     if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
51       $this->bootmode= "G";
52     } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
53       $this->bootmode= "D";
54     } elseif ($this->gotoKernelParameters == "") {
55       $this->bootmode= "G";
56     } else {
57       $this->bootmode= "T";
58     }
59     if (preg_match("/ o /", $this->gotoKernelParameters)){
60       $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
61     } else {
62       $this->customParameters= "";
63     }
65     /* Prepare Shares */
66     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
67       unset($this->attrs['gotoShare']['count']);
68       foreach($this->attrs['gotoShare'] as $share){
69         $tmp = $tmp2 = array();
70         $tmp = split("\|",$share);
71         $tmp2['server']      =$tmp[0];
72         $tmp2['name']        =$tmp[1];
73         $tmp2['mountPoint']  =$tmp[2];
74         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
75       }
76     }
78     $this->gotoShareSelections= $config->getShareList(true);
79     $this->gotoAvailableShares= $config->getShareList(false);
81     $this->orig_dn= $this->dn;
83     /* Get list of boot kernels */
84     if (isset($this->config->data['TABS'])){
85       $command= search_config($this->config->data['TABS'], get_class($this), "KERNELS");
87       if (!check_command($command)){
88         $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
89             get_class($this));
90       } else {
91         $fh= popen($command, "r");
92         while (!feof($fh)) {
93           $buffer= trim(fgets($fh, 256));
95           if(!empty($buffer)){
97             $name=$value = $buffer;
99             if(preg_match("/:/",$buffer)){
100               $name = preg_replace("/:.*$/","",$buffer);
101               $value= preg_replace("/^.*:/","",$buffer);
102               $this->gotoBootKernels[$name]= $name.":".$value;
103             }else{
104               $this->gotoBootKernels[$name]= $value;
105             }
106             if(empty($this->gotoBootKernel)){
107               $this->gotoBootKernel = $name; 
108             }
109           }
110         }
111         pclose($fh);
112       }
114     }
115     $tmp = $this->config->data['SERVERS']['LDAP'];  
116     foreach($tmp as $server){
117       $visible = $server;
118       if($server == "default-inherit"){
119         $visible = "["._("inherited")."]";
120       }
121       $this->goLdapServerList[$server] = $visible;
122     }
124      /* Load hardware list */
125     $ldap= $this->config->get_ldap_link();
126     $ldap->cd($this->config->current['BASE']);
127     $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))");
128     if ($ldap->count() == 1){
129       $map= array("gotoLdapServer","gotoBootKernel");
130       $attrs= $ldap->fetch();
132       foreach ($map as $name){
133         if (!isset($attrs[$name][0])){
134           continue;
135         }
137         switch ($name){
138           case 'gotoLdapServer':
139             $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
140             break;
141           case 'gotoBootKernel':
142             $this->gotoBootKernels= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->gotoBootKernels);
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'])){
157       /* Only change account state if allowed */
158       if($this->is_account && $this->acl == "#all#"){
159         $this->is_account= !$this->is_account;
160       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
161         $this->is_account= !$this->is_account;
162       }
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'] != "" && chkacl ($this->acl, "gotoModule") == ""){
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']) && chkacl ($this->acl, "gotoModule") == ""){
182         $this->gotoModules = $this->del_list($this->gotoModules, $_POST['modules_list']);
183       }
184     }
186     /* Show main page */
187     $smarty= get_smarty();
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']) && chkacl($this->acl,"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'])) && chkacl($this->acl,"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       $use .= chkacl($this->acl,"gotoLdapServer");
250       $SelectBoxLdapServer->AddEntry(
251           array(
252             array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
253                   "attach"=>"style='border-left:0px;'"),
254             array("string"=>$visible)
255             ));
256     }
258     $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
260     $smarty->assign("gotoShareACL", chkacl($this->acl, "gotoShareACL"));
261     foreach (array("gotoModules" ) as $val){
262       $smarty->assign("$val", $this->$val);
263     }
265     /* Values */
266     foreach(array("gotoBootKernel", "customParameters") as $val){
267       $smarty->assign($val, $this->$val);
268       $smarty->assign($val."ACL", chkacl($this->acl, $val));
269     }
271     /* Radio button group */
272     if (preg_match("/G/", $this->bootmode)) {
273       $smarty->assign("graphicalbootup", "checked");
274     } else {
275       $smarty->assign("graphicalbootup", "");
276     }
277     if (preg_match("/T/", $this->bootmode)) {
278       $smarty->assign("textbootup", "checked");
279     } else {
280       $smarty->assign("textbootup", "");
281     }
282     if (preg_match("/D/", $this->bootmode)) {
283       $smarty->assign("debugbootup", "checked");
284     } else {
285       $smarty->assign("debugbootup", "");
286     }
288     /* ACL's */
289     foreach (array("gotoKernelParameters", "gotoModules") as $value){
290       $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
291     }
293     /* Show main page */
294     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
295   }
297   function remove_from_parent()
298   {
299       $this->handle_post_events("remove");
300   }
303   /* Save data to object */
304   function save_object()
305   {
306     plugin::save_object();
308     /* Save group radio buttons */
309     if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
310       $this->bootmode= $_POST["bootmode"];
311     }
313     /* Save kernel parameters */
314     if (chkacl ($this->acl, "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     }
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       }else{
380         $this->attrs["$val"]=array();
381       }
382     }
384     /* Strip out 'default' values */
385     foreach(array("gotoBootKernel","gotoLdapServer") as $value){
386       if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
387         $this->attrs[$value] = array();
388       } 
389     }
391      /* prepare share settings */
392     $tmp = array();
393     foreach($this->gotoShares as $name => $settings){
394       $tmp2 = split("\|",$name);
395       $name = $tmp2[0];
396       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
397     }
398     $this->attrs['gotoShare']=$tmp;
400     /* Write back to ldap */
401     $ldap= $this->config->get_ldap_link();
402     $ldap->cd($this->dn);
403     $this->cleanup();
404     $ldap->modify ($this->attrs); 
406     show_ldap_error($ldap->get_error(), _("Saving terminal startup settings failed"));
407     $this->handle_post_events("modify");
408   }
410   /* Add value to array, check if unique */
411   function add_list (&$array, $value)
412   {
413     if ($value != ""){
414       $array[]= $value;
415       sort($array);
416       array_unique ($array);
417     }
418   }
421   /* Delete value to array, check if unique */
422   function del_list ($array, $list)
423   {
424     $tmp= array();
425     foreach ($array as $mod){
426       if (!in_array($mod, $list)){
427         $tmp[]= $mod;
428       }
429     }
430     return($tmp);
431   }
433    /* Generate ListBox frindly output for the defined shares
434    * Possibly Add or remove an attribute here,
435    */
436   function printOutAssignedShares()
437   {
438     $a_return = array();
439     if(is_array($this->gotoShares)){
440       foreach($this->gotoShares as $share){
441         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
442       }
443     }
444     return($a_return);
445   }
450 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
451 ?>