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