Code

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