124f61bd17ece318ccd11bb176ebefac23568a52
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= search_config($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= search_config($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= search_config($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);
151 $tmp2 = array();
152 foreach($this->config->data['SERVERS']['NFS'] as $server){
153 if($server != "default"){
154 $tmp = split("\|",$server);
155 $tmp3= split(":",$tmp[0]);
157 $servername = $tmp3[0];
158 $nfsname = $tmp3[1];
160 $path = $tmp[4];
162 $tmp2[$servername.":".$path]= $servername.":".$path;
163 }else{
164 $tmp2[$server]=$server;
165 }
166 }
168 $smarty->assign("nfsservers", $tmp2);
169 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
170 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
172 /* Variables */
173 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
174 "gotoSyslogServer", "gotoNtpServer") as $val){
176 $smarty->assign($val."_select", $this->$val);
177 $smarty->assign($val."ACL", chkacl($this->acl, $val));
178 }
179 $smarty->assign("ipHostNumber", $this->ipHostNumber);
180 $smarty->assign("macAddress", $this->macAddress);
182 /* Show main page */
183 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
184 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
185 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
186 }
188 function remove_from_parent()
189 {
190 $ldap= $this->config->get_ldap_link();
191 $ldap->rmdir($this->dn);
192 show_ldap_error($ldap->get_error());
194 /* Optionally execute a command after we're done */
195 $this->handle_post_events("remove");
197 /* Delete references to object groups */
198 $ldap->cd ($this->config->current['BASE']);
199 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
200 while ($ldap->fetch()){
201 $og= new ogroup($this->config, $ldap->getDN());
202 unset($og->member[$this->dn]);
203 $og->save ();
204 }
206 }
209 /* Save data to object */
210 function save_object()
211 {
212 plugin::save_object();
214 /* Save base, since this is no LDAP attribute */
215 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
216 $this->base= $_POST['base'];
217 }
219 /* Save terminal path to parent since it is used by termstartup, too */
220 $this->parent->by_object['termstartup']->gotoTerminalPath=
221 $this->gotoTerminalPath;
222 }
225 /* Check supplied data */
226 function check()
227 {
228 $message= array();
230 /* Permissions for that base? */
231 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
233 $ui= get_userinfo();
234 $acl= get_permissions ($this->dn, $ui->subtreeACL);
235 $acl= get_module_permission($acl, "group", $this->dn);
236 if (chkacl($acl, "create") != ""){
237 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
238 }
240 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
241 $message[]= _("The required field 'Terminal name' is not set.");
242 }
244 if ($this->orig_dn == 'new'){
245 $ldap= $this->config->get_ldap_link();
246 $ldap->cd ($this->base);
247 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
248 if ($ldap->count() != 0){
249 while ($attrs= $ldap->fetch()){
250 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
251 continue;
252 } else {
253 if ($attrs['dn'] != $this->orig_dn){
254 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
255 break;
256 }
257 }
258 }
259 }
260 }
262 return ($message);
263 }
266 /* Save to LDAP */
267 function save()
268 {
269 plugin::save();
271 /* Strip out 'default' values */
272 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
274 if ($this->attrs[$val] == "default"){
275 $this->attrs[$val]= array();
276 }
277 }
279 /* Add missing arrays */
280 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
281 if (isset ($this->$val) && count ($this->$val) != 0){
282 $this->attrs["$val"]= $this->$val;
283 }
284 }
286 /* Remove all empty values */
287 if ($this->orig_dn == 'new'){
288 $attrs= array();
289 foreach ($this->attrs as $key => $val){
290 if (is_array($val) && count($val) == 0){
291 continue;
292 }
293 $attrs[$key]= $val;
294 }
295 $this->attrs= $attrs;
296 }
298 /* Write back to ldap */
299 $ldap= $this->config->get_ldap_link();
300 if ($this->orig_dn == 'new'){
301 $ldap->cd($this->config->current['BASE']);
302 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
303 $ldap->cd($this->dn);
304 $ldap->add($this->attrs);
305 $this->handle_post_events("add");
306 } else {
307 if ($this->orig_dn != $this->dn){
308 $this->move($this->orig_dn, $this->dn);
309 }
310 $ldap->cd($this->dn);
311 $ldap->modify($this->attrs);
312 $this->handle_post_events("modify");
313 }
314 show_ldap_error($ldap->get_error());
316 /* Optionally execute a command after we're done */
317 $this->postcreate();
318 }
320 }
322 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
323 ?>