30ee0acac002cc04f680634225435dbd86b402ee
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 /* Save dn for later references */
64 $this->orig_dn= $this->dn;
65 }
67 function execute()
68 {
69 /* Call parent execute */
70 plugin::execute();
72 /* Do we represent a valid server? */
73 if (!$this->is_account && $this->parent == NULL){
74 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
75 _("This 'dn' has no server features.")."</b>";
76 return($display);
77 }
79 /* Check for action */
80 if (isset($_POST['action'])){
81 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
82 if ($cmd == ""){
83 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
84 } else {
86 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
87 if ($retval != 0){
88 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
89 } else {
90 $this->didAction= TRUE;
92 /* Set FAIstate */
94 if($this->fai_activated && $this->dn != "new"){
95 $ldap = $this->config->get_ldap_link();
96 $ldap->cd($this->config->current['BASE']);
97 $ldap->cat($this->dn,array("objectClass"));
98 $res = $ldap->fetch();
100 $attrs = array();
101 $attrs['FAIstate'] = "";
102 if(isset($this->mapActions[$_POST['saction']])){
103 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
104 }
106 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
107 $attrs['objectClass'][] = $res['objectClass'][$i];
108 }
110 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
111 $attrs['objectClass'][] = "FAIobject";
112 }
114 if($attrs['FAIstate'] == ""){
115 #FIXME we should check if FAIobject is used anymore
116 $attrs['FAIstate'] = array();
117 }
119 $ldap->cd($this->dn);
120 $ldap->modify($attrs);
121 show_ldap_error($ldap->get_error());
122 }
123 }
124 }
125 }
127 /* Base select dialog */
128 $once = true;
129 foreach($_POST as $name => $value){
130 if(preg_match("/^chooseBase/",$name) && $once){
131 $once = false;
132 $this->dialog = new baseSelectDialog($this->config);
133 $this->dialog->setCurrentBase($this->base);
134 }
135 }
137 /* Dialog handling */
138 if(is_object($this->dialog)){
139 /* Must be called before save_object */
140 $this->dialog->save_object();
142 if($this->dialog->isClosed()){
143 $this->dialog = false;
144 }elseif($this->dialog->isSelected()){
145 $this->base = $this->dialog->isSelected();
146 $this->dialog= false;
147 }else{
148 return($this->dialog->execute());
149 }
150 }
152 /* Fill templating stuff */
153 $smarty= get_smarty();
154 $smarty->assign("bases", $this->config->idepartments);
156 /* Assign attributes */
157 foreach ($this->attributes as $attr){
158 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
159 $smarty->assign("$attr", $this->$attr);
160 }
161 $smarty->assign("baseACL", chkacl($this->acl, "base"));
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 $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->remove_from_parent();
199 $ldap= $this->config->get_ldap_link();
200 $ldap->rmdir($this->dn);
201 show_ldap_error($ldap->get_error(), _("Removing server failed"));
203 /* Delete references to object groups */
204 $ldap->cd ($this->config->current['BASE']);
205 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
206 while ($ldap->fetch()){
207 $og= new ogroup($this->config, $ldap->getDN());
208 unset($og->member[$this->dn]);
209 $og->save ();
210 }
211 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
212 }
215 /* Save data to object */
216 function save_object()
217 {
218 plugin::save_object();
219 $this->netConfigDNS->save_object();
220 /* Save base, since this is no LDAP attribute */
221 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
222 $this->base= $_POST['base'];
223 }
224 }
227 /* Check supplied data */
228 function check()
229 {
230 /* Call common method to give check the hook */
231 $message= plugin::check();
232 $message= array_merge($message, $this->netConfigDNS->check());
233 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
235 /* must: cn */
236 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
237 $message[]= _("The required field 'Server name' is not set.");
238 }
240 $ui= get_userinfo();
241 $acl= get_permissions ($this->dn, $ui->subtreeACL);
242 $acl= get_module_permission($acl, "servgeneric", $this->dn);
243 if (chkacl($acl, "create") != ""){
244 $message[]= _("You have no permissions to create a server on this 'Base'.");
245 }
247 if ($this->orig_dn != $this->dn){
248 $ldap= $this->config->get_ldap_link();
249 $ldap->cd ($this->base);
250 $ldap->search ("(cn=".$this->cn.")", array("cn"));
251 if ($ldap->count() != 0){
252 while ($attrs= $ldap->fetch()){
253 if ($attrs['dn'] != $this->orig_dn){
254 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
255 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
256 break;
257 }
258 }
259 }
260 }
261 }
263 return ($message);
264 }
267 /* Save to LDAP */
268 function save()
269 {
270 plugin::save();
272 /* Remove all empty values */
273 if ($this->orig_dn == 'new'){
274 $attrs= array();
275 foreach ($this->attrs as $key => $val){
276 if (is_array($val) && count($val) == 0){
277 continue;
278 }
279 $attrs[$key]= $val;
280 }
281 $this->attrs= $attrs;
282 }
284 /* Write back to ldap */
285 $ldap= $this->config->get_ldap_link();
286 if ($this->orig_dn == 'new'){
287 $ldap->cd($this->config->current['BASE']);
288 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
289 $ldap->cd($this->dn);
290 $ldap->add($this->attrs);
291 $mode= "add";
292 } else {
294 /* cn is not case sensitive for ldap, but for php it is!! */
295 if($this->config->current['DNMODE'] == "cn"){
296 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
297 $this->move($this->orig_dn, $this->dn);
298 plugin::save();
299 }
300 }else{
301 if ($this->orig_dn != $this->dn){
302 $this->move($this->orig_dn, $this->dn);
303 plugin::save();
304 }
305 }
307 $ldap->cd($this->dn);
308 $this->cleanup();
309 $ldap->modify ($this->attrs);
311 $mode= "modify";
312 }
313 show_ldap_error($ldap->get_error(), _("Saving server failed"));
314 $this->netConfigDNS->cn = $this->cn;
315 $this->netConfigDNS->save($this->dn);
317 /* Optionally execute a command after we're done */
318 if(!$this->didAction){
319 $this->handle_post_events($mode);
320 }
321 }
323 }
325 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
326 ?>