d730adafecfc220ee460ed65de51ab61afac623f
1 <?php
3 class servgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $ignore_account= TRUE;
7 var $interfaces= array();
9 /* Needed values and lists */
10 var $base= "";
11 var $cn= "";
12 var $l= "";
13 var $description= "";
14 var $orig_dn= "";
15 var $didAction= FALSE;
17 /* attribute list for save action */
18 var $attributes= array("cn", "description","gotoMode");
19 var $objectclasses= array("top", "GOhard", "goServer");
21 var $gotoMode= "locked";
23 var $netConfigDNS;
24 var $modes = array();
26 var $mapActions = array("reboot" => "localboot",
27 "localboot" => "localboot",
28 "instant_update" => "softupdate",
29 "update" => "scheduledupdate",
30 "reinstall" => "install",
31 "rescan" => "",
32 "memcheck" => "memcheck",
33 "sysinfo" => "sysinfo");
35 var $fai_activated = FALSE;
37 function servgeneric ($config, $dn= NULL, $parent= NULL)
38 {
39 plugin::plugin ($config, $dn, $parent);
41 /* Check if FAI is activated */
42 $tmp = search_config($config->data,"faiManagement","CLASS");
43 if(!empty($tmp)){
44 $this->fai_activated = TRUE;
45 }
47 $this->modes["active"]= _("Activated");
48 $this->modes["locked"]= _("Locked");
50 /* Set base */
51 if ($this->dn == "new"){
52 $ui= get_userinfo();
53 $this->base= dn2base($ui->dn);
54 $this->cn= "";
55 } else {
56 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
57 }
58 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
59 $this->netConfigDNS->acl = $this->acl;
60 /* Save dn for later references */
61 $this->orig_dn= $this->dn;
62 }
64 function execute()
65 {
66 /* Call parent execute */
67 $this->netConfigDNS->acl = $this->acl;
68 plugin::execute();
70 /* Do we represent a valid server? */
71 if (!$this->is_account && $this->parent == NULL){
72 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
73 _("This 'dn' has no server features.")."</b>";
74 return($display);
75 }
77 /* Check for action */
78 if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
79 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
80 if ($cmd == ""){
81 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
82 } else {
84 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
85 if ($retval != 0){
86 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
87 } else {
88 $this->didAction= TRUE;
90 /* Set FAIstate */
92 if($this->fai_activated && $this->dn != "new"){
93 $ldap = $this->config->get_ldap_link();
94 $ldap->cd($this->config->current['BASE']);
95 $ldap->cat($this->dn,array("objectClass"));
96 $res = $ldap->fetch();
98 $attrs = array();
99 $attrs['FAIstate'] = "";
100 if(isset($this->mapActions[$_POST['saction']])){
101 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
102 }
104 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
105 $attrs['objectClass'][] = $res['objectClass'][$i];
106 }
108 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
109 $attrs['objectClass'][] = "FAIobject";
110 }
112 if($attrs['FAIstate'] == ""){
113 #FIXME we should check if FAIobject is used anymore
114 $attrs['FAIstate'] = array();
115 }
117 $ldap->cd($this->dn);
118 $ldap->modify($attrs);
119 show_ldap_error($ldap->get_error());
120 }
121 }
122 }
123 }
125 /* Base select dialog */
126 $once = true;
127 foreach($_POST as $name => $value){
128 if(preg_match("/^chooseBase/",$name) && $once){
129 $once = false;
130 $this->dialog = new baseSelectDialog($this->config);
131 $this->dialog->setCurrentBase($this->base);
132 }
133 }
135 /* Dialog handling */
136 if(is_object($this->dialog)){
137 /* Must be called before save_object */
138 $this->dialog->save_object();
140 if($this->dialog->isClosed()){
141 $this->dialog = false;
142 }elseif($this->dialog->isSelected()){
143 $this->base = $this->dialog->isSelected();
144 $this->dialog= false;
145 }else{
146 return($this->dialog->execute());
147 }
148 }
150 /* Fill templating stuff */
151 $smarty= get_smarty();
152 $smarty->assign("bases", $this->config->idepartments);
154 /* Assign attributes */
155 foreach ($this->attributes as $attr){
156 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
157 $smarty->assign("$attr", $this->$attr);
158 }
159 $smarty->assign("baseACL", chkacl($this->acl, "base"));
161 $smarty->assign("staticAddress", "");
162 $smarty->assign("base_select", $this->base);
164 /* Assign status */
165 $query= "fping -q -r 1 -t 500 ".$this->cn;
166 exec ($query, $dummy, $retval);
168 /* Offline */
169 if ($retval == 0){
170 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
171 "instant_update" => _("Instant update"),
172 "update" => _("Scheduled update"),
173 "reinstall" => _("Reinstall"),
174 "rescan" => _("Rescan hardware"),
175 "memcheck" => _("Memory test"),
176 "localboot" => _("Force localboot"),
177 "sysinfo" => _("System analysis")));
178 } else {
179 $smarty->assign("actions", array("wake" => _("Wake up"),
180 "reinstall" => _("Reinstall"),
181 "update" => _("Scheduled update"),
182 "memcheck" => _("Memory test"),
183 "localboot" => _("Force localboot"),
184 "sysinfo" => _("System analysis")));
185 }
187 /* Show main page */
188 $this->netConfigDNS->cn= $this->cn;
189 $smarty->assign("netconfig", $this->netConfigDNS->execute());
191 if($this->netConfigDNS->dialog){
192 $this->dialog = TRUE;
193 return($this->netConfigDNS->execute());
194 }else{
195 $this->dialog = FALSE;
196 }
197 $smarty->assign("fai_activated",$this->fai_activated);
198 $smarty->assign("modes", $this->modes);
200 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
201 }
203 function remove_from_parent()
204 {
205 $this->netConfigDNS->acl = $this->acl;
206 $this->netConfigDNS->remove_from_parent();
207 $ldap= $this->config->get_ldap_link();
208 $ldap->rmdir($this->dn);
209 show_ldap_error($ldap->get_error(), _("Removing server failed"));
211 /* Delete references to object groups */
212 $ldap->cd ($this->config->current['BASE']);
213 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
214 while ($ldap->fetch()){
215 $og= new ogroup($this->config, $ldap->getDN());
216 unset($og->member[$this->dn]);
217 $og->save ();
218 }
219 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
220 }
223 /* Save data to object */
224 function save_object()
225 {
226 plugin::save_object();
227 $this->netConfigDNS->save_object();
228 /* Save base, since this is no LDAP attribute */
229 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
230 $this->base= $_POST['base'];
231 }
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 == "" && chkacl ($this->acl, "cn") == ""){
245 $message[]= _("The required field 'Server name' is not set.");
246 }
248 $ui= get_userinfo();
249 $acl= get_permissions ($this->dn, $ui->subtreeACL);
250 $acl= get_module_permission($acl, "servgeneric", $this->dn);
251 if (chkacl($acl, "create") != ""){
252 $message[]= _("You have no permissions to create a server on this 'Base'.");
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("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,ou=incoming,/",$attrs['dn']) && preg_match("/,ou=servers,ou=systems,/",$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 $this->netConfigDNS->acl = $this->acl;
279 plugin::save();
281 /* Remove all empty values */
282 if ($this->orig_dn == 'new'){
283 $attrs= array();
284 foreach ($this->attrs as $key => $val){
285 if (is_array($val) && count($val) == 0){
286 continue;
287 }
288 $attrs[$key]= $val;
289 }
290 $this->attrs= $attrs;
291 }
293 /* Write back to ldap */
294 $ldap= $this->config->get_ldap_link();
295 if ($this->orig_dn == 'new'){
296 $ldap->cd($this->config->current['BASE']);
297 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
298 $ldap->cd($this->dn);
299 $ldap->add($this->attrs);
300 $mode= "add";
301 } else {
303 /* cn is not case sensitive for ldap, but for php it is!! */
304 if($this->config->current['DNMODE'] == "cn"){
305 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
306 $this->recursive_move($this->orig_dn, $this->dn);
307 plugin::save();
308 }
309 }else{
310 if ($this->orig_dn != $this->dn){
311 $this->recursive_move($this->orig_dn, $this->dn);
312 plugin::save();
313 }
314 }
316 $ldap->cd($this->dn);
317 $this->cleanup();
318 $ldap->modify ($this->attrs);
320 $mode= "modify";
321 }
322 show_ldap_error($ldap->get_error(), _("Saving server failed"));
323 $this->netConfigDNS->cn = $this->cn;
324 $this->netConfigDNS->save($this->dn);
326 /* Optionally execute a command after we're done */
327 if(!$this->didAction){
328 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
329 }
330 }
332 }
334 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
335 ?>