Code

Updated workstation startup
[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   
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>&nbsp;<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();
231     
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     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
248       $smarty->assign("actions", array( "halt" => _("Switch off"), 
249                                         "reboot" => _("Reboot"),
250                                         "memcheck" => _("Memory test"),
251                                         "sysinfo"  => _("System analysis")));
252     } else {
253       $smarty->assign("actions", array("wake" => _("Wake up"),
254                                        "memcheck" => _("Memory test"),
255                                        "sysinfo"  => _("System analysis")));
256     }
258     /* Arrays */
259     $smarty->assign("modes", $this->modes);
261     $tmp2 = array(); 
262     foreach($this->config->data['SERVERS']['NFS'] as $server){
263       if($server != "default"){
264         $tmp2[$server]= $server;
265       }else{
266         if($this->member_of_ogroup){
267           $tmp2[$server]="["._("inherited")."]";
268         }
269       }
270     }
271   
272     $smarty->assign("nfsservers",     $tmp2);
273     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
275     $tmp = array();
276     foreach($this->gotoNtpServers as $server){
277       if(!in_array($server,$this->gotoNtpServer)){
278         $tmp[$server] = $server;
279       }
280     }
281     
282     $smarty->assign("ntpservers",     $tmp);
283     $smarty->assign("fai_activated",$this->fai_activated);
285     /* Variables */
286     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
287       $smarty->assign($val."_select", $this->$val);
288     }
290     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
292     /* Show main page */
293     $str = $this->netConfigDNS->execute();
294     if(is_object($this->netConfigDNS->dialog)){
295       return($str);
296     }
297     $smarty->assign("netconfig", $str);
298     return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
299   }
301   function remove_from_parent()
302   {
303     if($this->acl_is_removeable()){   
304       $ldap= $this->config->get_ldap_link();
305       $ldap->cd($this->dn);
306       $ldap->cat($this->dn, array('dn'));
307       if($ldap->count()){
308         $this->netConfigDNS->remove_from_parent();
309         $ldap->rmDir($this->dn);
310   
311         new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
312   
313         show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
315         /* Optionally execute a command after we're done */
316         $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
318         /* Delete references to object groups */
319         $ldap->cd ($this->config->current['BASE']);
320         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
321         while ($ldap->fetch()){
322           $og= new ogroup($this->config, $ldap->getDN());
323           unset($og->member[$this->dn]);
324           $og->save ();
325         }
326       }
327     }
328   }
331   /* Save data to object */
332   function save_object()
333   {
334     /* Create a base backup and reset the
335        base directly after calling plugin::save_object();
336        Base will be set seperatly a few lines below */
337     $base_tmp = $this->base;
338     plugin::save_object();
339     $this->base = $base_tmp;
341     /* Set new base if allowed */
342     $tmp = $this->get_allowed_bases();
343     if(isset($_POST['base'])){
344       if(isset($tmp[$_POST['base']])){
345         $this->base= $_POST['base'];
346       }
347     }
348     
349     $this->netConfigDNS->save_object();
351     /* Save terminal path to parent since it is used by termstartup, too */
352     if(isset($this->parent->by_object['termstartup'])){
353       $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
354     }
355     
356     if(isset($_POST['termgeneric_posted'])){
357       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
358         $this->inheritTimeServer = true;
359       }else{
360         $this->inheritTimeServer = false;
361       }
362     }  
364     if(isset($_POST["inheritAll"])){
365       $this->set_everything_to_inherited();
366     }
368   }
371   /* Check supplied data */
372   function check()
373   {
374     /* Call common method to give check the hook */
375     $message= plugin::check();
377     /* Skip IP & Mac checks if this is a template */
378     if($this->cn != "default"){
379       $message= array_merge($message, $this->netConfigDNS->check());
380     }
382     /* Permissions for that base? */
383     $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
385     if ($this->cn == ""){
386       $message[]= _("The required field 'Terminal name' is not set.");
387     }
389     /* Check if given name is a valid host/dns name */
390     if(!tests::is_dns_name($this->cn) ){
391       $message[] = _("Please specify a valid name for this object.");
392     }
394     if ($this->orig_dn == 'new'){
395       $ldap= $this->config->get_ldap_link();
396       $ldap->cd ($this->base);
398       /* It is possible to have a 'default' terminal on every base */
399       if($this->cn == "default"){
400         $ldap->cat($this->dn);
401       }else{
402         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
403       }
404       if ($ldap->count() != 0){
405         while ($attrs= $ldap->fetch()){
406           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
407             continue;
408           } else {
409             if ($attrs['dn'] != $this->orig_dn){
410               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
411               break;
412             }
413           }
414         }
415       }
416     }
418     /* Check for valid ntpServer selection */
419     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
420       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
421     }
423     return ($message);
424   }
427   /* Save to LDAP */
428   function save()
429   {
431     /* Move object if requested */
432     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
433       $this->move($this->orig_dn, $this->dn);
434     }
436     /* Detect mode changes */
437     $activate= (isset($this->saved_attributes['gotoMode']) &&
438         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
439         $this->gotoMode == "enabled" &&
440         tests::is_ip($this->netConfigDNS->ipHostNumber));
442     plugin::save();
444     /* Strip out 'default' values */
445     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
446       if(isset($this->attrs[$val])){
447         if ($this->attrs[$val] == "default"){
448           $this->attrs[$val]= array();
449         }
450       }
451     }
453     /* Add missing arrays */
454     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
455       if (isset ($this->$val) && count ($this->$val) != 0){
456         $this->attrs["$val"]= $this->$val;
457       }
458     }
460     /* Remove all empty values */
461     if ($this->orig_dn == 'new'){
462       $attrs= array();
463       foreach ($this->attrs as $key => $val){
464         if (is_array($val) && count($val) == 0){
465           continue;
466         }
467         $attrs[$key]= $val;
468       }
469       $this->attrs= $attrs;
470     }
472     /* Set ntpServers */
473     $this->attrs['gotoNtpServer'] = array();
474     if(!$this->inheritTimeServer){
475       foreach($this->gotoNtpServer as $server){
476         $this->attrs['gotoNtpServer'][] = $server;
477       }
478     }
480     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
481     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
482       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
483     }
485     /* Write back to ldap */
486     $ldap= $this->config->get_ldap_link();
487     if ($this->orig_dn == 'new'){
488       $ldap->cd($this->config->current['BASE']);
489       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
490       $ldap->cd($this->dn);
491       if (!count($this->attrs['gotoNtpServer'])){
492         unset($this->attrs['gotoNtpServer']);
493       }
494       $ldap->add($this->attrs);
495       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
496       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
497     } else {
498       $ldap->cd($this->dn);
499       $this->cleanup();
500       $ldap->modify ($this->attrs); 
501       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
502       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
503     }
504     
505     /* cn=default and macAddress=- indicates that this is a template */
506     if($this->cn == "default"){
507       $this->netConfigDNS->macAddress = "-";
508     }
510     $this->netConfigDNS->cn = $this->cn;
511     $this->netConfigDNS->save();
512     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
514     if ($activate){
515       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
516     }
517   }
520   /* Display generic part for server copy & paste */
521   function getCopyDialog()
522   {
523     $vars = array("cn");
524     $smarty = get_smarty();
525     $smarty->assign("cn" ,$this->cn);
526     $smarty->assign("object","terminal");
527     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
528     $ret = array();
529     $ret['string'] = $str;
530     $ret['status'] = "";
531     return($ret);
532   }
535   function saveCopyDialog()
536   {
537     if(isset($_POST['cn'])){
538       $this->cn = $_POST['cn'];
539     }
540   }
543   function PrepareForCopyPaste($source)
544   {
545     plugin::PrepareForCopyPaste($source);
546     if(isset($source['macAddress'][0])){
547       $this->netConfigDNS->macAddress = $source['macAddress'][0];
548     }
549     if(isset($source['ipHostNumber'][0])){
550       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
551     }
553     /* Create used ntp server array */
554     $this->gotoNtpServer= array();
555     if(isset($source['gotoNtpServer'])){
556       $this->inheritTimeServer = false;
557       unset($source['gotoNtpServer']['count']);
558       foreach($source['gotoNtpServer'] as $server){
559         $this->gotoNtpServer[$server] = $server;
560       }
561     }
563     /* Set inherit checkbox state */
564     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
565       $this->inheritTimeServer = true;
566       $this->gotoNtpServer=array();
567     }
568   }
571   /* Return plugin informations for acl handling */
572   static function plInfo()
573   {
574     return (array(
575           "plShortName"   => _("Terminal"),
576           "plDescription" => _("Terminal generic"),
577           "plSelfModify"  => FALSE,
578           "plDepends"     => array(),
579           "plPriority"    => 1,
580           "plSection"     => array("administration"),
581           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
582                                                         "objectClass"  => "gotoTerminal")),
583           "plProvidedAcls"=> array(
584             "gotoMode"            => _("Mode"),
585             "gotoTerminalPath"    => _("Root server"),
586             "gotoSwapServer"      => _("Swap server"),
587             "gotoSyslogServer"    => _("Syslog server enabled"),
588             "gotoNtpServer"       => _("Ntp server settings"),
589             "base"                => _("Base"),
590             "cn"                  => _("Name"),
591             "description"         => _("Description"),
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 ?>