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= "locked";
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 "rescan" => "",
52 "wake" => "",
53 "memcheck" => "memcheck",
54 "sysinfo" => "sysinfo");
56 var $fai_activated = FALSE;
57 var $view_logged = FALSE;
59 var $member_of_ogroup = FALSE;
61 function termgeneric (&$config, $dn= NULL, $parent= NULL)
62 {
63 /* Check if FAI is activated */
64 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
65 if(!empty($tmp)){
66 $this->fai_activated = TRUE;
67 }
69 plugin::plugin ($config, $dn, $parent);
71 if(!isset($this->parent->by_object['ogroup'])){
72 $ldap = $this->config->get_ldap_link();
73 $ldap->cd ($this->config->current['BASE']);
74 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
75 $this->member_of_ogroup = $ldap->count() >= 1;
76 }
78 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
79 /* Read arrays */
80 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
81 if (!isset($this->attrs[$val])){
82 continue;
83 }
84 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
85 array_push($this->$val, $this->attrs[$val][$i]);
86 }
87 }
89 /* Create used ntp server array */
90 $this->gotoNtpServer= array();
91 if(isset($this->attrs['gotoNtpServer'])){
92 $this->inheritTimeServer = false;
93 unset($this->attrs['gotoNtpServer']['count']);
94 foreach($this->attrs['gotoNtpServer'] as $server){
95 $this->gotoNtpServer[$server] = $server;
96 }
97 }
99 /* Set inherit checkbox state */
100 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
101 $this->inheritTimeServer = true;
102 $this->gotoNtpServer=array();
103 }
105 /* You can't inherit the NTP service, if we are not member in an object group */
106 if(!$this->member_of_ogroup){
107 $this->inheritTimeServer = FALSE;
108 }
110 /* Create available ntp options */
111 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
112 foreach($this->gotoNtpServers as $key => $server){
113 if($server == "default"){
114 unset($this->gotoNtpServers[$key]);
115 }
116 }
118 $this->modes["locked"]= _("Locked");
119 $this->modes["active"]= _("Activated");
121 /* Set base */
122 if ($this->dn == "new"){
123 $ui= get_userinfo();
124 $this->base= dn2base($ui->dn);
125 } else {
126 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
127 }
129 /* Create an array of all Syslog servers */
130 $tmp = $this->config->data['SERVERS']['SYSLOG'];
131 foreach($tmp as $server){
132 $visible = $server;
133 if($server == "default" && $this->member_of_ogroup) {
134 $visible = "["._("inherited")."]";
135 }
136 $this->gotoSyslogServers[$server] = $visible;
137 }
139 $this->orig_dn= $this->dn;
140 }
142 function set_acl_base($base)
143 {
144 plugin::set_acl_base($base);
145 $this->netConfigDNS->set_acl_base($base);
146 }
148 function set_acl_category($cat)
149 {
150 plugin::set_acl_category($cat);
151 $this->netConfigDNS->set_acl_category($cat);
152 }
154 function execute()
155 {
156 /* Call parent execute */
157 plugin::execute();
159 if($this->is_account && !$this->view_logged){
160 $this->view_logged = TRUE;
161 new log("view","terminal/".get_class($this),$this->dn);
162 }
164 /* Do we need to flip is_account state? */
165 if (isset($_POST['modify_state'])){
166 $this->is_account= !$this->is_account;
167 }
169 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->mapActions[$_POST['saction']])){
170 $method= "gosa";
171 $action= $_POST['saction'];
172 if ($action == "reinstall" || $action == "softupdate"){
173 $method= "job";
174 }
175 gosaSupportDaemon::send("${method}_trigger_action_".$action, $this->netConfigDNS->macAddress, array("macAddress" => $t
176 his->netConfigDNS->macAddress));
177 }
179 /* Base select dialog */
180 $once = true;
181 foreach($_POST as $name => $value){
182 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
183 $once = false;
184 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
185 $this->dialog->setCurrentBase($this->base);
186 }
187 }
189 /* Dialog handling */
190 if(is_object($this->dialog)){
191 /* Must be called before save_object */
192 $this->dialog->save_object();
194 if($this->dialog->isClosed()){
195 $this->dialog = false;
196 }elseif($this->dialog->isSelected()){
198 /* A new base was selected, check if it is a valid one */
199 $tmp = $this->get_allowed_bases();
200 if(isset($tmp[$this->dialog->isSelected()])){
201 $this->base = $this->dialog->isSelected();
202 }
203 $this->dialog= false;
204 }else{
205 return($this->dialog->execute());
206 }
207 }
209 /* Do we represent a valid terminal? */
210 if (!$this->is_account && $this->parent === NULL){
211 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
212 _("This 'dn' has no terminal features.")."</b>";
213 return($display);
214 }
216 /* Add new ntp Server to our list */
217 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
218 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
219 }
221 /* Delete selected NtpServer for list of used servers */
222 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
223 foreach($_POST['gotoNtpServerSelected'] as $name){
224 unset($this->gotoNtpServer[$name]);
225 }
226 }
228 /* Fill templating stuff */
229 $smarty= get_smarty();
231 $tmp = $this->plInfo();
232 foreach($tmp['plProvidedAcls'] as $name => $translation){
233 $smarty->assign($name."ACL",$this->getacl($name));
234 }
236 $smarty->assign("cn", $this->cn);
237 $smarty->assign("staticAddress", "");
239 $smarty->assign("bases", $this->get_allowed_bases());
241 /* tell smarty the inherit checkbox state */
242 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
244 /* Check if terminal is online */
245 $query= "fping -q -r 1 -t 500 ".$this->cn;
246 exec ($query, $dummy, $retval);
248 /* Offline */
249 if ($retval == 0){
250 $smarty->assign("actions", array( "halt" => _("Switch off"),
251 "reboot" => _("Reboot"),
252 "memcheck" => _("Memory test"),
253 "sysinfo" => _("System analysis")));
254 } else {
255 $smarty->assign("actions", array("wake" => _("Wake up"),
256 "memcheck" => _("Memory test"),
257 "sysinfo" => _("System analysis")));
258 }
260 /* Arrays */
261 $smarty->assign("modes", $this->modes);
263 $tmp2 = array();
264 foreach($this->config->data['SERVERS']['NFS'] as $server){
265 if($server != "default"){
266 $tmp2[$server]= $server;
267 }else{
268 if($this->member_of_ogroup){
269 $tmp2[$server]="["._("inherited")."]";
270 }
271 }
272 }
274 $smarty->assign("nfsservers", $tmp2);
275 $smarty->assign("syslogservers", $this->gotoSyslogServers);
277 $tmp = array();
278 foreach($this->gotoNtpServers as $server){
279 if(!in_array($server,$this->gotoNtpServer)){
280 $tmp[$server] = $server;
281 }
282 }
284 $smarty->assign("ntpservers", $tmp);
285 $smarty->assign("fai_activated",$this->fai_activated);
287 /* Variables */
288 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
289 $smarty->assign($val."_select", $this->$val);
290 }
292 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
294 /* Show main page */
295 $str = $this->netConfigDNS->execute();
296 if(is_object($this->netConfigDNS->dialog)){
297 return($str);
298 }
299 $smarty->assign("netconfig", $str);
300 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
301 }
303 function remove_from_parent()
304 {
305 if($this->acl_is_removeable()){
306 $ldap= $this->config->get_ldap_link();
307 $ldap->cd($this->dn);
308 $ldap->cat($this->dn, array('dn'));
309 if($ldap->count()){
310 $this->netConfigDNS->remove_from_parent();
311 $ldap->rmDir($this->dn);
313 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
315 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
317 /* Optionally execute a command after we're done */
318 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
320 /* Delete references to object groups */
321 $ldap->cd ($this->config->current['BASE']);
322 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
323 while ($ldap->fetch()){
324 $og= new ogroup($this->config, $ldap->getDN());
325 unset($og->member[$this->dn]);
326 $og->save ();
327 }
328 }
329 }
330 }
333 /* Save data to object */
334 function save_object()
335 {
336 /* Create a base backup and reset the
337 base directly after calling plugin::save_object();
338 Base will be set seperatly a few lines below */
339 $base_tmp = $this->base;
340 plugin::save_object();
341 $this->base = $base_tmp;
343 /* Set new base if allowed */
344 $tmp = $this->get_allowed_bases();
345 if(isset($_POST['base'])){
346 if(isset($tmp[$_POST['base']])){
347 $this->base= $_POST['base'];
348 }
349 }
351 $this->netConfigDNS->save_object();
353 /* Save terminal path to parent since it is used by termstartup, too */
354 if(isset($this->parent->by_object['termstartup'])){
355 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
356 }
358 if(isset($_POST['termgeneric_posted'])){
359 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
360 $this->inheritTimeServer = true;
361 }else{
362 $this->inheritTimeServer = false;
363 }
364 }
366 if(isset($_POST["inheritAll"])){
367 $this->set_everything_to_inherited();
368 }
370 }
373 /* Check supplied data */
374 function check()
375 {
376 /* Call common method to give check the hook */
377 $message= plugin::check();
379 /* Skip IP & Mac checks if this is a template */
380 if($this->cn != "default"){
381 $message= array_merge($message, $this->netConfigDNS->check());
382 }
384 /* Permissions for that base? */
385 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
387 if ($this->cn == ""){
388 $message[]= _("The required field 'Terminal name' is not set.");
389 }
391 /* Check if given name is a valid host/dns name */
392 if(!tests::is_dns_name($this->cn) ){
393 $message[] = _("Please specify a valid name for this object.");
394 }
396 if ($this->orig_dn == 'new'){
397 $ldap= $this->config->get_ldap_link();
398 $ldap->cd ($this->base);
400 /* It is possible to have a 'default' terminal on every base */
401 if($this->cn == "default"){
402 $ldap->cat($this->dn);
403 }else{
404 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
405 }
406 if ($ldap->count() != 0){
407 while ($attrs= $ldap->fetch()){
408 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
409 continue;
410 } else {
411 if ($attrs['dn'] != $this->orig_dn){
412 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
413 break;
414 }
415 }
416 }
417 }
418 }
420 /* Check for valid ntpServer selection */
421 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
422 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
423 }
425 return ($message);
426 }
429 /* Save to LDAP */
430 function save()
431 {
433 /* Move object if requested */
434 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
435 $this->move($this->orig_dn, $this->dn);
436 }
438 /* Detect mode changes */
439 $activate= (isset($this->saved_attributes['gotoMode']) &&
440 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
441 $this->gotoMode == "enabled" &&
442 tests::is_ip($this->netConfigDNS->ipHostNumber));
444 plugin::save();
446 /* Strip out 'default' values */
447 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
448 if(isset($this->attrs[$val])){
449 if ($this->attrs[$val] == "default"){
450 $this->attrs[$val]= array();
451 }
452 }
453 }
455 /* Add missing arrays */
456 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
457 if (isset ($this->$val) && count ($this->$val) != 0){
458 $this->attrs["$val"]= $this->$val;
459 }
460 }
462 /* Remove all empty values */
463 if ($this->orig_dn == 'new'){
464 $attrs= array();
465 foreach ($this->attrs as $key => $val){
466 if (is_array($val) && count($val) == 0){
467 continue;
468 }
469 $attrs[$key]= $val;
470 }
471 $this->attrs= $attrs;
472 }
474 /* Set ntpServers */
475 $this->attrs['gotoNtpServer'] = array();
476 if(!$this->inheritTimeServer){
477 foreach($this->gotoNtpServer as $server){
478 $this->attrs['gotoNtpServer'][] = $server;
479 }
480 }
482 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
483 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
484 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
485 }
487 /* Write back to ldap */
488 $ldap= $this->config->get_ldap_link();
489 if ($this->orig_dn == 'new'){
490 $ldap->cd($this->config->current['BASE']);
491 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
492 $ldap->cd($this->dn);
493 if (!count($this->attrs['gotoNtpServer'])){
494 unset($this->attrs['gotoNtpServer']);
495 }
496 $ldap->add($this->attrs);
497 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
498 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
499 } else {
500 $ldap->cd($this->dn);
501 $this->cleanup();
502 $ldap->modify ($this->attrs);
503 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
504 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
505 }
507 /* cn=default and macAddress=- indicates that this is a template */
508 if($this->cn == "default"){
509 $this->netConfigDNS->macAddress = "-";
510 }
512 $this->netConfigDNS->cn = $this->cn;
513 $this->netConfigDNS->save();
514 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
516 if ($activate){
517 gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
518 }
519 }
522 /* Display generic part for server copy & paste */
523 function getCopyDialog()
524 {
525 $vars = array("cn");
526 $smarty = get_smarty();
527 $smarty->assign("cn" ,$this->cn);
528 $smarty->assign("object","terminal");
529 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
530 $ret = array();
531 $ret['string'] = $str;
532 $ret['status'] = "";
533 return($ret);
534 }
537 function saveCopyDialog()
538 {
539 if(isset($_POST['cn'])){
540 $this->cn = $_POST['cn'];
541 }
542 }
545 function PrepareForCopyPaste($source)
546 {
547 plugin::PrepareForCopyPaste($source);
548 if(isset($source['macAddress'][0])){
549 $this->netConfigDNS->macAddress = $source['macAddress'][0];
550 }
551 if(isset($source['ipHostNumber'][0])){
552 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
553 }
555 /* Create used ntp server array */
556 $this->gotoNtpServer= array();
557 if(isset($source['gotoNtpServer'])){
558 $this->inheritTimeServer = false;
559 unset($source['gotoNtpServer']['count']);
560 foreach($source['gotoNtpServer'] as $server){
561 $this->gotoNtpServer[$server] = $server;
562 }
563 }
565 /* Set inherit checkbox state */
566 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
567 $this->inheritTimeServer = true;
568 $this->gotoNtpServer=array();
569 }
570 }
573 /* Return plugin informations for acl handling */
574 static function plInfo()
575 {
576 return (array(
577 "plShortName" => _("Terminal"),
578 "plDescription" => _("Terminal generic"),
579 "plSelfModify" => FALSE,
580 "plDepends" => array(),
581 "plPriority" => 1,
582 "plSection" => array("administration"),
583 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
584 "objectClass" => "gotoTerminal")),
585 "plProvidedAcls"=> array(
586 "gotoMode" => _("Mode"),
587 "gotoTerminalPath" => _("Root server"),
588 "gotoSwapServer" => _("Swap server"),
589 "gotoSyslogServer" => _("Syslog server enabled"),
590 "gotoNtpServer" => _("Ntp server settings"),
591 "base" => _("Base"),
592 "cn" => _("Name"),
593 "gotoRootPasswd" => _("Root password"),
594 "FAIstate" => _("Action flag"))
595 ));
596 }
599 function set_everything_to_inherited()
600 {
601 $this->gotoTerminalPath = "default";
602 $this->gotoSwapServer = "default" ;
603 $this->gotoSyslogServer = "default";
604 $this->inheritTimeServer = TRUE;
606 /* Set workstation service attributes to inherited */
607 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
608 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
609 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
610 $this->parent->by_object['termservice']->$name = "default";
611 }
612 }
614 /* Set workstation startup attributes to inherited */
615 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
616 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
617 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
618 }
619 }
620 }
622 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
623 ?>