Code

Updates
[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       /* Check if we have an DaemonEvent for this action */
134       if(class_available("DaemonEvent_".$action)){
135         $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
136         if(isset($events['BY_CLASS']["DaemonEvent_".$action])){
137           $evt = $events['BY_CLASS']["DaemonEvent_".$action];
138           $tmp = new $evt['CLASS_NAME']($this->config);
139           $tmp->add_targets(array($this->netConfigDNS->macAddress));
140           $tmp->set_type(TRIGGERED_EVENT);
141           $o_queue = new gosaSupportDaemon();
142           if(!$o_queue->append($tmp)){
143             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
144                   $o_queue->get_error()),ERROR_DIALOG);
145           }
146         }
147       }else{
149         msg_dialog::display(_("Missing Daemon Event"),
150                     sprintf(_("The requested action does not exists '%s' Sending event manually."),"DaemonEvent_".$action),ERROR_DIALOG);
152         /* No event found, send action manually.
153          */
154         $method= "gosa";
155         $action= $_POST['saction'];
156         if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
157           $method= "job";
158         }
160         gosaSupportDaemon::send("${method}_trigger_action_".$action,
161             $this->netConfigDNS->macAddress,
162             array("macAddress" => $this->netConfigDNS->macAddress));
163       }
165       $this->didAction= TRUE;
167       if ($_POST['saction'] != "wake") {
169           /* Set FAIstate */
170           if($this->fai_activated && $this->dn != "new"){
171             $ldap = $this->config->get_ldap_link();
172             $ldap->cd($this->config->current['BASE']);
173             $ldap->cat($this->dn,array("objectClass"));
174             $res = $ldap->fetch();
176             $attrs = array();
177             $attrs['FAIstate'] = "";
178             if(isset($this->mapActions[$_POST['saction']])){
179               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
180             }
182             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
183               $attrs['objectClass'][] = $res['objectClass'][$i];
184             }
186             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
187               $attrs['objectClass'][] = "FAIobject";
188             }
190             if($attrs['FAIstate'] == ""){
191               #FIXME we should check if FAIobject is used anymore
192               $attrs['FAIstate'] = array();
193             }
195             $ldap->cd($this->dn);
196             $ldap->modify($attrs);
197           }
198           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
199       }
200     }
202     /* Base select dialog */
203     $once = true;
204     foreach($_POST as $name => $value){
205       if(preg_match("/^chooseBase/",$name) && $once ){
206         $once = false;
207         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
208         $this->dialog->setCurrentBase($this->base);
209       }
210     }
212     /* Dialog handling */
213     if(is_object($this->dialog)){
214       /* Must be called before save_object */
215       $this->dialog->save_object();
217       if($this->dialog->isClosed()){
218         $this->dialog = false;
219       }elseif($this->dialog->isSelected()){
220   
221         /* Only accept allowed bases */
222         $tmp = $this->get_allowed_bases();
223         if($tmp[$this->dialog->isSelected()]){
224           $this->base = $this->dialog->isSelected();
225         }
226         $this->dialog= false;
227       }else{
228         return($this->dialog->execute());
229       }
230     }
232     /* Fill templating stuff */
233     $smarty= get_smarty();
235     $tmp = $this->plInfo();
236     foreach($tmp['plProvidedAcls'] as $name => $translated){
237       $smarty->assign($name."ACL",$this->getacl($name));
238     }
240     /* Assign base ACL */
241     $smarty->assign("bases"   , $this->get_allowed_bases());
243     /* Assign attributes */
244     foreach ($this->attributes as $attr){
245       $smarty->assign("$attr", $this->$attr);
246     }
248     $smarty->assign("staticAddress", "");
249     $smarty->assign("base_select", $this->base);
251     /* Assign status */
252     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
253       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
254                                        "instant_update" => _("Instant update"),
255                                        "update" => _("Scheduled update"),
256                                        "reinstall" => _("Reinstall"),
257                                        "rescan" => _("Rescan hardware"),
258                                        "memcheck" => _("Memory test"),
259                                        "localboot" => _("Force localboot"),
260                                        "sysinfo"  => _("System analysis")));
261     } else {
262       $smarty->assign("actions", array("wake" => _("Wake up"),
263                                        "reinstall" => _("Reinstall"),
264                                        "update" => _("Scheduled update"),
265                                        "memcheck" => _("Memory test"),
266                                        "localboot" => _("Force localboot"),
267                                        "sysinfo"  => _("System analysis")));
268     }
270     /* Show main page */
271     $smarty->assign("fai_activated",$this->fai_activated);
273     $str = $this->netConfigDNS->execute();
274     if(is_object($this->netConfigDNS->dialog)){
275       return($str);
276     }
277     $smarty->assign("netconfig", $str);
278     $smarty->assign("modes", $this->modes);
280     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
281   }
283   function remove_from_parent()
284   {
285     /* Cancel if there's nothing to do here */
286     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
287       return;
288     }
290     $this->netConfigDNS->remove_from_parent();
291     $ldap= $this->config->get_ldap_link();
292     $ldap->rmdir($this->dn);
294     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
296     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
298     /* Delete references to object groups */
299     $ldap->cd ($this->config->current['BASE']);
300     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
301     while ($ldap->fetch()){
302       $og= new ogroup($this->config, $ldap->getDN());
303       unset($og->member[$this->dn]);
304       $og->save ();
305     }
306     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
307   }
310   /* Save data to object */
311   function save_object()
312   {
313     /* Save current base, to be able to revert to last base, 
314         if new base is invalid or not allowed to be selected */
315     $base_tmp = $this->base;
316     plugin::save_object();
317     $this->netConfigDNS->save_object();
319     /* Get base selection */
320     $tmp = $this->get_allowed_bases();
321     if(isset($_POST['base'])){
322       if($tmp[$_POST['base']]){
323         $this->base = $_POST['base'];
324       }else{
325         $this->base = $base_tmp;
326       }
327     }
328   }
331   /* Check supplied data */
332   function check()
333   {
334     /* Call common method to give check the hook */
335     $message= plugin::check();
336     $message= array_merge($message, $this->netConfigDNS->check());
337     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
339     /* must: cn */
340     if ($this->cn == ""){
341       msgPool::required(_("Server name"));
342     }
343     
344     /* Check if given name is a valid host/dns name */
345     if(!tests::is_dns_name($this->cn)){
346       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
347     }
349     if ($this->orig_dn != $this->dn){
350       $ldap= $this->config->get_ldap_link();
351       $ldap->cd ($this->base);
352       $ldap->search ("(cn=".$this->cn.")", array("cn"));
353       if ($ldap->count() != 0){
354         while ($attrs= $ldap->fetch()){
355           if ($attrs['dn'] != $this->orig_dn){
356             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
357               $message[]= msgPool::duplicated(_("Server name"));
358               break;
359             }
360           }
361         }
362       }
363     }
365     return ($message);
366   }
369   /* Save to LDAP */
370   function save()
371   {
372     /* Detect mode changes */
373     $activate= (isset($this->saved_attributes['gotoMode']) &&
374         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
375         $this->gotoMode == "active" &&
376         tests::is_ip($this->netConfigDNS->ipHostNumber));
378     plugin::save();
380     /* Remove all empty values */
381     if ($this->orig_dn == 'new'){
382       $attrs= array();
383       foreach ($this->attrs as $key => $val){
384         if (is_array($val) && count($val) == 0){
385           continue;
386         }
387         $attrs[$key]= $val;
388       }
389       $this->attrs= $attrs;
390     }
392     /* Write back to ldap */
393     $ldap= $this->config->get_ldap_link();
394     if ($this->orig_dn == 'new'){
395       $ldap->cd($this->config->current['BASE']);
396       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
397       $ldap->cd($this->dn);
398       $ldap->add($this->attrs);
399       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
400       $mode= "add";
401     } else {
402    
403       /* cn is not case sensitive for ldap, but for php it is!! */ 
404       if($this->config->current['DNMODE'] == "cn"){
405         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
406           $this->recursive_move($this->orig_dn, $this->dn);
407           plugin::save();
408         }
409       }else{
410         if ($this->orig_dn != $this->dn){
411           $this->recursive_move($this->orig_dn, $this->dn);
412           plugin::save();
413         }
414       }
415   
416       $ldap->cd($this->dn);
417       $this->cleanup();
418       $ldap->modify ($this->attrs); 
419       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
421       $mode= "modify";
422     }
423     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
424     $this->netConfigDNS->cn = $this->cn;
425     $this->netConfigDNS->save();
427     /* Optionally execute a command after we're done */
428     if(!$this->didAction){
429       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
430     }
432     if ($activate){
434       /* Send installation activation 
435        */
436       $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
437       $o_queue = new gosaSupportDaemon();
438       if(isset($events['BY_CLASS']['DaemonEvent_installation_activation'])){
439         $evt = $events['BY_CLASS']['DaemonEvent_installation_activation'];
440         $tmp = new $evt['CLASS_NAME']($this->config);
441         $tmp->set_type(TRIGGERED_EVENT);
442         $tmp->add_targets(array($this->netConfigDNS->macAddress));
443         if(!$o_queue->append($tmp)){
444           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
445                 $o_queue->get_error()),ERROR_DIALOG);
446         }
447       }
448     }
450   }
453   /* Display generic part for server copy & paste */
454   function getCopyDialog()
455   {
456     $vars = array("cn");
458     $smarty = get_smarty();
459     $smarty->assign("cn" ,$this->cn);
460     $smarty->assign("object","server");
461     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
462     $ret = array();
463     $ret['string'] = $str;
464     $ret['status'] = "";
465     return($ret);
466   }
469   function saveCopyDialog()
470   {
471     if(isset($_POST['cn'])){
472       $this->cn = $_POST['cn'];
473     }
474   }
476   
477   function PrepareForCopyPaste($source)  
478   {
479     plugin::PrepareForCopyPaste($source);
480     if(isset($source['macAddress'][0])){
481       $this->netConfigDNS->macAddress = $source['macAddress'][0];
482     }
483     if(isset($source['ipHostNumber'][0])){
484       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
485     }
487   }
488   
491   /* Return plugin informations for acl handling */ 
492   static function plInfo()
493   {
494     return (array( 
495           "plShortName"   => _("Generic"),
496           "plDescription" => _("Server generic"),
497           "plSelfModify"  => FALSE,
498           "plDepends"     => array(),
499           "plPriority"    => 1,
500           "plSection"     => array("administration"),           
501           "plCategory"    => array("server" => array("description"  => _("Server"),
502                                                      "objectClass"  => "goServer")),
503           "plProvidedAcls"=> array(
504             "cn"           => _("Name"),
505             "description"  => _("Description"),
506             "gotoMode"     => _("Goto mode"),
507             "base"         => _("Base"),
508             "FAIstate"     => _("Action flag"))
509           ));
510   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>