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);
134 $smarty->assign("staticAddress", "");
136 $smarty->assign("bases", $this->config->idepartments);
138 /* Check if terminal is online */
139 $query= "fping -q -r 1 -t 500 ".$this->cn;
140 exec ($query, $dummy, $retval);
142 /* Offline */
143 if ($retval == 0){
144 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
145 } else {
146 $smarty->assign("actions", array("wake" => _("Wake up")));
147 }
148 /* Arrays */
149 $smarty->assign("modes", $this->modes);
150 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
151 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
152 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
154 /* Variables */
155 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
156 "gotoSyslogServer", "gotoNtpServer") as $val){
158 $smarty->assign($val."_select", $this->$val);
159 $smarty->assign($val."ACL", chkacl($this->acl, $val));
160 }
161 $smarty->assign("ipHostNumber", $this->ipHostNumber);
162 $smarty->assign("macAddress", $this->macAddress);
164 /* Show main page */
165 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
166 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
167 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
168 }
170 function remove_from_parent()
171 {
172 $ldap= $this->config->get_ldap_link();
173 $ldap->rmdir($this->dn);
174 show_ldap_error($ldap->get_error());
176 /* Optionally execute a command after we're done */
177 $this->handle_post_events("remove");
179 /* Delete references to object groups */
180 $ldap->cd ($this->config->current['BASE']);
181 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
182 while ($ldap->fetch()){
183 $og= new ogroup($this->config, $ldap->getDN());
184 unset($og->member[$this->dn]);
185 $og->save ();
186 }
188 }
191 /* Save data to object */
192 function save_object()
193 {
194 plugin::save_object();
196 /* Save base, since this is no LDAP attribute */
197 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
198 $this->base= $_POST['base'];
199 }
201 /* Save terminal path to parent since it is used by termstartup, too */
202 $this->parent->by_object['termstartup']->gotoTerminalPath=
203 $this->gotoTerminalPath;
204 }
207 /* Check supplied data */
208 function check()
209 {
210 $message= array();
212 /* Permissions for that base? */
213 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
215 $ui= get_userinfo();
216 $acl= get_permissions ($this->dn, $ui->subtreeACL);
217 $acl= get_module_permission($acl, "group", $this->dn);
218 if (chkacl($acl, "create") != ""){
219 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
220 }
222 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
223 $message[]= _("The required field 'Terminal name' is not set.");
224 }
226 if ($this->orig_dn == 'new'){
227 $ldap= $this->config->get_ldap_link();
228 $ldap->cd ($this->base);
229 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
230 if ($ldap->count() != 0){
231 while ($attrs= $ldap->fetch()){
232 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
233 continue;
234 } else {
235 if ($attrs['dn'] != $this->orig_dn){
236 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
237 break;
238 }
239 }
240 }
241 }
242 }
244 return ($message);
245 }
248 /* Save to LDAP */
249 function save()
250 {
251 plugin::save();
253 /* Strip out 'default' values */
254 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
256 if ($this->attrs[$val] == "default"){
257 $this->attrs[$val]= array();
258 }
259 }
261 /* Add missing arrays */
262 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
263 if (isset ($this->$val) && count ($this->$val) != 0){
264 $this->attrs["$val"]= $this->$val;
265 }
266 }
268 /* Remove all empty values */
269 if ($this->orig_dn == 'new'){
270 $attrs= array();
271 foreach ($this->attrs as $key => $val){
272 if (is_array($val) && count($val) == 0){
273 continue;
274 }
275 $attrs[$key]= $val;
276 }
277 $this->attrs= $attrs;
278 }
280 /* Write back to ldap */
281 $ldap= $this->config->get_ldap_link();
282 if ($this->orig_dn == 'new'){
283 $ldap->cd($this->config->current['BASE']);
284 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
285 $ldap->cd($this->dn);
286 $ldap->add($this->attrs);
287 $this->handle_post_events("add");
288 } else {
289 if ($this->orig_dn != $this->dn){
290 $this->move($this->orig_dn, $this->dn);
291 }
292 $ldap->cd($this->dn);
293 $ldap->modify($this->attrs);
294 $this->handle_post_events("modify");
295 }
296 show_ldap_error($ldap->get_error());
298 /* Optionally execute a command after we're done */
299 $this->postcreate();
300 }
302 }
304 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
305 ?>