9039ef07700d2b91da296d60dcdd217ab5cae58b
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 var $fai_activated = FALSE;
41 function servgeneric ($config, $dn= NULL, $parent= NULL)
42 {
43 plugin::plugin ($config, $dn, $parent);
45 /* Check if FAI is activated */
46 $tmp = search_config($config->data,"faiManagement","CLASS");
47 if(!empty($tmp)){
48 $this->fai_activated = TRUE;
49 }
51 $this->modes["active"]= _("Activated");
52 $this->modes["locked"]= _("Locked");
54 /* Set base */
55 if ($this->dn == "new"){
56 $ui= get_userinfo();
57 $this->base= dn2base($ui->dn);
58 $this->cn= "";
59 } else {
60 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
61 }
62 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
63 $this->netConfigDNS->acl = $this->acl;
64 /* Save dn for later references */
65 $this->orig_dn= $this->dn;
66 }
68 function execute()
69 {
70 /* Call parent execute */
71 $this->netConfigDNS->acl = $this->acl;
72 plugin::execute();
74 /* Do we represent a valid server? */
75 if (!$this->is_account && $this->parent == NULL){
76 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
77 _("This 'dn' has no server features.")."</b>";
78 return($display);
79 }
81 /* Check for action */
82 if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
83 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
84 if ($cmd == ""){
85 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
86 } else {
88 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
89 if ($retval != 0){
90 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
91 } else {
92 $this->didAction= TRUE;
94 /* Set FAIstate */
96 if($this->fai_activated && $this->dn != "new"){
97 $ldap = $this->config->get_ldap_link();
98 $ldap->cd($this->config->current['BASE']);
99 $ldap->cat($this->dn,array("objectClass"));
100 $res = $ldap->fetch();
102 $attrs = array();
103 $attrs['FAIstate'] = "";
104 if(isset($this->mapActions[$_POST['saction']])){
105 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
106 }
108 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
109 $attrs['objectClass'][] = $res['objectClass'][$i];
110 }
112 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
113 $attrs['objectClass'][] = "FAIobject";
114 }
116 if($attrs['FAIstate'] == ""){
117 #FIXME we should check if FAIobject is used anymore
118 $attrs['FAIstate'] = array();
119 }
121 $ldap->cd($this->dn);
122 $ldap->modify($attrs);
123 show_ldap_error($ldap->get_error());
124 }
125 }
126 }
127 }
129 /* Base select dialog */
130 $once = true;
131 foreach($_POST as $name => $value){
132 if(preg_match("/^chooseBase/",$name) && $once){
133 $once = false;
134 $this->dialog = new baseSelectDialog($this->config);
135 $this->dialog->setCurrentBase($this->base);
136 }
137 }
139 /* Dialog handling */
140 if(is_object($this->dialog)){
141 /* Must be called before save_object */
142 $this->dialog->save_object();
144 if($this->dialog->isClosed()){
145 $this->dialog = false;
146 }elseif($this->dialog->isSelected()){
147 $this->base = $this->dialog->isSelected();
148 $this->dialog= false;
149 }else{
150 return($this->dialog->execute());
151 }
152 }
154 /* Fill templating stuff */
155 $smarty= get_smarty();
156 $smarty->assign("bases", $this->config->idepartments);
158 /* Assign attributes */
159 foreach ($this->attributes as $attr){
160 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
161 $smarty->assign("$attr", $this->$attr);
162 }
163 $smarty->assign("baseACL", chkacl($this->acl, "base"));
165 $smarty->assign("staticAddress", "");
166 $smarty->assign("base_select", $this->base);
168 /* Assign status */
169 $query= "fping -q -r 1 -t 500 ".$this->cn;
170 exec ($query, $dummy, $retval);
172 /* Offline */
173 if ($retval == 0){
174 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
175 "instant_update" => _("Instant update"),
176 "update" => _("Scheduled update"),
177 "reinstall" => _("Reinstall"),
178 "rescan" => _("Rescan hardware"),
179 "memcheck" => _("Memory test"),
180 "sysinfo" => _("System analysis")));
181 } else {
182 $smarty->assign("actions", array("wake" => _("Wake up"),
183 "reinstall" => _("Reinstall"),
184 "update" => _("Scheduled update"),
185 "memcheck" => _("Memory test"),
186 "sysinfo" => _("System analysis")));
187 }
189 /* Show main page */
190 $this->netConfigDNS->cn= $this->cn;
191 $smarty->assign("netconfig", $this->netConfigDNS->execute());
192 $smarty->assign("fai_activated",$this->fai_activated);
193 $smarty->assign("modes", $this->modes);
195 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
196 }
198 function remove_from_parent()
199 {
200 $this->netConfigDNS->acl = $this->acl;
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(), _("Removing server failed"));
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();
223 /* Save base, since this is no LDAP attribute */
224 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
225 $this->base= $_POST['base'];
226 }
227 }
230 /* Check supplied data */
231 function check()
232 {
233 /* Call common method to give check the hook */
234 $message= plugin::check();
235 $message= array_merge($message, $this->netConfigDNS->check());
236 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
238 /* must: cn */
239 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
240 $message[]= _("The required field 'Server name' is not set.");
241 }
243 $ui= get_userinfo();
244 $acl= get_permissions ($this->dn, $ui->subtreeACL);
245 $acl= get_module_permission($acl, "servgeneric", $this->dn);
246 if (chkacl($acl, "create") != ""){
247 $message[]= _("You have no permissions to create a server on this 'Base'.");
248 }
250 if ($this->orig_dn != $this->dn){
251 $ldap= $this->config->get_ldap_link();
252 $ldap->cd ($this->base);
253 $ldap->search ("(cn=".$this->cn.")", array("cn"));
254 if ($ldap->count() != 0){
255 while ($attrs= $ldap->fetch()){
256 if ($attrs['dn'] != $this->orig_dn){
257 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
258 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
259 break;
260 }
261 }
262 }
263 }
264 }
266 return ($message);
267 }
270 /* Save to LDAP */
271 function save()
272 {
273 $this->netConfigDNS->acl = $this->acl;
274 plugin::save();
276 /* Remove all empty values */
277 if ($this->orig_dn == 'new'){
278 $attrs= array();
279 foreach ($this->attrs as $key => $val){
280 if (is_array($val) && count($val) == 0){
281 continue;
282 }
283 $attrs[$key]= $val;
284 }
285 $this->attrs= $attrs;
286 }
288 /* Write back to ldap */
289 $ldap= $this->config->get_ldap_link();
290 if ($this->orig_dn == 'new'){
291 $ldap->cd($this->config->current['BASE']);
292 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
293 $ldap->cd($this->dn);
294 $ldap->add($this->attrs);
295 $mode= "add";
296 } else {
298 /* cn is not case sensitive for ldap, but for php it is!! */
299 if($this->config->current['DNMODE'] == "cn"){
300 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
301 $this->move($this->orig_dn, $this->dn);
302 plugin::save();
303 }
304 }else{
305 if ($this->orig_dn != $this->dn){
306 $this->move($this->orig_dn, $this->dn);
307 plugin::save();
308 }
309 }
311 $ldap->cd($this->dn);
312 $this->cleanup();
313 $ldap->modify ($this->attrs);
315 $mode= "modify";
316 }
317 show_ldap_error($ldap->get_error(), _("Saving server failed"));
318 $this->netConfigDNS->cn = $this->cn;
319 $this->netConfigDNS->save($this->dn);
321 /* Optionally execute a command after we're done */
322 if(!$this->didAction){
323 $this->handle_post_events($mode);
324 }
325 }
327 }
329 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
330 ?>