Code

797186de2f41404593392d196c1d570f967cd3a5
[gosa.git] / plugins / admin / systems / class_terminalGeneric.inc
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;
59   var $view_logged = FALSE;
61   var $member_of_ogroup = FALSE;
63   function termgeneric ($config, $dn= NULL, $parent= NULL)
64   {
65     /* Check if FAI is activated */
66     $tmp = search_config($config->data,"faiManagement","CLASS");
67     if(!empty($tmp)){
68       $this->fai_activated = TRUE;
69     }
71     plugin::plugin ($config, $dn, $parent);
73     if(!isset($this->parent->by_object['ogroup'])){
74       $ldap = $this->config->get_ldap_link();
75       $ldap->cd ($this->config->current['BASE']);
76       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))",array("cn"));
77       $this->member_of_ogroup = $ldap->count() >= 1;
78     }
80     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
81     /* Read arrays */
82     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
83       if (!isset($this->attrs[$val])){
84         continue;
85       }
86       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
87         array_push($this->$val, $this->attrs[$val][$i]);
88       }
89     }
91     /* Create used ntp server array */
92     $this->gotoNtpServer= array();
93     if(isset($this->attrs['gotoNtpServer'])){
94       $this->inheritTimeServer = false;
95       unset($this->attrs['gotoNtpServer']['count']);
96       foreach($this->attrs['gotoNtpServer'] as $server){
97         $this->gotoNtpServer[$server] = $server;
98       }
99     }
101     /* Set inherit checkbox state */
102     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
103       $this->inheritTimeServer = true;
104       $this->gotoNtpServer=array();
105     }
107     /* Create available ntp options */
108     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
109     foreach($this->gotoNtpServers as $key => $server){
110       if($server == "default"){
111         unset($this->gotoNtpServers[$key]);
112       }
113     }
115     $this->modes["disabled"]= _("disabled");
116     $this->modes["text"]= _("text");
117     $this->modes["graphic"]= _("graphic");
119     /* Set base */
120     if ($this->dn == "new"){
121       $ui= get_userinfo();
122       $this->base= dn2base($ui->dn);
123     } else {
124       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
125     }
127     /* Create an array of all Syslog servers */
128     $tmp = $this->config->data['SERVERS']['SYSLOG'];
129     foreach($tmp as $server){
130       $visible = $server;
131       if($server == "default") {
132         $visible = "["._("inherited")."]";
133       }
134       $this->gotoSyslogServers[$server] = $visible;
135     }
137     $this->orig_dn= $this->dn;
138   }
140   function set_acl_base($base)
141   {
142     plugin::set_acl_base($base);
143     $this->netConfigDNS->set_acl_base($base);
144   }
146   function set_acl_category($cat)
147   {
148     plugin::set_acl_category($cat);
149     $this->netConfigDNS->set_acl_category($cat);
150   }
152   function execute()
153   {
154     /* Call parent execute */
155     plugin::execute();
157     if($this->is_account && !$this->view_logged){
158       $this->view_logged = TRUE;
159       new log("view","terminal/".get_class($this),$this->dn);
160     }
162     /* Do we need to flip is_account state? */
163     if (isset($_POST['modify_state'])){
164       $this->is_account= !$this->is_account;
165     }
167     if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
169       /* Set FAIstate */
170       if($this->fai_activated && $this->dn != "new"){
171         $ldap = $this->config->get_ldap_link();
172         $ldap->cd($this->config->current['BASE']);
173         $ldap->cat($this->dn,array("objectClass"));
174         $res = $ldap->fetch();
176         $attrs = array();
177         $attrs['FAIstate'] = "";
178         if(isset($this->mapActions[$_POST['saction']])){
179           $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
180         }
182         for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
183           $attrs['objectClass'][] = $res['objectClass'][$i];
184         }
186         if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
187           $attrs['objectClass'][] = "FAIobject";
188         }
190         if($attrs['FAIstate'] == ""){
191 #FIXME we should check if FAIobject is used anymore
192           $attrs['FAIstate'] = array();
193         }
195         $ldap->cd($this->dn);
196         $ldap->modify($attrs);
197         show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
198       }
200       switch($_POST['saction']){
201         case 'wake':
202           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
203           if ($cmd == ""){
204             print_red(_("No WAKECMD definition found in your gosa.conf"));
205           } else {
206             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
207             if ($retval != 0){
208               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
209             }
210           }
211           break;
213         case 'reboot':
214           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
215           if ($cmd == ""){
216             print_red(_("No REBOOTCMD 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;
225         case 'halt':
226           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
227           if ($cmd == ""){
228             print_red(_("No HALTCMD definition found in your gosa.conf"));
229           } else {
230             exec ($cmd." ".$this->cn, $dummy, $retval);
231             if ($retval != 0){
232               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
233             }
234           }
235           break;
236       }
237     }
239     /* Base select dialog */
240     $once = true;
241     foreach($_POST as $name => $value){
242       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
243         $once = false;
244         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
245         $this->dialog->setCurrentBase($this->base);
246       }
247     }
249     /* Dialog handling */
250     if(is_object($this->dialog)){
251       /* Must be called before save_object */
252       $this->dialog->save_object();
254       if($this->dialog->isClosed()){
255         $this->dialog = false;
256       }elseif($this->dialog->isSelected()){
258         /* A new base was selected, check if it is a valid one */
259         $tmp = $this->get_allowed_bases();
260         if(isset($tmp[$this->dialog->isSelected()])){
261           $this->base = $this->dialog->isSelected();
262         }
263         $this->dialog= false;
264       }else{
265         return($this->dialog->execute());
266       }
267     }
269     /* Do we represent a valid terminal? */
270     if (!$this->is_account && $this->parent == NULL){
271       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
272         _("This 'dn' has no terminal features.")."</b>";
273       return($display);
274     }
276     /* Add new ntp Server to our list */
277     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
278       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
279     }
281     /* Delete selected NtpServer for list of used servers  */
282     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
283       foreach($_POST['gotoNtpServerSelected'] as $name){
284         unset($this->gotoNtpServer[$name]);
285       } 
286     }
288     /* Fill templating stuff */
289     $smarty= get_smarty();
290     
291     $tmp = $this->plInfo();
292     foreach($tmp['plProvidedAcls'] as $name => $translation){
293       $smarty->assign($name."ACL",$this->getacl($name));
294     }
296     $smarty->assign("cn", $this->cn);
297     $smarty->assign("staticAddress", "");
299     $smarty->assign("bases", $this->get_allowed_bases());
301     /* tell smarty the inherit checkbox state */
302     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
304     /* Check if terminal is online */
305     $query= "fping -q -r 1 -t 500 ".$this->cn;
306     exec ($query, $dummy, $retval);
308     /* Offline */
309     if ($retval == 0){
310       $smarty->assign("actions", array( "halt" => _("Switch off"), 
311                                         "reboot" => _("Reboot"),
312                                         "memcheck" => _("Memory test"),
313                                         "sysinfo"  => _("System analysis")));
314     } else {
315       $smarty->assign("actions", array("wake" => _("Wake up"),
316                                        "memcheck" => _("Memory test"),
317                                        "sysinfo"  => _("System analysis")));
318     }
320     /* Arrays */
321     $smarty->assign("modes", $this->modes);
323     $tmp2 = array(); 
324     foreach($this->config->data['SERVERS']['NFS'] as $server){
325       if($server != "default"){
326         $tmp2[$server]= $server;
327       }else{
328         $tmp2[$server]="["._("inherited")."]";
329       }
330     }
331   
332     $smarty->assign("nfsservers",     $tmp2);
333     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
335     $tmp = array();
336     foreach($this->gotoNtpServers as $server){
337       if(!in_array($server,$this->gotoNtpServer)){
338         $tmp[$server] = $server;
339       }
340     }
341     
342     $smarty->assign("ntpservers",     $tmp);
343     $smarty->assign("fai_activated",$this->fai_activated);
345     /* Variables */
346     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
347       $smarty->assign($val."_select", $this->$val);
348     }
350     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
352     /* Show main page */
353     $smarty->assign("netconfig", $this->netConfigDNS->execute());
354     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
355   }
357   function remove_from_parent()
358   {
359     if($this->acl_is_removeable()){   
360       $ldap= $this->config->get_ldap_link();
361       $ldap->cd($this->dn);
362       $ldap->cat($this->dn, array('dn'));
363       if($ldap->count()){
364         $this->netConfigDNS->remove_from_parent();
365         $ldap->rmDir($this->dn);
366   
367         new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
368   
369         show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
371         /* Optionally execute a command after we're done */
372         $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
374         /* Delete references to object groups */
375         $ldap->cd ($this->config->current['BASE']);
376         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
377         while ($ldap->fetch()){
378           $og= new ogroup($this->config, $ldap->getDN());
379           unset($og->member[$this->dn]);
380           $og->save ();
381         }
382       }
383     }
384   }
387   /* Save data to object */
388   function save_object()
389   {
390     /* Create a base backup and reset the
391        base directly after calling plugin::save_object();
392        Base will be set seperatly a few lines below */
393     $base_tmp = $this->base;
394     plugin::save_object();
395     $this->base = $base_tmp;
397     /* Set new base if allowed */
398     $tmp = $this->get_allowed_bases();
399     if(isset($_POST['base'])){
400       if(isset($tmp[$_POST['base']])){
401         $this->base= $_POST['base'];
402       }
403     }
404     
405     $this->netConfigDNS->save_object();
407     /* Save terminal path to parent since it is used by termstartup, too */
408     if(isset($this->parent->by_object['termstartup'])){
409       $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
410     }
411     
412     if(isset($_POST['termgeneric_posted'])){
413       if(isset($_POST["inheritTimeServer"])){
414         $this->inheritTimeServer = true;
415       }else{
416         $this->inheritTimeServer = false;
417       }
418     }  
420     if(isset($_POST["inheritAll"])){
421       $this->set_everything_to_inherited();
422     }
424   }
427   /* Check supplied data */
428   function check()
429   {
430     /* Call common method to give check the hook */
431     $message= plugin::check();
433     /* Skip IP & Mac checks if this is a template */
434     if($this->cn != "default"){
435       $message= array_merge($message, $this->netConfigDNS->check());
436     }
438     /* Permissions for that base? */
439     $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
441     if ($this->cn == ""){
442       $message[]= _("The required field 'Terminal name' is not set.");
443     }
445     if ($this->orig_dn == 'new'){
446       $ldap= $this->config->get_ldap_link();
447       $ldap->cd ($this->base);
449       /* It is possible to have a 'default' terminal on every base */
450       if($this->cn == "default"){
451         $ldap->cat($this->dn);
452       }else{
453         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
454       }
455       if ($ldap->count() != 0){
456         while ($attrs= $ldap->fetch()){
457           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
458             continue;
459           } else {
460             if ($attrs['dn'] != $this->orig_dn){
461               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
462               break;
463             }
464           }
465         }
466       }
467     }
469     /* Check for valid ntpServer selection */
470     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
471       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
472     }
474     return ($message);
475   }
478   /* Save to LDAP */
479   function save()
480   {
482     /* Move object if requested */
483     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
484       $this->move($this->orig_dn, $this->dn);
485     }
487     plugin::save();
489     /* Strip out 'default' values */
490     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
491       if(isset($this->attrs[$val])){
492         if ($this->attrs[$val] == "default"){
493           $this->attrs[$val]= array();
494         }
495       }
496     }
498     /* Add missing arrays */
499     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
500       if (isset ($this->$val) && count ($this->$val) != 0){
501         $this->attrs["$val"]= $this->$val;
502       }
503     }
505     /* Remove all empty values */
506     if ($this->orig_dn == 'new'){
507       $attrs= array();
508       foreach ($this->attrs as $key => $val){
509         if (is_array($val) && count($val) == 0){
510           continue;
511         }
512         $attrs[$key]= $val;
513       }
514       $this->attrs= $attrs;
515     }
517     /* Set ntpServers */
518     $this->attrs['gotoNtpServer'] = array();
519     if(!$this->inheritTimeServer){
520       foreach($this->gotoNtpServer as $server){
521         $this->attrs['gotoNtpServer'][] = $server;
522       }
523     }
525     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
526     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
527       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
528     }
530     /* Write back to ldap */
531     $ldap= $this->config->get_ldap_link();
532     if ($this->orig_dn == 'new'){
533       $ldap->cd($this->config->current['BASE']);
534       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
535       $ldap->cd($this->dn);
536       if (!count($this->attrs['gotoNtpServer'])){
537         unset($this->attrs['gotoNtpServer']);
538       }
539       $ldap->add($this->attrs);
540       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
541       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
542     } else {
543       $ldap->cd($this->dn);
544       $this->cleanup();
545       $ldap->modify ($this->attrs); 
546       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
547       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
548     }
549     
550     /* cn=default and macAddress=- indicates that this is a template */
551     if($this->cn == "default"){
552       $this->netConfigDNS->macAddress = "-";
553     }
555     $this->netConfigDNS->cn = $this->cn;
556     $this->netConfigDNS->save($this->dn);
557     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
558   }
564   /* Display generic part for server copy & paste */
565   function getCopyDialog()
566   {
567     $vars = array("cn");
568     $smarty = get_smarty();
569     $smarty->assign("cn" ,$this->cn);
570     $smarty->assign("object","terminal");
571     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
572     $ret = array();
573     $ret['string'] = $str;
574     $ret['status'] = "";
575     return($ret);
576   }
579   function saveCopyDialog()
580   {
581     if(isset($_POST['cn'])){
582       $this->cn = $_POST['cn'];
583     }
584   }
587   function PrepareForCopyPaste($source)
588   {
589     plugin::PrepareForCopyPaste($source);
590     if(isset($source['macAddress'][0])){
591       $this->netConfigDNS->macAddress = $source['macAddress'][0];
592     }
593     if(isset($source['ipHostNumber'][0])){
594       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
595     }
597     /* Create used ntp server array */
598     $this->gotoNtpServer= array();
599     if(isset($source['gotoNtpServer'])){
600       $this->inheritTimeServer = false;
601       unset($source['gotoNtpServer']['count']);
602       foreach($source['gotoNtpServer'] as $server){
603         $this->gotoNtpServer[$server] = $server;
604       }
605     }
607     /* Set inherit checkbox state */
608     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
609       $this->inheritTimeServer = true;
610       $this->gotoNtpServer=array();
611     }
612   }
615   /* Return plugin informations for acl handling */
616   function plInfo()
617   {
618     return (array(
619           "plShortName"   => _("Terminal"),
620           "plDescription" => _("Terminal generic"),
621           "plSelfModify"  => FALSE,
622           "plDepends"     => array(),
623           "plPriority"    => 1,
624           "plSection"     => array("administration"),
625           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
626                                                         "objectClass"  => "gotoTerminal")),
627           "plProvidedAcls"=> array(
628             "gotoMode"            => _("Mode"),
629             "gotoTerminalPath"    => _("Root server"),
630             "gotoSwapServer"      => _("Swap server"),
631             "gotoSyslogServer"    => _("Syslog server enabled"),
632             "gotoNtpServer"       => _("Ntp server settings"),
633             "base"                => _("Base"),
634             "cn"                  => _("Name"),
635             "gotoRootPasswd"      => _("Root password"),
636             "FAIstate"            => _("Action flag"))
637           ));
638   }
641   function set_everything_to_inherited()
642   {
643     $this->gotoTerminalPath  = "default";
644     $this->gotoSwapServer    = "default" ;
645     $this->gotoSyslogServer  = "default";
646     $this->inheritTimeServer = TRUE;
648     /* Set workstation service attributes to inherited */
649     if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
650       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
651             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
652         $this->parent->by_object['termservice']->$name = "default";
653       }
654     }
656     /* Set workstation startup attributes to inherited */
657     if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
658       $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
659       $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
660     }
661   }
664 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
665 ?>