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());
190 $smarty->assign("fai_activated",$this->fai_activated);
191 $smarty->assign("modes", $this->modes);
193 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
194 }
196 function remove_from_parent()
197 {
198 $this->netConfigDNS->acl = $this->acl;
199 $this->netConfigDNS->remove_from_parent();
200 $ldap= $this->config->get_ldap_link();
201 $ldap->rmdir($this->dn);
202 show_ldap_error($ldap->get_error(), _("Removing server failed"));
204 /* Delete references to object groups */
205 $ldap->cd ($this->config->current['BASE']);
206 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
207 while ($ldap->fetch()){
208 $og= new ogroup($this->config, $ldap->getDN());
209 unset($og->member[$this->dn]);
210 $og->save ();
211 }
212 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
213 }
216 /* Save data to object */
217 function save_object()
218 {
219 plugin::save_object();
220 $this->netConfigDNS->save_object();
221 /* Save base, since this is no LDAP attribute */
222 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
223 $this->base= $_POST['base'];
224 }
225 }
228 /* Check supplied data */
229 function check()
230 {
231 /* Call common method to give check the hook */
232 $message= plugin::check();
233 $message= array_merge($message, $this->netConfigDNS->check());
234 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
236 /* must: cn */
237 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
238 $message[]= _("The required field 'Server name' is not set.");
239 }
241 $ui= get_userinfo();
242 $acl= get_permissions ($this->dn, $ui->subtreeACL);
243 $acl= get_module_permission($acl, "servgeneric", $this->dn);
244 if (chkacl($acl, "create") != ""){
245 $message[]= _("You have no permissions to create a server on this 'Base'.");
246 }
248 if ($this->orig_dn != $this->dn){
249 $ldap= $this->config->get_ldap_link();
250 $ldap->cd ($this->base);
251 $ldap->search ("(cn=".$this->cn.")", array("cn"));
252 if ($ldap->count() != 0){
253 while ($attrs= $ldap->fetch()){
254 if ($attrs['dn'] != $this->orig_dn){
255 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
256 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
257 break;
258 }
259 }
260 }
261 }
262 }
264 return ($message);
265 }
268 /* Save to LDAP */
269 function save()
270 {
271 $this->netConfigDNS->acl = $this->acl;
272 plugin::save();
274 /* Remove all empty values */
275 if ($this->orig_dn == 'new'){
276 $attrs= array();
277 foreach ($this->attrs as $key => $val){
278 if (is_array($val) && count($val) == 0){
279 continue;
280 }
281 $attrs[$key]= $val;
282 }
283 $this->attrs= $attrs;
284 }
286 /* Write back to ldap */
287 $ldap= $this->config->get_ldap_link();
288 if ($this->orig_dn == 'new'){
289 $ldap->cd($this->config->current['BASE']);
290 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
291 $ldap->cd($this->dn);
292 $ldap->add($this->attrs);
293 $mode= "add";
294 } else {
296 /* cn is not case sensitive for ldap, but for php it is!! */
297 if($this->config->current['DNMODE'] == "cn"){
298 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
299 $this->move($this->orig_dn, $this->dn);
300 plugin::save();
301 }
302 }else{
303 if ($this->orig_dn != $this->dn){
304 $this->move($this->orig_dn, $this->dn);
305 plugin::save();
306 }
307 }
309 $ldap->cd($this->dn);
310 $this->cleanup();
311 $ldap->modify ($this->attrs);
313 $mode= "modify";
314 }
315 show_ldap_error($ldap->get_error(), _("Saving server failed"));
316 $this->netConfigDNS->cn = $this->cn;
317 $this->netConfigDNS->save($this->dn);
319 /* Optionally execute a command after we're done */
320 if(!$this->didAction){
321 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
322 }
323 }
325 }
327 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
328 ?>