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";
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"=> ("default"));
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 unset($this->attrs['gotoLdapServer']['count']);
130 sort($this->attrs['gotoLdapServer']);
131 foreach($this->attrs['gotoLdapServer'] as $value){
132 $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
133 }
134 }
135 if(!count($this->gotoLdapServers)){
136 $this->gotoLdap_inherit = TRUE;
137 }
139 /* Load hardware list */
140 $ldap= $this->config->get_ldap_link();
141 $ldap->cd($this->config->current['BASE']);
142 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))");
143 if ($ldap->count() == 1){
144 $map= array("gotoLdapServer","gotoBootKernel");
145 $attrs= $ldap->fetch();
147 foreach ($map as $name){
148 if (!isset($attrs[$name][0])){
149 continue;
150 }
151 switch ($name){
152 case 'gotoBootKernel':
153 $this->gotoBootKernels["default"] = "["._("inherited")."]";
154 break;
155 case 'gotoLdapServer':
156 $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
157 break;
158 }
159 }
160 }
161 }
163 function execute()
164 {
165 /* Call parent execute */
166 plugin::execute();
168 if($this->is_account && !$this->view_logged){
169 $this->view_logged = TRUE;
170 new log("view","terminal/".get_class($this),$this->dn);
171 }
173 /* Do we need to flip is_account state? */
174 if (isset($_POST['modify_state'])){
175 $this->is_account= !$this->is_account;
176 }
178 /* Do we represent a valid terminal? */
179 if (!$this->is_account && $this->parent === NULL){
180 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
181 _("This 'dn' has no terminal features.")."</b>";
182 return ($display);
183 }
185 /* Add module */
186 if (isset ($_POST['add_module'])){
187 if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
188 $this->add_list ($this->gotoModules, $_POST['module']);
189 }
190 }
192 /* Delete module */
193 if (isset ($_POST['delete_module'])){
194 if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
195 $this->del_list ($this->gotoModules, $_POST['modules_list']);
196 }
197 }
199 /* Show main page */
200 $smarty= get_smarty();
202 /* Assign acls */
203 $tmp = $this->plInfo();
204 foreach($tmp['plProvidedAcls'] as $name => $translation){
205 $smarty->assign($name."ACL",$this->getacl($name));
206 }
209 /* In this section server shares will be defined
210 * A user can select one of the given shares and a mount point
211 * and attach this combination to his setup.
212 */
213 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
214 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
215 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
217 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
218 * This entry will be, a combination of mountPoint and sharedefinitions
219 */
220 if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
221 /* We assign a share to this user, if we don't know where to mount the share */
222 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
223 print_red(_("You must specify a valid mount point."));
224 }else{
225 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
226 $s_mount = $_POST['gotoShareMountPoint'];
227 /* Preparing the new assignment */
228 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
229 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
230 }
231 }
233 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
234 * If there is no defined share selected, we will abort the deletion without any message
235 */
236 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
237 unset($this->gotoShares[$_POST['gotoShare']]);
238 }
240 $smarty->assign("gotoShares",$this->printOutAssignedShares());
241 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
243 /* Create divSelectBox for ldap server selection
244 */
245 $SelectBoxLdapServer = new divSelectBox("LdapServer");
246 $SelectBoxLdapServer->SetHeight(80);
248 /* Add new ldap server to the list */
249 if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
250 if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
251 $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
252 if(!in_array($to_add,$this->gotoLdapServers)){
253 $this->gotoLdapServers[] = $to_add;
254 }
255 }
256 }
258 /* Move ldap servers up and down */
259 if(!$this->gotoLdap_inherit){
260 foreach($_POST as $name => $value){
261 if(preg_match("/sort_ldap_up_/",$name)){
262 $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
263 $from = $id;
264 $to = $id -1;
265 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
266 if($tmp){
267 $this->gotoLdapServers = $tmp;
268 }
269 break;
270 }
271 if(preg_match("/sort_ldap_down_/",$name)){
272 $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
273 $from = $id;
274 $to = $id +1;
275 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
276 if($tmp){
277 $this->gotoLdapServers = $tmp;
278 }
279 break;
280 }
281 if(preg_match("/gotoLdapRemove_/",$name)){
282 $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
283 $value = $this->gotoLdapServers[$id];
284 $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
285 break;
286 }
287 }
288 }
289 /* Add Entries
290 */
291 foreach($this->gotoLdapServers as $key => $server){
292 if(!in_array($server,$this->gotoLdapServerList)){
293 $server = $server." <font style='color:red'>(missing)</font>";
294 }
296 $SelectBoxLdapServer->AddEntry(
297 array(array("string" => $server),
298 array("string" =>
299 "<input class='center' type='image' src='images/sort_up.png' name='sort_ldap_up_".$key."'> ".
300 "<input class='center' type='image' src='images/sort_down.png' name='sort_ldap_down_".$key."'> ".
301 "<input class='center' type='image' src='images/edittrash.png' name='gotoLdapRemove_".$key."'>",
302 "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
303 }
305 if($this->gotoLdap_inherit){
306 $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
307 }else{
308 $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
309 }
311 $list = array();
312 foreach($this->gotoLdapServerList as $key => $entry){
313 if(!in_array($entry,$this->gotoLdapServers)){
314 $list[$key] = $entry;
315 }
316 }
317 $smarty->assign("gotoLdapServers", $SelectBoxLdapServer->DrawList());
318 $smarty->assign("gotoLdapServerList", $list);
319 $smarty->assign("gotoLdap_inherit", $this->gotoLdap_inherit);
320 $smarty->assign("JS", session::get('js'));
322 foreach (array("gotoModules" ) as $val){
323 $smarty->assign("$val", $this->$val);
324 }
326 /* Values */
327 foreach(array("gotoBootKernel", "customParameters") as $val){
328 $smarty->assign($val, $this->$val);
329 }
331 /* Radio button group */
332 if (preg_match("/G/", $this->bootmode)) {
333 $smarty->assign("graphicalbootup", "checked");
334 } else {
335 $smarty->assign("graphicalbootup", "");
336 }
337 if (preg_match("/T/", $this->bootmode)) {
338 $smarty->assign("textbootup", "checked");
339 } else {
340 $smarty->assign("textbootup", "");
341 }
342 if (preg_match("/D/", $this->bootmode)) {
343 $smarty->assign("debugbootup", "checked");
344 } else {
345 $smarty->assign("debugbootup", "");
346 }
348 /* Show main page */
349 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
350 }
352 function remove_from_parent()
353 {
354 if($this->acl_is_removeable()){
355 $this->handle_post_events("remove");
356 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
357 }
358 }
361 /* Save data to object */
362 function save_object()
363 {
364 plugin::save_object();
366 if(isset($_POST['TerminalStarttabPosted'])){
367 if(isset($_POST['gotoLdap_inherit'])){
368 $this->gotoLdap_inherit = TRUE;
369 }else{
370 $this->gotoLdap_inherit = FALSE;
371 }
373 /* Save group radio buttons */
374 if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
375 $this->bootmode= $_POST["bootmode"];
376 }
378 /* Save kernel parameters */
379 if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
380 $this->customParameters= $_POST["customParameters"];
381 }
382 }
383 }
386 /* Save to LDAP */
387 function save()
388 {
389 /* Find proper terminal path for tftp configuration
390 FIXME: This is suboptimal when the default has changed to
391 another location! */
392 if ($this->gotoTerminalPath == "default-inherit"){
393 $ldap= $this->config->get_ldap_link();
395 /* Strip relevant part from dn, keep trailing ',' */
396 $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalou')."/i", "", $this->dn);
397 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
399 /* Walk from top to base and try to load default values for
400 'gotoTerminalPath'. Abort when an entry is found. */
401 while (TRUE){
402 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
404 $ldap->cat("cn=default,".get_ou('terminalou').$tmp.
405 $this->config->current['BASE'], array('gotoTerminalPath'));
406 $attrs= $ldap->fetch();
407 if (isset($attrs['gotoTerminalPath'])){
408 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
409 break;
410 }
412 /* Nothing left? */
413 if ($tmp == ""){
414 break;
415 }
416 }
417 }
419 /* Add semi automatic values */
420 // FIXME: LDAP Server may not be set here...
421 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
422 $this->gotoTerminalPath.
423 ",ro,hard,nolock,fg,rsize=8192 ".
424 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
426 switch ($this->bootmode){
427 case "D":
428 $this->gotoKernelParameters.= " debug";
429 break;
430 case "G":
431 $this->gotoKernelParameters.= " splash=silent";
432 break;
433 }
434 if ($this->customParameters != ""){
435 $this->gotoKernelParameters.= " o ".$this->customParameters;
436 }
438 plugin::save();
440 /* Add missing arrays */
441 foreach (array("gotoModules") as $val){
442 if (isset ($this->$val) && count ($this->$val) != 0){
444 $this->attrs["$val"]= array_unique($this->$val);
445 }
446 if(!isset($this->attrs["$val"])){
447 $this->attrs["$val"]=array();
448 }
449 }
451 /* Prepare list of ldap servers */
452 $this->attrs['gotoLdapServer'] = array();
453 if(!$this->gotoLdap_inherit){
454 $i = 0;
455 foreach($this->gotoLdapServers as $server){
456 $i ++;
457 $this->attrs['gotoLdapServer'][] = $i.":".$server;
458 }
459 }
461 /* Strip out 'default' values */
462 foreach(array("gotoBootKernel") as $value){
463 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default"){
464 $this->attrs[$value] = array();
465 }
466 }
468 /* prepare share settings */
469 $tmp = array();
470 foreach($this->gotoShares as $name => $settings){
471 $tmp2 = split("\|",$name);
472 $name = $tmp2[0];
473 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
474 }
475 $this->attrs['gotoShare']=$tmp;
477 /* Write back to ldap */
478 $ldap= $this->config->get_ldap_link();
479 $ldap->cd($this->dn);
481 $this->cleanup();
482 $ldap->modify ($this->attrs);
484 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
486 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
487 $this->handle_post_events("modify");
488 }
490 /* Add value to array, check if unique */
491 function add_list (&$array, $value)
492 {
493 if ($value != ""){
494 $array[]= $value;
495 sort($array);
496 array_unique ($array);
497 }
498 }
501 /* Delete value to array, check if unique */
502 function del_list (&$array, $list)
503 {
504 $tmp= array();
505 foreach ($array as $mod){
506 if (!in_array($mod, $list)){
507 $tmp[]= $mod;
508 }
509 }
510 $array= $tmp;
511 }
513 /* Generate ListBox frindly output for the defined shares
514 * Possibly Add or remove an attribute here,
515 */
516 function printOutAssignedShares()
517 {
518 $a_return = array();
519 if(is_array($this->gotoShares)){
520 foreach($this->gotoShares as $share){
521 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
522 }
523 }
524 return($a_return);
525 }
528 function PrepareForCopyPaste($source)
529 {
530 plugin::PrepareForCopyPaste($source);
532 $source_o = new termstartup ($this->config, $source['dn']);
534 foreach(array("gotoModules", "gotoKernelParameters","gotoShares","customParameters","bootmode","gotoTerminalPath","gotoShares","goLdapServerList","gotoBootKernel","gotoLdapServer","gotoBootKernels") as $attr){
535 $this->$attr = $source_o->$attr;
536 }
537 }
540 function array_switch_item($ar,$from,$to)
541 {
542 if(!is_array($ar)){
543 return(false);
544 }
545 if(!isset($ar[$from])){
546 return(false);
547 }
548 if(!isset($ar[$to])){
549 return(false);
550 }
552 $tmp = $ar[$from];
553 $ar[$from] = $ar[$to];
554 $ar[$to] = $tmp;
555 return($ar);
556 }
559 /* Return plugin informations for acl handling */
560 static function plInfo()
561 {
562 return (array(
563 "plShortName" => _("Startup"),
564 "plDescription" => _("Terminal startup"),
565 "plSelfModify" => FALSE,
566 "plDepends" => array(),
567 "plPriority" => 5,
568 "plSection" => array("administration"),
569 "plCategory" => array("terminal"),
571 "plProvidedAcls"=> array(
572 "gotoLdapServer" => _("Ldap server"),
573 "gotoShare" => _("Shares"),
574 "gotoModules" => _("Kernel modules"),
575 "gotoBootKernel" => _("Boot kernel"),
576 "gotoKernelParameters"=> _("Kernel parameter"))
577 ));
578 }
581 }
583 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
584 ?>