Code

Fixed server base selection
[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                             "update"          => "sceduledupdate",
34                             "reinstall"       => "install",
35                             "rescan"          => "",
36                             "memcheck"        => "memcheck",
37                             "sysinfo"         => "sysinfo");
39   function servgeneric ($config, $dn= NULL, $parent= NULL)
40   {
41     plugin::plugin ($config, $dn, $parent);
43     $this->ui = get_userinfo();
44     $this->modes["active"]= _("Activated");
45     $this->modes["locked"]= _("Locked");
47     /* Set base */
48     if ($this->dn == "new"){
49       $ui= get_userinfo();
50       $this->base= dn2base($ui->dn);
51       $this->cn= "";
52     } else {
53       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
54     }
55     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
56     $this->netConfigDNS->set_acl_category("server");
57     $this->netConfigDNS->set_acl_base($this->base);
58   
59     /* Save dn for later references */
60     $this->orig_dn= $this->dn;
61   }
63   function execute()
64   {
65     /* Call parent execute */
66     plugin::execute();
68     /* Do we represent a valid server? */
69     if (!$this->is_account && $this->parent == NULL){
70       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
71         _("This 'dn' has no server features.")."</b>";
72       return($display);
73     }
75     /* Check for action */
76     if (isset($_POST['action']) && $this->acvl_is_writeable("FAIstate")){
77       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
78       if ($cmd == ""){
79         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
80       } else {
82         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
83         if ($retval != 0){
84           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
85         } else {
86           $this->didAction= TRUE;
88           /* Set FAIstate */
89           $ldap = $this->config->get_ldap_link();
90           $ldap->cd($this->config->current['BASE']);
91           $ldap->cat($this->dn,array("objectClass"));
92           $res = $ldap->fetch();
93           
94           $attrs = array();
95           $attrs['FAIstate'] = "";
96           if(isset($this->mapActions[$_POST['saction']])){
97             $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
98           }
100           for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
101             $attrs['objectClass'][] = $res['objectClass'][$i];
102           }
104           if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
105             $attrs['objectClass'][] = "FAIobject";
106           }
108           if($attrs['FAIstate'] == ""){
109             #FIXME we should check if FAIobject is used anymore
110             $attrs['FAIstate'] = array();
111           }
112  
113           $ldap->cd($this->dn);
114           $ldap->modify($attrs);
115           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
116         }
117       }
118     }
120     /* Base select dialog */
121     $once = true;
122     foreach($_POST as $name => $value){
123       if(preg_match("/^chooseBase/",$name) && $once ){
124         $once = false;
125         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
126         $this->dialog->setCurrentBase($this->base);
127       }
128     }
130     /* Dialog handling */
131     if(is_object($this->dialog)){
132       /* Must be called before save_object */
133       $this->dialog->save_object();
135       if($this->dialog->isClosed()){
136         $this->dialog = false;
137       }elseif($this->dialog->isSelected()){
138   
139         /* Only accept allowed bases */
140         $tmp = $this->get_allowed_bases();
141         if($tmp[$this->dialog->isSelected()]){
142           $this->base = $this->dialog->isSelected();
143         }
144         $this->dialog= false;
145       }else{
146         return($this->dialog->execute());
147       }
148     }
150     /* Fill templating stuff */
151     $smarty= get_smarty();
153     $tmp = $this->plInfo();
154     foreach($tmp['plProvidedAcls'] as $name => $translated){
155       $smarty->assign($name."ACL",$this->getacl($name));
156     }
158     /* Assign base ACL */
159     $smarty->assign("baseACL" , $this->getacl("base"));
160     $smarty->assign("bases"   , $this->get_allowed_bases());
162     /* Assign attributes */
163     foreach ($this->attributes as $attr){
164       $smarty->assign("$attr", $this->$attr);
165     }
167     $smarty->assign("staticAddress", "");
168     $smarty->assign("base_select", $this->base);
170     /* Assign status */
171     $query= "fping -q -r 1 -t 500 ".$this->cn;
172     exec ($query, $dummy, $retval);
174     /* Offline */
175     if ($retval == 0){
176       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
177                                        "instant_update" => _("Instant update"),
178                                        "update" => _("Scheduled update"),
179                                        "reinstall" => _("Reinstall"),
180                                        "rescan" => _("Rescan hardware"),
181                                        "memcheck" => _("Memory test"),
182                                        "sysinfo"  => _("System analysis")));
183     } else {
184       $smarty->assign("actions", array("wake" => _("Wake up"),
185                                        "reinstall" => _("Reinstall"),
186                                        "update" => _("Scheduled update"),
187                                        "memcheck" => _("Memory test"),
188                                        "sysinfo"  => _("System analysis")));
189     }
191     /* Show main page */
192     $smarty->assign("netconfig", $this->netConfigDNS->execute());
193     $smarty->assign("modes", $this->modes);
195     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
196   }
198   function remove_from_parent()
199   {
200     /* Cancel if there's nothing to do here */
201     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
202       return;
203     }
205     $this->netConfigDNS->remove_from_parent();
206     $ldap= $this->config->get_ldap_link();
207     $ldap->rmdir($this->dn);
208     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
210     /* Delete references to object groups */
211     $ldap->cd ($this->config->current['BASE']);
212     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
213     while ($ldap->fetch()){
214       $og= new ogroup($this->config, $ldap->getDN());
215       unset($og->member[$this->dn]);
216       $og->save ();
217     }
218     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
219   }
222   /* Save data to object */
223   function save_object()
224   {
225     /* Save current base, to be able to revert to last base, 
226         if new base is invalid or not allowed to be selected */
227     $base_tmp = $this->base;
228     plugin::save_object();
229     $this->netConfigDNS->save_object();
231     /* Get base selection */
232     $tmp = $this->get_allowed_bases();
233     if($tmp[$_POST['base']]){
234       $this->base = $_POST['base'];
235     }else{
236       $this->base = $base_tmp;
237     }
238   }
241   /* Check supplied data */
242   function check()
243   {
244     /* Call common method to give check the hook */
245     $message= plugin::check();
246     $message= array_merge($message, $this->netConfigDNS->check());
247     $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
249     /* must: cn */
250     if ($this->cn == ""){
251       $message[]= _("The required field 'Server name' is not set.");
252     }
253     
254     /* Set new acl base */
255     if($this->dn == "new") {
256       $this->set_acl_base($this->base);
257     }
259     $ui= get_userinfo();
260     if (!$this->acl_is_createable() && $this->dn == "new"){
261       $message[]= _("You have no permissions to create a server on this 'Base'.");
262     }
264     if ($this->orig_dn != $this->dn){
265       $ldap= $this->config->get_ldap_link();
266       $ldap->cd ($this->base);
267       $ldap->search ("(cn=".$this->cn.")", array("cn"));
268       if ($ldap->count() != 0){
269         while ($attrs= $ldap->fetch()){
270           if ($attrs['dn'] != $this->orig_dn){
271             if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
272             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
273             break;
274             }
275           }
276         }
277       }
278     }
280     return ($message);
281   }
284   /* Save to LDAP */
285   function save()
286   {
287     plugin::save();
289     /* Remove all empty values */
290     if ($this->orig_dn == 'new'){
291       $attrs= array();
292       foreach ($this->attrs as $key => $val){
293         if (is_array($val) && count($val) == 0){
294           continue;
295         }
296         $attrs[$key]= $val;
297       }
298       $this->attrs= $attrs;
299     }
301     /* Write back to ldap */
302     $ldap= $this->config->get_ldap_link();
303     if ($this->orig_dn == 'new'){
304       $ldap->cd($this->config->current['BASE']);
305       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
306       $ldap->cd($this->dn);
307       $ldap->add($this->attrs);
308       $mode= "add";
309     } else {
310    
311       /* cn is not case sensitive for ldap, but for php it is!! */ 
312       if($this->config->current['DNMODE'] == "cn"){
313         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
314           $this->move($this->orig_dn, $this->dn);
315           plugin::save();
316         }
317       }else{
318         if ($this->orig_dn != $this->dn){
319           $this->move($this->orig_dn, $this->dn);
320           plugin::save();
321         }
322       }
323   
324       $ldap->cd($this->dn);
325       $this->cleanup();
326       $ldap->modify ($this->attrs); 
328       $mode= "modify";
329     }
330     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
331     $this->netConfigDNS->cn = $this->cn;
332     $this->netConfigDNS->save($this->dn);
334     /* Optionally execute a command after we're done */
335     if(!$this->didAction){
336       $this->handle_post_events($mode);
337     }
338   }
341   /* Return plugin informations for acl handling */ 
342   function plInfo()
343   {
344     return (array( 
345           "plShortName"   => _("Generic"),
346           "plDescription" => _("Server generic"),
347           "plSelfModify"  => FALSE,
348           "plDepends"     => array(),
349           "plPriority"    => 1,
350           "plSection"     => array("administration"),           
351           "plCategory"    => array("server" => array("description"  => _("Server"),
352                                                      "objectClass"  => "gotoWorkstation")),
353           "plProvidedAcls"=> array(
354             "cn"           => _("Name"),
355             "description"  => _("Description"),
356             "gotoMode"     => _("Goto mode"),
357             "base"         => _("Base"),
358             "FAIstate"     => _("Action flag"))
359           ));
360   }
363 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
364 ?>