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