1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class servgeneric extends plugin
24 {
25 /* Generic terminal attributes */
26 var $ignore_account= TRUE;
27 var $interfaces= array();
29 /* Needed values and lists */
30 var $base= "";
31 var $cn= "";
32 var $l= "";
33 var $description= "";
34 var $orig_dn= "";
36 /* attribute list for save action */
37 var $attributes= array("cn", "description","gotoMode");
38 var $objectclasses= array("top", "GOhard", "goServer");
40 var $gotoMode= "locked";
42 var $netConfigDNS;
43 var $modes = array();
44 var $ui ;
45 var $validActions = array("reboot" => "", "update" => "", "localboot" => "", "reinstall" => "", "rescan" => "",
46 "wake" => "", "memcheck" => "", "sysinfo" => "");
48 var $fai_activated =FALSE;
49 var $view_logged = FALSE;
51 function servgeneric (&$config, $dn= NULL, $parent= NULL)
52 {
53 /* Check if FAI is activated */
54 $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
56 if(!empty($tmp)){
57 $this->fai_activated = TRUE;
58 }
60 plugin::plugin ($config, $dn, $parent);
62 $this->ui = get_userinfo();
63 $this->modes["active"]= _("Activated");
64 $this->modes["locked"]= _("Locked");
66 /* Set base */
67 if ($this->dn == "new"){
68 $ui= get_userinfo();
69 $this->base= dn2base($ui->dn);
70 $this->cn= "";
71 } else {
72 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
73 }
74 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
75 $this->netConfigDNS->set_acl_category("server");
76 $this->netConfigDNS->set_acl_base($this->base);
78 /* Save dn for later references */
79 $this->orig_dn= $this->dn;
80 }
83 function set_acl_base($base)
84 {
85 plugin::set_acl_base($base);
86 $this->netConfigDNS->set_acl_base($base);
87 }
90 function set_acl_category($cat)
91 {
92 plugin::set_acl_category($cat);
93 $this->netConfigDNS->set_acl_category($cat);
94 }
97 function execute()
98 {
99 /* Call parent execute */
100 plugin::execute();
102 if($this->is_account && !$this->view_logged){
103 $this->view_logged = TRUE;
104 new log("view","server/".get_class($this),$this->dn);
105 }
107 /* Do we represent a valid server? */
108 if (!$this->is_account && $this->parent === NULL){
109 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
110 _("This 'dn' has no server features.")."</b>";
111 return($display);
112 }
116 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
117 $action = $_POST['saction'];
119 /* Check if we have an DaemonEvent for this action */
120 if(class_available("DaemonEvent")){
121 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
122 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
123 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
124 $tmp = new $evt['CLASS_NAME']($this->config);
125 $tmp->add_targets(array($this->netConfigDNS->macAddress));
126 $tmp->set_type(TRIGGERED_EVENT);
127 $o_queue = new gosaSupportDaemon();
128 if(!$o_queue->append($tmp)){
129 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
130 $o_queue->get_error()),ERROR_DIALOG);
131 }
132 }
133 }else{
134 msg_dialog::display(_("Event error"),
135 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
136 }
138 }
140 /* Base select dialog */
141 $once = true;
142 foreach($_POST as $name => $value){
143 if(preg_match("/^chooseBase/",$name) && $once ){
144 $once = false;
145 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
146 $this->dialog->setCurrentBase($this->base);
147 }
148 }
150 /* Dialog handling */
151 if(is_object($this->dialog)){
152 /* Must be called before save_object */
153 $this->dialog->save_object();
155 if($this->dialog->isClosed()){
156 $this->dialog = false;
157 }elseif($this->dialog->isSelected()){
159 /* Only accept allowed bases */
160 $tmp = $this->get_allowed_bases();
161 if($tmp[$this->dialog->isSelected()]){
162 $this->base = $this->dialog->isSelected();
163 }
164 $this->dialog= false;
165 }else{
166 return($this->dialog->execute());
167 }
168 }
170 /* Fill templating stuff */
171 $smarty= get_smarty();
173 $tmp = $this->plInfo();
174 foreach($tmp['plProvidedAcls'] as $name => $translated){
175 $smarty->assign($name."ACL",$this->getacl($name));
176 }
178 /* Assign base ACL */
179 $smarty->assign("bases" , $this->get_allowed_bases());
181 /* Assign attributes */
182 foreach ($this->attributes as $attr){
183 $smarty->assign("$attr", $this->$attr);
184 }
186 $smarty->assign("staticAddress", "");
187 $smarty->assign("base_select", $this->base);
189 /* Assign status */
190 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
191 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
192 "update" => _("Update"),
193 "reinstall" => _("Reinstall"),
194 "rescan" => _("Rescan hardware"),
195 "memcheck" => _("Memory test"),
196 "localboot" => _("Force localboot"),
197 "sysinfo" => _("System analysis")));
198 } else {
199 $smarty->assign("actions", array("wake" => _("Wake up"),
200 "reinstall" => _("Reinstall"),
201 "update" => _("Update"),
202 "memcheck" => _("Memory test"),
203 "localboot" => _("Force localboot"),
204 "sysinfo" => _("System analysis")));
205 }
207 /* Show main page */
208 $smarty->assign("fai_activated",$this->fai_activated);
210 $str = $this->netConfigDNS->execute();
211 if(is_object($this->netConfigDNS->dialog)){
212 return($str);
213 }
214 $smarty->assign("netconfig", $str);
215 $smarty->assign("modes", $this->modes);
217 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
218 }
220 function remove_from_parent()
221 {
222 /* Cancel if there's nothing to do here */
223 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
224 return;
225 }
227 $this->netConfigDNS->remove_from_parent();
228 $ldap= $this->config->get_ldap_link();
229 $ldap->rmdir($this->dn);
231 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
233 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn));
235 /* Delete references to object groups */
236 $ldap->cd ($this->config->current['BASE']);
237 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
238 while ($ldap->fetch()){
239 $og= new ogroup($this->config, $ldap->getDN());
240 unset($og->member[$this->dn]);
241 $og->save ();
242 }
243 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
244 }
247 /* Save data to object */
248 function save_object()
249 {
250 /* Save current base, to be able to revert to last base,
251 if new base is invalid or not allowed to be selected */
252 $base_tmp = $this->base;
253 plugin::save_object();
254 $this->netConfigDNS->save_object();
256 /* Get base selection */
257 $tmp = $this->get_allowed_bases();
258 if(isset($_POST['base'])){
259 if($tmp[$_POST['base']]){
260 $this->base = $_POST['base'];
261 }else{
262 $this->base = $base_tmp;
263 }
264 }
265 }
268 /* Check supplied data */
269 function check()
270 {
271 /* Call common method to give check the hook */
272 $message= plugin::check();
273 $message= array_merge($message, $this->netConfigDNS->check());
274 $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
276 /* must: cn */
277 if ($this->cn == ""){
278 msgPool::required(_("Server name"));
279 }
281 /* Check if given name is a valid host/dns name */
282 if(!tests::is_dns_name($this->cn)){
283 $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
284 }
286 if ($this->orig_dn != $this->dn){
287 $ldap= $this->config->get_ldap_link();
288 $ldap->cd ($this->base);
289 $ldap->search ("(cn=".$this->cn.")", array("cn"));
290 if ($ldap->count() != 0){
291 while ($attrs= $ldap->fetch()){
292 if ($attrs['dn'] != $this->orig_dn){
293 if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
294 $message[]= msgPool::duplicated(_("Server name"));
295 break;
296 }
297 }
298 }
299 }
300 }
302 return ($message);
303 }
306 /* Save to LDAP */
307 function save()
308 {
309 /* Detect mode changes */
310 $activate= (isset($this->saved_attributes['gotoMode']) &&
311 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
312 $this->gotoMode == "active" &&
313 tests::is_ip($this->netConfigDNS->ipHostNumber));
315 plugin::save();
317 /* Remove all empty values */
318 if ($this->orig_dn == 'new'){
319 $attrs= array();
320 foreach ($this->attrs as $key => $val){
321 if (is_array($val) && count($val) == 0){
322 continue;
323 }
324 $attrs[$key]= $val;
325 }
326 $this->attrs= $attrs;
327 }
329 /* Write back to ldap */
330 $ldap= $this->config->get_ldap_link();
331 if ($this->orig_dn == 'new'){
332 $ldap->cd($this->config->current['BASE']);
333 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
334 $ldap->cd($this->dn);
335 $ldap->add($this->attrs);
336 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
337 $mode= "add";
338 } else {
340 /* cn is not case sensitive for ldap, but for php it is!! */
341 if($this->config->current['DNMODE'] == "cn"){
342 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
343 $this->recursive_move($this->orig_dn, $this->dn);
344 plugin::save();
345 }
346 }else{
347 if ($this->orig_dn != $this->dn){
348 $this->recursive_move($this->orig_dn, $this->dn);
349 plugin::save();
350 }
351 }
353 $ldap->cd($this->dn);
354 $this->cleanup();
355 $ldap->modify ($this->attrs);
356 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
358 $mode= "modify";
359 }
360 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
361 $this->netConfigDNS->cn = $this->cn;
362 $this->netConfigDNS->save();
364 /* Optionally execute a command after we're done */
365 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
367 if ($activate){
369 /* Send installation activation
370 */
371 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
372 $o_queue = new gosaSupportDaemon();
373 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
374 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
375 $tmp = new $evt['CLASS_NAME']($this->config);
376 $tmp->set_type(TRIGGERED_EVENT);
377 $tmp->add_targets(array($this->netConfigDNS->macAddress));
378 if(!$o_queue->append($tmp)){
379 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
380 $o_queue->get_error()),ERROR_DIALOG);
381 }
382 }
383 }
385 }
388 /* Display generic part for server copy & paste */
389 function getCopyDialog()
390 {
391 $vars = array("cn");
393 $smarty = get_smarty();
394 $smarty->assign("cn" ,$this->cn);
395 $smarty->assign("object","server");
396 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
397 $ret = array();
398 $ret['string'] = $str;
399 $ret['status'] = "";
400 return($ret);
401 }
404 function saveCopyDialog()
405 {
406 if(isset($_POST['cn'])){
407 $this->cn = $_POST['cn'];
408 }
409 }
412 function PrepareForCopyPaste($source)
413 {
414 plugin::PrepareForCopyPaste($source);
415 if(isset($source['macAddress'][0])){
416 $this->netConfigDNS->macAddress = $source['macAddress'][0];
417 }
418 if(isset($source['ipHostNumber'][0])){
419 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
420 }
422 }
426 /* Return plugin informations for acl handling */
427 static function plInfo()
428 {
429 return (array(
430 "plShortName" => _("Generic"),
431 "plDescription" => _("Server generic"),
432 "plSelfModify" => FALSE,
433 "plDepends" => array(),
434 "plPriority" => 1,
435 "plSection" => array("administration"),
436 "plCategory" => array("server" => array("description" => _("Server"),
437 "objectClass" => "goServer")),
438 "plProvidedAcls"=> array(
439 "cn" => _("Name"),
440 "description" => _("Description"),
441 "gotoMode" => _("Goto mode"),
442 "base" => _("Base"),
443 "FAIstate" => _("Action flag"))
444 ));
445 }
446 }
448 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
449 ?>