Code

Added admin stuff
[gosa.git] / gosa-plugins / goto / admin / systems / goto / 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");
58   var $fai_activated = FALSE;
59   var $view_logged = FALSE;
61   var $member_of_ogroup = FALSE;
63   function termgeneric (&$config, $dn= NULL, $parent= NULL)
64   {
65     /* Check if FAI is activated */
66     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
67     if(!empty($tmp)){
68       $this->fai_activated = TRUE;
69     }
71     plugin::plugin ($config, $dn, $parent);
73     if(!isset($this->parent->by_object['ogroup'])){
74       $ldap = $this->config->get_ldap_link();
75       $ldap->cd ($this->config->current['BASE']);
76       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
77       $this->member_of_ogroup = $ldap->count() >= 1;
78     }
80     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
81     /* Read arrays */
82     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
83       if (!isset($this->attrs[$val])){
84         continue;
85       }
86       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
87         array_push($this->$val, $this->attrs[$val][$i]);
88       }
89     }
91     /* Create used ntp server array */
92     $this->gotoNtpServer= array();
93     if(isset($this->attrs['gotoNtpServer'])){
94       $this->inheritTimeServer = false;
95       unset($this->attrs['gotoNtpServer']['count']);
96       foreach($this->attrs['gotoNtpServer'] as $server){
97         $this->gotoNtpServer[$server] = $server;
98       }
99     }
101     /* Set inherit checkbox state */
102     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
103       $this->inheritTimeServer = true;
104       $this->gotoNtpServer=array();
105     }
107     /* You can't inherit the NTP service, if we are not member in an object group */
108     if(!$this->member_of_ogroup){
109       $this->inheritTimeServer = FALSE;
110     }
112     /* Create available ntp options */
113     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
114     foreach($this->gotoNtpServers as $key => $server){
115       if($server == "default"){
116         unset($this->gotoNtpServers[$key]);
117       }
118     }
120     $this->modes["disabled"]= _("disabled");
121     $this->modes["text"]= _("text");
122     $this->modes["graphic"]= _("graphic");
124     /* Set base */
125     if ($this->dn == "new"){
126       $ui= get_userinfo();
127       $this->base= dn2base($ui->dn);
128     } else {
129       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
130     }
132     /* Create an array of all Syslog servers */
133     $tmp = $this->config->data['SERVERS']['SYSLOG'];
134     foreach($tmp as $server){
135       $visible = $server;
136       if($server == "default" && $this->member_of_ogroup) {
137         $visible = "["._("inherited")."]";
138       }
139       $this->gotoSyslogServers[$server] = $visible;
140     }
142     $this->orig_dn= $this->dn;
143   }
145   function set_acl_base($base)
146   {
147     plugin::set_acl_base($base);
148     $this->netConfigDNS->set_acl_base($base);
149   }
151   function set_acl_category($cat)
152   {
153     plugin::set_acl_category($cat);
154     $this->netConfigDNS->set_acl_category($cat);
155   }
157   function execute()
158   {
159     /* Call parent execute */
160     plugin::execute();
162     if($this->is_account && !$this->view_logged){
163       $this->view_logged = TRUE;
164       new log("view","terminal/".get_class($this),$this->dn);
165     }
167     /* Do we need to flip is_account state? */
168     if (isset($_POST['modify_state'])){
169       $this->is_account= !$this->is_account;
170     }
172     if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
174       /* Set FAIstate */
175       if($this->fai_activated && $this->dn != "new"){
176         $ldap = $this->config->get_ldap_link();
177         $ldap->cd($this->config->current['BASE']);
178         $ldap->cat($this->dn,array("objectClass"));
179         $res = $ldap->fetch();
181         $attrs = array();
182         $attrs['FAIstate'] = "";
183         if(isset($this->mapActions[$_POST['saction']])){
184           $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
185         }
187         for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
188           $attrs['objectClass'][] = $res['objectClass'][$i];
189         }
191         if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
192           $attrs['objectClass'][] = "FAIobject";
193         }
195         if($attrs['FAIstate'] == ""){
196 #FIXME we should check if FAIobject is used anymore
197           $attrs['FAIstate'] = array();
198         }
200         $ldap->cd($this->dn);
201         $ldap->modify($attrs);
202         show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
203       }
205       switch($_POST['saction']){
206         case 'wake':
207           $cmd= $this->config->search("termgeneric", "WAKECMD",array('tabs'));
209           if ($cmd == ""){
210             print_red(_("No WAKECMD definition found in your gosa.conf"));
211           } else {
212             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
213             if ($retval != 0){
214               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
215             }
216           }
217           break;
219         case 'reboot':
220           $cmd= $this->config->search("termgeneric", "REBOOTCMD",array('tabs'));
221           if ($cmd == ""){
222             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
223           } else {
224             exec ($cmd." ".$this->cn, $dummy, $retval);
225             if ($retval != 0){
226               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
227             }
228           }
229           break;
231         case 'halt':
232           $cmd= $this->config->search("termgeneric", "HALTCMD",array('tabs'));
233           if ($cmd == ""){
234             print_red(_("No HALTCMD definition found in your gosa.conf"));
235           } else {
236             exec ($cmd." ".$this->cn, $dummy, $retval);
237             if ($retval != 0){
238               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
239             }
240           }
241           break;
242       }
243     }
245     /* Base select dialog */
246     $once = true;
247     foreach($_POST as $name => $value){
248       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
249         $once = false;
250         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
251         $this->dialog->setCurrentBase($this->base);
252       }
253     }
255     /* Dialog handling */
256     if(is_object($this->dialog)){
257       /* Must be called before save_object */
258       $this->dialog->save_object();
260       if($this->dialog->isClosed()){
261         $this->dialog = false;
262       }elseif($this->dialog->isSelected()){
264         /* A new base was selected, check if it is a valid one */
265         $tmp = $this->get_allowed_bases();
266         if(isset($tmp[$this->dialog->isSelected()])){
267           $this->base = $this->dialog->isSelected();
268         }
269         $this->dialog= false;
270       }else{
271         return($this->dialog->execute());
272       }
273     }
275     /* Do we represent a valid terminal? */
276     if (!$this->is_account && $this->parent === NULL){
277       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
278         _("This 'dn' has no terminal features.")."</b>";
279       return($display);
280     }
282     /* Add new ntp Server to our list */
283     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
284       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
285     }
287     /* Delete selected NtpServer for list of used servers  */
288     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
289       foreach($_POST['gotoNtpServerSelected'] as $name){
290         unset($this->gotoNtpServer[$name]);
291       } 
292     }
294     /* Fill templating stuff */
295     $smarty= get_smarty();
296     
297     $tmp = $this->plInfo();
298     foreach($tmp['plProvidedAcls'] as $name => $translation){
299       $smarty->assign($name."ACL",$this->getacl($name));
300     }
302     $smarty->assign("cn", $this->cn);
303     $smarty->assign("staticAddress", "");
305     $smarty->assign("bases", $this->get_allowed_bases());
307     /* tell smarty the inherit checkbox state */
308     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
310     /* Check if terminal is online */
311     $query= "fping -q -r 1 -t 500 ".$this->cn;
312     exec ($query, $dummy, $retval);
314     /* Offline */
315     if ($retval == 0){
316       $smarty->assign("actions", array( "halt" => _("Switch off"), 
317                                         "reboot" => _("Reboot"),
318                                         "memcheck" => _("Memory test"),
319                                         "sysinfo"  => _("System analysis")));
320     } else {
321       $smarty->assign("actions", array("wake" => _("Wake up"),
322                                        "memcheck" => _("Memory test"),
323                                        "sysinfo"  => _("System analysis")));
324     }
326     /* Arrays */
327     $smarty->assign("modes", $this->modes);
329     $tmp2 = array(); 
330     foreach($this->config->data['SERVERS']['NFS'] as $server){
331       if($server != "default"){
332         $tmp2[$server]= $server;
333       }else{
334         if($this->member_of_ogroup){
335           $tmp2[$server]="["._("inherited")."]";
336         }
337       }
338     }
339   
340     $smarty->assign("nfsservers",     $tmp2);
341     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
343     $tmp = array();
344     foreach($this->gotoNtpServers as $server){
345       if(!in_array($server,$this->gotoNtpServer)){
346         $tmp[$server] = $server;
347       }
348     }
349     
350     $smarty->assign("ntpservers",     $tmp);
351     $smarty->assign("fai_activated",$this->fai_activated);
353     /* Variables */
354     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
355       $smarty->assign($val."_select", $this->$val);
356     }
358     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
360     /* Show main page */
361     $str = $this->netConfigDNS->execute();
362     if(is_object($this->netConfigDNS->dialog)){
363       return($str);
364     }
365     $smarty->assign("netconfig", $str);
366     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
367   }
369   function remove_from_parent()
370   {
371     if($this->acl_is_removeable()){   
372       $ldap= $this->config->get_ldap_link();
373       $ldap->cd($this->dn);
374       $ldap->cat($this->dn, array('dn'));
375       if($ldap->count()){
376         $this->netConfigDNS->remove_from_parent();
377         $ldap->rmDir($this->dn);
378   
379         new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
380   
381         show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
383         /* Optionally execute a command after we're done */
384         $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
386         /* Delete references to object groups */
387         $ldap->cd ($this->config->current['BASE']);
388         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
389         while ($ldap->fetch()){
390           $og= new ogroup($this->config, $ldap->getDN());
391           unset($og->member[$this->dn]);
392           $og->save ();
393         }
394       }
395     }
396   }
399   /* Save data to object */
400   function save_object()
401   {
402     /* Create a base backup and reset the
403        base directly after calling plugin::save_object();
404        Base will be set seperatly a few lines below */
405     $base_tmp = $this->base;
406     plugin::save_object();
407     $this->base = $base_tmp;
409     /* Set new base if allowed */
410     $tmp = $this->get_allowed_bases();
411     if(isset($_POST['base'])){
412       if(isset($tmp[$_POST['base']])){
413         $this->base= $_POST['base'];
414       }
415     }
416     
417     $this->netConfigDNS->save_object();
419     /* Save terminal path to parent since it is used by termstartup, too */
420     if(isset($this->parent->by_object['termstartup'])){
421       $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
422     }
423     
424     if(isset($_POST['termgeneric_posted'])){
425       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
426         $this->inheritTimeServer = true;
427       }else{
428         $this->inheritTimeServer = false;
429       }
430     }  
432     if(isset($_POST["inheritAll"])){
433       $this->set_everything_to_inherited();
434     }
436   }
439   /* Check supplied data */
440   function check()
441   {
442     /* Call common method to give check the hook */
443     $message= plugin::check();
445     /* Skip IP & Mac checks if this is a template */
446     if($this->cn != "default"){
447       $message= array_merge($message, $this->netConfigDNS->check());
448     }
450     /* Permissions for that base? */
451     $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
453     if ($this->cn == ""){
454       $message[]= _("The required field 'Terminal name' is not set.");
455     }
457     /* Check if given name is a valid host/dns name */
458     if(!tests::is_dns_name($this->cn) ){
459       $message[] = _("Please specify a valid name for this object.");
460     }
462     if ($this->orig_dn == 'new'){
463       $ldap= $this->config->get_ldap_link();
464       $ldap->cd ($this->base);
466       /* It is possible to have a 'default' terminal on every base */
467       if($this->cn == "default"){
468         $ldap->cat($this->dn);
469       }else{
470         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
471       }
472       if ($ldap->count() != 0){
473         while ($attrs= $ldap->fetch()){
474           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
475             continue;
476           } else {
477             if ($attrs['dn'] != $this->orig_dn){
478               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
479               break;
480             }
481           }
482         }
483       }
484     }
486     /* Check for valid ntpServer selection */
487     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
488       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
489     }
491     return ($message);
492   }
495   /* Save to LDAP */
496   function save()
497   {
499     /* Move object if requested */
500     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
501       $this->move($this->orig_dn, $this->dn);
502     }
504     plugin::save();
506     /* Strip out 'default' values */
507     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
508       if(isset($this->attrs[$val])){
509         if ($this->attrs[$val] == "default"){
510           $this->attrs[$val]= array();
511         }
512       }
513     }
515     /* Add missing arrays */
516     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
517       if (isset ($this->$val) && count ($this->$val) != 0){
518         $this->attrs["$val"]= $this->$val;
519       }
520     }
522     /* Remove all empty values */
523     if ($this->orig_dn == 'new'){
524       $attrs= array();
525       foreach ($this->attrs as $key => $val){
526         if (is_array($val) && count($val) == 0){
527           continue;
528         }
529         $attrs[$key]= $val;
530       }
531       $this->attrs= $attrs;
532     }
534     /* Set ntpServers */
535     $this->attrs['gotoNtpServer'] = array();
536     if(!$this->inheritTimeServer){
537       foreach($this->gotoNtpServer as $server){
538         $this->attrs['gotoNtpServer'][] = $server;
539       }
540     }
542     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
543     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
544       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
545     }
547     /* Write back to ldap */
548     $ldap= $this->config->get_ldap_link();
549     if ($this->orig_dn == 'new'){
550       $ldap->cd($this->config->current['BASE']);
551       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
552       $ldap->cd($this->dn);
553       if (!count($this->attrs['gotoNtpServer'])){
554         unset($this->attrs['gotoNtpServer']);
555       }
556       $ldap->add($this->attrs);
557       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
558       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
559     } else {
560       $ldap->cd($this->dn);
561       $this->cleanup();
562       $ldap->modify ($this->attrs); 
563       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
564       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
565     }
566     
567     /* cn=default and macAddress=- indicates that this is a template */
568     if($this->cn == "default"){
569       $this->netConfigDNS->macAddress = "-";
570     }
572     $this->netConfigDNS->cn = $this->cn;
573     $this->netConfigDNS->save();
574     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
575   }
581   /* Display generic part for server copy & paste */
582   function getCopyDialog()
583   {
584     $vars = array("cn");
585     $smarty = get_smarty();
586     $smarty->assign("cn" ,$this->cn);
587     $smarty->assign("object","terminal");
588     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
589     $ret = array();
590     $ret['string'] = $str;
591     $ret['status'] = "";
592     return($ret);
593   }
596   function saveCopyDialog()
597   {
598     if(isset($_POST['cn'])){
599       $this->cn = $_POST['cn'];
600     }
601   }
604   function PrepareForCopyPaste($source)
605   {
606     plugin::PrepareForCopyPaste($source);
607     if(isset($source['macAddress'][0])){
608       $this->netConfigDNS->macAddress = $source['macAddress'][0];
609     }
610     if(isset($source['ipHostNumber'][0])){
611       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
612     }
614     /* Create used ntp server array */
615     $this->gotoNtpServer= array();
616     if(isset($source['gotoNtpServer'])){
617       $this->inheritTimeServer = false;
618       unset($source['gotoNtpServer']['count']);
619       foreach($source['gotoNtpServer'] as $server){
620         $this->gotoNtpServer[$server] = $server;
621       }
622     }
624     /* Set inherit checkbox state */
625     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
626       $this->inheritTimeServer = true;
627       $this->gotoNtpServer=array();
628     }
629   }
632   /* Return plugin informations for acl handling */
633   static function plInfo()
634   {
635     return (array(
636           "plShortName"   => _("Terminal"),
637           "plDescription" => _("Terminal generic"),
638           "plSelfModify"  => FALSE,
639           "plDepends"     => array(),
640           "plPriority"    => 1,
641           "plSection"     => array("administration"),
642           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
643                                                         "objectClass"  => "gotoTerminal")),
644           "plProvidedAcls"=> array(
645             "gotoMode"            => _("Mode"),
646             "gotoTerminalPath"    => _("Root server"),
647             "gotoSwapServer"      => _("Swap server"),
648             "gotoSyslogServer"    => _("Syslog server enabled"),
649             "gotoNtpServer"       => _("Ntp server settings"),
650             "base"                => _("Base"),
651             "cn"                  => _("Name"),
652             "gotoRootPasswd"      => _("Root password"),
653             "FAIstate"            => _("Action flag"))
654           ));
655   }
658   function set_everything_to_inherited()
659   {
660     $this->gotoTerminalPath  = "default";
661     $this->gotoSwapServer    = "default" ;
662     $this->gotoSyslogServer  = "default";
663     $this->inheritTimeServer = TRUE;
665     /* Set workstation service attributes to inherited */
666     if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
667       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
668             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
669         $this->parent->by_object['termservice']->$name = "default";
670       }
671     }
673     /* Set workstation startup attributes to inherited */
674     if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
675       $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
676       $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
677     }
678   }
681 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
682 ?>