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 "update" => "sceduledupdate",
34 "reinstall" => "install",
35 "rescan" => "",
36 "memcheck" => "memcheck",
37 "sysinfo" => "sysinfo");
39 function servgeneric ($config, $dn= NULL, $parent= NULL)
40 {
41 plugin::plugin ($config, $dn, $parent);
43 $this->ui = get_userinfo();
44 $this->modes["active"]= _("Activated");
45 $this->modes["locked"]= _("Locked");
47 /* Set base */
48 if ($this->dn == "new"){
49 $ui= get_userinfo();
50 $this->base= dn2base($ui->dn);
51 $this->cn= "";
52 } else {
53 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
54 }
55 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
56 $this->netConfigDNS->set_acl_category("server");
57 $this->netConfigDNS->set_acl_base($this->base);
59 /* Save dn for later references */
60 $this->orig_dn= $this->dn;
61 }
63 function execute()
64 {
65 /* Call parent execute */
66 plugin::execute();
68 /* Do we represent a valid server? */
69 if (!$this->is_account && $this->parent == NULL){
70 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
71 _("This 'dn' has no server features.")."</b>";
72 return($display);
73 }
75 /* Check for action */
76 if (isset($_POST['action']) && $this->acvl_is_writeable("FAIstate")){
77 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
78 if ($cmd == ""){
79 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
80 } else {
82 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
83 if ($retval != 0){
84 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
85 } else {
86 $this->didAction= TRUE;
88 /* Set FAIstate */
89 $ldap = $this->config->get_ldap_link();
90 $ldap->cd($this->config->current['BASE']);
91 $ldap->cat($this->dn,array("objectClass"));
92 $res = $ldap->fetch();
94 $attrs = array();
95 $attrs['FAIstate'] = "";
96 if(isset($this->mapActions[$_POST['saction']])){
97 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
98 }
100 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
101 $attrs['objectClass'][] = $res['objectClass'][$i];
102 }
104 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
105 $attrs['objectClass'][] = "FAIobject";
106 }
108 if($attrs['FAIstate'] == ""){
109 #FIXME we should check if FAIobject is used anymore
110 $attrs['FAIstate'] = array();
111 }
113 $ldap->cd($this->dn);
114 $ldap->modify($attrs);
115 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn));
116 }
117 }
118 }
120 /* Base select dialog */
121 $once = true;
122 foreach($_POST as $name => $value){
123 if(preg_match("/^chooseBase/",$name) && $once ){
124 $once = false;
125 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
126 $this->dialog->setCurrentBase($this->base);
127 }
128 }
130 /* Dialog handling */
131 if(is_object($this->dialog)){
132 /* Must be called before save_object */
133 $this->dialog->save_object();
135 if($this->dialog->isClosed()){
136 $this->dialog = false;
137 }elseif($this->dialog->isSelected()){
139 /* Only accept allowed bases */
140 $tmp = $this->get_allowed_bases();
141 if($tmp[$this->dialog->isSelected()]){
142 $this->base = $this->dialog->isSelected();
143 }
144 $this->dialog= false;
145 }else{
146 return($this->dialog->execute());
147 }
148 }
150 /* Fill templating stuff */
151 $smarty= get_smarty();
153 $tmp = $this->plInfo();
154 foreach($tmp['plProvidedAcls'] as $name => $translated){
155 $smarty->assign($name."ACL",$this->getacl($name));
156 }
158 /* Assign base ACL */
159 $smarty->assign("bases" , $this->get_allowed_bases());
161 /* Assign attributes */
162 foreach ($this->attributes as $attr){
163 $smarty->assign("$attr", $this->$attr);
164 }
166 $smarty->assign("staticAddress", "");
167 $smarty->assign("base_select", $this->base);
169 /* Assign status */
170 $query= "fping -q -r 1 -t 500 ".$this->cn;
171 exec ($query, $dummy, $retval);
173 /* Offline */
174 if ($retval == 0){
175 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
176 "instant_update" => _("Instant update"),
177 "update" => _("Scheduled update"),
178 "reinstall" => _("Reinstall"),
179 "rescan" => _("Rescan hardware"),
180 "memcheck" => _("Memory test"),
181 "sysinfo" => _("System analysis")));
182 } else {
183 $smarty->assign("actions", array("wake" => _("Wake up"),
184 "reinstall" => _("Reinstall"),
185 "update" => _("Scheduled update"),
186 "memcheck" => _("Memory test"),
187 "sysinfo" => _("System analysis")));
188 }
190 /* Show main page */
191 $smarty->assign("netconfig", $this->netConfigDNS->execute());
192 $smarty->assign("modes", $this->modes);
194 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
195 }
197 function remove_from_parent()
198 {
199 /* Cancel if there's nothing to do here */
200 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
201 return;
202 }
204 $this->netConfigDNS->remove_from_parent();
205 $ldap= $this->config->get_ldap_link();
206 $ldap->rmdir($this->dn);
207 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn));
209 /* Delete references to object groups */
210 $ldap->cd ($this->config->current['BASE']);
211 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
212 while ($ldap->fetch()){
213 $og= new ogroup($this->config, $ldap->getDN());
214 unset($og->member[$this->dn]);
215 $og->save ();
216 }
217 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
218 }
221 /* Save data to object */
222 function save_object()
223 {
224 /* Save current base, to be able to revert to last base,
225 if new base is invalid or not allowed to be selected */
226 $base_tmp = $this->base;
227 plugin::save_object();
228 $this->netConfigDNS->save_object();
230 /* Get base selection */
231 $tmp = $this->get_allowed_bases();
232 if(isset($_POST['base'])){
233 if($tmp[$_POST['base']]){
234 $this->base = $_POST['base'];
235 }else{
236 $this->base = $base_tmp;
237 }
238 }
239 }
242 /* Check supplied data */
243 function check()
244 {
245 /* Call common method to give check the hook */
246 $message= plugin::check();
247 $message= array_merge($message, $this->netConfigDNS->check());
248 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
250 /* must: cn */
251 if ($this->cn == ""){
252 $message[]= _("The required field 'Server name' is not set.");
253 }
255 if ($this->orig_dn != $this->dn){
256 $ldap= $this->config->get_ldap_link();
257 $ldap->cd ($this->base);
258 $ldap->search ("(cn=".$this->cn.")", array("cn"));
259 if ($ldap->count() != 0){
260 while ($attrs= $ldap->fetch()){
261 if ($attrs['dn'] != $this->orig_dn){
262 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
263 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
264 break;
265 }
266 }
267 }
268 }
269 }
271 return ($message);
272 }
275 /* Save to LDAP */
276 function save()
277 {
278 plugin::save();
280 /* Remove all empty values */
281 if ($this->orig_dn == 'new'){
282 $attrs= array();
283 foreach ($this->attrs as $key => $val){
284 if (is_array($val) && count($val) == 0){
285 continue;
286 }
287 $attrs[$key]= $val;
288 }
289 $this->attrs= $attrs;
290 }
292 /* Write back to ldap */
293 $ldap= $this->config->get_ldap_link();
294 if ($this->orig_dn == 'new'){
295 $ldap->cd($this->config->current['BASE']);
296 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
297 $ldap->cd($this->dn);
298 $ldap->add($this->attrs);
299 $mode= "add";
300 } else {
302 /* cn is not case sensitive for ldap, but for php it is!! */
303 if($this->config->current['DNMODE'] == "cn"){
304 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
305 $this->move($this->orig_dn, $this->dn);
306 plugin::save();
307 }
308 }else{
309 if ($this->orig_dn != $this->dn){
310 $this->move($this->orig_dn, $this->dn);
311 plugin::save();
312 }
313 }
315 $ldap->cd($this->dn);
316 $this->cleanup();
317 $ldap->modify ($this->attrs);
319 $mode= "modify";
320 }
321 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
322 $this->netConfigDNS->cn = $this->cn;
323 $this->netConfigDNS->save($this->dn);
325 /* Optionally execute a command after we're done */
326 if(!$this->didAction){
327 $this->handle_post_events($mode);
328 }
329 }
332 /* Return plugin informations for acl handling */
333 function plInfo()
334 {
335 return (array(
336 "plShortName" => _("Generic"),
337 "plDescription" => _("Server generic"),
338 "plSelfModify" => FALSE,
339 "plDepends" => array(),
340 "plPriority" => 1,
341 "plSection" => array("administration"),
342 "plCategory" => array("server" => array("description" => _("Server"),
343 "objectClass" => "gotoWorkstation")),
344 "plProvidedAcls"=> array(
345 "cn" => _("Name"),
346 "description" => _("Description"),
347 "gotoMode" => _("Goto mode"),
348 "base" => _("Base"),
349 "FAIstate" => _("Action flag"))
350 ));
351 }
352 }
354 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
355 ?>