8f5b2fde36ddd6d039ea9d91067b3dbbf20ec047
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 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
331 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
332 }
334 function remove_from_parent()
335 {
337 $this->netConfigDNS->acl = $this->acl;
338 $ldap= $this->config->get_ldap_link();
339 $ldap->cd($this->dn);
340 $ldap->cat($this->dn, array('dn'));
341 if($ldap->count()){
342 $this->netConfigDNS->remove_from_parent();
343 $ldap->rmDir($this->dn);
344 show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
346 /* Optionally execute a command after we're done */
347 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
349 /* Delete references to object groups */
350 $ldap->cd ($this->config->current['BASE']);
351 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
352 while ($ldap->fetch()){
353 $og= new ogroup($this->config, $ldap->getDN());
354 unset($og->member[$this->dn]);
355 $og->save ();
356 }
357 }
358 }
361 /* Save data to object */
362 function save_object()
363 {
364 plugin::save_object();
365 $this->netConfigDNS->save_object();
367 /* Save base, since this is no LDAP attribute */
368 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
369 $this->base= $_POST['base'];
370 }
372 /* Save terminal path to parent since it is used by termstartup, too */
373 if(isset($this->parent->by_object['termstartup'])){
374 $this->parent->by_object['termstartup']->gotoTerminalPath=
375 $this->gotoTerminalPath;
376 }
378 if(isset($_POST['termgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
379 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
380 $this->inheritTimeServer = true;
381 }else{
382 $this->inheritTimeServer = false;
383 }
384 }
386 if(isset($_POST["inheritAll"])){
387 $this->set_everything_to_inherited();
388 }
390 }
393 /* Check supplied data */
394 function check()
395 {
396 /* Call common method to give check the hook */
397 $message= plugin::check();
399 if($this->cn != "default"){
400 $message= array_merge($message, $this->netConfigDNS->check());
401 }
403 /* Permissions for that base? */
404 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
406 $ui= get_userinfo();
407 $acl= get_permissions ($this->dn, $ui->subtreeACL);
408 $acl= get_module_permission($acl, "group", $this->dn);
409 if (chkacl($acl, "create") != ""){
410 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
411 }
413 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
414 $message[]= _("The required field 'Terminal name' is not set.");
415 }
417 if ($this->orig_dn == 'new'){
418 $ldap= $this->config->get_ldap_link();
419 $ldap->cd ($this->config->current['BASE']);
421 /* It is possible to have a 'default' terminal on every base */
422 if($this->cn == "default"){
423 $ldap->cat($this->dn);
424 }else{
425 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
426 }
428 /* Check if there are some other terminals found */
429 if ($ldap->count() != 0){
430 while ($attrs= $ldap->fetch()){
431 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
432 continue;
433 } else {
434 if ($attrs['dn'] != $this->orig_dn){
435 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
436 break;
437 }
438 }
439 }
440 }
441 }
443 /* Check for valid ntpServer selection */
444 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
445 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
446 }
448 return ($message);
449 }
452 /* Save to LDAP */
453 function save()
454 {
455 $this->netConfigDNS->acl = $this->acl;
457 /* Move object if requested */
458 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
459 $this->move($this->orig_dn, $this->dn);
460 }
462 plugin::save();
464 /* Strip out 'default' values */
465 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
467 if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
468 $this->attrs[$val]= array();
469 }
470 }
472 /* Add missing arrays */
473 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
474 if (isset ($this->$val) && count ($this->$val) != 0){
475 $this->attrs["$val"]= $this->$val;
476 }
477 }
479 /* Remove all empty values */
480 if ($this->orig_dn == 'new'){
481 $attrs= array();
482 foreach ($this->attrs as $key => $val){
483 if (is_array($val) && count($val) == 0){
484 continue;
485 }
486 $attrs[$key]= $val;
487 }
488 $this->attrs= $attrs;
489 }
491 /* Set ntpServers */
492 $this->attrs['gotoNtpServer'] = array();
493 if(!$this->inheritTimeServer){
494 foreach($this->gotoNtpServer as $server){
495 $this->attrs['gotoNtpServer'][] = $server;
496 }
497 }
499 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
500 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
501 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
502 }
504 /* Write back to ldap */
505 $ldap= $this->config->get_ldap_link();
506 if ($this->orig_dn == 'new'){
507 $ldap->cd($this->config->current['BASE']);
508 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
509 $ldap->cd($this->dn);
510 if (!count($this->attrs['gotoNtpServer'])){
511 unset($this->attrs['gotoNtpServer']);
512 }
513 $ldap->add($this->attrs);
514 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
515 } else {
516 $ldap->cd($this->dn);
517 $this->cleanup();
518 $ldap->modify ($this->attrs);
519 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
520 }
522 /* cn=default and macAddress=- indicates that this is a template */
523 if($this->cn == "default"){
524 $this->netConfigDNS->macAddress = "-";
525 }
527 $this->netConfigDNS->cn = $this->cn;
528 $this->netConfigDNS->save($this->dn);
529 show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
530 }
533 function set_everything_to_inherited()
534 {
535 $this->gotoTerminalPath = "default";
536 $this->gotoSwapServer = "default" ;
537 $this->gotoSyslogServer = "default";
538 $this->inheritTimeServer = TRUE;
540 /* Set workstation service attributes to inherited */
541 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
542 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
543 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseType") as $name){
544 $this->parent->by_object['termservice']->$name = "default";
545 }
546 }
548 /* Set workstation startup attributes to inherited */
549 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
550 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
551 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
552 }
553 }
555 }
557 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
558 ?>