dcfce668c29693af556020d05854451cbff0c0ba
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 /* You can't inherit the NTP service, if we are not member in an object group */
102 if(!$this->member_of_ogroup){
103 $this->inheritTimeServer = FALSE;
104 }
106 /* Create available ntp options */
107 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
108 foreach($this->gotoNtpServers as $key => $server){
109 if($server == "default"){
110 unset($this->gotoNtpServers[$key]);
111 }
112 }
114 $this->modes["disabled"]= _("disabled");
115 $this->modes["text"]= _("text");
116 $this->modes["graphic"]= _("graphic");
118 /* Set base */
119 if ($this->dn == "new"){
120 $ui= get_userinfo();
121 $this->base= dn2base($ui->dn);
122 } else {
123 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
124 }
126 /* Create an array of all Syslog servers */
127 $tmp = $this->config->data['SERVERS']['SYSLOG'];
128 foreach($tmp as $server){
129 $visible = $server;
130 if($server == "default" && $this->member_of_ogroup) {
131 $visible = "["._("inherited")."]";
132 }
133 $this->gotoSyslogServers[$server] = $visible;
134 }
136 $this->orig_dn= $this->dn;
137 }
139 function execute()
140 {
141 /* Call parent execute */
142 plugin::execute();
144 $this->netConfigDNS->acl = $this->acl;
146 /* Do we need to flip is_account state? */
147 if (isset($_POST['modify_state'])){
148 $this->is_account= !$this->is_account;
149 }
151 if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") ==""){
153 if($this->fai_activated && $this->dn != "new"){
154 /* Set FAIstate */
155 $ldap = $this->config->get_ldap_link();
156 $ldap->cd($this->config->current['BASE']);
157 $ldap->cat($this->dn,array("objectClass"));
158 $res = $ldap->fetch();
160 $attrs = array();
161 $attrs['FAIstate'] = "";
162 if(isset($this->mapActions[$_POST['saction']])){
163 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
164 }
166 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
167 $attrs['objectClass'][] = $res['objectClass'][$i];
168 }
170 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
171 $attrs['objectClass'][] = "FAIobject";
172 }
174 if($attrs['FAIstate'] == ""){
175 #FIXME we should check if FAIobject is used anymore
176 $attrs['FAIstate'] = array();
177 }
179 $ldap->cd($this->dn);
180 $ldap->modify($attrs);
181 show_ldap_error($ldap->get_error());
182 }
184 switch($_POST['saction']){
185 case 'wake':
186 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
187 if ($cmd == ""){
188 print_red(_("No WAKECMD definition found in your gosa.conf"));
189 } else {
190 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
191 if ($retval != 0){
192 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
193 }
194 }
195 break;
197 case 'reboot':
198 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
199 if ($cmd == ""){
200 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
201 } else {
202 exec ($cmd." ".$this->cn, $dummy, $retval);
203 if ($retval != 0){
204 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
205 }
206 }
207 break;
209 case 'halt':
210 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
211 if ($cmd == ""){
212 print_red(_("No HALTCMD definition found in your gosa.conf"));
213 } else {
214 exec ($cmd." ".$this->cn, $dummy, $retval);
215 if ($retval != 0){
216 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
217 }
218 }
219 break;
220 }
221 }
223 /* Base select dialog */
224 $once = true;
225 foreach($_POST as $name => $value){
226 if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
227 $once = false;
228 $this->dialog = new baseSelectDialog($this->config);
229 $this->dialog->setCurrentBase($this->base);
230 }
231 }
233 /* Dialog handling */
234 if(is_object($this->dialog)){
235 /* Must be called before save_object */
236 $this->dialog->save_object();
238 if($this->dialog->isClosed()){
239 $this->dialog = false;
240 }elseif($this->dialog->isSelected()){
241 $this->base = $this->dialog->isSelected();
242 $this->dialog= false;
243 }else{
244 return($this->dialog->execute());
245 }
246 }
248 /* Do we represent a valid terminal? */
249 if (!$this->is_account && $this->parent == NULL){
250 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
251 _("This 'dn' has no terminal features.")."</b>";
252 return($display);
253 }
255 /* Add new ntp Server to our list */
256 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer")== ""){
257 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
258 }
260 /* Delete selected NtpServer for list of used servers */
261 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer")== ""){
262 foreach($_POST['gotoNtpServerSelected'] as $name){
263 unset($this->gotoNtpServer[$name]);
264 }
265 }
267 /* Fill templating stuff */
268 $smarty= get_smarty();
269 $smarty->assign("cn", $this->cn);
270 $smarty->assign("cnACL", chkacl($this->acl,"cn"));
271 $smarty->assign("staticAddress", "");
273 $smarty->assign("bases", $this->config->idepartments);
275 /* tell smarty the inherit checkbox state */
276 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
278 /* Check if terminal is online */
279 $query= "fping -q -r 1 -t 500 ".$this->cn;
280 exec ($query, $dummy, $retval);
282 /* Offline */
283 if ($retval == 0){
284 $smarty->assign("actions", array( "halt" => _("Switch off"),
285 "reboot" => _("Reboot"),
286 "memcheck" => _("Memory test"),
287 "sysinfo" => _("System analysis")));
288 } else {
289 $smarty->assign("actions", array("wake" => _("Wake up"),
290 "memcheck" => _("Memory test"),
291 "sysinfo" => _("System analysis")));
292 }
294 /* Arrays */
295 $smarty->assign("modes", $this->modes);
297 $tmp2 = array();
298 foreach($this->config->data['SERVERS']['NFS'] as $server){
299 if($server != "default"){
300 $tmp2[$server]= $server;
301 }else{
302 if($this->member_of_ogroup){
303 $tmp2[$server]="["._("inherited")."]";
304 }
305 }
306 }
308 $smarty->assign("nfsservers", $tmp2);
312 $smarty->assign("syslogservers", $this->gotoSyslogServers);
313 $smarty->assign("ntpservers", $this->gotoNtpServers);
315 /* Variables */
316 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
317 "gotoSyslogServer", "gotoNtpServer") as $val){
319 $smarty->assign($val."_select", $this->$val);
320 $smarty->assign($val."ACL", chkacl($this->acl, $val));
321 }
323 /* Show main page */
324 $this->netConfigDNS->cn= $this->cn;
325 $smarty->assign("netconfig", $this->netConfigDNS->execute());
326 $smarty->assign("fai_activated",$this->fai_activated);
327 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
329 /* Display sub dialog from network settings */
330 if($this->netConfigDNS->dialog){
331 $this->dialog = TRUE;
332 return($this->netConfigDNS->execute());
333 }else{
334 $this->dialog = FALSE;
335 }
337 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
339 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
340 }
342 function remove_from_parent()
343 {
345 $this->netConfigDNS->acl = $this->acl;
346 $ldap= $this->config->get_ldap_link();
347 $ldap->cd($this->dn);
348 $ldap->cat($this->dn, array('dn'));
349 if($ldap->count()){
350 $this->netConfigDNS->remove_from_parent();
351 $ldap->rmDir($this->dn);
352 show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
354 /* Optionally execute a command after we're done */
355 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
357 /* Delete references to object groups */
358 $ldap->cd ($this->config->current['BASE']);
359 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
360 while ($ldap->fetch()){
361 $og= new ogroup($this->config, $ldap->getDN());
362 unset($og->member[$this->dn]);
363 $og->save ();
364 }
365 }
366 }
369 /* Save data to object */
370 function save_object()
371 {
372 plugin::save_object();
373 $this->netConfigDNS->save_object();
375 /* Save base, since this is no LDAP attribute */
376 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
377 $this->base= $_POST['base'];
378 }
380 /* Save terminal path to parent since it is used by termstartup, too */
381 if(isset($this->parent->by_object['termstartup'])){
382 $this->parent->by_object['termstartup']->gotoTerminalPath=
383 $this->gotoTerminalPath;
384 }
386 if(isset($_POST['termgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
387 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
388 $this->inheritTimeServer = true;
389 }else{
390 $this->inheritTimeServer = false;
391 }
392 }
394 if(isset($_POST["inheritAll"])){
395 $this->set_everything_to_inherited();
396 }
398 }
401 /* Check supplied data */
402 function check()
403 {
404 /* Call common method to give check the hook */
405 $message= plugin::check();
407 if($this->cn != "default"){
408 $message= array_merge($message, $this->netConfigDNS->check());
409 }
411 /* Permissions for that base? */
412 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
414 $ui= get_userinfo();
415 $acl= get_permissions ($this->dn, $ui->subtreeACL);
416 $acl= get_module_permission($acl, "group", $this->dn);
417 if (chkacl($acl, "create") != ""){
418 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
419 }
421 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
422 $message[]= _("The required field 'Terminal name' is not set.");
423 }
425 if ($this->orig_dn == 'new'){
426 $ldap= $this->config->get_ldap_link();
427 $ldap->cd ($this->config->current['BASE']);
429 /* It is possible to have a 'default' terminal on every base */
430 if($this->cn == "default"){
431 $ldap->cat($this->dn);
432 }else{
433 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
434 }
436 /* Check if there are some other terminals found */
437 if ($ldap->count() != 0){
438 while ($attrs= $ldap->fetch()){
439 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
440 continue;
441 } else {
442 if ($attrs['dn'] != $this->orig_dn){
443 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
444 break;
445 }
446 }
447 }
448 }
449 }
451 /* Check for valid ntpServer selection */
452 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
453 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
454 }
456 return ($message);
457 }
460 /* Save to LDAP */
461 function save()
462 {
463 $this->netConfigDNS->acl = $this->acl;
465 /* Move object if requested */
466 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
467 $this->move($this->orig_dn, $this->dn);
468 }
470 plugin::save();
472 /* Strip out 'default' values */
473 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
475 if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
476 $this->attrs[$val]= array();
477 }
478 }
480 /* Add missing arrays */
481 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
482 if (isset ($this->$val) && count ($this->$val) != 0){
483 $this->attrs["$val"]= $this->$val;
484 }
485 }
487 /* Remove all empty values */
488 if ($this->orig_dn == 'new'){
489 $attrs= array();
490 foreach ($this->attrs as $key => $val){
491 if (is_array($val) && count($val) == 0){
492 continue;
493 }
494 $attrs[$key]= $val;
495 }
496 $this->attrs= $attrs;
497 }
499 /* Set ntpServers */
500 $this->attrs['gotoNtpServer'] = array();
501 if(!$this->inheritTimeServer){
502 foreach($this->gotoNtpServer as $server){
503 $this->attrs['gotoNtpServer'][] = $server;
504 }
505 }
507 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
508 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
509 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
510 }
512 /* Write back to ldap */
513 $ldap= $this->config->get_ldap_link();
514 if ($this->orig_dn == 'new'){
515 $ldap->cd($this->config->current['BASE']);
516 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
517 $ldap->cd($this->dn);
518 if (!count($this->attrs['gotoNtpServer'])){
519 unset($this->attrs['gotoNtpServer']);
520 }
521 $ldap->add($this->attrs);
522 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
523 } else {
524 $ldap->cd($this->dn);
525 $this->cleanup();
526 $ldap->modify ($this->attrs);
527 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
528 }
530 /* cn=default and macAddress=- indicates that this is a template */
531 if($this->cn == "default"){
532 $this->netConfigDNS->macAddress = "-";
533 }
535 $this->netConfigDNS->cn = $this->cn;
536 $this->netConfigDNS->save($this->dn);
537 show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
538 }
541 function set_everything_to_inherited()
542 {
543 $this->gotoTerminalPath = "default";
544 $this->gotoSwapServer = "default" ;
545 $this->gotoSyslogServer = "default";
546 $this->inheritTimeServer = TRUE;
548 /* Set workstation service attributes to inherited */
549 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
550 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
551 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
552 $this->parent->by_object['termservice']->$name = "default";
553 }
554 }
556 /* Set workstation startup attributes to inherited */
557 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
558 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
559 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
560 }
561 }
563 }
565 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
566 ?>