1 <?php
3 class termgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage terminal 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 $gotoMode= "disabled";
12 var $gotoTerminalPath= "";
13 var $gotoSwapServer= "";
14 var $gotoSyslogServer= "";
15 var $gotoNtpServer= "";
16 var $gotoSndModule= "";
17 var $gotoFloppyEnable= "";
18 var $gotoCdromEnable= "";
19 var $ghCpuType= "-";
20 var $ghMemSize= "-";
21 var $macAddress= "";
22 var $ipHostNumber= "";
23 var $ghUsbSupport= "-";
24 var $ghNetNic= array();
25 var $ghIdeDev= array();
26 var $ghScsiDev= array();
27 var $ghGfxAdapter= "-";
28 var $ghSoundAdapter= "-";
29 var $gotoLastUser= "-";
31 /* Needed values and lists */
32 var $base= "";
33 var $cn= "";
34 var $orig_dn= "";
36 /* Plugin side filled */
37 var $modes= array();
39 /* attribute list for save action */
40 var $ignore_account= TRUE;
41 var $attributes= array("gotoMode", "gotoTerminalPath", "macAddress",
42 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
43 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44 "ghCpuType", "ghMemSize", "ipHostNumber", "ghUsbSupport",
45 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
46 var $objectclasses= array("top", "gotoTerminal", "GOhard");
48 function termgeneric ($config, $dn= NULL)
49 {
50 plugin::plugin ($config, $dn);
52 /* Read arrays */
53 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
54 if (!isset($this->attrs[$val])){
55 continue;
56 }
57 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
58 array_push($this->$val, $this->attrs[$val][$i]);
59 }
60 }
62 $this->modes["disabled"]= _("disabled");
63 $this->modes["text"]= _("text");
64 $this->modes["graphic"]= _("graphic");
66 /* Set base */
67 if ($this->dn == "new"){
68 $ui= get_userinfo();
69 $this->base= dn2base($ui->dn);
70 } else {
71 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
72 }
74 $this->orig_dn= $this->dn;
75 }
77 function execute()
78 {
79 /* Do we need to flip is_account state? */
80 if (isset($_POST['modify_state'])){
81 $this->is_account= !$this->is_account;
82 }
84 if (isset($_POST['action'])){
85 switch($_POST['saction']){
86 case 'wake':
87 $cmd= $this->search($this->config->data['TABS'], "termgeneric", "WAKECMD");
88 if ($cmd == ""){
89 print_red(_("No WAKECMD definition found in your gosa.conf"));
90 } else {
91 exec ($cmd." ".$this->macAddress, $dummy, $retval);
92 if ($retval != 0){
93 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
94 }
95 }
96 break;
98 case 'reboot':
99 $cmd= $this->search($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
100 if ($cmd == ""){
101 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
102 } else {
103 exec ($cmd." ".$this->cn, $dummy, $retval);
104 if ($retval != 0){
105 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
106 }
107 }
108 break;
110 case 'halt':
111 $cmd= $this->search($this->config->data['TABS'], "termgeneric", "HALTCMD");
112 if ($cmd == ""){
113 print_red(_("No HALTCMD definition found in your gosa.conf"));
114 } else {
115 exec ($cmd." ".$this->cn, $dummy, $retval);
116 if ($retval != 0){
117 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
118 }
119 }
120 break;
121 }
122 }
124 /* Do we represent a valid terminal? */
125 if (!$this->is_account && $this->parent == NULL){
126 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
127 _("This 'dn' has no terminal features.")."</b>";
128 return($display);
129 }
131 /* Fill templating stuff */
132 $smarty= get_smarty();
133 $smarty->assign("cn", $this->cn);
135 $smarty->assign("bases", $this->config->idepartments);
137 /* Check if terminal is online */
138 $query= "fping -q -r 1 -t 500 ".$this->cn;
139 exec ($query, $dummy, $retval);
141 /* Offline */
142 if ($retval == 0){
143 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
144 } else {
145 $smarty->assign("actions", array("wake" => _("Wake up")));
146 }
147 /* Arrays */
148 $smarty->assign("modes", $this->modes);
149 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
150 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
151 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
153 /* Variables */
154 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
155 "gotoSyslogServer", "gotoNtpServer") as $val){
157 $smarty->assign($val."_select", $this->$val);
158 $smarty->assign($val."ACL", chkacl($this->acl, $val));
159 }
160 $smarty->assign("ipHostNumber", $this->ipHostNumber);
161 $smarty->assign("macAddress", $this->macAddress);
163 /* Show main page */
164 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
165 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
166 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
167 }
169 function remove_from_parent()
170 {
171 $ldap= $this->config->get_ldap_link();
172 $ldap->rmdir($this->dn);
173 show_ldap_error($ldap->get_error());
175 /* Optionally execute a command after we're done */
176 $this->handle_post_events("remove");
178 /* Delete references to object groups */
179 $ldap->cd ($this->config->current['BASE']);
180 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
181 while ($ldap->fetch()){
182 $og= new ogroup($this->config, $ldap->getDN());
183 unset($og->member[$this->dn]);
184 $og->save ();
185 }
187 }
190 /* Save data to object */
191 function save_object()
192 {
193 plugin::save_object();
195 /* Save base, since this is no LDAP attribute */
196 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
197 $this->base= $_POST['base'];
198 }
200 /* Save terminal path to parent since it is used by termstartup, too */
201 $this->parent->by_object['termstartup']->gotoTerminalPath=
202 $this->gotoTerminalPath;
203 }
206 /* Check supplied data */
207 function check()
208 {
209 $message= array();
211 /* Permissions for that base? */
212 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
214 $ui= get_userinfo();
215 $acl= get_permissions ($this->dn, $ui->subtreeACL);
216 $acl= get_module_permission($acl, "group", $this->dn);
217 if (chkacl($acl, "create") != ""){
218 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
219 }
221 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
222 $message[]= _("The required field 'Terminal name' is not set.");
223 }
225 if ($this->orig_dn == 'new'){
226 $ldap= $this->config->get_ldap_link();
227 $ldap->cd ($this->base);
228 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
229 if ($ldap->count() != 0){
230 while ($attrs= $ldap->fetch()){
231 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
232 continue;
233 } else {
234 if ($attrs['dn'] != $this->orig_dn){
235 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
236 break;
237 }
238 }
239 }
240 }
241 }
243 return ($message);
244 }
247 /* Save to LDAP */
248 function save()
249 {
250 plugin::save();
252 /* Strip out 'default' values */
253 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
255 if ($this->attrs[$val] == "default"){
256 $this->attrs[$val]= array();
257 }
258 }
260 /* Add missing arrays */
261 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
262 if (isset ($this->$val) && count ($this->$val) != 0){
263 $this->attrs["$val"]= $this->$val;
264 }
265 }
267 /* Remove all empty values */
268 if ($this->orig_dn == 'new'){
269 $attrs= array();
270 foreach ($this->attrs as $key => $val){
271 if (is_array($val) && count($val) == 0){
272 continue;
273 }
274 $attrs[$key]= $val;
275 }
276 $this->attrs= $attrs;
277 }
279 /* Write back to ldap */
280 $ldap= $this->config->get_ldap_link();
281 if ($this->orig_dn == 'new'){
282 $ldap->cd($this->config->current['BASE']);
283 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
284 $ldap->cd($this->dn);
285 $ldap->add($this->attrs);
286 $this->handle_post_events("add");
287 } else {
288 if ($this->orig_dn != $this->dn){
289 $this->move($this->orig_dn, $this->dn);
290 }
291 $ldap->cd($this->dn);
292 $ldap->modify($this->attrs);
293 $this->handle_post_events("modify");
294 }
295 show_ldap_error($ldap->get_error());
297 /* Optionally execute a command after we're done */
298 $this->postcreate();
299 }
301 }
303 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
304 ?>