Code

4e122d898bf859c892051095fa47f56c8ae8bc56
[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     foreach($this->getListOfXDrivers() as $xdriver) {
84       $this->XDrivers[$xdriver] = $xdriver;
85     }
87     if (isset($this->attrs['gosaGroupObjects'])) {
88       array_unshift($this->XDrivers, "["._("manual/auto")."]");
89     }
90     else {
91       array_unshift($this->XDrivers, "["._("unknown")."]");
92     }
93      
94     $this->XColordepths= array( 
95         "8"        => "8 " ._("bit"), 
96         "15"       => "15 "._("bit"),      
97         "16"       => "16 "._("bit"),   
98         "24"       => "24 "._("bit"));
100     foreach(array ("btc9000", "chicony", "compaq", "dell", "dell101", "everex",
101           "flexpro", "geniuscomfy", "hp", "itouch", "jp106", "logicordless",
102           "logiinetnav", "logiinternet", "macintosh", "microsoft",
103           "microsoftpro", "omnikey101", "pc101", "pc102", "pc104",
104           "pc105", "rapidaccess", "rapidaccess2", "winbook") as $type){
105       $this->XKbModels[$type] = $type;
106     }
108     $this->MouseTypes= array("auto" => "auto",
109                              "explorerps/2" => "explorerps/2",
110                              "ImPS/2" => "ImPS/2",
111                              "PS/2" => "PS/2",
112                              "Microsoft" => "Microsoft",
113                              "Logitech" => "Logitech",);
115     $this->MousePorts= array("/dev/input/mice" => "/dev/input/mice",
116                              "/dev/mouse" => "/dev/mouse",
117                              "/dev/psaux" => "/dev/psaux",
118                              "/dev/ttyS0" => "/dev/ttyS0",
119                              "/dev/ttyS1" => "/dev/ttyS1",);
121     /* Additional values will be extracted from CONFIG_DIR./keyboardLayouts */
122     $this->XKbLayouts = array("de"=> "de","en" =>"en", "es" => "es", "us" =>"us", "fr" => "fr");
123     $this->XKbVariants= array ("nodeadkeys"=>"nodeadkeys", "basic"=>"basic");
125     /* try to read additional keyboard layouts 
126      */
127     if(file_exists(CONFIG_DIR."/keyboardLayouts")){
128       if(is_readable(CONFIG_DIR."/keyboardLayouts")){
129         $str = file_get_contents(CONFIG_DIR."/keyboardLayouts");
130         $tmp = split("\n",$str);
131         foreach($tmp as $entry){
132           if((!empty($entry)) && (!preg_match("/^#/",$entry))){
133             $entry = trim($entry);
134             $tmp2 = split ("\:",$entry);
135             $la =   trim($tmp2[0]);   // What would be saved to ldap
136             $da =   trim($tmp2[1]);   // This wis displayed in the listbox
137             $this->XKbLayouts [ $la] = $da;  
138           } 
139         }
140       }
141     }
143     $this->orig_dn= $this->dn;
145     /* Load phone hardware list
146      */
147     $tmp = get_sub_list("(objectClass=goFonHardware)","",array(get_ou("phoneRDN")),
148                   $this->config->current['BASE'],array("cn","description"), GL_NO_ACL_CHECK);
149     foreach($tmp as $attrs){
150       $cn= $attrs['cn'][0];
151       $description= "";
152       if (isset($attrs['description'])){
153         $description= " - ".$attrs['description'][0];
154       }
155       $this->hardware_list[$cn]= "$cn$description";
156     }
157     $this->hardware_list["automatic"]= _("automatic");
158     ksort($this->hardware_list);
160     /* These departments may contain objects that have
161         goFonHardware set.
162      */
163     $deps_a = array(
164         get_people_ou(),
165         get_ou("ogroupRDN"),
166         get_ou("serverRDN"),
167         get_ou("terminalRDN"),
168         get_ou("workstationRDN"),
169         get_ou("printerRDN"),
170         get_ou("componentRDN"),
171         get_ou("phoneRDN"));
173     $tmp = get_sub_list("(goFonHardware=*)","",$deps_a,$this->config->current['BASE'],
174         array('cn','dn','goFonHardware'),GL_NO_ACL_CHECK);
175     foreach($tmp as $attrs){
176       $cn = $attrs['goFonHardware'][0];
177       if(isset($this->hardware_list[$cn])){
178         $this->used_hardware[$cn]= $cn;
179       }
180     }
182     if(preg_match("/\+/",$this->gotoXHsync)){
183       $this->AutoSync = true;
184       $this->gotoXHsync = preg_replace("/\+/","-",$this->gotoXHsync);
185       $this->gotoXVsync = preg_replace("/\+/","-",$this->gotoXVsync);
186     }
188     /* Load hardware list */
189     $ldap= $this->config->get_ldap_link();
190     $ldap->cd($this->config->current['BASE']);
191     $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))");
192     if ($ldap->count() == 1){
193       $map= array("gotoXResolution", "gotoXColordepth", "gotoXKbModel", "gotoXKbLayout",
194                   "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport", "gotoXDriver");
195       $attrs= $ldap->fetch();
197       foreach ($map as $name){
198         if (!isset($attrs[$name][0])){
199           continue;
200         }
201         
202         switch ($name){
203           case 'gotoXDriver':
204             $ogroup_driver = $attrs['gotoXDriver'][0];
205             if (in_array($ogroup_driver, $this->XDrivers) && (empty($this->attrs['gotoXDriver']))) {
206                 $this->XDrivers = array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XDrivers;
207             }
208             break;
209           case 'gotoXResolution':
210             $this->XResolutions= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XResolutions;
211             break;
212           case 'gotoXColordepth':
213             $this->XColordepths= array('default' => _("inherited").' ['.$attrs[$name][0].' '._('Bit').']') + $this->XColordepths;
214             break;
215           case 'gotoXKbModel':
216             $this->XKbModels= array('default' => _("inherited").' ['.$attrs[$name][0].']') + $this->XKbModels;
217             break;
218           case 'gotoXKbLayout':
219             $this->XKbLayouts= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XKbLayouts;
220             break;
221           case 'gotoXKbVariant':
222             $this->XKbVariants= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XKbVariants;
223             break;
224           case 'gotoXMouseType':
225             $this->MouseTypes= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->MouseTypes;
226             break;
227           case 'gotoXMouseport':
228             $this->MousePorts= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->MousePorts;
229             break;
230         }
232       }
234     }
236     /* Workaround to fill in inherited values if we've specified an objectclass */
237     $SelectedSystemType = session::get("SelectedSystemType");
238     if (isset($SelectedSystemType['ogroup']) && $SelectedSystemType['ogroup'] != 'none'){
239       $this->XDrivers=array('default' => _("inherited"));
240       $this->XResolutions= array('default' => _("inherited"));
241       $this->XColordepths= array('default' => _("inherited"));
242       $this->XKbModels= array('default' => _("inherited"));
243       $this->XKbLayouts= array('default' => _("inherited"));
244       $this->XKbVariants= array('default' => _("inherited"));
245       $this->MouseTypes= array('default' => _("inherited"));
246       $this->MousePorts= array('default' => _("inherited"));
247     }
248   }
250   function execute()
251   {
252     /* Call parent execute */
253     plugin::execute();
255     if($this->is_account && !$this->view_logged){
256       $this->view_logged = TRUE;
257       new log("view","workstation/".get_class($this),$this->dn);
258     }
260     /* Do we need to flip is_account state? */
261     if (isset($_POST['modify_state'])){
262       $this->is_account= !$this->is_account;
263     }
265     /* Do we need to flip is_account state? */
266     if(isset($_POST['modify_state'])){
267       if($this->is_account && $this->acl_is_removeable()){
268         $this->is_account= FALSE;
269       }elseif(!$this->is_account && $this->acl_is_createable()){
270         $this->is_account= TRUE;
271       }
272     }
274     /* Show main page */
275     $smarty= get_smarty();
277     /* Assign ACLs */
278     $tmp = $this->plInfo();
279     foreach($tmp['plProvidedAcls'] as $name => $translated){
280       $smarty->assign($name."ACL",$this->getacl($name));
281     }
283     /* Arrays */ 
284     foreach(array("XDrivers", "XResolutions", "XColordepths",
285           "XKbModels", "XKbVariants",
286           "MouseTypes", "MousePorts") as $val){
287       $smarty->assign("$val", $this->$val);
288     }
289     $smarty->assign("XKbLayouts"    ,$this->XKbLayouts);
290     $smarty->assign("XKbLayoutKeys" ,array_flip($this->XKbLayouts));
292     $smarty->assign("xdmcpservers", $this->config->data['SERVERS']['TERMINAL']);
293     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
294     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
296     /* Variables - select */
297     foreach(array(
298           "gotoXDriver", "gotoXResolution", "gotoXColordepth", 
299           "gotoXKbModel", "gotoXKbLayout","gotoScannerEnable",
300           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
302       $smarty->assign($val."_select", $this->$val);
303     }
305     /* Variables */
306     foreach(array("gotoXHsync", "gotoXVsync") as $val){
307       $smarty->assign($val, $this->$val);
308     }
309     $smarty->assign("staticAddress", "");
311     /* Checkboxes */
312     foreach(array("gotoScannerEnable") as $val){
313       if ($this->$val == TRUE) {
314         $smarty->assign("$val", "checked");
315       } else {
316         $smarty->assign("$val", "");
317       }
318     }
320     /* Phone stuff */
321     $smarty->assign ("goFonHardware", $this->goFonHardware);
323     $perms = "";
324     if(!$this->acl_is_writeable("goFonHardware")){
325       $perms = " disabled ";
326     }
328     $hl= "<select size=\"1\" name=\"goFonHardware\" ".$perms." title=\"".
329       _("Choose the phone located at the current terminal")."\" >";
330     foreach ($this->hardware_list as $cn => $description){
331       if ($cn == $this->goFonHardware){
332         $selected= "selected";
333       } else {
334         $selected= "";
335       }
336       if (isset($this->used_hardware[$cn])){
337         $color= "style=\"color:#A0A0A0\"";
338       } else {
339         $color= "";
340       }
341       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
342     }
343     $hl.= "</select>\n";
344     $smarty->assign ("hardware_list", $hl);
345     $smarty->assign ("gotoXMonitor", $this->gotoXMonitor);
347     if($this->AutoSync){
348       $smarty->assign("AutoSyncCHK"," checked ");
349       $smarty->assign("hiddenState"," disabled ");
350     }else{
351       $smarty->assign("AutoSyncCHK"," ");
352       $smarty->assign("hiddenState","");
353     }
355     /* Show main page */
356     return($smarty->fetch (get_template_path('workstationService.tpl',TRUE,dirname(__FILE__))));
357   }
359   function remove_from_parent()
360   {
361     $this->handle_post_events("remove");
362     new log("remove","workstation/".get_class($this),$this->dn);
363   }
365   /* Save data to object */
366   function save_object()
367   {
368     plugin::save_object();
370     if((isset($_POST['workservicePosted'])) && $this->acl_is_writeable("AutoSync")) {
371       if(isset($_POST['AutoSync'])){
372         $this->AutoSync = true;
373       }else{
374         $this->AutoSync = false;
375       }
376     }
377   }
379   /* Check supplied data */
380   function check()
381   {
382     /* Call common method to give check the hook */
383     $message= plugin::check();
385     /* Default entries can use blank hsync/vsync entries */
386     if ($this->dn != "" && $this->cn != "default" && $this->cn != "wdefault"){
388       /* But only if no auto sync is enabled... */
389       if (!$this->AutoSync){
391         /* Check vsync for correct usage */
392         $val= preg_replace ("/\s/", "", $this->gotoXVsync);
394         if($this->acl_is_writeable("gotoXVsync")){
395           if(empty($val)){
396             $message[]= msgPool::required(_("VSync"));
397           }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
398             $message[]= msgPool::invalid(_("VSync"));
399           } else{
400             list($v1,$v2)= preg_split ("/[-+]/", $val);
401             if ($v2 != ""){
402               if ($v1 > $v2){
403                 $message[]= msgPool::invalid(_("VSync"));
404               }
405             }
406           }
407         }
409         /* Check hsync for correct usage */
410         $val= preg_replace ("/\s/", "", $this->gotoXHsync);
411         if($this->acl_is_writeable("gotoXHsync")){
412           if(empty($val)){
413             $message[]= msgPool::required(_("HSync"));
414           }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
415             $message[]= msgPool::invalid(_("HSync"));
416           } else{
417             list($v1,$v2)= preg_split ("/[-+]/", $val);
418             if ($v2 != ""){
419               if ($v1 > $v2){
420                 $message[]= msgPool::invalid(_("HSync"));
421               }
422             }
423           }
424         }
425       }
426     }
428     return ($message);
429   }
432   /* Save to LDAP */
433   function save()
434   {
435     /* remove objectclass GOhard if this is an ogroup tab */
436     if(isset($this->parent->by_object['ogroup'])){
437       $this->objectclasses = array();
438     }
440     plugin::save();
442     /* Strip out 'default' values */
443     foreach(array(
444           "gotoXDriver", "gotoXResolution", "gotoXColordepth",
445           "gotoXKbModel", "gotoXKbLayout", "gotoXDriver",
446           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
448       if ($this->attrs[$val] == "default"){
449         $this->attrs[$val]= array();
450       }
451     }
453     if($this->AutoSync){
454       $this->attrs['gotoXHsync'] = "30+55";
455       $this->attrs['gotoXVsync'] = "50+70";
456     }
458     /* Write back to ldap */
459     $ldap= $this->config->get_ldap_link();
460     $ldap->cd($this->dn);
461     $this->cleanup();
464     $ldap->modify ($this->attrs); 
465     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
466     if (!$ldap->success()){
467       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
468     }
469     $this->handle_post_events("modify");
471     /* Send goto reload event to gosaSupportDaemon */
472     if(count($this->attrs)){
473       $this->send_goto_reload(); 
474     }
476   }
479   function getListOfXDrivers()
480   {
481     /* Generate a list of xdrivers from CONFIG_DIR./xdrivers */
482     $drivers = array();
483     if (file_exists(CONFIG_DIR.'/xdrivers')){
484       $xdrivers = file (CONFIG_DIR.'/xdrivers');
485       foreach ($xdrivers as $line){
486         if (!preg_match ("/^#/", $line)){
487           $drivers[]= trim($line);
488         }
489       }
490     } else {
491       $drivers = array("ati", "atimisc", "chips", "cirrus", "cyrix", "fbdev", "fglrx",
492           "i128", "i740", "i810", "intel", "imstt", "mga", "neomagic", "newport", "nsc",  "nv", "nvidia",
493           "r128", "radeon", "rendition", "s3", "s3virge", "savage", "siliconmotion",
494           "sis", "tdfx", "tga", "trident", "tseng", "vboxvideo", "vesa", "vga", "vmware");
495     }
496     return($drivers);
497   }
500   /* Return plugin informations for acl handling */
501   static function plInfo()
502   {
503     return (array(
504           "plShortName"   => _("Service"),
505           "plDescription" => _("Workstation service"),
506           "plSelfModify"  => FALSE,
507           "plDepends"     => array(),
508           "plPriority"    => 10,
509           "plSection"     => array("administration"),
510           "plCategory"    => array("workstation","ogroups"), 
512           "plProvidedAcls"=> array(
513             "gotoXMonitor"          => _("Monitor"),
514             "gotoXDriver"           => _("Gfx driver"),
515             "gotoXResolution"       => _("Gfx resolution"),
516             "gotoXColordepth"       => _("Gfx color depth"),
517             "gotoXHsync"            => _("HSync"),
518             "gotoXVsync"            => _("VSync"),
519             "AutoSync"              => _("Use DDC"),
520             "gotoScannerEnable"     => _("Scanner enabled"),
521             "gotoXKbModel"          => _("Keyboard model"),
522             "gotoXKbLayout"         => _("Keyboard layout"),
523             "gotoXKbVariant"        => _("Keyboard variant"),
524             "gotoXMouseType"        => _("Mouse type"),
525             "gotoXMouseport"        => _("Mouse port"),
526             "goFonHardware"         => _("Telephone hardware")) 
527             ));
528   }
531   /*! \brief  Send goto_reload event to support daemon
532    */
533   function send_goto_reload()
534   {
535     if(count($this->attrs) && class_available("DaemonEvent")){
536       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
537       $o_queue = new gosaSupportDaemon();
538       if(isset($events['TRIGGERED']['DaemonEvent_goto_reload'])){
539         $evt = $events['TRIGGERED']['DaemonEvent_goto_reload'];
540         $macs = array();
542         /* Get list of macAddresses 
543          */
544         if(isset($this->parent->by_object['ogroup'])){
546           /* If we are an object group, add all member macs 
547            */
548           $p = $this->parent->by_object['ogroup'];
549           foreach($p->memberList as $dn => $obj){
550             if(preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$dn)) continue;
551             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
552               $macs[] = $p->objcache[$dn]['macAddress'];
553             }
554           }
555         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
557           /* We are a workstation. Add current mac.
558            */
559           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
560           if(!empty($mac) && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$this->orig_dn)){
561             $macs[] = $mac;
562           }          
563         }
565         /* Trigger event for all member objects 
566          */
567         if(count($macs)){
568           $tmp = new $evt['CLASS_NAME']($this->config);
569           $tmp->set_type(TRIGGERED_EVENT);
570           $target = $o_queue->get_host().":".$o_queue->get_port();
571           $tmp->add_targets(array($target));
572           $tmp->set_macs($macs);
573           if(!$o_queue->append($tmp,TRUE)){
574             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
575           }
576         }
577       }
578     }
579   }
582   function PrepareForCopyPaste($source)
583   {
584     plugin::PrepareForCopyPaste($source);
586     if(preg_match("/\+/",$this->gotoXHsync)){
587       $this->AutoSync = true;
588       $this->gotoXHsync = preg_replace("/\+/","-",$this->gotoXHsync);
589       $this->gotoXVsync = preg_replace("/\+/","-",$this->gotoXVsync);
590     }
591   }
594 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
595 ?>