Code

0c43263ff4966c65c7d412ee442c5df95f1719b3
[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= "";
21   /* attribute list for save action */
22   var $attributes= array("cn", "description","gotoMode");
23   var $objectclasses= array("top", "GOhard", "goServer");
25   var $gotoMode= "locked";
27   var $netConfigDNS;
28   var $modes = array();
30   function servgeneric ($config, $dn= NULL)
31   {
32     plugin::plugin ($config, $dn);
34     $this->modes["active"]= _("Activated");
35     $this->modes["locked"]= _("Locked");
36     $this->modes["memcheck"]= _("Memory test");
37     $this->modes["sysinfo"]= _("System analysis");
39     /* Set base */
40     if ($this->dn == "new"){
41       $ui= get_userinfo();
42       $this->base= dn2base($ui->dn);
43       $this->cn= "";
44     } else {
45       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
46     }
47     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
48     /* Save dn for later references */
49     $this->orig_dn= $this->dn;
50   }
52   function execute()
53   {
54         /* Call parent execute */
55         plugin::execute();
57     /* Do we represent a valid server? */
58     if (!$this->is_account && $this->parent == NULL){
59       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
60         _("This 'dn' has no server features.")."</b>";
61       return($display);
62     }
64     /* Check for action */
65     if (isset($_POST['action'])){
66       switch($_POST['action']){
67         case 'wake':
68           $cmd= search_config($this->config->data['TABS'], "servgeneric", "WAKECMD");
69           if ($cmd == ""){
70             print_red(_("No WAKECMD definition found in your gosa.conf"));
71           } else {
72             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
73             if ($retval != 0){
74               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
75             }
76           }
77           break;
79         case 'reboot':
80           $cmd= search_config($this->config->data['TABS'], "servgeneric", "REBOOTCMD");
81           if ($cmd == ""){
82             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
83           } else {
84             exec ($cmd." ".$this->cn, $dummy, $retval);
85             if ($retval != 0){
86               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
87             }
88           }
89           break;
91         case 'halt':
92           $cmd= search_config($this->config->data['TABS'], "servgeneric", "HALTCMD");
93           if ($cmd == ""){
94             print_red(_("No HALTCMD definition found in your gosa.conf"));
95           } else {
96             exec ($cmd." ".$this->cn, $dummy, $retval);
97             if ($retval != 0){
98               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
99             }
100           }
101           break;
102       }
103     }
105     /* Base select dialog */
106     $once = true;
107     foreach($_POST as $name => $value){
108       if(preg_match("/^chooseBase/",$name) && $once){
109         $once = false;
110         $this->dialog = new baseSelectDialog($this->config);
111         $this->dialog->setCurrentBase($this->base);
112       }
113     }
115     /* Dialog handling */
116     if(is_object($this->dialog)){
117       /* Must be called before save_object */
118       $this->dialog->save_object();
120       if($this->dialog->isClosed()){
121         $this->dialog = false;
122       }elseif($this->dialog->isSelected()){
123         $this->base = $this->dialog->isSelected();
124         $this->dialog= false;
125       }else{
126         return($this->dialog->execute());
127       }
128     }
130     /* Fill templating stuff */
131     $smarty= get_smarty();
132     $smarty->assign("bases", $this->config->idepartments);
134     /* Assign attributes */
135     foreach ($this->attributes as $attr){
136       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
137       $smarty->assign("$attr", $this->$attr);
138     }
139     $smarty->assign("staticAddress", "");
140     $smarty->assign("base_select", $this->base);
142     /* Assign status */
143     $query= "fping -q -r 1 -t 500 ".$this->cn;
144     exec ($query, $dummy, $retval);
146     /* Offline */
147     if ($retval == 0){
148       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
149     } else {
150       $smarty->assign("actions", array("wake" => _("Wake up")));
151     }
153     /* Show main page */
154     $smarty->assign("netconfig", $this->netConfigDNS->execute());
155     $smarty->assign("modes", $this->modes);
157     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
158   }
160   function remove_from_parent()
161   {
162     $this->netConfigDNS->remove_from_parent();
163     $ldap= $this->config->get_ldap_link();
164     $ldap->rmdir($this->dn);
165     show_ldap_error($ldap->get_error());
167     /* Delete references to object groups */
168     $ldap->cd ($this->config->current['BASE']);
169     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
170     while ($ldap->fetch()){
171       $og= new ogroup($this->config, $ldap->getDN());
172       unset($og->member[$this->dn]);
173       $og->save ();
174     }
175     $this->handle_post_events("remove");
176   }
179   /* Save data to object */
180   function save_object()
181   {
182     plugin::save_object();
183     $this->netConfigDNS->save_object();
184     /* Save base, since this is no LDAP attribute */
185     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
186       $this->base= $_POST['base'];
187     }
188   }
191   /* Check supplied data */
192   function check()
193   {
194     /* Call common method to give check the hook */
195     $message= plugin::check();
196     $message= array_merge($message, $this->netConfigDNS->check());
197     $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
199     /* must: cn */
200     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
201       $message[]= _("The required field 'Server name' is not set.");
202     }
204     $ui= get_userinfo();
205     $acl= get_permissions ($this->dn, $ui->subtreeACL);
206     $acl= get_module_permission($acl, "server", $this->dn);
207     if (chkacl($acl, "create") != ""){
208       $message[]= _("You have no permissions to create a server on this 'Base'.");
209     }
211     if ($this->orig_dn != $this->dn){
212       $ldap= $this->config->get_ldap_link();
213       $ldap->cd ($this->base);
214       $ldap->search ("(cn=".$this->cn.")", array("cn"));
215       if ($ldap->count() != 0){
216         while ($attrs= $ldap->fetch()){
217           if ($attrs['dn'] != $this->orig_dn){
218             if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
219             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
220             break;
221             }
222           }
223         }
224       }
225     }
227     return ($message);
228   }
231   /* Save to LDAP */
232   function save()
233   {
234     plugin::save();
236     /* Remove all empty values */
237     if ($this->orig_dn == 'new'){
238       $attrs= array();
239       foreach ($this->attrs as $key => $val){
240         if (is_array($val) && count($val) == 0){
241           continue;
242         }
243         $attrs[$key]= $val;
244       }
245       $this->attrs= $attrs;
246     }
248     /* Write back to ldap */
249     $ldap= $this->config->get_ldap_link();
250     if ($this->orig_dn == 'new'){
251       $ldap->cd($this->config->current['BASE']);
252       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
253       $ldap->cd($this->dn);
254       $ldap->add($this->attrs);
255       $mode= "add";
256     } else {
257    
258       /* cn is not case sensitive for ldap, but for php it is!! */ 
259       if($this->config->current['DNMODE'] == "cn"){
260         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
261           $this->move($this->orig_dn, $this->dn);
262           plugin::save();
263         }
264       }else{
265         if ($this->orig_dn != $this->dn){
266           $this->move($this->orig_dn, $this->dn);
267           plugin::save();
268         }
269       }
270   
271       $ldap->cd($this->dn);
272       $this->cleanup();
273 $ldap->modify ($this->attrs); 
275       $mode= "modify";
276     }
277     $this->netConfigDNS->cn = $this->cn;
278     $this->netConfigDNS->save($this->dn);
279     show_ldap_error($ldap->get_error());
281     /* Optionally execute a command after we're done */
282     $this->handle_post_events($mode);
283   }
287 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
288 ?>