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 $goLdapServerList= array();
11 var $gotoBootKernel= "default";
12 var $gotoKernelParameters= "";
13 var $gotoLdapServer= "";
14 var $gotoModules= array();
15 var $gotoTerminalPath= "";
16 var $gotoBootKernels= array();
18 /* Ldap server list */
19 var $gotoLdapServers = array();
20 var $gotoLdapServerList = array();
21 var $gotoLdap_inherit = FALSE;
23 /* Share */
24 var $gotoShares = array();// Currently Share Option
25 var $gotoShare = ""; // currently selected Share Option
26 var $gotoShareSelections= array();// Available Shares for this account in Listbox format
27 var $gotoAvailableShares= array();// Available Shares for this account
30 /* attribute list for save action */
31 var $attributes= array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters","gotoModules");
32 var $objectclasses= array("GOhard");
33 var $view_logged = FALSE;
35 /* Helper */
36 var $customParameters= "";
37 var $orig_dn= "";
38 var $ignore_account= TRUE;
40 function termstartup (&$config, $dn= NULL, $parent= NULL)
41 {
42 plugin::plugin ($config, $dn, $parent);
44 $this->gotoBootKernels = array("default"=> ("default"));
46 /* Get arrays */
47 foreach (array("gotoModules") as $val){
48 $this->$val = array();
49 if (isset($this->attrs["$val"]["count"])){
50 for ($i= 0; $i<$this->attrs["count"]; $i++){
51 if (isset($this->attrs["$val"][$i])){
52 array_push($this->$val, $this->attrs["$val"][$i]);
53 }
54 }
55 }
56 sort ($this->$val);
57 $this->$val= array_unique($this->$val);
58 }
60 if (preg_match("/ o /", $this->gotoKernelParameters)){
61 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
62 } else {
63 $this->customParameters= "";
64 }
66 /* Prepare Shares */
67 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
68 unset($this->attrs['gotoShare']['count']);
69 foreach($this->attrs['gotoShare'] as $share){
70 $tmp = $tmp2 = array();
71 $tmp = split("\|",$share);
72 $tmp2['server'] =$tmp[0];
73 $tmp2['name'] =$tmp[1];
74 $tmp2['mountPoint'] =$tmp[2];
75 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
76 }
77 }
79 $this->gotoShareSelections= $config->getShareList(true);
80 $this->gotoAvailableShares= $config->getShareList(false);
82 $this->orig_dn= $this->dn;
84 /* Get list of boot kernels */
85 if (isset($this->config->data['TABS'])){
86 $command= $this->config->search(get_class($this), "KERNELS",array('tabs'));
88 if (!check_command($command)){
89 $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
90 get_class($this));
91 } else {
92 $fh= popen($command, "r");
93 while (!feof($fh)) {
94 $buffer= trim(fgets($fh, 256));
96 if(!empty($buffer)){
98 $name=$value = $buffer;
100 if(preg_match("/:/",$buffer)){
101 $name = preg_replace("/:.*$/","",$buffer);
102 $value= preg_replace("/^.*:/","",$buffer);
103 $this->gotoBootKernels[$name]= $name.":".$value;
104 }else{
105 $this->gotoBootKernels[$name]= $value;
106 }
107 }
108 }
109 pclose($fh);
110 }
112 }
114 foreach($this->config->data['SERVERS']['LDAP'] as $server) {
115 $this->gotoLdapServerList[]= $server;
116 }
117 if(isset($this->attrs['gotoLdapServer'])){
118 unset($this->attrs['gotoLdapServer']['count']);
119 sort($this->attrs['gotoLdapServer']);
120 foreach($this->attrs['gotoLdapServer'] as $value){
121 $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
122 }
123 }
124 if(!count($this->gotoLdapServers)){
125 $this->gotoLdap_inherit = TRUE;
126 }
128 /* Load hardware list */
129 $ldap= $this->config->get_ldap_link();
130 $ldap->cd($this->config->current['BASE']);
131 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))");
132 if ($ldap->count() == 1){
133 $map= array("gotoLdapServer","gotoBootKernel");
134 $attrs= $ldap->fetch();
136 foreach ($map as $name){
137 if (!isset($attrs[$name][0])){
138 continue;
139 }
140 switch ($name){
141 case 'gotoBootKernel':
142 $this->gotoBootKernels["default"] = "["._("inherited")."]";
143 break;
144 case 'gotoLdapServer':
145 $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
146 break;
147 }
148 }
149 }
150 }
152 function execute()
153 {
154 /* Call parent execute */
155 plugin::execute();
157 if($this->is_account && !$this->view_logged){
158 $this->view_logged = TRUE;
159 new log("view","terminal/".get_class($this),$this->dn);
160 }
162 /* Do we need to flip is_account state? */
163 if (isset($_POST['modify_state'])){
164 $this->is_account= !$this->is_account;
165 }
167 /* Do we represent a valid terminal? */
168 if (!$this->is_account && $this->parent === NULL){
169 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
170 _("This 'dn' has no terminal features.")."</b>";
171 return ($display);
172 }
174 /* Add module */
175 if (isset ($_POST['add_module'])){
176 if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
177 $this->add_list ($this->gotoModules, $_POST['module']);
178 }
179 }
181 /* Delete module */
182 if (isset ($_POST['delete_module'])){
183 if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
184 $this->del_list ($this->gotoModules, $_POST['modules_list']);
185 }
186 }
188 /* Show main page */
189 $smarty= get_smarty();
191 /* Assign acls */
192 $tmp = $this->plInfo();
193 foreach($tmp['plProvidedAcls'] as $name => $translation){
194 $smarty->assign($name."ACL",$this->getacl($name));
195 }
198 /* In this section server shares will be defined
199 * A user can select one of the given shares and a mount point
200 * and attach this combination to his setup.
201 */
202 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
203 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
204 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
206 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
207 * This entry will be, a combination of mountPoint and sharedefinitions
208 */
209 if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
210 /* We assign a share to this user, if we don't know where to mount the share */
211 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
212 print_red(_("You must specify a valid mount point."));
213 }else{
214 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
215 $s_mount = $_POST['gotoShareMountPoint'];
216 /* Preparing the new assignment */
217 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
218 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
219 }
220 }
222 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
223 * If there is no defined share selected, we will abort the deletion without any message
224 */
225 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
226 unset($this->gotoShares[$_POST['gotoShare']]);
227 }
229 $smarty->assign("gotoShares",$this->printOutAssignedShares());
230 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
232 /* Create divSelectBox for ldap server selection
233 */
234 $SelectBoxLdapServer = new divSelectBox("LdapServer");
235 $SelectBoxLdapServer->SetHeight(80);
237 /* Add new ldap server to the list */
238 if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
239 if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
240 $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
241 if(!in_array($to_add,$this->gotoLdapServers)){
242 $this->gotoLdapServers[] = $to_add;
243 }
244 }
245 }
247 /* Move ldap servers up and down */
248 if(!$this->gotoLdap_inherit){
249 foreach($_POST as $name => $value){
250 if(preg_match("/sort_ldap_up_/",$name)){
251 $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
252 $from = $id;
253 $to = $id -1;
254 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
255 if($tmp){
256 $this->gotoLdapServers = $tmp;
257 }
258 break;
259 }
260 if(preg_match("/sort_ldap_down_/",$name)){
261 $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
262 $from = $id;
263 $to = $id +1;
264 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
265 if($tmp){
266 $this->gotoLdapServers = $tmp;
267 }
268 break;
269 }
270 if(preg_match("/gotoLdapRemove_/",$name)){
271 $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
272 $value = $this->gotoLdapServers[$id];
273 $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
274 break;
275 }
276 }
277 }
278 /* Add Entries
279 */
280 foreach($this->gotoLdapServers as $key => $server){
281 if(!in_array($server,$this->gotoLdapServerList)){
282 $server = $server." <font style='color:red'>(missing)</font>";
283 }
285 $SelectBoxLdapServer->AddEntry(
286 array(array("string" => $server),
287 array("string" =>
288 "<input class='center' type='image' src='images/sort_up.png' name='sort_ldap_up_".$key."'> ".
289 "<input class='center' type='image' src='images/sort_down.png' name='sort_ldap_down_".$key."'> ".
290 "<input class='center' type='image' src='images/edittrash.png' name='gotoLdapRemove_".$key."'>",
291 "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
292 }
294 if($this->gotoLdap_inherit){
295 $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
296 }else{
297 $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
298 }
300 $list = array();
301 foreach($this->gotoLdapServerList as $key => $entry){
302 if(!in_array($entry,$this->gotoLdapServers)){
303 $list[$key] = $entry;
304 }
305 }
306 $smarty->assign("gotoLdapServers", $SelectBoxLdapServer->DrawList());
307 $smarty->assign("gotoLdapServerList", $list);
308 $smarty->assign("gotoLdap_inherit", $this->gotoLdap_inherit);
309 $smarty->assign("JS", session::get('js'));
311 foreach (array("gotoModules" ) as $val){
312 $smarty->assign("$val", $this->$val);
313 }
315 /* Values */
316 foreach(array("gotoBootKernel", "customParameters") as $val){
317 $smarty->assign($val, $this->$val);
318 }
320 /* Show main page */
321 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
322 }
324 function remove_from_parent()
325 {
326 if($this->acl_is_removeable()){
327 $this->handle_post_events("remove");
328 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
329 }
330 }
333 /* Save data to object */
334 function save_object()
335 {
336 plugin::save_object();
338 if(isset($_POST['TerminalStarttabPosted'])){
339 if(isset($_POST['gotoLdap_inherit'])){
340 $this->gotoLdap_inherit = TRUE;
341 }else{
342 $this->gotoLdap_inherit = FALSE;
343 }
345 /* Save kernel parameters */
346 if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
347 $this->customParameters= $_POST["customParameters"];
348 }
349 }
350 }
353 /* Save to LDAP */
354 function save()
355 {
356 /* Find proper terminal path for tftp configuration
357 FIXME: This is suboptimal when the default has changed to
358 another location! */
359 if ($this->gotoTerminalPath == "default-inherit"){
360 $ldap= $this->config->get_ldap_link();
362 /* Strip relevant part from dn, keep trailing ',' */
363 $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalou')."/i", "", $this->dn);
364 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
366 /* Walk from top to base and try to load default values for
367 'gotoTerminalPath'. Abort when an entry is found. */
368 while (TRUE){
369 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
371 $ldap->cat("cn=default,".get_ou('terminalou').$tmp.
372 $this->config->current['BASE'], array('gotoTerminalPath'));
373 $attrs= $ldap->fetch();
374 if (isset($attrs['gotoTerminalPath'])){
375 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
376 break;
377 }
379 /* Nothing left? */
380 if ($tmp == ""){
381 break;
382 }
383 }
384 }
386 /* Add semi automatic values */
387 // FIXME: LDAP Server may not be set here...
388 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
389 $this->gotoTerminalPath.
390 ",ro,hard,nolock,fg,rsize=8192 ".
391 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
393 if ($this->customParameters != ""){
394 $this->gotoKernelParameters.= " o ".$this->customParameters;
395 }
397 plugin::save();
399 /* Add missing arrays */
400 foreach (array("gotoModules") as $val){
401 if (isset ($this->$val) && count ($this->$val) != 0){
403 $this->attrs["$val"]= array_unique($this->$val);
404 }
405 if(!isset($this->attrs["$val"])){
406 $this->attrs["$val"]=array();
407 }
408 }
410 /* Prepare list of ldap servers */
411 $this->attrs['gotoLdapServer'] = array();
412 if(!$this->gotoLdap_inherit){
413 $i = 0;
414 foreach($this->gotoLdapServers as $server){
415 $i ++;
416 $this->attrs['gotoLdapServer'][] = $i.":".$server;
417 }
418 }
420 /* Check if LDAP server has changed */
421 $ldap_changed= ($this->attrs['gotoLdapServer'] != $this->saved_attributes['gotoLdapServer']);
423 /* Strip out 'default' values */
424 foreach(array("gotoBootKernel") as $value){
425 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default"){
426 $this->attrs[$value] = array();
427 }
428 }
430 /* prepare share settings */
431 $tmp = array();
432 foreach($this->gotoShares as $name => $settings){
433 $tmp2 = split("\|",$name);
434 $name = $tmp2[0];
435 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
436 }
437 $this->attrs['gotoShare']=$tmp;
439 /* Write back to ldap */
440 $ldap= $this->config->get_ldap_link();
441 $ldap->cd($this->dn);
443 $this->cleanup();
444 $ldap->modify ($this->attrs);
446 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
448 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
449 $this->handle_post_events("modify");
451 if ($ldap_changed){
452 gosaSupportDaemon::send("gosa_reload_ldap_config", $this->parent->by_object['termgeneric']->netConfigDNS->macAddress);
453 }
454 }
456 /* Add value to array, check if unique */
457 function add_list (&$array, $value)
458 {
459 if ($value != ""){
460 $array[]= $value;
461 sort($array);
462 array_unique ($array);
463 }
464 }
467 /* Delete value to array, check if unique */
468 function del_list (&$array, $list)
469 {
470 $tmp= array();
471 foreach ($array as $mod){
472 if (!in_array($mod, $list)){
473 $tmp[]= $mod;
474 }
475 }
476 $array= $tmp;
477 }
479 /* Generate ListBox frindly output for the defined shares
480 * Possibly Add or remove an attribute here,
481 */
482 function printOutAssignedShares()
483 {
484 $a_return = array();
485 if(is_array($this->gotoShares)){
486 foreach($this->gotoShares as $share){
487 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
488 }
489 }
490 return($a_return);
491 }
494 function PrepareForCopyPaste($source)
495 {
496 plugin::PrepareForCopyPaste($source);
498 $source_o = new termstartup ($this->config, $source['dn']);
500 foreach(array("gotoModules", "gotoKernelParameters","gotoShares","customParameters","gotoTerminalPath","gotoShares","goLdapServerList","gotoBootKernel","gotoLdapServer","gotoBootKernels") as $attr){
501 $this->$attr = $source_o->$attr;
502 }
503 }
506 function array_switch_item($ar,$from,$to)
507 {
508 if(!is_array($ar)){
509 return(false);
510 }
511 if(!isset($ar[$from])){
512 return(false);
513 }
514 if(!isset($ar[$to])){
515 return(false);
516 }
518 $tmp = $ar[$from];
519 $ar[$from] = $ar[$to];
520 $ar[$to] = $tmp;
521 return($ar);
522 }
525 /* Return plugin informations for acl handling */
526 static function plInfo()
527 {
528 return (array(
529 "plShortName" => _("Startup"),
530 "plDescription" => _("Terminal startup"),
531 "plSelfModify" => FALSE,
532 "plDepends" => array(),
533 "plPriority" => 5,
534 "plSection" => array("administration"),
535 "plCategory" => array("terminal"),
537 "plProvidedAcls"=> array(
538 "gotoLdapServer" => _("Ldap server"),
539 "gotoShare" => _("Shares"),
540 "gotoModules" => _("Kernel modules"),
541 "gotoBootKernel" => _("Boot kernel"),
542 "gotoKernelParameters"=> _("Kernel parameter"))
543 ));
544 }
547 }
549 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
550 ?>