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('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 $smarty->assign("netconfig", $this->netConfigDNS->execute());
362 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
363 }
365 function remove_from_parent()
366 {
367 if($this->acl_is_removeable()){
368 $ldap= $this->config->get_ldap_link();
369 $ldap->cd($this->dn);
370 $ldap->cat($this->dn, array('dn'));
371 if($ldap->count()){
372 $this->netConfigDNS->remove_from_parent();
373 $ldap->rmDir($this->dn);
375 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
377 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
379 /* Optionally execute a command after we're done */
380 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
382 /* Delete references to object groups */
383 $ldap->cd ($this->config->current['BASE']);
384 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
385 while ($ldap->fetch()){
386 $og= new ogroup($this->config, $ldap->getDN());
387 unset($og->member[$this->dn]);
388 $og->save ();
389 }
390 }
391 }
392 }
395 /* Save data to object */
396 function save_object()
397 {
398 /* Create a base backup and reset the
399 base directly after calling plugin::save_object();
400 Base will be set seperatly a few lines below */
401 $base_tmp = $this->base;
402 plugin::save_object();
403 $this->base = $base_tmp;
405 /* Set new base if allowed */
406 $tmp = $this->get_allowed_bases();
407 if(isset($_POST['base'])){
408 if(isset($tmp[$_POST['base']])){
409 $this->base= $_POST['base'];
410 }
411 }
413 $this->netConfigDNS->save_object();
415 /* Save terminal path to parent since it is used by termstartup, too */
416 if(isset($this->parent->by_object['termstartup'])){
417 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
418 }
420 if(isset($_POST['termgeneric_posted'])){
421 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
422 $this->inheritTimeServer = true;
423 }else{
424 $this->inheritTimeServer = false;
425 }
426 }
428 if(isset($_POST["inheritAll"])){
429 $this->set_everything_to_inherited();
430 }
432 }
435 /* Check supplied data */
436 function check()
437 {
438 /* Call common method to give check the hook */
439 $message= plugin::check();
441 /* Skip IP & Mac checks if this is a template */
442 if($this->cn != "default"){
443 $message= array_merge($message, $this->netConfigDNS->check());
444 }
446 /* Permissions for that base? */
447 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
449 if ($this->cn == ""){
450 $message[]= _("The required field 'Terminal name' is not set.");
451 }
453 if ($this->orig_dn == 'new'){
454 $ldap= $this->config->get_ldap_link();
455 $ldap->cd ($this->base);
457 /* It is possible to have a 'default' terminal on every base */
458 if($this->cn == "default"){
459 $ldap->cat($this->dn);
460 }else{
461 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
462 }
463 if ($ldap->count() != 0){
464 while ($attrs= $ldap->fetch()){
465 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
466 continue;
467 } else {
468 if ($attrs['dn'] != $this->orig_dn){
469 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
470 break;
471 }
472 }
473 }
474 }
475 }
477 /* Check for valid ntpServer selection */
478 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
479 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
480 }
482 return ($message);
483 }
486 /* Save to LDAP */
487 function save()
488 {
490 /* Move object if requested */
491 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
492 $this->move($this->orig_dn, $this->dn);
493 }
495 plugin::save();
497 /* Strip out 'default' values */
498 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
499 if(isset($this->attrs[$val])){
500 if ($this->attrs[$val] == "default"){
501 $this->attrs[$val]= array();
502 }
503 }
504 }
506 /* Add missing arrays */
507 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
508 if (isset ($this->$val) && count ($this->$val) != 0){
509 $this->attrs["$val"]= $this->$val;
510 }
511 }
513 /* Remove all empty values */
514 if ($this->orig_dn == 'new'){
515 $attrs= array();
516 foreach ($this->attrs as $key => $val){
517 if (is_array($val) && count($val) == 0){
518 continue;
519 }
520 $attrs[$key]= $val;
521 }
522 $this->attrs= $attrs;
523 }
525 /* Set ntpServers */
526 $this->attrs['gotoNtpServer'] = array();
527 if(!$this->inheritTimeServer){
528 foreach($this->gotoNtpServer as $server){
529 $this->attrs['gotoNtpServer'][] = $server;
530 }
531 }
533 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
534 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
535 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
536 }
538 /* Write back to ldap */
539 $ldap= $this->config->get_ldap_link();
540 if ($this->orig_dn == 'new'){
541 $ldap->cd($this->config->current['BASE']);
542 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
543 $ldap->cd($this->dn);
544 if (!count($this->attrs['gotoNtpServer'])){
545 unset($this->attrs['gotoNtpServer']);
546 }
547 $ldap->add($this->attrs);
548 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
549 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
550 } else {
551 $ldap->cd($this->dn);
552 $this->cleanup();
553 $ldap->modify ($this->attrs);
554 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
555 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
556 }
558 /* cn=default and macAddress=- indicates that this is a template */
559 if($this->cn == "default"){
560 $this->netConfigDNS->macAddress = "-";
561 }
563 $this->netConfigDNS->cn = $this->cn;
564 $this->netConfigDNS->save();
565 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
566 }
572 /* Display generic part for server copy & paste */
573 function getCopyDialog()
574 {
575 $vars = array("cn");
576 $smarty = get_smarty();
577 $smarty->assign("cn" ,$this->cn);
578 $smarty->assign("object","terminal");
579 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
580 $ret = array();
581 $ret['string'] = $str;
582 $ret['status'] = "";
583 return($ret);
584 }
587 function saveCopyDialog()
588 {
589 if(isset($_POST['cn'])){
590 $this->cn = $_POST['cn'];
591 }
592 }
595 function PrepareForCopyPaste($source)
596 {
597 plugin::PrepareForCopyPaste($source);
598 if(isset($source['macAddress'][0])){
599 $this->netConfigDNS->macAddress = $source['macAddress'][0];
600 }
601 if(isset($source['ipHostNumber'][0])){
602 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
603 }
605 /* Create used ntp server array */
606 $this->gotoNtpServer= array();
607 if(isset($source['gotoNtpServer'])){
608 $this->inheritTimeServer = false;
609 unset($source['gotoNtpServer']['count']);
610 foreach($source['gotoNtpServer'] as $server){
611 $this->gotoNtpServer[$server] = $server;
612 }
613 }
615 /* Set inherit checkbox state */
616 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
617 $this->inheritTimeServer = true;
618 $this->gotoNtpServer=array();
619 }
620 }
623 /* Return plugin informations for acl handling */
624 function plInfo()
625 {
626 return (array(
627 "plShortName" => _("Terminal"),
628 "plDescription" => _("Terminal generic"),
629 "plSelfModify" => FALSE,
630 "plDepends" => array(),
631 "plPriority" => 1,
632 "plSection" => array("administration"),
633 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
634 "objectClass" => "gotoTerminal")),
635 "plProvidedAcls"=> array(
636 "gotoMode" => _("Mode"),
637 "gotoTerminalPath" => _("Root server"),
638 "gotoSwapServer" => _("Swap server"),
639 "gotoSyslogServer" => _("Syslog server enabled"),
640 "gotoNtpServer" => _("Ntp server settings"),
641 "base" => _("Base"),
642 "cn" => _("Name"),
643 "gotoRootPasswd" => _("Root password"),
644 "FAIstate" => _("Action flag"))
645 ));
646 }
649 function set_everything_to_inherited()
650 {
651 $this->gotoTerminalPath = "default";
652 $this->gotoSwapServer = "default" ;
653 $this->gotoSyslogServer = "default";
654 $this->inheritTimeServer = TRUE;
656 /* Set workstation service attributes to inherited */
657 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
658 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
659 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
660 $this->parent->by_object['termservice']->$name = "default";
661 }
662 }
664 /* Set workstation startup attributes to inherited */
665 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
666 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
667 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
668 }
669 }
670 }
672 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
673 ?>