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 $attributes= array("gotoMode", "gotoTerminalPath", "macAddress",
41 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
42 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
43 "ghCpuType", "ghMemSize", "ipHostNumber", "ghUsbSupport",
44 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
45 var $objectclasses= array("top", "gotoTerminal", "GOhard");
47 function termgeneric ($config, $dn= NULL)
48 {
49 plugin::plugin ($config, $dn);
51 /* Read arrays */
52 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
53 if (!isset($this->attrs[$val])){
54 continue;
55 }
56 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
57 array_push($this->$val, $this->attrs[$val][$i]);
58 }
59 }
61 $this->modes["disabled"]= _("disabled");
62 $this->modes["text"]= _("text");
63 $this->modes["graphic"]= _("graphic");
65 /* Set base */
66 if ($this->dn == "new"){
67 $ui= get_userinfo();
68 $this->base= dn2base($ui->dn);
69 } else {
70 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
71 }
73 $this->orig_dn= $this->dn;
74 }
76 function execute()
77 {
78 /* Do we need to flip is_account state? */
79 if (isset($_POST['modify_state'])){
80 $this->is_account= !$this->is_account;
81 }
83 if (isset($_POST['action'])){
84 switch($_POST['saction']){
85 case 'wake':
86 $cmd= $this->search($this->config->data['TABS'], "termgeneric", "WAKECMD");
87 if ($cmd == ""){
88 print_red(_("No WAKECMD definition found in your gosa.conf"));
89 } else {
90 exec ($cmd." ".$this->macAddress, $dummy, $retval);
91 if ($retval != 0){
92 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
93 }
94 }
95 break;
97 case 'reboot':
98 $cmd= $this->search($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
99 if ($cmd == ""){
100 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
101 } else {
102 exec ($cmd." ".$this->cn, $dummy, $retval);
103 if ($retval != 0){
104 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
105 }
106 }
107 break;
109 case 'halt':
110 $cmd= $this->search($this->config->data['TABS'], "termgeneric", "HALTCMD");
111 if ($cmd == ""){
112 print_red(_("No HALTCMD definition found in your gosa.conf"));
113 } else {
114 exec ($cmd." ".$this->cn, $dummy, $retval);
115 if ($retval != 0){
116 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
117 }
118 }
119 break;
120 }
121 }
123 /* Do we represent a valid terminal? */
124 if (!$this->is_account && $this->parent == NULL){
125 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
126 _("This 'dn' has no terminal features.")."</b>";
127 return($display);
128 }
130 /* Fill templating stuff */
131 $smarty= get_smarty();
132 $smarty->assign("cn", $this->cn);
134 $smarty->assign("bases", $this->config->idepartments);
136 /* Check if terminal is online */
137 $query= "fping -q -r 1 -t 500 ".$this->cn;
138 exec ($query, $dummy, $retval);
140 /* Offline */
141 if ($retval == 0){
142 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
143 } else {
144 $smarty->assign("actions", array("wake" => _("Wake up")));
145 }
146 /* Arrays */
147 $smarty->assign("modes", $this->modes);
148 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
149 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
150 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
152 /* Variables */
153 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
154 "gotoSyslogServer", "gotoNtpServer") as $val){
156 $smarty->assign($val."_select", $this->$val);
157 $smarty->assign($val."ACL", chkacl($this->acl, $val));
158 }
159 $smarty->assign("ipHostNumber", $this->ipHostNumber);
160 $smarty->assign("macAddress", $this->macAddress);
162 /* Show main page */
163 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
164 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
165 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
166 }
168 function remove_from_parent()
169 {
170 $ldap= $this->config->get_ldap_link();
171 $ldap->rmdir($this->dn);
172 show_ldap_error($ldap->get_error());
174 /* Optionally execute a command after we're done */
175 $this->handle_post_events("remove");
177 /* Delete references to object groups */
178 $ldap->cd ($this->config->current['BASE']);
179 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
180 while ($ldap->fetch()){
181 $og= new ogroup($this->config, $ldap->getDN());
182 unset($og->member[$this->dn]);
183 $og->save ();
184 }
186 }
189 /* Save data to object */
190 function save_object()
191 {
192 plugin::save_object();
194 /* Save base, since this is no LDAP attribute */
195 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
196 $this->base= $_POST['base'];
197 }
199 /* Save terminal path to parent since it is used by termstartup, too */
200 $this->parent->by_object['termstartup']->gotoTerminalPath=
201 $this->gotoTerminalPath;
202 }
205 /* Check supplied data */
206 function check()
207 {
208 $message= array();
210 /* Permissions for that base? */
211 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
213 $ui= get_userinfo();
214 $acl= get_permissions ($this->dn, $ui->subtreeACL);
215 $acl= get_module_permission($acl, "group", $this->dn);
216 if (chkacl($acl, "create") != ""){
217 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
218 }
220 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
221 $message[]= _("The required field 'Terminal name' is not set.");
222 }
224 if ($this->orig_dn == 'new'){
225 $ldap= $this->config->get_ldap_link();
226 $ldap->cd ($this->base);
227 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
228 if ($ldap->count() != 0){
229 while ($attrs= $ldap->fetch()){
230 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
231 continue;
232 } else {
233 if ($attrs['dn'] != $this->orig_dn){
234 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
235 break;
236 }
237 }
238 }
239 }
240 }
242 return ($message);
243 }
246 /* Save to LDAP */
247 function save()
248 {
249 plugin::save();
251 /* Strip out 'default' values */
252 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
254 if ($this->attrs[$val] == "default"){
255 $this->attrs[$val]= array();
256 }
257 }
259 /* Add missing arrays */
260 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
261 if (isset ($this->$val) && count ($this->$val) != 0){
262 $this->attrs["$val"]= $this->$val;
263 }
264 }
266 /* Remove all empty values */
267 if ($this->orig_dn == 'new'){
268 $attrs= array();
269 foreach ($this->attrs as $key => $val){
270 if (is_array($val) && count($val) == 0){
271 continue;
272 }
273 $attrs[$key]= $val;
274 }
275 $this->attrs= $attrs;
276 }
278 /* Write back to ldap */
279 $ldap= $this->config->get_ldap_link();
280 if ($this->orig_dn == 'new'){
281 $ldap->cd($this->config->current['BASE']);
282 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
283 $ldap->cd($this->dn);
284 $ldap->add($this->attrs);
285 $this->handle_post_events("add");
286 } else {
287 if ($this->orig_dn != $this->dn){
288 $this->move($this->orig_dn, $this->dn);
289 }
290 $ldap->cd($this->dn);
291 $ldap->modify($this->attrs);
292 $this->handle_post_events("modify");
293 }
294 show_ldap_error($ldap->get_error());
296 /* Optionally execute a command after we're done */
297 $this->postcreate();
298 }
300 }
302 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
303 ?>