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)
40 {
41 plugin::plugin ($config, $dn);
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'])){
73 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
74 $cmd= "/bin/echo";
75 if ($cmd == ""){
76 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
77 } else {
79 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
80 if ($retval != 0){
81 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
82 } else {
83 $this->didAction= TRUE;
85 /* Set FAIstate */
86 $ldap = $this->config->get_ldap_link();
87 $ldap->cd($this->config->current['BASE']);
88 $ldap->cat($this->dn,array("objectClass"));
89 $res = $ldap->fetch();
91 $attrs = array();
92 $attrs['FAIstate'] = "";
93 if(isset($this->mapActions[$_POST['saction']])){
94 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
95 }
97 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
98 if(($attrs['FAIstate'] == "") && ($res['objectClass'][$i] == "FAIobject")) continue;
99 $attrs['objectClass'][] = $res['objectClass'][$i];
100 }
102 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
103 $attrs['objectClass'][] = "FAIobject";
104 }
106 if($attrs['FAIstate'] == ""){
107 $attrs['FAIstate'] = array();
108 }
110 $ldap->cd($this->dn);
111 $ldap->modify($attrs);
112 show_ldap_error($ldap->get_error());
113 }
114 }
115 }
117 /* Base select dialog */
118 $once = true;
119 foreach($_POST as $name => $value){
120 if(preg_match("/^chooseBase/",$name) && $once){
121 $once = false;
122 $this->dialog = new baseSelectDialog($this->config);
123 $this->dialog->setCurrentBase($this->base);
124 }
125 }
127 /* Dialog handling */
128 if(is_object($this->dialog)){
129 /* Must be called before save_object */
130 $this->dialog->save_object();
132 if($this->dialog->isClosed()){
133 $this->dialog = false;
134 }elseif($this->dialog->isSelected()){
135 $this->base = $this->dialog->isSelected();
136 $this->dialog= false;
137 }else{
138 return($this->dialog->execute());
139 }
140 }
142 /* Fill templating stuff */
143 $smarty= get_smarty();
144 $smarty->assign("bases", $this->config->idepartments);
146 /* Assign attributes */
147 foreach ($this->attributes as $attr){
148 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
149 $smarty->assign("$attr", $this->$attr);
150 }
151 $smarty->assign("baseACL", chkacl($this->acl, "base"));
153 $smarty->assign("staticAddress", "");
154 $smarty->assign("base_select", $this->base);
156 /* Assign status */
157 $query= "fping -q -r 1 -t 500 ".$this->cn;
158 exec ($query, $dummy, $retval);
160 /* Offline */
161 if ($retval == 0){
162 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
163 "instant_update" => _("Instant update"),
164 "update" => _("Scheduled update"),
165 "reinstall" => _("Reinstall"),
166 "rescan" => _("Rescan hardware"),
167 "memcheck" => _("Memory test"),
168 "sysinfo" => _("System analysis")));
169 } else {
170 $smarty->assign("actions", array("wake" => _("Wake up"),
171 "reinstall" => _("Reinstall"),
172 "update" => _("Scheduled update"),
173 "memcheck" => _("Memory test"),
174 "sysinfo" => _("System analysis")));
175 }
177 /* Show main page */
178 $smarty->assign("netconfig", $this->netConfigDNS->execute());
179 $smarty->assign("modes", $this->modes);
181 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
182 }
184 function remove_from_parent()
185 {
186 $this->netConfigDNS->remove_from_parent();
187 $ldap= $this->config->get_ldap_link();
188 $ldap->rmdir($this->dn);
189 show_ldap_error($ldap->get_error(), _("Removing server failed"));
191 /* Delete references to object groups */
192 $ldap->cd ($this->config->current['BASE']);
193 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
194 while ($ldap->fetch()){
195 $og= new ogroup($this->config, $ldap->getDN());
196 unset($og->member[$this->dn]);
197 $og->save ();
198 }
199 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
200 }
203 /* Save data to object */
204 function save_object()
205 {
206 plugin::save_object();
207 $this->netConfigDNS->save_object();
208 /* Save base, since this is no LDAP attribute */
209 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
210 $this->base= $_POST['base'];
211 }
212 }
215 /* Check supplied data */
216 function check()
217 {
218 /* Call common method to give check the hook */
219 $message= plugin::check();
220 $message= array_merge($message, $this->netConfigDNS->check());
221 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
223 /* must: cn */
224 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
225 $message[]= _("The required field 'Server name' is not set.");
226 }
228 $ui= get_userinfo();
229 $acl= get_permissions ($this->dn, $ui->subtreeACL);
230 $acl= get_module_permission($acl, "server", $this->dn);
231 if (chkacl($acl, "create") != ""){
232 $message[]= _("You have no permissions to create a server on this 'Base'.");
233 }
235 if ($this->orig_dn != $this->dn){
236 $ldap= $this->config->get_ldap_link();
237 $ldap->cd ($this->base);
238 $ldap->search ("(cn=".$this->cn.")", array("cn"));
239 if ($ldap->count() != 0){
240 while ($attrs= $ldap->fetch()){
241 if ($attrs['dn'] != $this->orig_dn){
242 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
243 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
244 break;
245 }
246 }
247 }
248 }
249 }
251 return ($message);
252 }
255 /* Save to LDAP */
256 function save()
257 {
258 plugin::save();
260 /* Remove all empty values */
261 if ($this->orig_dn == 'new'){
262 $attrs= array();
263 foreach ($this->attrs as $key => $val){
264 if (is_array($val) && count($val) == 0){
265 continue;
266 }
267 $attrs[$key]= $val;
268 }
269 $this->attrs= $attrs;
270 }
272 /* Write back to ldap */
273 $ldap= $this->config->get_ldap_link();
274 if ($this->orig_dn == 'new'){
275 $ldap->cd($this->config->current['BASE']);
276 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
277 $ldap->cd($this->dn);
278 $ldap->add($this->attrs);
279 $mode= "add";
280 } else {
282 /* cn is not case sensitive for ldap, but for php it is!! */
283 if($this->config->current['DNMODE'] == "cn"){
284 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
285 $this->move($this->orig_dn, $this->dn);
286 plugin::save();
287 }
288 }else{
289 if ($this->orig_dn != $this->dn){
290 $this->move($this->orig_dn, $this->dn);
291 plugin::save();
292 }
293 }
295 $ldap->cd($this->dn);
296 $this->cleanup();
297 $ldap->modify ($this->attrs);
299 $mode= "modify";
300 }
301 show_ldap_error($ldap->get_error(), _("Saving server failed"));
302 $this->netConfigDNS->cn = $this->cn;
303 $this->netConfigDNS->save($this->dn);
305 /* Optionally execute a command after we're done */
306 if(!$this->didAction){
307 $this->handle_post_events($mode);
308 }
309 }
311 }
313 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
314 ?>