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;
127 }
129 function set_acl_base($base)
130 {
131 plugin::set_acl_base($base);
132 $this->netConfigDNS->set_acl_base($base);
133 }
135 function set_acl_category($cat)
136 {
137 plugin::set_acl_category($cat);
138 $this->netConfigDNS->set_acl_category($cat);
139 }
141 function execute()
142 {
143 /* Call parent execute */
144 plugin::execute();
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']) && $this->acl_is_writeable("FAIstate")){
153 /* Set FAIstate */
154 if($this->fai_activated && $this->dn != "new"){
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(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
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 && $this->acl_is_moveable()){
227 $once = false;
228 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
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()){
242 /* A new base was selected, check if it is a valid one */
243 $tmp = $this->get_allowed_bases();
244 if(isset($tmp[$this->dialog->isSelected()])){
245 $this->base = $this->dialog->isSelected();
246 }
247 $this->dialog= false;
248 }else{
249 return($this->dialog->execute());
250 }
251 }
253 /* Do we represent a valid terminal? */
254 if (!$this->is_account && $this->parent == NULL){
255 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
256 _("This 'dn' has no terminal features.")."</b>";
257 return($display);
258 }
260 /* Add new ntp Server to our list */
261 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
262 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
263 }
265 /* Delete selected NtpServer for list of used servers */
266 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
267 foreach($_POST['gotoNtpServerSelected'] as $name){
268 unset($this->gotoNtpServer[$name]);
269 }
270 }
272 /* Fill templating stuff */
273 $smarty= get_smarty();
275 $tmp = $this->plInfo();
276 foreach($tmp['plProvidedAcls'] as $name => $translation){
277 $smarty->assign($name."ACL",$this->getacl($name));
278 }
280 $smarty->assign("cn", $this->cn);
281 $smarty->assign("staticAddress", "");
283 $smarty->assign("bases", $this->get_allowed_bases());
285 /* tell smarty the inherit checkbox state */
286 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
288 /* Check if terminal is online */
289 $query= "fping -q -r 1 -t 500 ".$this->cn;
290 exec ($query, $dummy, $retval);
292 /* Offline */
293 if ($retval == 0){
294 $smarty->assign("actions", array( "halt" => _("Switch off"),
295 "reboot" => _("Reboot"),
296 "memcheck" => _("Memory test"),
297 "sysinfo" => _("System analysis")));
298 } else {
299 $smarty->assign("actions", array("wake" => _("Wake up"),
300 "memcheck" => _("Memory test"),
301 "sysinfo" => _("System analysis")));
302 }
304 /* Arrays */
305 $smarty->assign("modes", $this->modes);
307 $tmp2 = array();
308 foreach($this->config->data['SERVERS']['NFS'] as $server){
309 if($server != "default"){
310 $tmp2[$server]= $server;
311 }else{
312 $tmp2[$server]="["._("inherited")."]";
313 }
314 }
316 $smarty->assign("nfsservers", $tmp2);
317 $smarty->assign("syslogservers", $this->gotoSyslogServers);
319 $tmp = array();
320 foreach($this->gotoNtpServers as $server){
321 if(!in_array($server,$this->gotoNtpServer)){
322 $tmp[$server] = $server;
323 }
324 }
326 $smarty->assign("ntpservers", $tmp);
327 $smarty->assign("fai_activated",$this->fai_activated);
329 /* Variables */
330 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
331 $smarty->assign($val."_select", $this->$val);
332 }
334 /* Show main page */
335 $smarty->assign("netconfig", $this->netConfigDNS->execute());
336 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
337 }
339 function remove_from_parent()
340 {
341 if($this->acl_is_removeable()){
342 $ldap= $this->config->get_ldap_link();
343 $ldap->cd($this->dn);
344 $ldap->cat($this->dn, array('dn'));
345 if($ldap->count()){
346 $this->netConfigDNS->remove_from_parent();
347 $ldap->rmDir($this->dn);
348 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
350 /* Optionally execute a command after we're done */
351 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
353 /* Delete references to object groups */
354 $ldap->cd ($this->config->current['BASE']);
355 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
356 while ($ldap->fetch()){
357 $og= new ogroup($this->config, $ldap->getDN());
358 unset($og->member[$this->dn]);
359 $og->save ();
360 }
361 }
362 }
363 }
366 /* Save data to object */
367 function save_object()
368 {
369 /* Create a base backup and reset the
370 base directly after calling plugin::save_object();
371 Base will be set seperatly a few lines below */
372 $base_tmp = $this->base;
373 plugin::save_object();
374 $this->base = $base_tmp;
376 /* Set new base if allowed */
377 $tmp = $this->get_allowed_bases();
378 if(isset($_POST['base'])){
379 if(isset($tmp[$_POST['base']])){
380 $this->base= $_POST['base'];
381 }
382 }
384 $this->netConfigDNS->save_object();
386 /* Save terminal path to parent since it is used by termstartup, too */
387 $this->parent->by_object['termstartup']->gotoTerminalPath=
388 $this->gotoTerminalPath;
390 if(isset($_POST['termgeneric_posted'])){
391 if(isset($_POST["inheritTimeServer"])){
392 $this->inheritTimeServer = true;
393 }else{
394 $this->inheritTimeServer = false;
395 }
396 }
397 }
400 /* Check supplied data */
401 function check()
402 {
403 /* Call common method to give check the hook */
404 $message= plugin::check();
406 /* Skip IP & Mac checks if this is a template */
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 if ($this->cn == ""){
415 $message[]= _("The required field 'Terminal name' is not set.");
416 }
418 if ($this->orig_dn == 'new'){
419 $ldap= $this->config->get_ldap_link();
420 $ldap->cd ($this->base);
422 /* It is possible to have a 'default' terminal on every base */
423 if($this->cn == "default"){
424 $ldap->cat($this->dn);
425 }else{
426 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
427 }
428 if ($ldap->count() != 0){
429 while ($attrs= $ldap->fetch()){
430 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
431 continue;
432 } else {
433 if ($attrs['dn'] != $this->orig_dn){
434 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
435 break;
436 }
437 }
438 }
439 }
440 }
442 /* Check for valid ntpServer selection */
443 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
444 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
445 }
447 return ($message);
448 }
451 /* Save to LDAP */
452 function save()
453 {
455 /* Move object if requested */
456 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
457 $this->move($this->orig_dn, $this->dn);
458 }
460 plugin::save();
462 /* Strip out 'default' values */
463 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
465 if ($this->attrs[$val] == "default"){
466 $this->attrs[$val]= array();
467 }
468 }
470 /* Add missing arrays */
471 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
472 if (isset ($this->$val) && count ($this->$val) != 0){
473 $this->attrs["$val"]= $this->$val;
474 }
475 }
477 /* Remove all empty values */
478 if ($this->orig_dn == 'new'){
479 $attrs= array();
480 foreach ($this->attrs as $key => $val){
481 if (is_array($val) && count($val) == 0){
482 continue;
483 }
484 $attrs[$key]= $val;
485 }
486 $this->attrs= $attrs;
487 }
489 /* Set ntpServers */
490 $this->attrs['gotoNtpServer'] = array();
491 if(!$this->inheritTimeServer){
492 foreach($this->gotoNtpServer as $server){
493 $this->attrs['gotoNtpServer'][] = $server;
494 }
495 }
497 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
498 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
499 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
500 }
502 /* Write back to ldap */
503 $ldap= $this->config->get_ldap_link();
504 if ($this->orig_dn == 'new'){
505 $ldap->cd($this->config->current['BASE']);
506 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
507 $ldap->cd($this->dn);
508 if (!count($this->attrs['gotoNtpServer'])){
509 unset($this->attrs['gotoNtpServer']);
510 }
511 $ldap->add($this->attrs);
512 $this->handle_post_events("add");
513 } else {
514 $ldap->cd($this->dn);
515 $this->cleanup();
516 $ldap->modify ($this->attrs);
518 $this->handle_post_events("modify");
519 }
521 /* cn=default and macAddress=- indicates that this is a template */
522 if($this->cn == "default"){
523 $this->netConfigDNS->macAddress = "-";
524 }
526 $this->netConfigDNS->cn = $this->cn;
527 $this->netConfigDNS->save($this->dn);
528 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
530 /* Optionally execute a command after we're done */
531 $this->postcreate();
532 }
535 /* Return plugin informations for acl handling */
536 function plInfo()
537 {
538 return (array(
539 "plShortName" => _("Terminal"),
540 "plDescription" => _("Terminal generic"),
541 "plSelfModify" => FALSE,
542 "plDepends" => array(),
543 "plPriority" => 1,
544 "plSection" => array("administration"),
545 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
546 "objectClass" => "gotoTerminal")),
547 "plProvidedAcls"=> array(
548 "gotoMode" => _("Mode"),
549 "gotoTerminalPath" => _("Root server"),
550 "gotoSwapServer" => _("Swap server"),
551 "gotoSyslogServer" => _("Syslog server enabled"),
552 "gotoNtpServer" => _("Ntp server settings"),
553 "base" => _("Base"),
554 "cn" => _("Name"),
555 "gotoRootPasswd" => _("Root password"),
556 "FAIstate" => _("Action flag"))
557 ));
558 }
559 }
561 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
562 ?>