Code

d17b6d92da1ca6c570df74e77a5630684aa0e50d
[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"          => "localboot",
27                             "localboot"       => "localboot",
28                             "instant_update"  => "softupdate",
29                             "update"          => "scheduledupdate",
30                             "reinstall"       => "install",
31                             "rescan"          => "",
32                             "memcheck"        => "memcheck",
33                             "sysinfo"         => "sysinfo");
35   var $fai_activated = FALSE;
37   function servgeneric ($config, $dn= NULL, $parent= NULL)
38   {
39     plugin::plugin ($config, $dn, $parent);
41     /* Check if FAI is activated */
42     $tmp = search_config($config->data,"faiManagement","CLASS");
43     if(!empty($tmp)){
44       $this->fai_activated = TRUE;
45     }
47     $this->modes["active"]= _("Activated");
48     $this->modes["locked"]= _("Locked");
50     /* Set base */
51     if ($this->dn == "new"){
52       $ui= get_userinfo();
53       $this->base= dn2base($ui->dn);
54       $this->cn= "";
55     } else {
56       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
57     }
58     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
59     $this->netConfigDNS->acl = $this->acl;
60     /* Save dn for later references */
61     $this->orig_dn= $this->dn;
62   }
64   function execute()
65   {
66     /* Call parent execute */
67     $this->netConfigDNS->acl = $this->acl;
68     plugin::execute();
70     /* Do we represent a valid server? */
71     if (!$this->is_account && $this->parent == NULL){
72       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
73         _("This 'dn' has no server features.")."</b>";
74       return($display);
75     }
77     /* Check for action */
78     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
79       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
80       if ($cmd == ""){
81         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
82       } else {
84         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
85         if ($retval != 0){
86           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
87         } else {
88           $this->didAction= TRUE;
90           /* Set FAIstate */
92           if($this->fai_activated && $this->dn != "new"){
93             $ldap = $this->config->get_ldap_link();
94             $ldap->cd($this->config->current['BASE']);
95             $ldap->cat($this->dn,array("objectClass"));
96             $res = $ldap->fetch();
98             $attrs = array();
99             $attrs['FAIstate'] = "";
100             if(isset($this->mapActions[$_POST['saction']])){
101               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
102             }
104             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
105               $attrs['objectClass'][] = $res['objectClass'][$i];
106             }
108             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
109               $attrs['objectClass'][] = "FAIobject";
110             }
112             if($attrs['FAIstate'] == ""){
113 #FIXME we should check if FAIobject is used anymore
114               $attrs['FAIstate'] = array();
115             }
117             $ldap->cd($this->dn);
118             $ldap->modify($attrs);
119             show_ldap_error($ldap->get_error());
120           }
121         }
122       }
123     }
125     /* Base select dialog */
126     $once = true;
127     foreach($_POST as $name => $value){
128       if(preg_match("/^chooseBase/",$name) && $once){
129         $once = false;
130         $this->dialog = new baseSelectDialog($this->config);
131         $this->dialog->setCurrentBase($this->base);
132       }
133     }
135     /* Dialog handling */
136     if(is_object($this->dialog)){
137       /* Must be called before save_object */
138       $this->dialog->save_object();
140       if($this->dialog->isClosed()){
141         $this->dialog = false;
142       }elseif($this->dialog->isSelected()){
143         $this->base = $this->dialog->isSelected();
144         $this->dialog= false;
145       }else{
146         return($this->dialog->execute());
147       }
148     }
150     /* Fill templating stuff */
151     $smarty= get_smarty();
152     $smarty->assign("bases", $this->config->idepartments);
154     /* Assign attributes */
155     foreach ($this->attributes as $attr){
156       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
157       $smarty->assign("$attr", $this->$attr);
158     }
159     $smarty->assign("baseACL", chkacl($this->acl, "base"));
161     $smarty->assign("staticAddress", "");
162     $smarty->assign("base_select", $this->base);
164     /* Assign status */
165     $query= "fping -q -r 1 -t 500 ".$this->cn;
166     exec ($query, $dummy, $retval);
168     /* Offline */
169     if ($retval == 0){
170       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
171                                        "instant_update" => _("Instant update"),
172                                        "update" => _("Scheduled update"),
173                                        "reinstall" => _("Reinstall"),
174                                        "rescan" => _("Rescan hardware"),
175                                        "memcheck" => _("Memory test"),
176                                        "localboot" => _("Force localboot"),
177                                        "sysinfo"  => _("System analysis")));
178     } else {
179       $smarty->assign("actions", array("wake" => _("Wake up"),
180                                        "reinstall" => _("Reinstall"),
181                                        "update" => _("Scheduled update"),
182                                        "memcheck" => _("Memory test"),
183                                        "localboot" => _("Force localboot"),
184                                        "sysinfo"  => _("System analysis")));
185     }
187     /* Show main page */
188     $this->netConfigDNS->cn= $this->cn;
189     $smarty->assign("netconfig", $this->netConfigDNS->execute());
191     if($this->netConfigDNS->dialog){
192       $this->dialog = TRUE;
193       return($this->netConfigDNS->execute());
194     }else{ 
195       $this->dialog = FALSE;
196     }
197     $smarty->assign("fai_activated",$this->fai_activated);
198     $smarty->assign("modes", $this->modes);
200     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
201   }
203   function remove_from_parent()
204   {
205     $this->netConfigDNS->acl = $this->acl;
206     $this->netConfigDNS->remove_from_parent();
207     $ldap= $this->config->get_ldap_link();
208     $ldap->rmdir($this->dn);
209     show_ldap_error($ldap->get_error(), _("Removing server failed"));
211     /* Delete references to object groups */
212     $ldap->cd ($this->config->current['BASE']);
213     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
214     while ($ldap->fetch()){
215       $og= new ogroup($this->config, $ldap->getDN());
216       unset($og->member[$this->dn]);
217       $og->save ();
218     }
219     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
220   }
223   /* Save data to object */
224   function save_object()
225   {
226     plugin::save_object();
227     $this->netConfigDNS->save_object();
228     /* Save base, since this is no LDAP attribute */
229     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
230       $this->base= $_POST['base'];
231     }
232   }
235   /* Check supplied data */
236   function check()
237   {
238     /* Call common method to give check the hook */
239     $message= plugin::check();
240     $message= array_merge($message, $this->netConfigDNS->check());
241     $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
243     /* must: cn */
244     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
245       $message[]= _("The required field 'Server name' is not set.");
246     }
248     /* Check if given name is a valid host/dns name */
249     if(!is_dns_name($this->cn) && chkacl ($this->acl, "cn") == ""){
250       $message[] = _("Please specify a valid name for this object.");
251     }
253     $ui= get_userinfo();
254     $acl= get_permissions ($this->dn, $ui->subtreeACL);
255     $acl= get_module_permission($acl, "servgeneric", $this->dn);
256     if (chkacl($acl, "create") != ""){
257       $message[]= _("You have no permissions to create a server on this 'Base'.");
258     }
260     if ($this->orig_dn != $this->dn){
261       $ldap= $this->config->get_ldap_link();
262       $ldap->cd ($this->base);
263       $ldap->search ("(cn=".$this->cn.")", array("cn"));
264       if ($ldap->count() != 0){
265         while ($attrs= $ldap->fetch()){
266           if ($attrs['dn'] != $this->orig_dn){
267             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,ou=incoming,/",$attrs['dn']) && preg_match("/,ou=servers,ou=systems,/",$attrs['dn'])){
268               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
269               break;
270             }
271           }
272         }
273       }
274     }
276     return ($message);
277   }
280   /* Save to LDAP */
281   function save()
282   {
283     $this->netConfigDNS->acl = $this->acl;
284     plugin::save();
286     /* Remove all empty values */
287     if ($this->orig_dn == 'new'){
288       $attrs= array();
289       foreach ($this->attrs as $key => $val){
290         if (is_array($val) && count($val) == 0){
291           continue;
292         }
293         $attrs[$key]= $val;
294       }
295       $this->attrs= $attrs;
296     }
298     /* Write back to ldap */
299     $ldap= $this->config->get_ldap_link();
300     if ($this->orig_dn == 'new'){
301       $ldap->cd($this->config->current['BASE']);
302       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
303       $ldap->cd($this->dn);
304       $ldap->add($this->attrs);
305       $mode= "add";
306     } else {
307    
308       /* cn is not case sensitive for ldap, but for php it is!! */ 
309       if($this->config->current['DNMODE'] == "cn"){
310         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
311           $this->recursive_move($this->orig_dn, $this->dn);
312           plugin::save();
313         }
314       }else{
315         if ($this->orig_dn != $this->dn){
316           $this->recursive_move($this->orig_dn, $this->dn);
317           plugin::save();
318         }
319       }
320   
321       $ldap->cd($this->dn);
322       $this->cleanup();
323       $ldap->modify ($this->attrs); 
325       $mode= "modify";
326     }
327     show_ldap_error($ldap->get_error(), _("Saving server failed"));
328     $this->netConfigDNS->cn = $this->cn;
329     $this->netConfigDNS->save($this->dn);
331     /* Optionally execute a command after we're done */
332     if(!$this->didAction){
333       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
334     }
335   }
339 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
340 ?>