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= "";
33 var $inheritTimeServer = true;
35 /* Plugin side filled */
36 var $modes= array();
38 /* attribute list for save action */
39 var $ignore_account= TRUE;
40 var $attributes= array("gotoMode", "gotoTerminalPath",
41 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
42 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
43 "ghCpuType", "ghMemSize","ghUsbSupport", "description",
44 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
45 var $objectclasses= array("top", "gotoTerminal", "GOhard");
47 var $mapActions = array("reboot" => "",
48 "rescan" => "",
49 "wake" => "",
50 "memcheck" => "memcheck",
51 "sysinfo" => "sysinfo");
53 var $fai_activated = FALSE;
54 var $view_logged = FALSE;
56 var $member_of_ogroup = FALSE;
58 function termgeneric (&$config, $dn= NULL, $parent= NULL)
59 {
60 /* Check if FAI is activated */
61 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
62 if(!empty($tmp)){
63 $this->fai_activated = TRUE;
64 }
66 plugin::plugin ($config, $dn, $parent);
68 if(!isset($this->parent->by_object['ogroup'])){
69 $ldap = $this->config->get_ldap_link();
70 $ldap->cd ($this->config->current['BASE']);
71 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
72 $this->member_of_ogroup = $ldap->count() >= 1;
73 }
75 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
76 /* Read arrays */
77 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
78 if (!isset($this->attrs[$val])){
79 continue;
80 }
81 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
82 array_push($this->$val, $this->attrs[$val][$i]);
83 }
84 }
86 /* Create used ntp server array */
87 $this->gotoNtpServer= array();
88 if(isset($this->attrs['gotoNtpServer'])){
89 $this->inheritTimeServer = false;
90 unset($this->attrs['gotoNtpServer']['count']);
91 foreach($this->attrs['gotoNtpServer'] as $server){
92 $this->gotoNtpServer[$server] = $server;
93 }
94 }
96 /* Set inherit checkbox state */
97 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
98 $this->inheritTimeServer = true;
99 $this->gotoNtpServer=array();
100 }
102 /* You can't inherit the NTP service, if we are not member in an object group */
103 if(!$this->member_of_ogroup){
104 $this->inheritTimeServer = FALSE;
105 }
107 /* Create available ntp options */
108 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
109 foreach($this->gotoNtpServers as $key => $server){
110 if($server == "default"){
111 unset($this->gotoNtpServers[$key]);
112 }
113 }
115 $this->modes["locked"]= _("Locked");
116 $this->modes["active"]= _("Activated");
118 /* Set base */
119 if ($this->dn == "new"){
120 $ui= get_userinfo();
121 $this->base= dn2base($ui->dn);
122 } else {
123 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
124 }
126 /* Create an array of all Syslog servers */
127 $tmp = $this->config->data['SERVERS']['SYSLOG'];
128 foreach($tmp as $server){
129 $visible = $server;
130 if($server == "default" && $this->member_of_ogroup) {
131 $visible = "["._("inherited")."]";
132 }
133 $this->gotoSyslogServers[$server] = $visible;
134 }
136 $this->orig_dn= $this->dn;
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->mapActions[$_POST['saction']])){
170 /* Check given action */
171 $mapEvent = array("wake" => "wakeup",
172 "instant_update" => "update");
173 $action = $_POST['saction'];
174 if(isset($mapEvent[$action])){
175 $action = $mapEvent[$action];
176 }
178 /* Check if we have an DaemonEvent for this action */
179 if(class_available("DaemonEvent_".$action)){
180 $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
181 if(isset($events['BY_CLASS']["DaemonEvent_".$action])){
182 $evt = $events['BY_CLASS']["DaemonEvent_".$action];
183 $tmp = new $evt['CLASS_NAME']($this->config);
184 $tmp->add_targets(array($this->netConfigDNS->macAddress));
185 $tmp->set_type(TRIGGERED_EVENT);
186 $o_queue = new gosaSupportDaemon();
187 if(!$o_queue->append($tmp)){
188 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
189 $o_queue->get_error()),ERROR_DIALOG);
190 }
191 }
192 }else{
194 msg_dialog::display(_("Missing Daemon Event"),
195 sprintf(_("The requested action does not exists '%s' Sending event manually."),"DaemonEvent_".$action),ERROR_DIALOG);
197 /* No event found, send action manually.
198 */
199 $method= "gosa";
200 $action= $_POST['saction'];
201 if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
202 $method= "job";
203 }
205 gosaSupportDaemon::send("${method}_trigger_action_".$action,
206 $this->netConfigDNS->macAddress,
207 array("macAddress" => $this->netConfigDNS->macAddress));
208 }
209 }
211 /* Base select dialog */
212 $once = true;
213 foreach($_POST as $name => $value){
214 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
215 $once = false;
216 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
217 $this->dialog->setCurrentBase($this->base);
218 }
219 }
221 /* Dialog handling */
222 if(is_object($this->dialog)){
223 /* Must be called before save_object */
224 $this->dialog->save_object();
226 if($this->dialog->isClosed()){
227 $this->dialog = false;
228 }elseif($this->dialog->isSelected()){
230 /* A new base was selected, check if it is a valid one */
231 $tmp = $this->get_allowed_bases();
232 if(isset($tmp[$this->dialog->isSelected()])){
233 $this->base = $this->dialog->isSelected();
234 }
235 $this->dialog= false;
236 }else{
237 return($this->dialog->execute());
238 }
239 }
241 /* Do we represent a valid terminal? */
242 if (!$this->is_account && $this->parent === NULL){
243 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
244 _("This 'dn' has no terminal features.")."</b>";
245 return($display);
246 }
248 /* Add new ntp Server to our list */
249 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
250 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
251 }
253 /* Delete selected NtpServer for list of used servers */
254 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
255 foreach($_POST['gotoNtpServerSelected'] as $name){
256 unset($this->gotoNtpServer[$name]);
257 }
258 }
260 /* Fill templating stuff */
261 $smarty= get_smarty();
263 $tmp = $this->plInfo();
264 foreach($tmp['plProvidedAcls'] as $name => $translation){
265 $smarty->assign($name."ACL",$this->getacl($name));
266 }
268 $smarty->assign("cn", $this->cn);
269 $smarty->assign("description", $this->description);
270 $smarty->assign("staticAddress", "");
272 $smarty->assign("bases", $this->get_allowed_bases());
274 /* tell smarty the inherit checkbox state */
275 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
277 /* Check if terminal is online */
278 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
279 $smarty->assign("actions", array( "halt" => _("Switch off"),
280 "reboot" => _("Reboot"),
281 "memcheck" => _("Memory test"),
282 "sysinfo" => _("System analysis")));
283 } else {
284 $smarty->assign("actions", array("wake" => _("Wake up"),
285 "memcheck" => _("Memory test"),
286 "sysinfo" => _("System analysis")));
287 }
289 /* Arrays */
290 $smarty->assign("modes", $this->modes);
292 $tmp2 = array();
293 foreach($this->config->data['SERVERS']['NFS'] as $server){
294 if($server != "default"){
295 $tmp2[$server]= $server;
296 }else{
297 if($this->member_of_ogroup){
298 $tmp2[$server]="["._("inherited")."]";
299 }
300 }
301 }
303 $smarty->assign("nfsservers", $tmp2);
304 $smarty->assign("syslogservers", $this->gotoSyslogServers);
306 $tmp = array();
307 foreach($this->gotoNtpServers as $server){
308 if(!in_array($server,$this->gotoNtpServer)){
309 $tmp[$server] = $server;
310 }
311 }
313 $smarty->assign("ntpservers", $tmp);
314 $smarty->assign("fai_activated",$this->fai_activated);
316 /* Variables */
317 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
318 $smarty->assign($val."_select", $this->$val);
319 }
321 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
323 /* Show main page */
324 $str = $this->netConfigDNS->execute();
325 if(is_object($this->netConfigDNS->dialog)){
326 return($str);
327 }
328 $smarty->assign("netconfig", $str);
329 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
330 }
332 function remove_from_parent()
333 {
334 if($this->acl_is_removeable()){
335 $ldap= $this->config->get_ldap_link();
336 $ldap->cd($this->dn);
337 $ldap->cat($this->dn, array('dn'));
338 if($ldap->count()){
339 $this->netConfigDNS->remove_from_parent();
340 $ldap->rmDir($this->dn);
342 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
344 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
346 /* Optionally execute a command after we're done */
347 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
349 /* Delete references to object groups */
350 $ldap->cd ($this->config->current['BASE']);
351 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
352 while ($ldap->fetch()){
353 $og= new ogroup($this->config, $ldap->getDN());
354 unset($og->member[$this->dn]);
355 $og->save ();
356 }
357 }
358 }
359 }
362 /* Save data to object */
363 function save_object()
364 {
365 /* Create a base backup and reset the
366 base directly after calling plugin::save_object();
367 Base will be set seperatly a few lines below */
368 $base_tmp = $this->base;
369 plugin::save_object();
370 $this->base = $base_tmp;
372 /* Set new base if allowed */
373 $tmp = $this->get_allowed_bases();
374 if(isset($_POST['base'])){
375 if(isset($tmp[$_POST['base']])){
376 $this->base= $_POST['base'];
377 }
378 }
380 $this->netConfigDNS->save_object();
382 /* Save terminal path to parent since it is used by termstartup, too */
383 if(isset($this->parent->by_object['termstartup'])){
384 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
385 }
387 if(isset($_POST['termgeneric_posted'])){
388 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
389 $this->inheritTimeServer = true;
390 }else{
391 $this->inheritTimeServer = false;
392 }
393 }
395 if(isset($_POST["inheritAll"])){
396 $this->set_everything_to_inherited();
397 }
399 }
402 /* Check supplied data */
403 function check()
404 {
405 /* Call common method to give check the hook */
406 $message= plugin::check();
408 /* Skip IP & Mac checks if this is a template */
409 if($this->cn != "default"){
410 $message= array_merge($message, $this->netConfigDNS->check());
411 }
413 /* Permissions for that base? */
414 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
416 if ($this->cn == ""){
417 $message[]= _("The required field 'Terminal name' is not set.");
418 }
420 /* Check if given name is a valid host/dns name */
421 if(!tests::is_dns_name($this->cn) ){
422 $message[] = _("Please specify a valid name for this object.");
423 }
425 if ($this->orig_dn == 'new'){
426 $ldap= $this->config->get_ldap_link();
427 $ldap->cd ($this->base);
429 /* It is possible to have a 'default' terminal on every base */
430 if($this->cn == "default"){
431 $ldap->cat($this->dn);
432 }else{
433 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
434 }
435 if ($ldap->count() != 0){
436 while ($attrs= $ldap->fetch()){
437 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
438 continue;
439 } else {
440 if ($attrs['dn'] != $this->orig_dn){
441 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
442 break;
443 }
444 }
445 }
446 }
447 }
449 /* Check for valid ntpServer selection */
450 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
451 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
452 }
454 return ($message);
455 }
458 /* Save to LDAP */
459 function save()
460 {
462 /* Move object if requested */
463 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
464 $this->move($this->orig_dn, $this->dn);
465 }
467 /* Detect mode changes */
468 $activate= (isset($this->saved_attributes['gotoMode']) &&
469 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
470 $this->gotoMode == "enabled" &&
471 tests::is_ip($this->netConfigDNS->ipHostNumber));
473 plugin::save();
475 /* Strip out 'default' values */
476 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
477 if(isset($this->attrs[$val])){
478 if ($this->attrs[$val] == "default"){
479 $this->attrs[$val]= array();
480 }
481 }
482 }
484 /* Add missing arrays */
485 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
486 if (isset ($this->$val) && count ($this->$val) != 0){
487 $this->attrs["$val"]= $this->$val;
488 }
489 }
491 /* Remove all empty values */
492 if ($this->orig_dn == 'new'){
493 $attrs= array();
494 foreach ($this->attrs as $key => $val){
495 if (is_array($val) && count($val) == 0){
496 continue;
497 }
498 $attrs[$key]= $val;
499 }
500 $this->attrs= $attrs;
501 }
503 /* Set ntpServers */
504 $this->attrs['gotoNtpServer'] = array();
505 if(!$this->inheritTimeServer){
506 foreach($this->gotoNtpServer as $server){
507 $this->attrs['gotoNtpServer'][] = $server;
508 }
509 }
511 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
512 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
513 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
514 }
516 /* Write back to ldap */
517 $ldap= $this->config->get_ldap_link();
518 if ($this->orig_dn == 'new'){
519 $ldap->cd($this->config->current['BASE']);
520 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
521 $ldap->cd($this->dn);
522 if (!count($this->attrs['gotoNtpServer'])){
523 unset($this->attrs['gotoNtpServer']);
524 }
525 $ldap->add($this->attrs);
526 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
527 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
528 } else {
529 $ldap->cd($this->dn);
530 $this->cleanup();
531 $ldap->modify ($this->attrs);
532 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
533 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
534 }
536 /* cn=default and macAddress=- indicates that this is a template */
537 if($this->cn == "default"){
538 $this->netConfigDNS->macAddress = "-";
539 }
541 $this->netConfigDNS->cn = $this->cn;
542 $this->netConfigDNS->save();
543 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
545 /* Send installation activation
546 */
547 if ($activate){
548 $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
549 $o_queue = new gosaSupportDaemon();
550 if(isset($events['BY_CLASS']['DaemonEvent_installation_activation'])){
551 $evt = $events['BY_CLASS']['DaemonEvent_installation_activation'];
552 $tmp = new $evt['CLASS_NAME']($this->config);
553 $tmp->set_type(TRIGGERED_EVENT);
554 $tmp->add_targets(array($this->netConfigDNS->macAddress));
555 if(!$o_queue->append($tmp)){
556 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
557 $o_queue->get_error()),ERROR_DIALOG);
558 }
559 }
560 }
561 }
564 /* Display generic part for server copy & paste */
565 function getCopyDialog()
566 {
567 $vars = array("cn");
568 $smarty = get_smarty();
569 $smarty->assign("cn" ,$this->cn);
570 $smarty->assign("object","terminal");
571 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
572 $ret = array();
573 $ret['string'] = $str;
574 $ret['status'] = "";
575 return($ret);
576 }
579 function saveCopyDialog()
580 {
581 if(isset($_POST['cn'])){
582 $this->cn = $_POST['cn'];
583 }
584 }
587 function PrepareForCopyPaste($source)
588 {
589 plugin::PrepareForCopyPaste($source);
590 if(isset($source['macAddress'][0])){
591 $this->netConfigDNS->macAddress = $source['macAddress'][0];
592 }
593 if(isset($source['ipHostNumber'][0])){
594 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
595 }
597 /* Create used ntp server array */
598 $this->gotoNtpServer= array();
599 if(isset($source['gotoNtpServer'])){
600 $this->inheritTimeServer = false;
601 unset($source['gotoNtpServer']['count']);
602 foreach($source['gotoNtpServer'] as $server){
603 $this->gotoNtpServer[$server] = $server;
604 }
605 }
607 /* Set inherit checkbox state */
608 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
609 $this->inheritTimeServer = true;
610 $this->gotoNtpServer=array();
611 }
612 }
615 /* Return plugin informations for acl handling */
616 static function plInfo()
617 {
618 return (array(
619 "plShortName" => _("Terminal"),
620 "plDescription" => _("Terminal generic"),
621 "plSelfModify" => FALSE,
622 "plDepends" => array(),
623 "plPriority" => 1,
624 "plSection" => array("administration"),
625 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
626 "objectClass" => "gotoTerminal")),
627 "plProvidedAcls"=> array(
628 "gotoMode" => _("Mode"),
629 "gotoTerminalPath" => _("Root server"),
630 "gotoSwapServer" => _("Swap server"),
631 "gotoSyslogServer" => _("Syslog server enabled"),
632 "gotoNtpServer" => _("Ntp server settings"),
633 "base" => _("Base"),
634 "cn" => _("Name"),
635 "description" => _("Description"),
636 "gotoRootPasswd" => _("Root password"),
637 "FAIstate" => _("Action flag"))
638 ));
639 }
642 function set_everything_to_inherited()
643 {
644 $this->gotoTerminalPath = "default";
645 $this->gotoSwapServer = "default" ;
646 $this->gotoSyslogServer = "default";
647 $this->inheritTimeServer = TRUE;
649 /* Set workstation service attributes to inherited */
650 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
651 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
652 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
653 $this->parent->by_object['termservice']->$name = "default";
654 }
655 }
657 /* Set workstation startup attributes to inherited */
658 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
659 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
660 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
661 }
662 }
663 }
665 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
666 ?>