Code

Added execute methods
[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 $macAddress= "";
22   var $ipHostNumber= "";
23   var $ghUsbSupport= "-";
24   var $ghNetNic= array();
25   var $ghIdeDev= array();
26   var $ghScsiDev= array();
27   var $ghGfxAdapter= "-";
28   var $ghSoundAdapter= "-";
29   var $gotoLastUser= "-";
31   /* Needed values and lists */
32   var $base= "";
33   var $cn= "";
34   var $orig_dn= "";
36   /* Plugin side filled */
37   var $modes= array();
39   /* attribute list for save action */
40   var $ignore_account= TRUE;
41   var $attributes= array("gotoMode", "gotoTerminalPath", "macAddress",
42       "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
43       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44       "ghCpuType", "ghMemSize", "ipHostNumber", "ghUsbSupport",
45       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
46   var $objectclasses= array("top", "gotoTerminal", "GOhard");
48   function termgeneric ($config, $dn= NULL)
49   {
50     plugin::plugin ($config, $dn);
52     /* Read arrays */
53     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
54       if (!isset($this->attrs[$val])){
55         continue;
56       }
57       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
58         array_push($this->$val, $this->attrs[$val][$i]);
59       }
60     }
62     $this->modes["disabled"]= _("disabled");
63     $this->modes["text"]= _("text");
64     $this->modes["graphic"]= _("graphic");
66     /* Set base */
67     if ($this->dn == "new"){
68       $ui= get_userinfo();
69       $this->base= dn2base($ui->dn);
70     } else {
71       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
72     }
74     $this->orig_dn= $this->dn;
75   }
77   function execute()
78   {
79         /* Call parent execute */
80         plugin::execute();
81     /* Do we need to flip is_account state? */
82     if (isset($_POST['modify_state'])){
83       $this->is_account= !$this->is_account;
84     }
86     if (isset($_POST['action'])){
87       switch($_POST['saction']){
88         case 'wake':
89           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
90           if ($cmd == ""){
91             print_red(_("No WAKECMD definition found in your gosa.conf"));
92           } else {
93             exec ($cmd." ".$this->macAddress, $dummy, $retval);
94             if ($retval != 0){
95               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
96             }
97           }
98           break;
100         case 'reboot':
101           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
102           if ($cmd == ""){
103             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
104           } else {
105             exec ($cmd." ".$this->cn, $dummy, $retval);
106             if ($retval != 0){
107               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
108             }
109           }
110           break;
112         case 'halt':
113           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
114           if ($cmd == ""){
115             print_red(_("No HALTCMD definition found in your gosa.conf"));
116           } else {
117             exec ($cmd." ".$this->cn, $dummy, $retval);
118             if ($retval != 0){
119               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
120             }
121           }
122           break;
123       }
124     }
126     /* Do we represent a valid terminal? */
127     if (!$this->is_account && $this->parent == NULL){
128       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
129         _("This 'dn' has no terminal features.")."</b>";
130       return($display);
131     }
133     /* Fill templating stuff */
134     $smarty= get_smarty();
135     $smarty->assign("cn", $this->cn);
136     $smarty->assign("staticAddress", "");
138     $smarty->assign("bases", $this->config->idepartments);
140     /* Check if terminal is online */
141     $query= "fping -q -r 1 -t 500 ".$this->cn;
142     exec ($query, $dummy, $retval);
144     /* Offline */
145     if ($retval == 0){
146       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
147     } else {
148       $smarty->assign("actions", array("wake" => _("Wake up")));
149     }
150     /* Arrays */
151     $smarty->assign("modes", $this->modes);
153     $tmp2 = array(); 
154     foreach($this->config->data['SERVERS']['NFS'] as $server){
155       if($server != "default"){
156         $tmp = split("\|",$server);
157         $tmp3= split(":",$tmp[0]);
159         $servername = $tmp3[0];
160         $nfsname    = $tmp3[1];  
162         $path ="";
163         if(isset($tmp[4])){
164           $path       = $tmp[4];  
165         }
167         $tmp2[$servername.":".$path]= $servername.":".$path; 
168       }else{
169         $tmp2[$server]=$server;
170       }
171     }
172   
173     $smarty->assign("nfsservers", $tmp2);
174     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
175     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
177     /* Variables */
178     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
179           "gotoSyslogServer", "gotoNtpServer") as $val){
181       $smarty->assign($val."_select", $this->$val);
182       $smarty->assign($val."ACL", chkacl($this->acl, $val));
183     }
184     $smarty->assign("ipHostNumber", $this->ipHostNumber);
185     $smarty->assign("macAddress", $this->macAddress);
187     /* Show main page */
188     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
189     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
190     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
191   }
193   function remove_from_parent()
194   {
195     $ldap= $this->config->get_ldap_link();
196     $ldap->rmdir($this->dn);
197     show_ldap_error($ldap->get_error());
199     /* Optionally execute a command after we're done */
200     $this->handle_post_events("remove");
202     /* Delete references to object groups */
203     $ldap->cd ($this->config->current['BASE']);
204     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
205     while ($ldap->fetch()){
206       $og= new ogroup($this->config, $ldap->getDN());
207       unset($og->member[$this->dn]);
208       $og->save ();
209     }
211   }
214   /* Save data to object */
215   function save_object()
216   {
217     plugin::save_object();
219     /* Save base, since this is no LDAP attribute */
220     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
221       $this->base= $_POST['base'];
222     }
224     /* Save terminal path to parent since it is used by termstartup, too */
225     $this->parent->by_object['termstartup']->gotoTerminalPath=
226       $this->gotoTerminalPath;
227   }
230   /* Check supplied data */
231   function check()
232   {
233     $message= array();
235     /* Permissions for that base? */
236     $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
238     $ui= get_userinfo();
239     $acl= get_permissions ($this->dn, $ui->subtreeACL);
240     $acl= get_module_permission($acl, "group", $this->dn);
241     if (chkacl($acl, "create") != ""){
242       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
243     }
245     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
246       $message[]= _("The required field 'Terminal name' is not set.");
247     }
249     if ($this->orig_dn == 'new'){
250       $ldap= $this->config->get_ldap_link();
251       $ldap->cd ($this->base);
252       $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
253       if ($ldap->count() != 0){
254         while ($attrs= $ldap->fetch()){
255           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
256             continue;
257           } else {
258             if ($attrs['dn'] != $this->orig_dn){
259               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
260               break;
261             }
262           }
263         }
264       }
265     }
267     return ($message);
268   }
271   /* Save to LDAP */
272   function save()
273   {
274     plugin::save();
276     /* Strip out 'default' values */
277     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
279       if ($this->attrs[$val] == "default"){
280         $this->attrs[$val]= array();
281       }
282     }
284     /* Add missing arrays */
285     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
286       if (isset ($this->$val) && count ($this->$val) != 0){
287         $this->attrs["$val"]= $this->$val;
288       }
289     }
291     /* Remove all empty values */
292     if ($this->orig_dn == 'new'){
293       $attrs= array();
294       foreach ($this->attrs as $key => $val){
295         if (is_array($val) && count($val) == 0){
296           continue;
297         }
298         $attrs[$key]= $val;
299       }
300       $this->attrs= $attrs;
301     }
303     /* Write back to ldap */
304     $ldap= $this->config->get_ldap_link();
305     if ($this->orig_dn == 'new'){
306       $ldap->cd($this->config->current['BASE']);
307       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
308       $ldap->cd($this->dn);
309       $ldap->add($this->attrs);
310       $this->handle_post_events("add");
311     } else {
312       if ($this->orig_dn != $this->dn){
313         $this->move($this->orig_dn, $this->dn);
314       }
315       $ldap->cd($this->dn);
316       $ldap->modify($this->attrs);
317       $this->handle_post_events("modify");
318     }
319     show_ldap_error($ldap->get_error());
321     /* Optionally execute a command after we're done */
322     $this->postcreate();
323   }
327 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
328 ?>