1 <?php
3 class termgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $gotoMode= "locked";
7 var $gotoTerminalPath= "";
8 var $gotoSwapServer= "";
9 var $gotoSyslogServer= "";
10 var $gotoSyslogServers = array();
11 var $gotoNtpServer= array();
12 var $gotoNtpServers= array();
13 var $gotoSndModule= "";
14 var $gotoFloppyEnable= "";
15 var $gotoCdromEnable= "";
16 var $ghCpuType= "-";
17 var $ghMemSize= "-";
18 var $ghUsbSupport= "-";
19 var $ghNetNic= array();
20 var $ghIdeDev= array();
21 var $ghScsiDev= array();
22 var $ghGfxAdapter= "-";
23 var $ghSoundAdapter= "-";
24 var $gotoLastUser= "-";
25 var $netConfigDNS;
27 /* Needed values and lists */
28 var $base= "";
29 var $cn= "";
30 var $description= "";
31 var $orig_dn= "";
32 var $orig_cn= "";
34 var $inheritTimeServer = true;
36 /* Plugin side filled */
37 var $modes= array();
39 /* attribute list for save action */
40 var $ignore_account= TRUE;
41 var $attributes= array("gotoMode", "gotoTerminalPath",
42 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
43 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44 "ghCpuType", "ghMemSize","ghUsbSupport", "description",
45 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
46 var $objectclasses= array("top", "gotoTerminal", "GOhard");
48 var $validActions = array("reboot" => "", "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
50 var $fai_activated = FALSE;
51 var $view_logged = FALSE;
53 var $member_of_ogroup = FALSE;
55 function termgeneric (&$config, $dn= NULL, $parent= NULL)
56 {
57 /* Check if FAI is activated */
58 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
59 if(!empty($tmp)){
60 $this->fai_activated = TRUE;
61 }
63 plugin::plugin ($config, $dn, $parent);
65 if(!isset($this->parent->by_object['ogroup'])){
66 $ldap = $this->config->get_ldap_link();
67 $ldap->cd ($this->config->current['BASE']);
68 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
69 $this->member_of_ogroup = $ldap->count() >= 1;
70 }
72 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
73 /* Read arrays */
74 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
75 if (!isset($this->attrs[$val])){
76 continue;
77 }
78 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
79 array_push($this->$val, $this->attrs[$val][$i]);
80 }
81 }
83 /* Create used ntp server array */
84 $this->gotoNtpServer= array();
85 if(isset($this->attrs['gotoNtpServer'])){
86 $this->inheritTimeServer = false;
87 for($i = 0 ; $i < $this->attrs['gotoNtpServer']['count']; $i++ ){
88 $server = $this->attrs['gotoNtpServer'][$i];
89 $this->gotoNtpServer[$server] = $server;
90 }
91 }
93 /* Set inherit checkbox state */
94 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
95 $this->inheritTimeServer = true;
96 $this->gotoNtpServer=array();
97 }
99 /* You can't inherit the NTP service, if we are not member in an object group */
100 if(!$this->member_of_ogroup){
101 $this->inheritTimeServer = FALSE;
102 }
104 /* Create available ntp options */
105 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
106 foreach($this->gotoNtpServers as $key => $server){
107 if($server == "default"){
108 unset($this->gotoNtpServers[$key]);
109 }
110 }
112 $this->modes["locked"]= _("Locked");
113 $this->modes["active"]= _("Activated");
115 /* Set base */
116 if ($this->dn == "new"){
117 $ui= get_userinfo();
118 $this->base= dn2base($ui->dn);
119 } else {
120 $this->base= preg_replace ("/^[^,]+,".normalizePreg(get_ou("terminalou"))."/", "", $this->dn);
121 }
123 /* Create an array of all Syslog servers */
124 $tmp = $this->config->data['SERVERS']['SYSLOG'];
125 foreach($tmp as $server){
126 $visible = $server;
127 if($server == "default" && $this->member_of_ogroup) {
128 $visible = "["._("inherited")."]";
129 }
130 $this->gotoSyslogServers[$server] = $visible;
131 }
133 $this->orig_dn= $this->dn;
134 $this->orig_cn= $this->cn;
135 }
137 function set_acl_base($base)
138 {
139 plugin::set_acl_base($base);
140 $this->netConfigDNS->set_acl_base($base);
141 }
143 function set_acl_category($cat)
144 {
145 plugin::set_acl_category($cat);
146 $this->netConfigDNS->set_acl_category($cat);
147 }
149 function execute()
150 {
151 /* Call parent execute */
152 plugin::execute();
154 if($this->is_account && !$this->view_logged){
155 $this->view_logged = TRUE;
156 new log("view","terminal/".get_class($this),$this->dn);
157 }
159 /* Do we need to flip is_account state? */
160 if (isset($_POST['modify_state'])){
161 $this->is_account= !$this->is_account;
162 }
164 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->validActions[$_POST['saction']])){
165 $action = $_POST['saction'];
167 /* Check if we have an DaemonEvent for this action */
168 if(class_available("DaemonEvent_".$action)){
169 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
170 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
171 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
172 $tmp = new $evt['CLASS_NAME']($this->config);
173 $tmp->add_targets(array($this->netConfigDNS->macAddress));
174 $tmp->set_type(TRIGGERED_EVENT);
175 $o_queue = new gosaSupportDaemon();
176 if(!$o_queue->append($tmp)){
177 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
178 }
179 }
180 }else{
181 msg_dialog::display(_("Event error"),
182 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
183 }
185 }
187 /* Base select dialog */
188 $once = true;
189 foreach($_POST as $name => $value){
190 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
191 $once = false;
192 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
193 $this->dialog->setCurrentBase($this->base);
194 }
195 }
197 /* Dialog handling */
198 if(is_object($this->dialog)){
199 /* Must be called before save_object */
200 $this->dialog->save_object();
202 if($this->dialog->isClosed()){
203 $this->dialog = false;
204 }elseif($this->dialog->isSelected()){
206 /* A new base was selected, check if it is a valid one */
207 $tmp = $this->get_allowed_bases();
208 if(isset($tmp[$this->dialog->isSelected()])){
209 $this->base = $this->dialog->isSelected();
210 }
211 $this->dialog= false;
212 }else{
213 return($this->dialog->execute());
214 }
215 }
217 /* Do we represent a valid terminal? */
218 if (!$this->is_account && $this->parent === NULL){
219 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
220 msgPool::noValidExtension(_("terminal"))."</b>";
221 return($display);
222 }
224 /* Add new ntp Server to our list */
225 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
226 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
227 }
229 /* Delete selected NtpServer for list of used servers */
230 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
231 foreach($_POST['gotoNtpServerSelected'] as $name){
232 unset($this->gotoNtpServer[$name]);
233 }
234 }
236 /* Fill templating stuff */
237 $smarty= get_smarty();
239 $tmp = $this->plInfo();
240 foreach($tmp['plProvidedAcls'] as $name => $translation){
241 $smarty->assign($name."ACL",$this->getacl($name));
242 }
244 $smarty->assign("cn", $this->cn);
245 $smarty->assign("description", $this->description);
246 $smarty->assign("staticAddress", "");
248 $smarty->assign("bases", $this->get_allowed_bases());
250 /* tell smarty the inherit checkbox state */
251 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
253 /* Check if terminal is online */
254 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
255 $smarty->assign("actions", array( "halt" => _("Switch off"),
256 "reboot" => _("Reboot"),
257 "memcheck" => _("Memory test"),
258 "sysinfo" => _("System analysis")));
259 } else {
260 $smarty->assign("actions", array("wake" => _("Wake up"),
261 "memcheck" => _("Memory test"),
262 "sysinfo" => _("System analysis")));
263 }
265 /* Arrays */
266 $smarty->assign("modes", $this->modes);
268 $tmp2 = array();
269 foreach($this->config->data['SERVERS']['NFS'] as $server){
270 if($server != "default"){
271 $tmp2[$server]= $server;
272 }else{
273 if($this->member_of_ogroup){
274 $tmp2[$server]="["._("inherited")."]";
275 }
276 }
277 }
279 $smarty->assign("nfsservers", $tmp2);
280 $smarty->assign("syslogservers", $this->gotoSyslogServers);
282 $tmp = array();
283 foreach($this->gotoNtpServers as $server){
284 if(!in_array($server,$this->gotoNtpServer)){
285 $tmp[$server] = $server;
286 }
287 }
289 $smarty->assign("ntpservers", $tmp);
290 $smarty->assign("fai_activated",$this->fai_activated);
292 /* Variables */
293 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
294 $smarty->assign($val."_select", $this->$val);
295 }
297 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
299 /* Show main page */
300 $str = $this->netConfigDNS->execute();
301 if(is_object($this->netConfigDNS->dialog)){
302 return($str);
303 }
304 $smarty->assign("netconfig", $str);
305 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
306 }
308 function remove_from_parent()
309 {
310 if($this->acl_is_removeable()){
311 $ldap= $this->config->get_ldap_link();
312 $ldap->cd($this->dn);
313 $ldap->cat($this->dn, array('dn'));
314 if($ldap->count()){
315 $this->netConfigDNS->remove_from_parent();
316 $ldap->rmDir($this->dn);
318 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
320 if (!$ldap->success()){
321 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
322 }
324 /* Optionally execute a command after we're done */
325 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
327 /* Delete references to object groups */
328 $ldap->cd ($this->config->current['BASE']);
329 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
330 while ($ldap->fetch()){
331 $og= new ogroup($this->config, $ldap->getDN());
332 unset($og->member[$this->dn]);
333 $og->save ();
334 }
336 /* Remove all accessTo/trust dependencies */
337 update_accessTo($this->cn,"");
338 }
340 /* Clean queue form entries with this mac
341 */
342 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
343 $q = new gosaSupportDaemon();
344 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
345 }
346 }
347 }
350 /* Save data to object */
351 function save_object()
352 {
353 /* Create a base backup and reset the
354 base directly after calling plugin::save_object();
355 Base will be set seperatly a few lines below */
356 $base_tmp = $this->base;
357 plugin::save_object();
358 $this->base = $base_tmp;
360 /* Set new base if allowed */
361 $tmp = $this->get_allowed_bases();
362 if(isset($_POST['base'])){
363 if(isset($tmp[$_POST['base']])){
364 $this->base= $_POST['base'];
365 }
366 }
368 $this->netConfigDNS->save_object();
370 /* Save terminal path to parent since it is used by termstartup, too */
371 if(isset($this->parent->by_object['termstartup'])){
372 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
373 }
375 if(isset($_POST['termgeneric_posted'])){
376 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
377 $this->inheritTimeServer = true;
378 }else{
379 $this->inheritTimeServer = false;
380 }
381 }
383 if(isset($_POST["inheritAll"])){
384 $this->set_everything_to_inherited();
385 }
387 }
390 /* Check supplied data */
391 function check()
392 {
393 /* Call common method to give check the hook */
394 $message= plugin::check();
396 /* Skip IP & Mac checks if this is a template */
397 if($this->cn != "default"){
398 $message= array_merge($message, $this->netConfigDNS->check());
399 }
401 /* Permissions for that base? */
402 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
404 if ($this->cn == ""){
405 $message[]= msgPool::required(_("Name"));
406 }
408 /* Check if given name is a valid host/dns name */
409 if(!tests::is_dns_name($this->cn) ){
410 $message[] = msgPool::invalid(_("Name"));
411 }
413 if ($this->orig_dn == 'new'){
414 $ldap= $this->config->get_ldap_link();
415 $ldap->cd ($this->base);
417 /* It is possible to have a 'default' terminal on every base */
418 if($this->cn == "default"){
419 $ldap->cat($this->dn);
420 }else{
421 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
422 }
423 if ($ldap->count() != 0){
424 while ($attrs= $ldap->fetch()){
425 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
426 continue;
427 } else {
428 if ($attrs['dn'] != $this->orig_dn){
429 $message[]= msgPool::duplicated(_("Name"));
430 break;
431 }
432 }
433 }
434 }
435 }
437 /* Check for valid ntpServer selection */
438 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
439 $message[]= msgPool::required(_("NTP server"));
440 }
442 return ($message);
443 }
446 /* Save to LDAP */
447 function save()
448 {
449 /* Detect mode changes */
450 $activate= (isset($this->saved_attributes['gotoMode']) &&
451 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
452 $this->gotoMode == "enabled" &&
453 tests::is_ip($this->netConfigDNS->ipHostNumber));
455 plugin::save();
457 /* Strip out 'default' values */
458 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
459 if(isset($this->attrs[$val])){
460 if ($this->attrs[$val] == "default"){
461 $this->attrs[$val]= array();
462 }
463 }
464 }
466 /* Add missing arrays */
467 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
468 if (isset ($this->$val) && count ($this->$val) != 0){
469 $this->attrs["$val"]= $this->$val;
470 }
471 }
473 /* Remove all empty values */
474 if ($this->orig_dn == 'new'){
475 $attrs= array();
476 foreach ($this->attrs as $key => $val){
477 if (is_array($val) && count($val) == 0){
478 continue;
479 }
480 $attrs[$key]= $val;
481 }
482 $this->attrs= $attrs;
483 }
485 /* Set ntpServers */
486 $this->attrs['gotoNtpServer'] = array();
487 if(!$this->inheritTimeServer){
488 foreach($this->gotoNtpServer as $server){
489 $this->attrs['gotoNtpServer'][] = $server;
490 }
491 }
493 /* Write back to ldap */
494 $ldap= $this->config->get_ldap_link();
495 if ($this->orig_dn == 'new'){
496 $ldap->cd($this->config->current['BASE']);
497 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
498 $ldap->cd($this->dn);
499 if (!count($this->attrs['gotoNtpServer'])){
500 unset($this->attrs['gotoNtpServer']);
501 }
502 $ldap->add($this->attrs);
503 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
504 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
505 } else {
506 $ldap->cd($this->dn);
507 $this->cleanup();
508 $ldap->modify ($this->attrs);
509 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
510 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
512 /* Update all accessTo/trust dependencies */
513 if($this->orig_cn != $this->cn){
514 update_accessTo($this->orig_cn,$this->cn);
515 }
516 }
518 /* cn=default and macAddress=- indicates that this is a template */
519 if($this->cn == "default"){
520 $this->netConfigDNS->macAddress = "-";
521 }
523 $this->netConfigDNS->cn = $this->cn;
524 $this->netConfigDNS->save();
525 if (!$ldap->success()){
526 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
527 }
529 /* Send installation activation
530 */
531 if ($activate && class_available("DaemonEvent")){
532 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
533 $o_queue = new gosaSupportDaemon();
534 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
535 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
536 $tmp = new $evt['CLASS_NAME']($this->config);
537 $tmp->set_type(TRIGGERED_EVENT);
538 $tmp->add_targets(array($this->netConfigDNS->macAddress));
539 if(!$o_queue->append($tmp)){
540 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
541 }
542 }
543 }
544 }
547 /* Display generic part for server copy & paste */
548 function getCopyDialog()
549 {
550 $vars = array("cn");
551 $smarty = get_smarty();
552 $smarty->assign("cn" ,$this->cn);
553 $smarty->assign("object","terminal");
554 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
555 $ret = array();
556 $ret['string'] = $str;
557 $ret['status'] = "";
558 return($ret);
559 }
562 function saveCopyDialog()
563 {
564 if(isset($_POST['cn'])){
565 $this->cn = $_POST['cn'];
566 }
567 }
570 function PrepareForCopyPaste($source)
571 {
572 plugin::PrepareForCopyPaste($source);
573 if(isset($source['macAddress'][0])){
574 $this->netConfigDNS->macAddress = $source['macAddress'][0];
575 }
576 if(isset($source['ipHostNumber'][0])){
577 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
578 }
580 /* Create used ntp server array */
581 $this->gotoNtpServer= array();
582 if(isset($source['gotoNtpServer'])){
583 $this->inheritTimeServer = false;
584 unset($source['gotoNtpServer']['count']);
585 foreach($source['gotoNtpServer'] as $server){
586 $this->gotoNtpServer[$server] = $server;
587 }
588 }
590 /* Set inherit checkbox state */
591 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
592 $this->inheritTimeServer = true;
593 $this->gotoNtpServer=array();
594 }
595 }
598 /* Return plugin informations for acl handling */
599 static function plInfo()
600 {
601 return (array(
602 "plShortName" => _("Terminal"),
603 "plDescription" => _("Terminal generic"),
604 "plSelfModify" => FALSE,
605 "plDepends" => array(),
606 "plPriority" => 1,
607 "plSection" => array("administration"),
608 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
609 "objectClass" => "gotoTerminal")),
610 "plProvidedAcls"=> array(
611 "gotoMode" => _("Mode"),
612 "gotoTerminalPath" => _("Root server"),
613 "gotoSwapServer" => _("Swap server"),
614 "gotoSyslogServer" => _("Syslog server enabled"),
615 "gotoNtpServer" => _("Ntp server settings"),
616 "base" => _("Base"),
617 "cn" => _("Name"),
618 "description" => _("Description"),
619 "gotoRootPasswd" => _("Root password"),
620 "FAIstate" => _("Action flag"))
621 ));
622 }
625 function set_everything_to_inherited()
626 {
627 $this->gotoTerminalPath = "default";
628 $this->gotoSwapServer = "default" ;
629 $this->gotoSyslogServer = "default";
630 $this->inheritTimeServer = TRUE;
632 /* Set workstation service attributes to inherited */
633 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
634 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
635 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
636 $this->parent->by_object['termservice']->$name = "default";
637 }
638 }
640 /* Set workstation startup attributes to inherited */
641 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
642 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
643 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
644 }
645 }
646 }
648 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
649 ?>