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 /* Save base, since this is no LDAP attribute */
361 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
362 $this->base= $_POST['base'];
363 }
365 /* Save terminal path to parent since it is used by termstartup, too */
366 if(isset($this->parent->by_object['termstartup'])){
367 $this->parent->by_object['termstartup']->gotoTerminalPath=
368 $this->gotoTerminalPath;
369 }
371 if(isset($_POST['termgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
372 if(isset($_POST["inheritTimeServer"])){
373 $this->inheritTimeServer = true;
374 }else{
375 $this->inheritTimeServer = false;
376 }
377 }
379 if(isset($_POST["inheritAll"])){
380 $this->set_everything_to_inherited();
381 }
383 }
386 /* Check supplied data */
387 function check()
388 {
389 /* Call common method to give check the hook */
390 $message= plugin::check();
392 if($this->cn != "default"){
393 $message= array_merge($message, $this->netConfigDNS->check());
394 }
396 /* Permissions for that base? */
397 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
399 $ui= get_userinfo();
400 $acl= get_permissions ($this->dn, $ui->subtreeACL);
401 $acl= get_module_permission($acl, "group", $this->dn);
402 if (chkacl($acl, "create") != ""){
403 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
404 }
406 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
407 $message[]= _("The required field 'Terminal name' is not set.");
408 }
410 if ($this->orig_dn == 'new'){
411 $ldap= $this->config->get_ldap_link();
412 $ldap->cd ($this->config->current['BASE']);
414 /* It is possible to have a 'default' terminal on every base */
415 if($this->cn == "default"){
416 $ldap->cat($this->dn);
417 }else{
418 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
419 }
421 /* Check if there are some other terminals found */
422 if ($ldap->count() != 0){
423 while ($attrs= $ldap->fetch()){
424 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
425 continue;
426 } else {
427 if ($attrs['dn'] != $this->orig_dn){
428 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
429 break;
430 }
431 }
432 }
433 }
434 }
436 /* Check for valid ntpServer selection */
437 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
438 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
439 }
441 return ($message);
442 }
445 /* Save to LDAP */
446 function save()
447 {
448 $this->netConfigDNS->acl = $this->acl;
450 /* Move object if requested */
451 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
452 $this->move($this->orig_dn, $this->dn);
453 }
455 plugin::save();
457 /* Strip out 'default' values */
458 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
460 if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
461 $this->attrs[$val]= array();
462 }
463 }
465 /* Add missing arrays */
466 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
467 if (isset ($this->$val) && count ($this->$val) != 0){
468 $this->attrs["$val"]= $this->$val;
469 }
470 }
472 /* Remove all empty values */
473 if ($this->orig_dn == 'new'){
474 $attrs= array();
475 foreach ($this->attrs as $key => $val){
476 if (is_array($val) && count($val) == 0){
477 continue;
478 }
479 $attrs[$key]= $val;
480 }
481 $this->attrs= $attrs;
482 }
484 /* Set ntpServers */
485 $this->attrs['gotoNtpServer'] = array();
486 if(!$this->inheritTimeServer){
487 foreach($this->gotoNtpServer as $server){
488 $this->attrs['gotoNtpServer'][] = $server;
489 }
490 }
492 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
493 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
494 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
495 }
497 /* Write back to ldap */
498 $ldap= $this->config->get_ldap_link();
499 if ($this->orig_dn == 'new'){
500 $ldap->cd($this->config->current['BASE']);
501 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
502 $ldap->cd($this->dn);
503 if (!count($this->attrs['gotoNtpServer'])){
504 unset($this->attrs['gotoNtpServer']);
505 }
506 $ldap->add($this->attrs);
507 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
508 } else {
509 $ldap->cd($this->dn);
510 $this->cleanup();
511 $ldap->modify ($this->attrs);
512 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
513 }
515 /* cn=default and macAddress=- indicates that this is a template */
516 if($this->cn == "default"){
517 $this->netConfigDNS->macAddress = "-";
518 }
520 $this->netConfigDNS->cn = $this->cn;
521 $this->netConfigDNS->save($this->dn);
522 show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
523 }
526 function set_everything_to_inherited()
527 {
528 $this->gotoTerminalPath = "default";
529 $this->gotoSwapServer = "default" ;
530 $this->gotoSyslogServer = "default";
531 $this->inheritTimeServer = TRUE;
533 /* Set workstation service attributes to inherited */
534 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
535 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
536 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseType") as $name){
537 $this->parent->by_object['termservice']->$name = "default";
538 }
539 }
541 /* Set workstation startup attributes to inherited */
542 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
543 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
544 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
545 }
546 }
548 }
550 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
551 ?>