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" => _("System 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" => _("System 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 if (!$ldap->success()){
234 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
235 }
237 /* Delete references to object groups */
238 $ldap->cd ($this->config->current['BASE']);
239 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
240 while ($ldap->fetch()){
241 $og= new ogroup($this->config, $ldap->getDN());
242 unset($og->member[$this->dn]);
243 $og->save ();
244 }
246 /* Clean queue form entries with this mac
247 */
248 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
249 $q = new gosaSupportDaemon();
250 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
251 }
252 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
253 }
256 /* Save data to object */
257 function save_object()
258 {
259 /* Save current base, to be able to revert to last base,
260 if new base is invalid or not allowed to be selected */
261 $base_tmp = $this->base;
262 plugin::save_object();
263 $this->netConfigDNS->save_object();
265 /* Get base selection */
266 $tmp = $this->get_allowed_bases();
267 if(isset($_POST['base'])){
268 if($tmp[$_POST['base']]){
269 $this->base = $_POST['base'];
270 }else{
271 $this->base = $base_tmp;
272 }
273 }
274 }
277 /* Check supplied data */
278 function check()
279 {
280 /* Call common method to give check the hook */
281 $message= plugin::check();
282 $message= array_merge($message, $this->netConfigDNS->check());
283 $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
285 /* must: cn */
286 if ($this->cn == ""){
287 msgPool::required(_("Server name"));
288 }
290 /* Check if given name is a valid host/dns name */
291 if(!tests::is_dns_name($this->cn)){
292 $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
293 }
295 if ($this->orig_dn != $this->dn){
296 $ldap= $this->config->get_ldap_link();
297 $ldap->cd ($this->base);
298 $ldap->search ("(cn=".$this->cn.")", array("cn"));
299 if ($ldap->count() != 0){
300 while ($attrs= $ldap->fetch()){
301 if ($attrs['dn'] != $this->orig_dn){
302 if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
303 $message[]= msgPool::duplicated(_("Server name"));
304 break;
305 }
306 }
307 }
308 }
309 }
311 return ($message);
312 }
315 /* Save to LDAP */
316 function save()
317 {
318 /* Detect mode changes */
319 $activate= (isset($this->saved_attributes['gotoMode']) &&
320 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
321 $this->gotoMode == "active" &&
322 tests::is_ip($this->netConfigDNS->ipHostNumber));
324 plugin::save();
326 /* Remove all empty values */
327 if ($this->orig_dn == 'new'){
328 $attrs= array();
329 foreach ($this->attrs as $key => $val){
330 if (is_array($val) && count($val) == 0){
331 continue;
332 }
333 $attrs[$key]= $val;
334 }
335 $this->attrs= $attrs;
336 }
338 /* Write back to ldap */
339 $ldap= $this->config->get_ldap_link();
340 if ($this->orig_dn == 'new'){
341 $ldap->cd($this->config->current['BASE']);
342 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
343 $ldap->cd($this->dn);
344 $ldap->add($this->attrs);
345 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
346 $mode= "add";
347 } else {
349 /* cn is not case sensitive for ldap, but for php it is!! */
350 if($this->config->current['DNMODE'] == "cn"){
351 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
352 $this->recursive_move($this->orig_dn, $this->dn);
353 plugin::save();
354 }
355 }else{
356 if ($this->orig_dn != $this->dn){
357 $this->recursive_move($this->orig_dn, $this->dn);
358 plugin::save();
359 }
360 }
362 $ldap->cd($this->dn);
363 $this->cleanup();
364 $ldap->modify ($this->attrs);
365 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
367 $mode= "modify";
368 }
369 if (!$ldap->success()){
370 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
371 }
372 $this->netConfigDNS->cn = $this->cn;
373 $this->netConfigDNS->save();
375 /* Optionally execute a command after we're done */
376 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
378 if ($activate){
380 /* Send installation activation
381 */
382 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
383 $o_queue = new gosaSupportDaemon();
384 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
385 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
386 $tmp = new $evt['CLASS_NAME']($this->config);
387 $tmp->set_type(TRIGGERED_EVENT);
388 $tmp->add_targets(array($this->netConfigDNS->macAddress));
389 if(!$o_queue->append($tmp)){
390 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
391 $o_queue->get_error()),ERROR_DIALOG);
392 }
393 }
394 }
396 }
399 /* Display generic part for server copy & paste */
400 function getCopyDialog()
401 {
402 $vars = array("cn");
404 $smarty = get_smarty();
405 $smarty->assign("cn" ,$this->cn);
406 $smarty->assign("object","server");
407 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
408 $ret = array();
409 $ret['string'] = $str;
410 $ret['status'] = "";
411 return($ret);
412 }
415 function saveCopyDialog()
416 {
417 if(isset($_POST['cn'])){
418 $this->cn = $_POST['cn'];
419 }
420 }
423 function PrepareForCopyPaste($source)
424 {
425 plugin::PrepareForCopyPaste($source);
426 if(isset($source['macAddress'][0])){
427 $this->netConfigDNS->macAddress = $source['macAddress'][0];
428 }
429 if(isset($source['ipHostNumber'][0])){
430 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
431 }
433 }
437 /* Return plugin informations for acl handling */
438 static function plInfo()
439 {
440 return (array(
441 "plShortName" => _("Generic"),
442 "plDescription" => _("Server generic"),
443 "plSelfModify" => FALSE,
444 "plDepends" => array(),
445 "plPriority" => 1,
446 "plSection" => array("administration"),
447 "plCategory" => array("server" => array("description" => _("Server"),
448 "objectClass" => "goServer")),
449 "plProvidedAcls"=> array(
450 "cn" => _("Name"),
451 "description" => _("Description"),
452 "gotoMode" => _("Goto mode"),
453 "base" => _("Base"),
454 "FAIstate" => _("Action flag"))
455 ));
456 }
457 }
459 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
460 ?>