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 $gotoSyslogServers = array();
16 var $gotoNtpServer= array();
17 var $gotoNtpServers= array();
18 var $gotoSndModule= "";
19 var $gotoFloppyEnable= "";
20 var $gotoCdromEnable= "";
21 var $ghCpuType= "-";
22 var $ghMemSize= "-";
23 var $ghUsbSupport= "-";
24 var $ghNetNic= array();
25 var $ghIdeDev= array();
26 var $ghScsiDev= array();
27 var $ghGfxAdapter= "-";
28 var $ghSoundAdapter= "-";
29 var $gotoLastUser= "-";
30 var $netConfigDNS;
31 /* Needed values and lists */
32 var $base= "";
33 var $cn= "";
34 var $orig_dn= "";
36 var $inheritTimeServer = true;
38 /* Plugin side filled */
39 var $modes= array();
41 /* attribute list for save action */
42 var $ignore_account= TRUE;
43 var $attributes= array("gotoMode", "gotoTerminalPath",
44 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
45 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
46 "ghCpuType", "ghMemSize","ghUsbSupport",
47 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
48 var $objectclasses= array("top", "gotoTerminal", "GOhard");
50 function termgeneric ($config, $dn= NULL)
51 {
52 plugin::plugin ($config, $dn);
53 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
54 /* Read arrays */
55 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
56 if (!isset($this->attrs[$val])){
57 continue;
58 }
59 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
60 array_push($this->$val, $this->attrs[$val][$i]);
61 }
62 }
64 /* Create used ntp server array */
65 $this->gotoNtpServer= array();
66 if(isset($this->attrs['gotoNtpServer'])){
67 $this->inheritTimeServer = false;
68 unset($this->attrs['gotoNtpServer']['count']);
69 foreach($this->attrs['gotoNtpServer'] as $server){
70 $this->gotoNtpServer[$server] = $server;
71 }
72 }
74 /* Set inherit checkbox state */
75 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
76 $this->inheritTimeServer = true;
77 $this->gotoNtpServer=array();
78 }
80 /* Create available ntp options */
81 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
82 foreach($this->gotoNtpServers as $key => $server){
83 if($server == "default"){
84 unset($this->gotoNtpServers[$key]);
85 }
86 }
88 $this->modes["disabled"]= _("disabled");
89 $this->modes["text"]= _("text");
90 $this->modes["graphic"]= _("graphic");
92 /* Set base */
93 if ($this->dn == "new"){
94 $ui= get_userinfo();
95 $this->base= dn2base($ui->dn);
96 } else {
97 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
98 }
100 /* Create an array of all Syslog servers */
101 $tmp = $this->config->data['SERVERS']['SYSLOG'];
102 foreach($tmp as $server){
103 $visible = $server;
104 if($server == "default") {
105 $visible = "["._("inherited")."]";
106 }
107 $this->gotoSyslogServers[$server] = $visible;
108 }
110 $this->orig_dn= $this->dn;
111 }
113 function execute()
114 {
115 /* Call parent execute */
116 plugin::execute();
118 /* Do we need to flip is_account state? */
119 if (isset($_POST['modify_state'])){
120 $this->is_account= !$this->is_account;
121 }
123 if (isset($_POST['action'])){
124 switch($_POST['saction']){
125 case 'wake':
126 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
127 if ($cmd == ""){
128 print_red(_("No WAKECMD definition found in your gosa.conf"));
129 } else {
130 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
131 if ($retval != 0){
132 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
133 }
134 }
135 break;
137 case 'reboot':
138 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
139 if ($cmd == ""){
140 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
141 } else {
142 exec ($cmd." ".$this->cn, $dummy, $retval);
143 if ($retval != 0){
144 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
145 }
146 }
147 break;
149 case 'halt':
150 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
151 if ($cmd == ""){
152 print_red(_("No HALTCMD definition found in your gosa.conf"));
153 } else {
154 exec ($cmd." ".$this->cn, $dummy, $retval);
155 if ($retval != 0){
156 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
157 }
158 }
159 break;
160 }
161 }
163 /* Base select dialog */
164 $once = true;
165 foreach($_POST as $name => $value){
166 if(preg_match("/^chooseBase/",$name) && $once){
167 $once = false;
168 $this->dialog = new baseSelectDialog($this->config);
169 $this->dialog->setCurrentBase($this->base);
170 }
171 }
173 /* Dialog handling */
174 if(is_object($this->dialog)){
175 /* Must be called before save_object */
176 $this->dialog->save_object();
178 if($this->dialog->isClosed()){
179 $this->dialog = false;
180 }elseif($this->dialog->isSelected()){
181 $this->base = $this->dialog->isSelected();
182 $this->dialog= false;
183 }else{
184 return($this->dialog->execute());
185 }
186 }
188 /* Do we represent a valid terminal? */
189 if (!$this->is_account && $this->parent == NULL){
190 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
191 _("This 'dn' has no terminal features.")."</b>";
192 return($display);
193 }
195 /* Add new ntp Server to our list */
196 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
197 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
198 }
200 /* Delete selected NtpServer for list of used servers */
201 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
202 foreach($_POST['gotoNtpServerSelected'] as $name){
203 unset($this->gotoNtpServer[$name]);
204 }
205 }
207 /* Fill templating stuff */
208 $smarty= get_smarty();
209 $smarty->assign("cn", $this->cn);
210 $smarty->assign("staticAddress", "");
212 $smarty->assign("bases", $this->config->idepartments);
214 /* tell smarty the inherit checkbox state */
215 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
217 /* Check if terminal is online */
218 $query= "fping -q -r 1 -t 500 ".$this->cn;
219 exec ($query, $dummy, $retval);
221 /* Offline */
222 if ($retval == 0){
223 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
224 } else {
225 $smarty->assign("actions", array("wake" => _("Wake up")));
226 }
227 /* Arrays */
228 $smarty->assign("modes", $this->modes);
230 $tmp2 = array();
231 foreach($this->config->data['SERVERS']['NFS'] as $server){
232 if($server != "default"){
233 $tmp = split("\|",$server);
234 $tmp3= split(":",$tmp[0]);
236 $servername = $tmp3[0];
237 $nfsname = $tmp3[1];
239 $path ="";
240 if(isset($tmp[4])){
241 $path = $tmp[4];
242 }
244 $tmp2[$servername.":".$path]= $servername.":".$path;
245 }else{
246 $tmp2[$server]="["._("inherited")."]";
247 }
248 }
250 $smarty->assign("nfsservers", $tmp2);
254 $smarty->assign("syslogservers", $this->gotoSyslogServers);
255 $smarty->assign("ntpservers", $this->gotoNtpServers);
257 /* Variables */
258 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
259 "gotoSyslogServer", "gotoNtpServer") as $val){
261 $smarty->assign($val."_select", $this->$val);
262 $smarty->assign($val."ACL", chkacl($this->acl, $val));
263 }
265 /* Show main page */
266 $smarty->assign("netconfig", $this->netConfigDNS->execute());
267 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
268 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
269 }
271 function remove_from_parent()
272 {
274 $ldap= $this->config->get_ldap_link();
275 $ldap->cd($this->dn);
276 $ldap->cat($this->dn, array('dn'));
277 if($ldap->count()){
278 $this->netConfigDNS->remove_from_parent();
279 $ldap->rmDir($this->dn);
280 show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
282 /* Optionally execute a command after we're done */
283 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
285 /* Delete references to object groups */
286 $ldap->cd ($this->config->current['BASE']);
287 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
288 while ($ldap->fetch()){
289 $og= new ogroup($this->config, $ldap->getDN());
290 unset($og->member[$this->dn]);
291 $og->save ();
292 }
293 }
294 }
297 /* Save data to object */
298 function save_object()
299 {
300 plugin::save_object();
301 $this->netConfigDNS->save_object();
302 /* Save base, since this is no LDAP attribute */
303 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
304 $this->base= $_POST['base'];
305 }
307 /* Save terminal path to parent since it is used by termstartup, too */
308 $this->parent->by_object['termstartup']->gotoTerminalPath=
309 $this->gotoTerminalPath;
311 if(isset($_POST['termgeneric_posted'])){
312 if(isset($_POST["inheritTimeServer"])){
313 $this->inheritTimeServer = true;
314 }else{
315 $this->inheritTimeServer = false;
316 }
317 }
318 }
321 /* Check supplied data */
322 function check()
323 {
324 /* Call common method to give check the hook */
325 $message= plugin::check();
326 $message= array_merge($message, $this->netConfigDNS->check());
328 /* Permissions for that base? */
329 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
331 $ui= get_userinfo();
332 $acl= get_permissions ($this->dn, $ui->subtreeACL);
333 $acl= get_module_permission($acl, "group", $this->dn);
334 if (chkacl($acl, "create") != ""){
335 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
336 }
338 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
339 $message[]= _("The required field 'Terminal name' is not set.");
340 }
342 if ($this->orig_dn == 'new'){
343 $ldap= $this->config->get_ldap_link();
344 $ldap->cd ($this->base);
345 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
346 if ($ldap->count() != 0){
347 while ($attrs= $ldap->fetch()){
348 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
349 continue;
350 } else {
351 if ($attrs['dn'] != $this->orig_dn){
352 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
353 break;
354 }
355 }
356 }
357 }
358 }
360 /* Check for valid ntpServer selection */
361 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
362 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
363 }
365 return ($message);
366 }
369 /* Save to LDAP */
370 function save()
371 {
372 plugin::save();
374 /* Strip out 'default' values */
375 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
377 if ($this->attrs[$val] == "default"){
378 $this->attrs[$val]= array();
379 }
380 }
382 /* Add missing arrays */
383 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
384 if (isset ($this->$val) && count ($this->$val) != 0){
385 $this->attrs["$val"]= $this->$val;
386 }
387 }
389 /* Remove all empty values */
390 if ($this->orig_dn == 'new'){
391 $attrs= array();
392 foreach ($this->attrs as $key => $val){
393 if (is_array($val) && count($val) == 0){
394 continue;
395 }
396 $attrs[$key]= $val;
397 }
398 $this->attrs= $attrs;
399 }
401 if($this->inheritTimeServer){
402 $this->attrs['gotoNtpServer'] = array();
403 }else{
404 /* Set ntpServers */
405 $this->attrs['gotoNtpServer'] = array();
406 foreach($this->gotoNtpServer as $server){
407 $this->attrs['gotoNtpServer'][] = $server;
408 }
409 }
411 /* Write back to ldap */
412 $ldap= $this->config->get_ldap_link();
413 if ($this->orig_dn == 'new'){
414 $ldap->cd($this->config->current['BASE']);
415 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
416 $ldap->cd($this->dn);
417 $ldap->add($this->attrs);
418 $this->handle_post_events("add");
419 } else {
420 if ($this->orig_dn != $this->dn){
421 $this->move($this->orig_dn, $this->dn);
422 }
423 $ldap->cd($this->dn);
424 $this->cleanup();
425 $ldap->modify ($this->attrs);
427 $this->handle_post_events("modify");
428 }
429 $this->netConfigDNS->cn = $this->cn;
430 $this->netConfigDNS->save($this->dn);
431 show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
433 /* Optionally execute a command after we're done */
434 $this->postcreate();
435 }
437 }
439 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
440 ?>