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