51aa620bea0f7b5d36df4a0c146c1b30011d3c3b
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 var $mapActions = array("reboot" => "",
51 "instant_update" => "softupdate",
52 "update" => "sceduledupdate",
53 "reinstall" => "install",
54 "rescan" => "",
55 "memcheck" => "memcheck",
56 "sysinfo" => "sysinfo");
58 var $fai_activated = FALSE;
60 function termgeneric ($config, $dn= NULL, $parent= NULL)
61 {
62 /* Check if FAI is activated */
63 $tmp = search_config($config->data,"faiManagement","CLASS");
64 if(!empty($tmp)){
65 $this->fai_activated = TRUE;
66 }
68 plugin::plugin ($config, $dn, $parent);
69 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
70 /* Read arrays */
71 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
72 if (!isset($this->attrs[$val])){
73 continue;
74 }
75 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
76 array_push($this->$val, $this->attrs[$val][$i]);
77 }
78 }
80 /* Create used ntp server array */
81 $this->gotoNtpServer= array();
82 if(isset($this->attrs['gotoNtpServer'])){
83 $this->inheritTimeServer = false;
84 unset($this->attrs['gotoNtpServer']['count']);
85 foreach($this->attrs['gotoNtpServer'] as $server){
86 $this->gotoNtpServer[$server] = $server;
87 }
88 }
90 /* Set inherit checkbox state */
91 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
92 $this->inheritTimeServer = true;
93 $this->gotoNtpServer=array();
94 }
96 /* Create available ntp options */
97 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
98 foreach($this->gotoNtpServers as $key => $server){
99 if($server == "default"){
100 unset($this->gotoNtpServers[$key]);
101 }
102 }
104 $this->modes["disabled"]= _("disabled");
105 $this->modes["text"]= _("text");
106 $this->modes["graphic"]= _("graphic");
108 /* Set base */
109 if ($this->dn == "new"){
110 $ui= get_userinfo();
111 $this->base= dn2base($ui->dn);
112 } else {
113 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
114 }
116 /* Create an array of all Syslog servers */
117 $tmp = $this->config->data['SERVERS']['SYSLOG'];
118 foreach($tmp as $server){
119 $visible = $server;
120 if($server == "default") {
121 $visible = "["._("inherited")."]";
122 }
123 $this->gotoSyslogServers[$server] = $visible;
124 }
126 $this->orig_dn= $this->dn;
128 if($this->is_account){
129 @log::log("view","terminal/".get_class($this),$this->dn);
130 }
131 }
133 function set_acl_base($base)
134 {
135 plugin::set_acl_base($base);
136 $this->netConfigDNS->set_acl_base($base);
137 }
139 function set_acl_category($cat)
140 {
141 plugin::set_acl_category($cat);
142 $this->netConfigDNS->set_acl_category($cat);
143 }
145 function execute()
146 {
147 /* Call parent execute */
148 plugin::execute();
150 /* Do we need to flip is_account state? */
151 if (isset($_POST['modify_state'])){
152 $this->is_account= !$this->is_account;
153 }
155 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
157 /* Set FAIstate */
158 if($this->fai_activated && $this->dn != "new"){
159 $ldap = $this->config->get_ldap_link();
160 $ldap->cd($this->config->current['BASE']);
161 $ldap->cat($this->dn,array("objectClass"));
162 $res = $ldap->fetch();
164 $attrs = array();
165 $attrs['FAIstate'] = "";
166 if(isset($this->mapActions[$_POST['saction']])){
167 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
168 }
170 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
171 $attrs['objectClass'][] = $res['objectClass'][$i];
172 }
174 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
175 $attrs['objectClass'][] = "FAIobject";
176 }
178 if($attrs['FAIstate'] == ""){
179 #FIXME we should check if FAIobject is used anymore
180 $attrs['FAIstate'] = array();
181 }
183 $ldap->cd($this->dn);
184 $ldap->modify($attrs);
185 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
186 }
188 switch($_POST['saction']){
189 case 'wake':
190 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
191 if ($cmd == ""){
192 print_red(_("No WAKECMD definition found in your gosa.conf"));
193 } else {
194 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
195 if ($retval != 0){
196 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
197 }
198 }
199 break;
201 case 'reboot':
202 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
203 if ($cmd == ""){
204 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
205 } else {
206 exec ($cmd." ".$this->cn, $dummy, $retval);
207 if ($retval != 0){
208 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
209 }
210 }
211 break;
213 case 'halt':
214 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
215 if ($cmd == ""){
216 print_red(_("No HALTCMD definition found in your gosa.conf"));
217 } else {
218 exec ($cmd." ".$this->cn, $dummy, $retval);
219 if ($retval != 0){
220 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
221 }
222 }
223 break;
224 }
225 }
227 /* Base select dialog */
228 $once = true;
229 foreach($_POST as $name => $value){
230 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
231 $once = false;
232 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
233 $this->dialog->setCurrentBase($this->base);
234 }
235 }
237 /* Dialog handling */
238 if(is_object($this->dialog)){
239 /* Must be called before save_object */
240 $this->dialog->save_object();
242 if($this->dialog->isClosed()){
243 $this->dialog = false;
244 }elseif($this->dialog->isSelected()){
246 /* A new base was selected, check if it is a valid one */
247 $tmp = $this->get_allowed_bases();
248 if(isset($tmp[$this->dialog->isSelected()])){
249 $this->base = $this->dialog->isSelected();
250 }
251 $this->dialog= false;
252 }else{
253 return($this->dialog->execute());
254 }
255 }
257 /* Do we represent a valid terminal? */
258 if (!$this->is_account && $this->parent == NULL){
259 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
260 _("This 'dn' has no terminal features.")."</b>";
261 return($display);
262 }
264 /* Add new ntp Server to our list */
265 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
266 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
267 }
269 /* Delete selected NtpServer for list of used servers */
270 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
271 foreach($_POST['gotoNtpServerSelected'] as $name){
272 unset($this->gotoNtpServer[$name]);
273 }
274 }
276 /* Fill templating stuff */
277 $smarty= get_smarty();
279 $tmp = $this->plInfo();
280 foreach($tmp['plProvidedAcls'] as $name => $translation){
281 $smarty->assign($name."ACL",$this->getacl($name));
282 }
284 $smarty->assign("cn", $this->cn);
285 $smarty->assign("staticAddress", "");
287 $smarty->assign("bases", $this->get_allowed_bases());
289 /* tell smarty the inherit checkbox state */
290 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
292 /* Check if terminal is online */
293 $query= "fping -q -r 1 -t 500 ".$this->cn;
294 exec ($query, $dummy, $retval);
296 /* Offline */
297 if ($retval == 0){
298 $smarty->assign("actions", array( "halt" => _("Switch off"),
299 "reboot" => _("Reboot"),
300 "memcheck" => _("Memory test"),
301 "sysinfo" => _("System analysis")));
302 } else {
303 $smarty->assign("actions", array("wake" => _("Wake up"),
304 "memcheck" => _("Memory test"),
305 "sysinfo" => _("System analysis")));
306 }
308 /* Arrays */
309 $smarty->assign("modes", $this->modes);
311 $tmp2 = array();
312 foreach($this->config->data['SERVERS']['NFS'] as $server){
313 if($server != "default"){
314 $tmp2[$server]= $server;
315 }else{
316 $tmp2[$server]="["._("inherited")."]";
317 }
318 }
320 $smarty->assign("nfsservers", $tmp2);
321 $smarty->assign("syslogservers", $this->gotoSyslogServers);
323 $tmp = array();
324 foreach($this->gotoNtpServers as $server){
325 if(!in_array($server,$this->gotoNtpServer)){
326 $tmp[$server] = $server;
327 }
328 }
330 $smarty->assign("ntpservers", $tmp);
331 $smarty->assign("fai_activated",$this->fai_activated);
333 /* Variables */
334 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
335 $smarty->assign($val."_select", $this->$val);
336 }
338 /* Show main page */
339 $smarty->assign("netconfig", $this->netConfigDNS->execute());
340 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
341 }
343 function remove_from_parent()
344 {
345 if($this->acl_is_removeable()){
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);
353 @log::log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
355 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
357 /* Optionally execute a command after we're done */
358 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
360 /* Delete references to object groups */
361 $ldap->cd ($this->config->current['BASE']);
362 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
363 while ($ldap->fetch()){
364 $og= new ogroup($this->config, $ldap->getDN());
365 unset($og->member[$this->dn]);
366 $og->save ();
367 }
368 }
369 }
370 }
373 /* Save data to object */
374 function save_object()
375 {
376 /* Create a base backup and reset the
377 base directly after calling plugin::save_object();
378 Base will be set seperatly a few lines below */
379 $base_tmp = $this->base;
380 plugin::save_object();
381 $this->base = $base_tmp;
383 /* Set new base if allowed */
384 $tmp = $this->get_allowed_bases();
385 if(isset($_POST['base'])){
386 if(isset($tmp[$_POST['base']])){
387 $this->base= $_POST['base'];
388 }
389 }
391 $this->netConfigDNS->save_object();
393 /* Save terminal path to parent since it is used by termstartup, too */
394 if(isset($this->parent->by_object['termstartup'])){
395 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
396 }
398 if(isset($_POST['termgeneric_posted'])){
399 if(isset($_POST["inheritTimeServer"])){
400 $this->inheritTimeServer = true;
401 }else{
402 $this->inheritTimeServer = false;
403 }
404 }
405 }
408 /* Check supplied data */
409 function check()
410 {
411 /* Call common method to give check the hook */
412 $message= plugin::check();
414 /* Skip IP & Mac checks if this is a template */
415 if($this->cn != "default"){
416 $message= array_merge($message, $this->netConfigDNS->check());
417 }
419 /* Permissions for that base? */
420 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
422 if ($this->cn == ""){
423 $message[]= _("The required field 'Terminal name' is not set.");
424 }
426 if ($this->orig_dn == 'new'){
427 $ldap= $this->config->get_ldap_link();
428 $ldap->cd ($this->base);
430 /* It is possible to have a 'default' terminal on every base */
431 if($this->cn == "default"){
432 $ldap->cat($this->dn);
433 }else{
434 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
435 }
436 if ($ldap->count() != 0){
437 while ($attrs= $ldap->fetch()){
438 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
439 continue;
440 } else {
441 if ($attrs['dn'] != $this->orig_dn){
442 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
443 break;
444 }
445 }
446 }
447 }
448 }
450 /* Check for valid ntpServer selection */
451 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
452 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
453 }
455 return ($message);
456 }
459 /* Save to LDAP */
460 function save()
461 {
463 /* Move object if requested */
464 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
465 $this->move($this->orig_dn, $this->dn);
466 }
468 plugin::save();
470 /* Strip out 'default' values */
471 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
473 if ($this->attrs[$val] == "default"){
474 $this->attrs[$val]= array();
475 }
476 }
478 /* Add missing arrays */
479 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
480 if (isset ($this->$val) && count ($this->$val) != 0){
481 $this->attrs["$val"]= $this->$val;
482 }
483 }
485 /* Remove all empty values */
486 if ($this->orig_dn == 'new'){
487 $attrs= array();
488 foreach ($this->attrs as $key => $val){
489 if (is_array($val) && count($val) == 0){
490 continue;
491 }
492 $attrs[$key]= $val;
493 }
494 $this->attrs= $attrs;
495 }
497 /* Set ntpServers */
498 $this->attrs['gotoNtpServer'] = array();
499 if(!$this->inheritTimeServer){
500 foreach($this->gotoNtpServer as $server){
501 $this->attrs['gotoNtpServer'][] = $server;
502 }
503 }
505 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
506 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
507 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
508 }
510 /* Write back to ldap */
511 $ldap= $this->config->get_ldap_link();
512 if ($this->orig_dn == 'new'){
513 $ldap->cd($this->config->current['BASE']);
514 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
515 $ldap->cd($this->dn);
516 if (!count($this->attrs['gotoNtpServer'])){
517 unset($this->attrs['gotoNtpServer']);
518 }
519 $ldap->add($this->attrs);
520 @log::log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
521 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
522 } else {
523 $ldap->cd($this->dn);
524 $this->cleanup();
525 $ldap->modify ($this->attrs);
526 @log::log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
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(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
538 }
541 /* Return plugin informations for acl handling */
542 function plInfo()
543 {
544 return (array(
545 "plShortName" => _("Terminal"),
546 "plDescription" => _("Terminal generic"),
547 "plSelfModify" => FALSE,
548 "plDepends" => array(),
549 "plPriority" => 1,
550 "plSection" => array("administration"),
551 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
552 "objectClass" => "gotoTerminal")),
553 "plProvidedAcls"=> array(
554 "gotoMode" => _("Mode"),
555 "gotoTerminalPath" => _("Root server"),
556 "gotoSwapServer" => _("Swap server"),
557 "gotoSyslogServer" => _("Syslog server enabled"),
558 "gotoNtpServer" => _("Ntp server settings"),
559 "base" => _("Base"),
560 "cn" => _("Name"),
561 "gotoRootPasswd" => _("Root password"),
562 "FAIstate" => _("Action flag"))
563 ));
564 }
565 }
567 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
568 ?>