Code

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