Code

Enable inheritance of XDriver
[gosa.git] / trunk / gosa-plugins / goto / admin / systems / goto / class_workstationService.inc
1 <?php
3 class workservice extends plugin
4 {
5   var $gotoScannerEnable;
7   /* Generic terminal attributes */
8   var $gotoXMonitor= "";
9   var $gotoXDriver= "";
10   var $gotoXResolution= "";
11   var $gotoXColordepth= "";
12   var $gotoXHsync= "";
13   var $gotoXVsync= "";
14   var $AutoSync = false;
15   var $gotoXKbModel= "";
16   var $gotoXKbLayout= "";
17   var $gotoXKbVariant= "";
18   var $gotoXMouseType= "";
19   var $gotoXMouseport= "";
20   var $gotoScannerClients= "";
21   var $gotoScannerBackend= "";
22   var $goFonHardware= "automatic";
23   var $view_logged = FALSE;
25   /* Needed values and lists */
26   var $ignore_account= TRUE;
27   var $base= "";
28   var $cn= "";
29   var $orig_dn= "";
30   var $XDrivers= array();
31   var $XResolutions = array();
32   var $MouseTypes= array();
33   var $MousePorts= array();
34   var $hardware_list= array();
35   var $used_hardware= array();
38   /* attribute list for save action */
39   var $attributes= array("gotoXMonitor", "gotoXDriver", "gotoXResolution", "gotoXColordepth",
40       "gotoXHsync", "gotoXVsync",
41       "gotoScannerEnable", "gotoScannerClients",
42       "gotoScannerBackend", "gotoXKbModel", "gotoXKbLayout", "gotoXKbVariant",
43       "gotoXMouseType", "gotoXMouseport", "goFonHardware");
44   var $objectclasses= array("GOhard");
46   var $XColordepths     =array();
47   var $XKbModels        =array();
48   var $XKbLayouts       =array();
49   var $XKbVariants      =array();
51   function workservice (&$config, $dn= NULL, $parent= NULL)
52   {
53     plugin::plugin ($config, $dn, $parent);
55     $this->XResolutions= array( 
56         "640x480"   =>  "640x480",
57         "800x600"   =>  "800x600",
58         "1024x768"  =>  "1024x768",
59         "1152x864"  =>  "1152x864", 
60         "1280x1024" =>  "1280x1024",
61         "1400x1050" =>  "1400x1050", 
62         "1600x1200" =>  "1600x1200");
64     if($this->config->get_cfg_value("resolutions") != ""){
65       $file = $this->config->get_cfg_value("resolutions");
67       if(is_readable($file)){
68         $str = file_get_contents($file);
69         $lines = split("\n",$str);
70         foreach($lines as $line){
71           $line = trim($line);
72           if(!empty($line)){
73             $this->XResolutions[$line]=$line;
74           }
75         }
76         //natcasesort($this->gotoXResolutions);
77       }else{
78         msg_dialog::display(_("Configuration error"), msgPool::cannotReadFile($file), WARNING_DIALOG);
79       }
80     }
82     /* Get list of available xdrivers */
83     $this->XDrivers = $this->getListOfXDrivers();
85     array_unshift($this->XDrivers, "["._("unknown")."]");
86  
87     $this->XColordepths= array( 
88         "8"        => "8 " ._("bit"), 
89         "15"       => "15 "._("bit"),      
90         "16"       => "16 "._("bit"),   
91         "24"       => "24 "._("bit"));
93     foreach(array ("btc9000", "chicony", "compaq", "dell", "dell101", "everex",
94           "flexpro", "geniuscomfy", "hp", "itouch", "jp106", "logicordless",
95           "logiinetnav", "logiinternet", "macintosh", "microsoft",
96           "microsoftpro", "omnikey101", "pc101", "pc102", "pc104",
97           "pc105", "rapidaccess", "rapidaccess2", "winbook") as $type){
98       $this->XKbModels[$type] = $type;
99     }
101     $this->MouseTypes= array("auto" => "auto",
102                              "explorerps/2" => "explorerps/2",
103                              "ImPS/2" => "ImPS/2",
104                              "PS/2" => "PS/2",
105                              "Microsoft" => "Microsoft",
106                              "Logitech" => "Logitech",);
108     $this->MousePorts= array("/dev/input/mice" => "/dev/input/mice",
109                              "/dev/mouse" => "/dev/mouse",
110                              "/dev/psaux" => "/dev/psaux",
111                              "/dev/ttyS0" => "/dev/ttyS0",
112                              "/dev/ttyS1" => "/dev/ttyS1",);
114     /* Additional values will be extracted from CONFIG_DIR./keyboardLayouts */
115     $this->XKbLayouts = array("de"=> "de","en" =>"en", "es" => "es", "us" =>"us", "fr" => "fr");
116     $this->XKbVariants= array ("nodeadkeys"=>"nodeadkeys", "basic"=>"basic");
118     /* try to read additional keyboard layouts 
119      */
120     if(file_exists(CONFIG_DIR."/keyboardLayouts")){
121       if(is_readable(CONFIG_DIR."/keyboardLayouts")){
122         $str = file_get_contents(CONFIG_DIR."/keyboardLayouts");
123         $tmp = split("\n",$str);
124         foreach($tmp as $entry){
125           if((!empty($entry)) && (!preg_match("/^#/",$entry))){
126             $entry = trim($entry);
127             $tmp2 = split ("\:",$entry);
128             $la =   trim($tmp2[0]);   // What would be saved to ldap
129             $da =   trim($tmp2[1]);   // This wis displayed in the listbox
130             $this->XKbLayouts [ $la] = $da;  
131           } 
132         }
133       }
134     }
136     $this->orig_dn= $this->dn;
138     /* Load phone hardware list
139      */
140     $tmp = get_sub_list("(objectClass=goFonHardware)","",array(get_ou("phoneRDN")),
141                   $this->config->current['BASE'],array("cn","description"), GL_NO_ACL_CHECK);
142     foreach($tmp as $attrs){
143       $cn= $attrs['cn'][0];
144       $description= "";
145       if (isset($attrs['description'])){
146         $description= " - ".$attrs['description'][0];
147       }
148       $this->hardware_list[$cn]= "$cn$description";
149     }
150     $this->hardware_list["automatic"]= _("automatic");
151     ksort($this->hardware_list);
153     /* These departments may contain objects that have
154         goFonHardware set.
155      */
156     $deps_a = array(
157         get_people_ou(),
158         get_ou("ogroupRDN"),
159         get_ou("serverRDN"),
160         get_ou("terminalRDN"),
161         get_ou("workstationRDN"),
162         get_ou("printerRDN"),
163         get_ou("componentRDN"),
164         get_ou("phoneRDN"));
166     $tmp = get_sub_list("(goFonHardware=*)","",$deps_a,$this->config->current['BASE'],
167         array('cn','dn','goFonHardware'),GL_NO_ACL_CHECK);
168     foreach($tmp as $attrs){
169       $cn = $attrs['goFonHardware'][0];
170       if(isset($this->hardware_list[$cn])){
171         $this->used_hardware[$cn]= $cn;
172       }
173     }
175     if(preg_match("/\+/",$this->gotoXHsync)){
176       $this->AutoSync = true;
177       $this->gotoXHsync = preg_replace("/\+/","-",$this->gotoXHsync);
178       $this->gotoXVsync = preg_replace("/\+/","-",$this->gotoXVsync);
179     }
181     /* Load hardware list */
182     $ldap= $this->config->get_ldap_link();
183     $ldap->cd($this->config->current['BASE']);
184     $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))");
185     if ($ldap->count() == 1){
186       $map= array("gotoXResolution", "gotoXColordepth", "gotoXKbModel", "gotoXKbLayout",
187                   "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport", "gotoXDriver");
188       $attrs= $ldap->fetch();
190       foreach ($map as $name){
191         if (!isset($attrs[$name][0])){
192           continue;
193         }
194         
195         switch ($name){
196           case 'gotoXDriver':
197             $this->XDrivers = array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XDrivers;
198             break;
199           case 'gotoXResolution':
200             $this->XResolutions= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XResolutions;
201             break;
202           case 'gotoXColordepth':
203             $this->XColordepths= array('default' => _("inherited").' ['.$attrs[$name][0].' '._('Bit').']') + $this->XColordepths;
204             break;
205           case 'gotoXKbModel':
206             $this->XKbModels= array('default' => _("inherited").' ['.$attrs[$name][0].']') + $this->XKbModels;
207             break;
208           case 'gotoXKbLayout':
209             $this->XKbLayouts= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XKbLayouts;
210             break;
211           case 'gotoXKbVariant':
212             $this->XKbVariants= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XKbVariants;
213             break;
214           case 'gotoXMouseType':
215             $this->MouseTypes= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->MouseTypes;
216             break;
217           case 'gotoXMouseport':
218             $this->MousePorts= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->MousePorts;
219             break;
220         }
222       }
224     }
226     /* Workaround to fill in inherited values if we've specified an objectclass */
227     $SelectedSystemType = session::get("SelectedSystemType");
228     if (isset($SelectedSystemType['ogroup']) && $SelectedSystemType['ogroup'] != 'none'){
229       $this->XDrivers=array('default' => _("inherited"));
230       $this->XResolutions= array('default' => _("inherited"));
231       $this->XColordepths= array('default' => _("inherited"));
232       $this->XKbModels= array('default' => _("inherited"));
233       $this->XKbLayouts= array('default' => _("inherited"));
234       $this->XKbVariants= array('default' => _("inherited"));
235       $this->MouseTypes= array('default' => _("inherited"));
236       $this->MousePorts= array('default' => _("inherited"));
237     }
238   }
240   function execute()
241   {
242     /* Call parent execute */
243     plugin::execute();
245     if($this->is_account && !$this->view_logged){
246       $this->view_logged = TRUE;
247       new log("view","workstation/".get_class($this),$this->dn);
248     }
250     /* Do we need to flip is_account state? */
251     if (isset($_POST['modify_state'])){
252       $this->is_account= !$this->is_account;
253     }
255     /* Do we need to flip is_account state? */
256     if(isset($_POST['modify_state'])){
257       if($this->is_account && $this->acl_is_removeable()){
258         $this->is_account= FALSE;
259       }elseif(!$this->is_account && $this->acl_is_createable()){
260         $this->is_account= TRUE;
261       }
262     }
264     /* Show main page */
265     $smarty= get_smarty();
267     /* Assign ACLs */
268     $tmp = $this->plInfo();
269     foreach($tmp['plProvidedAcls'] as $name => $translated){
270       $smarty->assign($name."ACL",$this->getacl($name));
271     }
273     /* Arrays */ 
274     foreach(array("XDrivers", "XResolutions", "XColordepths",
275           "XKbModels", "XKbVariants",
276           "MouseTypes", "MousePorts") as $val){
277       $smarty->assign("$val", $this->$val);
278     }
279     $smarty->assign("XKbLayouts"    ,$this->XKbLayouts);
280     $smarty->assign("XKbLayoutKeys" ,array_flip($this->XKbLayouts));
282     $smarty->assign("xdmcpservers", $this->config->data['SERVERS']['TERMINAL']);
283     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
284     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
286     /* Variables - select */
287     foreach(array(
288           "gotoXDriver", "gotoXResolution", "gotoXColordepth", 
289           "gotoXKbModel", "gotoXKbLayout","gotoScannerEnable",
290           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
292       $smarty->assign($val."_select", $this->$val);
293     }
295     /* Variables */
296     foreach(array("gotoXHsync", "gotoXVsync") as $val){
297       $smarty->assign($val, $this->$val);
298     }
299     $smarty->assign("staticAddress", "");
301     /* Checkboxes */
302     foreach(array("gotoScannerEnable") as $val){
303       if ($this->$val == TRUE) {
304         $smarty->assign("$val", "checked");
305       } else {
306         $smarty->assign("$val", "");
307       }
308     }
310     /* Phone stuff */
311     $smarty->assign ("goFonHardware", $this->goFonHardware);
313     $perms = "";
314     if(!$this->acl_is_writeable("goFonHardware")){
315       $perms = " disabled ";
316     }
318     $hl= "<select size=\"1\" name=\"goFonHardware\" ".$perms." title=\"".
319       _("Choose the phone located at the current terminal")."\" >";
320     foreach ($this->hardware_list as $cn => $description){
321       if ($cn == $this->goFonHardware){
322         $selected= "selected";
323       } else {
324         $selected= "";
325       }
326       if (isset($this->used_hardware[$cn])){
327         $color= "style=\"color:#A0A0A0\"";
328       } else {
329         $color= "";
330       }
331       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
332     }
333     $hl.= "</select>\n";
334     $smarty->assign ("hardware_list", $hl);
335     $smarty->assign ("gotoXMonitor", $this->gotoXMonitor);
337     if($this->AutoSync){
338       $smarty->assign("AutoSyncCHK"," checked ");
339       $smarty->assign("hiddenState"," disabled ");
340     }else{
341       $smarty->assign("AutoSyncCHK"," ");
342       $smarty->assign("hiddenState","");
343     }
345     /* Show main page */
346     return($smarty->fetch (get_template_path('workstationService.tpl',TRUE,dirname(__FILE__))));
347   }
349   function remove_from_parent()
350   {
351     $this->handle_post_events("remove");
352     new log("remove","workstation/".get_class($this),$this->dn);
353   }
355   /* Save data to object */
356   function save_object()
357   {
358     plugin::save_object();
360     if((isset($_POST['workservicePosted'])) && $this->acl_is_writeable("AutoSync")) {
361       if(isset($_POST['AutoSync'])){
362         $this->AutoSync = true;
363       }else{
364         $this->AutoSync = false;
365       }
366     }
367   }
369   /* Check supplied data */
370   function check()
371   {
372     /* Call common method to give check the hook */
373     $message= plugin::check();
375     /* Default entries can use blank hsync/vsync entries */
376     if ($this->dn != "" && $this->cn != "default" && $this->cn != "wdefault"){
378       /* But only if no auto sync is enabled... */
379       if (!$this->AutoSync){
381         /* Check vsync for correct usage */
382         $val= preg_replace ("/\s/", "", $this->gotoXVsync);
384         if($this->acl_is_writeable("gotoXVsync")){
385           if(empty($val)){
386             $message[]= msgPool::required(_("VSync"));
387           }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
388             $message[]= msgPool::invalid(_("VSync"));
389           } else{
390             list($v1,$v2)= preg_split ("/[-+]/", $val);
391             if ($v2 != ""){
392               if ($v1 > $v2){
393                 $message[]= msgPool::invalid(_("VSync"));
394               }
395             }
396           }
397         }
399         /* Check hsync for correct usage */
400         $val= preg_replace ("/\s/", "", $this->gotoXHsync);
401         if($this->acl_is_writeable("gotoXHsync")){
402           if(empty($val)){
403             $message[]= msgPool::required(_("HSync"));
404           }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
405             $message[]= msgPool::invalid(_("HSync"));
406           } else{
407             list($v1,$v2)= preg_split ("/[-+]/", $val);
408             if ($v2 != ""){
409               if ($v1 > $v2){
410                 $message[]= msgPool::invalid(_("HSync"));
411               }
412             }
413           }
414         }
415       }
416     }
418     return ($message);
419   }
422   /* Save to LDAP */
423   function save()
424   {
425     /* remove objectclass GOhard if this is an ogroup tab */
426     if(isset($this->parent->by_object['ogroup'])){
427       $this->objectclasses = array();
428     }
430     plugin::save();
432     /* Strip out 'default' values */
433     foreach(array(
434           "gotoXDriver", "gotoXResolution", "gotoXColordepth",
435           "gotoXKbModel", "gotoXKbLayout", "gotoXDriver",
436           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
438       if ($this->attrs[$val] == "default"){
439         $this->attrs[$val]= array();
440       }
441     }
443     if($this->AutoSync){
444       $this->attrs['gotoXHsync'] = "30+55";
445       $this->attrs['gotoXVsync'] = "50+70";
446     }
448     /* Write back to ldap */
449     $ldap= $this->config->get_ldap_link();
450     $ldap->cd($this->dn);
451     $this->cleanup();
454     $ldap->modify ($this->attrs); 
455     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
456     if (!$ldap->success()){
457       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
458     }
459     $this->handle_post_events("modify");
461     /* Send goto reload event to gosaSupportDaemon */
462     if(count($this->attrs)){
463       $this->send_goto_reload(); 
464     }
466   }
469   function getListOfXDrivers()
470   {
471     /* Generate a list of xdrivers from CONFIG_DIR./xdrivers */
472     $drivers = array();
473     if (file_exists(CONFIG_DIR.'/xdrivers')){
474       $xdrivers = file (CONFIG_DIR.'/xdrivers');
475       foreach ($xdrivers as $line){
476         if (!preg_match ("/^#/", $line)){
477           $drivers[]= trim($line);
478         }
479       }
480     } else {
481       $drivers = array("ati", "atimisc", "chips", "cirrus", "cyrix", "fbdev", "fglrx",
482           "i128", "i740", "i810", "intel", "imstt", "mga", "neomagic", "newport", "nsc",  "nv", "nvidia",
483           "r128", "radeon", "rendition", "s3", "s3virge", "savage", "siliconmotion",
484           "sis", "tdfx", "tga", "trident", "tseng", "vboxvideo", "vesa", "vga", "vmware");
485     }
486     return($drivers);
487   }
490   /* Return plugin informations for acl handling */
491   static function plInfo()
492   {
493     return (array(
494           "plShortName"   => _("Service"),
495           "plDescription" => _("Workstation service"),
496           "plSelfModify"  => FALSE,
497           "plDepends"     => array(),
498           "plPriority"    => 10,
499           "plSection"     => array("administration"),
500           "plCategory"    => array("workstation","ogroups"), 
502           "plProvidedAcls"=> array(
503             "gotoXMonitor"          => _("Monitor"),
504             "gotoXDriver"           => _("Gfx driver"),
505             "gotoXResolution"       => _("Gfx resolution"),
506             "gotoXColordepth"       => _("Gfx color depth"),
507             "gotoXHsync"            => _("HSync"),
508             "gotoXVsync"            => _("VSync"),
509             "AutoSync"              => _("Use DDC"),
510             "gotoScannerEnable"     => _("Scanner enabled"),
511             "gotoXKbModel"          => _("Keyboard model"),
512             "gotoXKbLayout"         => _("Keyboard layout"),
513             "gotoXKbVariant"        => _("Keyboard variant"),
514             "gotoXMouseType"        => _("Mouse type"),
515             "gotoXMouseport"        => _("Mouse port"),
516             "goFonHardware"         => _("Telephone hardware")) 
517             ));
518   }
521   /*! \brief  Send goto_reload event to support daemon
522    */
523   function send_goto_reload()
524   {
525     if(count($this->attrs) && class_available("DaemonEvent")){
526       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
527       $o_queue = new gosaSupportDaemon();
528       if(isset($events['TRIGGERED']['DaemonEvent_goto_reload'])){
529         $evt = $events['TRIGGERED']['DaemonEvent_goto_reload'];
530         $macs = array();
532         /* Get list of macAddresses 
533          */
534         if(isset($this->parent->by_object['ogroup'])){
536           /* If we are an object group, add all member macs 
537            */
538           $p = $this->parent->by_object['ogroup'];
539           foreach($p->memberList as $dn => $obj){
540             if(preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$dn)) continue;
541             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
542               $macs[] = $p->objcache[$dn]['macAddress'];
543             }
544           }
545         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
547           /* We are a workstation. Add current mac.
548            */
549           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
550           if(!empty($mac) && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$this->orig_dn)){
551             $macs[] = $mac;
552           }          
553         }
555         /* Trigger event for all member objects 
556          */
557         if(count($macs)){
558           $tmp = new $evt['CLASS_NAME']($this->config);
559           $tmp->set_type(TRIGGERED_EVENT);
560           $target = $o_queue->get_host().":".$o_queue->get_port();
561           $tmp->add_targets(array($target));
562           $tmp->set_macs($macs);
563           if(!$o_queue->append($tmp,TRUE)){
564             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
565           }
566         }
567       }
568     }
569   }
572   function PrepareForCopyPaste($source)
573   {
574     plugin::PrepareForCopyPaste($source);
576     if(preg_match("/\+/",$this->gotoXHsync)){
577       $this->AutoSync = true;
578       $this->gotoXHsync = preg_replace("/\+/","-",$this->gotoXHsync);
579       $this->gotoXVsync = preg_replace("/\+/","-",$this->gotoXVsync);
580     }
581   }
584 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
585 ?>