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