a28ed75b7143a9f1e2cfab8db4d832f859bdf5e0
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> <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("baseACL", chkacl($this->acl, "base"));
141 $smarty->assign("staticAddress", "");
142 $smarty->assign("base_select", $this->base);
144 /* Assign status */
145 $query= "fping -q -r 1 -t 500 ".$this->cn;
146 exec ($query, $dummy, $retval);
148 /* Offline */
149 if ($retval == 0){
150 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
151 "instant_update" => _("Instant update"),
152 "update" => _("Scheduled update"),
153 "reinstall" => _("Reinstall"),
154 "rescan" => _("Rescan hardware")));
155 } else {
156 $smarty->assign("actions", array("wake" => _("Wake up"),
157 "reinstall" => _("Reinstall"),
158 "update" => _("Scheduled update")));
159 }
161 /* Show main page */
162 $smarty->assign("netconfig", $this->netConfigDNS->execute());
163 $smarty->assign("modes", $this->modes);
165 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
166 }
168 function remove_from_parent()
169 {
170 $this->netConfigDNS->remove_from_parent();
171 $ldap= $this->config->get_ldap_link();
172 $ldap->rmdir($this->dn);
173 show_ldap_error($ldap->get_error(), _("Removing server failed"));
175 /* Delete references to object groups */
176 $ldap->cd ($this->config->current['BASE']);
177 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
178 while ($ldap->fetch()){
179 $og= new ogroup($this->config, $ldap->getDN());
180 unset($og->member[$this->dn]);
181 $og->save ();
182 }
183 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
184 }
187 /* Save data to object */
188 function save_object()
189 {
190 plugin::save_object();
191 $this->netConfigDNS->save_object();
192 /* Save base, since this is no LDAP attribute */
193 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
194 $this->base= $_POST['base'];
195 }
196 }
199 /* Check supplied data */
200 function check()
201 {
202 /* Call common method to give check the hook */
203 $message= plugin::check();
204 $message= array_merge($message, $this->netConfigDNS->check());
205 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
207 /* must: cn */
208 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
209 $message[]= _("The required field 'Server name' is not set.");
210 }
212 $ui= get_userinfo();
213 $acl= get_permissions ($this->dn, $ui->subtreeACL);
214 $acl= get_module_permission($acl, "server", $this->dn);
215 if (chkacl($acl, "create") != ""){
216 $message[]= _("You have no permissions to create a server on this 'Base'.");
217 }
219 if ($this->orig_dn != $this->dn){
220 $ldap= $this->config->get_ldap_link();
221 $ldap->cd ($this->base);
222 $ldap->search ("(cn=".$this->cn.")", array("cn"));
223 if ($ldap->count() != 0){
224 while ($attrs= $ldap->fetch()){
225 if ($attrs['dn'] != $this->orig_dn){
226 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
227 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
228 break;
229 }
230 }
231 }
232 }
233 }
235 return ($message);
236 }
239 /* Save to LDAP */
240 function save()
241 {
242 plugin::save();
244 /* Remove all empty values */
245 if ($this->orig_dn == 'new'){
246 $attrs= array();
247 foreach ($this->attrs as $key => $val){
248 if (is_array($val) && count($val) == 0){
249 continue;
250 }
251 $attrs[$key]= $val;
252 }
253 $this->attrs= $attrs;
254 }
256 /* Write back to ldap */
257 $ldap= $this->config->get_ldap_link();
258 if ($this->orig_dn == 'new'){
259 $ldap->cd($this->config->current['BASE']);
260 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
261 $ldap->cd($this->dn);
262 $ldap->add($this->attrs);
263 $mode= "add";
264 } else {
266 /* cn is not case sensitive for ldap, but for php it is!! */
267 if($this->config->current['DNMODE'] == "cn"){
268 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
269 $this->move($this->orig_dn, $this->dn);
270 plugin::save();
271 }
272 }else{
273 if ($this->orig_dn != $this->dn){
274 $this->move($this->orig_dn, $this->dn);
275 plugin::save();
276 }
277 }
279 $ldap->cd($this->dn);
280 $this->cleanup();
281 $ldap->modify ($this->attrs);
283 $mode= "modify";
284 }
285 $this->netConfigDNS->cn = $this->cn;
286 $this->netConfigDNS->save($this->dn);
287 show_ldap_error($ldap->get_error(), _("Saving server failed"));
289 /* Optionally execute a command after we're done */
290 $this->handle_post_events($mode);
291 }
293 }
295 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
296 ?>