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 = search_config($config->data,"faiManagement","CLASS");
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->dn,$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= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
208 if ($cmd == ""){
209 print_red(_("No WAKECMD definition found in your gosa.conf"));
210 } else {
211 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
212 if ($retval != 0){
213 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
214 }
215 }
216 break;
218 case 'reboot':
219 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
220 if ($cmd == ""){
221 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
222 } else {
223 exec ($cmd." ".$this->cn, $dummy, $retval);
224 if ($retval != 0){
225 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
226 }
227 }
228 break;
230 case 'halt':
231 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
232 if ($cmd == ""){
233 print_red(_("No HALTCMD definition found in your gosa.conf"));
234 } else {
235 exec ($cmd." ".$this->cn, $dummy, $retval);
236 if ($retval != 0){
237 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
238 }
239 }
240 break;
241 }
242 }
244 /* Base select dialog */
245 $once = true;
246 foreach($_POST as $name => $value){
247 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
248 $once = false;
249 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
250 $this->dialog->setCurrentBase($this->base);
251 }
252 }
254 /* Dialog handling */
255 if(is_object($this->dialog)){
256 /* Must be called before save_object */
257 $this->dialog->save_object();
259 if($this->dialog->isClosed()){
260 $this->dialog = false;
261 }elseif($this->dialog->isSelected()){
263 /* A new base was selected, check if it is a valid one */
264 $tmp = $this->get_allowed_bases();
265 if(isset($tmp[$this->dialog->isSelected()])){
266 $this->base = $this->dialog->isSelected();
267 }
268 $this->dialog= false;
269 }else{
270 return($this->dialog->execute());
271 }
272 }
274 /* Do we represent a valid terminal? */
275 if (!$this->is_account && $this->parent == NULL){
276 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
277 _("This 'dn' has no terminal features.")."</b>";
278 return($display);
279 }
281 /* Add new ntp Server to our list */
282 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
283 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
284 }
286 /* Delete selected NtpServer for list of used servers */
287 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
288 foreach($_POST['gotoNtpServerSelected'] as $name){
289 unset($this->gotoNtpServer[$name]);
290 }
291 }
293 /* Fill templating stuff */
294 $smarty= get_smarty();
296 $tmp = $this->plInfo();
297 foreach($tmp['plProvidedAcls'] as $name => $translation){
298 $smarty->assign($name."ACL",$this->getacl($name));
299 }
301 $smarty->assign("cn", $this->cn);
302 $smarty->assign("staticAddress", "");
304 $smarty->assign("bases", $this->get_allowed_bases());
306 /* tell smarty the inherit checkbox state */
307 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
309 /* Check if terminal is online */
310 $query= "fping -q -r 1 -t 500 ".$this->cn;
311 exec ($query, $dummy, $retval);
313 /* Offline */
314 if ($retval == 0){
315 $smarty->assign("actions", array( "halt" => _("Switch off"),
316 "reboot" => _("Reboot"),
317 "memcheck" => _("Memory test"),
318 "sysinfo" => _("System analysis")));
319 } else {
320 $smarty->assign("actions", array("wake" => _("Wake up"),
321 "memcheck" => _("Memory test"),
322 "sysinfo" => _("System analysis")));
323 }
325 /* Arrays */
326 $smarty->assign("modes", $this->modes);
328 $tmp2 = array();
329 foreach($this->config->data['SERVERS']['NFS'] as $server){
330 if($server != "default"){
331 $tmp2[$server]= $server;
332 }else{
333 if($this->member_of_ogroup){
334 $tmp2[$server]="["._("inherited")."]";
335 }
336 }
337 }
339 $smarty->assign("nfsservers", $tmp2);
340 $smarty->assign("syslogservers", $this->gotoSyslogServers);
342 $tmp = array();
343 foreach($this->gotoNtpServers as $server){
344 if(!in_array($server,$this->gotoNtpServer)){
345 $tmp[$server] = $server;
346 }
347 }
349 $smarty->assign("ntpservers", $tmp);
350 $smarty->assign("fai_activated",$this->fai_activated);
352 /* Variables */
353 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
354 $smarty->assign($val."_select", $this->$val);
355 }
357 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
359 /* Show main page */
360 $smarty->assign("netconfig", $this->netConfigDNS->execute());
361 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
362 }
364 function remove_from_parent()
365 {
366 if($this->acl_is_removeable()){
367 $ldap= $this->config->get_ldap_link();
368 $ldap->cd($this->dn);
369 $ldap->cat($this->dn, array('dn'));
370 if($ldap->count()){
371 $this->netConfigDNS->remove_from_parent();
372 $ldap->rmDir($this->dn);
374 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
376 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
378 /* Optionally execute a command after we're done */
379 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
381 /* Delete references to object groups */
382 $ldap->cd ($this->config->current['BASE']);
383 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
384 while ($ldap->fetch()){
385 $og= new ogroup($this->config, $ldap->getDN());
386 unset($og->member[$this->dn]);
387 $og->save ();
388 }
389 }
390 }
391 }
394 /* Save data to object */
395 function save_object()
396 {
397 /* Create a base backup and reset the
398 base directly after calling plugin::save_object();
399 Base will be set seperatly a few lines below */
400 $base_tmp = $this->base;
401 plugin::save_object();
402 $this->base = $base_tmp;
404 /* Set new base if allowed */
405 $tmp = $this->get_allowed_bases();
406 if(isset($_POST['base'])){
407 if(isset($tmp[$_POST['base']])){
408 $this->base= $_POST['base'];
409 }
410 }
412 $this->netConfigDNS->save_object();
414 /* Save terminal path to parent since it is used by termstartup, too */
415 if(isset($this->parent->by_object['termstartup'])){
416 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
417 }
419 if(isset($_POST['termgeneric_posted'])){
420 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
421 $this->inheritTimeServer = true;
422 }else{
423 $this->inheritTimeServer = false;
424 }
425 }
427 if(isset($_POST["inheritAll"])){
428 $this->set_everything_to_inherited();
429 }
431 }
434 /* Check supplied data */
435 function check()
436 {
437 /* Call common method to give check the hook */
438 $message= plugin::check();
440 /* Skip IP & Mac checks if this is a template */
441 if($this->cn != "default"){
442 $message= array_merge($message, $this->netConfigDNS->check());
443 }
445 /* Permissions for that base? */
446 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
448 if ($this->cn == ""){
449 $message[]= _("The required field 'Terminal name' is not set.");
450 }
452 if ($this->orig_dn == 'new'){
453 $ldap= $this->config->get_ldap_link();
454 $ldap->cd ($this->base);
456 /* It is possible to have a 'default' terminal on every base */
457 if($this->cn == "default"){
458 $ldap->cat($this->dn);
459 }else{
460 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
461 }
462 if ($ldap->count() != 0){
463 while ($attrs= $ldap->fetch()){
464 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
465 continue;
466 } else {
467 if ($attrs['dn'] != $this->orig_dn){
468 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
469 break;
470 }
471 }
472 }
473 }
474 }
476 /* Check for valid ntpServer selection */
477 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
478 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
479 }
481 return ($message);
482 }
485 /* Save to LDAP */
486 function save()
487 {
489 /* Move object if requested */
490 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
491 $this->move($this->orig_dn, $this->dn);
492 }
494 plugin::save();
496 /* Strip out 'default' values */
497 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
498 if(isset($this->attrs[$val])){
499 if ($this->attrs[$val] == "default"){
500 $this->attrs[$val]= array();
501 }
502 }
503 }
505 /* Add missing arrays */
506 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
507 if (isset ($this->$val) && count ($this->$val) != 0){
508 $this->attrs["$val"]= $this->$val;
509 }
510 }
512 /* Remove all empty values */
513 if ($this->orig_dn == 'new'){
514 $attrs= array();
515 foreach ($this->attrs as $key => $val){
516 if (is_array($val) && count($val) == 0){
517 continue;
518 }
519 $attrs[$key]= $val;
520 }
521 $this->attrs= $attrs;
522 }
524 /* Set ntpServers */
525 $this->attrs['gotoNtpServer'] = array();
526 if(!$this->inheritTimeServer){
527 foreach($this->gotoNtpServer as $server){
528 $this->attrs['gotoNtpServer'][] = $server;
529 }
530 }
532 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
533 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
534 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
535 }
537 /* Write back to ldap */
538 $ldap= $this->config->get_ldap_link();
539 if ($this->orig_dn == 'new'){
540 $ldap->cd($this->config->current['BASE']);
541 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
542 $ldap->cd($this->dn);
543 if (!count($this->attrs['gotoNtpServer'])){
544 unset($this->attrs['gotoNtpServer']);
545 }
546 $ldap->add($this->attrs);
547 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
548 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
549 } else {
550 $ldap->cd($this->dn);
551 $this->cleanup();
552 $ldap->modify ($this->attrs);
553 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
554 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
555 }
557 /* cn=default and macAddress=- indicates that this is a template */
558 if($this->cn == "default"){
559 $this->netConfigDNS->macAddress = "-";
560 }
562 $this->netConfigDNS->cn = $this->cn;
563 $this->netConfigDNS->save($this->dn);
564 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
565 }
571 /* Display generic part for server copy & paste */
572 function getCopyDialog()
573 {
574 $vars = array("cn");
575 $smarty = get_smarty();
576 $smarty->assign("cn" ,$this->cn);
577 $smarty->assign("object","terminal");
578 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
579 $ret = array();
580 $ret['string'] = $str;
581 $ret['status'] = "";
582 return($ret);
583 }
586 function saveCopyDialog()
587 {
588 if(isset($_POST['cn'])){
589 $this->cn = $_POST['cn'];
590 }
591 }
594 function PrepareForCopyPaste($source)
595 {
596 plugin::PrepareForCopyPaste($source);
597 if(isset($source['macAddress'][0])){
598 $this->netConfigDNS->macAddress = $source['macAddress'][0];
599 }
600 if(isset($source['ipHostNumber'][0])){
601 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
602 }
604 /* Create used ntp server array */
605 $this->gotoNtpServer= array();
606 if(isset($source['gotoNtpServer'])){
607 $this->inheritTimeServer = false;
608 unset($source['gotoNtpServer']['count']);
609 foreach($source['gotoNtpServer'] as $server){
610 $this->gotoNtpServer[$server] = $server;
611 }
612 }
614 /* Set inherit checkbox state */
615 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
616 $this->inheritTimeServer = true;
617 $this->gotoNtpServer=array();
618 }
619 }
622 /* Return plugin informations for acl handling */
623 function plInfo()
624 {
625 return (array(
626 "plShortName" => _("Terminal"),
627 "plDescription" => _("Terminal generic"),
628 "plSelfModify" => FALSE,
629 "plDepends" => array(),
630 "plPriority" => 1,
631 "plSection" => array("administration"),
632 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
633 "objectClass" => "gotoTerminal")),
634 "plProvidedAcls"=> array(
635 "gotoMode" => _("Mode"),
636 "gotoTerminalPath" => _("Root server"),
637 "gotoSwapServer" => _("Swap server"),
638 "gotoSyslogServer" => _("Syslog server enabled"),
639 "gotoNtpServer" => _("Ntp server settings"),
640 "base" => _("Base"),
641 "cn" => _("Name"),
642 "gotoRootPasswd" => _("Root password"),
643 "FAIstate" => _("Action flag"))
644 ));
645 }
648 function set_everything_to_inherited()
649 {
650 $this->gotoTerminalPath = "default";
651 $this->gotoSwapServer = "default" ;
652 $this->gotoSyslogServer = "default";
653 $this->inheritTimeServer = TRUE;
655 /* Set workstation service attributes to inherited */
656 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
657 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
658 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
659 $this->parent->by_object['termservice']->$name = "default";
660 }
661 }
663 /* Set workstation startup attributes to inherited */
664 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
665 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
666 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
667 }
668 }
669 }
671 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
672 ?>