Code

07fccd1adef8c8245cc80bd4cdd3bb90285d9494
[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 $macAddress= "";
19   var $ipHostNumber= "";
20   var $description= "";
21   var $orig_dn= "";
23   /* attribute list for save action */
24   var $attributes= array("cn", "description", "macAddress", "ipHostNumber");
25   var $objectclasses= array("top", "goServer");
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     }
40     /* Save dn for later references */
41     $this->orig_dn= $this->dn;
42   }
44   function execute()
45   {
46         /* Call parent execute */
47         plugin::execute();
48     /* Do we represent a valid server? */
49     if (!$this->is_account && $this->parent == NULL){
50       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
51         _("This 'dn' has no server features.")."</b>";
52       return($display);
53     }
55     /* Check for action */
56     if (isset($_POST['action'])){
57       switch($_POST['action']){
58         case 'wake':
59           $cmd= search_config($this->config->data['TABS'], "servgeneric", "WAKECMD");
60           if ($cmd == ""){
61             print_red(_("No WAKECMD definition found in your gosa.conf"));
62           } else {
63             exec ($cmd." ".$this->macAddress, $dummy, $retval);
64             if ($retval != 0){
65               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
66             }
67           }
68           break;
70         case 'reboot':
71           $cmd= search_config($this->config->data['TABS'], "servgeneric", "REBOOTCMD");
72           if ($cmd == ""){
73             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
74           } else {
75             exec ($cmd." ".$this->cn, $dummy, $retval);
76             if ($retval != 0){
77               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
78             }
79           }
80           break;
82         case 'halt':
83           $cmd= search_config($this->config->data['TABS'], "servgeneric", "HALTCMD");
84           if ($cmd == ""){
85             print_red(_("No HALTCMD definition found in your gosa.conf"));
86           } else {
87             exec ($cmd." ".$this->cn, $dummy, $retval);
88             if ($retval != 0){
89               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
90             }
91           }
92           break;
93       }
94     }
96     /* Fill templating stuff */
97     $smarty= get_smarty();
98     $smarty->assign("bases", $this->config->idepartments);
100     /* Assign attributes */
101     foreach ($this->attributes as $attr){
102       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
103       $smarty->assign("$attr", $this->$attr);
104     }
105     $smarty->assign("staticAddress", "");
106     $smarty->assign("base_select", $this->base);
108     /* Assign status */
109     $query= "fping -q -r 1 -t 500 ".$this->cn;
110     exec ($query, $dummy, $retval);
112     /* Offline */
113     if ($retval == 0){
114       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
115     } else {
116       $smarty->assign("actions", array("wake" => _("Wake up")));
117     }
120     /* Show main page */
121     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
123     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
124   }
126   function remove_from_parent()
127   {
128     $ldap= $this->config->get_ldap_link();
129     $ldap->rmdir($this->dn);
130     show_ldap_error($ldap->get_error());
132     /* Delete references to object groups */
133     $ldap->cd ($this->config->current['BASE']);
134     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
135     while ($ldap->fetch()){
136       $og= new ogroup($this->config, $ldap->getDN());
137       unset($og->member[$this->dn]);
138       $og->save ();
139     }
141     $this->handle_post_events("remove");
142   }
145   /* Save data to object */
146   function save_object()
147   {
148     plugin::save_object();
150     /* Save base, since this is no LDAP attribute */
151     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
152       $this->base= $_POST['base'];
153     }
154   }
157   /* Check supplied data */
158   function check()
159   {
160     $message= array();
161     $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
163     /* must: cn */
164     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
165       $message[]= _("The required field 'Server name' is not set.");
166     }
168     $ui= get_userinfo();
169     $acl= get_permissions ($this->dn, $ui->subtreeACL);
170     $acl= get_module_permission($acl, "server", $this->dn);
171     if (chkacl($acl, "create") != ""){
172       $message[]= _("You have no permissions to create a server on this 'Base'.");
173     }
175     if ($this->orig_dn != $this->dn){
176       $ldap= $this->config->get_ldap_link();
177       $ldap->cd ($this->base);
178       $ldap->search ("(cn=".$this->cn.")", array("cn"));
179       if ($ldap->count() != 0){
180         while ($attrs= $ldap->fetch()){
181           if ($attrs['dn'] != $this->orig_dn){
182             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
183             break;
184           }
185         }
186       }
187     }
189     return ($message);
190   }
193   /* Save to LDAP */
194   function save()
195   {
196     plugin::save();
198     /* Remove all empty values */
199     if ($this->orig_dn == 'new'){
200       $attrs= array();
201       foreach ($this->attrs as $key => $val){
202         if (is_array($val) && count($val) == 0){
203           continue;
204         }
205         $attrs[$key]= $val;
206       }
207       $this->attrs= $attrs;
208     }
210     /* Write back to ldap */
211     $ldap= $this->config->get_ldap_link();
212     if ($this->orig_dn == 'new'){
213       $ldap->cd($this->config->current['BASE']);
214       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
215       $ldap->cd($this->dn);
216       $ldap->add($this->attrs);
217       $mode= "add";
218     } else {
219    
220       /* cn is not case sensitive for ldap, but for php it is!! */ 
221       if($this->config->current['DNMODE'] == "cn"){
222         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
223           $this->move($this->orig_dn, $this->dn);
224           plugin::save();
225         }
226       }else{
227         if ($this->orig_dn != $this->dn){
228           $this->move($this->orig_dn, $this->dn);
229           plugin::save();
230         }
231       }
232   
233       $ldap->cd($this->dn);
234       $ldap->modify($this->attrs);
235       $mode= "modify";
236     }
237     show_ldap_error($ldap->get_error());
239     /* Optionally execute a command after we're done */
240     $this->handle_post_events($mode);
241   }
245 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
246 ?>