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 $validActions = array("reboot" => "", "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
49 var $fai_activated = FALSE;
50 var $view_logged = FALSE;
52 var $member_of_ogroup = FALSE;
54 function termgeneric (&$config, $dn= NULL, $parent= NULL)
55 {
56 /* Check if FAI is activated */
57 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
58 if(!empty($tmp)){
59 $this->fai_activated = TRUE;
60 }
62 plugin::plugin ($config, $dn, $parent);
64 if(!isset($this->parent->by_object['ogroup'])){
65 $ldap = $this->config->get_ldap_link();
66 $ldap->cd ($this->config->current['BASE']);
67 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
68 $this->member_of_ogroup = $ldap->count() >= 1;
69 }
71 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
72 /* Read arrays */
73 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
74 if (!isset($this->attrs[$val])){
75 continue;
76 }
77 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
78 array_push($this->$val, $this->attrs[$val][$i]);
79 }
80 }
82 /* Create used ntp server array */
83 $this->gotoNtpServer= array();
84 if(isset($this->attrs['gotoNtpServer'])){
85 $this->inheritTimeServer = false;
86 unset($this->attrs['gotoNtpServer']['count']);
87 foreach($this->attrs['gotoNtpServer'] as $server){
88 $this->gotoNtpServer[$server] = $server;
89 }
90 }
92 /* Set inherit checkbox state */
93 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
94 $this->inheritTimeServer = true;
95 $this->gotoNtpServer=array();
96 }
98 /* You can't inherit the NTP service, if we are not member in an object group */
99 if(!$this->member_of_ogroup){
100 $this->inheritTimeServer = FALSE;
101 }
103 /* Create available ntp options */
104 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
105 foreach($this->gotoNtpServers as $key => $server){
106 if($server == "default"){
107 unset($this->gotoNtpServers[$key]);
108 }
109 }
111 $this->modes["locked"]= _("Locked");
112 $this->modes["active"]= _("Activated");
114 /* Set base */
115 if ($this->dn == "new"){
116 $ui= get_userinfo();
117 $this->base= dn2base($ui->dn);
118 } else {
119 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
120 }
122 /* Create an array of all Syslog servers */
123 $tmp = $this->config->data['SERVERS']['SYSLOG'];
124 foreach($tmp as $server){
125 $visible = $server;
126 if($server == "default" && $this->member_of_ogroup) {
127 $visible = "["._("inherited")."]";
128 }
129 $this->gotoSyslogServers[$server] = $visible;
130 }
132 $this->orig_dn= $this->dn;
133 }
135 function set_acl_base($base)
136 {
137 plugin::set_acl_base($base);
138 $this->netConfigDNS->set_acl_base($base);
139 }
141 function set_acl_category($cat)
142 {
143 plugin::set_acl_category($cat);
144 $this->netConfigDNS->set_acl_category($cat);
145 }
147 function execute()
148 {
149 /* Call parent execute */
150 plugin::execute();
152 if($this->is_account && !$this->view_logged){
153 $this->view_logged = TRUE;
154 new log("view","terminal/".get_class($this),$this->dn);
155 }
157 /* Do we need to flip is_account state? */
158 if (isset($_POST['modify_state'])){
159 $this->is_account= !$this->is_account;
160 }
162 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->validActions[$_POST['saction']])){
163 $action = $_POST['saction'];
165 /* Check if we have an DaemonEvent for this action */
166 if(class_available("DaemonEvent_".$action)){
167 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
168 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
169 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
170 $tmp = new $evt['CLASS_NAME']($this->config);
171 $tmp->add_targets(array($this->netConfigDNS->macAddress));
172 $tmp->set_type(TRIGGERED_EVENT);
173 $o_queue = new gosaSupportDaemon();
174 if(!$o_queue->append($tmp)){
175 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
176 $o_queue->get_error()),ERROR_DIALOG);
177 }
178 }
179 }else{
180 msg_dialog::display(_("Event error"),
181 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
182 }
184 }
186 /* Base select dialog */
187 $once = true;
188 foreach($_POST as $name => $value){
189 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
190 $once = false;
191 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
192 $this->dialog->setCurrentBase($this->base);
193 }
194 }
196 /* Dialog handling */
197 if(is_object($this->dialog)){
198 /* Must be called before save_object */
199 $this->dialog->save_object();
201 if($this->dialog->isClosed()){
202 $this->dialog = false;
203 }elseif($this->dialog->isSelected()){
205 /* A new base was selected, check if it is a valid one */
206 $tmp = $this->get_allowed_bases();
207 if(isset($tmp[$this->dialog->isSelected()])){
208 $this->base = $this->dialog->isSelected();
209 }
210 $this->dialog= false;
211 }else{
212 return($this->dialog->execute());
213 }
214 }
216 /* Do we represent a valid terminal? */
217 if (!$this->is_account && $this->parent === NULL){
218 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
219 _("This 'dn' has no terminal features.")."</b>";
220 return($display);
221 }
223 /* Add new ntp Server to our list */
224 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
225 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
226 }
228 /* Delete selected NtpServer for list of used servers */
229 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
230 foreach($_POST['gotoNtpServerSelected'] as $name){
231 unset($this->gotoNtpServer[$name]);
232 }
233 }
235 /* Fill templating stuff */
236 $smarty= get_smarty();
238 $tmp = $this->plInfo();
239 foreach($tmp['plProvidedAcls'] as $name => $translation){
240 $smarty->assign($name."ACL",$this->getacl($name));
241 }
243 $smarty->assign("cn", $this->cn);
244 $smarty->assign("description", $this->description);
245 $smarty->assign("staticAddress", "");
247 $smarty->assign("bases", $this->get_allowed_bases());
249 /* tell smarty the inherit checkbox state */
250 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
252 /* Check if terminal is online */
253 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
254 $smarty->assign("actions", array( "halt" => _("Switch off"),
255 "reboot" => _("Reboot"),
256 "memcheck" => _("Memory test"),
257 "sysinfo" => _("System analysis")));
258 } else {
259 $smarty->assign("actions", array("wake" => _("Wake up"),
260 "memcheck" => _("Memory test"),
261 "sysinfo" => _("System analysis")));
262 }
264 /* Arrays */
265 $smarty->assign("modes", $this->modes);
267 $tmp2 = array();
268 foreach($this->config->data['SERVERS']['NFS'] as $server){
269 if($server != "default"){
270 $tmp2[$server]= $server;
271 }else{
272 if($this->member_of_ogroup){
273 $tmp2[$server]="["._("inherited")."]";
274 }
275 }
276 }
278 $smarty->assign("nfsservers", $tmp2);
279 $smarty->assign("syslogservers", $this->gotoSyslogServers);
281 $tmp = array();
282 foreach($this->gotoNtpServers as $server){
283 if(!in_array($server,$this->gotoNtpServer)){
284 $tmp[$server] = $server;
285 }
286 }
288 $smarty->assign("ntpservers", $tmp);
289 $smarty->assign("fai_activated",$this->fai_activated);
291 /* Variables */
292 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
293 $smarty->assign($val."_select", $this->$val);
294 }
296 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
298 /* Show main page */
299 $str = $this->netConfigDNS->execute();
300 if(is_object($this->netConfigDNS->dialog)){
301 return($str);
302 }
303 $smarty->assign("netconfig", $str);
304 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
305 }
307 function remove_from_parent()
308 {
309 if($this->acl_is_removeable()){
310 $ldap= $this->config->get_ldap_link();
311 $ldap->cd($this->dn);
312 $ldap->cat($this->dn, array('dn'));
313 if($ldap->count()){
314 $this->netConfigDNS->remove_from_parent();
315 $ldap->rmDir($this->dn);
317 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
319 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
321 /* Optionally execute a command after we're done */
322 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
324 /* Delete references to object groups */
325 $ldap->cd ($this->config->current['BASE']);
326 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
327 while ($ldap->fetch()){
328 $og= new ogroup($this->config, $ldap->getDN());
329 unset($og->member[$this->dn]);
330 $og->save ();
331 }
332 }
334 /* Clean queue form entries with this mac
335 */
336 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
337 $q = new gosaSupportDaemon();
338 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
339 }
340 }
341 }
344 /* Save data to object */
345 function save_object()
346 {
347 /* Create a base backup and reset the
348 base directly after calling plugin::save_object();
349 Base will be set seperatly a few lines below */
350 $base_tmp = $this->base;
351 plugin::save_object();
352 $this->base = $base_tmp;
354 /* Set new base if allowed */
355 $tmp = $this->get_allowed_bases();
356 if(isset($_POST['base'])){
357 if(isset($tmp[$_POST['base']])){
358 $this->base= $_POST['base'];
359 }
360 }
362 $this->netConfigDNS->save_object();
364 /* Save terminal path to parent since it is used by termstartup, too */
365 if(isset($this->parent->by_object['termstartup'])){
366 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
367 }
369 if(isset($_POST['termgeneric_posted'])){
370 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
371 $this->inheritTimeServer = true;
372 }else{
373 $this->inheritTimeServer = false;
374 }
375 }
377 if(isset($_POST["inheritAll"])){
378 $this->set_everything_to_inherited();
379 }
381 }
384 /* Check supplied data */
385 function check()
386 {
387 /* Call common method to give check the hook */
388 $message= plugin::check();
390 /* Skip IP & Mac checks if this is a template */
391 if($this->cn != "default"){
392 $message= array_merge($message, $this->netConfigDNS->check());
393 }
395 /* Permissions for that base? */
396 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
398 if ($this->cn == ""){
399 $message[]= _("The required field 'Terminal name' is not set.");
400 }
402 /* Check if given name is a valid host/dns name */
403 if(!tests::is_dns_name($this->cn) ){
404 $message[] = _("Please specify a valid name for this object.");
405 }
407 if ($this->orig_dn == 'new'){
408 $ldap= $this->config->get_ldap_link();
409 $ldap->cd ($this->base);
411 /* It is possible to have a 'default' terminal on every base */
412 if($this->cn == "default"){
413 $ldap->cat($this->dn);
414 }else{
415 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
416 }
417 if ($ldap->count() != 0){
418 while ($attrs= $ldap->fetch()){
419 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
420 continue;
421 } else {
422 if ($attrs['dn'] != $this->orig_dn){
423 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
424 break;
425 }
426 }
427 }
428 }
429 }
431 /* Check for valid ntpServer selection */
432 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
433 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
434 }
436 return ($message);
437 }
440 /* Save to LDAP */
441 function save()
442 {
444 /* Move object if requested */
445 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
446 $this->move($this->orig_dn, $this->dn);
447 }
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));
511 }
513 /* cn=default and macAddress=- indicates that this is a template */
514 if($this->cn == "default"){
515 $this->netConfigDNS->macAddress = "-";
516 }
518 $this->netConfigDNS->cn = $this->cn;
519 $this->netConfigDNS->save();
520 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
522 /* Send installation activation
523 */
524 if ($activate && class_available("DaemonEvent")){
525 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
526 $o_queue = new gosaSupportDaemon();
527 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
528 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
529 $tmp = new $evt['CLASS_NAME']($this->config);
530 $tmp->set_type(TRIGGERED_EVENT);
531 $tmp->add_targets(array($this->netConfigDNS->macAddress));
532 if(!$o_queue->append($tmp)){
533 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
534 $o_queue->get_error()),ERROR_DIALOG);
535 }
536 }
537 }
538 }
541 /* Display generic part for server copy & paste */
542 function getCopyDialog()
543 {
544 $vars = array("cn");
545 $smarty = get_smarty();
546 $smarty->assign("cn" ,$this->cn);
547 $smarty->assign("object","terminal");
548 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
549 $ret = array();
550 $ret['string'] = $str;
551 $ret['status'] = "";
552 return($ret);
553 }
556 function saveCopyDialog()
557 {
558 if(isset($_POST['cn'])){
559 $this->cn = $_POST['cn'];
560 }
561 }
564 function PrepareForCopyPaste($source)
565 {
566 plugin::PrepareForCopyPaste($source);
567 if(isset($source['macAddress'][0])){
568 $this->netConfigDNS->macAddress = $source['macAddress'][0];
569 }
570 if(isset($source['ipHostNumber'][0])){
571 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
572 }
574 /* Create used ntp server array */
575 $this->gotoNtpServer= array();
576 if(isset($source['gotoNtpServer'])){
577 $this->inheritTimeServer = false;
578 unset($source['gotoNtpServer']['count']);
579 foreach($source['gotoNtpServer'] as $server){
580 $this->gotoNtpServer[$server] = $server;
581 }
582 }
584 /* Set inherit checkbox state */
585 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
586 $this->inheritTimeServer = true;
587 $this->gotoNtpServer=array();
588 }
589 }
592 /* Return plugin informations for acl handling */
593 static function plInfo()
594 {
595 return (array(
596 "plShortName" => _("Terminal"),
597 "plDescription" => _("Terminal generic"),
598 "plSelfModify" => FALSE,
599 "plDepends" => array(),
600 "plPriority" => 1,
601 "plSection" => array("administration"),
602 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
603 "objectClass" => "gotoTerminal")),
604 "plProvidedAcls"=> array(
605 "gotoMode" => _("Mode"),
606 "gotoTerminalPath" => _("Root server"),
607 "gotoSwapServer" => _("Swap server"),
608 "gotoSyslogServer" => _("Syslog server enabled"),
609 "gotoNtpServer" => _("Ntp server settings"),
610 "base" => _("Base"),
611 "cn" => _("Name"),
612 "description" => _("Description"),
613 "gotoRootPasswd" => _("Root password"),
614 "FAIstate" => _("Action flag"))
615 ));
616 }
619 function set_everything_to_inherited()
620 {
621 $this->gotoTerminalPath = "default";
622 $this->gotoSwapServer = "default" ;
623 $this->gotoSyslogServer = "default";
624 $this->inheritTimeServer = TRUE;
626 /* Set workstation service attributes to inherited */
627 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
628 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
629 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
630 $this->parent->by_object['termservice']->$name = "default";
631 }
632 }
634 /* Set workstation startup attributes to inherited */
635 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
636 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
637 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
638 }
639 }
640 }
642 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
643 ?>