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();
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->modes["active"]= _("Activated");
44 $this->modes["locked"]= _("Locked");
46 /* Set base */
47 if ($this->dn == "new"){
48 $ui= get_userinfo();
49 $this->base= dn2base($ui->dn);
50 $this->cn= "";
51 } else {
52 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
53 }
54 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
55 $this->netConfigDNS->set_acl_category("server");
56 $this->netConfigDNS->set_acl_base($this->base);
58 /* Save dn for later references */
59 $this->orig_dn= $this->dn;
60 }
62 function execute()
63 {
64 /* Call parent execute */
65 plugin::execute();
67 /* Do we represent a valid server? */
68 if (!$this->is_account && $this->parent == NULL){
69 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
70 _("This 'dn' has no server features.")."</b>";
71 return($display);
72 }
74 /* Check for action */
75 if (isset($_POST['action']) && $this->acvl_is_writeable("FAIstate")){
76 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
77 if ($cmd == ""){
78 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
79 } else {
81 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
82 if ($retval != 0){
83 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
84 } else {
85 $this->didAction= TRUE;
87 /* Set FAIstate */
88 $ldap = $this->config->get_ldap_link();
89 $ldap->cd($this->config->current['BASE']);
90 $ldap->cat($this->dn,array("objectClass"));
91 $res = $ldap->fetch();
93 $attrs = array();
94 $attrs['FAIstate'] = "";
95 if(isset($this->mapActions[$_POST['saction']])){
96 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
97 }
99 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
100 $attrs['objectClass'][] = $res['objectClass'][$i];
101 }
103 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
104 $attrs['objectClass'][] = "FAIobject";
105 }
107 if($attrs['FAIstate'] == ""){
108 #FIXME we should check if FAIobject is used anymore
109 $attrs['FAIstate'] = array();
110 }
112 $ldap->cd($this->dn);
113 $ldap->modify($attrs);
114 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn));
115 }
116 }
117 }
119 /* Base select dialog */
120 $once = true;
121 foreach($_POST as $name => $value){
122 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
123 $once = false;
124 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
125 $this->dialog->setCurrentBase($this->base);
126 }
127 }
129 /* Dialog handling */
130 if(is_object($this->dialog)){
131 /* Must be called before save_object */
132 $this->dialog->save_object();
134 if($this->dialog->isClosed()){
135 $this->dialog = false;
136 }elseif($this->dialog->isSelected()){
137 $this->base = $this->dialog->isSelected();
138 $this->dialog= false;
139 }else{
140 return($this->dialog->execute());
141 }
142 }
144 /* Fill templating stuff */
145 $smarty= get_smarty();
147 $tmp = $this->plInfo();
148 foreach($tmp['plProvidedAcls'] as $name => $translated){
149 $smarty->assign($name."ACL",$this->getacl($name));
150 }
152 /* Assign base ACL */
153 $baseACL = $this->getacl("base");
154 if(!$this->acl_is_moveable()) {
155 $baseACL = preg_replace("/w/","",$baseACL);
156 }
157 $smarty->assign("baseACL", $baseACL);
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 plugin::save_object();
225 $this->netConfigDNS->save_object();
227 /* Get base selection */
228 if($this->acl_is_moveable() && isset($_POST['base'])){
229 $this->base = $_POST['base'];
230 }
232 }
235 /* Check supplied data */
236 function check()
237 {
238 /* Call common method to give check the hook */
239 $message= plugin::check();
240 $message= array_merge($message, $this->netConfigDNS->check());
241 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
243 /* must: cn */
244 if ($this->cn == ""){
245 $message[]= _("The required field 'Server name' is not set.");
246 }
248 /* Set new acl base */
249 if($this->dn == "new") {
250 $this->set_acl_base($this->base);
251 }
253 $ui= get_userinfo();
254 if (!$this->acl_is_createable() && $this->dn == "new"){
255 $message[]= _("You have no permissions to create a server on this 'Base'.");
256 }
258 if ($this->orig_dn != $this->dn){
259 $ldap= $this->config->get_ldap_link();
260 $ldap->cd ($this->base);
261 $ldap->search ("(cn=".$this->cn.")", array("cn"));
262 if ($ldap->count() != 0){
263 while ($attrs= $ldap->fetch()){
264 if ($attrs['dn'] != $this->orig_dn){
265 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
266 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
267 break;
268 }
269 }
270 }
271 }
272 }
274 return ($message);
275 }
278 /* Save to LDAP */
279 function save()
280 {
281 plugin::save();
283 /* Remove all empty values */
284 if ($this->orig_dn == 'new'){
285 $attrs= array();
286 foreach ($this->attrs as $key => $val){
287 if (is_array($val) && count($val) == 0){
288 continue;
289 }
290 $attrs[$key]= $val;
291 }
292 $this->attrs= $attrs;
293 }
295 /* Write back to ldap */
296 $ldap= $this->config->get_ldap_link();
297 if ($this->orig_dn == 'new'){
298 $ldap->cd($this->config->current['BASE']);
299 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
300 $ldap->cd($this->dn);
301 $ldap->add($this->attrs);
302 $mode= "add";
303 } else {
305 /* cn is not case sensitive for ldap, but for php it is!! */
306 if($this->config->current['DNMODE'] == "cn"){
307 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
308 $this->move($this->orig_dn, $this->dn);
309 plugin::save();
310 }
311 }else{
312 if ($this->orig_dn != $this->dn){
313 $this->move($this->orig_dn, $this->dn);
314 plugin::save();
315 }
316 }
318 $ldap->cd($this->dn);
319 $this->cleanup();
320 $ldap->modify ($this->attrs);
322 $mode= "modify";
323 }
324 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
325 $this->netConfigDNS->cn = $this->cn;
326 $this->netConfigDNS->save($this->dn);
328 /* Optionally execute a command after we're done */
329 if(!$this->didAction){
330 $this->handle_post_events($mode);
331 }
332 }
335 /* Return plugin informations for acl handling */
336 function plInfo()
337 {
338 return (array(
339 "plShortName" => _("Generic"),
340 "plDescription" => _("Server generic"),
341 "plSelfModify" => FALSE,
342 "plDepends" => array(),
343 "plPriority" => 1,
344 "plSection" => array("administration"),
345 "plCategory" => array("server" => array("description" => _("Server"),
346 "objectClass" => "gotoWorkstation")),
347 "plProvidedAcls"=> array(
348 "cn" => _("Name"),
349 "description" => _("Description"),
350 "gotoMode" => _("Goto mode"),
351 "base" => _("Base"),
352 "FAIstate" => _("Action flag"))
353 ));
354 }
355 }
357 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
358 ?>