Code

205415f09d0ef7232f518fb2b70bf593b8b4f8fb
[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"          => "scheduledupdate",
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 execute()
130   {
131     /* Call parent execute */
132     plugin::execute();
134     $this->netConfigDNS->acl = $this->acl;
136     /* Do we need to flip is_account state? */
137     if (isset($_POST['modify_state'])){
138       $this->is_account= !$this->is_account;
139     }
141     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") ==""){
143       if($this->fai_activated && $this->dn != "new"){
144         /* Set FAIstate */
145         $ldap = $this->config->get_ldap_link();
146         $ldap->cd($this->config->current['BASE']);
147         $ldap->cat($this->dn,array("objectClass"));
148         $res = $ldap->fetch();
150         $attrs = array();
151         $attrs['FAIstate'] = "";
152         if(isset($this->mapActions[$_POST['saction']])){
153           $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
154         }
156         for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
157           $attrs['objectClass'][] = $res['objectClass'][$i];
158         }
160         if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
161           $attrs['objectClass'][] = "FAIobject";
162         }
164         if($attrs['FAIstate'] == ""){
165 #FIXME we should check if FAIobject is used anymore
166           $attrs['FAIstate'] = array();
167         }
169         $ldap->cd($this->dn);
170         $ldap->modify($attrs);
171         show_ldap_error($ldap->get_error());
172       }
174       switch($_POST['saction']){
175         case 'wake':
176           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
177           if ($cmd == ""){
178             print_red(_("No WAKECMD definition found in your gosa.conf"));
179           } else {
180             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
181             if ($retval != 0){
182               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
183             }
184           }
185           break;
187         case 'reboot':
188           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
189           if ($cmd == ""){
190             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
191           } else {
192             exec ($cmd." ".$this->cn, $dummy, $retval);
193             if ($retval != 0){
194               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
195             }
196           }
197           break;
199         case 'halt':
200           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
201           if ($cmd == ""){
202             print_red(_("No HALTCMD definition found in your gosa.conf"));
203           } else {
204             exec ($cmd." ".$this->cn, $dummy, $retval);
205             if ($retval != 0){
206               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
207             }
208           }
209           break;
210       }
211     }
213     /* Base select dialog */
214     $once = true;
215     foreach($_POST as $name => $value){
216       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
217         $once = false;
218         $this->dialog = new baseSelectDialog($this->config);
219         $this->dialog->setCurrentBase($this->base);
220       }
221     }
223     /* Dialog handling */
224     if(is_object($this->dialog)){
225       /* Must be called before save_object */
226       $this->dialog->save_object();
228       if($this->dialog->isClosed()){
229         $this->dialog = false;
230       }elseif($this->dialog->isSelected()){
231         $this->base = $this->dialog->isSelected();
232         $this->dialog= false;
233       }else{
234         return($this->dialog->execute());
235       }
236     }
238     /* Do we represent a valid terminal? */
239     if (!$this->is_account && $this->parent == NULL){
240       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
241         _("This 'dn' has no terminal features.")."</b>";
242       return($display);
243     }
245     /* Add new ntp Server to our list */
246     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer")== ""){
247       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
248     }
250     /* Delete selected NtpServer for list of used servers  */
251     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer")== ""){
252       foreach($_POST['gotoNtpServerSelected'] as $name){
253         unset($this->gotoNtpServer[$name]);
254       } 
255     }
257     /* Fill templating stuff */
258     $smarty= get_smarty();
259     $smarty->assign("cn", $this->cn);
260     $smarty->assign("cnACL", chkacl($this->acl,"cn"));
261     $smarty->assign("staticAddress", "");
263     $smarty->assign("bases", $this->config->idepartments);
265     /* tell smarty the inherit checkbox state */
266     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
268     /* Check if terminal is online */
269     $query= "fping -q -r 1 -t 500 ".$this->cn;
270     exec ($query, $dummy, $retval);
272     /* Offline */
273     if ($retval == 0){
274       $smarty->assign("actions", array( "halt" => _("Switch off"), 
275                                         "reboot" => _("Reboot"),
276                                         "memcheck" => _("Memory test"),
277                                         "sysinfo"  => _("System analysis")));
278     } else {
279       $smarty->assign("actions", array("wake" => _("Wake up"),
280                                        "memcheck" => _("Memory test"),
281                                        "sysinfo"  => _("System analysis")));
282     }
284     /* Arrays */
285     $smarty->assign("modes", $this->modes);
287     $tmp2 = array(); 
288     foreach($this->config->data['SERVERS']['NFS'] as $server){
289       if($server != "default"){
290         $tmp2[$server]= $server;
291       }else{
292         $tmp2[$server]="["._("inherited")."]";
293       }
294     }
295   
296     $smarty->assign("nfsservers",     $tmp2);
298     
300     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
301     $smarty->assign("ntpservers",     $this->gotoNtpServers);
303     /* Variables */
304     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
305           "gotoSyslogServer", "gotoNtpServer") as $val){
307       $smarty->assign($val."_select", $this->$val);
308       $smarty->assign($val."ACL", chkacl($this->acl, $val));
309     }
311     /* Show main page */
312     $this->netConfigDNS->cn= $this->cn;
313     $smarty->assign("netconfig", $this->netConfigDNS->execute());
314     $smarty->assign("fai_activated",$this->fai_activated);
315     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
316     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
317   }
319   function remove_from_parent()
320   {
321     
322     $this->netConfigDNS->acl = $this->acl;
323     $ldap= $this->config->get_ldap_link();
324     $ldap->cd($this->dn);
325     $ldap->cat($this->dn, array('dn'));
326     if($ldap->count()){
327       $this->netConfigDNS->remove_from_parent();
328       $ldap->rmDir($this->dn);
329       show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
331       /* Optionally execute a command after we're done */
332       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
334       /* Delete references to object groups */
335       $ldap->cd ($this->config->current['BASE']);
336       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
337       while ($ldap->fetch()){
338         $og= new ogroup($this->config, $ldap->getDN());
339         unset($og->member[$this->dn]);
340         $og->save ();
341       }
342     }
343   }
346   /* Save data to object */
347   function save_object()
348   {
349     plugin::save_object();
350     $this->netConfigDNS->save_object();
351     /* Save base, since this is no LDAP attribute */
352     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
353       $this->base= $_POST['base'];
354     }
356     /* Save terminal path to parent since it is used by termstartup, too */
357     if(isset($this->parent->by_object['termstartup'])){
358     $this->parent->by_object['termstartup']->gotoTerminalPath=
359       $this->gotoTerminalPath;
360     }
361   
362     if(isset($_POST['termgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
363       if(isset($_POST["inheritTimeServer"])){
364         $this->inheritTimeServer = true;
365       }else{
366         $this->inheritTimeServer = false;
367       }
368     }  
369   }
372   /* Check supplied data */
373   function check()
374   {
375     /* Call common method to give check the hook */
376     $message= plugin::check();
378     if($this->cn != "default"){
379       $message= array_merge($message, $this->netConfigDNS->check());
380     }
382     /* Permissions for that base? */
383     $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
385     $ui= get_userinfo();
386     $acl= get_permissions ($this->dn, $ui->subtreeACL);
387     $acl= get_module_permission($acl, "group", $this->dn);
388     if (chkacl($acl, "create") != ""){
389       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
390     }
392     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
393       $message[]= _("The required field 'Terminal name' is not set.");
394     }
396     if ($this->orig_dn == 'new'){
397       $ldap= $this->config->get_ldap_link();
398       $ldap->cd ($this->config->current['BASE']);
399  
400       /* It is possible to have a 'default' terminal on every base */
401       if($this->cn == "default"){
402         $ldap->cat($this->dn);
403       }else{
404         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
405       }
406   
407       /* Check if there are some other terminals found */
408       if ($ldap->count() != 0){
409         while ($attrs= $ldap->fetch()){
410           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
411             continue;
412           } else {
413             if ($attrs['dn'] != $this->orig_dn){
414               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
415               break;
416             }
417           }
418         }
419       }
420     }
422     /* Check for valid ntpServer selection */
423     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
424       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
425     }
427     return ($message);
428   }
431   /* Save to LDAP */
432   function save()
433   {
434     $this->netConfigDNS->acl = $this->acl;
436     /* Move object if requested */
437     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
438       $this->move($this->orig_dn, $this->dn);
439     }
440  
441     plugin::save();
443     /* Strip out 'default' values */
444     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
446       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
447         $this->attrs[$val]= array();
448       }
449     }
451     /* Add missing arrays */
452     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
453       if (isset ($this->$val) && count ($this->$val) != 0){
454         $this->attrs["$val"]= $this->$val;
455       }
456     }
458     /* Remove all empty values */
459     if ($this->orig_dn == 'new'){
460       $attrs= array();
461       foreach ($this->attrs as $key => $val){
462         if (is_array($val) && count($val) == 0){
463           continue;
464         }
465         $attrs[$key]= $val;
466       }
467       $this->attrs= $attrs;
468     }
470     /* Set ntpServers */
471     $this->attrs['gotoNtpServer'] = array();
472     if(!$this->inheritTimeServer){
473       foreach($this->gotoNtpServer as $server){
474         $this->attrs['gotoNtpServer'][] = $server;
475       }
476     }
478     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
479     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
480       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
481     }
483     /* Write back to ldap */
484     $ldap= $this->config->get_ldap_link();
485     if ($this->orig_dn == 'new'){
486       $ldap->cd($this->config->current['BASE']);
487       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
488       $ldap->cd($this->dn);
489       if (!count($this->attrs['gotoNtpServer'])){
490         unset($this->attrs['gotoNtpServer']);
491       }
492       $ldap->add($this->attrs);
493       $this->handle_post_events("add");
494     } else {
495       $ldap->cd($this->dn);
496       $this->cleanup();
497       $ldap->modify ($this->attrs); 
498       $this->handle_post_events("modify");
499     }
500     
501     /* cn=default and macAddress=- indicates that this is a template */ 
502     if($this->cn == "default"){
503       $this->netConfigDNS->macAddress = "-";
504     }
505   
506     $this->netConfigDNS->cn = $this->cn;
507     $this->netConfigDNS->save($this->dn);
508     show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
510     /* Optionally execute a command after we're done */
511     $this->postcreate();
512   }
516 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
517 ?>