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