1 <?php
3 class termgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $gotoMode= "disabled";
7 var $gotoTerminalPath= "";
8 var $gotoSwapServer= "";
9 var $gotoSyslogServer= "";
10 var $gotoSyslogServers = array();
11 var $gotoNtpServer= array();
12 var $gotoNtpServers= array();
13 var $gotoSndModule= "";
14 var $gotoFloppyEnable= "";
15 var $gotoCdromEnable= "";
16 var $ghCpuType= "-";
17 var $ghMemSize= "-";
18 var $ghUsbSupport= "-";
19 var $ghNetNic= array();
20 var $ghIdeDev= array();
21 var $ghScsiDev= array();
22 var $ghGfxAdapter= "-";
23 var $ghSoundAdapter= "-";
24 var $gotoLastUser= "-";
25 var $netConfigDNS;
26 /* Needed values and lists */
27 var $base= "";
28 var $cn= "";
29 var $orig_dn= "";
31 var $inheritTimeServer = true;
33 /* Plugin side filled */
34 var $modes= array();
36 /* attribute list for save action */
37 var $ignore_account= TRUE;
38 var $attributes= array("gotoMode", "gotoTerminalPath",
39 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
40 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
41 "ghCpuType", "ghMemSize","ghUsbSupport",
42 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
43 var $objectclasses= array("top", "gotoTerminal", "GOhard");
45 var $mapActions = array("reboot" => "",
46 "instant_update" => "softupdate",
47 "update" => "scheduledupdate",
48 "reinstall" => "install",
49 "rescan" => "",
50 "memcheck" => "memcheck",
51 "sysinfo" => "sysinfo");
53 var $fai_activated = FALSE;
55 var $member_of_ogroup = FALSE;
57 function termgeneric ($config, $dn= NULL, $parent= NULL)
58 {
59 /* Check if FAI is activated */
60 $tmp = search_config($config->data,"faiManagement","CLASS");
61 if(!empty($tmp)){
62 $this->fai_activated = TRUE;
63 }
65 plugin::plugin ($config, $dn, $parent);
67 if(!isset($this->parent->by_object['ogroup'])){
68 $ldap = $this->config->get_ldap_link();
69 $ldap->cd ($this->config->current['BASE']);
70 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))",array("cn"));
71 $this->member_of_ogroup = $ldap->count() >= 1;
72 }
74 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
75 /* Read arrays */
76 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
77 if (!isset($this->attrs[$val])){
78 continue;
79 }
80 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
81 array_push($this->$val, $this->attrs[$val][$i]);
82 }
83 }
85 /* Create used ntp server array */
86 $this->gotoNtpServer= array();
87 if(isset($this->attrs['gotoNtpServer'])){
88 $this->inheritTimeServer = false;
89 unset($this->attrs['gotoNtpServer']['count']);
90 foreach($this->attrs['gotoNtpServer'] as $server){
91 $this->gotoNtpServer[$server] = $server;
92 }
93 }
95 /* Set inherit checkbox state */
96 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
97 $this->inheritTimeServer = true;
98 $this->gotoNtpServer=array();
99 }
101 /* Create available ntp options */
102 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
103 foreach($this->gotoNtpServers as $key => $server){
104 if($server == "default"){
105 unset($this->gotoNtpServers[$key]);
106 }
107 }
109 $this->modes["disabled"]= _("disabled");
110 $this->modes["text"]= _("text");
111 $this->modes["graphic"]= _("graphic");
113 /* Set base */
114 if ($this->dn == "new"){
115 $ui= get_userinfo();
116 $this->base= dn2base($ui->dn);
117 } else {
118 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
119 }
121 /* Create an array of all Syslog servers */
122 $tmp = $this->config->data['SERVERS']['SYSLOG'];
123 foreach($tmp as $server){
124 $visible = $server;
125 if($server == "default") {
126 $visible = "["._("inherited")."]";
127 }
128 $this->gotoSyslogServers[$server] = $visible;
129 }
131 $this->orig_dn= $this->dn;
132 }
134 function execute()
135 {
136 /* Call parent execute */
137 plugin::execute();
139 $this->netConfigDNS->acl = $this->acl;
141 /* Do we need to flip is_account state? */
142 if (isset($_POST['modify_state'])){
143 $this->is_account= !$this->is_account;
144 }
146 if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") ==""){
148 if($this->fai_activated && $this->dn != "new"){
149 /* Set FAIstate */
150 $ldap = $this->config->get_ldap_link();
151 $ldap->cd($this->config->current['BASE']);
152 $ldap->cat($this->dn,array("objectClass"));
153 $res = $ldap->fetch();
155 $attrs = array();
156 $attrs['FAIstate'] = "";
157 if(isset($this->mapActions[$_POST['saction']])){
158 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
159 }
161 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
162 $attrs['objectClass'][] = $res['objectClass'][$i];
163 }
165 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
166 $attrs['objectClass'][] = "FAIobject";
167 }
169 if($attrs['FAIstate'] == ""){
170 #FIXME we should check if FAIobject is used anymore
171 $attrs['FAIstate'] = array();
172 }
174 $ldap->cd($this->dn);
175 $ldap->modify($attrs);
176 show_ldap_error($ldap->get_error());
177 }
179 switch($_POST['saction']){
180 case 'wake':
181 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
182 if ($cmd == ""){
183 print_red(_("No WAKECMD definition found in your gosa.conf"));
184 } else {
185 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
186 if ($retval != 0){
187 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
188 }
189 }
190 break;
192 case 'reboot':
193 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
194 if ($cmd == ""){
195 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
196 } else {
197 exec ($cmd." ".$this->cn, $dummy, $retval);
198 if ($retval != 0){
199 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
200 }
201 }
202 break;
204 case 'halt':
205 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
206 if ($cmd == ""){
207 print_red(_("No HALTCMD definition found in your gosa.conf"));
208 } else {
209 exec ($cmd." ".$this->cn, $dummy, $retval);
210 if ($retval != 0){
211 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
212 }
213 }
214 break;
215 }
216 }
218 /* Base select dialog */
219 $once = true;
220 foreach($_POST as $name => $value){
221 if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
222 $once = false;
223 $this->dialog = new baseSelectDialog($this->config);
224 $this->dialog->setCurrentBase($this->base);
225 }
226 }
228 /* Dialog handling */
229 if(is_object($this->dialog)){
230 /* Must be called before save_object */
231 $this->dialog->save_object();
233 if($this->dialog->isClosed()){
234 $this->dialog = false;
235 }elseif($this->dialog->isSelected()){
236 $this->base = $this->dialog->isSelected();
237 $this->dialog= false;
238 }else{
239 return($this->dialog->execute());
240 }
241 }
243 /* Do we represent a valid terminal? */
244 if (!$this->is_account && $this->parent == NULL){
245 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
246 _("This 'dn' has no terminal features.")."</b>";
247 return($display);
248 }
250 /* Add new ntp Server to our list */
251 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer")== ""){
252 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
253 }
255 /* Delete selected NtpServer for list of used servers */
256 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer")== ""){
257 foreach($_POST['gotoNtpServerSelected'] as $name){
258 unset($this->gotoNtpServer[$name]);
259 }
260 }
262 /* Fill templating stuff */
263 $smarty= get_smarty();
264 $smarty->assign("cn", $this->cn);
265 $smarty->assign("cnACL", chkacl($this->acl,"cn"));
266 $smarty->assign("staticAddress", "");
268 $smarty->assign("bases", $this->config->idepartments);
270 /* tell smarty the inherit checkbox state */
271 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
273 /* Check if terminal is online */
274 $query= "fping -q -r 1 -t 500 ".$this->cn;
275 exec ($query, $dummy, $retval);
277 /* Offline */
278 if ($retval == 0){
279 $smarty->assign("actions", array( "halt" => _("Switch off"),
280 "reboot" => _("Reboot"),
281 "memcheck" => _("Memory test"),
282 "sysinfo" => _("System analysis")));
283 } else {
284 $smarty->assign("actions", array("wake" => _("Wake up"),
285 "memcheck" => _("Memory test"),
286 "sysinfo" => _("System analysis")));
287 }
289 /* Arrays */
290 $smarty->assign("modes", $this->modes);
292 $tmp2 = array();
293 foreach($this->config->data['SERVERS']['NFS'] as $server){
294 if($server != "default"){
295 $tmp2[$server]= $server;
296 }else{
297 $tmp2[$server]="["._("inherited")."]";
298 }
299 }
301 $smarty->assign("nfsservers", $tmp2);
305 $smarty->assign("syslogservers", $this->gotoSyslogServers);
306 $smarty->assign("ntpservers", $this->gotoNtpServers);
308 /* Variables */
309 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
310 "gotoSyslogServer", "gotoNtpServer") as $val){
312 $smarty->assign($val."_select", $this->$val);
313 $smarty->assign($val."ACL", chkacl($this->acl, $val));
314 }
316 /* Show main page */
317 $this->netConfigDNS->cn= $this->cn;
318 $smarty->assign("netconfig", $this->netConfigDNS->execute());
319 $smarty->assign("fai_activated",$this->fai_activated);
320 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
322 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
324 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
325 }
327 function remove_from_parent()
328 {
330 $this->netConfigDNS->acl = $this->acl;
331 $ldap= $this->config->get_ldap_link();
332 $ldap->cd($this->dn);
333 $ldap->cat($this->dn, array('dn'));
334 if($ldap->count()){
335 $this->netConfigDNS->remove_from_parent();
336 $ldap->rmDir($this->dn);
337 show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
339 /* Optionally execute a command after we're done */
340 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
342 /* Delete references to object groups */
343 $ldap->cd ($this->config->current['BASE']);
344 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
345 while ($ldap->fetch()){
346 $og= new ogroup($this->config, $ldap->getDN());
347 unset($og->member[$this->dn]);
348 $og->save ();
349 }
350 }
351 }
354 /* Save data to object */
355 function save_object()
356 {
357 plugin::save_object();
358 $this->netConfigDNS->save_object();
360 if(isset($_POST["inheritAll"])){
361 $this->set_everything_to_inherited();
362 }
364 /* Save base, since this is no LDAP attribute */
365 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
366 $this->base= $_POST['base'];
367 }
369 /* Save terminal path to parent since it is used by termstartup, too */
370 if(isset($this->parent->by_object['termstartup'])){
371 $this->parent->by_object['termstartup']->gotoTerminalPath=
372 $this->gotoTerminalPath;
373 }
375 if(isset($_POST['termgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
376 if(isset($_POST["inheritTimeServer"])){
377 $this->inheritTimeServer = true;
378 }else{
379 $this->inheritTimeServer = false;
380 }
381 }
382 }
385 /* Check supplied data */
386 function check()
387 {
388 /* Call common method to give check the hook */
389 $message= plugin::check();
391 if($this->cn != "default"){
392 $message= array_merge($message, $this->netConfigDNS->check());
393 }
395 /* Permissions for that base? */
396 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
398 $ui= get_userinfo();
399 $acl= get_permissions ($this->dn, $ui->subtreeACL);
400 $acl= get_module_permission($acl, "group", $this->dn);
401 if (chkacl($acl, "create") != ""){
402 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
403 }
405 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
406 $message[]= _("The required field 'Terminal name' is not set.");
407 }
409 if ($this->orig_dn == 'new'){
410 $ldap= $this->config->get_ldap_link();
411 $ldap->cd ($this->config->current['BASE']);
413 /* It is possible to have a 'default' terminal on every base */
414 if($this->cn == "default"){
415 $ldap->cat($this->dn);
416 }else{
417 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
418 }
420 /* Check if there are some other terminals found */
421 if ($ldap->count() != 0){
422 while ($attrs= $ldap->fetch()){
423 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
424 continue;
425 } else {
426 if ($attrs['dn'] != $this->orig_dn){
427 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
428 break;
429 }
430 }
431 }
432 }
433 }
435 /* Check for valid ntpServer selection */
436 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
437 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
438 }
440 return ($message);
441 }
444 /* Save to LDAP */
445 function save()
446 {
447 $this->netConfigDNS->acl = $this->acl;
449 /* Move object if requested */
450 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
451 $this->move($this->orig_dn, $this->dn);
452 }
454 plugin::save();
456 /* Strip out 'default' values */
457 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
459 if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
460 $this->attrs[$val]= array();
461 }
462 }
464 /* Add missing arrays */
465 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
466 if (isset ($this->$val) && count ($this->$val) != 0){
467 $this->attrs["$val"]= $this->$val;
468 }
469 }
471 /* Remove all empty values */
472 if ($this->orig_dn == 'new'){
473 $attrs= array();
474 foreach ($this->attrs as $key => $val){
475 if (is_array($val) && count($val) == 0){
476 continue;
477 }
478 $attrs[$key]= $val;
479 }
480 $this->attrs= $attrs;
481 }
483 /* Set ntpServers */
484 $this->attrs['gotoNtpServer'] = array();
485 if(!$this->inheritTimeServer){
486 foreach($this->gotoNtpServer as $server){
487 $this->attrs['gotoNtpServer'][] = $server;
488 }
489 }
491 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
492 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
493 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
494 }
496 /* Write back to ldap */
497 $ldap= $this->config->get_ldap_link();
498 if ($this->orig_dn == 'new'){
499 $ldap->cd($this->config->current['BASE']);
500 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
501 $ldap->cd($this->dn);
502 if (!count($this->attrs['gotoNtpServer'])){
503 unset($this->attrs['gotoNtpServer']);
504 }
505 $ldap->add($this->attrs);
506 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
507 } else {
508 $ldap->cd($this->dn);
509 $this->cleanup();
510 $ldap->modify ($this->attrs);
511 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
512 }
514 /* cn=default and macAddress=- indicates that this is a template */
515 if($this->cn == "default"){
516 $this->netConfigDNS->macAddress = "-";
517 }
519 $this->netConfigDNS->cn = $this->cn;
520 $this->netConfigDNS->save($this->dn);
521 show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
522 }
525 function set_everything_to_inherited()
526 {
527 /* Set workstation service attributes to inherited */
528 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
529 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
530 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseType") as $name){
531 $this->parent->by_object['termservice']->$name = "default";
532 }
533 }
535 /* Set workstation startup attributes to inherited */
536 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
537 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
538 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
539 }
540 }
542 }
544 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
545 ?>