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 /* Call parent execute */
80 plugin::execute();
82 /* Do we need to flip is_account state? */
83 if (isset($_POST['modify_state'])){
84 $this->is_account= !$this->is_account;
85 }
87 if (isset($_POST['action'])){
88 switch($_POST['saction']){
89 case 'wake':
90 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
91 if ($cmd == ""){
92 print_red(_("No WAKECMD definition found in your gosa.conf"));
93 } else {
94 exec ($cmd." ".$this->macAddress, $dummy, $retval);
95 if ($retval != 0){
96 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
97 }
98 }
99 break;
101 case 'reboot':
102 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
103 if ($cmd == ""){
104 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
105 } else {
106 exec ($cmd." ".$this->cn, $dummy, $retval);
107 if ($retval != 0){
108 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
109 }
110 }
111 break;
113 case 'halt':
114 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
115 if ($cmd == ""){
116 print_red(_("No HALTCMD definition found in your gosa.conf"));
117 } else {
118 exec ($cmd." ".$this->cn, $dummy, $retval);
119 if ($retval != 0){
120 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
121 }
122 }
123 break;
124 }
125 }
127 /* Do we represent a valid terminal? */
128 if (!$this->is_account && $this->parent == NULL){
129 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
130 _("This 'dn' has no terminal features.")."</b>";
131 return($display);
132 }
134 /* Fill templating stuff */
135 $smarty= get_smarty();
136 $smarty->assign("cn", $this->cn);
137 $smarty->assign("staticAddress", "");
139 $smarty->assign("bases", $this->config->idepartments);
141 /* Check if terminal is online */
142 $query= "fping -q -r 1 -t 500 ".$this->cn;
143 exec ($query, $dummy, $retval);
145 /* Offline */
146 if ($retval == 0){
147 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
148 } else {
149 $smarty->assign("actions", array("wake" => _("Wake up")));
150 }
151 /* Arrays */
152 $smarty->assign("modes", $this->modes);
154 $tmp2 = array();
155 foreach($this->config->data['SERVERS']['NFS'] as $server){
156 if($server != "default"){
157 $tmp = split("\|",$server);
158 $tmp3= split(":",$tmp[0]);
160 $servername = $tmp3[0];
161 $nfsname = $tmp3[1];
163 $path ="";
164 if(isset($tmp[4])){
165 $path = $tmp[4];
166 }
168 $tmp2[$servername.":".$path]= $servername.":".$path;
169 }else{
170 $tmp2[$server]=$server;
171 }
172 }
174 $smarty->assign("nfsservers", $tmp2);
175 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
176 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
178 /* Variables */
179 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
180 "gotoSyslogServer", "gotoNtpServer") as $val){
182 $smarty->assign($val."_select", $this->$val);
183 $smarty->assign($val."ACL", chkacl($this->acl, $val));
184 }
185 $smarty->assign("ipHostNumber", $this->ipHostNumber);
186 $smarty->assign("macAddress", $this->macAddress);
188 /* Show main page */
189 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
190 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
191 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
192 }
194 function remove_from_parent()
195 {
197 $ldap= $this->config->get_ldap_link();
198 $ldap->cd($this->dn);
199 $ldap->cat($this->dn);
200 if($ldap->count()){
202 $ldap->rmdir($this->dn);
203 show_ldap_error($ldap->get_error());
205 /* Optionally execute a command after we're done */
206 $this->handle_post_events("remove");
208 /* Delete references to object groups */
209 $ldap->cd ($this->config->current['BASE']);
210 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
211 while ($ldap->fetch()){
212 $og= new ogroup($this->config, $ldap->getDN());
213 unset($og->member[$this->dn]);
214 $og->save ();
215 }
216 }
217 }
220 /* Save data to object */
221 function save_object()
222 {
223 plugin::save_object();
225 /* Save base, since this is no LDAP attribute */
226 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
227 $this->base= $_POST['base'];
228 }
230 /* Save terminal path to parent since it is used by termstartup, too */
231 $this->parent->by_object['termstartup']->gotoTerminalPath=
232 $this->gotoTerminalPath;
233 }
236 /* Check supplied data */
237 function check()
238 {
239 $message= array();
241 /* Permissions for that base? */
242 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
244 $ui= get_userinfo();
245 $acl= get_permissions ($this->dn, $ui->subtreeACL);
246 $acl= get_module_permission($acl, "group", $this->dn);
247 if (chkacl($acl, "create") != ""){
248 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
249 }
251 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
252 $message[]= _("The required field 'Terminal name' is not set.");
253 }
255 if ($this->orig_dn == 'new'){
256 $ldap= $this->config->get_ldap_link();
257 $ldap->cd ($this->base);
258 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
259 if ($ldap->count() != 0){
260 while ($attrs= $ldap->fetch()){
261 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
262 continue;
263 } else {
264 if ($attrs['dn'] != $this->orig_dn){
265 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
266 break;
267 }
268 }
269 }
270 }
271 }
273 return ($message);
274 }
277 /* Save to LDAP */
278 function save()
279 {
280 plugin::save();
282 /* Strip out 'default' values */
283 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
285 if ($this->attrs[$val] == "default"){
286 $this->attrs[$val]= array();
287 }
288 }
290 /* Add missing arrays */
291 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
292 if (isset ($this->$val) && count ($this->$val) != 0){
293 $this->attrs["$val"]= $this->$val;
294 }
295 }
297 /* Remove all empty values */
298 if ($this->orig_dn == 'new'){
299 $attrs= array();
300 foreach ($this->attrs as $key => $val){
301 if (is_array($val) && count($val) == 0){
302 continue;
303 }
304 $attrs[$key]= $val;
305 }
306 $this->attrs= $attrs;
307 }
309 /* Write back to ldap */
310 $ldap= $this->config->get_ldap_link();
311 if ($this->orig_dn == 'new'){
312 $ldap->cd($this->config->current['BASE']);
313 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
314 $ldap->cd($this->dn);
315 $ldap->add($this->attrs);
316 $this->handle_post_events("add");
317 } else {
318 if ($this->orig_dn != $this->dn){
319 $this->move($this->orig_dn, $this->dn);
320 }
321 $ldap->cd($this->dn);
322 $ldap->modify($this->attrs);
323 $this->handle_post_events("modify");
324 }
325 show_ldap_error($ldap->get_error());
327 /* Optionally execute a command after we're done */
328 $this->postcreate();
329 }
331 }
333 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
334 ?>