Code

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