Code

Added RDN support for users
[gosa.git] / plugins / admin / systems / class_terminalGeneric.inc
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   function termgeneric ($config, $dn= NULL, $parent= NULL)
56   {
57     /* Check if FAI is activated */
58     $tmp = search_config($config->data,"faiManagement","CLASS");
59     if(!empty($tmp)){
60       $this->fai_activated = TRUE;
61     }
63     plugin::plugin ($config, $dn, $parent);
64     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
65     /* Read arrays */
66     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
67       if (!isset($this->attrs[$val])){
68         continue;
69       }
70       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
71         array_push($this->$val, $this->attrs[$val][$i]);
72       }
73     }
75     /* Create used ntp server array */
76     $this->gotoNtpServer= array();
77     if(isset($this->attrs['gotoNtpServer'])){
78       $this->inheritTimeServer = false;
79       unset($this->attrs['gotoNtpServer']['count']);
80       foreach($this->attrs['gotoNtpServer'] as $server){
81         $this->gotoNtpServer[$server] = $server;
82       }
83     }
85     /* Set inherit checkbox state */
86     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
87       $this->inheritTimeServer = true;
88       $this->gotoNtpServer=array();
89     }
91     /* Create available ntp options */
92     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
93     foreach($this->gotoNtpServers as $key => $server){
94       if($server == "default"){
95         unset($this->gotoNtpServers[$key]);
96       }
97     }
99     $this->modes["disabled"]= _("disabled");
100     $this->modes["text"]= _("text");
101     $this->modes["graphic"]= _("graphic");
103     /* Set base */
104     if ($this->dn == "new"){
105       $ui= get_userinfo();
106       $this->base= dn2base($ui->dn);
107     } else {
108       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
109     }
111     /* Create an array of all Syslog servers */
112     $tmp = $this->config->data['SERVERS']['SYSLOG'];
113     foreach($tmp as $server){
114       $visible = $server;
115       if($server == "default") {
116         $visible = "["._("inherited")."]";
117       }
118       $this->gotoSyslogServers[$server] = $visible;
119     }
121     $this->orig_dn= $this->dn;
122   }
124   function execute()
125   {
126     /* Call parent execute */
127     plugin::execute();
129     $this->netConfigDNS->acl = $this->acl;
131     /* Do we need to flip is_account state? */
132     if (isset($_POST['modify_state'])){
133       $this->is_account= !$this->is_account;
134     }
136     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") ==""){
138       if($this->fai_activated && $this->dn != "new"){
139         /* Set FAIstate */
140         $ldap = $this->config->get_ldap_link();
141         $ldap->cd($this->config->current['BASE']);
142         $ldap->cat($this->dn,array("objectClass"));
143         $res = $ldap->fetch();
145         $attrs = array();
146         $attrs['FAIstate'] = "";
147         if(isset($this->mapActions[$_POST['saction']])){
148           $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
149         }
151         for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
152           $attrs['objectClass'][] = $res['objectClass'][$i];
153         }
155         if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
156           $attrs['objectClass'][] = "FAIobject";
157         }
159         if($attrs['FAIstate'] == ""){
160 #FIXME we should check if FAIobject is used anymore
161           $attrs['FAIstate'] = array();
162         }
164         $ldap->cd($this->dn);
165         $ldap->modify($attrs);
166         show_ldap_error($ldap->get_error());
167       }
169       switch($_POST['saction']){
170         case 'wake':
171           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
172           if ($cmd == ""){
173             print_red(_("No WAKECMD definition found in your gosa.conf"));
174           } else {
175             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
176             if ($retval != 0){
177               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
178             }
179           }
180           break;
182         case 'reboot':
183           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
184           if ($cmd == ""){
185             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
186           } else {
187             exec ($cmd." ".$this->cn, $dummy, $retval);
188             if ($retval != 0){
189               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
190             }
191           }
192           break;
194         case 'halt':
195           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
196           if ($cmd == ""){
197             print_red(_("No HALTCMD definition found in your gosa.conf"));
198           } else {
199             exec ($cmd." ".$this->cn, $dummy, $retval);
200             if ($retval != 0){
201               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
202             }
203           }
204           break;
205       }
206     }
208     /* Base select dialog */
209     $once = true;
210     foreach($_POST as $name => $value){
211       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
212         $once = false;
213         $this->dialog = new baseSelectDialog($this->config);
214         $this->dialog->setCurrentBase($this->base);
215       }
216     }
218     /* Dialog handling */
219     if(is_object($this->dialog)){
220       /* Must be called before save_object */
221       $this->dialog->save_object();
223       if($this->dialog->isClosed()){
224         $this->dialog = false;
225       }elseif($this->dialog->isSelected()){
226         $this->base = $this->dialog->isSelected();
227         $this->dialog= false;
228       }else{
229         return($this->dialog->execute());
230       }
231     }
233     /* Do we represent a valid terminal? */
234     if (!$this->is_account && $this->parent == NULL){
235       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
236         _("This 'dn' has no terminal features.")."</b>";
237       return($display);
238     }
240     /* Add new ntp Server to our list */
241     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer")== ""){
242       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
243     }
245     /* Delete selected NtpServer for list of used servers  */
246     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer")== ""){
247       foreach($_POST['gotoNtpServerSelected'] as $name){
248         unset($this->gotoNtpServer[$name]);
249       } 
250     }
252     /* Fill templating stuff */
253     $smarty= get_smarty();
254     $smarty->assign("cn", $this->cn);
255     $smarty->assign("cnACL", chkacl($this->acl,"cn"));
256     $smarty->assign("staticAddress", "");
258     $smarty->assign("bases", $this->config->idepartments);
260     /* tell smarty the inherit checkbox state */
261     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
263     /* Check if terminal is online */
264     $query= "fping -q -r 1 -t 500 ".$this->cn;
265     exec ($query, $dummy, $retval);
267     /* Offline */
268     if ($retval == 0){
269       $smarty->assign("actions", array( "halt" => _("Switch off"), 
270                                         "reboot" => _("Reboot"),
271                                         "memcheck" => _("Memory test"),
272                                         "sysinfo"  => _("System analysis")));
273     } else {
274       $smarty->assign("actions", array("wake" => _("Wake up"),
275                                        "memcheck" => _("Memory test"),
276                                        "sysinfo"  => _("System analysis")));
277     }
279     /* Arrays */
280     $smarty->assign("modes", $this->modes);
282     $tmp2 = array(); 
283     foreach($this->config->data['SERVERS']['NFS'] as $server){
284       if($server != "default"){
285         $tmp2[$server]= $server;
286       }else{
287         $tmp2[$server]="["._("inherited")."]";
288       }
289     }
290   
291     $smarty->assign("nfsservers",     $tmp2);
293     
295     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
296     $smarty->assign("ntpservers",     $this->gotoNtpServers);
298     /* Variables */
299     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
300           "gotoSyslogServer", "gotoNtpServer") as $val){
302       $smarty->assign($val."_select", $this->$val);
303       $smarty->assign($val."ACL", chkacl($this->acl, $val));
304     }
306     /* Show main page */
307     $this->netConfigDNS->cn= $this->cn;
308     $smarty->assign("netconfig", $this->netConfigDNS->execute());
309     $smarty->assign("fai_activated",$this->fai_activated);
310     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
311     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
312   }
314   function remove_from_parent()
315   {
316     
317     $this->netConfigDNS->acl = $this->acl;
318     $ldap= $this->config->get_ldap_link();
319     $ldap->cd($this->dn);
320     $ldap->cat($this->dn, array('dn'));
321     if($ldap->count()){
322       $this->netConfigDNS->remove_from_parent();
323       $ldap->rmDir($this->dn);
324       show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
326       /* Optionally execute a command after we're done */
327       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
329       /* Delete references to object groups */
330       $ldap->cd ($this->config->current['BASE']);
331       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
332       while ($ldap->fetch()){
333         $og= new ogroup($this->config, $ldap->getDN());
334         unset($og->member[$this->dn]);
335         $og->save ();
336       }
337     }
338   }
341   /* Save data to object */
342   function save_object()
343   {
344     plugin::save_object();
345     $this->netConfigDNS->save_object();
346     /* Save base, since this is no LDAP attribute */
347     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
348       $this->base= $_POST['base'];
349     }
351     /* Save terminal path to parent since it is used by termstartup, too */
352     if(isset($this->parent->by_object['termstartup'])){
353     $this->parent->by_object['termstartup']->gotoTerminalPath=
354       $this->gotoTerminalPath;
355     }
356   
357     if(isset($_POST['termgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
358       if(isset($_POST["inheritTimeServer"])){
359         $this->inheritTimeServer = true;
360       }else{
361         $this->inheritTimeServer = false;
362       }
363     }  
364   }
367   /* Check supplied data */
368   function check()
369   {
370     /* Call common method to give check the hook */
371     $message= plugin::check();
373     if($this->cn != "default"){
374       $message= array_merge($message, $this->netConfigDNS->check());
375     }
377     /* Permissions for that base? */
378     $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
380     $ui= get_userinfo();
381     $acl= get_permissions ($this->dn, $ui->subtreeACL);
382     $acl= get_module_permission($acl, "group", $this->dn);
383     if (chkacl($acl, "create") != ""){
384       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
385     }
387     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
388       $message[]= _("The required field 'Terminal name' is not set.");
389     }
391     if ($this->orig_dn == 'new'){
392       $ldap= $this->config->get_ldap_link();
393       $ldap->cd ($this->config->current['BASE']);
394  
395       /* It is possible to have a 'default' terminal on every base */
396       if($this->cn == "default"){
397         $ldap->cat($this->dn);
398       }else{
399         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
400       }
401   
402       /* Check if there are some other terminals found */
403       if ($ldap->count() != 0){
404         while ($attrs= $ldap->fetch()){
405           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
406             continue;
407           } else {
408             if ($attrs['dn'] != $this->orig_dn){
409               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
410               break;
411             }
412           }
413         }
414       }
415     }
417     /* Check for valid ntpServer selection */
418     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
419       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
420     }
422     return ($message);
423   }
426   /* Save to LDAP */
427   function save()
428   {
429     $this->netConfigDNS->acl = $this->acl;
431     /* Move object if requested */
432     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
433       $this->move($this->orig_dn, $this->dn);
434     }
435  
436     plugin::save();
438     /* Strip out 'default' values */
439     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
441       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
442         $this->attrs[$val]= array();
443       }
444     }
446     /* Add missing arrays */
447     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
448       if (isset ($this->$val) && count ($this->$val) != 0){
449         $this->attrs["$val"]= $this->$val;
450       }
451     }
453     /* Remove all empty values */
454     if ($this->orig_dn == 'new'){
455       $attrs= array();
456       foreach ($this->attrs as $key => $val){
457         if (is_array($val) && count($val) == 0){
458           continue;
459         }
460         $attrs[$key]= $val;
461       }
462       $this->attrs= $attrs;
463     }
465     /* Set ntpServers */
466     $this->attrs['gotoNtpServer'] = array();
467     if(!$this->inheritTimeServer){
468       foreach($this->gotoNtpServer as $server){
469         $this->attrs['gotoNtpServer'][] = $server;
470       }
471     }
473     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
474     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
475       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
476     }
478     /* Write back to ldap */
479     $ldap= $this->config->get_ldap_link();
480     if ($this->orig_dn == 'new'){
481       $ldap->cd($this->config->current['BASE']);
482       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
483       $ldap->cd($this->dn);
484       if (!count($this->attrs['gotoNtpServer'])){
485         unset($this->attrs['gotoNtpServer']);
486       }
487       $ldap->add($this->attrs);
488       $this->handle_post_events("add");
489     } else {
490       $ldap->cd($this->dn);
491       $this->cleanup();
492       $ldap->modify ($this->attrs); 
493       $this->handle_post_events("modify");
494     }
495     
496     /* cn=default and macAddress=- indicates that this is a template */ 
497     if($this->cn == "default"){
498       $this->netConfigDNS->macAddress = "-";
499     }
500   
501     $this->netConfigDNS->cn = $this->cn;
502     $this->netConfigDNS->save($this->dn);
503     show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
505     /* Optionally execute a command after we're done */
506     $this->postcreate();
507   }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>