7679c72e2d9683167f0bae3117d0ae0006d55a45
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= "";
21 /* attribute list for save action */
22 var $attributes= array("cn", "description","gotoMode");
23 var $objectclasses= array("top", "GOhard", "goServer");
25 var $gotoMode= "locked";
27 var $netConfigDNS;
28 var $modes = array();
30 function servgeneric ($config, $dn= NULL)
31 {
32 plugin::plugin ($config, $dn);
34 $this->modes["active"]= _("Activated");
35 $this->modes["locked"]= _("Locked");
36 $this->modes["memcheck"]= _("Memory test");
37 $this->modes["sysinfo"]= _("System analysis");
39 /* Set base */
40 if ($this->dn == "new"){
41 $ui= get_userinfo();
42 $this->base= dn2base($ui->dn);
43 $this->cn= "";
44 } else {
45 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
46 }
47 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
48 /* Save dn for later references */
49 $this->orig_dn= $this->dn;
50 }
52 function execute()
53 {
54 /* Call parent execute */
55 plugin::execute();
57 /* Do we represent a valid server? */
58 if (!$this->is_account && $this->parent == NULL){
59 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
60 _("This 'dn' has no server features.")."</b>";
61 return($display);
62 }
64 /* Check for action */
65 if (isset($_POST['action'])){
66 switch($_POST['action']){
67 case 'wake':
68 $cmd= search_config($this->config->data['TABS'], "servgeneric", "WAKECMD");
69 if ($cmd == ""){
70 print_red(_("No WAKECMD definition found in your gosa.conf"));
71 } else {
72 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
73 if ($retval != 0){
74 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
75 }
76 }
77 break;
79 case 'reboot':
80 $cmd= search_config($this->config->data['TABS'], "servgeneric", "REBOOTCMD");
81 if ($cmd == ""){
82 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
83 } else {
84 exec ($cmd." ".$this->cn, $dummy, $retval);
85 if ($retval != 0){
86 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
87 }
88 }
89 break;
91 case 'halt':
92 $cmd= search_config($this->config->data['TABS'], "servgeneric", "HALTCMD");
93 if ($cmd == ""){
94 print_red(_("No HALTCMD definition found in your gosa.conf"));
95 } else {
96 exec ($cmd." ".$this->cn, $dummy, $retval);
97 if ($retval != 0){
98 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
99 }
100 }
101 break;
102 }
103 }
105 /* Base select dialog */
106 $once = true;
107 foreach($_POST as $name => $value){
108 if(preg_match("/^chooseBase/",$name) && $once){
109 $once = false;
110 $this->dialog = new baseSelectDialog($this->config);
111 $this->dialog->setCurrentBase($this->base);
112 }
113 }
115 /* Dialog handling */
116 if(is_object($this->dialog)){
117 /* Must be called before save_object */
118 $this->dialog->save_object();
120 if($this->dialog->isClosed()){
121 $this->dialog = false;
122 }elseif($this->dialog->isSelected()){
123 $this->base = $this->dialog->isSelected();
124 $this->dialog= false;
125 }else{
126 return($this->dialog->execute());
127 }
128 }
130 /* Fill templating stuff */
131 $smarty= get_smarty();
132 $smarty->assign("bases", $this->config->idepartments);
134 /* Assign attributes */
135 foreach ($this->attributes as $attr){
136 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
137 $smarty->assign("$attr", $this->$attr);
138 }
139 $smarty->assign("staticAddress", "");
140 $smarty->assign("base_select", $this->base);
142 /* Assign status */
143 $query= "fping -q -r 1 -t 500 ".$this->cn;
144 exec ($query, $dummy, $retval);
146 /* Offline */
147 if ($retval == 0){
148 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
149 "instant_update" => _("Instant update"),
150 "update" => _("Scheduled update"),
151 "reinstall" => _("Reinstall"),
152 "rescan" => _("Rescan hardware")));
153 } else {
154 $smarty->assign("actions", array("wake" => _("Wake up"),
155 "reinstall" => _("Reinstall"),
156 "update" => _("Scheduled update")));
157 }
159 /* Show main page */
160 $smarty->assign("netconfig", $this->netConfigDNS->execute());
161 $smarty->assign("modes", $this->modes);
163 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
164 }
166 function remove_from_parent()
167 {
168 $this->netConfigDNS->remove_from_parent();
169 $ldap= $this->config->get_ldap_link();
170 $ldap->rmdir($this->dn);
171 show_ldap_error($ldap->get_error(), _("Removing server failed"));
173 /* Delete references to object groups */
174 $ldap->cd ($this->config->current['BASE']);
175 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
176 while ($ldap->fetch()){
177 $og= new ogroup($this->config, $ldap->getDN());
178 unset($og->member[$this->dn]);
179 $og->save ();
180 }
181 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
182 }
185 /* Save data to object */
186 function save_object()
187 {
188 plugin::save_object();
189 $this->netConfigDNS->save_object();
190 /* Save base, since this is no LDAP attribute */
191 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
192 $this->base= $_POST['base'];
193 }
194 }
197 /* Check supplied data */
198 function check()
199 {
200 /* Call common method to give check the hook */
201 $message= plugin::check();
202 $message= array_merge($message, $this->netConfigDNS->check());
203 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
205 /* must: cn */
206 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
207 $message[]= _("The required field 'Server name' is not set.");
208 }
210 $ui= get_userinfo();
211 $acl= get_permissions ($this->dn, $ui->subtreeACL);
212 $acl= get_module_permission($acl, "server", $this->dn);
213 if (chkacl($acl, "create") != ""){
214 $message[]= _("You have no permissions to create a server on this 'Base'.");
215 }
217 if ($this->orig_dn != $this->dn){
218 $ldap= $this->config->get_ldap_link();
219 $ldap->cd ($this->base);
220 $ldap->search ("(cn=".$this->cn.")", array("cn"));
221 if ($ldap->count() != 0){
222 while ($attrs= $ldap->fetch()){
223 if ($attrs['dn'] != $this->orig_dn){
224 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
225 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
226 break;
227 }
228 }
229 }
230 }
231 }
233 return ($message);
234 }
237 /* Save to LDAP */
238 function save()
239 {
240 plugin::save();
242 /* Remove all empty values */
243 if ($this->orig_dn == 'new'){
244 $attrs= array();
245 foreach ($this->attrs as $key => $val){
246 if (is_array($val) && count($val) == 0){
247 continue;
248 }
249 $attrs[$key]= $val;
250 }
251 $this->attrs= $attrs;
252 }
254 /* Write back to ldap */
255 $ldap= $this->config->get_ldap_link();
256 if ($this->orig_dn == 'new'){
257 $ldap->cd($this->config->current['BASE']);
258 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
259 $ldap->cd($this->dn);
260 $ldap->add($this->attrs);
261 $mode= "add";
262 } else {
264 /* cn is not case sensitive for ldap, but for php it is!! */
265 if($this->config->current['DNMODE'] == "cn"){
266 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
267 $this->move($this->orig_dn, $this->dn);
268 plugin::save();
269 }
270 }else{
271 if ($this->orig_dn != $this->dn){
272 $this->move($this->orig_dn, $this->dn);
273 plugin::save();
274 }
275 }
277 $ldap->cd($this->dn);
278 $this->cleanup();
279 $ldap->modify ($this->attrs);
281 $mode= "modify";
282 }
283 $this->netConfigDNS->cn = $this->cn;
284 $this->netConfigDNS->save($this->dn);
285 show_ldap_error($ldap->get_error(), _("Saving server failed"));
287 /* Optionally execute a command after we're done */
288 $this->handle_post_events($mode);
289 }
291 }
293 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
294 ?>