Code

Added macAddress to the attributes to be replaced
[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 $gotoNtpServer= array();
16   var $gotoNtpServers= array();
17   var $gotoSndModule= "";
18   var $gotoFloppyEnable= "";
19   var $gotoCdromEnable= "";
20   var $ghCpuType= "-";
21   var $ghMemSize= "-";
22   var $ghUsbSupport= "-";
23   var $ghNetNic= array();
24   var $ghIdeDev= array();
25   var $ghScsiDev= array();
26   var $ghGfxAdapter= "-";
27   var $ghSoundAdapter= "-";
28   var $gotoLastUser= "-";
29   var $netConfigDNS;
30   /* Needed values and lists */
31   var $base= "";
32   var $cn= "";
33   var $orig_dn= "";
35   var $inheritTimeServer = true;
37   /* Plugin side filled */
38   var $modes= array();
40   /* attribute list for save action */
41   var $ignore_account= TRUE;
42   var $attributes= array("gotoMode", "gotoTerminalPath", 
43       "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
44       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
45       "ghCpuType", "ghMemSize","ghUsbSupport",
46       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
47   var $objectclasses= array("top", "gotoTerminal", "GOhard");
49   function termgeneric ($config, $dn= NULL)
50   {
51     plugin::plugin ($config, $dn);
52     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
53     /* Read arrays */
54     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
55       if (!isset($this->attrs[$val])){
56         continue;
57       }
58       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
59         array_push($this->$val, $this->attrs[$val][$i]);
60       }
61     }
63     /* Create used ntp server array */
64     $this->gotoNtpServer= array();
65     if(isset($this->attrs['gotoNtpServer'])){
66       $this->inheritTimeServer = false;
67       unset($this->attrs['gotoNtpServer']['count']);
68       foreach($this->attrs['gotoNtpServer'] as $server){
69         $this->gotoNtpServer[$server] = $server;
70       }
71     }
73     /* Set inherit checkbox state */
74     if(in_array("default",$this->gotoNtpServer)){
75       $this->inheritTimeServer = true;
76       $this->gotoNtpServer=array();
77     }
79     /* Create available ntp options */
80     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
81     foreach($this->gotoNtpServers as $key => $server){
82       if($server == "default"){
83         unset($this->gotoNtpServers[$key]);
84       }
85     }
87     $this->modes["disabled"]= _("disabled");
88     $this->modes["text"]= _("text");
89     $this->modes["graphic"]= _("graphic");
91     /* Set base */
92     if ($this->dn == "new"){
93       $ui= get_userinfo();
94       $this->base= dn2base($ui->dn);
95     } else {
96       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
97     }
99     $this->orig_dn= $this->dn;
100   }
102   function execute()
103   {
104     /* Call parent execute */
105     plugin::execute();
107     /* Do we need to flip is_account state? */
108     if (isset($_POST['modify_state'])){
109       $this->is_account= !$this->is_account;
110     }
112     if (isset($_POST['action'])){
113       switch($_POST['saction']){
114         case 'wake':
115           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
116           if ($cmd == ""){
117             print_red(_("No WAKECMD definition found in your gosa.conf"));
118           } else {
119             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
120             if ($retval != 0){
121               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
122             }
123           }
124           break;
126         case 'reboot':
127           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
128           if ($cmd == ""){
129             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
130           } else {
131             exec ($cmd." ".$this->cn, $dummy, $retval);
132             if ($retval != 0){
133               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
134             }
135           }
136           break;
138         case 'halt':
139           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
140           if ($cmd == ""){
141             print_red(_("No HALTCMD definition found in your gosa.conf"));
142           } else {
143             exec ($cmd." ".$this->cn, $dummy, $retval);
144             if ($retval != 0){
145               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
146             }
147           }
148           break;
149       }
150     }
152     /* Base select dialog */
153     $once = true;
154     foreach($_POST as $name => $value){
155       if(preg_match("/^chooseBase/",$name) && $once){
156         $once = false;
157         $this->dialog = new baseSelectDialog($this->config);
158         $this->dialog->setCurrentBase($this->base);
159       }
160     }
162     /* Dialog handling */
163     if(is_object($this->dialog)){
164       /* Must be called before save_object */
165       $this->dialog->save_object();
167       if($this->dialog->isClosed()){
168         $this->dialog = false;
169       }elseif($this->dialog->isSelected()){
170         $this->base = $this->dialog->isSelected();
171         $this->dialog= false;
172       }else{
173         return($this->dialog->execute());
174       }
175     }
177     /* Do we represent a valid terminal? */
178     if (!$this->is_account && $this->parent == NULL){
179       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
180         _("This 'dn' has no terminal features.")."</b>";
181       return($display);
182     }
184     /* Add new ntp Server to our list */
185     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
186       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
187     }
189     /* Delete selected NtpServer for list of used servers  */
190     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
191       foreach($_POST['gotoNtpServerSelected'] as $name){
192         unset($this->gotoNtpServer[$name]);
193       } 
194     }
196     /* Fill templating stuff */
197     $smarty= get_smarty();
198     $smarty->assign("cn", $this->cn);
199     $smarty->assign("staticAddress", "");
201     $smarty->assign("bases", $this->config->idepartments);
203     /* tell smarty the inherit checkbox state */
204     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
206     /* Check if terminal is online */
207     $query= "fping -q -r 1 -t 500 ".$this->cn;
208     exec ($query, $dummy, $retval);
210     /* Offline */
211     if ($retval == 0){
212       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
213     } else {
214       $smarty->assign("actions", array("wake" => _("Wake up")));
215     }
216     /* Arrays */
217     $smarty->assign("modes", $this->modes);
219     $tmp2 = array(); 
220     foreach($this->config->data['SERVERS']['NFS'] as $server){
221       if($server != "default"){
222         $tmp = split("\|",$server);
223         $tmp3= split(":",$tmp[0]);
225         $servername = $tmp3[0];
226         $nfsname    = $tmp3[1];  
228         $path ="";
229         if(isset($tmp[4])){
230           $path       = $tmp[4];  
231         }
233         $tmp2[$servername.":".$path]= $servername.":".$path; 
234       }else{
235         $tmp2[$server]=$server;
236       }
237     }
238   
239     $smarty->assign("nfsservers",     $tmp2);
240     $smarty->assign("syslogservers",  $this->config->data['SERVERS']['SYSLOG']);
241     $smarty->assign("ntpservers",     $this->gotoNtpServers);
243     /* Variables */
244     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
245           "gotoSyslogServer", "gotoNtpServer") as $val){
247       $smarty->assign($val."_select", $this->$val);
248       $smarty->assign($val."ACL", chkacl($this->acl, $val));
249     }
251     /* Show main page */
252     $smarty->assign("netconfig", $this->netConfigDNS->execute());
253     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
254     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
255   }
257   function remove_from_parent()
258   {
259     
260     $ldap= $this->config->get_ldap_link();
261     $ldap->cd($this->dn);
262     $ldap->cat($this->dn);
263     if($ldap->count()){
264       $this->netConfigDNS->remove_from_parent();
265       $ldap->rmDir($this->dn);
266       show_ldap_error($ldap->get_error());
268       /* Optionally execute a command after we're done */
269       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
271       /* Delete references to object groups */
272       $ldap->cd ($this->config->current['BASE']);
273       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
274       while ($ldap->fetch()){
275         $og= new ogroup($this->config, $ldap->getDN());
276         unset($og->member[$this->dn]);
277         $og->save ();
278       }
279     }
280   }
283   /* Save data to object */
284   function save_object()
285   {
286     plugin::save_object();
287     $this->netConfigDNS->save_object();
288     /* Save base, since this is no LDAP attribute */
289     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
290       $this->base= $_POST['base'];
291     }
293     /* Save terminal path to parent since it is used by termstartup, too */
294     $this->parent->by_object['termstartup']->gotoTerminalPath=
295       $this->gotoTerminalPath;
296   
297     if(isset($_POST['termgeneric_posted'])){
298       if(isset($_POST["inheritTimeServer"])){
299         $this->inheritTimeServer = true;
300       }else{
301         $this->inheritTimeServer = false;
302       }
303     }  
304   }
307   /* Check supplied data */
308   function check()
309   {
310     /* Call common method to give check the hook */
311     $message= plugin::check();
312     $message= array_merge($message, $this->netConfigDNS->check());
314     /* Permissions for that base? */
315     $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
317     $ui= get_userinfo();
318     $acl= get_permissions ($this->dn, $ui->subtreeACL);
319     $acl= get_module_permission($acl, "group", $this->dn);
320     if (chkacl($acl, "create") != ""){
321       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
322     }
324     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
325       $message[]= _("The required field 'Terminal name' is not set.");
326     }
328     if ($this->orig_dn == 'new'){
329       $ldap= $this->config->get_ldap_link();
330       $ldap->cd ($this->base);
331       $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
332       if ($ldap->count() != 0){
333         while ($attrs= $ldap->fetch()){
334           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
335             continue;
336           } else {
337             if ($attrs['dn'] != $this->orig_dn){
338               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
339               break;
340             }
341           }
342         }
343       }
344     }
346     /* Check for valid ntpServer selection */
347     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
348       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
349     }
351     return ($message);
352   }
355   /* Save to LDAP */
356   function save()
357   {
358     plugin::save();
360     /* Strip out 'default' values */
361     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
363       if ($this->attrs[$val] == "default"){
364         $this->attrs[$val]= array();
365       }
366     }
368     /* Add missing arrays */
369     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
370       if (isset ($this->$val) && count ($this->$val) != 0){
371         $this->attrs["$val"]= $this->$val;
372       }
373     }
375     /* Remove all empty values */
376     if ($this->orig_dn == 'new'){
377       $attrs= array();
378       foreach ($this->attrs as $key => $val){
379         if (is_array($val) && count($val) == 0){
380           continue;
381         }
382         $attrs[$key]= $val;
383       }
384       $this->attrs= $attrs;
385     }
387     if($this->inheritTimeServer){
388       $this->attrs['gotoNtpServer'] = "default";
389     }else{  
390       /* Set ntpServers */
391       $this->attrs['gotoNtpServer'] = array();
392       foreach($this->gotoNtpServer as $server){
393         $this->attrs['gotoNtpServer'][] = $server;
394       }
395     }
397     /* Write back to ldap */
398     $ldap= $this->config->get_ldap_link();
399     if ($this->orig_dn == 'new'){
400       $ldap->cd($this->config->current['BASE']);
401       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
402       $ldap->cd($this->dn);
403       $ldap->add($this->attrs);
404       $this->handle_post_events("add");
405     } else {
406       if ($this->orig_dn != $this->dn){
407         $this->move($this->orig_dn, $this->dn);
408       }
409       $ldap->cd($this->dn);
410       $this->cleanup();
411 $ldap->modify ($this->attrs); 
413       $this->handle_post_events("modify");
414     }
415     $this->netConfigDNS->cn = $this->cn;
416     $this->netConfigDNS->save($this->dn);
417     show_ldap_error($ldap->get_error());
419     /* Optionally execute a command after we're done */
420     $this->postcreate();
421   }
425 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
426 ?>