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> <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 }
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 {
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 $message= $this->netConfigDNS->check();
262 /* Permissions for that base? */
263 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
265 $ui= get_userinfo();
266 $acl= get_permissions ($this->dn, $ui->subtreeACL);
267 $acl= get_module_permission($acl, "group", $this->dn);
268 if (chkacl($acl, "create") != ""){
269 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
270 }
272 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
273 $message[]= _("The required field 'Terminal name' is not set.");
274 }
276 if ($this->orig_dn == 'new'){
277 $ldap= $this->config->get_ldap_link();
278 $ldap->cd ($this->base);
279 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
280 if ($ldap->count() != 0){
281 while ($attrs= $ldap->fetch()){
282 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
283 continue;
284 } else {
285 if ($attrs['dn'] != $this->orig_dn){
286 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
287 break;
288 }
289 }
290 }
291 }
292 }
294 return ($message);
295 }
298 /* Save to LDAP */
299 function save()
300 {
301 plugin::save();
303 /* Strip out 'default' values */
304 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
306 if ($this->attrs[$val] == "default"){
307 $this->attrs[$val]= array();
308 }
309 }
311 /* Add missing arrays */
312 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
313 if (isset ($this->$val) && count ($this->$val) != 0){
314 $this->attrs["$val"]= $this->$val;
315 }
316 }
318 /* Remove all empty values */
319 if ($this->orig_dn == 'new'){
320 $attrs= array();
321 foreach ($this->attrs as $key => $val){
322 if (is_array($val) && count($val) == 0){
323 continue;
324 }
325 $attrs[$key]= $val;
326 }
327 $this->attrs= $attrs;
328 }
330 /* Write back to ldap */
331 $ldap= $this->config->get_ldap_link();
332 if ($this->orig_dn == 'new'){
333 $ldap->cd($this->config->current['BASE']);
334 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
335 $ldap->cd($this->dn);
336 $ldap->add($this->attrs);
337 $this->handle_post_events("add");
338 } else {
339 if ($this->orig_dn != $this->dn){
340 $this->move($this->orig_dn, $this->dn);
341 }
342 $ldap->cd($this->dn);
343 $this->cleanup();
344 $ldap->modify ($this->attrs);
346 $this->handle_post_events("modify");
347 }
348 $this->netConfigDNS->cn = $this->cn;
349 $this->netConfigDNS->save($this->dn);
350 show_ldap_error($ldap->get_error());
352 /* Optionally execute a command after we're done */
353 $this->postcreate();
354 }
356 }
358 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
359 ?>