Code

Removed old CLI stuff - cleanup
[gosa.git] / plugins / admin / systems / class_servGeneric.inc
1 <?php
3 class servgeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $ignore_account= TRUE;
7   var $interfaces= array();
9   /* Needed values and lists */
10   var $base= "";
11   var $cn= "";
12   var $l= "";
13   var $description= "";
14   var $orig_dn= "";
15   var $didAction= FALSE;
17   /* attribute list for save action */
18   var $attributes= array("cn", "description","gotoMode");
19   var $objectclasses= array("top", "GOhard", "goServer");
21   var $gotoMode= "locked";
23   var $netConfigDNS;
24   var $modes = array();
26   var $mapActions   = array("reboot"          => "",
27                             "instant_update"  => "softupdate",
28                             "update"          => "scheduledupdate",
29                             "reinstall"       => "install",
30                             "rescan"          => "",
31                             "memcheck"        => "memcheck",
32                             "sysinfo"         => "sysinfo");
34   var $fai_activated = FALSE;
36   function servgeneric ($config, $dn= NULL, $parent= NULL)
37   {
38     plugin::plugin ($config, $dn, $parent);
40     /* Check if FAI is activated */
41     $tmp = search_config($config->data,"faiManagement","CLASS");
42     if(!empty($tmp)){
43       $this->fai_activated = TRUE;
44     }
46     $this->modes["active"]= _("Activated");
47     $this->modes["locked"]= _("Locked");
49     /* Set base */
50     if ($this->dn == "new"){
51       $ui= get_userinfo();
52       $this->base= dn2base($ui->dn);
53       $this->cn= "";
54     } else {
55       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
56     }
57     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
58     $this->netConfigDNS->acl = $this->acl;
59     /* Save dn for later references */
60     $this->orig_dn= $this->dn;
61   }
63   function execute()
64   {
65     /* Call parent execute */
66     $this->netConfigDNS->acl = $this->acl;
67     plugin::execute();
69     /* Do we represent a valid server? */
70     if (!$this->is_account && $this->parent == NULL){
71       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
72         _("This 'dn' has no server features.")."</b>";
73       return($display);
74     }
76     /* Check for action */
77     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
78       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
79       if ($cmd == ""){
80         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
81       } else {
83         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
84         if ($retval != 0){
85           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
86         } else {
87           $this->didAction= TRUE;
89           /* Set FAIstate */
91           if($this->fai_activated && $this->dn != "new"){
92             $ldap = $this->config->get_ldap_link();
93             $ldap->cd($this->config->current['BASE']);
94             $ldap->cat($this->dn,array("objectClass"));
95             $res = $ldap->fetch();
97             $attrs = array();
98             $attrs['FAIstate'] = "";
99             if(isset($this->mapActions[$_POST['saction']])){
100               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
101             }
103             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
104               $attrs['objectClass'][] = $res['objectClass'][$i];
105             }
107             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
108               $attrs['objectClass'][] = "FAIobject";
109             }
111             if($attrs['FAIstate'] == ""){
112 #FIXME we should check if FAIobject is used anymore
113               $attrs['FAIstate'] = array();
114             }
116             $ldap->cd($this->dn);
117             $ldap->modify($attrs);
118             show_ldap_error($ldap->get_error());
119           }
120         }
121       }
122     }
124     /* Base select dialog */
125     $once = true;
126     foreach($_POST as $name => $value){
127       if(preg_match("/^chooseBase/",$name) && $once){
128         $once = false;
129         $this->dialog = new baseSelectDialog($this->config);
130         $this->dialog->setCurrentBase($this->base);
131       }
132     }
134     /* Dialog handling */
135     if(is_object($this->dialog)){
136       /* Must be called before save_object */
137       $this->dialog->save_object();
139       if($this->dialog->isClosed()){
140         $this->dialog = false;
141       }elseif($this->dialog->isSelected()){
142         $this->base = $this->dialog->isSelected();
143         $this->dialog= false;
144       }else{
145         return($this->dialog->execute());
146       }
147     }
149     /* Fill templating stuff */
150     $smarty= get_smarty();
151     $smarty->assign("bases", $this->config->idepartments);
153     /* Assign attributes */
154     foreach ($this->attributes as $attr){
155       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
156       $smarty->assign("$attr", $this->$attr);
157     }
158     $smarty->assign("baseACL", chkacl($this->acl, "base"));
160     $smarty->assign("staticAddress", "");
161     $smarty->assign("base_select", $this->base);
163     /* Assign status */
164     $query= "fping -q -r 1 -t 500 ".$this->cn;
165     exec ($query, $dummy, $retval);
167     /* Offline */
168     if ($retval == 0){
169       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
170                                        "instant_update" => _("Instant update"),
171                                        "update" => _("Scheduled update"),
172                                        "reinstall" => _("Reinstall"),
173                                        "rescan" => _("Rescan hardware"),
174                                        "memcheck" => _("Memory test"),
175                                        "sysinfo"  => _("System analysis")));
176     } else {
177       $smarty->assign("actions", array("wake" => _("Wake up"),
178                                        "reinstall" => _("Reinstall"),
179                                        "update" => _("Scheduled update"),
180                                        "memcheck" => _("Memory test"),
181                                        "sysinfo"  => _("System analysis")));
182     }
184     /* Show main page */
185     $this->netConfigDNS->cn= $this->cn;
186     $smarty->assign("netconfig", $this->netConfigDNS->execute());
187     $smarty->assign("fai_activated",$this->fai_activated);
188     $smarty->assign("modes", $this->modes);
190     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
191   }
193   function remove_from_parent()
194   {
195     $this->netConfigDNS->acl = $this->acl;
196     $this->netConfigDNS->remove_from_parent();
197     $ldap= $this->config->get_ldap_link();
198     $ldap->rmdir($this->dn);
199     show_ldap_error($ldap->get_error(), _("Removing server failed"));
201     /* Delete references to object groups */
202     $ldap->cd ($this->config->current['BASE']);
203     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
204     while ($ldap->fetch()){
205       $og= new ogroup($this->config, $ldap->getDN());
206       unset($og->member[$this->dn]);
207       $og->save ();
208     }
209     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
210   }
213   /* Save data to object */
214   function save_object()
215   {
216     plugin::save_object();
217     $this->netConfigDNS->save_object();
218     /* Save base, since this is no LDAP attribute */
219     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
220       $this->base= $_POST['base'];
221     }
222   }
225   /* Check supplied data */
226   function check()
227   {
228     /* Call common method to give check the hook */
229     $message= plugin::check();
230     $message= array_merge($message, $this->netConfigDNS->check());
231     $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
233     /* must: cn */
234     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
235       $message[]= _("The required field 'Server name' is not set.");
236     }
238     $ui= get_userinfo();
239     $acl= get_permissions ($this->dn, $ui->subtreeACL);
240     $acl= get_module_permission($acl, "servgeneric", $this->dn);
241     if (chkacl($acl, "create") != ""){
242       $message[]= _("You have no permissions to create a server on this 'Base'.");
243     }
245     if ($this->orig_dn != $this->dn){
246       $ldap= $this->config->get_ldap_link();
247       $ldap->cd ($this->base);
248       $ldap->search ("(cn=".$this->cn.")", array("cn"));
249       if ($ldap->count() != 0){
250         while ($attrs= $ldap->fetch()){
251           if ($attrs['dn'] != $this->orig_dn){
252             if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
253             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
254             break;
255             }
256           }
257         }
258       }
259     }
261     return ($message);
262   }
265   /* Save to LDAP */
266   function save()
267   {
268     $this->netConfigDNS->acl = $this->acl;
269     plugin::save();
271     /* Remove all empty values */
272     if ($this->orig_dn == 'new'){
273       $attrs= array();
274       foreach ($this->attrs as $key => $val){
275         if (is_array($val) && count($val) == 0){
276           continue;
277         }
278         $attrs[$key]= $val;
279       }
280       $this->attrs= $attrs;
281     }
283     /* Write back to ldap */
284     $ldap= $this->config->get_ldap_link();
285     if ($this->orig_dn == 'new'){
286       $ldap->cd($this->config->current['BASE']);
287       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
288       $ldap->cd($this->dn);
289       $ldap->add($this->attrs);
290       $mode= "add";
291     } else {
292    
293       /* cn is not case sensitive for ldap, but for php it is!! */ 
294       if($this->config->current['DNMODE'] == "cn"){
295         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
296           $this->move($this->orig_dn, $this->dn);
297           plugin::save();
298         }
299       }else{
300         if ($this->orig_dn != $this->dn){
301           $this->move($this->orig_dn, $this->dn);
302           plugin::save();
303         }
304       }
305   
306       $ldap->cd($this->dn);
307       $this->cleanup();
308       $ldap->modify ($this->attrs); 
310       $mode= "modify";
311     }
312     show_ldap_error($ldap->get_error(), _("Saving server failed"));
313     $this->netConfigDNS->cn = $this->cn;
314     $this->netConfigDNS->save($this->dn);
316     /* Optionally execute a command after we're done */
317     if(!$this->didAction){
318       $this->handle_post_events($mode);
319     }
320   }
324 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
325 ?>