Code

Removed show_ldap_error() calls
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_terminalGeneric.inc
1 <?php
3 class termgeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $gotoMode= "locked";
7   var $gotoTerminalPath= "";
8   var $gotoSwapServer= "";
9   var $gotoSyslogServer= "";
10   var $gotoSyslogServers = array();
11   var $gotoNtpServer= array();
12   var $gotoNtpServers= array();
13   var $gotoSndModule= "";
14   var $gotoFloppyEnable= "";
15   var $gotoCdromEnable= "";
16   var $ghCpuType= "-";
17   var $ghMemSize= "-";
18   var $ghUsbSupport= "-";
19   var $ghNetNic= array();
20   var $ghIdeDev= array();
21   var $ghScsiDev= array();
22   var $ghGfxAdapter= "-";
23   var $ghSoundAdapter= "-";
24   var $gotoLastUser= "-";
25   var $netConfigDNS;
26   
27   /* Needed values and lists */
28   var $base= "";
29   var $cn= "";
30   var $description= "";
31   var $orig_dn= "";
33   var $inheritTimeServer = true;
35   /* Plugin side filled */
36   var $modes= array();
38   /* attribute list for save action */
39   var $ignore_account= TRUE;
40   var $attributes= array("gotoMode", "gotoTerminalPath", 
41       "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
42       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
43       "ghCpuType", "ghMemSize","ghUsbSupport", "description",
44       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
45   var $objectclasses= array("top", "gotoTerminal", "GOhard");
47   var $validActions   = array("reboot" => "", "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
49   var $fai_activated = FALSE;
50   var $view_logged = FALSE;
52   var $member_of_ogroup = FALSE;
54   function termgeneric (&$config, $dn= NULL, $parent= NULL)
55   {
56     /* Check if FAI is activated */
57     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
58     if(!empty($tmp)){
59       $this->fai_activated = TRUE;
60     }
62     plugin::plugin ($config, $dn, $parent);
64     if(!isset($this->parent->by_object['ogroup'])){
65       $ldap = $this->config->get_ldap_link();
66       $ldap->cd ($this->config->current['BASE']);
67       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
68       $this->member_of_ogroup = $ldap->count() >= 1;
69     }
71     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
72     /* Read arrays */
73     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
74       if (!isset($this->attrs[$val])){
75         continue;
76       }
77       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
78         array_push($this->$val, $this->attrs[$val][$i]);
79       }
80     }
82     /* Create used ntp server array */
83     $this->gotoNtpServer= array();
84     if(isset($this->attrs['gotoNtpServer'])){
85       $this->inheritTimeServer = false;
86       unset($this->attrs['gotoNtpServer']['count']);
87       foreach($this->attrs['gotoNtpServer'] as $server){
88         $this->gotoNtpServer[$server] = $server;
89       }
90     }
92     /* Set inherit checkbox state */
93     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
94       $this->inheritTimeServer = true;
95       $this->gotoNtpServer=array();
96     }
98     /* You can't inherit the NTP service, if we are not member in an object group */
99     if(!$this->member_of_ogroup){
100       $this->inheritTimeServer = FALSE;
101     }
103     /* Create available ntp options */
104     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
105     foreach($this->gotoNtpServers as $key => $server){
106       if($server == "default"){
107         unset($this->gotoNtpServers[$key]);
108       }
109     }
111     $this->modes["locked"]= _("Locked");
112     $this->modes["active"]= _("Activated");
114     /* Set base */
115     if ($this->dn == "new"){
116       $ui= get_userinfo();
117       $this->base= dn2base($ui->dn);
118     } else {
119       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
120     }
122     /* Create an array of all Syslog servers */
123     $tmp = $this->config->data['SERVERS']['SYSLOG'];
124     foreach($tmp as $server){
125       $visible = $server;
126       if($server == "default" && $this->member_of_ogroup) {
127         $visible = "["._("inherited")."]";
128       }
129       $this->gotoSyslogServers[$server] = $visible;
130     }
132     $this->orig_dn= $this->dn;
133   }
135   function set_acl_base($base)
136   {
137     plugin::set_acl_base($base);
138     $this->netConfigDNS->set_acl_base($base);
139   }
141   function set_acl_category($cat)
142   {
143     plugin::set_acl_category($cat);
144     $this->netConfigDNS->set_acl_category($cat);
145   }
147   function execute()
148   {
149     /* Call parent execute */
150     plugin::execute();
152     if($this->is_account && !$this->view_logged){
153       $this->view_logged = TRUE;
154       new log("view","terminal/".get_class($this),$this->dn);
155     }
157     /* Do we need to flip is_account state? */
158     if (isset($_POST['modify_state'])){
159       $this->is_account= !$this->is_account;
160     }
162     if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->validActions[$_POST['saction']])){
163       $action = $_POST['saction'];
165       /* Check if we have an DaemonEvent for this action */
166       if(class_available("DaemonEvent_".$action)){
167         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
168         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
169           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
170           $tmp = new $evt['CLASS_NAME']($this->config);
171           $tmp->add_targets(array($this->netConfigDNS->macAddress));
172           $tmp->set_type(TRIGGERED_EVENT);
173           $o_queue = new gosaSupportDaemon();
174           if(!$o_queue->append($tmp)){
175             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
176                   $o_queue->get_error()),ERROR_DIALOG);
177           }
178         }
179       }else{
180         msg_dialog::display(_("Event error"),
181                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
182       }
184     }
186     /* Base select dialog */
187     $once = true;
188     foreach($_POST as $name => $value){
189       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
190         $once = false;
191         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
192         $this->dialog->setCurrentBase($this->base);
193       }
194     }
196     /* Dialog handling */
197     if(is_object($this->dialog)){
198       /* Must be called before save_object */
199       $this->dialog->save_object();
201       if($this->dialog->isClosed()){
202         $this->dialog = false;
203       }elseif($this->dialog->isSelected()){
205         /* A new base was selected, check if it is a valid one */
206         $tmp = $this->get_allowed_bases();
207         if(isset($tmp[$this->dialog->isSelected()])){
208           $this->base = $this->dialog->isSelected();
209         }
210         $this->dialog= false;
211       }else{
212         return($this->dialog->execute());
213       }
214     }
216     /* Do we represent a valid terminal? */
217     if (!$this->is_account && $this->parent === NULL){
218       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
219         _("This 'dn' has no terminal features.")."</b>";
220       return($display);
221     }
223     /* Add new ntp Server to our list */
224     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
225       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
226     }
228     /* Delete selected NtpServer for list of used servers  */
229     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
230       foreach($_POST['gotoNtpServerSelected'] as $name){
231         unset($this->gotoNtpServer[$name]);
232       } 
233     }
235     /* Fill templating stuff */
236     $smarty= get_smarty();
237     
238     $tmp = $this->plInfo();
239     foreach($tmp['plProvidedAcls'] as $name => $translation){
240       $smarty->assign($name."ACL",$this->getacl($name));
241     }
243     $smarty->assign("cn", $this->cn);
244     $smarty->assign("description", $this->description);
245     $smarty->assign("staticAddress", "");
247     $smarty->assign("bases", $this->get_allowed_bases());
249     /* tell smarty the inherit checkbox state */
250     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
252     /* Check if terminal is online */
253     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
254       $smarty->assign("actions", array( "halt" => _("Switch off"), 
255                                         "reboot" => _("Reboot"),
256                                         "memcheck" => _("Memory test"),
257                                         "sysinfo"  => _("System analysis")));
258     } else {
259       $smarty->assign("actions", array("wake" => _("Wake up"),
260                                        "memcheck" => _("Memory test"),
261                                        "sysinfo"  => _("System analysis")));
262     }
264     /* Arrays */
265     $smarty->assign("modes", $this->modes);
267     $tmp2 = array(); 
268     foreach($this->config->data['SERVERS']['NFS'] as $server){
269       if($server != "default"){
270         $tmp2[$server]= $server;
271       }else{
272         if($this->member_of_ogroup){
273           $tmp2[$server]="["._("inherited")."]";
274         }
275       }
276     }
277   
278     $smarty->assign("nfsservers",     $tmp2);
279     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
281     $tmp = array();
282     foreach($this->gotoNtpServers as $server){
283       if(!in_array($server,$this->gotoNtpServer)){
284         $tmp[$server] = $server;
285       }
286     }
287     
288     $smarty->assign("ntpservers",     $tmp);
289     $smarty->assign("fai_activated",$this->fai_activated);
291     /* Variables */
292     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
293       $smarty->assign($val."_select", $this->$val);
294     }
296     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
298     /* Show main page */
299     $str = $this->netConfigDNS->execute();
300     if(is_object($this->netConfigDNS->dialog)){
301       return($str);
302     }
303     $smarty->assign("netconfig", $str);
304     return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
305   }
307   function remove_from_parent()
308   {
309     if($this->acl_is_removeable()){   
310       $ldap= $this->config->get_ldap_link();
311       $ldap->cd($this->dn);
312       $ldap->cat($this->dn, array('dn'));
313       if($ldap->count()){
314         $this->netConfigDNS->remove_from_parent();
315         $ldap->rmDir($this->dn);
316   
317         new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
318   
319         if (!$ldap->success()){
320           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
321         }
323         /* Optionally execute a command after we're done */
324         $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
326         /* Delete references to object groups */
327         $ldap->cd ($this->config->current['BASE']);
328         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
329         while ($ldap->fetch()){
330           $og= new ogroup($this->config, $ldap->getDN());
331           unset($og->member[$this->dn]);
332           $og->save ();
333         }
334       }
336       /* Clean queue form entries with this mac 
337        */
338       if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
339         $q = new gosaSupportDaemon();
340         $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
341       }
342     }
343   }
346   /* Save data to object */
347   function save_object()
348   {
349     /* Create a base backup and reset the
350        base directly after calling plugin::save_object();
351        Base will be set seperatly a few lines below */
352     $base_tmp = $this->base;
353     plugin::save_object();
354     $this->base = $base_tmp;
356     /* Set new base if allowed */
357     $tmp = $this->get_allowed_bases();
358     if(isset($_POST['base'])){
359       if(isset($tmp[$_POST['base']])){
360         $this->base= $_POST['base'];
361       }
362     }
363     
364     $this->netConfigDNS->save_object();
366     /* Save terminal path to parent since it is used by termstartup, too */
367     if(isset($this->parent->by_object['termstartup'])){
368       $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
369     }
370     
371     if(isset($_POST['termgeneric_posted'])){
372       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
373         $this->inheritTimeServer = true;
374       }else{
375         $this->inheritTimeServer = false;
376       }
377     }  
379     if(isset($_POST["inheritAll"])){
380       $this->set_everything_to_inherited();
381     }
383   }
386   /* Check supplied data */
387   function check()
388   {
389     /* Call common method to give check the hook */
390     $message= plugin::check();
392     /* Skip IP & Mac checks if this is a template */
393     if($this->cn != "default"){
394       $message= array_merge($message, $this->netConfigDNS->check());
395     }
397     /* Permissions for that base? */
398     $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
400     if ($this->cn == ""){
401       $message[]= _("The required field 'Terminal name' is not set.");
402     }
404     /* Check if given name is a valid host/dns name */
405     if(!tests::is_dns_name($this->cn) ){
406       $message[] = _("Please specify a valid name for this object.");
407     }
409     if ($this->orig_dn == 'new'){
410       $ldap= $this->config->get_ldap_link();
411       $ldap->cd ($this->base);
413       /* It is possible to have a 'default' terminal on every base */
414       if($this->cn == "default"){
415         $ldap->cat($this->dn);
416       }else{
417         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
418       }
419       if ($ldap->count() != 0){
420         while ($attrs= $ldap->fetch()){
421           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
422             continue;
423           } else {
424             if ($attrs['dn'] != $this->orig_dn){
425               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
426               break;
427             }
428           }
429         }
430       }
431     }
433     /* Check for valid ntpServer selection */
434     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
435       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
436     }
438     return ($message);
439   }
442   /* Save to LDAP */
443   function save()
444   {
446     /* Move object if requested */
447     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
448       $this->move($this->orig_dn, $this->dn);
449     }
451     /* Detect mode changes */
452     $activate= (isset($this->saved_attributes['gotoMode']) &&
453         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
454         $this->gotoMode == "enabled" &&
455         tests::is_ip($this->netConfigDNS->ipHostNumber));
457     plugin::save();
459     /* Strip out 'default' values */
460     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
461       if(isset($this->attrs[$val])){
462         if ($this->attrs[$val] == "default"){
463           $this->attrs[$val]= array();
464         }
465       }
466     }
468     /* Add missing arrays */
469     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
470       if (isset ($this->$val) && count ($this->$val) != 0){
471         $this->attrs["$val"]= $this->$val;
472       }
473     }
475     /* Remove all empty values */
476     if ($this->orig_dn == 'new'){
477       $attrs= array();
478       foreach ($this->attrs as $key => $val){
479         if (is_array($val) && count($val) == 0){
480           continue;
481         }
482         $attrs[$key]= $val;
483       }
484       $this->attrs= $attrs;
485     }
487     /* Set ntpServers */
488     $this->attrs['gotoNtpServer'] = array();
489     if(!$this->inheritTimeServer){
490       foreach($this->gotoNtpServer as $server){
491         $this->attrs['gotoNtpServer'][] = $server;
492       }
493     }
495     /* Write back to ldap */
496     $ldap= $this->config->get_ldap_link();
497     if ($this->orig_dn == 'new'){
498       $ldap->cd($this->config->current['BASE']);
499       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
500       $ldap->cd($this->dn);
501       if (!count($this->attrs['gotoNtpServer'])){
502         unset($this->attrs['gotoNtpServer']);
503       }
504       $ldap->add($this->attrs);
505       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
506       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
507     } else {
508       $ldap->cd($this->dn);
509       $this->cleanup();
510       $ldap->modify ($this->attrs); 
511       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
512       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
513     }
514     
515     /* cn=default and macAddress=- indicates that this is a template */
516     if($this->cn == "default"){
517       $this->netConfigDNS->macAddress = "-";
518     }
520     $this->netConfigDNS->cn = $this->cn;
521     $this->netConfigDNS->save();
522     if (!$ldap->success()){
523       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
524     }
526     /* Send installation activation
527      */
528     if ($activate && class_available("DaemonEvent")){
529       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
530       $o_queue = new gosaSupportDaemon();
531       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
532         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
533         $tmp = new $evt['CLASS_NAME']($this->config);
534         $tmp->set_type(TRIGGERED_EVENT);
535         $tmp->add_targets(array($this->netConfigDNS->macAddress));
536         if(!$o_queue->append($tmp)){
537           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
538                 $o_queue->get_error()),ERROR_DIALOG);
539         }
540       }
541     }
542   }
545   /* Display generic part for server copy & paste */
546   function getCopyDialog()
547   {
548     $vars = array("cn");
549     $smarty = get_smarty();
550     $smarty->assign("cn" ,$this->cn);
551     $smarty->assign("object","terminal");
552     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
553     $ret = array();
554     $ret['string'] = $str;
555     $ret['status'] = "";
556     return($ret);
557   }
560   function saveCopyDialog()
561   {
562     if(isset($_POST['cn'])){
563       $this->cn = $_POST['cn'];
564     }
565   }
568   function PrepareForCopyPaste($source)
569   {
570     plugin::PrepareForCopyPaste($source);
571     if(isset($source['macAddress'][0])){
572       $this->netConfigDNS->macAddress = $source['macAddress'][0];
573     }
574     if(isset($source['ipHostNumber'][0])){
575       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
576     }
578     /* Create used ntp server array */
579     $this->gotoNtpServer= array();
580     if(isset($source['gotoNtpServer'])){
581       $this->inheritTimeServer = false;
582       unset($source['gotoNtpServer']['count']);
583       foreach($source['gotoNtpServer'] as $server){
584         $this->gotoNtpServer[$server] = $server;
585       }
586     }
588     /* Set inherit checkbox state */
589     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
590       $this->inheritTimeServer = true;
591       $this->gotoNtpServer=array();
592     }
593   }
596   /* Return plugin informations for acl handling */
597   static function plInfo()
598   {
599     return (array(
600           "plShortName"   => _("Terminal"),
601           "plDescription" => _("Terminal generic"),
602           "plSelfModify"  => FALSE,
603           "plDepends"     => array(),
604           "plPriority"    => 1,
605           "plSection"     => array("administration"),
606           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
607                                                         "objectClass"  => "gotoTerminal")),
608           "plProvidedAcls"=> array(
609             "gotoMode"            => _("Mode"),
610             "gotoTerminalPath"    => _("Root server"),
611             "gotoSwapServer"      => _("Swap server"),
612             "gotoSyslogServer"    => _("Syslog server enabled"),
613             "gotoNtpServer"       => _("Ntp server settings"),
614             "base"                => _("Base"),
615             "cn"                  => _("Name"),
616             "description"         => _("Description"),
617             "gotoRootPasswd"      => _("Root password"),
618             "FAIstate"            => _("Action flag"))
619           ));
620   }
623   function set_everything_to_inherited()
624   {
625     $this->gotoTerminalPath  = "default";
626     $this->gotoSwapServer    = "default" ;
627     $this->gotoSyslogServer  = "default";
628     $this->inheritTimeServer = TRUE;
630     /* Set workstation service attributes to inherited */
631     if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
632       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
633             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
634         $this->parent->by_object['termservice']->$name = "default";
635       }
636     }
638     /* Set workstation startup attributes to inherited */
639     if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
640       $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
641       $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
642     }
643   }
646 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
647 ?>