Code

Unified FAIstate handling. Moved to gosa-si
[gosa.git] / gosa-plugins / systems / admin / systems / class_servGeneric.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class servgeneric extends plugin
24 {
25   /* Generic terminal attributes */
26   var $ignore_account= TRUE;
27   var $interfaces= array();
29   /* Needed values and lists */
30   var $base= "";
31   var $cn= "";
32   var $l= "";
33   var $description= "";
34   var $orig_dn= "";
36   /* attribute list for save action */
37   var $attributes= array("cn", "description","gotoMode");
38   var $objectclasses= array("top", "GOhard", "goServer");
40   var $gotoMode= "locked";
42   var $netConfigDNS;
43   var $modes = array();
44   var $ui   ;
45   var $validActions   = array("reboot" => "", "update" => "", "localboot" => "", "reinstall" => "", "rescan" => "",
46                             "wake" => "", "memcheck" => "", "sysinfo" => "");
48   var $fai_activated  =FALSE;
49   var $view_logged = FALSE;
51   function servgeneric (&$config, $dn= NULL, $parent= NULL)
52   {
53     /* Check if FAI is activated */
54     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
55     
56     if(!empty($tmp)){
57       $this->fai_activated = TRUE;
58     }
60     plugin::plugin ($config, $dn, $parent);
62     $this->ui = get_userinfo();
63     $this->modes["active"]= _("Activated");
64     $this->modes["locked"]= _("Locked");
66     /* Set base */
67     if ($this->dn == "new"){
68       $ui= get_userinfo();
69       $this->base= dn2base($ui->dn);
70       $this->cn= "";
71     } else {
72       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
73     }
74     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
75     $this->netConfigDNS->set_acl_category("server");
76     $this->netConfigDNS->set_acl_base($this->base);
77        
78     /* Save dn for later references */
79     $this->orig_dn= $this->dn;
80   }
83   function set_acl_base($base)
84   {
85     plugin::set_acl_base($base);
86     $this->netConfigDNS->set_acl_base($base);
87   }
90   function set_acl_category($cat)
91   {
92     plugin::set_acl_category($cat);
93     $this->netConfigDNS->set_acl_category($cat);
94   }
97   function execute()
98   {
99     /* Call parent execute */
100     plugin::execute();
102     if($this->is_account && !$this->view_logged){
103       $this->view_logged = TRUE;
104       new log("view","server/".get_class($this),$this->dn);
105     }
107     /* Do we represent a valid server? */
108     if (!$this->is_account && $this->parent === NULL){
109       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
110         _("This 'dn' has no server features.")."</b>";
111       return($display);
112     }
116     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
117       $action = $_POST['saction'];
118       
119       /* Check if we have an DaemonEvent for this action */
120       if(class_available("DaemonEvent")){
121         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
122         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
123           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
124           $tmp = new $evt['CLASS_NAME']($this->config);
125           $tmp->add_targets(array($this->netConfigDNS->macAddress));
126           $tmp->set_type(TRIGGERED_EVENT);
127           $o_queue = new gosaSupportDaemon();
128           if(!$o_queue->append($tmp)){
129             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
130                   $o_queue->get_error()),ERROR_DIALOG);
131           }
132         }
133       }else{
134         msg_dialog::display(_("Event error"),
135                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
136       }
138     }
140     /* Base select dialog */
141     $once = true;
142     foreach($_POST as $name => $value){
143       if(preg_match("/^chooseBase/",$name) && $once ){
144         $once = false;
145         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
146         $this->dialog->setCurrentBase($this->base);
147       }
148     }
150     /* Dialog handling */
151     if(is_object($this->dialog)){
152       /* Must be called before save_object */
153       $this->dialog->save_object();
155       if($this->dialog->isClosed()){
156         $this->dialog = false;
157       }elseif($this->dialog->isSelected()){
158   
159         /* Only accept allowed bases */
160         $tmp = $this->get_allowed_bases();
161         if($tmp[$this->dialog->isSelected()]){
162           $this->base = $this->dialog->isSelected();
163         }
164         $this->dialog= false;
165       }else{
166         return($this->dialog->execute());
167       }
168     }
170     /* Fill templating stuff */
171     $smarty= get_smarty();
173     $tmp = $this->plInfo();
174     foreach($tmp['plProvidedAcls'] as $name => $translated){
175       $smarty->assign($name."ACL",$this->getacl($name));
176     }
178     /* Assign base ACL */
179     $smarty->assign("bases"   , $this->get_allowed_bases());
181     /* Assign attributes */
182     foreach ($this->attributes as $attr){
183       $smarty->assign("$attr", $this->$attr);
184     }
186     $smarty->assign("staticAddress", "");
187     $smarty->assign("base_select", $this->base);
189     /* Assign status */
190     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
191       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
192                                        "instant_update" => _("Instant update"),
193                                        "update" => _("Scheduled update"),
194                                        "reinstall" => _("Reinstall"),
195                                        "rescan" => _("Rescan hardware"),
196                                        "memcheck" => _("Memory test"),
197                                        "localboot" => _("Force localboot"),
198                                        "sysinfo"  => _("System analysis")));
199     } else {
200       $smarty->assign("actions", array("wake" => _("Wake up"),
201                                        "reinstall" => _("Reinstall"),
202                                        "update" => _("Scheduled update"),
203                                        "memcheck" => _("Memory test"),
204                                        "localboot" => _("Force localboot"),
205                                        "sysinfo"  => _("System analysis")));
206     }
208     /* Show main page */
209     $smarty->assign("fai_activated",$this->fai_activated);
211     $str = $this->netConfigDNS->execute();
212     if(is_object($this->netConfigDNS->dialog)){
213       return($str);
214     }
215     $smarty->assign("netconfig", $str);
216     $smarty->assign("modes", $this->modes);
218     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
219   }
221   function remove_from_parent()
222   {
223     /* Cancel if there's nothing to do here */
224     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
225       return;
226     }
228     $this->netConfigDNS->remove_from_parent();
229     $ldap= $this->config->get_ldap_link();
230     $ldap->rmdir($this->dn);
232     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
234     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
236     /* Delete references to object groups */
237     $ldap->cd ($this->config->current['BASE']);
238     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
239     while ($ldap->fetch()){
240       $og= new ogroup($this->config, $ldap->getDN());
241       unset($og->member[$this->dn]);
242       $og->save ();
243     }
244     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
245   }
248   /* Save data to object */
249   function save_object()
250   {
251     /* Save current base, to be able to revert to last base, 
252         if new base is invalid or not allowed to be selected */
253     $base_tmp = $this->base;
254     plugin::save_object();
255     $this->netConfigDNS->save_object();
257     /* Get base selection */
258     $tmp = $this->get_allowed_bases();
259     if(isset($_POST['base'])){
260       if($tmp[$_POST['base']]){
261         $this->base = $_POST['base'];
262       }else{
263         $this->base = $base_tmp;
264       }
265     }
266   }
269   /* Check supplied data */
270   function check()
271   {
272     /* Call common method to give check the hook */
273     $message= plugin::check();
274     $message= array_merge($message, $this->netConfigDNS->check());
275     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
277     /* must: cn */
278     if ($this->cn == ""){
279       msgPool::required(_("Server name"));
280     }
281     
282     /* Check if given name is a valid host/dns name */
283     if(!tests::is_dns_name($this->cn)){
284       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
285     }
287     if ($this->orig_dn != $this->dn){
288       $ldap= $this->config->get_ldap_link();
289       $ldap->cd ($this->base);
290       $ldap->search ("(cn=".$this->cn.")", array("cn"));
291       if ($ldap->count() != 0){
292         while ($attrs= $ldap->fetch()){
293           if ($attrs['dn'] != $this->orig_dn){
294             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
295               $message[]= msgPool::duplicated(_("Server name"));
296               break;
297             }
298           }
299         }
300       }
301     }
303     return ($message);
304   }
307   /* Save to LDAP */
308   function save()
309   {
310     /* Detect mode changes */
311     $activate= (isset($this->saved_attributes['gotoMode']) &&
312         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
313         $this->gotoMode == "active" &&
314         tests::is_ip($this->netConfigDNS->ipHostNumber));
316     plugin::save();
318     /* Remove all empty values */
319     if ($this->orig_dn == 'new'){
320       $attrs= array();
321       foreach ($this->attrs as $key => $val){
322         if (is_array($val) && count($val) == 0){
323           continue;
324         }
325         $attrs[$key]= $val;
326       }
327       $this->attrs= $attrs;
328     }
330     /* Write back to ldap */
331     $ldap= $this->config->get_ldap_link();
332     if ($this->orig_dn == 'new'){
333       $ldap->cd($this->config->current['BASE']);
334       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
335       $ldap->cd($this->dn);
336       $ldap->add($this->attrs);
337       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
338       $mode= "add";
339     } else {
340    
341       /* cn is not case sensitive for ldap, but for php it is!! */ 
342       if($this->config->current['DNMODE'] == "cn"){
343         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
344           $this->recursive_move($this->orig_dn, $this->dn);
345           plugin::save();
346         }
347       }else{
348         if ($this->orig_dn != $this->dn){
349           $this->recursive_move($this->orig_dn, $this->dn);
350           plugin::save();
351         }
352       }
353   
354       $ldap->cd($this->dn);
355       $this->cleanup();
356       $ldap->modify ($this->attrs); 
357       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
359       $mode= "modify";
360     }
361     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
362     $this->netConfigDNS->cn = $this->cn;
363     $this->netConfigDNS->save();
365     /* Optionally execute a command after we're done */
366     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
368     if ($activate){
370       /* Send installation activation 
371        */
372       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
373       $o_queue = new gosaSupportDaemon();
374       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
375         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
376         $tmp = new $evt['CLASS_NAME']($this->config);
377         $tmp->set_type(TRIGGERED_EVENT);
378         $tmp->add_targets(array($this->netConfigDNS->macAddress));
379         if(!$o_queue->append($tmp)){
380           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
381                 $o_queue->get_error()),ERROR_DIALOG);
382         }
383       }
384     }
386   }
389   /* Display generic part for server copy & paste */
390   function getCopyDialog()
391   {
392     $vars = array("cn");
394     $smarty = get_smarty();
395     $smarty->assign("cn" ,$this->cn);
396     $smarty->assign("object","server");
397     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
398     $ret = array();
399     $ret['string'] = $str;
400     $ret['status'] = "";
401     return($ret);
402   }
405   function saveCopyDialog()
406   {
407     if(isset($_POST['cn'])){
408       $this->cn = $_POST['cn'];
409     }
410   }
412   
413   function PrepareForCopyPaste($source)  
414   {
415     plugin::PrepareForCopyPaste($source);
416     if(isset($source['macAddress'][0])){
417       $this->netConfigDNS->macAddress = $source['macAddress'][0];
418     }
419     if(isset($source['ipHostNumber'][0])){
420       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
421     }
423   }
424   
427   /* Return plugin informations for acl handling */ 
428   static function plInfo()
429   {
430     return (array( 
431           "plShortName"   => _("Generic"),
432           "plDescription" => _("Server generic"),
433           "plSelfModify"  => FALSE,
434           "plDepends"     => array(),
435           "plPriority"    => 1,
436           "plSection"     => array("administration"),           
437           "plCategory"    => array("server" => array("description"  => _("Server"),
438                                                      "objectClass"  => "goServer")),
439           "plProvidedAcls"=> array(
440             "cn"           => _("Name"),
441             "description"  => _("Description"),
442             "gotoMode"     => _("Goto mode"),
443             "base"         => _("Base"),
444             "FAIstate"     => _("Action flag"))
445           ));
446   }
449 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
450 ?>