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 "wake" => "",
38 "memcheck" => "memcheck",
39 "sysinfo" => "sysinfo");
41 var $fai_activated =FALSE;
42 var $view_logged = FALSE;
44 function servgeneric (&$config, $dn= NULL, $parent= NULL)
45 {
46 /* Check if FAI is activated */
47 $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
49 if(!empty($tmp)){
50 $this->fai_activated = TRUE;
51 }
53 plugin::plugin ($config, $dn, $parent);
55 $this->ui = get_userinfo();
56 $this->modes["active"]= _("Activated");
57 $this->modes["locked"]= _("Locked");
59 /* Set base */
60 if ($this->dn == "new"){
61 $ui= get_userinfo();
62 $this->base= dn2base($ui->dn);
63 $this->cn= "";
64 } else {
65 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
66 }
67 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
68 $this->netConfigDNS->set_acl_category("server");
69 $this->netConfigDNS->set_acl_base($this->base);
71 /* Save dn for later references */
72 $this->orig_dn= $this->dn;
73 }
76 function set_acl_base($base)
77 {
78 plugin::set_acl_base($base);
79 $this->netConfigDNS->set_acl_base($base);
80 }
83 function set_acl_category($cat)
84 {
85 plugin::set_acl_category($cat);
86 $this->netConfigDNS->set_acl_category($cat);
87 }
90 function execute()
91 {
92 /* Call parent execute */
93 plugin::execute();
95 if($this->is_account && !$this->view_logged){
96 $this->view_logged = TRUE;
97 new log("view","server/".get_class($this),$this->dn);
98 }
100 /* Do we represent a valid server? */
101 if (!$this->is_account && $this->parent === NULL){
102 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
103 _("This 'dn' has no server features.")."</b>";
104 return($display);
105 }
107 /* Check for action */
108 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
109 $method= "gosa";
110 $action= $_POST['saction'];
111 if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
112 $method= "job";
113 }
114 gosaSupportDaemon::send("${method}_trigger_action_".$action, $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
116 $this->didAction= TRUE;
118 if ($_POST['saction'] != "wake") {
120 /* Set FAIstate */
121 if($this->fai_activated && $this->dn != "new"){
122 $ldap = $this->config->get_ldap_link();
123 $ldap->cd($this->config->current['BASE']);
124 $ldap->cat($this->dn,array("objectClass"));
125 $res = $ldap->fetch();
127 $attrs = array();
128 $attrs['FAIstate'] = "";
129 if(isset($this->mapActions[$_POST['saction']])){
130 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
131 }
133 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
134 $attrs['objectClass'][] = $res['objectClass'][$i];
135 }
137 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
138 $attrs['objectClass'][] = "FAIobject";
139 }
141 if($attrs['FAIstate'] == ""){
142 #FIXME we should check if FAIobject is used anymore
143 $attrs['FAIstate'] = array();
144 }
146 $ldap->cd($this->dn);
147 $ldap->modify($attrs);
148 }
149 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn));
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 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
204 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
205 "instant_update" => _("Instant update"),
206 "update" => _("Scheduled update"),
207 "reinstall" => _("Reinstall"),
208 "rescan" => _("Rescan hardware"),
209 "memcheck" => _("Memory test"),
210 "localboot" => _("Force localboot"),
211 "sysinfo" => _("System analysis")));
212 } else {
213 $smarty->assign("actions", array("wake" => _("Wake up"),
214 "reinstall" => _("Reinstall"),
215 "update" => _("Scheduled update"),
216 "memcheck" => _("Memory test"),
217 "localboot" => _("Force localboot"),
218 "sysinfo" => _("System analysis")));
219 }
221 /* Show main page */
222 $smarty->assign("fai_activated",$this->fai_activated);
224 $str = $this->netConfigDNS->execute();
225 if(is_object($this->netConfigDNS->dialog)){
226 return($str);
227 }
228 $smarty->assign("netconfig", $str);
229 $smarty->assign("modes", $this->modes);
231 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
232 }
234 function remove_from_parent()
235 {
236 /* Cancel if there's nothing to do here */
237 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
238 return;
239 }
241 $this->netConfigDNS->remove_from_parent();
242 $ldap= $this->config->get_ldap_link();
243 $ldap->rmdir($this->dn);
245 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
247 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn));
249 /* Delete references to object groups */
250 $ldap->cd ($this->config->current['BASE']);
251 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
252 while ($ldap->fetch()){
253 $og= new ogroup($this->config, $ldap->getDN());
254 unset($og->member[$this->dn]);
255 $og->save ();
256 }
257 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
258 }
261 /* Save data to object */
262 function save_object()
263 {
264 /* Save current base, to be able to revert to last base,
265 if new base is invalid or not allowed to be selected */
266 $base_tmp = $this->base;
267 plugin::save_object();
268 $this->netConfigDNS->save_object();
270 /* Get base selection */
271 $tmp = $this->get_allowed_bases();
272 if(isset($_POST['base'])){
273 if($tmp[$_POST['base']]){
274 $this->base = $_POST['base'];
275 }else{
276 $this->base = $base_tmp;
277 }
278 }
279 }
282 /* Check supplied data */
283 function check()
284 {
285 /* Call common method to give check the hook */
286 $message= plugin::check();
287 $message= array_merge($message, $this->netConfigDNS->check());
288 $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
290 /* must: cn */
291 if ($this->cn == ""){
292 $message[]= _("The required field 'Server name' is not set.");
293 }
295 /* Check if given name is a valid host/dns name */
296 if(!tests::is_dns_name($this->cn)){
297 $message[] = _("Please specify a valid name for this object.");
298 }
300 if ($this->orig_dn != $this->dn){
301 $ldap= $this->config->get_ldap_link();
302 $ldap->cd ($this->base);
303 $ldap->search ("(cn=".$this->cn.")", array("cn"));
304 if ($ldap->count() != 0){
305 while ($attrs= $ldap->fetch()){
306 if ($attrs['dn'] != $this->orig_dn){
307 if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
308 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
309 break;
310 }
311 }
312 }
313 }
314 }
316 return ($message);
317 }
320 /* Save to LDAP */
321 function save()
322 {
323 /* Detect mode changes */
324 $activate= (isset($this->saved_attributes['gotoMode']) &&
325 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
326 $this->gotoMode == "active" &&
327 tests::is_ip($this->netConfigDNS->ipHostNumber));
329 plugin::save();
331 /* Remove all empty values */
332 if ($this->orig_dn == 'new'){
333 $attrs= array();
334 foreach ($this->attrs as $key => $val){
335 if (is_array($val) && count($val) == 0){
336 continue;
337 }
338 $attrs[$key]= $val;
339 }
340 $this->attrs= $attrs;
341 }
343 /* Write back to ldap */
344 $ldap= $this->config->get_ldap_link();
345 if ($this->orig_dn == 'new'){
346 $ldap->cd($this->config->current['BASE']);
347 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
348 $ldap->cd($this->dn);
349 $ldap->add($this->attrs);
350 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
351 $mode= "add";
352 } else {
354 /* cn is not case sensitive for ldap, but for php it is!! */
355 if($this->config->current['DNMODE'] == "cn"){
356 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
357 $this->recursive_move($this->orig_dn, $this->dn);
358 plugin::save();
359 }
360 }else{
361 if ($this->orig_dn != $this->dn){
362 $this->recursive_move($this->orig_dn, $this->dn);
363 plugin::save();
364 }
365 }
367 $ldap->cd($this->dn);
368 $this->cleanup();
369 $ldap->modify ($this->attrs);
370 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
372 $mode= "modify";
373 }
374 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
375 $this->netConfigDNS->cn = $this->cn;
376 $this->netConfigDNS->save();
378 /* Optionally execute a command after we're done */
379 if(!$this->didAction){
380 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
381 }
383 if ($activate){
384 gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
385 }
387 }
390 /* Display generic part for server copy & paste */
391 function getCopyDialog()
392 {
393 $vars = array("cn");
395 $smarty = get_smarty();
396 $smarty->assign("cn" ,$this->cn);
397 $smarty->assign("object","server");
398 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
399 $ret = array();
400 $ret['string'] = $str;
401 $ret['status'] = "";
402 return($ret);
403 }
406 function saveCopyDialog()
407 {
408 if(isset($_POST['cn'])){
409 $this->cn = $_POST['cn'];
410 }
411 }
414 function PrepareForCopyPaste($source)
415 {
416 plugin::PrepareForCopyPaste($source);
417 if(isset($source['macAddress'][0])){
418 $this->netConfigDNS->macAddress = $source['macAddress'][0];
419 }
420 if(isset($source['ipHostNumber'][0])){
421 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
422 }
424 }
428 /* Return plugin informations for acl handling */
429 static function plInfo()
430 {
431 return (array(
432 "plShortName" => _("Generic"),
433 "plDescription" => _("Server generic"),
434 "plSelfModify" => FALSE,
435 "plDepends" => array(),
436 "plPriority" => 1,
437 "plSection" => array("administration"),
438 "plCategory" => array("server" => array("description" => _("Server"),
439 "objectClass" => "goServer")),
440 "plProvidedAcls"=> array(
441 "cn" => _("Name"),
442 "description" => _("Description"),
443 "gotoMode" => _("Goto mode"),
444 "base" => _("Base"),
445 "FAIstate" => _("Action flag"))
446 ));
447 }
448 }
450 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
451 ?>