Code

a8e0c501245c4fb19cf1e7c26e0b9cf9218f5d82
[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     /* Do we need to flip is_account state? */
80     if (isset($_POST['modify_state'])){
81       $this->is_account= !$this->is_account;
82     }
84     if (isset($_POST['action'])){
85       switch($_POST['saction']){
86         case 'wake':
87           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
88           if ($cmd == ""){
89             print_red(_("No WAKECMD definition found in your gosa.conf"));
90           } else {
91             exec ($cmd." ".$this->macAddress, $dummy, $retval);
92             if ($retval != 0){
93               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
94             }
95           }
96           break;
98         case 'reboot':
99           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
100           if ($cmd == ""){
101             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
102           } else {
103             exec ($cmd." ".$this->cn, $dummy, $retval);
104             if ($retval != 0){
105               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
106             }
107           }
108           break;
110         case 'halt':
111           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
112           if ($cmd == ""){
113             print_red(_("No HALTCMD definition found in your gosa.conf"));
114           } else {
115             exec ($cmd." ".$this->cn, $dummy, $retval);
116             if ($retval != 0){
117               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
118             }
119           }
120           break;
121       }
122     }
124     /* Do we represent a valid terminal? */
125     if (!$this->is_account && $this->parent == NULL){
126       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
127         _("This 'dn' has no terminal features.")."</b>";
128       return($display);
129     }
131     /* Fill templating stuff */
132     $smarty= get_smarty();
133     $smarty->assign("cn", $this->cn);
134     $smarty->assign("staticAddress", "");
136     $smarty->assign("bases", $this->config->idepartments);
138     /* Check if terminal is online */
139     $query= "fping -q -r 1 -t 500 ".$this->cn;
140     exec ($query, $dummy, $retval);
142     /* Offline */
143     if ($retval == 0){
144       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
145     } else {
146       $smarty->assign("actions", array("wake" => _("Wake up")));
147     }
148     /* Arrays */
149     $smarty->assign("modes", $this->modes);
151     $tmp2 = array(); 
152     foreach($this->config->data['SERVERS']['NFS'] as $server){
153       if($server != "default"){
154         $tmp = split("\|",$server);
155         $tmp3= split(":",$tmp[0]);
157         $servername = $tmp3[0];
158         $nfsname    = $tmp3[1];  
160         $path ="";
161         if(isset($tmp[4])){
162           $path       = $tmp[4];  
163         }
165         $tmp2[$servername.":".$path]= $servername.":".$path; 
166       }else{
167         $tmp2[$server]=$server;
168       }
169     }
170   
171     $smarty->assign("nfsservers", $tmp2);
172     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
173     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
175     /* Variables */
176     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
177           "gotoSyslogServer", "gotoNtpServer") as $val){
179       $smarty->assign($val."_select", $this->$val);
180       $smarty->assign($val."ACL", chkacl($this->acl, $val));
181     }
182     $smarty->assign("ipHostNumber", $this->ipHostNumber);
183     $smarty->assign("macAddress", $this->macAddress);
185     /* Show main page */
186     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
187     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
188     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
189   }
191   function remove_from_parent()
192   {
193     $ldap= $this->config->get_ldap_link();
194     $ldap->rmdir($this->dn);
195     show_ldap_error($ldap->get_error());
197     /* Optionally execute a command after we're done */
198     $this->handle_post_events("remove");
200     /* Delete references to object groups */
201     $ldap->cd ($this->config->current['BASE']);
202     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
203     while ($ldap->fetch()){
204       $og= new ogroup($this->config, $ldap->getDN());
205       unset($og->member[$this->dn]);
206       $og->save ();
207     }
209   }
212   /* Save data to object */
213   function save_object()
214   {
215     plugin::save_object();
217     /* Save base, since this is no LDAP attribute */
218     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
219       $this->base= $_POST['base'];
220     }
222     /* Save terminal path to parent since it is used by termstartup, too */
223     $this->parent->by_object['termstartup']->gotoTerminalPath=
224       $this->gotoTerminalPath;
225   }
228   /* Check supplied data */
229   function check()
230   {
231     $message= array();
233     /* Permissions for that base? */
234     $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
236     $ui= get_userinfo();
237     $acl= get_permissions ($this->dn, $ui->subtreeACL);
238     $acl= get_module_permission($acl, "group", $this->dn);
239     if (chkacl($acl, "create") != ""){
240       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
241     }
243     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
244       $message[]= _("The required field 'Terminal name' is not set.");
245     }
247     if ($this->orig_dn == 'new'){
248       $ldap= $this->config->get_ldap_link();
249       $ldap->cd ($this->base);
250       $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
251       if ($ldap->count() != 0){
252         while ($attrs= $ldap->fetch()){
253           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
254             continue;
255           } else {
256             if ($attrs['dn'] != $this->orig_dn){
257               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
258               break;
259             }
260           }
261         }
262       }
263     }
265     return ($message);
266   }
269   /* Save to LDAP */
270   function save()
271   {
272     plugin::save();
274     /* Strip out 'default' values */
275     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
277       if ($this->attrs[$val] == "default"){
278         $this->attrs[$val]= array();
279       }
280     }
282     /* Add missing arrays */
283     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
284       if (isset ($this->$val) && count ($this->$val) != 0){
285         $this->attrs["$val"]= $this->$val;
286       }
287     }
289     /* Remove all empty values */
290     if ($this->orig_dn == 'new'){
291       $attrs= array();
292       foreach ($this->attrs as $key => $val){
293         if (is_array($val) && count($val) == 0){
294           continue;
295         }
296         $attrs[$key]= $val;
297       }
298       $this->attrs= $attrs;
299     }
301     /* Write back to ldap */
302     $ldap= $this->config->get_ldap_link();
303     if ($this->orig_dn == 'new'){
304       $ldap->cd($this->config->current['BASE']);
305       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
306       $ldap->cd($this->dn);
307       $ldap->add($this->attrs);
308       $this->handle_post_events("add");
309     } else {
310       if ($this->orig_dn != $this->dn){
311         $this->move($this->orig_dn, $this->dn);
312       }
313       $ldap->cd($this->dn);
314       $ldap->modify($this->attrs);
315       $this->handle_post_events("modify");
316     }
317     show_ldap_error($ldap->get_error());
319     /* Optionally execute a command after we're done */
320     $this->postcreate();
321   }
325 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
326 ?>