Code

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