f136a1c16c66706210948358ef3a4ec29bce2936
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();
30 var $ui ;
31 var $mapActions = array("reboot" => "",
32 "instant_update" => "softupdate",
33 "localboot" => "localboot",
34 "update" => "sceduledupdate",
35 "reinstall" => "install",
36 "rescan" => "",
37 "memcheck" => "memcheck",
38 "sysinfo" => "sysinfo");
40 var $fai_activated =FALSE;
41 var $view_logged = FALSE;
43 function servgeneric ($config, $dn= NULL, $parent= NULL)
44 {
45 /* Check if FAI is activated */
46 $tmp = search_config($config->data,"faiManagement","CLASS");
47 if(!empty($tmp)){
48 $this->fai_activated = TRUE;
49 }
51 plugin::plugin ($config, $dn, $parent);
53 $this->ui = get_userinfo();
54 $this->modes["active"]= _("Activated");
55 $this->modes["locked"]= _("Locked");
57 /* Set base */
58 if ($this->dn == "new"){
59 $ui= get_userinfo();
60 $this->base= dn2base($ui->dn);
61 $this->cn= "";
62 } else {
63 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
64 }
65 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
66 $this->netConfigDNS->set_acl_category("server");
67 $this->netConfigDNS->set_acl_base($this->base);
69 /* Save dn for later references */
70 $this->orig_dn= $this->dn;
71 }
74 function set_acl_base($base)
75 {
76 plugin::set_acl_base($base);
77 $this->netConfigDNS->set_acl_base($base);
78 }
81 function set_acl_category($cat)
82 {
83 plugin::set_acl_category($cat);
84 $this->netConfigDNS->set_acl_category($cat);
85 }
88 function execute()
89 {
90 /* Call parent execute */
91 plugin::execute();
93 if($this->is_account && !$this->view_logged){
94 $this->view_logged = TRUE;
95 new log("view","server/".get_class($this),$this->dn);
96 }
98 /* Do we represent a valid server? */
99 if (!$this->is_account && $this->parent == NULL){
100 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
101 _("This 'dn' has no server features.")."</b>";
102 return($display);
103 }
105 /* Check for action */
106 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
107 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
108 if ($cmd == ""){
109 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
110 } else {
112 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
113 if ($retval != 0){
114 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
115 } else {
116 $this->didAction= TRUE;
118 /* Set FAIstate */
119 if($this->fai_activated && $this->dn != "new"){
120 $ldap = $this->config->get_ldap_link();
121 $ldap->cd($this->config->current['BASE']);
122 $ldap->cat($this->dn,array("objectClass"));
123 $res = $ldap->fetch();
125 $attrs = array();
126 $attrs['FAIstate'] = "";
127 if(isset($this->mapActions[$_POST['saction']])){
128 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
129 }
131 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
132 $attrs['objectClass'][] = $res['objectClass'][$i];
133 }
135 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
136 $attrs['objectClass'][] = "FAIobject";
137 }
139 if($attrs['FAIstate'] == ""){
140 #FIXME we should check if FAIobject is used anymore
141 $attrs['FAIstate'] = array();
142 }
144 $ldap->cd($this->dn);
145 $ldap->modify($attrs);
146 }
147 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn));
148 }
149 }
150 }
152 /* Base select dialog */
153 $once = true;
154 foreach($_POST as $name => $value){
155 if(preg_match("/^chooseBase/",$name) && $once ){
156 $once = false;
157 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
158 $this->dialog->setCurrentBase($this->base);
159 }
160 }
162 /* Dialog handling */
163 if(is_object($this->dialog)){
164 /* Must be called before save_object */
165 $this->dialog->save_object();
167 if($this->dialog->isClosed()){
168 $this->dialog = false;
169 }elseif($this->dialog->isSelected()){
171 /* Only accept allowed bases */
172 $tmp = $this->get_allowed_bases();
173 if($tmp[$this->dialog->isSelected()]){
174 $this->base = $this->dialog->isSelected();
175 }
176 $this->dialog= false;
177 }else{
178 return($this->dialog->execute());
179 }
180 }
182 /* Fill templating stuff */
183 $smarty= get_smarty();
185 $tmp = $this->plInfo();
186 foreach($tmp['plProvidedAcls'] as $name => $translated){
187 $smarty->assign($name."ACL",$this->getacl($name));
188 }
190 /* Assign base ACL */
191 $smarty->assign("bases" , $this->get_allowed_bases());
193 /* Assign attributes */
194 foreach ($this->attributes as $attr){
195 $smarty->assign("$attr", $this->$attr);
196 }
198 $smarty->assign("staticAddress", "");
199 $smarty->assign("base_select", $this->base);
201 /* Assign status */
202 $query= "fping -q -r 1 -t 500 ".$this->cn;
203 exec ($query, $dummy, $retval);
205 /* Offline */
206 if ($retval == 0){
207 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
208 "instant_update" => _("Instant update"),
209 "update" => _("Scheduled update"),
210 "reinstall" => _("Reinstall"),
211 "rescan" => _("Rescan hardware"),
212 "memcheck" => _("Memory test"),
213 "localboot" => _("Force localboot"),
214 "sysinfo" => _("System analysis")));
215 } else {
216 $smarty->assign("actions", array("wake" => _("Wake up"),
217 "reinstall" => _("Reinstall"),
218 "update" => _("Scheduled update"),
219 "memcheck" => _("Memory test"),
220 "localboot" => _("Force localboot"),
221 "sysinfo" => _("System analysis")));
222 }
224 /* Show main page */
225 $smarty->assign("fai_activated",$this->fai_activated);
226 $smarty->assign("netconfig", $this->netConfigDNS->execute());
227 $smarty->assign("modes", $this->modes);
229 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
230 }
232 function remove_from_parent()
233 {
234 /* Cancel if there's nothing to do here */
235 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
236 return;
237 }
239 $this->netConfigDNS->remove_from_parent();
240 $ldap= $this->config->get_ldap_link();
241 $ldap->rmdir($this->dn);
243 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
245 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn));
247 /* Delete references to object groups */
248 $ldap->cd ($this->config->current['BASE']);
249 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
250 while ($ldap->fetch()){
251 $og= new ogroup($this->config, $ldap->getDN());
252 unset($og->member[$this->dn]);
253 $og->save ();
254 }
255 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
256 }
259 /* Save data to object */
260 function save_object()
261 {
262 /* Save current base, to be able to revert to last base,
263 if new base is invalid or not allowed to be selected */
264 $base_tmp = $this->base;
265 plugin::save_object();
266 $this->netConfigDNS->save_object();
268 /* Get base selection */
269 $tmp = $this->get_allowed_bases();
270 if(isset($_POST['base'])){
271 if($tmp[$_POST['base']]){
272 $this->base = $_POST['base'];
273 }else{
274 $this->base = $base_tmp;
275 }
276 }
277 }
280 /* Check supplied data */
281 function check()
282 {
283 /* Call common method to give check the hook */
284 $message= plugin::check();
285 $message= array_merge($message, $this->netConfigDNS->check());
286 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
288 /* must: cn */
289 if ($this->cn == ""){
290 $message[]= _("The required field 'Server name' is not set.");
291 }
293 if ($this->orig_dn != $this->dn){
294 $ldap= $this->config->get_ldap_link();
295 $ldap->cd ($this->base);
296 $ldap->search ("(cn=".$this->cn.")", array("cn"));
297 if ($ldap->count() != 0){
298 while ($attrs= $ldap->fetch()){
299 if ($attrs['dn'] != $this->orig_dn){
300 if(!preg_match("/,ou=incoming,/",$attrs['dn']) && preg_match("/,ou=servers,ou=systems,/",$attrs['dn'])){
301 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
302 break;
303 }
304 }
305 }
306 }
307 }
309 return ($message);
310 }
313 /* Save to LDAP */
314 function save()
315 {
316 plugin::save();
318 /* Remove all empty values */
319 if ($this->orig_dn == 'new'){
320 $attrs= array();
321 foreach ($this->attrs as $key => $val){
322 if (is_array($val) && count($val) == 0){
323 continue;
324 }
325 $attrs[$key]= $val;
326 }
327 $this->attrs= $attrs;
328 }
330 /* Write back to ldap */
331 $ldap= $this->config->get_ldap_link();
332 if ($this->orig_dn == 'new'){
333 $ldap->cd($this->config->current['BASE']);
334 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
335 $ldap->cd($this->dn);
336 $ldap->add($this->attrs);
337 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
338 $mode= "add";
339 } else {
341 /* cn is not case sensitive for ldap, but for php it is!! */
342 if($this->config->current['DNMODE'] == "cn"){
343 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
344 $this->recursive_move($this->orig_dn, $this->dn);
345 plugin::save();
346 }
347 }else{
348 if ($this->orig_dn != $this->dn){
349 $this->recursive_move($this->orig_dn, $this->dn);
350 plugin::save();
351 }
352 }
354 $ldap->cd($this->dn);
355 $this->cleanup();
356 $ldap->modify ($this->attrs);
357 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
359 $mode= "modify";
360 }
361 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
362 $this->netConfigDNS->cn = $this->cn;
363 $this->netConfigDNS->save($this->dn);
365 /* Optionally execute a command after we're done */
366 if(!$this->didAction){
367 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
368 }
369 }
372 /* Display generic part for server copy & paste */
373 function getCopyDialog()
374 {
375 $vars = array("cn");
377 $smarty = get_smarty();
378 $smarty->assign("cn" ,$this->cn);
379 $smarty->assign("object","server");
380 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
381 $ret = array();
382 $ret['string'] = $str;
383 $ret['status'] = "";
384 return($ret);
385 }
388 function saveCopyDialog()
389 {
390 if(isset($_POST['cn'])){
391 $this->cn = $_POST['cn'];
392 }
393 }
396 function PrepareForCopyPaste($source)
397 {
398 plugin::PrepareForCopyPaste($source);
399 if(isset($source['macAddress'][0])){
400 $this->netConfigDNS->macAddress = $source['macAddress'][0];
401 }
402 if(isset($source['ipHostNumber'][0])){
403 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
404 }
406 }
410 /* Return plugin informations for acl handling */
411 function plInfo()
412 {
413 return (array(
414 "plShortName" => _("Generic"),
415 "plDescription" => _("Server generic"),
416 "plSelfModify" => FALSE,
417 "plDepends" => array(),
418 "plPriority" => 1,
419 "plSection" => array("administration"),
420 "plCategory" => array("server" => array("description" => _("Server"),
421 "objectClass" => "goServer")),
422 "plProvidedAcls"=> array(
423 "cn" => _("Name"),
424 "description" => _("Description"),
425 "gotoMode" => _("Goto mode"),
426 "base" => _("Base"),
427 "FAIstate" => _("Action flag"))
428 ));
429 }
430 }
432 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
433 ?>