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;
32 /* Needed values and lists */
33 var $base= "";
34 var $cn= "";
35 var $description= "";
36 var $orig_dn= "";
38 var $inheritTimeServer = true;
40 /* Plugin side filled */
41 var $modes= array();
43 /* attribute list for save action */
44 var $ignore_account= TRUE;
45 var $attributes= array("gotoMode", "gotoTerminalPath",
46 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
47 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
48 "ghCpuType", "ghMemSize","ghUsbSupport", "description",
49 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
50 var $objectclasses= array("top", "gotoTerminal", "GOhard");
52 var $mapActions = array("reboot" => "",
53 "rescan" => "",
54 "wake" => "",
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=".LDAP::prepare4filter($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["locked"]= _("Locked");
121 $this->modes["active"]= _("Activated");
123 /* Set base */
124 if ($this->dn == "new"){
125 $ui= get_userinfo();
126 $this->base= dn2base($ui->dn);
127 } else {
128 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
129 }
131 /* Create an array of all Syslog servers */
132 $tmp = $this->config->data['SERVERS']['SYSLOG'];
133 foreach($tmp as $server){
134 $visible = $server;
135 if($server == "default" && $this->member_of_ogroup) {
136 $visible = "["._("inherited")."]";
137 }
138 $this->gotoSyslogServers[$server] = $visible;
139 }
141 $this->orig_dn= $this->dn;
142 }
144 function set_acl_base($base)
145 {
146 plugin::set_acl_base($base);
147 $this->netConfigDNS->set_acl_base($base);
148 }
150 function set_acl_category($cat)
151 {
152 plugin::set_acl_category($cat);
153 $this->netConfigDNS->set_acl_category($cat);
154 }
156 function execute()
157 {
158 /* Call parent execute */
159 plugin::execute();
161 if($this->is_account && !$this->view_logged){
162 $this->view_logged = TRUE;
163 new log("view","terminal/".get_class($this),$this->dn);
164 }
166 /* Do we need to flip is_account state? */
167 if (isset($_POST['modify_state'])){
168 $this->is_account= !$this->is_account;
169 }
171 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->mapActions[$_POST['saction']])){
172 $method= "gosa";
173 $action= $_POST['saction'];
174 if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
175 $method= "job";
176 }
177 gosaSupportDaemon::send("${method}_trigger_action_".$action, $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
178 }
180 /* Base select dialog */
181 $once = true;
182 foreach($_POST as $name => $value){
183 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
184 $once = false;
185 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
186 $this->dialog->setCurrentBase($this->base);
187 }
188 }
190 /* Dialog handling */
191 if(is_object($this->dialog)){
192 /* Must be called before save_object */
193 $this->dialog->save_object();
195 if($this->dialog->isClosed()){
196 $this->dialog = false;
197 }elseif($this->dialog->isSelected()){
199 /* A new base was selected, check if it is a valid one */
200 $tmp = $this->get_allowed_bases();
201 if(isset($tmp[$this->dialog->isSelected()])){
202 $this->base = $this->dialog->isSelected();
203 }
204 $this->dialog= false;
205 }else{
206 return($this->dialog->execute());
207 }
208 }
210 /* Do we represent a valid terminal? */
211 if (!$this->is_account && $this->parent === NULL){
212 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
213 _("This 'dn' has no terminal features.")."</b>";
214 return($display);
215 }
217 /* Add new ntp Server to our list */
218 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
219 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
220 }
222 /* Delete selected NtpServer for list of used servers */
223 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
224 foreach($_POST['gotoNtpServerSelected'] as $name){
225 unset($this->gotoNtpServer[$name]);
226 }
227 }
229 /* Fill templating stuff */
230 $smarty= get_smarty();
232 $tmp = $this->plInfo();
233 foreach($tmp['plProvidedAcls'] as $name => $translation){
234 $smarty->assign($name."ACL",$this->getacl($name));
235 }
237 $smarty->assign("cn", $this->cn);
238 $smarty->assign("description", $this->description);
239 $smarty->assign("staticAddress", "");
241 $smarty->assign("bases", $this->get_allowed_bases());
243 /* tell smarty the inherit checkbox state */
244 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
246 /* Check if terminal is online */
247 $query= "fping -q -r 1 -t 500 ".$this->cn;
248 exec ($query, $dummy, $retval);
250 /* Offline */
251 if ($retval == 0){
252 $smarty->assign("actions", array( "halt" => _("Switch off"),
253 "reboot" => _("Reboot"),
254 "memcheck" => _("Memory test"),
255 "sysinfo" => _("System analysis")));
256 } else {
257 $smarty->assign("actions", array("wake" => _("Wake up"),
258 "memcheck" => _("Memory test"),
259 "sysinfo" => _("System analysis")));
260 }
262 /* Arrays */
263 $smarty->assign("modes", $this->modes);
265 $tmp2 = array();
266 foreach($this->config->data['SERVERS']['NFS'] as $server){
267 if($server != "default"){
268 $tmp2[$server]= $server;
269 }else{
270 if($this->member_of_ogroup){
271 $tmp2[$server]="["._("inherited")."]";
272 }
273 }
274 }
276 $smarty->assign("nfsservers", $tmp2);
277 $smarty->assign("syslogservers", $this->gotoSyslogServers);
279 $tmp = array();
280 foreach($this->gotoNtpServers as $server){
281 if(!in_array($server,$this->gotoNtpServer)){
282 $tmp[$server] = $server;
283 }
284 }
286 $smarty->assign("ntpservers", $tmp);
287 $smarty->assign("fai_activated",$this->fai_activated);
289 /* Variables */
290 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
291 $smarty->assign($val."_select", $this->$val);
292 }
294 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
296 /* Show main page */
297 $str = $this->netConfigDNS->execute();
298 if(is_object($this->netConfigDNS->dialog)){
299 return($str);
300 }
301 $smarty->assign("netconfig", $str);
302 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
303 }
305 function remove_from_parent()
306 {
307 if($this->acl_is_removeable()){
308 $ldap= $this->config->get_ldap_link();
309 $ldap->cd($this->dn);
310 $ldap->cat($this->dn, array('dn'));
311 if($ldap->count()){
312 $this->netConfigDNS->remove_from_parent();
313 $ldap->rmDir($this->dn);
315 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
317 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
319 /* Optionally execute a command after we're done */
320 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
322 /* Delete references to object groups */
323 $ldap->cd ($this->config->current['BASE']);
324 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
325 while ($ldap->fetch()){
326 $og= new ogroup($this->config, $ldap->getDN());
327 unset($og->member[$this->dn]);
328 $og->save ();
329 }
330 }
331 }
332 }
335 /* Save data to object */
336 function save_object()
337 {
338 /* Create a base backup and reset the
339 base directly after calling plugin::save_object();
340 Base will be set seperatly a few lines below */
341 $base_tmp = $this->base;
342 plugin::save_object();
343 $this->base = $base_tmp;
345 /* Set new base if allowed */
346 $tmp = $this->get_allowed_bases();
347 if(isset($_POST['base'])){
348 if(isset($tmp[$_POST['base']])){
349 $this->base= $_POST['base'];
350 }
351 }
353 $this->netConfigDNS->save_object();
355 /* Save terminal path to parent since it is used by termstartup, too */
356 if(isset($this->parent->by_object['termstartup'])){
357 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
358 }
360 if(isset($_POST['termgeneric_posted'])){
361 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
362 $this->inheritTimeServer = true;
363 }else{
364 $this->inheritTimeServer = false;
365 }
366 }
368 if(isset($_POST["inheritAll"])){
369 $this->set_everything_to_inherited();
370 }
372 }
375 /* Check supplied data */
376 function check()
377 {
378 /* Call common method to give check the hook */
379 $message= plugin::check();
381 /* Skip IP & Mac checks if this is a template */
382 if($this->cn != "default"){
383 $message= array_merge($message, $this->netConfigDNS->check());
384 }
386 /* Permissions for that base? */
387 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
389 if ($this->cn == ""){
390 $message[]= _("The required field 'Terminal name' is not set.");
391 }
393 /* Check if given name is a valid host/dns name */
394 if(!tests::is_dns_name($this->cn) ){
395 $message[] = _("Please specify a valid name for this object.");
396 }
398 if ($this->orig_dn == 'new'){
399 $ldap= $this->config->get_ldap_link();
400 $ldap->cd ($this->base);
402 /* It is possible to have a 'default' terminal on every base */
403 if($this->cn == "default"){
404 $ldap->cat($this->dn);
405 }else{
406 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
407 }
408 if ($ldap->count() != 0){
409 while ($attrs= $ldap->fetch()){
410 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
411 continue;
412 } else {
413 if ($attrs['dn'] != $this->orig_dn){
414 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
415 break;
416 }
417 }
418 }
419 }
420 }
422 /* Check for valid ntpServer selection */
423 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
424 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
425 }
427 return ($message);
428 }
431 /* Save to LDAP */
432 function save()
433 {
435 /* Move object if requested */
436 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
437 $this->move($this->orig_dn, $this->dn);
438 }
440 /* Detect mode changes */
441 $activate= (isset($this->saved_attributes['gotoMode']) &&
442 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
443 $this->gotoMode == "enabled" &&
444 tests::is_ip($this->netConfigDNS->ipHostNumber));
446 plugin::save();
448 /* Strip out 'default' values */
449 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
450 if(isset($this->attrs[$val])){
451 if ($this->attrs[$val] == "default"){
452 $this->attrs[$val]= array();
453 }
454 }
455 }
457 /* Add missing arrays */
458 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
459 if (isset ($this->$val) && count ($this->$val) != 0){
460 $this->attrs["$val"]= $this->$val;
461 }
462 }
464 /* Remove all empty values */
465 if ($this->orig_dn == 'new'){
466 $attrs= array();
467 foreach ($this->attrs as $key => $val){
468 if (is_array($val) && count($val) == 0){
469 continue;
470 }
471 $attrs[$key]= $val;
472 }
473 $this->attrs= $attrs;
474 }
476 /* Set ntpServers */
477 $this->attrs['gotoNtpServer'] = array();
478 if(!$this->inheritTimeServer){
479 foreach($this->gotoNtpServer as $server){
480 $this->attrs['gotoNtpServer'][] = $server;
481 }
482 }
484 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
485 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
486 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
487 }
489 /* Write back to ldap */
490 $ldap= $this->config->get_ldap_link();
491 if ($this->orig_dn == 'new'){
492 $ldap->cd($this->config->current['BASE']);
493 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
494 $ldap->cd($this->dn);
495 if (!count($this->attrs['gotoNtpServer'])){
496 unset($this->attrs['gotoNtpServer']);
497 }
498 $ldap->add($this->attrs);
499 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
500 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
501 } else {
502 $ldap->cd($this->dn);
503 $this->cleanup();
504 $ldap->modify ($this->attrs);
505 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
506 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
507 }
509 /* cn=default and macAddress=- indicates that this is a template */
510 if($this->cn == "default"){
511 $this->netConfigDNS->macAddress = "-";
512 }
514 $this->netConfigDNS->cn = $this->cn;
515 $this->netConfigDNS->save();
516 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
518 if ($activate){
519 gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
520 }
521 }
524 /* Display generic part for server copy & paste */
525 function getCopyDialog()
526 {
527 $vars = array("cn");
528 $smarty = get_smarty();
529 $smarty->assign("cn" ,$this->cn);
530 $smarty->assign("object","terminal");
531 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
532 $ret = array();
533 $ret['string'] = $str;
534 $ret['status'] = "";
535 return($ret);
536 }
539 function saveCopyDialog()
540 {
541 if(isset($_POST['cn'])){
542 $this->cn = $_POST['cn'];
543 }
544 }
547 function PrepareForCopyPaste($source)
548 {
549 plugin::PrepareForCopyPaste($source);
550 if(isset($source['macAddress'][0])){
551 $this->netConfigDNS->macAddress = $source['macAddress'][0];
552 }
553 if(isset($source['ipHostNumber'][0])){
554 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
555 }
557 /* Create used ntp server array */
558 $this->gotoNtpServer= array();
559 if(isset($source['gotoNtpServer'])){
560 $this->inheritTimeServer = false;
561 unset($source['gotoNtpServer']['count']);
562 foreach($source['gotoNtpServer'] as $server){
563 $this->gotoNtpServer[$server] = $server;
564 }
565 }
567 /* Set inherit checkbox state */
568 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
569 $this->inheritTimeServer = true;
570 $this->gotoNtpServer=array();
571 }
572 }
575 /* Return plugin informations for acl handling */
576 static function plInfo()
577 {
578 return (array(
579 "plShortName" => _("Terminal"),
580 "plDescription" => _("Terminal generic"),
581 "plSelfModify" => FALSE,
582 "plDepends" => array(),
583 "plPriority" => 1,
584 "plSection" => array("administration"),
585 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
586 "objectClass" => "gotoTerminal")),
587 "plProvidedAcls"=> array(
588 "gotoMode" => _("Mode"),
589 "gotoTerminalPath" => _("Root server"),
590 "gotoSwapServer" => _("Swap server"),
591 "gotoSyslogServer" => _("Syslog server enabled"),
592 "gotoNtpServer" => _("Ntp server settings"),
593 "base" => _("Base"),
594 "cn" => _("Name"),
595 "description" => _("Description"),
596 "gotoRootPasswd" => _("Root password"),
597 "FAIstate" => _("Action flag"))
598 ));
599 }
602 function set_everything_to_inherited()
603 {
604 $this->gotoTerminalPath = "default";
605 $this->gotoSwapServer = "default" ;
606 $this->gotoSyslogServer = "default";
607 $this->inheritTimeServer = TRUE;
609 /* Set workstation service attributes to inherited */
610 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
611 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
612 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
613 $this->parent->by_object['termservice']->$name = "default";
614 }
615 }
617 /* Set workstation startup attributes to inherited */
618 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
619 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
620 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
621 }
622 }
623 }
625 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
626 ?>