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 /* Ldap server list */
20 var $gotoLdapServers = array();
21 var $gotoLdapServerList = array();
22 var $gotoLdap_inherit = FALSE;
24 /* Share */
25 var $gotoShares = array();// Currently Share Option
26 var $gotoShare = ""; // currently selected Share Option
27 var $gotoShareSelections= array();// Available Shares for this account in Listbox format
28 var $gotoAvailableShares= array();// Available Shares for this account
31 /* attribute list for save action */
32 var $attributes= array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters","gotoModules");
33 var $objectclasses= array("GOhard");
34 var $view_logged = FALSE;
36 /* Helper */
37 var $customParameters= "";
38 var $orig_dn= "";
39 var $ignore_account= TRUE;
41 function termstartup (&$config, $dn= NULL, $parent= NULL)
42 {
43 plugin::plugin ($config, $dn, $parent);
45 $this->gotoBootKernels = array("default-inherit"=>"["._("inherited")."]");
47 /* Get arrays */
48 foreach (array("gotoModules") as $val){
49 $this->$val = array();
50 if (isset($this->attrs["$val"]["count"])){
51 for ($i= 0; $i<$this->attrs["count"]; $i++){
52 if (isset($this->attrs["$val"][$i])){
53 array_push($this->$val, $this->attrs["$val"][$i]);
54 }
55 }
56 }
57 sort ($this->$val);
58 $this->$val= array_unique($this->$val);
59 }
61 /* Parse Kernel Parameters to decide what boot mode is enabled */
62 if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
63 $this->bootmode= "G";
64 } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
65 $this->bootmode= "D";
66 } elseif ($this->gotoKernelParameters == "") {
67 $this->bootmode= "G";
68 } else {
69 $this->bootmode= "T";
70 }
71 if (preg_match("/ o /", $this->gotoKernelParameters)){
72 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
73 } else {
74 $this->customParameters= "";
75 }
77 /* Prepare Shares */
78 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
79 unset($this->attrs['gotoShare']['count']);
80 foreach($this->attrs['gotoShare'] as $share){
81 $tmp = $tmp2 = array();
82 $tmp = split("\|",$share);
83 $tmp2['server'] =$tmp[0];
84 $tmp2['name'] =$tmp[1];
85 $tmp2['mountPoint'] =$tmp[2];
86 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
87 }
88 }
90 $this->gotoShareSelections= $config->getShareList(true);
91 $this->gotoAvailableShares= $config->getShareList(false);
93 $this->orig_dn= $this->dn;
95 /* Get list of boot kernels */
96 if (isset($this->config->data['TABS'])){
97 $command= $this->config->search(get_class($this), "KERNELS",array('tabs'));
99 if (!check_command($command)){
100 $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
101 get_class($this));
102 } else {
103 $fh= popen($command, "r");
104 while (!feof($fh)) {
105 $buffer= trim(fgets($fh, 256));
107 if(!empty($buffer)){
109 $name=$value = $buffer;
111 if(preg_match("/:/",$buffer)){
112 $name = preg_replace("/:.*$/","",$buffer);
113 $value= preg_replace("/^.*:/","",$buffer);
114 $this->gotoBootKernels[$name]= $name.":".$value;
115 }else{
116 $this->gotoBootKernels[$name]= $value;
117 }
118 }
119 }
120 pclose($fh);
121 }
123 }
125 foreach($this->config->data['SERVERS']['LDAP'] as $server) {
126 $this->gotoLdapServerList[]= $server;
127 }
128 if(isset($this->attrs['gotoLdapServer'])){
129 for($i = 0 ; $i < $this->attrs['gotoLdapServer']['count'];$i++){
130 $this->gotoLdapServers[] = $this->attrs['gotoLdapServer'][$i];
131 }
132 }
133 if(!count($this->gotoLdapServers)){
134 $this->gotoLdap_inherit = TRUE;
135 }
137 /* Load hardware list */
138 $ldap= $this->config->get_ldap_link();
139 $ldap->cd($this->config->current['BASE']);
140 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))");
141 if ($ldap->count() == 1){
142 $map= array("gotoLdapServer");
143 $attrs= $ldap->fetch();
145 foreach ($map as $name){
146 if (!isset($attrs[$name][0])){
147 continue;
148 }
150 switch ($name){
151 case 'gotoLdapServer':
152 $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
153 break;
154 }
155 }
156 }
157 }
159 function execute()
160 {
161 /* Call parent execute */
162 plugin::execute();
164 if($this->is_account && !$this->view_logged){
165 $this->view_logged = TRUE;
166 new log("view","terminal/".get_class($this),$this->dn);
167 }
169 /* Do we need to flip is_account state? */
170 if (isset($_POST['modify_state'])){
171 $this->is_account= !$this->is_account;
172 }
174 /* Do we represent a valid terminal? */
175 if (!$this->is_account && $this->parent === NULL){
176 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
177 _("This 'dn' has no terminal features.")."</b>";
178 return ($display);
179 }
181 /* Add module */
182 if (isset ($_POST['add_module'])){
183 if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
184 $this->add_list ($this->gotoModules, $_POST['module']);
185 }
186 }
188 /* Delete module */
189 if (isset ($_POST['delete_module'])){
190 if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
191 $this->del_list ($this->gotoModules, $_POST['modules_list']);
192 }
193 }
195 /* Show main page */
196 $smarty= get_smarty();
198 /* Assign acls */
199 $tmp = $this->plInfo();
200 foreach($tmp['plProvidedAcls'] as $name => $translation){
201 $smarty->assign($name."ACL",$this->getacl($name));
202 }
205 /* In this section server shares will be defined
206 * A user can select one of the given shares and a mount point
207 * and attach this combination to his setup.
208 */
209 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
210 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
211 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
213 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
214 * This entry will be, a combination of mountPoint and sharedefinitions
215 */
216 if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
217 /* We assign a share to this user, if we don't know where to mount the share */
218 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
219 print_red(_("You must specify a valid mount point."));
220 }else{
221 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
222 $s_mount = $_POST['gotoShareMountPoint'];
223 /* Preparing the new assignment */
224 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
225 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
226 }
227 }
229 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
230 * If there is no defined share selected, we will abort the deletion without any message
231 */
232 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
233 unset($this->gotoShares[$_POST['gotoShare']]);
234 }
236 $smarty->assign("gotoShares",$this->printOutAssignedShares());
237 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
239 /* Create divSelectBox for ldap server selection
240 */
241 $SelectBoxLdapServer = new divSelectBox("LdapServer");
242 $SelectBoxLdapServer->SetHeight(80);
244 /* Add new ldap server to the list */
245 if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
246 if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
247 $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
248 if(!in_array($to_add,$this->gotoLdapServers)){
249 $this->gotoLdapServers[] = $to_add;
250 }
251 }
252 }
254 /* Move ldap servers up and down */
255 if(!$this->gotoLdap_inherit){
256 foreach($_POST as $name => $value){
257 if(preg_match("/sort_ldap_up_/",$name)){
258 $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
259 $from = $id;
260 $to = $id -1;
261 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
262 if($tmp){
263 $this->gotoLdapServers = $tmp;
264 }
265 break;
266 }
267 if(preg_match("/sort_ldap_down_/",$name)){
268 $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
269 $from = $id;
270 $to = $id +1;
271 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
272 if($tmp){
273 $this->gotoLdapServers = $tmp;
274 }
275 break;
276 }
277 if(preg_match("/gotoLdapRemove_/",$name)){
278 $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
279 $value = $this->gotoLdapServers[$id];
280 $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
281 break;
282 }
283 }
284 }
285 /* Add Entries
286 */
287 foreach($this->gotoLdapServers as $key => $server){
288 if(!in_array($server,$this->gotoLdapServerList)){
289 $server = $server." <font style='color:red'>(missing)</font>";
290 }
292 $SelectBoxLdapServer->AddEntry(
293 array(array("string" => $server),
294 array("string" =>
295 "<input class='center' type='image' src='images/sort_up.png' name='sort_ldap_up_".$key."'> ".
296 "<input class='center' type='image' src='images/sort_down.png' name='sort_ldap_down_".$key."'> ".
297 "<input class='center' type='image' src='images/edittrash.png' name='gotoLdapRemove_".$key."'>",
298 "attach" => "style='border-right:0px;'")));
299 }
301 if($this->gotoLdap_inherit){
302 $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
303 }else{
304 $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
305 }
307 $list = array();
308 foreach($this->gotoLdapServerList as $key => $entry){
309 if(!in_array($entry,$this->gotoLdapServers)){
310 $list[$key] = $entry;
311 }
312 }
313 $smarty->assign("gotoLdapServers", $SelectBoxLdapServer->DrawList());
314 $smarty->assign("gotoLdapServerList", $list);
315 $smarty->assign("gotoLdap_inherit", $this->gotoLdap_inherit);
316 $smarty->assign("JS", $_SESSION['js']);
318 foreach (array("gotoModules" ) as $val){
319 $smarty->assign("$val", $this->$val);
320 }
322 /* Values */
323 foreach(array("gotoBootKernel", "customParameters") as $val){
324 $smarty->assign($val, $this->$val);
325 }
327 /* Radio button group */
328 if (preg_match("/G/", $this->bootmode)) {
329 $smarty->assign("graphicalbootup", "checked");
330 } else {
331 $smarty->assign("graphicalbootup", "");
332 }
333 if (preg_match("/T/", $this->bootmode)) {
334 $smarty->assign("textbootup", "checked");
335 } else {
336 $smarty->assign("textbootup", "");
337 }
338 if (preg_match("/D/", $this->bootmode)) {
339 $smarty->assign("debugbootup", "checked");
340 } else {
341 $smarty->assign("debugbootup", "");
342 }
344 /* Show main page */
345 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
346 }
348 function remove_from_parent()
349 {
350 if($this->acl_is_removeable()){
351 $this->handle_post_events("remove");
352 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
353 }
354 }
357 /* Save data to object */
358 function save_object()
359 {
360 plugin::save_object();
362 if(isset($_POST['TerminalStarttabPosted'])){
363 if(isset($_POST['gotoLdap_inherit'])){
364 $this->gotoLdap_inherit = TRUE;
365 }else{
366 $this->gotoLdap_inherit = FALSE;
367 }
369 /* Save group radio buttons */
370 if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
371 $this->bootmode= $_POST["bootmode"];
372 }
374 /* Save kernel parameters */
375 if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
376 $this->customParameters= $_POST["customParameters"];
377 }
378 }
379 }
382 /* Save to LDAP */
383 function save()
384 {
385 /* Find proper terminal path for tftp configuration
386 FIXME: This is suboptimal when the default has changed to
387 another location! */
388 if ($this->gotoTerminalPath == "default-inherit"){
389 $ldap= $this->config->get_ldap_link();
391 /* Strip relevant part from dn, keep trailing ',' */
392 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
393 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
395 /* Walk from top to base and try to load default values for
396 'gotoTerminalPath'. Abort when an entry is found. */
397 while (TRUE){
398 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
400 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
401 $this->config->current['BASE'], array('gotoTerminalPath'));
402 $attrs= $ldap->fetch();
403 if (isset($attrs['gotoTerminalPath'])){
404 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
405 break;
406 }
408 /* Nothing left? */
409 if ($tmp == ""){
410 break;
411 }
412 }
413 }
415 /* Add semi automatic values */
416 // FIXME: LDAP Server may not be set here...
417 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
418 $this->gotoTerminalPath.
419 ",ro,hard,nolock,fg,rsize=8192 ".
420 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
422 switch ($this->bootmode){
423 case "D":
424 $this->gotoKernelParameters.= " debug";
425 break;
426 case "G":
427 $this->gotoKernelParameters.= " splash=silent";
428 break;
429 }
430 if ($this->customParameters != ""){
431 $this->gotoKernelParameters.= " o ".$this->customParameters;
432 }
434 plugin::save();
436 /* Add missing arrays */
437 foreach (array("gotoModules") as $val){
438 if (isset ($this->$val) && count ($this->$val) != 0){
440 $this->attrs["$val"]= array_unique($this->$val);
441 }
442 if(!isset($this->attrs["$val"])){
443 $this->attrs["$val"]=array();
444 }
445 }
447 /* Prepare list of ldap servers */
448 $this->attrs['gotoLdapServer'] = array();
449 if(!$this->gotoLdap_inherit){
450 foreach($this->gotoLdapServers as $server){
451 $this->attrs['gotoLdapServer'][] = $server;
452 }
453 }
455 /* Strip out 'default' values */
456 foreach(array("gotoBootKernel") as $value){
457 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
458 $this->attrs[$value] = array();
459 }
460 }
462 /* prepare share settings */
463 $tmp = array();
464 foreach($this->gotoShares as $name => $settings){
465 $tmp2 = split("\|",$name);
466 $name = $tmp2[0];
467 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
468 }
469 $this->attrs['gotoShare']=$tmp;
471 /* Write back to ldap */
472 $ldap= $this->config->get_ldap_link();
473 $ldap->cd($this->dn);
475 $this->cleanup();
476 $ldap->modify ($this->attrs);
478 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
480 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
481 $this->handle_post_events("modify");
482 }
484 /* Add value to array, check if unique */
485 function add_list (&$array, $value)
486 {
487 if ($value != ""){
488 $array[]= $value;
489 sort($array);
490 array_unique ($array);
491 }
492 }
495 /* Delete value to array, check if unique */
496 function del_list (&$array, $list)
497 {
498 $tmp= array();
499 foreach ($array as $mod){
500 if (!in_array($mod, $list)){
501 $tmp[]= $mod;
502 }
503 }
504 $array= $tmp;
505 }
507 /* Generate ListBox frindly output for the defined shares
508 * Possibly Add or remove an attribute here,
509 */
510 function printOutAssignedShares()
511 {
512 $a_return = array();
513 if(is_array($this->gotoShares)){
514 foreach($this->gotoShares as $share){
515 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
516 }
517 }
518 return($a_return);
519 }
522 function PrepareForCopyPaste($source)
523 {
524 plugin::PrepareForCopyPaste($source);
526 $source_o = new termstartup ($this->config, $source['dn']);
528 foreach(array("gotoModules", "gotoKernelParameters","gotoShares","customParameters","bootmode","gotoTerminalPath","gotoShares","goLdapServerList","gotoBootKernel","gotoLdapServer","gotoBootKernels") as $attr){
529 $this->$attr = $source_o->$attr;
530 }
531 }
534 function array_switch_item($ar,$from,$to)
535 {
536 if(!is_array($ar)){
537 return(false);
538 }
539 if(!isset($ar[$from])){
540 return(false);
541 }
542 if(!isset($ar[$to])){
543 return(false);
544 }
546 $tmp = $ar[$from];
547 $ar[$from] = $ar[$to];
548 $ar[$to] = $tmp;
549 return($ar);
550 }
553 /* Return plugin informations for acl handling */
554 function plInfo()
555 {
556 return (array(
557 "plShortName" => _("Startup"),
558 "plDescription" => _("Terminal startup"),
559 "plSelfModify" => FALSE,
560 "plDepends" => array(),
561 "plPriority" => 5,
562 "plSection" => array("administration"),
563 "plCategory" => array("terminal"),
565 "plProvidedAcls"=> array(
566 "gotoLdapServer" => _("Ldap server"),
567 "gotoShare" => _("Shares"),
568 "gotoModules" => _("Kernel modules"),
569 "gotoBootKernel" => _("Boot kernel"),
570 "gotoKernelParameters"=> _("Kernel parameter"))
571 ));
572 }
575 }
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>