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(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
176 }
177 }
178 }else{
179 msg_dialog::display(_("Event error"),
180 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
181 }
183 }
185 /* Base select dialog */
186 $once = true;
187 foreach($_POST as $name => $value){
188 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
189 $once = false;
190 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
191 $this->dialog->setCurrentBase($this->base);
192 }
193 }
195 /* Dialog handling */
196 if(is_object($this->dialog)){
197 /* Must be called before save_object */
198 $this->dialog->save_object();
200 if($this->dialog->isClosed()){
201 $this->dialog = false;
202 }elseif($this->dialog->isSelected()){
204 /* A new base was selected, check if it is a valid one */
205 $tmp = $this->get_allowed_bases();
206 if(isset($tmp[$this->dialog->isSelected()])){
207 $this->base = $this->dialog->isSelected();
208 }
209 $this->dialog= false;
210 }else{
211 return($this->dialog->execute());
212 }
213 }
215 /* Do we represent a valid terminal? */
216 if (!$this->is_account && $this->parent === NULL){
217 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
218 msgPool::noValidExtension(_("terminal"))."</b>";
219 return($display);
220 }
222 /* Add new ntp Server to our list */
223 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
224 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
225 }
227 /* Delete selected NtpServer for list of used servers */
228 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
229 foreach($_POST['gotoNtpServerSelected'] as $name){
230 unset($this->gotoNtpServer[$name]);
231 }
232 }
234 /* Fill templating stuff */
235 $smarty= get_smarty();
237 $tmp = $this->plInfo();
238 foreach($tmp['plProvidedAcls'] as $name => $translation){
239 $smarty->assign($name."ACL",$this->getacl($name));
240 }
242 $smarty->assign("cn", $this->cn);
243 $smarty->assign("description", $this->description);
244 $smarty->assign("staticAddress", "");
246 $smarty->assign("bases", $this->get_allowed_bases());
248 /* tell smarty the inherit checkbox state */
249 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
251 /* Check if terminal is online */
252 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
253 $smarty->assign("actions", array( "halt" => _("Switch off"),
254 "reboot" => _("Reboot"),
255 "memcheck" => _("Memory test"),
256 "sysinfo" => _("System analysis")));
257 } else {
258 $smarty->assign("actions", array("wake" => _("Wake up"),
259 "memcheck" => _("Memory test"),
260 "sysinfo" => _("System analysis")));
261 }
263 /* Arrays */
264 $smarty->assign("modes", $this->modes);
266 $tmp2 = array();
267 foreach($this->config->data['SERVERS']['NFS'] as $server){
268 if($server != "default"){
269 $tmp2[$server]= $server;
270 }else{
271 if($this->member_of_ogroup){
272 $tmp2[$server]="["._("inherited")."]";
273 }
274 }
275 }
277 $smarty->assign("nfsservers", $tmp2);
278 $smarty->assign("syslogservers", $this->gotoSyslogServers);
280 $tmp = array();
281 foreach($this->gotoNtpServers as $server){
282 if(!in_array($server,$this->gotoNtpServer)){
283 $tmp[$server] = $server;
284 }
285 }
287 $smarty->assign("ntpservers", $tmp);
288 $smarty->assign("fai_activated",$this->fai_activated);
290 /* Variables */
291 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
292 $smarty->assign($val."_select", $this->$val);
293 }
295 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
297 /* Show main page */
298 $str = $this->netConfigDNS->execute();
299 if(is_object($this->netConfigDNS->dialog)){
300 return($str);
301 }
302 $smarty->assign("netconfig", $str);
303 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
304 }
306 function remove_from_parent()
307 {
308 if($this->acl_is_removeable()){
309 $ldap= $this->config->get_ldap_link();
310 $ldap->cd($this->dn);
311 $ldap->cat($this->dn, array('dn'));
312 if($ldap->count()){
313 $this->netConfigDNS->remove_from_parent();
314 $ldap->rmDir($this->dn);
316 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
318 if (!$ldap->success()){
319 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
320 }
322 /* Optionally execute a command after we're done */
323 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
325 /* Delete references to object groups */
326 $ldap->cd ($this->config->current['BASE']);
327 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
328 while ($ldap->fetch()){
329 $og= new ogroup($this->config, $ldap->getDN());
330 unset($og->member[$this->dn]);
331 $og->save ();
332 }
333 }
335 /* Clean queue form entries with this mac
336 */
337 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
338 $q = new gosaSupportDaemon();
339 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
340 }
341 }
342 }
345 /* Save data to object */
346 function save_object()
347 {
348 /* Create a base backup and reset the
349 base directly after calling plugin::save_object();
350 Base will be set seperatly a few lines below */
351 $base_tmp = $this->base;
352 plugin::save_object();
353 $this->base = $base_tmp;
355 /* Set new base if allowed */
356 $tmp = $this->get_allowed_bases();
357 if(isset($_POST['base'])){
358 if(isset($tmp[$_POST['base']])){
359 $this->base= $_POST['base'];
360 }
361 }
363 $this->netConfigDNS->save_object();
365 /* Save terminal path to parent since it is used by termstartup, too */
366 if(isset($this->parent->by_object['termstartup'])){
367 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
368 }
370 if(isset($_POST['termgeneric_posted'])){
371 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
372 $this->inheritTimeServer = true;
373 }else{
374 $this->inheritTimeServer = false;
375 }
376 }
378 if(isset($_POST["inheritAll"])){
379 $this->set_everything_to_inherited();
380 }
382 }
385 /* Check supplied data */
386 function check()
387 {
388 /* Call common method to give check the hook */
389 $message= plugin::check();
391 /* Skip IP & Mac checks if this is a template */
392 if($this->cn != "default"){
393 $message= array_merge($message, $this->netConfigDNS->check());
394 }
396 /* Permissions for that base? */
397 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
399 if ($this->cn == ""){
400 $message[]= msgPool::required(_("Name"));
401 }
403 /* Check if given name is a valid host/dns name */
404 if(!tests::is_dns_name($this->cn) ){
405 $message[] = msgPool::invalid(_("Name"));
406 }
408 if ($this->orig_dn == 'new'){
409 $ldap= $this->config->get_ldap_link();
410 $ldap->cd ($this->base);
412 /* It is possible to have a 'default' terminal on every base */
413 if($this->cn == "default"){
414 $ldap->cat($this->dn);
415 }else{
416 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
417 }
418 if ($ldap->count() != 0){
419 while ($attrs= $ldap->fetch()){
420 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
421 continue;
422 } else {
423 if ($attrs['dn'] != $this->orig_dn){
424 $message[]= msgPool::duplicated(_("Name"));
425 break;
426 }
427 }
428 }
429 }
430 }
432 /* Check for valid ntpServer selection */
433 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
434 $message[]= msgPool::required(_("NTP server"));
435 }
437 return ($message);
438 }
441 /* Save to LDAP */
442 function save()
443 {
445 /* Move object if requested */
446 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
447 $this->move($this->orig_dn, $this->dn);
448 }
450 /* Detect mode changes */
451 $activate= (isset($this->saved_attributes['gotoMode']) &&
452 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
453 $this->gotoMode == "enabled" &&
454 tests::is_ip($this->netConfigDNS->ipHostNumber));
456 plugin::save();
458 /* Strip out 'default' values */
459 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
460 if(isset($this->attrs[$val])){
461 if ($this->attrs[$val] == "default"){
462 $this->attrs[$val]= array();
463 }
464 }
465 }
467 /* Add missing arrays */
468 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
469 if (isset ($this->$val) && count ($this->$val) != 0){
470 $this->attrs["$val"]= $this->$val;
471 }
472 }
474 /* Remove all empty values */
475 if ($this->orig_dn == 'new'){
476 $attrs= array();
477 foreach ($this->attrs as $key => $val){
478 if (is_array($val) && count($val) == 0){
479 continue;
480 }
481 $attrs[$key]= $val;
482 }
483 $this->attrs= $attrs;
484 }
486 /* Set ntpServers */
487 $this->attrs['gotoNtpServer'] = array();
488 if(!$this->inheritTimeServer){
489 foreach($this->gotoNtpServer as $server){
490 $this->attrs['gotoNtpServer'][] = $server;
491 }
492 }
494 /* Write back to ldap */
495 $ldap= $this->config->get_ldap_link();
496 if ($this->orig_dn == 'new'){
497 $ldap->cd($this->config->current['BASE']);
498 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
499 $ldap->cd($this->dn);
500 if (!count($this->attrs['gotoNtpServer'])){
501 unset($this->attrs['gotoNtpServer']);
502 }
503 $ldap->add($this->attrs);
504 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
505 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
506 } else {
507 $ldap->cd($this->dn);
508 $this->cleanup();
509 $ldap->modify ($this->attrs);
510 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
511 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
512 }
514 /* cn=default and macAddress=- indicates that this is a template */
515 if($this->cn == "default"){
516 $this->netConfigDNS->macAddress = "-";
517 }
519 $this->netConfigDNS->cn = $this->cn;
520 $this->netConfigDNS->save();
521 if (!$ldap->success()){
522 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
523 }
525 /* Send installation activation
526 */
527 if ($activate && class_available("DaemonEvent")){
528 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
529 $o_queue = new gosaSupportDaemon();
530 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
531 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
532 $tmp = new $evt['CLASS_NAME']($this->config);
533 $tmp->set_type(TRIGGERED_EVENT);
534 $tmp->add_targets(array($this->netConfigDNS->macAddress));
535 if(!$o_queue->append($tmp)){
536 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
537 }
538 }
539 }
540 }
543 /* Display generic part for server copy & paste */
544 function getCopyDialog()
545 {
546 $vars = array("cn");
547 $smarty = get_smarty();
548 $smarty->assign("cn" ,$this->cn);
549 $smarty->assign("object","terminal");
550 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
551 $ret = array();
552 $ret['string'] = $str;
553 $ret['status'] = "";
554 return($ret);
555 }
558 function saveCopyDialog()
559 {
560 if(isset($_POST['cn'])){
561 $this->cn = $_POST['cn'];
562 }
563 }
566 function PrepareForCopyPaste($source)
567 {
568 plugin::PrepareForCopyPaste($source);
569 if(isset($source['macAddress'][0])){
570 $this->netConfigDNS->macAddress = $source['macAddress'][0];
571 }
572 if(isset($source['ipHostNumber'][0])){
573 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
574 }
576 /* Create used ntp server array */
577 $this->gotoNtpServer= array();
578 if(isset($source['gotoNtpServer'])){
579 $this->inheritTimeServer = false;
580 unset($source['gotoNtpServer']['count']);
581 foreach($source['gotoNtpServer'] as $server){
582 $this->gotoNtpServer[$server] = $server;
583 }
584 }
586 /* Set inherit checkbox state */
587 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
588 $this->inheritTimeServer = true;
589 $this->gotoNtpServer=array();
590 }
591 }
594 /* Return plugin informations for acl handling */
595 static function plInfo()
596 {
597 return (array(
598 "plShortName" => _("Terminal"),
599 "plDescription" => _("Terminal generic"),
600 "plSelfModify" => FALSE,
601 "plDepends" => array(),
602 "plPriority" => 1,
603 "plSection" => array("administration"),
604 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
605 "objectClass" => "gotoTerminal")),
606 "plProvidedAcls"=> array(
607 "gotoMode" => _("Mode"),
608 "gotoTerminalPath" => _("Root server"),
609 "gotoSwapServer" => _("Swap server"),
610 "gotoSyslogServer" => _("Syslog server enabled"),
611 "gotoNtpServer" => _("Ntp server settings"),
612 "base" => _("Base"),
613 "cn" => _("Name"),
614 "description" => _("Description"),
615 "gotoRootPasswd" => _("Root password"),
616 "FAIstate" => _("Action flag"))
617 ));
618 }
621 function set_everything_to_inherited()
622 {
623 $this->gotoTerminalPath = "default";
624 $this->gotoSwapServer = "default" ;
625 $this->gotoSyslogServer = "default";
626 $this->inheritTimeServer = TRUE;
628 /* Set workstation service attributes to inherited */
629 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
630 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
631 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
632 $this->parent->by_object['termservice']->$name = "default";
633 }
634 }
636 /* Set workstation startup attributes to inherited */
637 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
638 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
639 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
640 }
641 }
642 }
644 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
645 ?>