Code

Fixed typo
[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         show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
321         /* Optionally execute a command after we're done */
322         $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
324         /* Delete references to object groups */
325         $ldap->cd ($this->config->current['BASE']);
326         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
327         while ($ldap->fetch()){
328           $og= new ogroup($this->config, $ldap->getDN());
329           unset($og->member[$this->dn]);
330           $og->save ();
331         }
332       }
333     }
334   }
337   /* Save data to object */
338   function save_object()
339   {
340     /* Create a base backup and reset the
341        base directly after calling plugin::save_object();
342        Base will be set seperatly a few lines below */
343     $base_tmp = $this->base;
344     plugin::save_object();
345     $this->base = $base_tmp;
347     /* Set new base if allowed */
348     $tmp = $this->get_allowed_bases();
349     if(isset($_POST['base'])){
350       if(isset($tmp[$_POST['base']])){
351         $this->base= $_POST['base'];
352       }
353     }
354     
355     $this->netConfigDNS->save_object();
357     /* Save terminal path to parent since it is used by termstartup, too */
358     if(isset($this->parent->by_object['termstartup'])){
359       $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
360     }
361     
362     if(isset($_POST['termgeneric_posted'])){
363       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
364         $this->inheritTimeServer = true;
365       }else{
366         $this->inheritTimeServer = false;
367       }
368     }  
370     if(isset($_POST["inheritAll"])){
371       $this->set_everything_to_inherited();
372     }
374   }
377   /* Check supplied data */
378   function check()
379   {
380     /* Call common method to give check the hook */
381     $message= plugin::check();
383     /* Skip IP & Mac checks if this is a template */
384     if($this->cn != "default"){
385       $message= array_merge($message, $this->netConfigDNS->check());
386     }
388     /* Permissions for that base? */
389     $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
391     if ($this->cn == ""){
392       $message[]= _("The required field 'Terminal name' is not set.");
393     }
395     /* Check if given name is a valid host/dns name */
396     if(!tests::is_dns_name($this->cn) ){
397       $message[] = _("Please specify a valid name for this object.");
398     }
400     if ($this->orig_dn == 'new'){
401       $ldap= $this->config->get_ldap_link();
402       $ldap->cd ($this->base);
404       /* It is possible to have a 'default' terminal on every base */
405       if($this->cn == "default"){
406         $ldap->cat($this->dn);
407       }else{
408         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
409       }
410       if ($ldap->count() != 0){
411         while ($attrs= $ldap->fetch()){
412           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
413             continue;
414           } else {
415             if ($attrs['dn'] != $this->orig_dn){
416               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
417               break;
418             }
419           }
420         }
421       }
422     }
424     /* Check for valid ntpServer selection */
425     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
426       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
427     }
429     return ($message);
430   }
433   /* Save to LDAP */
434   function save()
435   {
437     /* Move object if requested */
438     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
439       $this->move($this->orig_dn, $this->dn);
440     }
442     /* Detect mode changes */
443     $activate= (isset($this->saved_attributes['gotoMode']) &&
444         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
445         $this->gotoMode == "enabled" &&
446         tests::is_ip($this->netConfigDNS->ipHostNumber));
448     plugin::save();
450     /* Strip out 'default' values */
451     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
452       if(isset($this->attrs[$val])){
453         if ($this->attrs[$val] == "default"){
454           $this->attrs[$val]= array();
455         }
456       }
457     }
459     /* Add missing arrays */
460     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
461       if (isset ($this->$val) && count ($this->$val) != 0){
462         $this->attrs["$val"]= $this->$val;
463       }
464     }
466     /* Remove all empty values */
467     if ($this->orig_dn == 'new'){
468       $attrs= array();
469       foreach ($this->attrs as $key => $val){
470         if (is_array($val) && count($val) == 0){
471           continue;
472         }
473         $attrs[$key]= $val;
474       }
475       $this->attrs= $attrs;
476     }
478     /* Set ntpServers */
479     $this->attrs['gotoNtpServer'] = array();
480     if(!$this->inheritTimeServer){
481       foreach($this->gotoNtpServer as $server){
482         $this->attrs['gotoNtpServer'][] = $server;
483       }
484     }
486     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
487     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
488       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
489     }
491     /* Write back to ldap */
492     $ldap= $this->config->get_ldap_link();
493     if ($this->orig_dn == 'new'){
494       $ldap->cd($this->config->current['BASE']);
495       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
496       $ldap->cd($this->dn);
497       if (!count($this->attrs['gotoNtpServer'])){
498         unset($this->attrs['gotoNtpServer']);
499       }
500       $ldap->add($this->attrs);
501       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
502       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
503     } else {
504       $ldap->cd($this->dn);
505       $this->cleanup();
506       $ldap->modify ($this->attrs); 
507       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
508       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
509     }
510     
511     /* cn=default and macAddress=- indicates that this is a template */
512     if($this->cn == "default"){
513       $this->netConfigDNS->macAddress = "-";
514     }
516     $this->netConfigDNS->cn = $this->cn;
517     $this->netConfigDNS->save();
518     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
520     /* Send installation activation
521      */
522     if ($activate && class_available("DaemonEvent")){
523       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
524       $o_queue = new gosaSupportDaemon();
525       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
526         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
527         $tmp = new $evt['CLASS_NAME']($this->config);
528         $tmp->set_type(TRIGGERED_EVENT);
529         $tmp->add_targets(array($this->netConfigDNS->macAddress));
530         if(!$o_queue->append($tmp)){
531           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
532                 $o_queue->get_error()),ERROR_DIALOG);
533         }
534       }
535     }
536   }
539   /* Display generic part for server copy & paste */
540   function getCopyDialog()
541   {
542     $vars = array("cn");
543     $smarty = get_smarty();
544     $smarty->assign("cn" ,$this->cn);
545     $smarty->assign("object","terminal");
546     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
547     $ret = array();
548     $ret['string'] = $str;
549     $ret['status'] = "";
550     return($ret);
551   }
554   function saveCopyDialog()
555   {
556     if(isset($_POST['cn'])){
557       $this->cn = $_POST['cn'];
558     }
559   }
562   function PrepareForCopyPaste($source)
563   {
564     plugin::PrepareForCopyPaste($source);
565     if(isset($source['macAddress'][0])){
566       $this->netConfigDNS->macAddress = $source['macAddress'][0];
567     }
568     if(isset($source['ipHostNumber'][0])){
569       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
570     }
572     /* Create used ntp server array */
573     $this->gotoNtpServer= array();
574     if(isset($source['gotoNtpServer'])){
575       $this->inheritTimeServer = false;
576       unset($source['gotoNtpServer']['count']);
577       foreach($source['gotoNtpServer'] as $server){
578         $this->gotoNtpServer[$server] = $server;
579       }
580     }
582     /* Set inherit checkbox state */
583     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
584       $this->inheritTimeServer = true;
585       $this->gotoNtpServer=array();
586     }
587   }
590   /* Return plugin informations for acl handling */
591   static function plInfo()
592   {
593     return (array(
594           "plShortName"   => _("Terminal"),
595           "plDescription" => _("Terminal generic"),
596           "plSelfModify"  => FALSE,
597           "plDepends"     => array(),
598           "plPriority"    => 1,
599           "plSection"     => array("administration"),
600           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
601                                                         "objectClass"  => "gotoTerminal")),
602           "plProvidedAcls"=> array(
603             "gotoMode"            => _("Mode"),
604             "gotoTerminalPath"    => _("Root server"),
605             "gotoSwapServer"      => _("Swap server"),
606             "gotoSyslogServer"    => _("Syslog server enabled"),
607             "gotoNtpServer"       => _("Ntp server settings"),
608             "base"                => _("Base"),
609             "cn"                  => _("Name"),
610             "description"         => _("Description"),
611             "gotoRootPasswd"      => _("Root password"),
612             "FAIstate"            => _("Action flag"))
613           ));
614   }
617   function set_everything_to_inherited()
618   {
619     $this->gotoTerminalPath  = "default";
620     $this->gotoSwapServer    = "default" ;
621     $this->gotoSyslogServer  = "default";
622     $this->inheritTimeServer = TRUE;
624     /* Set workstation service attributes to inherited */
625     if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
626       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
627             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
628         $this->parent->by_object['termservice']->$name = "default";
629       }
630     }
632     /* Set workstation startup attributes to inherited */
633     if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
634       $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
635       $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
636     }
637   }
640 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
641 ?>