1 <?php
3 class termgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage terminal base objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* Generic terminal attributes */
11 var $gotoMode= "locked";
12 var $gotoTerminalPath= "";
13 var $gotoSwapServer= "";
14 var $gotoSyslogServer= "";
15 var $gotoSyslogServers = array();
16 var $gotoNtpServer= array();
17 var $gotoNtpServers= array();
18 var $gotoSndModule= "";
19 var $gotoFloppyEnable= "";
20 var $gotoCdromEnable= "";
21 var $ghCpuType= "-";
22 var $ghMemSize= "-";
23 var $ghUsbSupport= "-";
24 var $ghNetNic= array();
25 var $ghIdeDev= array();
26 var $ghScsiDev= array();
27 var $ghGfxAdapter= "-";
28 var $ghSoundAdapter= "-";
29 var $gotoLastUser= "-";
30 var $netConfigDNS;
32 /* Needed values and lists */
33 var $base= "";
34 var $cn= "";
35 var $description= "";
36 var $orig_dn= "";
38 var $inheritTimeServer = true;
40 /* Plugin side filled */
41 var $modes= array();
43 /* attribute list for save action */
44 var $ignore_account= TRUE;
45 var $attributes= array("gotoMode", "gotoTerminalPath",
46 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
47 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
48 "ghCpuType", "ghMemSize","ghUsbSupport", "description",
49 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
50 var $objectclasses= array("top", "gotoTerminal", "GOhard");
52 var $mapActions = array("reboot" => "",
53 "rescan" => "",
54 "wake" => "",
55 "memcheck" => "memcheck",
56 "sysinfo" => "sysinfo");
58 var $fai_activated = FALSE;
59 var $view_logged = FALSE;
61 var $member_of_ogroup = FALSE;
63 function termgeneric (&$config, $dn= NULL, $parent= NULL)
64 {
65 /* Check if FAI is activated */
66 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
67 if(!empty($tmp)){
68 $this->fai_activated = TRUE;
69 }
71 plugin::plugin ($config, $dn, $parent);
73 if(!isset($this->parent->by_object['ogroup'])){
74 $ldap = $this->config->get_ldap_link();
75 $ldap->cd ($this->config->current['BASE']);
76 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
77 $this->member_of_ogroup = $ldap->count() >= 1;
78 }
80 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
81 /* Read arrays */
82 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
83 if (!isset($this->attrs[$val])){
84 continue;
85 }
86 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
87 array_push($this->$val, $this->attrs[$val][$i]);
88 }
89 }
91 /* Create used ntp server array */
92 $this->gotoNtpServer= array();
93 if(isset($this->attrs['gotoNtpServer'])){
94 $this->inheritTimeServer = false;
95 unset($this->attrs['gotoNtpServer']['count']);
96 foreach($this->attrs['gotoNtpServer'] as $server){
97 $this->gotoNtpServer[$server] = $server;
98 }
99 }
101 /* Set inherit checkbox state */
102 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
103 $this->inheritTimeServer = true;
104 $this->gotoNtpServer=array();
105 }
107 /* You can't inherit the NTP service, if we are not member in an object group */
108 if(!$this->member_of_ogroup){
109 $this->inheritTimeServer = FALSE;
110 }
112 /* Create available ntp options */
113 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
114 foreach($this->gotoNtpServers as $key => $server){
115 if($server == "default"){
116 unset($this->gotoNtpServers[$key]);
117 }
118 }
120 $this->modes["locked"]= _("Locked");
121 $this->modes["active"]= _("Activated");
123 /* Set base */
124 if ($this->dn == "new"){
125 $ui= get_userinfo();
126 $this->base= dn2base($ui->dn);
127 } else {
128 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
129 }
131 /* Create an array of all Syslog servers */
132 $tmp = $this->config->data['SERVERS']['SYSLOG'];
133 foreach($tmp as $server){
134 $visible = $server;
135 if($server == "default" && $this->member_of_ogroup) {
136 $visible = "["._("inherited")."]";
137 }
138 $this->gotoSyslogServers[$server] = $visible;
139 }
141 $this->orig_dn= $this->dn;
142 }
144 function set_acl_base($base)
145 {
146 plugin::set_acl_base($base);
147 $this->netConfigDNS->set_acl_base($base);
148 }
150 function set_acl_category($cat)
151 {
152 plugin::set_acl_category($cat);
153 $this->netConfigDNS->set_acl_category($cat);
154 }
156 function execute()
157 {
158 /* Call parent execute */
159 plugin::execute();
161 if($this->is_account && !$this->view_logged){
162 $this->view_logged = TRUE;
163 new log("view","terminal/".get_class($this),$this->dn);
164 }
166 /* Do we need to flip is_account state? */
167 if (isset($_POST['modify_state'])){
168 $this->is_account= !$this->is_account;
169 }
171 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->mapActions[$_POST['saction']])){
175 /* Check given action */
176 $mapEvent = array("wake" => "wakeup",
177 "instant_update" => "update");
178 $action = $_POST['saction'];
179 if(isset($mapEvent[$action])){
180 $action = $mapEvent[$action];
181 }
183 /* Check if we have an DaemonEvent for this action */
184 if(class_available("DaemonEvent_".$action)){
185 $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
186 if(isset($events['BY_CLASS']["DaemonEvent_".$action])){
187 $evt = $events['BY_CLASS']["DaemonEvent_".$action];
188 $tmp = new $evt['CLASS_NAME']($this->config);
189 $tmp->add_targets(array($this->netConfigDNS->macAddress));
190 $tmp->set_type(TRIGGERED_EVENT);
191 $o_queue = new gosaSupportDaemon();
192 if(!$o_queue->append($tmp)){
193 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
194 $o_queue->get_error()),ERROR_DIALOG);
195 }
196 }
197 }else{
199 msg_dialog::display(_("Missing Daemon Event"),
200 sprintf(_("The requested action does not exists '%s' Sending event manually."),"DaemonEvent_".$action),ERROR_DIALOG);
202 /* No event found, send action manually.
203 */
204 $method= "gosa";
205 $action= $_POST['saction'];
206 if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
207 $method= "job";
208 }
210 gosaSupportDaemon::send("${method}_trigger_action_".$action,
211 $this->netConfigDNS->macAddress,
212 array("macAddress" => $this->netConfigDNS->macAddress));
213 }
214 }
216 /* Base select dialog */
217 $once = true;
218 foreach($_POST as $name => $value){
219 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
220 $once = false;
221 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
222 $this->dialog->setCurrentBase($this->base);
223 }
224 }
226 /* Dialog handling */
227 if(is_object($this->dialog)){
228 /* Must be called before save_object */
229 $this->dialog->save_object();
231 if($this->dialog->isClosed()){
232 $this->dialog = false;
233 }elseif($this->dialog->isSelected()){
235 /* A new base was selected, check if it is a valid one */
236 $tmp = $this->get_allowed_bases();
237 if(isset($tmp[$this->dialog->isSelected()])){
238 $this->base = $this->dialog->isSelected();
239 }
240 $this->dialog= false;
241 }else{
242 return($this->dialog->execute());
243 }
244 }
246 /* Do we represent a valid terminal? */
247 if (!$this->is_account && $this->parent === NULL){
248 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
249 _("This 'dn' has no terminal features.")."</b>";
250 return($display);
251 }
253 /* Add new ntp Server to our list */
254 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
255 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
256 }
258 /* Delete selected NtpServer for list of used servers */
259 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
260 foreach($_POST['gotoNtpServerSelected'] as $name){
261 unset($this->gotoNtpServer[$name]);
262 }
263 }
265 /* Fill templating stuff */
266 $smarty= get_smarty();
268 $tmp = $this->plInfo();
269 foreach($tmp['plProvidedAcls'] as $name => $translation){
270 $smarty->assign($name."ACL",$this->getacl($name));
271 }
273 $smarty->assign("cn", $this->cn);
274 $smarty->assign("description", $this->description);
275 $smarty->assign("staticAddress", "");
277 $smarty->assign("bases", $this->get_allowed_bases());
279 /* tell smarty the inherit checkbox state */
280 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
282 /* Check if terminal is online */
283 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
284 $smarty->assign("actions", array( "halt" => _("Switch off"),
285 "reboot" => _("Reboot"),
286 "memcheck" => _("Memory test"),
287 "sysinfo" => _("System analysis")));
288 } else {
289 $smarty->assign("actions", array("wake" => _("Wake up"),
290 "memcheck" => _("Memory test"),
291 "sysinfo" => _("System analysis")));
292 }
294 /* Arrays */
295 $smarty->assign("modes", $this->modes);
297 $tmp2 = array();
298 foreach($this->config->data['SERVERS']['NFS'] as $server){
299 if($server != "default"){
300 $tmp2[$server]= $server;
301 }else{
302 if($this->member_of_ogroup){
303 $tmp2[$server]="["._("inherited")."]";
304 }
305 }
306 }
308 $smarty->assign("nfsservers", $tmp2);
309 $smarty->assign("syslogservers", $this->gotoSyslogServers);
311 $tmp = array();
312 foreach($this->gotoNtpServers as $server){
313 if(!in_array($server,$this->gotoNtpServer)){
314 $tmp[$server] = $server;
315 }
316 }
318 $smarty->assign("ntpservers", $tmp);
319 $smarty->assign("fai_activated",$this->fai_activated);
321 /* Variables */
322 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
323 $smarty->assign($val."_select", $this->$val);
324 }
326 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
328 /* Show main page */
329 $str = $this->netConfigDNS->execute();
330 if(is_object($this->netConfigDNS->dialog)){
331 return($str);
332 }
333 $smarty->assign("netconfig", $str);
334 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
335 }
337 function remove_from_parent()
338 {
339 if($this->acl_is_removeable()){
340 $ldap= $this->config->get_ldap_link();
341 $ldap->cd($this->dn);
342 $ldap->cat($this->dn, array('dn'));
343 if($ldap->count()){
344 $this->netConfigDNS->remove_from_parent();
345 $ldap->rmDir($this->dn);
347 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
349 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
351 /* Optionally execute a command after we're done */
352 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
354 /* Delete references to object groups */
355 $ldap->cd ($this->config->current['BASE']);
356 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
357 while ($ldap->fetch()){
358 $og= new ogroup($this->config, $ldap->getDN());
359 unset($og->member[$this->dn]);
360 $og->save ();
361 }
362 }
363 }
364 }
367 /* Save data to object */
368 function save_object()
369 {
370 /* Create a base backup and reset the
371 base directly after calling plugin::save_object();
372 Base will be set seperatly a few lines below */
373 $base_tmp = $this->base;
374 plugin::save_object();
375 $this->base = $base_tmp;
377 /* Set new base if allowed */
378 $tmp = $this->get_allowed_bases();
379 if(isset($_POST['base'])){
380 if(isset($tmp[$_POST['base']])){
381 $this->base= $_POST['base'];
382 }
383 }
385 $this->netConfigDNS->save_object();
387 /* Save terminal path to parent since it is used by termstartup, too */
388 if(isset($this->parent->by_object['termstartup'])){
389 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
390 }
392 if(isset($_POST['termgeneric_posted'])){
393 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
394 $this->inheritTimeServer = true;
395 }else{
396 $this->inheritTimeServer = false;
397 }
398 }
400 if(isset($_POST["inheritAll"])){
401 $this->set_everything_to_inherited();
402 }
404 }
407 /* Check supplied data */
408 function check()
409 {
410 /* Call common method to give check the hook */
411 $message= plugin::check();
413 /* Skip IP & Mac checks if this is a template */
414 if($this->cn != "default"){
415 $message= array_merge($message, $this->netConfigDNS->check());
416 }
418 /* Permissions for that base? */
419 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
421 if ($this->cn == ""){
422 $message[]= _("The required field 'Terminal name' is not set.");
423 }
425 /* Check if given name is a valid host/dns name */
426 if(!tests::is_dns_name($this->cn) ){
427 $message[] = _("Please specify a valid name for this object.");
428 }
430 if ($this->orig_dn == 'new'){
431 $ldap= $this->config->get_ldap_link();
432 $ldap->cd ($this->base);
434 /* It is possible to have a 'default' terminal on every base */
435 if($this->cn == "default"){
436 $ldap->cat($this->dn);
437 }else{
438 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
439 }
440 if ($ldap->count() != 0){
441 while ($attrs= $ldap->fetch()){
442 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
443 continue;
444 } else {
445 if ($attrs['dn'] != $this->orig_dn){
446 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
447 break;
448 }
449 }
450 }
451 }
452 }
454 /* Check for valid ntpServer selection */
455 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
456 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
457 }
459 return ($message);
460 }
463 /* Save to LDAP */
464 function save()
465 {
467 /* Move object if requested */
468 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
469 $this->move($this->orig_dn, $this->dn);
470 }
472 /* Detect mode changes */
473 $activate= (isset($this->saved_attributes['gotoMode']) &&
474 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
475 $this->gotoMode == "enabled" &&
476 tests::is_ip($this->netConfigDNS->ipHostNumber));
478 plugin::save();
480 /* Strip out 'default' values */
481 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
482 if(isset($this->attrs[$val])){
483 if ($this->attrs[$val] == "default"){
484 $this->attrs[$val]= array();
485 }
486 }
487 }
489 /* Add missing arrays */
490 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
491 if (isset ($this->$val) && count ($this->$val) != 0){
492 $this->attrs["$val"]= $this->$val;
493 }
494 }
496 /* Remove all empty values */
497 if ($this->orig_dn == 'new'){
498 $attrs= array();
499 foreach ($this->attrs as $key => $val){
500 if (is_array($val) && count($val) == 0){
501 continue;
502 }
503 $attrs[$key]= $val;
504 }
505 $this->attrs= $attrs;
506 }
508 /* Set ntpServers */
509 $this->attrs['gotoNtpServer'] = array();
510 if(!$this->inheritTimeServer){
511 foreach($this->gotoNtpServer as $server){
512 $this->attrs['gotoNtpServer'][] = $server;
513 }
514 }
516 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
517 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
518 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
519 }
521 /* Write back to ldap */
522 $ldap= $this->config->get_ldap_link();
523 if ($this->orig_dn == 'new'){
524 $ldap->cd($this->config->current['BASE']);
525 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
526 $ldap->cd($this->dn);
527 if (!count($this->attrs['gotoNtpServer'])){
528 unset($this->attrs['gotoNtpServer']);
529 }
530 $ldap->add($this->attrs);
531 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
532 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
533 } else {
534 $ldap->cd($this->dn);
535 $this->cleanup();
536 $ldap->modify ($this->attrs);
537 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
538 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
539 }
541 /* cn=default and macAddress=- indicates that this is a template */
542 if($this->cn == "default"){
543 $this->netConfigDNS->macAddress = "-";
544 }
546 $this->netConfigDNS->cn = $this->cn;
547 $this->netConfigDNS->save();
548 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
550 /* Send installation activation
551 */
552 if ($activate){
553 $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
554 $o_queue = new gosaSupportDaemon();
555 if(isset($events['BY_CLASS']['DaemonEvent_installation_activation'])){
556 $evt = $events['BY_CLASS']['DaemonEvent_installation_activation'];
557 $tmp = new $evt['CLASS_NAME']($this->config);
558 $tmp->set_type(TRIGGERED_EVENT);
559 $tmp->add_targets(array($this->netConfigDNS->macAddress));
560 if(!$o_queue->append($tmp)){
561 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
562 $o_queue->get_error()),ERROR_DIALOG);
563 }
564 }
565 }
566 }
569 /* Display generic part for server copy & paste */
570 function getCopyDialog()
571 {
572 $vars = array("cn");
573 $smarty = get_smarty();
574 $smarty->assign("cn" ,$this->cn);
575 $smarty->assign("object","terminal");
576 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
577 $ret = array();
578 $ret['string'] = $str;
579 $ret['status'] = "";
580 return($ret);
581 }
584 function saveCopyDialog()
585 {
586 if(isset($_POST['cn'])){
587 $this->cn = $_POST['cn'];
588 }
589 }
592 function PrepareForCopyPaste($source)
593 {
594 plugin::PrepareForCopyPaste($source);
595 if(isset($source['macAddress'][0])){
596 $this->netConfigDNS->macAddress = $source['macAddress'][0];
597 }
598 if(isset($source['ipHostNumber'][0])){
599 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
600 }
602 /* Create used ntp server array */
603 $this->gotoNtpServer= array();
604 if(isset($source['gotoNtpServer'])){
605 $this->inheritTimeServer = false;
606 unset($source['gotoNtpServer']['count']);
607 foreach($source['gotoNtpServer'] as $server){
608 $this->gotoNtpServer[$server] = $server;
609 }
610 }
612 /* Set inherit checkbox state */
613 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
614 $this->inheritTimeServer = true;
615 $this->gotoNtpServer=array();
616 }
617 }
620 /* Return plugin informations for acl handling */
621 static function plInfo()
622 {
623 return (array(
624 "plShortName" => _("Terminal"),
625 "plDescription" => _("Terminal generic"),
626 "plSelfModify" => FALSE,
627 "plDepends" => array(),
628 "plPriority" => 1,
629 "plSection" => array("administration"),
630 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
631 "objectClass" => "gotoTerminal")),
632 "plProvidedAcls"=> array(
633 "gotoMode" => _("Mode"),
634 "gotoTerminalPath" => _("Root server"),
635 "gotoSwapServer" => _("Swap server"),
636 "gotoSyslogServer" => _("Syslog server enabled"),
637 "gotoNtpServer" => _("Ntp server settings"),
638 "base" => _("Base"),
639 "cn" => _("Name"),
640 "description" => _("Description"),
641 "gotoRootPasswd" => _("Root password"),
642 "FAIstate" => _("Action flag"))
643 ));
644 }
647 function set_everything_to_inherited()
648 {
649 $this->gotoTerminalPath = "default";
650 $this->gotoSwapServer = "default" ;
651 $this->gotoSyslogServer = "default";
652 $this->inheritTimeServer = TRUE;
654 /* Set workstation service attributes to inherited */
655 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
656 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
657 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
658 $this->parent->by_object['termservice']->$name = "default";
659 }
660 }
662 /* Set workstation startup attributes to inherited */
663 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
664 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
665 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
666 }
667 }
668 }
670 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
671 ?>