Code

Updated an error message. fixed server acl string
[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 $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, $parent= NULL)
60   {
61     plugin::plugin ($config, $dn, $parent);
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 set_acl_base($base)
123   {
124     plugin::set_acl_base($base);
125     $this->netConfigDNS->set_acl_base($base);
126   }
128   function set_acl_category($cat)
129   {
130     plugin::set_acl_category($cat);
131     $this->netConfigDNS->set_acl_category($cat);
132   }
134   function execute()
135   {
136     /* Call parent execute */
137     plugin::execute();
139     /* Do we need to flip is_account state? */
140     if (isset($_POST['modify_state'])){
141       $this->is_account= !$this->is_account;
142     }
144     if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
146       /* Set FAIstate */
147       $ldap = $this->config->get_ldap_link();
148       $ldap->cd($this->config->current['BASE']);
149       $ldap->cat($this->dn,array("objectClass"));
150       $res = $ldap->fetch();
152       $attrs = array();
153       $attrs['FAIstate'] = "";
154       if(isset($this->mapActions[$_POST['saction']])){
155         $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
156       }
158       for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
159         $attrs['objectClass'][] = $res['objectClass'][$i];
160       }
162       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
163         $attrs['objectClass'][] = "FAIobject";
164       }
166       if($attrs['FAIstate'] == ""){
167       #FIXME we should check if FAIobject is used anymore
168         $attrs['FAIstate'] = array();
169       }
171       $ldap->cd($this->dn);
172       $ldap->modify($attrs);
173       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
175       switch($_POST['saction']){
176         case 'wake':
177           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
178           if ($cmd == ""){
179             print_red(_("No WAKECMD definition found in your gosa.conf"));
180           } else {
181             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
182             if ($retval != 0){
183               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
184             }
185           }
186           break;
188         case 'reboot':
189           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
190           if ($cmd == ""){
191             print_red(_("No REBOOTCMD 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;
200         case 'halt':
201           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
202           if ($cmd == ""){
203             print_red(_("No HALTCMD definition found in your gosa.conf"));
204           } else {
205             exec ($cmd." ".$this->cn, $dummy, $retval);
206             if ($retval != 0){
207               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
208             }
209           }
210           break;
211       }
212     }
214     /* Base select dialog */
215     $once = true;
216     foreach($_POST as $name => $value){
217       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
218         $once = false;
219         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
220         $this->dialog->setCurrentBase($this->base);
221       }
222     }
224     /* Dialog handling */
225     if(is_object($this->dialog)){
226       /* Must be called before save_object */
227       $this->dialog->save_object();
229       if($this->dialog->isClosed()){
230         $this->dialog = false;
231       }elseif($this->dialog->isSelected()){
233         /* A new base was selected, check if it is a valid one */
234         $tmp = $this->get_allowed_bases();
235         if(isset($tmp[$this->dialog->isSelected()])){
236           $this->base = $this->dialog->isSelected();
237         }
238         $this->dialog= false;
239       }else{
240         return($this->dialog->execute());
241       }
242     }
244     /* Do we represent a valid terminal? */
245     if (!$this->is_account && $this->parent == NULL){
246       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
247         _("This 'dn' has no terminal features.")."</b>";
248       return($display);
249     }
251     /* Add new ntp Server to our list */
252     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
253       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
254     }
256     /* Delete selected NtpServer for list of used servers  */
257     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
258       foreach($_POST['gotoNtpServerSelected'] as $name){
259         unset($this->gotoNtpServer[$name]);
260       } 
261     }
263     /* Fill templating stuff */
264     $smarty= get_smarty();
265     
266     $tmp = $this->plInfo();
267     foreach($tmp['plProvidedAcls'] as $name => $translation){
268       $smarty->assign($name."ACL",$this->getacl($name));
269     }
271     $smarty->assign("cn", $this->cn);
272     $smarty->assign("staticAddress", "");
274     $smarty->assign("bases", $this->get_allowed_bases());
276     /* tell smarty the inherit checkbox state */
277     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
279     /* Check if terminal is online */
280     $query= "fping -q -r 1 -t 500 ".$this->cn;
281     exec ($query, $dummy, $retval);
283     /* Offline */
284     if ($retval == 0){
285       $smarty->assign("actions", array( "halt" => _("Switch off"), 
286                                         "reboot" => _("Reboot"),
287                                         "memcheck" => _("Memory test"),
288                                         "sysinfo"  => _("System analysis")));
289     } else {
290       $smarty->assign("actions", array("wake" => _("Wake up"),
291                                        "memcheck" => _("Memory test"),
292                                        "sysinfo"  => _("System analysis")));
293     }
295     /* Arrays */
296     $smarty->assign("modes", $this->modes);
298     $tmp2 = array(); 
299     foreach($this->config->data['SERVERS']['NFS'] as $server){
300       if($server != "default"){
301         $tmp2[$server]= $server;
302       }else{
303         $tmp2[$server]="["._("inherited")."]";
304       }
305     }
306   
307     $smarty->assign("nfsservers",     $tmp2);
308     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
310     $tmp = array();
311     foreach($this->gotoNtpServers as $server){
312       if(!in_array($server,$this->gotoNtpServer)){
313         $tmp[$server] = $server;
314       }
315     }
316     
317     $smarty->assign("ntpservers",     $tmp);
319     /* Variables */
320     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
321       $smarty->assign($val."_select", $this->$val);
322     }
324     /* Show main page */
325     $smarty->assign("netconfig", $this->netConfigDNS->execute());
326     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
327   }
329   function remove_from_parent()
330   {
331     if($this->acl_is_removeable()){   
332       $ldap= $this->config->get_ldap_link();
333       $ldap->cd($this->dn);
334       $ldap->cat($this->dn, array('dn'));
335       if($ldap->count()){
336         $this->netConfigDNS->remove_from_parent();
337         $ldap->rmDir($this->dn);
338         show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
340         /* Optionally execute a command after we're done */
341         $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
343         /* Delete references to object groups */
344         $ldap->cd ($this->config->current['BASE']);
345         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
346         while ($ldap->fetch()){
347           $og= new ogroup($this->config, $ldap->getDN());
348           unset($og->member[$this->dn]);
349           $og->save ();
350         }
351       }
352     }
353   }
356   /* Save data to object */
357   function save_object()
358   {
359     /* Create a base backup and reset the
360        base directly after calling plugin::save_object();
361        Base will be set seperatly a few lines below */
362     $base_tmp = $this->base;
363     plugin::save_object();
364     $this->base = $base_tmp;
366     /* Set new base if allowed */
367     $tmp = $this->get_allowed_bases();
368     if(isset($_POST['base'])){
369       if(isset($tmp[$_POST['base']])){
370         $this->base= $_POST['base'];
371       }
372     }
373     
374     $this->netConfigDNS->save_object();
376     /* Save terminal path to parent since it is used by termstartup, too */
377     $this->parent->by_object['termstartup']->gotoTerminalPath=
378       $this->gotoTerminalPath;
379   
380     if(isset($_POST['termgeneric_posted'])){
381       if(isset($_POST["inheritTimeServer"])){
382         $this->inheritTimeServer = true;
383       }else{
384         $this->inheritTimeServer = false;
385       }
386     }  
387   }
390   /* Check supplied data */
391   function check()
392   {
393     /* Call common method to give check the hook */
394     $message= plugin::check();
396     /* Skip IP & Mac checks if this is a template */
397     if($this->cn != "default"){
398       $message= array_merge($message, $this->netConfigDNS->check());
399     }
401     /* Permissions for that base? */
402     $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
404     /* Set new acl base */
405     if($this->dn == "new") {
406       $this->set_acl_base($this->base);
407     }
409     $ui= get_userinfo();
410     if ($this->dn == "new" && !$this->acl_is_createable()){
411       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
412     }
414     if ($this->cn == ""){
415       $message[]= _("The required field 'Terminal name' is not set.");
416     }
418     if ($this->orig_dn == 'new'){
419       $ldap= $this->config->get_ldap_link();
420       $ldap->cd ($this->base);
422       /* It is possible to have a 'default' terminal on every base */
423       if($this->cn == "default"){
424         $ldap->cat($this->dn);
425       }else{
426         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
427       }
428       if ($ldap->count() != 0){
429         while ($attrs= $ldap->fetch()){
430           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
431             continue;
432           } else {
433             if ($attrs['dn'] != $this->orig_dn){
434               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
435               break;
436             }
437           }
438         }
439       }
440     }
442     /* Check for valid ntpServer selection */
443     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
444       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
445     }
447     return ($message);
448   }
451   /* Save to LDAP */
452   function save()
453   {
455     /* Move object if requested */
456     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
457       $this->move($this->orig_dn, $this->dn);
458     }
460     plugin::save();
462     /* Strip out 'default' values */
463     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
465       if ($this->attrs[$val] == "default"){
466         $this->attrs[$val]= array();
467       }
468     }
470     /* Add missing arrays */
471     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
472       if (isset ($this->$val) && count ($this->$val) != 0){
473         $this->attrs["$val"]= $this->$val;
474       }
475     }
477     /* Remove all empty values */
478     if ($this->orig_dn == 'new'){
479       $attrs= array();
480       foreach ($this->attrs as $key => $val){
481         if (is_array($val) && count($val) == 0){
482           continue;
483         }
484         $attrs[$key]= $val;
485       }
486       $this->attrs= $attrs;
487     }
489     /* Set ntpServers */
490     $this->attrs['gotoNtpServer'] = array();
491     if(!$this->inheritTimeServer){
492       foreach($this->gotoNtpServer as $server){
493         $this->attrs['gotoNtpServer'][] = $server;
494       }
495     }
497     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
498     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
499       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
500     }
502     /* Write back to ldap */
503     $ldap= $this->config->get_ldap_link();
504     if ($this->orig_dn == 'new'){
505       $ldap->cd($this->config->current['BASE']);
506       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
507       $ldap->cd($this->dn);
508       if (!count($this->attrs['gotoNtpServer'])){
509         unset($this->attrs['gotoNtpServer']);
510       }
511       $ldap->add($this->attrs);
512       $this->handle_post_events("add");
513     } else {
514       $ldap->cd($this->dn);
515       $this->cleanup();
516       $ldap->modify ($this->attrs); 
518       $this->handle_post_events("modify");
519     }
520     
521     /* cn=default and macAddress=- indicates that this is a template */
522     if($this->cn == "default"){
523       $this->netConfigDNS->macAddress = "-";
524     }
526     $this->netConfigDNS->cn = $this->cn;
527     $this->netConfigDNS->save($this->dn);
528     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
530     /* Optionally execute a command after we're done */
531     $this->postcreate();
532   }
535   /* Return plugin informations for acl handling */
536   function plInfo()
537   {
538     return (array(
539           "plShortName"   => _("Terminal"),
540           "plDescription" => _("Terminal generic"),
541           "plSelfModify"  => FALSE,
542           "plDepends"     => array(),
543           "plPriority"    => 1,
544           "plSection"     => array("administration"),
545           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
546                                                         "objectClass"  => "gotoTerminal")),
547           "plProvidedAcls"=> array(
548             "gotoMode"            => _("Mode"),
549             "gotoTerminalPath"    => _("Root server"),
550             "gotoSwapServer"      => _("Swap server"),
551             "gotoSyslogServer"    => _("Syslog server enabled"),
552             "gotoNtpServer"       => _("Ntp server settings"),
553             "base"                => _("Base"),
554             "cn"                  => _("Name"),
555             "gotoRootPasswd"      => _("Root password"),
556             "FAIstate"            => _("Action flag"))
557           ));
558   }
561 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
562 ?>