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= "disabled";
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;
31 /* Needed values and lists */
32 var $base= "";
33 var $cn= "";
34 var $orig_dn= "";
36 var $inheritTimeServer = true;
38 /* Plugin side filled */
39 var $modes= array();
41 /* attribute list for save action */
42 var $ignore_account= TRUE;
43 var $attributes= array("gotoMode", "gotoTerminalPath",
44 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
45 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
46 "ghCpuType", "ghMemSize","ghUsbSupport",
47 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
48 var $objectclasses= array("top", "gotoTerminal", "GOhard");
50 var $mapActions = array("reboot" => "",
51 "instant_update" => "softupdate",
52 "update" => "sceduledupdate",
53 "reinstall" => "install",
54 "rescan" => "",
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=".$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["disabled"]= _("disabled");
121 $this->modes["text"]= _("text");
122 $this->modes["graphic"]= _("graphic");
124 /* Set base */
125 if ($this->dn == "new"){
126 $ui= get_userinfo();
127 $this->base= dn2base($ui->dn);
128 } else {
129 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
130 }
132 /* Create an array of all Syslog servers */
133 $tmp = $this->config->data['SERVERS']['SYSLOG'];
134 foreach($tmp as $server){
135 $visible = $server;
136 if($server == "default" && $this->member_of_ogroup) {
137 $visible = "["._("inherited")."]";
138 }
139 $this->gotoSyslogServers[$server] = $visible;
140 }
142 $this->orig_dn= $this->dn;
143 }
145 function set_acl_base($base)
146 {
147 plugin::set_acl_base($base);
148 $this->netConfigDNS->set_acl_base($base);
149 }
151 function set_acl_category($cat)
152 {
153 plugin::set_acl_category($cat);
154 $this->netConfigDNS->set_acl_category($cat);
155 }
157 function execute()
158 {
159 /* Call parent execute */
160 plugin::execute();
162 if($this->is_account && !$this->view_logged){
163 $this->view_logged = TRUE;
164 new log("view","terminal/".get_class($this),$this->dn);
165 }
167 /* Do we need to flip is_account state? */
168 if (isset($_POST['modify_state'])){
169 $this->is_account= !$this->is_account;
170 }
172 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
174 /* Set FAIstate */
175 if($this->fai_activated && $this->dn != "new"){
176 $ldap = $this->config->get_ldap_link();
177 $ldap->cd($this->config->current['BASE']);
178 $ldap->cat($this->dn,array("objectClass"));
179 $res = $ldap->fetch();
181 $attrs = array();
182 $attrs['FAIstate'] = "";
183 if(isset($this->mapActions[$_POST['saction']])){
184 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
185 }
187 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
188 $attrs['objectClass'][] = $res['objectClass'][$i];
189 }
191 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
192 $attrs['objectClass'][] = "FAIobject";
193 }
195 if($attrs['FAIstate'] == ""){
196 #FIXME we should check if FAIobject is used anymore
197 $attrs['FAIstate'] = array();
198 }
200 $ldap->cd($this->dn);
201 $ldap->modify($attrs);
202 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
203 }
205 switch($_POST['saction']){
206 case 'wake':
207 $cmd= $this->config->search("termgeneric", "WAKECMD",array('tabs'));
209 if ($cmd == ""){
210 print_red(_("No WAKECMD definition found in your gosa.conf"));
211 } else {
212 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
213 if ($retval != 0){
214 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
215 }
216 }
217 break;
219 case 'reboot':
220 $cmd= $this->config->search("termgeneric", "REBOOTCMD",array('tabs'));
221 if ($cmd == ""){
222 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
223 } else {
224 exec ($cmd." ".$this->cn, $dummy, $retval);
225 if ($retval != 0){
226 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
227 }
228 }
229 break;
231 case 'halt':
232 $cmd= $this->config->search("termgeneric", "HALTCMD",array('tabs'));
233 if ($cmd == ""){
234 print_red(_("No HALTCMD definition found in your gosa.conf"));
235 } else {
236 exec ($cmd." ".$this->cn, $dummy, $retval);
237 if ($retval != 0){
238 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
239 }
240 }
241 break;
242 }
243 }
245 /* Base select dialog */
246 $once = true;
247 foreach($_POST as $name => $value){
248 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
249 $once = false;
250 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
251 $this->dialog->setCurrentBase($this->base);
252 }
253 }
255 /* Dialog handling */
256 if(is_object($this->dialog)){
257 /* Must be called before save_object */
258 $this->dialog->save_object();
260 if($this->dialog->isClosed()){
261 $this->dialog = false;
262 }elseif($this->dialog->isSelected()){
264 /* A new base was selected, check if it is a valid one */
265 $tmp = $this->get_allowed_bases();
266 if(isset($tmp[$this->dialog->isSelected()])){
267 $this->base = $this->dialog->isSelected();
268 }
269 $this->dialog= false;
270 }else{
271 return($this->dialog->execute());
272 }
273 }
275 /* Do we represent a valid terminal? */
276 if (!$this->is_account && $this->parent === NULL){
277 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
278 _("This 'dn' has no terminal features.")."</b>";
279 return($display);
280 }
282 /* Add new ntp Server to our list */
283 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
284 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
285 }
287 /* Delete selected NtpServer for list of used servers */
288 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
289 foreach($_POST['gotoNtpServerSelected'] as $name){
290 unset($this->gotoNtpServer[$name]);
291 }
292 }
294 /* Fill templating stuff */
295 $smarty= get_smarty();
297 $tmp = $this->plInfo();
298 foreach($tmp['plProvidedAcls'] as $name => $translation){
299 $smarty->assign($name."ACL",$this->getacl($name));
300 }
302 $smarty->assign("cn", $this->cn);
303 $smarty->assign("staticAddress", "");
305 $smarty->assign("bases", $this->get_allowed_bases());
307 /* tell smarty the inherit checkbox state */
308 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
310 /* Check if terminal is online */
311 $query= "fping -q -r 1 -t 500 ".$this->cn;
312 exec ($query, $dummy, $retval);
314 /* Offline */
315 if ($retval == 0){
316 $smarty->assign("actions", array( "halt" => _("Switch off"),
317 "reboot" => _("Reboot"),
318 "memcheck" => _("Memory test"),
319 "sysinfo" => _("System analysis")));
320 } else {
321 $smarty->assign("actions", array("wake" => _("Wake up"),
322 "memcheck" => _("Memory test"),
323 "sysinfo" => _("System analysis")));
324 }
326 /* Arrays */
327 $smarty->assign("modes", $this->modes);
329 $tmp2 = array();
330 foreach($this->config->data['SERVERS']['NFS'] as $server){
331 if($server != "default"){
332 $tmp2[$server]= $server;
333 }else{
334 if($this->member_of_ogroup){
335 $tmp2[$server]="["._("inherited")."]";
336 }
337 }
338 }
340 $smarty->assign("nfsservers", $tmp2);
341 $smarty->assign("syslogservers", $this->gotoSyslogServers);
343 $tmp = array();
344 foreach($this->gotoNtpServers as $server){
345 if(!in_array($server,$this->gotoNtpServer)){
346 $tmp[$server] = $server;
347 }
348 }
350 $smarty->assign("ntpservers", $tmp);
351 $smarty->assign("fai_activated",$this->fai_activated);
353 /* Variables */
354 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
355 $smarty->assign($val."_select", $this->$val);
356 }
358 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
360 /* Show main page */
361 $str = $this->netConfigDNS->execute();
362 if(is_object($this->netConfigDNS->dialog)){
363 return($str);
364 }
365 $smarty->assign("netconfig", $str);
366 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
367 }
369 function remove_from_parent()
370 {
371 if($this->acl_is_removeable()){
372 $ldap= $this->config->get_ldap_link();
373 $ldap->cd($this->dn);
374 $ldap->cat($this->dn, array('dn'));
375 if($ldap->count()){
376 $this->netConfigDNS->remove_from_parent();
377 $ldap->rmDir($this->dn);
379 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
381 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
383 /* Optionally execute a command after we're done */
384 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
386 /* Delete references to object groups */
387 $ldap->cd ($this->config->current['BASE']);
388 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
389 while ($ldap->fetch()){
390 $og= new ogroup($this->config, $ldap->getDN());
391 unset($og->member[$this->dn]);
392 $og->save ();
393 }
394 }
395 }
396 }
399 /* Save data to object */
400 function save_object()
401 {
402 /* Create a base backup and reset the
403 base directly after calling plugin::save_object();
404 Base will be set seperatly a few lines below */
405 $base_tmp = $this->base;
406 plugin::save_object();
407 $this->base = $base_tmp;
409 /* Set new base if allowed */
410 $tmp = $this->get_allowed_bases();
411 if(isset($_POST['base'])){
412 if(isset($tmp[$_POST['base']])){
413 $this->base= $_POST['base'];
414 }
415 }
417 $this->netConfigDNS->save_object();
419 /* Save terminal path to parent since it is used by termstartup, too */
420 if(isset($this->parent->by_object['termstartup'])){
421 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
422 }
424 if(isset($_POST['termgeneric_posted'])){
425 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
426 $this->inheritTimeServer = true;
427 }else{
428 $this->inheritTimeServer = false;
429 }
430 }
432 if(isset($_POST["inheritAll"])){
433 $this->set_everything_to_inherited();
434 }
436 }
439 /* Check supplied data */
440 function check()
441 {
442 /* Call common method to give check the hook */
443 $message= plugin::check();
445 /* Skip IP & Mac checks if this is a template */
446 if($this->cn != "default"){
447 $message= array_merge($message, $this->netConfigDNS->check());
448 }
450 /* Permissions for that base? */
451 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
453 if ($this->cn == ""){
454 $message[]= _("The required field 'Terminal name' is not set.");
455 }
457 if ($this->orig_dn == 'new'){
458 $ldap= $this->config->get_ldap_link();
459 $ldap->cd ($this->base);
461 /* It is possible to have a 'default' terminal on every base */
462 if($this->cn == "default"){
463 $ldap->cat($this->dn);
464 }else{
465 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
466 }
467 if ($ldap->count() != 0){
468 while ($attrs= $ldap->fetch()){
469 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
470 continue;
471 } else {
472 if ($attrs['dn'] != $this->orig_dn){
473 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
474 break;
475 }
476 }
477 }
478 }
479 }
481 /* Check for valid ntpServer selection */
482 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
483 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
484 }
486 return ($message);
487 }
490 /* Save to LDAP */
491 function save()
492 {
494 /* Move object if requested */
495 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
496 $this->move($this->orig_dn, $this->dn);
497 }
499 plugin::save();
501 /* Strip out 'default' values */
502 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
503 if(isset($this->attrs[$val])){
504 if ($this->attrs[$val] == "default"){
505 $this->attrs[$val]= array();
506 }
507 }
508 }
510 /* Add missing arrays */
511 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
512 if (isset ($this->$val) && count ($this->$val) != 0){
513 $this->attrs["$val"]= $this->$val;
514 }
515 }
517 /* Remove all empty values */
518 if ($this->orig_dn == 'new'){
519 $attrs= array();
520 foreach ($this->attrs as $key => $val){
521 if (is_array($val) && count($val) == 0){
522 continue;
523 }
524 $attrs[$key]= $val;
525 }
526 $this->attrs= $attrs;
527 }
529 /* Set ntpServers */
530 $this->attrs['gotoNtpServer'] = array();
531 if(!$this->inheritTimeServer){
532 foreach($this->gotoNtpServer as $server){
533 $this->attrs['gotoNtpServer'][] = $server;
534 }
535 }
537 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
538 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
539 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
540 }
542 /* Write back to ldap */
543 $ldap= $this->config->get_ldap_link();
544 if ($this->orig_dn == 'new'){
545 $ldap->cd($this->config->current['BASE']);
546 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
547 $ldap->cd($this->dn);
548 if (!count($this->attrs['gotoNtpServer'])){
549 unset($this->attrs['gotoNtpServer']);
550 }
551 $ldap->add($this->attrs);
552 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
553 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
554 } else {
555 $ldap->cd($this->dn);
556 $this->cleanup();
557 $ldap->modify ($this->attrs);
558 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
559 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
560 }
562 /* cn=default and macAddress=- indicates that this is a template */
563 if($this->cn == "default"){
564 $this->netConfigDNS->macAddress = "-";
565 }
567 $this->netConfigDNS->cn = $this->cn;
568 $this->netConfigDNS->save();
569 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
570 }
576 /* Display generic part for server copy & paste */
577 function getCopyDialog()
578 {
579 $vars = array("cn");
580 $smarty = get_smarty();
581 $smarty->assign("cn" ,$this->cn);
582 $smarty->assign("object","terminal");
583 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
584 $ret = array();
585 $ret['string'] = $str;
586 $ret['status'] = "";
587 return($ret);
588 }
591 function saveCopyDialog()
592 {
593 if(isset($_POST['cn'])){
594 $this->cn = $_POST['cn'];
595 }
596 }
599 function PrepareForCopyPaste($source)
600 {
601 plugin::PrepareForCopyPaste($source);
602 if(isset($source['macAddress'][0])){
603 $this->netConfigDNS->macAddress = $source['macAddress'][0];
604 }
605 if(isset($source['ipHostNumber'][0])){
606 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
607 }
609 /* Create used ntp server array */
610 $this->gotoNtpServer= array();
611 if(isset($source['gotoNtpServer'])){
612 $this->inheritTimeServer = false;
613 unset($source['gotoNtpServer']['count']);
614 foreach($source['gotoNtpServer'] as $server){
615 $this->gotoNtpServer[$server] = $server;
616 }
617 }
619 /* Set inherit checkbox state */
620 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
621 $this->inheritTimeServer = true;
622 $this->gotoNtpServer=array();
623 }
624 }
627 /* Return plugin informations for acl handling */
628 function plInfo()
629 {
630 return (array(
631 "plShortName" => _("Terminal"),
632 "plDescription" => _("Terminal generic"),
633 "plSelfModify" => FALSE,
634 "plDepends" => array(),
635 "plPriority" => 1,
636 "plSection" => array("administration"),
637 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
638 "objectClass" => "gotoTerminal")),
639 "plProvidedAcls"=> array(
640 "gotoMode" => _("Mode"),
641 "gotoTerminalPath" => _("Root server"),
642 "gotoSwapServer" => _("Swap server"),
643 "gotoSyslogServer" => _("Syslog server enabled"),
644 "gotoNtpServer" => _("Ntp server settings"),
645 "base" => _("Base"),
646 "cn" => _("Name"),
647 "gotoRootPasswd" => _("Root password"),
648 "FAIstate" => _("Action flag"))
649 ));
650 }
653 function set_everything_to_inherited()
654 {
655 $this->gotoTerminalPath = "default";
656 $this->gotoSwapServer = "default" ;
657 $this->gotoSyslogServer = "default";
658 $this->inheritTimeServer = TRUE;
660 /* Set workstation service attributes to inherited */
661 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
662 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
663 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
664 $this->parent->by_object['termservice']->$name = "default";
665 }
666 }
668 /* Set workstation startup attributes to inherited */
669 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
670 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
671 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
672 }
673 }
674 }
676 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
677 ?>