Code

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