c081b412ae90c7cb550eb7adf27f6355312cb04d
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 /* Do we represent a valid server? */
47 if (!$this->is_account && $this->parent == NULL){
48 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
49 _("This 'dn' has no server features.")."</b>";
50 return($display);
51 }
53 /* Check for action */
54 if (isset($_POST['action'])){
55 switch($_POST['action']){
56 case 'wake':
57 $cmd= $this->search($this->config->data['TABS'], "servgeneric", "WAKECMD");
58 if ($cmd == ""){
59 print_red(_("No WAKECMD definition found in your gosa.conf"));
60 } else {
61 exec ($cmd." ".$this->macAddress, $dummy, $retval);
62 if ($retval != 0){
63 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
64 }
65 }
66 break;
68 case 'reboot':
69 $cmd= $this->search($this->config->data['TABS'], "servgeneric", "REBOOTCMD");
70 if ($cmd == ""){
71 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
72 } else {
73 exec ($cmd." ".$this->cn, $dummy, $retval);
74 if ($retval != 0){
75 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
76 }
77 }
78 break;
80 case 'halt':
81 $cmd= $this->search($this->config->data['TABS'], "servgeneric", "HALTCMD");
82 if ($cmd == ""){
83 print_red(_("No HALTCMD definition found in your gosa.conf"));
84 } else {
85 exec ($cmd." ".$this->cn, $dummy, $retval);
86 if ($retval != 0){
87 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
88 }
89 }
90 break;
91 }
92 }
94 /* Fill templating stuff */
95 $smarty= get_smarty();
96 $smarty->assign("bases", $this->config->idepartments);
98 /* Assign attributes */
99 foreach ($this->attributes as $attr){
100 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
101 $smarty->assign("$attr", $this->$attr);
102 }
103 $smarty->assign("base_select", $this->base);
105 /* Assign status */
106 $query= "fping -q -r 1 -t 500 ".$this->cn;
107 exec ($query, $dummy, $retval);
109 /* Offline */
110 if ($retval == 0){
111 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
112 } else {
113 $smarty->assign("actions", array("wake" => _("Wake up")));
114 }
117 /* Show main page */
118 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
120 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
121 }
123 function remove_from_parent()
124 {
125 $ldap= $this->config->get_ldap_link();
126 $ldap->rmdir($this->dn);
127 show_ldap_error($ldap->get_error());
129 /* Delete references to object groups */
130 $ldap->cd ($this->config->current['BASE']);
131 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
132 while ($ldap->fetch()){
133 $og= new ogroup($this->config, $ldap->getDN());
134 unset($og->member[$this->dn]);
135 $og->save ();
136 }
138 $this->handle_post_events("remove");
139 }
142 /* Save data to object */
143 function save_object()
144 {
145 plugin::save_object();
147 /* Save base, since this is no LDAP attribute */
148 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
149 $this->base= $_POST['base'];
150 }
151 }
154 /* Check supplied data */
155 function check()
156 {
157 $message= array();
158 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
160 /* must: cn */
161 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
162 $message[]= _("The required field 'Server name' is not set.");
163 }
165 $ui= get_userinfo();
166 $acl= get_permissions ($this->dn, $ui->subtreeACL);
167 $acl= get_module_permission($acl, "server", $this->dn);
168 if (chkacl($acl, "create") != ""){
169 $message[]= _("You have no permissions to create a server on this 'Base'.");
170 }
172 if ($this->orig_dn != $this->dn){
173 $ldap= $this->config->get_ldap_link();
174 $ldap->cd ($this->base);
175 $ldap->search ("(cn=".$this->cn.")", array("cn"));
176 if ($ldap->count() != 0){
177 while ($attrs= $ldap->fetch()){
178 if ($attrs['dn'] != $this->orig_dn){
179 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
180 break;
181 }
182 }
183 }
184 }
186 return ($message);
187 }
190 /* Save to LDAP */
191 function save()
192 {
193 plugin::save();
195 /* Remove all empty values */
196 if ($this->orig_dn == 'new'){
197 $attrs= array();
198 foreach ($this->attrs as $key => $val){
199 if (is_array($val) && count($val) == 0){
200 continue;
201 }
202 $attrs[$key]= $val;
203 }
204 $this->attrs= $attrs;
205 }
207 /* Write back to ldap */
208 $ldap= $this->config->get_ldap_link();
209 if ($this->orig_dn == 'new'){
210 $ldap->cd($this->config->current['BASE']);
211 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
212 $ldap->cd($this->dn);
213 $ldap->add($this->attrs);
214 $mode= "add";
215 } else {
216 if ($this->orig_dn != $this->dn){
217 $this->move($this->orig_dn, $this->dn);
218 }
220 $ldap->cd($this->dn);
221 $ldap->modify($this->attrs);
222 $mode= "modify";
223 }
224 show_ldap_error($ldap->get_error());
226 /* Optionally execute a command after we're done */
227 $this->handle_post_events($mode);
228 }
230 }
232 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
233 ?>