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 $gotoSyslogServers = array();
16 var $gotoNtpServer= array();
17 var $gotoNtpServers= array();
18 var $gotoSndModule= "";
19 var $gotoFloppyEnable= "";
20 var $gotoCdromEnable= "";
21 var $ghCpuType= "-";
22 var $ghMemSize= "-";
23 var $ghUsbSupport= "-";
24 var $ghNetNic= array();
25 var $ghIdeDev= array();
26 var $ghScsiDev= array();
27 var $ghGfxAdapter= "-";
28 var $ghSoundAdapter= "-";
29 var $gotoLastUser= "-";
30 var $netConfigDNS;
31 /* Needed values and lists */
32 var $base= "";
33 var $cn= "";
34 var $orig_dn= "";
36 var $inheritTimeServer = true;
38 /* Plugin side filled */
39 var $modes= array();
41 /* attribute list for save action */
42 var $ignore_account= TRUE;
43 var $attributes= array("gotoMode", "gotoTerminalPath",
44 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
45 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
46 "ghCpuType", "ghMemSize","ghUsbSupport",
47 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
48 var $objectclasses= array("top", "gotoTerminal", "GOhard");
50 var $mapActions = array("reboot" => "",
51 "instant_update" => "softupdate",
52 "update" => "sceduledupdate",
53 "reinstall" => "install",
54 "rescan" => "",
55 "memcheck" => "memcheck",
56 "sysinfo" => "sysinfo");
59 function termgeneric ($config, $dn= NULL)
60 {
61 plugin::plugin ($config, $dn);
62 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
63 /* Read arrays */
64 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
65 if (!isset($this->attrs[$val])){
66 continue;
67 }
68 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
69 array_push($this->$val, $this->attrs[$val][$i]);
70 }
71 }
73 /* Create used ntp server array */
74 $this->gotoNtpServer= array();
75 if(isset($this->attrs['gotoNtpServer'])){
76 $this->inheritTimeServer = false;
77 unset($this->attrs['gotoNtpServer']['count']);
78 foreach($this->attrs['gotoNtpServer'] as $server){
79 $this->gotoNtpServer[$server] = $server;
80 }
81 }
83 /* Set inherit checkbox state */
84 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
85 $this->inheritTimeServer = true;
86 $this->gotoNtpServer=array();
87 }
89 /* Create available ntp options */
90 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
91 foreach($this->gotoNtpServers as $key => $server){
92 if($server == "default"){
93 unset($this->gotoNtpServers[$key]);
94 }
95 }
97 $this->modes["disabled"]= _("disabled");
98 $this->modes["text"]= _("text");
99 $this->modes["graphic"]= _("graphic");
101 /* Set base */
102 if ($this->dn == "new"){
103 $ui= get_userinfo();
104 $this->base= dn2base($ui->dn);
105 } else {
106 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
107 }
109 /* Create an array of all Syslog servers */
110 $tmp = $this->config->data['SERVERS']['SYSLOG'];
111 foreach($tmp as $server){
112 $visible = $server;
113 if($server == "default") {
114 $visible = "["._("inherited")."]";
115 }
116 $this->gotoSyslogServers[$server] = $visible;
117 }
119 $this->orig_dn= $this->dn;
120 }
122 function execute()
123 {
124 /* Call parent execute */
125 plugin::execute();
127 /* Do we need to flip is_account state? */
128 if (isset($_POST['modify_state'])){
129 $this->is_account= !$this->is_account;
130 }
132 if (isset($_POST['action'])){
134 /* Set FAIstate */
135 $ldap = $this->config->get_ldap_link();
136 $ldap->cd($this->config->current['BASE']);
137 $ldap->cat($this->dn,array("objectClass"));
138 $res = $ldap->fetch();
140 $attrs = array();
141 $attrs['FAIstate'] = "";
142 if(isset($this->mapActions[$_POST['saction']])){
143 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
144 }
146 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
147 $attrs['objectClass'][] = $res['objectClass'][$i];
148 }
150 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
151 $attrs['objectClass'][] = "FAIobject";
152 }
154 if($attrs['FAIstate'] == ""){
155 #FIXME we should check if FAIobject is used anymore
156 $attrs['FAIstate'] = array();
157 }
159 $ldap->cd($this->dn);
160 $ldap->modify($attrs);
161 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
163 switch($_POST['saction']){
164 case 'wake':
165 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
166 if ($cmd == ""){
167 print_red(_("No WAKECMD definition found in your gosa.conf"));
168 } else {
169 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
170 if ($retval != 0){
171 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
172 }
173 }
174 break;
176 case 'reboot':
177 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
178 if ($cmd == ""){
179 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
180 } else {
181 exec ($cmd." ".$this->cn, $dummy, $retval);
182 if ($retval != 0){
183 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
184 }
185 }
186 break;
188 case 'halt':
189 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
190 if ($cmd == ""){
191 print_red(_("No HALTCMD definition found in your gosa.conf"));
192 } else {
193 exec ($cmd." ".$this->cn, $dummy, $retval);
194 if ($retval != 0){
195 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
196 }
197 }
198 break;
199 }
200 }
202 /* Base select dialog */
203 $once = true;
204 foreach($_POST as $name => $value){
205 if(preg_match("/^chooseBase/",$name) && $once){
206 $once = false;
207 $this->dialog = new baseSelectDialog($this->config,$this);
208 $this->dialog->setCurrentBase($this->base);
209 }
210 }
212 /* Dialog handling */
213 if(is_object($this->dialog)){
214 /* Must be called before save_object */
215 $this->dialog->save_object();
217 if($this->dialog->isClosed()){
218 $this->dialog = false;
219 }elseif($this->dialog->isSelected()){
220 $this->base = $this->dialog->isSelected();
221 $this->dialog= false;
222 }else{
223 return($this->dialog->execute());
224 }
225 }
227 /* Do we represent a valid terminal? */
228 if (!$this->is_account && $this->parent == NULL){
229 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
230 _("This 'dn' has no terminal features.")."</b>";
231 return($display);
232 }
234 /* Add new ntp Server to our list */
235 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
236 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
237 }
239 /* Delete selected NtpServer for list of used servers */
240 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
241 foreach($_POST['gotoNtpServerSelected'] as $name){
242 unset($this->gotoNtpServer[$name]);
243 }
244 }
246 /* Fill templating stuff */
247 $smarty= get_smarty();
248 $smarty->assign("cn", $this->cn);
249 $smarty->assign("staticAddress", "");
251 $smarty->assign("bases", $this->config->idepartments);
253 /* tell smarty the inherit checkbox state */
254 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
256 /* Check if terminal is online */
257 $query= "fping -q -r 1 -t 500 ".$this->cn;
258 exec ($query, $dummy, $retval);
260 /* Offline */
261 if ($retval == 0){
262 $smarty->assign("actions", array( "halt" => _("Switch off"),
263 "reboot" => _("Reboot"),
264 "memcheck" => _("Memory test"),
265 "sysinfo" => _("System analysis")));
266 } else {
267 $smarty->assign("actions", array("wake" => _("Wake up"),
268 "memcheck" => _("Memory test"),
269 "sysinfo" => _("System analysis")));
270 }
272 /* Arrays */
273 $smarty->assign("modes", $this->modes);
275 $tmp2 = array();
276 foreach($this->config->data['SERVERS']['NFS'] as $server){
277 if($server != "default"){
278 $tmp2[$server]= $server;
279 }else{
280 $tmp2[$server]="["._("inherited")."]";
281 }
282 }
284 $smarty->assign("nfsservers", $tmp2);
285 $smarty->assign("syslogservers", $this->gotoSyslogServers);
287 $tmp = array();
288 foreach($this->gotoNtpServers as $server){
289 if(!in_array($server,$this->gotoNtpServer)){
290 $tmp[$server] = $server;
291 }
292 }
294 $smarty->assign("ntpservers", $tmp);
296 /* Variables */
297 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
298 "gotoSyslogServer", "gotoNtpServer") as $val){
300 $smarty->assign($val."_select", $this->$val);
301 $smarty->assign($val."ACL", chkacl($this->acl, $val));
302 }
304 /* Show main page */
305 $smarty->assign("netconfig", $this->netConfigDNS->execute());
306 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
307 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
308 }
310 function remove_from_parent()
311 {
313 $ldap= $this->config->get_ldap_link();
314 $ldap->cd($this->dn);
315 $ldap->cat($this->dn, array('dn'));
316 if($ldap->count()){
317 $this->netConfigDNS->remove_from_parent();
318 $ldap->rmDir($this->dn);
319 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
321 /* Optionally execute a command after we're done */
322 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
324 /* Delete references to object groups */
325 $ldap->cd ($this->config->current['BASE']);
326 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
327 while ($ldap->fetch()){
328 $og= new ogroup($this->config, $ldap->getDN());
329 unset($og->member[$this->dn]);
330 $og->save ();
331 }
332 }
333 }
336 /* Save data to object */
337 function save_object()
338 {
339 plugin::save_object();
340 $this->netConfigDNS->save_object();
341 /* Save base, since this is no LDAP attribute */
342 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
343 $this->base= $_POST['base'];
344 }
346 /* Save terminal path to parent since it is used by termstartup, too */
347 $this->parent->by_object['termstartup']->gotoTerminalPath=
348 $this->gotoTerminalPath;
350 if(isset($_POST['termgeneric_posted'])){
351 if(isset($_POST["inheritTimeServer"])){
352 $this->inheritTimeServer = true;
353 }else{
354 $this->inheritTimeServer = false;
355 }
356 }
357 }
360 /* Check supplied data */
361 function check()
362 {
363 /* Call common method to give check the hook */
364 $message= plugin::check();
365 $message= array_merge($message, $this->netConfigDNS->check());
367 /* Permissions for that base? */
368 $this->dn= "cn=".$this->cn."ou=terminals,ou=systems,".$this->base;
370 $ui= get_userinfo();
371 $acl= get_permissions ($this->dn, $ui->subtreeACL);
372 $acl= get_module_permission($acl, "group", $this->dn);
373 if (chkacl($acl, "create") != ""){
374 $message[]= _("You have no permissions to create a terminal on this 'Base'.");
375 }
377 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
378 $message[]= _("The required field 'Terminal name' is not set.");
379 }
381 if ($this->orig_dn == 'new'){
382 $ldap= $this->config->get_ldap_link();
383 $ldap->cd ($this->base);
384 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
385 if ($ldap->count() != 0){
386 while ($attrs= $ldap->fetch()){
387 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
388 continue;
389 } else {
390 if ($attrs['dn'] != $this->orig_dn){
391 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
392 break;
393 }
394 }
395 }
396 }
397 }
399 /* Check for valid ntpServer selection */
400 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
401 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
402 }
404 return ($message);
405 }
408 /* Save to LDAP */
409 function save()
410 {
411 plugin::save();
413 /* Strip out 'default' values */
414 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
416 if ($this->attrs[$val] == "default"){
417 $this->attrs[$val]= array();
418 }
419 }
421 /* Add missing arrays */
422 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
423 if (isset ($this->$val) && count ($this->$val) != 0){
424 $this->attrs["$val"]= $this->$val;
425 }
426 }
428 /* Remove all empty values */
429 if ($this->orig_dn == 'new'){
430 $attrs= array();
431 foreach ($this->attrs as $key => $val){
432 if (is_array($val) && count($val) == 0){
433 continue;
434 }
435 $attrs[$key]= $val;
436 }
437 $this->attrs= $attrs;
438 }
440 /* Set ntpServers */
441 $this->attrs['gotoNtpServer'] = array();
442 if(!$this->inheritTimeServer){
443 foreach($this->gotoNtpServer as $server){
444 $this->attrs['gotoNtpServer'][] = $server;
445 }
446 }
448 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
449 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
450 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
451 }
453 /* Write back to ldap */
454 $ldap= $this->config->get_ldap_link();
455 if ($this->orig_dn == 'new'){
456 $ldap->cd($this->config->current['BASE']);
457 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
458 $ldap->cd($this->dn);
459 if (!count($this->attrs['gotoNtpServer'])){
460 unset($this->attrs['gotoNtpServer']);
461 }
462 $ldap->add($this->attrs);
463 $this->handle_post_events("add");
464 } else {
465 if ($this->orig_dn != $this->dn){
466 $this->move($this->orig_dn, $this->dn);
467 }
468 $ldap->cd($this->dn);
469 $this->cleanup();
470 $ldap->modify ($this->attrs);
472 $this->handle_post_events("modify");
473 }
474 $this->netConfigDNS->cn = $this->cn;
475 $this->netConfigDNS->save($this->dn);
476 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
478 /* Optionally execute a command after we're done */
479 $this->postcreate();
480 }
483 /* Return plugin informations for acl handling */
484 function plInfo()
485 {
486 return (array(
487 "plShortName" => _("Terminal"),
488 "plDescription" => _("Terminal generic"),
489 "plSelfModify" => FALSE,
490 "plDepends" => array(),
491 "plPriority" => 0,
492 "plSection" => array("administration"),
493 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
494 "objectClass" => "gotoTerminal")),
495 "plProvidedAcls"=> array(
496 "gotoMode" => _("Mode"),
497 "gotoTerminalPath" => _("Root server"),
498 "gotoSwapServer" => _("Swap server"),
499 "gotoSyslogServer" => _("Syslog server enabled"),
500 "gotoNtpServer" => _("Ntp server settings"),
501 "base" => _("Base"),
502 "cn" => _("Name"),
503 "FAIstate" => _("Action flag"))
504 ));
505 }
506 }
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
509 ?>