Code

Moved to mac
[gosa.git] / gosa-plugins / systems / admin / systems / class_servGeneric.inc
1 <?php
3 class servgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server 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 $ignore_account= TRUE;
12   var $interfaces= array();
14   /* Needed values and lists */
15   var $base= "";
16   var $cn= "";
17   var $l= "";
18   var $description= "";
19   var $orig_dn= "";
20   var $didAction= FALSE;
22   /* attribute list for save action */
23   var $attributes= array("cn", "description","gotoMode");
24   var $objectclasses= array("top", "GOhard", "goServer");
26   var $gotoMode= "locked";
28   var $netConfigDNS;
29   var $modes = array();
30   var $ui   ;
31   var $mapActions   = array("reboot"          => "",
32                             "instant_update"  => "softupdate",
33                             "localboot"       => "localboot",
34                             "update"          => "sceduledupdate",
35                             "reinstall"       => "install",
36                             "rescan"          => "",
37                             "wake"            => "",
38                             "memcheck"        => "memcheck",
39                             "sysinfo"         => "sysinfo");
41   var $fai_activated  =FALSE;
42   var $view_logged = FALSE;
44   function servgeneric (&$config, $dn= NULL, $parent= NULL)
45   {
46     /* Check if FAI is activated */
47     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
48     
49     if(!empty($tmp)){
50       $this->fai_activated = TRUE;
51     }
53     plugin::plugin ($config, $dn, $parent);
55     $this->ui = get_userinfo();
56     $this->modes["active"]= _("Activated");
57     $this->modes["locked"]= _("Locked");
59     /* Set base */
60     if ($this->dn == "new"){
61       $ui= get_userinfo();
62       $this->base= dn2base($ui->dn);
63       $this->cn= "";
64     } else {
65       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
66     }
67     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
68     $this->netConfigDNS->set_acl_category("server");
69     $this->netConfigDNS->set_acl_base($this->base);
70        
71     /* Save dn for later references */
72     $this->orig_dn= $this->dn;
73   }
76   function set_acl_base($base)
77   {
78     plugin::set_acl_base($base);
79     $this->netConfigDNS->set_acl_base($base);
80   }
83   function set_acl_category($cat)
84   {
85     plugin::set_acl_category($cat);
86     $this->netConfigDNS->set_acl_category($cat);
87   }
90   function execute()
91   {
92     /* Call parent execute */
93     plugin::execute();
95     if($this->is_account && !$this->view_logged){
96       $this->view_logged = TRUE;
97       new log("view","server/".get_class($this),$this->dn);
98     }
100     /* Do we represent a valid server? */
101     if (!$this->is_account && $this->parent === NULL){
102       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
103         _("This 'dn' has no server features.")."</b>";
104       return($display);
105     }
107     /* Check for action */
108     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
109       gosaSupportDaemon::send("gosa_trigger_action_".$_POST['saction'], $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
111       $this->didAction= TRUE;
113       if ($_POST['saction'] != "wake") {
115           /* Set FAIstate */
116           if($this->fai_activated && $this->dn != "new"){
117             $ldap = $this->config->get_ldap_link();
118             $ldap->cd($this->config->current['BASE']);
119             $ldap->cat($this->dn,array("objectClass"));
120             $res = $ldap->fetch();
122             $attrs = array();
123             $attrs['FAIstate'] = "";
124             if(isset($this->mapActions[$_POST['saction']])){
125               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
126             }
128             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
129               $attrs['objectClass'][] = $res['objectClass'][$i];
130             }
132             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
133               $attrs['objectClass'][] = "FAIobject";
134             }
136             if($attrs['FAIstate'] == ""){
137               #FIXME we should check if FAIobject is used anymore
138               $attrs['FAIstate'] = array();
139             }
141             $ldap->cd($this->dn);
142             $ldap->modify($attrs);
143           }
144           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
145       }
146     }
148     /* Base select dialog */
149     $once = true;
150     foreach($_POST as $name => $value){
151       if(preg_match("/^chooseBase/",$name) && $once ){
152         $once = false;
153         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
154         $this->dialog->setCurrentBase($this->base);
155       }
156     }
158     /* Dialog handling */
159     if(is_object($this->dialog)){
160       /* Must be called before save_object */
161       $this->dialog->save_object();
163       if($this->dialog->isClosed()){
164         $this->dialog = false;
165       }elseif($this->dialog->isSelected()){
166   
167         /* Only accept allowed bases */
168         $tmp = $this->get_allowed_bases();
169         if($tmp[$this->dialog->isSelected()]){
170           $this->base = $this->dialog->isSelected();
171         }
172         $this->dialog= false;
173       }else{
174         return($this->dialog->execute());
175       }
176     }
178     /* Fill templating stuff */
179     $smarty= get_smarty();
181     $tmp = $this->plInfo();
182     foreach($tmp['plProvidedAcls'] as $name => $translated){
183       $smarty->assign($name."ACL",$this->getacl($name));
184     }
186     /* Assign base ACL */
187     $smarty->assign("bases"   , $this->get_allowed_bases());
189     /* Assign attributes */
190     foreach ($this->attributes as $attr){
191       $smarty->assign("$attr", $this->$attr);
192     }
194     $smarty->assign("staticAddress", "");
195     $smarty->assign("base_select", $this->base);
197     /* Assign status */
198     $query= "fping -q -r 1 -t 500 ".$this->cn;
199     exec ($query, $dummy, $retval);
201     /* Offline */
202     if ($retval == 0){
203       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
204                                        "instant_update" => _("Instant update"),
205                                        "update" => _("Scheduled update"),
206                                        "reinstall" => _("Reinstall"),
207                                        "rescan" => _("Rescan hardware"),
208                                        "memcheck" => _("Memory test"),
209                                        "localboot" => _("Force localboot"),
210                                        "sysinfo"  => _("System analysis")));
211     } else {
212       $smarty->assign("actions", array("wake" => _("Wake up"),
213                                        "reinstall" => _("Reinstall"),
214                                        "update" => _("Scheduled update"),
215                                        "memcheck" => _("Memory test"),
216                                        "localboot" => _("Force localboot"),
217                                        "sysinfo"  => _("System analysis")));
218     }
220     /* Show main page */
221     $smarty->assign("fai_activated",$this->fai_activated);
223     $str = $this->netConfigDNS->execute();
224     if(is_object($this->netConfigDNS->dialog)){
225       return($str);
226     }
227     $smarty->assign("netconfig", $str);
228     $smarty->assign("modes", $this->modes);
230     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
231   }
233   function remove_from_parent()
234   {
235     /* Cancel if there's nothing to do here */
236     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
237       return;
238     }
240     $this->netConfigDNS->remove_from_parent();
241     $ldap= $this->config->get_ldap_link();
242     $ldap->rmdir($this->dn);
244     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
246     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
248     /* Delete references to object groups */
249     $ldap->cd ($this->config->current['BASE']);
250     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
251     while ($ldap->fetch()){
252       $og= new ogroup($this->config, $ldap->getDN());
253       unset($og->member[$this->dn]);
254       $og->save ();
255     }
256     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
257   }
260   /* Save data to object */
261   function save_object()
262   {
263     /* Save current base, to be able to revert to last base, 
264         if new base is invalid or not allowed to be selected */
265     $base_tmp = $this->base;
266     plugin::save_object();
267     $this->netConfigDNS->save_object();
269     /* Get base selection */
270     $tmp = $this->get_allowed_bases();
271     if(isset($_POST['base'])){
272       if($tmp[$_POST['base']]){
273         $this->base = $_POST['base'];
274       }else{
275         $this->base = $base_tmp;
276       }
277     }
278   }
281   /* Check supplied data */
282   function check()
283   {
284     /* Call common method to give check the hook */
285     $message= plugin::check();
286     $message= array_merge($message, $this->netConfigDNS->check());
287     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
289     /* must: cn */
290     if ($this->cn == ""){
291       $message[]= _("The required field 'Server name' is not set.");
292     }
293     
294     /* Check if given name is a valid host/dns name */
295     if(!tests::is_dns_name($this->cn)){
296       $message[] = _("Please specify a valid name for this object.");
297     }
299     if ($this->orig_dn != $this->dn){
300       $ldap= $this->config->get_ldap_link();
301       $ldap->cd ($this->base);
302       $ldap->search ("(cn=".$this->cn.")", array("cn"));
303       if ($ldap->count() != 0){
304         while ($attrs= $ldap->fetch()){
305           if ($attrs['dn'] != $this->orig_dn){
306             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
307               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
308               break;
309             }
310           }
311         }
312       }
313     }
315     return ($message);
316   }
319   /* Save to LDAP */
320   function save()
321   {
322     /* Detect mode changes */
323     $activate= (isset($this->saved_attributes['gotoMode']) &&
324         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
325         $this->gotoMode == "active" &&
326         tests::is_ip($this->netConfigDNS->ipHostNumber));
328     plugin::save();
330     /* Remove all empty values */
331     if ($this->orig_dn == 'new'){
332       $attrs= array();
333       foreach ($this->attrs as $key => $val){
334         if (is_array($val) && count($val) == 0){
335           continue;
336         }
337         $attrs[$key]= $val;
338       }
339       $this->attrs= $attrs;
340     }
342     /* Write back to ldap */
343     $ldap= $this->config->get_ldap_link();
344     if ($this->orig_dn == 'new'){
345       $ldap->cd($this->config->current['BASE']);
346       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
347       $ldap->cd($this->dn);
348       $ldap->add($this->attrs);
349       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
350       $mode= "add";
351     } else {
352    
353       /* cn is not case sensitive for ldap, but for php it is!! */ 
354       if($this->config->current['DNMODE'] == "cn"){
355         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
356           $this->recursive_move($this->orig_dn, $this->dn);
357           plugin::save();
358         }
359       }else{
360         if ($this->orig_dn != $this->dn){
361           $this->recursive_move($this->orig_dn, $this->dn);
362           plugin::save();
363         }
364       }
365   
366       $ldap->cd($this->dn);
367       $this->cleanup();
368       $ldap->modify ($this->attrs); 
369       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
371       $mode= "modify";
372     }
373     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
374     $this->netConfigDNS->cn = $this->cn;
375     $this->netConfigDNS->save();
377     /* Optionally execute a command after we're done */
378     if(!$this->didAction){
379       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
380     }
382     if ($activate){
383       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
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 ?>