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