Code

Upated server event
[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= "";
35   var $didAction= FALSE;
37   /* attribute list for save action */
38   var $attributes= array("cn", "description","gotoMode");
39   var $objectclasses= array("top", "GOhard", "goServer");
41   var $gotoMode= "locked";
43   var $netConfigDNS;
44   var $modes = array();
45   var $ui   ;
46   var $mapActions   = array("reboot"          => "",
47                             "update"          => "softupdate",
48                             "localboot"       => "localboot",
49                             "reinstall"       => "install",
50                             "rescan"          => "",
51                             "wake"            => "",
52                             "memcheck"        => "memcheck",
53                             "sysinfo"         => "sysinfo");
55   var $fai_activated  =FALSE;
56   var $view_logged = FALSE;
58   function servgeneric (&$config, $dn= NULL, $parent= NULL)
59   {
60     /* Check if FAI is activated */
61     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
62     
63     if(!empty($tmp)){
64       $this->fai_activated = TRUE;
65     }
67     plugin::plugin ($config, $dn, $parent);
69     $this->ui = get_userinfo();
70     $this->modes["active"]= _("Activated");
71     $this->modes["locked"]= _("Locked");
73     /* Set base */
74     if ($this->dn == "new"){
75       $ui= get_userinfo();
76       $this->base= dn2base($ui->dn);
77       $this->cn= "";
78     } else {
79       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
80     }
81     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
82     $this->netConfigDNS->set_acl_category("server");
83     $this->netConfigDNS->set_acl_base($this->base);
84        
85     /* Save dn for later references */
86     $this->orig_dn= $this->dn;
87   }
90   function set_acl_base($base)
91   {
92     plugin::set_acl_base($base);
93     $this->netConfigDNS->set_acl_base($base);
94   }
97   function set_acl_category($cat)
98   {
99     plugin::set_acl_category($cat);
100     $this->netConfigDNS->set_acl_category($cat);
101   }
104   function execute()
105   {
106     /* Call parent execute */
107     plugin::execute();
109     if($this->is_account && !$this->view_logged){
110       $this->view_logged = TRUE;
111       new log("view","server/".get_class($this),$this->dn);
112     }
114     /* Do we represent a valid server? */
115     if (!$this->is_account && $this->parent === NULL){
116       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
117         _("This 'dn' has no server features.")."</b>";
118       return($display);
119     }
123     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
125       /* Check given action */
126       $mapEvent = array("wake"           => "wakeup",
127                         "instant_update" => "update");
128       $action = $_POST['saction'];
129       if(isset($mapEvent[$action])){
130         $action = $mapEvent[$action];
131       }
133       
134       /* Check if we have an DaemonEvent for this action */
135       if(class_available("DaemonEvent")){
136         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
137         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
138           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
139           $tmp = new $evt['CLASS_NAME']($this->config);
140           $tmp->add_targets(array($this->netConfigDNS->macAddress));
141           $tmp->set_type(TRIGGERED_EVENT);
142           $o_queue = new gosaSupportDaemon();
143           if(!$o_queue->append($tmp)){
144             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
145                   $o_queue->get_error()),ERROR_DIALOG);
146           }
147         }
148       }else{
150         msg_dialog::display(_("Missing Daemon Event"),
151                     sprintf(_("The requested action does not exists '%s' Sending event manually."),"DaemonEvent_".$action),ERROR_DIALOG);
153         /* No event found, send action manually.
154          */
155         $method= "gosa";
156         $action= $_POST['saction'];
157         if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
158           $method= "job";
159         }
161         gosaSupportDaemon::send("${method}_trigger_action_".$action,
162             $this->netConfigDNS->macAddress,
163             array("macAddress" => $this->netConfigDNS->macAddress));
164       }
166       $this->didAction= TRUE;
168       if ($_POST['saction'] != "wake") {
170           /* Set FAIstate */
171           if($this->fai_activated && $this->dn != "new"){
172             $ldap = $this->config->get_ldap_link();
173             $ldap->cd($this->config->current['BASE']);
174             $ldap->cat($this->dn,array("objectClass"));
175             $res = $ldap->fetch();
177             $attrs = array();
178             $attrs['FAIstate'] = "";
179             if(isset($this->mapActions[$_POST['saction']])){
180               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
181             }
183             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
184               $attrs['objectClass'][] = $res['objectClass'][$i];
185             }
187             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
188               $attrs['objectClass'][] = "FAIobject";
189             }
191             if($attrs['FAIstate'] == ""){
192               #FIXME we should check if FAIobject is used anymore
193               $attrs['FAIstate'] = array();
194             }
196             $ldap->cd($this->dn);
197             $ldap->modify($attrs);
198           }
199           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
200       }
201     }
203     /* Base select dialog */
204     $once = true;
205     foreach($_POST as $name => $value){
206       if(preg_match("/^chooseBase/",$name) && $once ){
207         $once = false;
208         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
209         $this->dialog->setCurrentBase($this->base);
210       }
211     }
213     /* Dialog handling */
214     if(is_object($this->dialog)){
215       /* Must be called before save_object */
216       $this->dialog->save_object();
218       if($this->dialog->isClosed()){
219         $this->dialog = false;
220       }elseif($this->dialog->isSelected()){
221   
222         /* Only accept allowed bases */
223         $tmp = $this->get_allowed_bases();
224         if($tmp[$this->dialog->isSelected()]){
225           $this->base = $this->dialog->isSelected();
226         }
227         $this->dialog= false;
228       }else{
229         return($this->dialog->execute());
230       }
231     }
233     /* Fill templating stuff */
234     $smarty= get_smarty();
236     $tmp = $this->plInfo();
237     foreach($tmp['plProvidedAcls'] as $name => $translated){
238       $smarty->assign($name."ACL",$this->getacl($name));
239     }
241     /* Assign base ACL */
242     $smarty->assign("bases"   , $this->get_allowed_bases());
244     /* Assign attributes */
245     foreach ($this->attributes as $attr){
246       $smarty->assign("$attr", $this->$attr);
247     }
249     $smarty->assign("staticAddress", "");
250     $smarty->assign("base_select", $this->base);
252     /* Assign status */
253     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
254       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
255                                        "instant_update" => _("Instant update"),
256                                        "update" => _("Scheduled update"),
257                                        "reinstall" => _("Reinstall"),
258                                        "rescan" => _("Rescan hardware"),
259                                        "memcheck" => _("Memory test"),
260                                        "localboot" => _("Force localboot"),
261                                        "sysinfo"  => _("System analysis")));
262     } else {
263       $smarty->assign("actions", array("wake" => _("Wake up"),
264                                        "reinstall" => _("Reinstall"),
265                                        "update" => _("Scheduled update"),
266                                        "memcheck" => _("Memory test"),
267                                        "localboot" => _("Force localboot"),
268                                        "sysinfo"  => _("System analysis")));
269     }
271     /* Show main page */
272     $smarty->assign("fai_activated",$this->fai_activated);
274     $str = $this->netConfigDNS->execute();
275     if(is_object($this->netConfigDNS->dialog)){
276       return($str);
277     }
278     $smarty->assign("netconfig", $str);
279     $smarty->assign("modes", $this->modes);
281     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
282   }
284   function remove_from_parent()
285   {
286     /* Cancel if there's nothing to do here */
287     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
288       return;
289     }
291     $this->netConfigDNS->remove_from_parent();
292     $ldap= $this->config->get_ldap_link();
293     $ldap->rmdir($this->dn);
295     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
297     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
299     /* Delete references to object groups */
300     $ldap->cd ($this->config->current['BASE']);
301     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
302     while ($ldap->fetch()){
303       $og= new ogroup($this->config, $ldap->getDN());
304       unset($og->member[$this->dn]);
305       $og->save ();
306     }
307     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
308   }
311   /* Save data to object */
312   function save_object()
313   {
314     /* Save current base, to be able to revert to last base, 
315         if new base is invalid or not allowed to be selected */
316     $base_tmp = $this->base;
317     plugin::save_object();
318     $this->netConfigDNS->save_object();
320     /* Get base selection */
321     $tmp = $this->get_allowed_bases();
322     if(isset($_POST['base'])){
323       if($tmp[$_POST['base']]){
324         $this->base = $_POST['base'];
325       }else{
326         $this->base = $base_tmp;
327       }
328     }
329   }
332   /* Check supplied data */
333   function check()
334   {
335     /* Call common method to give check the hook */
336     $message= plugin::check();
337     $message= array_merge($message, $this->netConfigDNS->check());
338     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
340     /* must: cn */
341     if ($this->cn == ""){
342       msgPool::required(_("Server name"));
343     }
344     
345     /* Check if given name is a valid host/dns name */
346     if(!tests::is_dns_name($this->cn)){
347       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
348     }
350     if ($this->orig_dn != $this->dn){
351       $ldap= $this->config->get_ldap_link();
352       $ldap->cd ($this->base);
353       $ldap->search ("(cn=".$this->cn.")", array("cn"));
354       if ($ldap->count() != 0){
355         while ($attrs= $ldap->fetch()){
356           if ($attrs['dn'] != $this->orig_dn){
357             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
358               $message[]= msgPool::duplicated(_("Server name"));
359               break;
360             }
361           }
362         }
363       }
364     }
366     return ($message);
367   }
370   /* Save to LDAP */
371   function save()
372   {
373     /* Detect mode changes */
374     $activate= (isset($this->saved_attributes['gotoMode']) &&
375         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
376         $this->gotoMode == "active" &&
377         tests::is_ip($this->netConfigDNS->ipHostNumber));
379     plugin::save();
381     /* Remove all empty values */
382     if ($this->orig_dn == 'new'){
383       $attrs= array();
384       foreach ($this->attrs as $key => $val){
385         if (is_array($val) && count($val) == 0){
386           continue;
387         }
388         $attrs[$key]= $val;
389       }
390       $this->attrs= $attrs;
391     }
393     /* Write back to ldap */
394     $ldap= $this->config->get_ldap_link();
395     if ($this->orig_dn == 'new'){
396       $ldap->cd($this->config->current['BASE']);
397       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
398       $ldap->cd($this->dn);
399       $ldap->add($this->attrs);
400       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
401       $mode= "add";
402     } else {
403    
404       /* cn is not case sensitive for ldap, but for php it is!! */ 
405       if($this->config->current['DNMODE'] == "cn"){
406         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
407           $this->recursive_move($this->orig_dn, $this->dn);
408           plugin::save();
409         }
410       }else{
411         if ($this->orig_dn != $this->dn){
412           $this->recursive_move($this->orig_dn, $this->dn);
413           plugin::save();
414         }
415       }
416   
417       $ldap->cd($this->dn);
418       $this->cleanup();
419       $ldap->modify ($this->attrs); 
420       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
422       $mode= "modify";
423     }
424     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
425     $this->netConfigDNS->cn = $this->cn;
426     $this->netConfigDNS->save();
428     /* Optionally execute a command after we're done */
429     if(!$this->didAction){
430       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
431     }
433     if ($activate){
435       /* Send installation activation 
436        */
437       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
438       $o_queue = new gosaSupportDaemon();
439       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
440         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
441         $tmp = new $evt['CLASS_NAME']($this->config);
442         $tmp->set_type(TRIGGERED_EVENT);
443         $tmp->add_targets(array($this->netConfigDNS->macAddress));
444         if(!$o_queue->append($tmp)){
445           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
446                 $o_queue->get_error()),ERROR_DIALOG);
447         }
448       }
449     }
451   }
454   /* Display generic part for server copy & paste */
455   function getCopyDialog()
456   {
457     $vars = array("cn");
459     $smarty = get_smarty();
460     $smarty->assign("cn" ,$this->cn);
461     $smarty->assign("object","server");
462     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
463     $ret = array();
464     $ret['string'] = $str;
465     $ret['status'] = "";
466     return($ret);
467   }
470   function saveCopyDialog()
471   {
472     if(isset($_POST['cn'])){
473       $this->cn = $_POST['cn'];
474     }
475   }
477   
478   function PrepareForCopyPaste($source)  
479   {
480     plugin::PrepareForCopyPaste($source);
481     if(isset($source['macAddress'][0])){
482       $this->netConfigDNS->macAddress = $source['macAddress'][0];
483     }
484     if(isset($source['ipHostNumber'][0])){
485       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
486     }
488   }
489   
492   /* Return plugin informations for acl handling */ 
493   static function plInfo()
494   {
495     return (array( 
496           "plShortName"   => _("Generic"),
497           "plDescription" => _("Server generic"),
498           "plSelfModify"  => FALSE,
499           "plDepends"     => array(),
500           "plPriority"    => 1,
501           "plSection"     => array("administration"),           
502           "plCategory"    => array("server" => array("description"  => _("Server"),
503                                                      "objectClass"  => "goServer")),
504           "plProvidedAcls"=> array(
505             "cn"           => _("Name"),
506             "description"  => _("Description"),
507             "gotoMode"     => _("Goto mode"),
508             "base"         => _("Base"),
509             "FAIstate"     => _("Action flag"))
510           ));
511   }
514 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
515 ?>