Code

Updated workstation - Startup
[gosa.git] / plugins / 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=".$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.",ou=servers,ou=systems,".$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(!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("/,ou=incoming,/",$attrs['dn']) && preg_match("/,ou=servers,ou=systems,/",$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     plugin::save();
329     /* Remove all empty values */
330     if ($this->orig_dn == 'new'){
331       $attrs= array();
332       foreach ($this->attrs as $key => $val){
333         if (is_array($val) && count($val) == 0){
334           continue;
335         }
336         $attrs[$key]= $val;
337       }
338       $this->attrs= $attrs;
339     }
341     /* Write back to ldap */
342     $ldap= $this->config->get_ldap_link();
343     if ($this->orig_dn == 'new'){
344       $ldap->cd($this->config->current['BASE']);
345       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
346       $ldap->cd($this->dn);
347       $ldap->add($this->attrs);
348       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
349       $mode= "add";
350     } else {
351    
352       /* cn is not case sensitive for ldap, but for php it is!! */ 
353       if($this->config->current['DNMODE'] == "cn"){
354         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
355           $this->recursive_move($this->orig_dn, $this->dn);
356           plugin::save();
357         }
358       }else{
359         if ($this->orig_dn != $this->dn){
360           $this->recursive_move($this->orig_dn, $this->dn);
361           plugin::save();
362         }
363       }
364   
365       $ldap->cd($this->dn);
366       $this->cleanup();
367       $ldap->modify ($this->attrs); 
368       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
370       $mode= "modify";
371     }
372     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
373     $this->netConfigDNS->cn = $this->cn;
374     $this->netConfigDNS->save();
376     /* Optionally execute a command after we're done */
377     if(!$this->didAction){
378       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
379     }
380   }
383   /* Display generic part for server copy & paste */
384   function getCopyDialog()
385   {
386     $vars = array("cn");
388     $smarty = get_smarty();
389     $smarty->assign("cn" ,$this->cn);
390     $smarty->assign("object","server");
391     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
392     $ret = array();
393     $ret['string'] = $str;
394     $ret['status'] = "";
395     return($ret);
396   }
399   function saveCopyDialog()
400   {
401     if(isset($_POST['cn'])){
402       $this->cn = $_POST['cn'];
403     }
404   }
406   
407   function PrepareForCopyPaste($source)  
408   {
409     plugin::PrepareForCopyPaste($source);
410     if(isset($source['macAddress'][0])){
411       $this->netConfigDNS->macAddress = $source['macAddress'][0];
412     }
413     if(isset($source['ipHostNumber'][0])){
414       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
415     }
417   }
418   
421   /* Return plugin informations for acl handling */ 
422   function plInfo()
423   {
424     return (array( 
425           "plShortName"   => _("Generic"),
426           "plDescription" => _("Server generic"),
427           "plSelfModify"  => FALSE,
428           "plDepends"     => array(),
429           "plPriority"    => 1,
430           "plSection"     => array("administration"),           
431           "plCategory"    => array("server" => array("description"  => _("Server"),
432                                                      "objectClass"  => "goServer")),
433           "plProvidedAcls"=> array(
434             "cn"           => _("Name"),
435             "description"  => _("Description"),
436             "gotoMode"     => _("Goto mode"),
437             "base"         => _("Base"),
438             "FAIstate"     => _("Action flag"))
439           ));
440   }
443 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
444 ?>