e5ce8f8c40e8f75a5c389322d87282a204977579
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 $interfaces= array();
13 /* Needed values and lists */
14 var $base= "";
15 var $cn= "";
16 var $l= "";
17 var $macAddress= "";
18 var $ipHostNumber= "";
19 var $description= "";
20 var $orig_dn= "";
22 /* attribute list for save action */
23 var $attributes= array("cn", "description", "macAddress", "ipHostNumber");
24 var $objectclasses= array("top", "goServer");
26 function servgeneric ($config, $dn= NULL)
27 {
28 plugin::plugin ($config, $dn);
30 /* Set base */
31 if ($this->dn == "new"){
32 $ui= get_userinfo();
33 $this->base= dn2base($ui->dn);
34 $this->cn= "";
35 } else {
36 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
37 }
39 /* Save dn for later references */
40 $this->orig_dn= $this->dn;
41 }
43 function execute()
44 {
45 /* Do we represent a valid server? */
46 if (!$this->is_account && $this->parent == NULL){
47 $display= "<img src=\"images/stop.png\" align=center> <b>".
48 _("This 'dn' has no server features.")."</b>";
49 return($display);
50 }
52 /* Check for action */
53 if (isset($_POST['action'])){
54 switch($_POST['action']){
55 case 'wake':
56 $cmd= $this->search($this->config->data['TABS'], "servgeneric", "WAKECMD");
57 if ($cmd == ""){
58 print_red(_("No WAKECMD definition found in your gosa.conf"));
59 } else {
60 exec ($cmd." ".$this->macAddress, $dummy, $retval);
61 if ($retval != 0){
62 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
63 }
64 }
65 break;
67 case 'reboot':
68 $cmd= $this->search($this->config->data['TABS'], "servgeneric", "REBOOTCMD");
69 if ($cmd == ""){
70 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
71 } else {
72 exec ($cmd." ".$this->cn, $dummy, $retval);
73 if ($retval != 0){
74 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
75 }
76 }
77 break;
79 case 'halt':
80 $cmd= $this->search($this->config->data['TABS'], "servgeneric", "HALTCMD");
81 if ($cmd == ""){
82 print_red(_("No HALTCMD 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;
90 }
91 }
93 /* Fill templating stuff */
94 $smarty= get_smarty();
95 $smarty->assign("bases", $this->config->idepartments);
97 /* Assign attributes */
98 foreach ($this->attributes as $attr){
99 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
100 $smarty->assign("$attr", $this->$attr);
101 }
102 $smarty->assign("base_select", $this->base);
104 /* Assign status */
105 $query= "fping -q -r 1 -t 500 ".$this->cn;
106 exec ($query, $dummy, $retval);
108 /* Offline */
109 if ($retval == 0){
110 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
111 } else {
112 $smarty->assign("actions", array("wake" => _("Wake up")));
113 }
116 /* Show main page */
117 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
119 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
120 }
122 function remove_from_parent()
123 {
124 $ldap= $this->config->get_ldap_link();
125 $ldap->rmdir($this->dn);
126 show_ldap_error($ldap->get_error());
128 /* Delete references to object groups */
129 $ldap->cd ($this->config->current['BASE']);
130 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
131 while ($ldap->fetch()){
132 $og= new ogroup($this->config, $ldap->getDN());
133 unset($og->member[$this->dn]);
134 $og->save ();
135 }
137 $this->handle_post_events("remove");
138 }
141 /* Save data to object */
142 function save_object()
143 {
144 plugin::save_object();
146 /* Save base, since this is no LDAP attribute */
147 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
148 $this->base= $_POST['base'];
149 }
150 }
153 /* Check supplied data */
154 function check()
155 {
156 $message= array();
157 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
159 /* must: cn */
160 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
161 $message[]= "The required field 'Server name' is not set.";
162 }
164 $ui= get_userinfo();
165 $acl= get_permissions ($this->dn, $ui->subtreeACL);
166 $acl= get_module_permission($acl, "server", $this->dn);
167 if (chkacl($acl, "create") != ""){
168 $message[]= _("You have no permissions to create a server on this 'Base'.");
169 }
171 if ($this->orig_dn != $this->dn){
172 $ldap= $this->config->get_ldap_link();
173 $ldap->cd ($this->base);
174 $ldap->search ("(cn=".$this->cn.")", array("cn"));
175 if ($ldap->count() != 0){
176 while ($attrs= $ldap->fetch()){
177 if ($attrs['dn'] != $this->orig_dn){
178 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
179 break;
180 }
181 }
182 }
183 }
185 return ($message);
186 }
189 /* Save to LDAP */
190 function save()
191 {
192 plugin::save();
194 /* Remove all empty values */
195 if ($this->orig_dn == 'new'){
196 $attrs= array();
197 foreach ($this->attrs as $key => $val){
198 if (is_array($val) && count($val) == 0){
199 continue;
200 }
201 $attrs[$key]= $val;
202 }
203 $this->attrs= $attrs;
204 }
206 /* Write back to ldap */
207 $ldap= $this->config->get_ldap_link();
208 if ($this->orig_dn == 'new'){
209 $ldap->cd($this->config->current['BASE']);
210 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
211 $ldap->cd($this->dn);
212 $ldap->add($this->attrs);
213 $mode= "add";
214 } else {
215 if ($this->orig_dn != $this->dn){
216 $this->move($this->orig_dn, $this->dn);
217 }
219 $ldap->cd($this->dn);
220 $ldap->modify($this->attrs);
221 $mode= "modify";
222 }
223 show_ldap_error($ldap->get_error());
225 /* Optionally execute a command after we're done */
226 $this->handle_post_events($mode);
227 }
229 }
231 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
232 ?>