Code

Moved to mac based target
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_terminalGeneric.inc
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             gosaSupportDaemon::send("gosa_trigger_action_".$_POST['saction'], $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
171     }
173     /* Base select dialog */
174     $once = true;
175     foreach($_POST as $name => $value){
176       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
177         $once = false;
178         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
179         $this->dialog->setCurrentBase($this->base);
180       }
181     }
183     /* Dialog handling */
184     if(is_object($this->dialog)){
185       /* Must be called before save_object */
186       $this->dialog->save_object();
188       if($this->dialog->isClosed()){
189         $this->dialog = false;
190       }elseif($this->dialog->isSelected()){
192         /* A new base was selected, check if it is a valid one */
193         $tmp = $this->get_allowed_bases();
194         if(isset($tmp[$this->dialog->isSelected()])){
195           $this->base = $this->dialog->isSelected();
196         }
197         $this->dialog= false;
198       }else{
199         return($this->dialog->execute());
200       }
201     }
203     /* Do we represent a valid terminal? */
204     if (!$this->is_account && $this->parent === NULL){
205       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
206         _("This 'dn' has no terminal features.")."</b>";
207       return($display);
208     }
210     /* Add new ntp Server to our list */
211     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
212       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
213     }
215     /* Delete selected NtpServer for list of used servers  */
216     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
217       foreach($_POST['gotoNtpServerSelected'] as $name){
218         unset($this->gotoNtpServer[$name]);
219       } 
220     }
222     /* Fill templating stuff */
223     $smarty= get_smarty();
224     
225     $tmp = $this->plInfo();
226     foreach($tmp['plProvidedAcls'] as $name => $translation){
227       $smarty->assign($name."ACL",$this->getacl($name));
228     }
230     $smarty->assign("cn", $this->cn);
231     $smarty->assign("staticAddress", "");
233     $smarty->assign("bases", $this->get_allowed_bases());
235     /* tell smarty the inherit checkbox state */
236     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
238     /* Check if terminal is online */
239     $query= "fping -q -r 1 -t 500 ".$this->cn;
240     exec ($query, $dummy, $retval);
242     /* Offline */
243     if ($retval == 0){
244       $smarty->assign("actions", array( "halt" => _("Switch off"), 
245                                         "reboot" => _("Reboot"),
246                                         "memcheck" => _("Memory test"),
247                                         "sysinfo"  => _("System analysis")));
248     } else {
249       $smarty->assign("actions", array("wake" => _("Wake up"),
250                                        "memcheck" => _("Memory test"),
251                                        "sysinfo"  => _("System analysis")));
252     }
254     /* Arrays */
255     $smarty->assign("modes", $this->modes);
257     $tmp2 = array(); 
258     foreach($this->config->data['SERVERS']['NFS'] as $server){
259       if($server != "default"){
260         $tmp2[$server]= $server;
261       }else{
262         if($this->member_of_ogroup){
263           $tmp2[$server]="["._("inherited")."]";
264         }
265       }
266     }
267   
268     $smarty->assign("nfsservers",     $tmp2);
269     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
271     $tmp = array();
272     foreach($this->gotoNtpServers as $server){
273       if(!in_array($server,$this->gotoNtpServer)){
274         $tmp[$server] = $server;
275       }
276     }
277     
278     $smarty->assign("ntpservers",     $tmp);
279     $smarty->assign("fai_activated",$this->fai_activated);
281     /* Variables */
282     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
283       $smarty->assign($val."_select", $this->$val);
284     }
286     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
288     /* Show main page */
289     $str = $this->netConfigDNS->execute();
290     if(is_object($this->netConfigDNS->dialog)){
291       return($str);
292     }
293     $smarty->assign("netconfig", $str);
294     return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
295   }
297   function remove_from_parent()
298   {
299     if($this->acl_is_removeable()){   
300       $ldap= $this->config->get_ldap_link();
301       $ldap->cd($this->dn);
302       $ldap->cat($this->dn, array('dn'));
303       if($ldap->count()){
304         $this->netConfigDNS->remove_from_parent();
305         $ldap->rmDir($this->dn);
306   
307         new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
308   
309         show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
311         /* Optionally execute a command after we're done */
312         $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
314         /* Delete references to object groups */
315         $ldap->cd ($this->config->current['BASE']);
316         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
317         while ($ldap->fetch()){
318           $og= new ogroup($this->config, $ldap->getDN());
319           unset($og->member[$this->dn]);
320           $og->save ();
321         }
322       }
323     }
324   }
327   /* Save data to object */
328   function save_object()
329   {
330     /* Create a base backup and reset the
331        base directly after calling plugin::save_object();
332        Base will be set seperatly a few lines below */
333     $base_tmp = $this->base;
334     plugin::save_object();
335     $this->base = $base_tmp;
337     /* Set new base if allowed */
338     $tmp = $this->get_allowed_bases();
339     if(isset($_POST['base'])){
340       if(isset($tmp[$_POST['base']])){
341         $this->base= $_POST['base'];
342       }
343     }
344     
345     $this->netConfigDNS->save_object();
347     /* Save terminal path to parent since it is used by termstartup, too */
348     if(isset($this->parent->by_object['termstartup'])){
349       $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
350     }
351     
352     if(isset($_POST['termgeneric_posted'])){
353       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
354         $this->inheritTimeServer = true;
355       }else{
356         $this->inheritTimeServer = false;
357       }
358     }  
360     if(isset($_POST["inheritAll"])){
361       $this->set_everything_to_inherited();
362     }
364   }
367   /* Check supplied data */
368   function check()
369   {
370     /* Call common method to give check the hook */
371     $message= plugin::check();
373     /* Skip IP & Mac checks if this is a template */
374     if($this->cn != "default"){
375       $message= array_merge($message, $this->netConfigDNS->check());
376     }
378     /* Permissions for that base? */
379     $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
381     if ($this->cn == ""){
382       $message[]= _("The required field 'Terminal name' is not set.");
383     }
385     /* Check if given name is a valid host/dns name */
386     if(!tests::is_dns_name($this->cn) ){
387       $message[] = _("Please specify a valid name for this object.");
388     }
390     if ($this->orig_dn == 'new'){
391       $ldap= $this->config->get_ldap_link();
392       $ldap->cd ($this->base);
394       /* It is possible to have a 'default' terminal on every base */
395       if($this->cn == "default"){
396         $ldap->cat($this->dn);
397       }else{
398         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
399       }
400       if ($ldap->count() != 0){
401         while ($attrs= $ldap->fetch()){
402           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
403             continue;
404           } else {
405             if ($attrs['dn'] != $this->orig_dn){
406               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
407               break;
408             }
409           }
410         }
411       }
412     }
414     /* Check for valid ntpServer selection */
415     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
416       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
417     }
419     return ($message);
420   }
423   /* Save to LDAP */
424   function save()
425   {
427     /* Move object if requested */
428     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
429       $this->move($this->orig_dn, $this->dn);
430     }
432     /* Detect mode changes */
433     $activate= (isset($this->saved_attributes['gotoMode']) &&
434         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
435         $this->gotoMode == "enabled" &&
436         tests::is_ip($this->netConfigDNS->ipHostNumber));
438     plugin::save();
440     /* Strip out 'default' values */
441     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
442       if(isset($this->attrs[$val])){
443         if ($this->attrs[$val] == "default"){
444           $this->attrs[$val]= array();
445         }
446       }
447     }
449     /* Add missing arrays */
450     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
451       if (isset ($this->$val) && count ($this->$val) != 0){
452         $this->attrs["$val"]= $this->$val;
453       }
454     }
456     /* Remove all empty values */
457     if ($this->orig_dn == 'new'){
458       $attrs= array();
459       foreach ($this->attrs as $key => $val){
460         if (is_array($val) && count($val) == 0){
461           continue;
462         }
463         $attrs[$key]= $val;
464       }
465       $this->attrs= $attrs;
466     }
468     /* Set ntpServers */
469     $this->attrs['gotoNtpServer'] = array();
470     if(!$this->inheritTimeServer){
471       foreach($this->gotoNtpServer as $server){
472         $this->attrs['gotoNtpServer'][] = $server;
473       }
474     }
476     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
477     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
478       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
479     }
481     /* Write back to ldap */
482     $ldap= $this->config->get_ldap_link();
483     if ($this->orig_dn == 'new'){
484       $ldap->cd($this->config->current['BASE']);
485       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
486       $ldap->cd($this->dn);
487       if (!count($this->attrs['gotoNtpServer'])){
488         unset($this->attrs['gotoNtpServer']);
489       }
490       $ldap->add($this->attrs);
491       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
492       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
493     } else {
494       $ldap->cd($this->dn);
495       $this->cleanup();
496       $ldap->modify ($this->attrs); 
497       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
498       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
499     }
500     
501     /* cn=default and macAddress=- indicates that this is a template */
502     if($this->cn == "default"){
503       $this->netConfigDNS->macAddress = "-";
504     }
506     $this->netConfigDNS->cn = $this->cn;
507     $this->netConfigDNS->save();
508     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
510     if ($activate){
511       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
512     }
513   }
516   /* Display generic part for server copy & paste */
517   function getCopyDialog()
518   {
519     $vars = array("cn");
520     $smarty = get_smarty();
521     $smarty->assign("cn" ,$this->cn);
522     $smarty->assign("object","terminal");
523     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
524     $ret = array();
525     $ret['string'] = $str;
526     $ret['status'] = "";
527     return($ret);
528   }
531   function saveCopyDialog()
532   {
533     if(isset($_POST['cn'])){
534       $this->cn = $_POST['cn'];
535     }
536   }
539   function PrepareForCopyPaste($source)
540   {
541     plugin::PrepareForCopyPaste($source);
542     if(isset($source['macAddress'][0])){
543       $this->netConfigDNS->macAddress = $source['macAddress'][0];
544     }
545     if(isset($source['ipHostNumber'][0])){
546       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
547     }
549     /* Create used ntp server array */
550     $this->gotoNtpServer= array();
551     if(isset($source['gotoNtpServer'])){
552       $this->inheritTimeServer = false;
553       unset($source['gotoNtpServer']['count']);
554       foreach($source['gotoNtpServer'] as $server){
555         $this->gotoNtpServer[$server] = $server;
556       }
557     }
559     /* Set inherit checkbox state */
560     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
561       $this->inheritTimeServer = true;
562       $this->gotoNtpServer=array();
563     }
564   }
567   /* Return plugin informations for acl handling */
568   static function plInfo()
569   {
570     return (array(
571           "plShortName"   => _("Terminal"),
572           "plDescription" => _("Terminal generic"),
573           "plSelfModify"  => FALSE,
574           "plDepends"     => array(),
575           "plPriority"    => 1,
576           "plSection"     => array("administration"),
577           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
578                                                         "objectClass"  => "gotoTerminal")),
579           "plProvidedAcls"=> array(
580             "gotoMode"            => _("Mode"),
581             "gotoTerminalPath"    => _("Root server"),
582             "gotoSwapServer"      => _("Swap server"),
583             "gotoSyslogServer"    => _("Syslog server enabled"),
584             "gotoNtpServer"       => _("Ntp server settings"),
585             "base"                => _("Base"),
586             "cn"                  => _("Name"),
587             "gotoRootPasswd"      => _("Root password"),
588             "FAIstate"            => _("Action flag"))
589           ));
590   }
593   function set_everything_to_inherited()
594   {
595     $this->gotoTerminalPath  = "default";
596     $this->gotoSwapServer    = "default" ;
597     $this->gotoSyslogServer  = "default";
598     $this->inheritTimeServer = TRUE;
600     /* Set workstation service attributes to inherited */
601     if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
602       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
603             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
604         $this->parent->by_object['termservice']->$name = "default";
605       }
606     }
608     /* Set workstation startup attributes to inherited */
609     if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
610       $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
611       $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
612     }
613   }
616 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
617 ?>