Code

8a2597b06a5eb35b816b274f59146781fe409395
[gosa.git] / gosa-plugins / systems / admin / systems / class_servGeneric.inc
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                             "update"          => "softupdate",
33                             "localboot"       => "localboot",
34                             "reinstall"       => "install",
35                             "rescan"          => "",
36                             "wake"            => "",
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('menu','tabs'));
47     
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);
69        
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>&nbsp;<b>".
102         _("This 'dn' has no server features.")."</b>";
103       return($display);
104     }
108     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
110       /* Check given action */
111       $mapEvent = array("wake"           => "wakeup",
112                         "instant_update" => "update");
113       $action = $_POST['saction'];
114       if(isset($mapEvent[$action])){
115         $action = $mapEvent[$action];
116       }
118       /* Check if we have an DaemonEvent for this action */
119       if(class_available("DaemonEvent_".$action)){
120         $events = DaemonEvent::get_event_types();
121         if(isset($events['BY_CLASS']["DaemonEvent_".$action])){
122           $evt = $events['BY_CLASS']["DaemonEvent_".$action];
123           $tmp = new $evt['CLASS_NAME']($this->config);
124           $tmp->add_targets(array($this->netConfigDNS->macAddress));
125           $tmp->set_type(TRIGGERED_EVENT);
126           $o_queue = new gosaSupportDaemon();
127           if(!$o_queue->append($tmp)){
128             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
129                   $o_queue->get_error()),ERROR_DIALOG);
130           }
131         }
132       }else{
134         msg_dialog::display(_("Missing Daemon Event"),
135                     sprintf(_("The requested action does not exists '%s' Sending event manually."),"DaemonEvent_".$action),ERROR_DIALOG);
137         /* No event found, send action manually.
138          */
139         $method= "gosa";
140         $action= $_POST['saction'];
141         if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
142           $method= "job";
143         }
145         gosaSupportDaemon::send("${method}_trigger_action_".$action,
146             $this->netConfigDNS->macAddress,
147             array("macAddress" => $this->netConfigDNS->macAddress));
148       }
150       $this->didAction= TRUE;
152       if ($_POST['saction'] != "wake") {
154           /* Set FAIstate */
155           if($this->fai_activated && $this->dn != "new"){
156             $ldap = $this->config->get_ldap_link();
157             $ldap->cd($this->config->current['BASE']);
158             $ldap->cat($this->dn,array("objectClass"));
159             $res = $ldap->fetch();
161             $attrs = array();
162             $attrs['FAIstate'] = "";
163             if(isset($this->mapActions[$_POST['saction']])){
164               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
165             }
167             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
168               $attrs['objectClass'][] = $res['objectClass'][$i];
169             }
171             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
172               $attrs['objectClass'][] = "FAIobject";
173             }
175             if($attrs['FAIstate'] == ""){
176               #FIXME we should check if FAIobject is used anymore
177               $attrs['FAIstate'] = array();
178             }
180             $ldap->cd($this->dn);
181             $ldap->modify($attrs);
182           }
183           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
184       }
185     }
187     /* Base select dialog */
188     $once = true;
189     foreach($_POST as $name => $value){
190       if(preg_match("/^chooseBase/",$name) && $once ){
191         $once = false;
192         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
193         $this->dialog->setCurrentBase($this->base);
194       }
195     }
197     /* Dialog handling */
198     if(is_object($this->dialog)){
199       /* Must be called before save_object */
200       $this->dialog->save_object();
202       if($this->dialog->isClosed()){
203         $this->dialog = false;
204       }elseif($this->dialog->isSelected()){
205   
206         /* Only accept allowed bases */
207         $tmp = $this->get_allowed_bases();
208         if($tmp[$this->dialog->isSelected()]){
209           $this->base = $this->dialog->isSelected();
210         }
211         $this->dialog= false;
212       }else{
213         return($this->dialog->execute());
214       }
215     }
217     /* Fill templating stuff */
218     $smarty= get_smarty();
220     $tmp = $this->plInfo();
221     foreach($tmp['plProvidedAcls'] as $name => $translated){
222       $smarty->assign($name."ACL",$this->getacl($name));
223     }
225     /* Assign base ACL */
226     $smarty->assign("bases"   , $this->get_allowed_bases());
228     /* Assign attributes */
229     foreach ($this->attributes as $attr){
230       $smarty->assign("$attr", $this->$attr);
231     }
233     $smarty->assign("staticAddress", "");
234     $smarty->assign("base_select", $this->base);
236     /* Assign status */
237     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
238       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
239                                        "instant_update" => _("Instant update"),
240                                        "update" => _("Scheduled update"),
241                                        "reinstall" => _("Reinstall"),
242                                        "rescan" => _("Rescan hardware"),
243                                        "memcheck" => _("Memory test"),
244                                        "localboot" => _("Force localboot"),
245                                        "sysinfo"  => _("System analysis")));
246     } else {
247       $smarty->assign("actions", array("wake" => _("Wake up"),
248                                        "reinstall" => _("Reinstall"),
249                                        "update" => _("Scheduled update"),
250                                        "memcheck" => _("Memory test"),
251                                        "localboot" => _("Force localboot"),
252                                        "sysinfo"  => _("System analysis")));
253     }
255     /* Show main page */
256     $smarty->assign("fai_activated",$this->fai_activated);
258     $str = $this->netConfigDNS->execute();
259     if(is_object($this->netConfigDNS->dialog)){
260       return($str);
261     }
262     $smarty->assign("netconfig", $str);
263     $smarty->assign("modes", $this->modes);
265     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
266   }
268   function remove_from_parent()
269   {
270     /* Cancel if there's nothing to do here */
271     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
272       return;
273     }
275     $this->netConfigDNS->remove_from_parent();
276     $ldap= $this->config->get_ldap_link();
277     $ldap->rmdir($this->dn);
279     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
281     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
283     /* Delete references to object groups */
284     $ldap->cd ($this->config->current['BASE']);
285     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
286     while ($ldap->fetch()){
287       $og= new ogroup($this->config, $ldap->getDN());
288       unset($og->member[$this->dn]);
289       $og->save ();
290     }
291     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
292   }
295   /* Save data to object */
296   function save_object()
297   {
298     /* Save current base, to be able to revert to last base, 
299         if new base is invalid or not allowed to be selected */
300     $base_tmp = $this->base;
301     plugin::save_object();
302     $this->netConfigDNS->save_object();
304     /* Get base selection */
305     $tmp = $this->get_allowed_bases();
306     if(isset($_POST['base'])){
307       if($tmp[$_POST['base']]){
308         $this->base = $_POST['base'];
309       }else{
310         $this->base = $base_tmp;
311       }
312     }
313   }
316   /* Check supplied data */
317   function check()
318   {
319     /* Call common method to give check the hook */
320     $message= plugin::check();
321     $message= array_merge($message, $this->netConfigDNS->check());
322     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
324     /* must: cn */
325     if ($this->cn == ""){
326       $message[]= _("The required field 'Server name' is not set.");
327     }
328     
329     /* Check if given name is a valid host/dns name */
330     if(!tests::is_dns_name($this->cn)){
331       $message[] = _("Please specify a valid name for this object.");
332     }
334     if ($this->orig_dn != $this->dn){
335       $ldap= $this->config->get_ldap_link();
336       $ldap->cd ($this->base);
337       $ldap->search ("(cn=".$this->cn.")", array("cn"));
338       if ($ldap->count() != 0){
339         while ($attrs= $ldap->fetch()){
340           if ($attrs['dn'] != $this->orig_dn){
341             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
342               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
343               break;
344             }
345           }
346         }
347       }
348     }
350     return ($message);
351   }
354   /* Save to LDAP */
355   function save()
356   {
357     /* Detect mode changes */
358     $activate= (isset($this->saved_attributes['gotoMode']) &&
359         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
360         $this->gotoMode == "active" &&
361         tests::is_ip($this->netConfigDNS->ipHostNumber));
363     plugin::save();
365     /* Remove all empty values */
366     if ($this->orig_dn == 'new'){
367       $attrs= array();
368       foreach ($this->attrs as $key => $val){
369         if (is_array($val) && count($val) == 0){
370           continue;
371         }
372         $attrs[$key]= $val;
373       }
374       $this->attrs= $attrs;
375     }
377     /* Write back to ldap */
378     $ldap= $this->config->get_ldap_link();
379     if ($this->orig_dn == 'new'){
380       $ldap->cd($this->config->current['BASE']);
381       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
382       $ldap->cd($this->dn);
383       $ldap->add($this->attrs);
384       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
385       $mode= "add";
386     } else {
387    
388       /* cn is not case sensitive for ldap, but for php it is!! */ 
389       if($this->config->current['DNMODE'] == "cn"){
390         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
391           $this->recursive_move($this->orig_dn, $this->dn);
392           plugin::save();
393         }
394       }else{
395         if ($this->orig_dn != $this->dn){
396           $this->recursive_move($this->orig_dn, $this->dn);
397           plugin::save();
398         }
399       }
400   
401       $ldap->cd($this->dn);
402       $this->cleanup();
403       $ldap->modify ($this->attrs); 
404       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
406       $mode= "modify";
407     }
408     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
409     $this->netConfigDNS->cn = $this->cn;
410     $this->netConfigDNS->save();
412     /* Optionally execute a command after we're done */
413     if(!$this->didAction){
414       $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
415     }
417     if ($activate){
419       /* Send installation activation 
420        */
421       $events = DaemonEvent::get_event_types();
422       $o_queue = new gosaSupportDaemon();
423       if(isset($events['BY_CLASS']['DaemonEvent_installation_activation'])){
424         $evt = $events['BY_CLASS']['DaemonEvent_installation_activation'];
425         $tmp = new $evt['CLASS_NAME']($this->config);
426         $tmp->set_type(TRIGGERED_EVENT);
427         $tmp->add_targets(array($this->netConfigDNS->macAddress));
428         if(!$o_queue->append($tmp)){
429           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
430                 $o_queue->get_error()),ERROR_DIALOG);
431         }
432       }
433     }
435   }
438   /* Display generic part for server copy & paste */
439   function getCopyDialog()
440   {
441     $vars = array("cn");
443     $smarty = get_smarty();
444     $smarty->assign("cn" ,$this->cn);
445     $smarty->assign("object","server");
446     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
447     $ret = array();
448     $ret['string'] = $str;
449     $ret['status'] = "";
450     return($ret);
451   }
454   function saveCopyDialog()
455   {
456     if(isset($_POST['cn'])){
457       $this->cn = $_POST['cn'];
458     }
459   }
461   
462   function PrepareForCopyPaste($source)  
463   {
464     plugin::PrepareForCopyPaste($source);
465     if(isset($source['macAddress'][0])){
466       $this->netConfigDNS->macAddress = $source['macAddress'][0];
467     }
468     if(isset($source['ipHostNumber'][0])){
469       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
470     }
472   }
473   
476   /* Return plugin informations for acl handling */ 
477   static function plInfo()
478   {
479     return (array( 
480           "plShortName"   => _("Generic"),
481           "plDescription" => _("Server generic"),
482           "plSelfModify"  => FALSE,
483           "plDepends"     => array(),
484           "plPriority"    => 1,
485           "plSection"     => array("administration"),           
486           "plCategory"    => array("server" => array("description"  => _("Server"),
487                                                      "objectClass"  => "goServer")),
488           "plProvidedAcls"=> array(
489             "cn"           => _("Name"),
490             "description"  => _("Description"),
491             "gotoMode"     => _("Goto mode"),
492             "base"         => _("Base"),
493             "FAIstate"     => _("Action flag"))
494           ));
495   }
498 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
499 ?>