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 src=\"images/stop.png\" align=center> <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 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
165 }
167 function remove_from_parent()
168 {
169 $ldap= $this->config->get_ldap_link();
170 $ldap->rmdir($this->dn);
171 show_ldap_error($ldap->get_error());
173 /* Optionally execute a command after we're done */
174 $this->handle_post_events("remove");
176 /* Delete references to object groups */
177 $ldap->cd ($this->config->current['BASE']);
178 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
179 while ($ldap->fetch()){
180 $og= new ogroup($this->config, $ldap->getDN());
181 unset($og->member[$this->dn]);
182 $og->save ();
183 }
185 }
188 /* Save data to object */
189 function save_object()
190 {
191 plugin::save_object();
193 /* Save base, since this is no LDAP attribute */
194 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
195 $this->base= $_POST['base'];
196 }
198 /* Save terminal path to parent since it is used by termstartup, too */
199 $this->parent->by_object['termstartup']->gotoTerminalPath=
200 $this->gotoTerminalPath;
201 }
204 /* Check supplied data */
205 function check()
206 {
207 $message= array();
209 /* Permissions for that base? */
210 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
212 $ui= get_userinfo();
213 $acl= get_permissions ($this->dn, $ui->subtreeACL);
214 $acl= get_module_permission($acl, "group", $this->dn);
215 if (chkacl($acl, "create") != ""){
216 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
217 }
219 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
220 $message[]= _("The required field 'Terminal name' is not set.");
221 }
223 if ($this->orig_dn == 'new'){
224 $ldap= $this->config->get_ldap_link();
225 $ldap->cd ($this->base);
226 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
227 if ($ldap->count() != 0){
228 while ($attrs= $ldap->fetch()){
229 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
230 continue;
231 } else {
232 if ($attrs['dn'] != $this->orig_dn){
233 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
234 break;
235 }
236 }
237 }
238 }
239 }
241 return ($message);
242 }
245 /* Save to LDAP */
246 function save()
247 {
248 plugin::save();
250 /* Strip out 'default' values */
251 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
253 if ($this->attrs[$val] == "default"){
254 $this->attrs[$val]= array();
255 }
256 }
258 /* Add missing arrays */
259 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
260 if (isset ($this->$val) && count ($this->$val) != 0){
261 $this->attrs["$val"]= $this->$val;
262 }
263 }
265 /* Remove all empty values */
266 if ($this->orig_dn == 'new'){
267 $attrs= array();
268 foreach ($this->attrs as $key => $val){
269 if (is_array($val) && count($val) == 0){
270 continue;
271 }
272 $attrs[$key]= $val;
273 }
274 $this->attrs= $attrs;
275 }
277 /* Write back to ldap */
278 $ldap= $this->config->get_ldap_link();
279 if ($this->orig_dn == 'new'){
280 $ldap->cd($this->config->current['BASE']);
281 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
282 $ldap->cd($this->dn);
283 $ldap->add($this->attrs);
284 $this->handle_post_events("add");
285 } else {
286 if ($this->orig_dn != $this->dn){
287 $this->move($this->orig_dn, $this->dn);
288 }
289 $ldap->cd($this->dn);
290 $ldap->modify($this->attrs);
291 $this->handle_post_events("modify");
292 }
293 show_ldap_error($ldap->get_error());
295 /* Optionally execute a command after we're done */
296 $this->postcreate();
297 }
299 }
301 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
302 ?>