Code

Added support for general check hooks
[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= "";
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     $this->modes["disabled"]= _("disabled");
61     $this->modes["text"]= _("text");
62     $this->modes["graphic"]= _("graphic");
64     /* Set base */
65     if ($this->dn == "new"){
66       $ui= get_userinfo();
67       $this->base= dn2base($ui->dn);
68     } else {
69       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
70     }
72     $this->orig_dn= $this->dn;
73   }
75   function execute()
76   {
77         /* Call parent execute */
78         plugin::execute();
80     /* Do we need to flip is_account state? */
81     if (isset($_POST['modify_state'])){
82       $this->is_account= !$this->is_account;
83     }
85     if (isset($_POST['action'])){
86       switch($_POST['saction']){
87         case 'wake':
88           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
89           if ($cmd == ""){
90             print_red(_("No WAKECMD definition found in your gosa.conf"));
91           } else {
92             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
93             if ($retval != 0){
94               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
95             }
96           }
97           break;
99         case 'reboot':
100           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
101           if ($cmd == ""){
102             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
103           } else {
104             exec ($cmd." ".$this->cn, $dummy, $retval);
105             if ($retval != 0){
106               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
107             }
108           }
109           break;
111         case 'halt':
112           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
113           if ($cmd == ""){
114             print_red(_("No HALTCMD definition found in your gosa.conf"));
115           } else {
116             exec ($cmd." ".$this->cn, $dummy, $retval);
117             if ($retval != 0){
118               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
119             }
120           }
121           break;
122       }
123     }
125     /* Base select dialog */
126     $once = true;
127     foreach($_POST as $name => $value){
128       if(preg_match("/^chooseBase/",$name) && $once){
129         $once = false;
130         $this->dialog = new baseSelectDialog($this->config);
131         $this->dialog->setCurrentBase($this->base);
132       }
133     }
135     /* Dialog handling */
136     if(is_object($this->dialog)){
137       /* Must be called before save_object */
138       $this->dialog->save_object();
140       if($this->dialog->isClosed()){
141         $this->dialog = false;
142       }elseif($this->dialog->isSelected()){
143         $this->base = $this->dialog->isSelected();
144         $this->dialog= false;
145       }else{
146         return($this->dialog->execute());
147       }
148     }
150     /* Do we represent a valid terminal? */
151     if (!$this->is_account && $this->parent == NULL){
152       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
153         _("This 'dn' has no terminal features.")."</b>";
154       return($display);
155     }
157     /* Fill templating stuff */
158     $smarty= get_smarty();
159     $smarty->assign("cn", $this->cn);
160     $smarty->assign("staticAddress", "");
162     $smarty->assign("bases", $this->config->idepartments);
164     /* Check if terminal is online */
165     $query= "fping -q -r 1 -t 500 ".$this->cn;
166     exec ($query, $dummy, $retval);
168     /* Offline */
169     if ($retval == 0){
170       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
171     } else {
172       $smarty->assign("actions", array("wake" => _("Wake up")));
173     }
174     /* Arrays */
175     $smarty->assign("modes", $this->modes);
177     $tmp2 = array(); 
178     foreach($this->config->data['SERVERS']['NFS'] as $server){
179       if($server != "default"){
180         $tmp = split("\|",$server);
181         $tmp3= split(":",$tmp[0]);
183         $servername = $tmp3[0];
184         $nfsname    = $tmp3[1];  
186         $path ="";
187         if(isset($tmp[4])){
188           $path       = $tmp[4];  
189         }
191         $tmp2[$servername.":".$path]= $servername.":".$path; 
192       }else{
193         $tmp2[$server]=$server;
194       }
195     }
196   
197     $smarty->assign("nfsservers", $tmp2);
198     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
199     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
201     /* Variables */
202     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
203           "gotoSyslogServer", "gotoNtpServer") as $val){
205       $smarty->assign($val."_select", $this->$val);
206       $smarty->assign($val."ACL", chkacl($this->acl, $val));
207     }
209     /* Show main page */
210     $smarty->assign("netconfig", $this->netConfigDNS->execute());
211     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
212     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
213   }
215   function remove_from_parent()
216   {
217     
218     $ldap= $this->config->get_ldap_link();
219     $ldap->cd($this->dn);
220     $ldap->cat($this->dn);
221     if($ldap->count()){
222       $this->netConfigDNS->remove_from_parent();
223       $ldap->rmDir($this->dn);
224       show_ldap_error($ldap->get_error());
226       /* Optionally execute a command after we're done */
227       $this->handle_post_events("remove");
229       /* Delete references to object groups */
230       $ldap->cd ($this->config->current['BASE']);
231       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
232       while ($ldap->fetch()){
233         $og= new ogroup($this->config, $ldap->getDN());
234         unset($og->member[$this->dn]);
235         $og->save ();
236       }
237     }
238   }
241   /* Save data to object */
242   function save_object()
243   {
244     plugin::save_object();
245     $this->netConfigDNS->save_object();
246     /* Save base, since this is no LDAP attribute */
247     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
248       $this->base= $_POST['base'];
249     }
251     /* Save terminal path to parent since it is used by termstartup, too */
252     $this->parent->by_object['termstartup']->gotoTerminalPath=
253       $this->gotoTerminalPath;
254   }
257   /* Check supplied data */
258   function check()
259   {
260     /* Call common method to give check the hook */
261     $message= plugin::check();
262     $message= array_merge($message, $this->netConfigDNS->check());
264     /* Permissions for that base? */
265     $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
267     $ui= get_userinfo();
268     $acl= get_permissions ($this->dn, $ui->subtreeACL);
269     $acl= get_module_permission($acl, "group", $this->dn);
270     if (chkacl($acl, "create") != ""){
271       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
272     }
274     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
275       $message[]= _("The required field 'Terminal name' is not set.");
276     }
278     if ($this->orig_dn == 'new'){
279       $ldap= $this->config->get_ldap_link();
280       $ldap->cd ($this->base);
281       $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
282       if ($ldap->count() != 0){
283         while ($attrs= $ldap->fetch()){
284           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
285             continue;
286           } else {
287             if ($attrs['dn'] != $this->orig_dn){
288               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
289               break;
290             }
291           }
292         }
293       }
294     }
296     return ($message);
297   }
300   /* Save to LDAP */
301   function save()
302   {
303     plugin::save();
305     /* Strip out 'default' values */
306     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
308       if ($this->attrs[$val] == "default"){
309         $this->attrs[$val]= array();
310       }
311     }
313     /* Add missing arrays */
314     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
315       if (isset ($this->$val) && count ($this->$val) != 0){
316         $this->attrs["$val"]= $this->$val;
317       }
318     }
320     /* Remove all empty values */
321     if ($this->orig_dn == 'new'){
322       $attrs= array();
323       foreach ($this->attrs as $key => $val){
324         if (is_array($val) && count($val) == 0){
325           continue;
326         }
327         $attrs[$key]= $val;
328       }
329       $this->attrs= $attrs;
330     }
332     /* Write back to ldap */
333     $ldap= $this->config->get_ldap_link();
334     if ($this->orig_dn == 'new'){
335       $ldap->cd($this->config->current['BASE']);
336       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
337       $ldap->cd($this->dn);
338       $ldap->add($this->attrs);
339       $this->handle_post_events("add");
340     } else {
341       if ($this->orig_dn != $this->dn){
342         $this->move($this->orig_dn, $this->dn);
343       }
344       $ldap->cd($this->dn);
345       $this->cleanup();
346 $ldap->modify ($this->attrs); 
348       $this->handle_post_events("modify");
349     }
350     $this->netConfigDNS->cn = $this->cn;
351     $this->netConfigDNS->save($this->dn);
352     show_ldap_error($ldap->get_error());
354     /* Optionally execute a command after we're done */
355     $this->postcreate();
356   }
360 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
361 ?>