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 $macAddress= "";
19 var $ipHostNumber= "";
20 var $description= "";
21 var $orig_dn= "";
23 /* attribute list for save action */
24 var $attributes= array("cn", "description", "macAddress", "ipHostNumber");
25 var $objectclasses= array("top", "goServer");
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 }
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->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 /* Fill templating stuff */
98 $smarty= get_smarty();
99 $smarty->assign("bases", $this->config->idepartments);
101 /* Assign attributes */
102 foreach ($this->attributes as $attr){
103 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
104 $smarty->assign("$attr", $this->$attr);
105 }
106 $smarty->assign("staticAddress", "");
107 $smarty->assign("base_select", $this->base);
109 /* Assign status */
110 $query= "fping -q -r 1 -t 500 ".$this->cn;
111 exec ($query, $dummy, $retval);
113 /* Offline */
114 if ($retval == 0){
115 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
116 } else {
117 $smarty->assign("actions", array("wake" => _("Wake up")));
118 }
121 /* Show main page */
122 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
124 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
125 }
127 function remove_from_parent()
128 {
129 $ldap= $this->config->get_ldap_link();
130 $ldap->rmdir($this->dn);
131 show_ldap_error($ldap->get_error());
133 /* Delete references to object groups */
134 $ldap->cd ($this->config->current['BASE']);
135 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
136 while ($ldap->fetch()){
137 $og= new ogroup($this->config, $ldap->getDN());
138 unset($og->member[$this->dn]);
139 $og->save ();
140 }
142 $this->handle_post_events("remove");
143 }
146 /* Save data to object */
147 function save_object()
148 {
149 plugin::save_object();
151 /* Save base, since this is no LDAP attribute */
152 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
153 $this->base= $_POST['base'];
154 }
155 }
158 /* Check supplied data */
159 function check()
160 {
161 $message= array();
162 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
164 /* must: cn */
165 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
166 $message[]= _("The required field 'Server name' is not set.");
167 }
169 $ui= get_userinfo();
170 $acl= get_permissions ($this->dn, $ui->subtreeACL);
171 $acl= get_module_permission($acl, "server", $this->dn);
172 if (chkacl($acl, "create") != ""){
173 $message[]= _("You have no permissions to create a server on this 'Base'.");
174 }
176 if ($this->orig_dn != $this->dn){
177 $ldap= $this->config->get_ldap_link();
178 $ldap->cd ($this->base);
179 $ldap->search ("(cn=".$this->cn.")", array("cn"));
180 if ($ldap->count() != 0){
181 while ($attrs= $ldap->fetch()){
182 if ($attrs['dn'] != $this->orig_dn){
183 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
184 break;
185 }
186 }
187 }
188 }
190 return ($message);
191 }
194 /* Save to LDAP */
195 function save()
196 {
197 plugin::save();
199 /* Remove all empty values */
200 if ($this->orig_dn == 'new'){
201 $attrs= array();
202 foreach ($this->attrs as $key => $val){
203 if (is_array($val) && count($val) == 0){
204 continue;
205 }
206 $attrs[$key]= $val;
207 }
208 $this->attrs= $attrs;
209 }
211 /* Write back to ldap */
212 $ldap= $this->config->get_ldap_link();
213 if ($this->orig_dn == 'new'){
214 $ldap->cd($this->config->current['BASE']);
215 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
216 $ldap->cd($this->dn);
217 $ldap->add($this->attrs);
218 $mode= "add";
219 } else {
221 /* cn is not case sensitive for ldap, but for php it is!! */
222 if($this->config->current['DNMODE'] == "cn"){
223 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
224 $this->move($this->orig_dn, $this->dn);
225 plugin::save();
226 }
227 }else{
228 if ($this->orig_dn != $this->dn){
229 $this->move($this->orig_dn, $this->dn);
230 plugin::save();
231 }
232 }
234 $ldap->cd($this->dn);
235 $ldap->modify($this->attrs);
236 $mode= "modify";
237 }
238 show_ldap_error($ldap->get_error());
240 /* Optionally execute a command after we're done */
241 $this->handle_post_events($mode);
242 }
244 }
246 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
247 ?>