Code

Replaced NULL with FALSE.
[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                             "memcheck"        => "memcheck",
38                             "sysinfo"         => "sysinfo");
40   var $fai_activated  =FALSE;
41   var $view_logged = FALSE;
43   function servgeneric (&$config, $dn= NULL, $parent= NULL)
44   {
45     /* Check if FAI is activated */
46     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
47     
48     if(!empty($tmp)){
49       $this->fai_activated = TRUE;
50     }
52     plugin::plugin ($config, $dn, $parent);
54     $this->ui = get_userinfo();
55     $this->modes["active"]= _("Activated");
56     $this->modes["locked"]= _("Locked");
58     /* Set base */
59     if ($this->dn == "new"){
60       $ui= get_userinfo();
61       $this->base= dn2base($ui->dn);
62       $this->cn= "";
63     } else {
64       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
65     }
66     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
67     $this->netConfigDNS->set_acl_category("server");
68     $this->netConfigDNS->set_acl_base($this->base);
69        
70     /* Save dn for later references */
71     $this->orig_dn= $this->dn;
72   }
75   function set_acl_base($base)
76   {
77     plugin::set_acl_base($base);
78     $this->netConfigDNS->set_acl_base($base);
79   }
82   function set_acl_category($cat)
83   {
84     plugin::set_acl_category($cat);
85     $this->netConfigDNS->set_acl_category($cat);
86   }
89   function execute()
90   {
91     /* Call parent execute */
92     plugin::execute();
94     if($this->is_account && !$this->view_logged){
95       $this->view_logged = TRUE;
96       new log("view","server/".get_class($this),$this->dn);
97     }
99     /* Do we represent a valid server? */
100     if (!$this->is_account && $this->parent === NULL){
101       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
102         _("This 'dn' has no server features.")."</b>";
103       return($display);
104     }
106     /* Check for action */
107     if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
108       $cmd = $this->config->search("workgeneric", "ACTIONCMD",array('tabs'));
109       if ($cmd == ""){
110         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
111       } else {
113         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
114         if ($retval != 0){
115           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
116         } else {
117           $this->didAction= TRUE;
119           /* Set FAIstate */
120           if($this->fai_activated && $this->dn != "new"){
121             $ldap = $this->config->get_ldap_link();
122             $ldap->cd($this->config->current['BASE']);
123             $ldap->cat($this->dn,array("objectClass"));
124             $res = $ldap->fetch();
126             $attrs = array();
127             $attrs['FAIstate'] = "";
128             if(isset($this->mapActions[$_POST['saction']])){
129               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
130             }
132             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
133               $attrs['objectClass'][] = $res['objectClass'][$i];
134             }
136             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
137               $attrs['objectClass'][] = "FAIobject";
138             }
140             if($attrs['FAIstate'] == ""){
141 #FIXME we should check if FAIobject is used anymore
142               $attrs['FAIstate'] = array();
143             }
145             $ldap->cd($this->dn);
146             $ldap->modify($attrs);
147           }
148           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
149         }
150       }
151     }
153     /* Base select dialog */
154     $once = true;
155     foreach($_POST as $name => $value){
156       if(preg_match("/^chooseBase/",$name) && $once ){
157         $once = false;
158         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
159         $this->dialog->setCurrentBase($this->base);
160       }
161     }
163     /* Dialog handling */
164     if(is_object($this->dialog)){
165       /* Must be called before save_object */
166       $this->dialog->save_object();
168       if($this->dialog->isClosed()){
169         $this->dialog = false;
170       }elseif($this->dialog->isSelected()){
171   
172         /* Only accept allowed bases */
173         $tmp = $this->get_allowed_bases();
174         if($tmp[$this->dialog->isSelected()]){
175           $this->base = $this->dialog->isSelected();
176         }
177         $this->dialog= false;
178       }else{
179         return($this->dialog->execute());
180       }
181     }
183     /* Fill templating stuff */
184     $smarty= get_smarty();
186     $tmp = $this->plInfo();
187     foreach($tmp['plProvidedAcls'] as $name => $translated){
188       $smarty->assign($name."ACL",$this->getacl($name));
189     }
191     /* Assign base ACL */
192     $smarty->assign("bases"   , $this->get_allowed_bases());
194     /* Assign attributes */
195     foreach ($this->attributes as $attr){
196       $smarty->assign("$attr", $this->$attr);
197     }
199     $smarty->assign("staticAddress", "");
200     $smarty->assign("base_select", $this->base);
202     /* Assign status */
203     $query= "fping -q -r 1 -t 500 ".$this->cn;
204     exec ($query, $dummy, $retval);
206     /* Offline */
207     if ($retval == 0){
208       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
209                                        "instant_update" => _("Instant update"),
210                                        "update" => _("Scheduled update"),
211                                        "reinstall" => _("Reinstall"),
212                                        "rescan" => _("Rescan hardware"),
213                                        "memcheck" => _("Memory test"),
214                                        "localboot" => _("Force localboot"),
215                                        "sysinfo"  => _("System analysis")));
216     } else {
217       $smarty->assign("actions", array("wake" => _("Wake up"),
218                                        "reinstall" => _("Reinstall"),
219                                        "update" => _("Scheduled update"),
220                                        "memcheck" => _("Memory test"),
221                                        "localboot" => _("Force localboot"),
222                                        "sysinfo"  => _("System analysis")));
223     }
225     /* Show main page */
226     $smarty->assign("fai_activated",$this->fai_activated);
228     $str = $this->netConfigDNS->execute();
229     if(is_object($this->netConfigDNS->dialog)){
230       return($str);
231     }
232     $smarty->assign("netconfig", $str);
233     $smarty->assign("modes", $this->modes);
235     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
236   }
238   function remove_from_parent()
239   {
240     /* Cancel if there's nothing to do here */
241     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
242       return;
243     }
245     $this->netConfigDNS->remove_from_parent();
246     $ldap= $this->config->get_ldap_link();
247     $ldap->rmdir($this->dn);
249     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
251     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
253     /* Delete references to object groups */
254     $ldap->cd ($this->config->current['BASE']);
255     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
256     while ($ldap->fetch()){
257       $og= new ogroup($this->config, $ldap->getDN());
258       unset($og->member[$this->dn]);
259       $og->save ();
260     }
261     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
262   }
265   /* Save data to object */
266   function save_object()
267   {
268     /* Save current base, to be able to revert to last base, 
269         if new base is invalid or not allowed to be selected */
270     $base_tmp = $this->base;
271     plugin::save_object();
272     $this->netConfigDNS->save_object();
274     /* Get base selection */
275     $tmp = $this->get_allowed_bases();
276     if(isset($_POST['base'])){
277       if($tmp[$_POST['base']]){
278         $this->base = $_POST['base'];
279       }else{
280         $this->base = $base_tmp;
281       }
282     }
283   }
286   /* Check supplied data */
287   function check()
288   {
289     /* Call common method to give check the hook */
290     $message= plugin::check();
291     $message= array_merge($message, $this->netConfigDNS->check());
292     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
294     /* must: cn */
295     if ($this->cn == ""){
296       $message[]= _("The required field 'Server name' is not set.");
297     }
298     
299     /* Check if given name is a valid host/dns name */
300     if(!tests::is_dns_name($this->cn)){
301       $message[] = _("Please specify a valid name for this object.");
302     }
304     if ($this->orig_dn != $this->dn){
305       $ldap= $this->config->get_ldap_link();
306       $ldap->cd ($this->base);
307       $ldap->search ("(cn=".$this->cn.")", array("cn"));
308       if ($ldap->count() != 0){
309         while ($attrs= $ldap->fetch()){
310           if ($attrs['dn'] != $this->orig_dn){
311             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
312               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
313               break;
314             }
315           }
316         }
317       }
318     }
320     return ($message);
321   }
324   /* Save to LDAP */
325   function save()
326   {
327     /* Detect mode changes */
328     $activate= (isset($this->saved_attributes['gotoMode']) &&
329         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
330         $this->gotoMode == "active" &&
331         tests::is_ip($this->netConfigDNS->ipHostNumber));
333     plugin::save();
335     /* Remove all empty values */
336     if ($this->orig_dn == 'new'){
337       $attrs= array();
338       foreach ($this->attrs as $key => $val){
339         if (is_array($val) && count($val) == 0){
340           continue;
341         }
342         $attrs[$key]= $val;
343       }
344       $this->attrs= $attrs;
345     }
347     /* Write back to ldap */
348     $ldap= $this->config->get_ldap_link();
349     if ($this->orig_dn == 'new'){
350       $ldap->cd($this->config->current['BASE']);
351       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
352       $ldap->cd($this->dn);
353       $ldap->add($this->attrs);
354       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
355       $mode= "add";
356     } else {
357    
358       /* cn is not case sensitive for ldap, but for php it is!! */ 
359       if($this->config->current['DNMODE'] == "cn"){
360         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
361           $this->recursive_move($this->orig_dn, $this->dn);
362           plugin::save();
363         }
364       }else{
365         if ($this->orig_dn != $this->dn){
366           $this->recursive_move($this->orig_dn, $this->dn);
367           plugin::save();
368         }
369       }
370   
371       $ldap->cd($this->dn);
372       $this->cleanup();
373       $ldap->modify ($this->attrs); 
374       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
376       $mode= "modify";
377     }
378     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
379     $this->netConfigDNS->cn = $this->cn;
380     $this->netConfigDNS->save();
382     /* Optionally execute a command after we're done */
383     if(!$this->didAction){
384       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
385     }
387     if ($activate){
388       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
389     }
391   }
394   /* Display generic part for server copy & paste */
395   function getCopyDialog()
396   {
397     $vars = array("cn");
399     $smarty = get_smarty();
400     $smarty->assign("cn" ,$this->cn);
401     $smarty->assign("object","server");
402     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
403     $ret = array();
404     $ret['string'] = $str;
405     $ret['status'] = "";
406     return($ret);
407   }
410   function saveCopyDialog()
411   {
412     if(isset($_POST['cn'])){
413       $this->cn = $_POST['cn'];
414     }
415   }
417   
418   function PrepareForCopyPaste($source)  
419   {
420     plugin::PrepareForCopyPaste($source);
421     if(isset($source['macAddress'][0])){
422       $this->netConfigDNS->macAddress = $source['macAddress'][0];
423     }
424     if(isset($source['ipHostNumber'][0])){
425       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
426     }
428   }
429   
432   /* Return plugin informations for acl handling */ 
433   static function plInfo()
434   {
435     return (array( 
436           "plShortName"   => _("Generic"),
437           "plDescription" => _("Server generic"),
438           "plSelfModify"  => FALSE,
439           "plDepends"     => array(),
440           "plPriority"    => 1,
441           "plSection"     => array("administration"),           
442           "plCategory"    => array("server" => array("description"  => _("Server"),
443                                                      "objectClass"  => "goServer")),
444           "plProvidedAcls"=> array(
445             "cn"           => _("Name"),
446             "description"  => _("Description"),
447             "gotoMode"     => _("Goto mode"),
448             "base"         => _("Base"),
449             "FAIstate"     => _("Action flag"))
450           ));
451   }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>