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= "";
33 var $orig_base= "";
35 var $inheritTimeServer = true;
37 /* Plugin side filled */
38 var $modes= array();
40 /* attribute list for save action */
41 var $ignore_account= TRUE;
42 var $attributes= array("gotoMode", "gotoTerminalPath",
43 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
44 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
45 "ghCpuType", "ghMemSize","ghUsbSupport", "description",
46 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
47 var $objectclasses= array("top", "gotoTerminal", "GOhard");
49 var $validActions = array("reboot" => "", "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
51 var $fai_activated = FALSE;
52 var $view_logged = FALSE;
54 var $member_of_ogroup = FALSE;
56 function termgeneric (&$config, $dn= NULL, $parent= NULL)
57 {
58 /* Check if FAI is activated */
59 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
60 if(!empty($tmp)){
61 $this->fai_activated = TRUE;
62 }
64 plugin::plugin ($config, $dn, $parent);
66 if(!isset($this->parent->by_object['ogroup'])){
67 $ldap = $this->config->get_ldap_link();
68 $ldap->cd ($this->config->current['BASE']);
69 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
70 $this->member_of_ogroup = $ldap->count() >= 1;
71 }
73 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
74 /* Read arrays */
75 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
76 if (!isset($this->attrs[$val])){
77 continue;
78 }
79 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
80 array_push($this->$val, $this->attrs[$val][$i]);
81 }
82 }
84 /* Create used ntp server array */
85 $this->gotoNtpServer= array();
86 if(isset($this->attrs['gotoNtpServer'])){
87 $this->inheritTimeServer = false;
88 for($i = 0 ; $i < $this->attrs['gotoNtpServer']['count']; $i++ ){
89 $server = $this->attrs['gotoNtpServer'][$i];
90 $this->gotoNtpServer[$server] = $server;
91 }
92 }
94 /* Set inherit checkbox state */
95 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
96 $this->inheritTimeServer = true;
97 $this->gotoNtpServer=array();
98 }
100 /* You can't inherit the NTP service, if we are not member in an object group */
101 if(!$this->member_of_ogroup){
102 $this->inheritTimeServer = FALSE;
103 }
105 /* Create available ntp options */
106 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
107 foreach($this->gotoNtpServers as $key => $server){
108 if($server == "default"){
109 unset($this->gotoNtpServers[$key]);
110 }
111 }
113 $this->modes["locked"]= _("Locked");
114 $this->modes["active"]= _("Activated");
116 /* Set base */
117 if ($this->dn == "new"){
118 $ui= get_userinfo();
119 $this->base= dn2base($ui->dn);
120 } else {
121 $this->base= preg_replace ("/^[^,]+,".normalizePreg(get_ou("terminalou"))."/", "", $this->dn);
122 }
124 /* Create an array of all Syslog servers */
125 $tmp = $this->config->data['SERVERS']['SYSLOG'];
126 foreach($tmp as $server){
127 $visible = $server;
128 if($server == "default" && $this->member_of_ogroup) {
129 $visible = "["._("inherited")."]";
130 }
131 $this->gotoSyslogServers[$server] = $visible;
132 }
134 $this->orig_dn= $this->dn;
135 $this->orig_cn= $this->cn;
136 $this->orig_base= $this->base;
137 }
139 function set_acl_base($base)
140 {
141 plugin::set_acl_base($base);
142 $this->netConfigDNS->set_acl_base($base);
143 }
145 function set_acl_category($cat)
146 {
147 plugin::set_acl_category($cat);
148 $this->netConfigDNS->set_acl_category($cat);
149 }
151 function execute()
152 {
153 /* Call parent execute */
154 plugin::execute();
156 if($this->is_account && !$this->view_logged){
157 $this->view_logged = TRUE;
158 new log("view","terminal/".get_class($this),$this->dn);
159 }
161 /* Do we need to flip is_account state? */
162 if (isset($_POST['modify_state'])){
163 $this->is_account= !$this->is_account;
164 }
166 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->validActions[$_POST['saction']])){
167 $action = $_POST['saction'];
169 /* Check if we have an DaemonEvent for this action */
170 if(class_available("DaemonEvent_".$action)){
171 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
172 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
173 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
174 $tmp = new $evt['CLASS_NAME']($this->config);
175 $tmp->add_targets(array($this->netConfigDNS->macAddress));
176 $tmp->set_type(TRIGGERED_EVENT);
177 $o_queue = new gosaSupportDaemon();
178 if(!$o_queue->append($tmp)){
179 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
180 }
181 }
182 }else{
183 msg_dialog::display(_("Event error"),
184 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
185 }
187 }
189 /* Base select dialog */
190 $once = true;
191 foreach($_POST as $name => $value){
192 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
193 $once = false;
194 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
195 $this->dialog->setCurrentBase($this->base);
196 }
197 }
199 /* Dialog handling */
200 if(is_object($this->dialog)){
201 /* Must be called before save_object */
202 $this->dialog->save_object();
204 if($this->dialog->isClosed()){
205 $this->dialog = false;
206 }elseif($this->dialog->isSelected()){
208 /* A new base was selected, check if it is a valid one */
209 $tmp = $this->get_allowed_bases();
210 if(isset($tmp[$this->dialog->isSelected()])){
211 $this->base = $this->dialog->isSelected();
212 }
213 $this->dialog= false;
214 }else{
215 return($this->dialog->execute());
216 }
217 }
219 /* Do we represent a valid terminal? */
220 if (!$this->is_account && $this->parent === NULL){
221 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
222 msgPool::noValidExtension(_("terminal"))."</b>";
223 return($display);
224 }
226 /* Add new ntp Server to our list */
227 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
228 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
229 }
231 /* Delete selected NtpServer for list of used servers */
232 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
233 foreach($_POST['gotoNtpServerSelected'] as $name){
234 unset($this->gotoNtpServer[$name]);
235 }
236 }
238 /* Fill templating stuff */
239 $smarty= get_smarty();
241 $tmp = $this->plInfo();
242 foreach($tmp['plProvidedAcls'] as $name => $translation){
243 $smarty->assign($name."ACL",$this->getacl($name));
244 }
246 $smarty->assign("cn", $this->cn);
247 $smarty->assign("description", $this->description);
248 $smarty->assign("staticAddress", "");
250 $smarty->assign("bases", $this->get_allowed_bases());
252 /* tell smarty the inherit checkbox state */
253 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
255 /* Check if terminal is online */
256 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
257 $smarty->assign("actions", array( "halt" => _("Switch off"),
258 "reboot" => _("Reboot"),
259 "memcheck" => _("Memory test"),
260 "sysinfo" => _("System analysis")));
261 } else {
262 $smarty->assign("actions", array("wake" => _("Wake up"),
263 "memcheck" => _("Memory test"),
264 "sysinfo" => _("System analysis")));
265 }
267 /* Arrays */
268 $smarty->assign("modes", $this->modes);
270 $tmp2 = array();
271 foreach($this->config->data['SERVERS']['NFS'] as $server){
272 if($server != "default"){
273 $tmp2[$server]= $server;
274 }else{
275 if($this->member_of_ogroup){
276 $tmp2[$server]="["._("inherited")."]";
277 }
278 }
279 }
281 $smarty->assign("nfsservers", $tmp2);
282 $smarty->assign("syslogservers", $this->gotoSyslogServers);
284 $tmp = array();
285 foreach($this->gotoNtpServers as $server){
286 if(!in_array($server,$this->gotoNtpServer)){
287 $tmp[$server] = $server;
288 }
289 }
291 $smarty->assign("ntpservers", $tmp);
292 $smarty->assign("fai_activated",$this->fai_activated);
294 /* Variables */
295 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
296 $smarty->assign($val."_select", $this->$val);
297 }
299 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
301 /* Show main page */
302 $str = $this->netConfigDNS->execute();
303 if(is_object($this->netConfigDNS->dialog)){
304 return($str);
305 }
306 $smarty->assign("netconfig", $str);
307 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
308 }
310 function remove_from_parent()
311 {
312 if($this->acl_is_removeable()){
313 $ldap= $this->config->get_ldap_link();
314 $ldap->cd($this->dn);
315 $ldap->cat($this->dn, array('dn'));
316 if($ldap->count()){
317 $this->netConfigDNS->remove_from_parent();
318 $ldap->rmDir($this->dn);
320 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
322 if (!$ldap->success()){
323 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
324 }
326 /* Optionally execute a command after we're done */
327 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
329 /* Delete references to object groups */
330 $ldap->cd ($this->config->current['BASE']);
331 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
332 while ($ldap->fetch()){
333 $og= new ogroup($this->config, $ldap->getDN());
334 unset($og->member[$this->dn]);
335 $og->save ();
336 }
338 /* Remove all accessTo/trust dependencies */
339 update_accessTo($this->cn,"");
340 }
342 /* Clean queue form entries with this mac
343 */
344 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
345 $q = new gosaSupportDaemon();
346 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
347 }
348 }
349 }
352 /* Save data to object */
353 function save_object()
354 {
355 /* Create a base backup and reset the
356 base directly after calling plugin::save_object();
357 Base will be set seperatly a few lines below */
358 $base_tmp = $this->base;
359 plugin::save_object();
360 $this->base = $base_tmp;
362 /* Set new base if allowed */
363 $tmp = $this->get_allowed_bases();
364 if(isset($_POST['base'])){
365 if(isset($tmp[$_POST['base']])){
366 $this->base= $_POST['base'];
367 }
368 }
370 $this->netConfigDNS->save_object();
372 /* Save terminal path to parent since it is used by termstartup, too */
373 if(isset($this->parent->by_object['termstartup'])){
374 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
375 }
377 if(isset($_POST['termgeneric_posted'])){
378 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
379 $this->inheritTimeServer = true;
380 }else{
381 $this->inheritTimeServer = false;
382 }
383 }
385 if(isset($_POST["inheritAll"])){
386 $this->set_everything_to_inherited();
387 }
389 }
392 /* Check supplied data */
393 function check()
394 {
395 /* Call common method to give check the hook */
396 $message= plugin::check();
398 /* Skip IP & Mac checks if this is a template */
399 if($this->cn != "default"){
400 $message= array_merge($message, $this->netConfigDNS->check());
401 }
403 /* Permissions for that base? */
404 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
406 if ($this->cn == ""){
407 $message[]= msgPool::required(_("Name"));
408 }
410 /* Check if given name is a valid host/dns name */
411 if(!tests::is_dns_name($this->cn) ){
412 $message[] = msgPool::invalid(_("Name"));
413 }
415 if ($this->orig_dn == 'new'){
416 $ldap= $this->config->get_ldap_link();
417 $ldap->cd ($this->base);
419 /* It is possible to have a 'default' terminal on every base */
420 if($this->cn == "default"){
421 $ldap->cat($this->dn);
422 }else{
423 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
424 }
425 if ($ldap->count() != 0){
426 while ($attrs= $ldap->fetch()){
427 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
428 continue;
429 } else {
430 if ($attrs['dn'] != $this->orig_dn){
431 $message[]= msgPool::duplicated(_("Name"));
432 break;
433 }
434 }
435 }
436 }
437 }
439 /* Check for valid ntpServer selection */
440 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
441 $message[]= msgPool::required(_("NTP server"));
442 }
444 /* Check if we are allowed to create or move this object
445 */
446 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
447 $message[] = msgPool::permCreate();
448 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
449 $message[] = msgPool::permMove();
450 }
452 return ($message);
453 }
456 /* Save to LDAP */
457 function save()
458 {
459 /* Detect mode changes */
460 $activate= (isset($this->saved_attributes['gotoMode']) &&
461 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
462 $this->gotoMode == "enabled" &&
463 tests::is_ip($this->netConfigDNS->ipHostNumber));
465 plugin::save();
467 /* Strip out 'default' values */
468 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
469 if(isset($this->attrs[$val])){
470 if ($this->attrs[$val] == "default"){
471 $this->attrs[$val]= array();
472 }
473 }
474 }
476 /* Add missing arrays */
477 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
478 if (isset ($this->$val) && count ($this->$val) != 0){
479 $this->attrs["$val"]= $this->$val;
480 }
481 }
483 /* Remove all empty values */
484 if ($this->orig_dn == 'new'){
485 $attrs= array();
486 foreach ($this->attrs as $key => $val){
487 if (is_array($val) && count($val) == 0){
488 continue;
489 }
490 $attrs[$key]= $val;
491 }
492 $this->attrs= $attrs;
493 }
495 /* Set ntpServers */
496 $this->attrs['gotoNtpServer'] = array();
497 if(!$this->inheritTimeServer){
498 foreach($this->gotoNtpServer as $server){
499 $this->attrs['gotoNtpServer'][] = $server;
500 }
501 }
503 /* Write back to ldap */
504 $ldap= $this->config->get_ldap_link();
505 if ($this->orig_dn == 'new'){
506 $ldap->cd($this->config->current['BASE']);
507 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
508 $ldap->cd($this->dn);
509 if (!count($this->attrs['gotoNtpServer'])){
510 unset($this->attrs['gotoNtpServer']);
511 }
512 $ldap->add($this->attrs);
513 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
514 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
515 } else {
516 $ldap->cd($this->dn);
517 $this->cleanup();
518 $ldap->modify ($this->attrs);
519 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
520 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
522 /* Update all accessTo/trust dependencies */
523 if($this->orig_cn != $this->cn){
524 update_accessTo($this->orig_cn,$this->cn);
525 }
526 }
528 /* cn=default and macAddress=- indicates that this is a template */
529 if($this->cn == "default"){
530 $this->netConfigDNS->macAddress = "-";
531 }
533 $this->netConfigDNS->cn = $this->cn;
534 $this->netConfigDNS->save();
535 if (!$ldap->success()){
536 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
537 }
539 /* Send installation activation
540 */
541 if ($activate && class_available("DaemonEvent")){
542 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
543 $o_queue = new gosaSupportDaemon();
544 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
545 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
546 $tmp = new $evt['CLASS_NAME']($this->config);
547 $tmp->set_type(TRIGGERED_EVENT);
548 $tmp->add_targets(array($this->netConfigDNS->macAddress));
549 if(!$o_queue->append($tmp)){
550 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
551 }
552 }
553 }
554 }
557 /* Display generic part for server copy & paste */
558 function getCopyDialog()
559 {
560 $vars = array("cn");
561 $smarty = get_smarty();
562 $smarty->assign("cn" ,$this->cn);
563 $smarty->assign("object","terminal");
564 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
565 $ret = array();
566 $ret['string'] = $str;
567 $ret['status'] = "";
568 return($ret);
569 }
572 function saveCopyDialog()
573 {
574 if(isset($_POST['cn'])){
575 $this->cn = $_POST['cn'];
576 }
577 }
580 function PrepareForCopyPaste($source)
581 {
582 plugin::PrepareForCopyPaste($source);
583 if(isset($source['macAddress'][0])){
584 $this->netConfigDNS->macAddress = $source['macAddress'][0];
585 }
586 if(isset($source['ipHostNumber'][0])){
587 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
588 }
590 /* Create used ntp server array */
591 $this->gotoNtpServer= array();
592 if(isset($source['gotoNtpServer'])){
593 $this->inheritTimeServer = false;
594 unset($source['gotoNtpServer']['count']);
595 foreach($source['gotoNtpServer'] as $server){
596 $this->gotoNtpServer[$server] = $server;
597 }
598 }
600 /* Set inherit checkbox state */
601 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
602 $this->inheritTimeServer = true;
603 $this->gotoNtpServer=array();
604 }
605 }
608 /* Return plugin informations for acl handling */
609 static function plInfo()
610 {
611 return (array(
612 "plShortName" => _("Terminal"),
613 "plDescription" => _("Terminal generic"),
614 "plSelfModify" => FALSE,
615 "plDepends" => array(),
616 "plPriority" => 1,
617 "plSection" => array("administration"),
618 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
619 "objectClass" => "gotoTerminal")),
620 "plProvidedAcls"=> array(
621 "gotoMode" => _("Mode"),
622 "gotoTerminalPath" => _("Root server"),
623 "gotoSwapServer" => _("Swap server"),
624 "gotoSyslogServer" => _("Syslog server enabled"),
625 "gotoNtpServer" => _("Ntp server settings"),
626 "base" => _("Base"),
627 "cn" => _("Name"),
628 "description" => _("Description"),
629 "gotoRootPasswd" => _("Root password"),
630 "FAIstate" => _("Action flag"))
631 ));
632 }
635 function set_everything_to_inherited()
636 {
637 $this->gotoTerminalPath = "default";
638 $this->gotoSwapServer = "default" ;
639 $this->gotoSyslogServer = "default";
640 $this->inheritTimeServer = TRUE;
642 /* Set workstation service attributes to inherited */
643 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
644 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
645 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
646 $this->parent->by_object['termservice']->$name = "default";
647 }
648 }
650 /* Set workstation startup attributes to inherited */
651 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
652 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
653 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
654 }
655 }
656 }
658 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
659 ?>