Code

Followup commit for #3020
[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             $this->XDrivers = array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XDrivers;
205             break;
206           case 'gotoXResolution':
207             $this->XResolutions= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XResolutions;
208             break;
209           case 'gotoXColordepth':
210             $this->XColordepths= array('default' => _("inherited").' ['.$attrs[$name][0].' '._('Bit').']') + $this->XColordepths;
211             break;
212           case 'gotoXKbModel':
213             $this->XKbModels= array('default' => _("inherited").' ['.$attrs[$name][0].']') + $this->XKbModels;
214             break;
215           case 'gotoXKbLayout':
216             $this->XKbLayouts= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XKbLayouts;
217             break;
218           case 'gotoXKbVariant':
219             $this->XKbVariants= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->XKbVariants;
220             break;
221           case 'gotoXMouseType':
222             $this->MouseTypes= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->MouseTypes;
223             break;
224           case 'gotoXMouseport':
225             $this->MousePorts= array('default' => _("inherited").' ['.$attrs[$name][0].']' ) + $this->MousePorts;
226             break;
227         }
229       }
231     }
233     /* Workaround to fill in inherited values if we've specified an objectclass */
234     $SelectedSystemType = session::get("SelectedSystemType");
235     if (isset($SelectedSystemType['ogroup']) && $SelectedSystemType['ogroup'] != 'none'){
236       $this->XDrivers=array('default' => _("inherited"));
237       $this->XResolutions= array('default' => _("inherited"));
238       $this->XColordepths= array('default' => _("inherited"));
239       $this->XKbModels= array('default' => _("inherited"));
240       $this->XKbLayouts= array('default' => _("inherited"));
241       $this->XKbVariants= array('default' => _("inherited"));
242       $this->MouseTypes= array('default' => _("inherited"));
243       $this->MousePorts= array('default' => _("inherited"));
244     }
245   }
247   function execute()
248   {
249     /* Call parent execute */
250     plugin::execute();
252     if($this->is_account && !$this->view_logged){
253       $this->view_logged = TRUE;
254       new log("view","workstation/".get_class($this),$this->dn);
255     }
257     /* Do we need to flip is_account state? */
258     if (isset($_POST['modify_state'])){
259       $this->is_account= !$this->is_account;
260     }
262     /* Do we need to flip is_account state? */
263     if(isset($_POST['modify_state'])){
264       if($this->is_account && $this->acl_is_removeable()){
265         $this->is_account= FALSE;
266       }elseif(!$this->is_account && $this->acl_is_createable()){
267         $this->is_account= TRUE;
268       }
269     }
271     /* Show main page */
272     $smarty= get_smarty();
274     /* Assign ACLs */
275     $tmp = $this->plInfo();
276     foreach($tmp['plProvidedAcls'] as $name => $translated){
277       $smarty->assign($name."ACL",$this->getacl($name));
278     }
280     /* Arrays */ 
281     foreach(array("XDrivers", "XResolutions", "XColordepths",
282           "XKbModels", "XKbVariants",
283           "MouseTypes", "MousePorts") as $val){
284       $smarty->assign("$val", $this->$val);
285     }
286     $smarty->assign("XKbLayouts"    ,$this->XKbLayouts);
287     $smarty->assign("XKbLayoutKeys" ,array_flip($this->XKbLayouts));
289     $smarty->assign("xdmcpservers", $this->config->data['SERVERS']['TERMINAL']);
290     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
291     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
293     /* Variables - select */
294     foreach(array(
295           "gotoXDriver", "gotoXResolution", "gotoXColordepth", 
296           "gotoXKbModel", "gotoXKbLayout","gotoScannerEnable",
297           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
299       $smarty->assign($val."_select", $this->$val);
300     }
302     /* Variables */
303     foreach(array("gotoXHsync", "gotoXVsync") as $val){
304       $smarty->assign($val, $this->$val);
305     }
306     $smarty->assign("staticAddress", "");
308     /* Checkboxes */
309     foreach(array("gotoScannerEnable") as $val){
310       if ($this->$val == TRUE) {
311         $smarty->assign("$val", "checked");
312       } else {
313         $smarty->assign("$val", "");
314       }
315     }
317     /* Phone stuff */
318     $smarty->assign ("goFonHardware", $this->goFonHardware);
320     $perms = "";
321     if(!$this->acl_is_writeable("goFonHardware")){
322       $perms = " disabled ";
323     }
325     $hl= "<select size=\"1\" name=\"goFonHardware\" ".$perms." title=\"".
326       _("Choose the phone located at the current terminal")."\" >";
327     foreach ($this->hardware_list as $cn => $description){
328       if ($cn == $this->goFonHardware){
329         $selected= "selected";
330       } else {
331         $selected= "";
332       }
333       if (isset($this->used_hardware[$cn])){
334         $color= "style=\"color:#A0A0A0\"";
335       } else {
336         $color= "";
337       }
338       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
339     }
340     $hl.= "</select>\n";
341     $smarty->assign ("hardware_list", $hl);
342     $smarty->assign ("gotoXMonitor", $this->gotoXMonitor);
344     if($this->AutoSync){
345       $smarty->assign("AutoSyncCHK"," checked ");
346       $smarty->assign("hiddenState"," disabled ");
347     }else{
348       $smarty->assign("AutoSyncCHK"," ");
349       $smarty->assign("hiddenState","");
350     }
352     /* Show main page */
353     return($smarty->fetch (get_template_path('workstationService.tpl',TRUE,dirname(__FILE__))));
354   }
356   function remove_from_parent()
357   {
358     $this->handle_post_events("remove");
359     new log("remove","workstation/".get_class($this),$this->dn);
360   }
362   /* Save data to object */
363   function save_object()
364   {
365     plugin::save_object();
367     if((isset($_POST['workservicePosted'])) && $this->acl_is_writeable("AutoSync")) {
368       if(isset($_POST['AutoSync'])){
369         $this->AutoSync = true;
370       }else{
371         $this->AutoSync = false;
372       }
373     }
374   }
376   /* Check supplied data */
377   function check()
378   {
379     /* Call common method to give check the hook */
380     $message= plugin::check();
382     /* Default entries can use blank hsync/vsync entries */
383     if ($this->dn != "" && $this->cn != "default" && $this->cn != "wdefault"){
385       /* But only if no auto sync is enabled... */
386       if (!$this->AutoSync){
388         /* Check vsync for correct usage */
389         $val= preg_replace ("/\s/", "", $this->gotoXVsync);
391         if($this->acl_is_writeable("gotoXVsync")){
392           if(empty($val)){
393             $message[]= msgPool::required(_("VSync"));
394           }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
395             $message[]= msgPool::invalid(_("VSync"));
396           } else{
397             list($v1,$v2)= preg_split ("/[-+]/", $val);
398             if ($v2 != ""){
399               if ($v1 > $v2){
400                 $message[]= msgPool::invalid(_("VSync"));
401               }
402             }
403           }
404         }
406         /* Check hsync for correct usage */
407         $val= preg_replace ("/\s/", "", $this->gotoXHsync);
408         if($this->acl_is_writeable("gotoXHsync")){
409           if(empty($val)){
410             $message[]= msgPool::required(_("HSync"));
411           }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
412             $message[]= msgPool::invalid(_("HSync"));
413           } else{
414             list($v1,$v2)= preg_split ("/[-+]/", $val);
415             if ($v2 != ""){
416               if ($v1 > $v2){
417                 $message[]= msgPool::invalid(_("HSync"));
418               }
419             }
420           }
421         }
422       }
423     }
425     return ($message);
426   }
429   /* Save to LDAP */
430   function save()
431   {
432     /* remove objectclass GOhard if this is an ogroup tab */
433     if(isset($this->parent->by_object['ogroup'])){
434       $this->objectclasses = array();
435     }
437     plugin::save();
439     /* Strip out 'default' values */
440     foreach(array(
441           "gotoXDriver", "gotoXResolution", "gotoXColordepth",
442           "gotoXKbModel", "gotoXKbLayout", "gotoXDriver",
443           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
445       if ($this->attrs[$val] == "default"){
446         $this->attrs[$val]= array();
447       }
448     }
450     if($this->AutoSync){
451       $this->attrs['gotoXHsync'] = "30+55";
452       $this->attrs['gotoXVsync'] = "50+70";
453     }
455     /* Write back to ldap */
456     $ldap= $this->config->get_ldap_link();
457     $ldap->cd($this->dn);
458     $this->cleanup();
461     $ldap->modify ($this->attrs); 
462     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
463     if (!$ldap->success()){
464       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
465     }
466     $this->handle_post_events("modify");
468     /* Send goto reload event to gosaSupportDaemon */
469     if(count($this->attrs)){
470       $this->send_goto_reload(); 
471     }
473   }
476   function getListOfXDrivers()
477   {
478     /* Generate a list of xdrivers from CONFIG_DIR./xdrivers */
479     $drivers = array();
480     if (file_exists(CONFIG_DIR.'/xdrivers')){
481       $xdrivers = file (CONFIG_DIR.'/xdrivers');
482       foreach ($xdrivers as $line){
483         if (!preg_match ("/^#/", $line)){
484           $drivers[]= trim($line);
485         }
486       }
487     } else {
488       $drivers = array("ati", "atimisc", "chips", "cirrus", "cyrix", "fbdev", "fglrx",
489           "i128", "i740", "i810", "intel", "imstt", "mga", "neomagic", "newport", "nsc",  "nv", "nvidia",
490           "r128", "radeon", "rendition", "s3", "s3virge", "savage", "siliconmotion",
491           "sis", "tdfx", "tga", "trident", "tseng", "vboxvideo", "vesa", "vga", "vmware");
492     }
493     return($drivers);
494   }
497   /* Return plugin informations for acl handling */
498   static function plInfo()
499   {
500     return (array(
501           "plShortName"   => _("Service"),
502           "plDescription" => _("Workstation service"),
503           "plSelfModify"  => FALSE,
504           "plDepends"     => array(),
505           "plPriority"    => 10,
506           "plSection"     => array("administration"),
507           "plCategory"    => array("workstation","ogroups"), 
509           "plProvidedAcls"=> array(
510             "gotoXMonitor"          => _("Monitor"),
511             "gotoXDriver"           => _("Gfx driver"),
512             "gotoXResolution"       => _("Gfx resolution"),
513             "gotoXColordepth"       => _("Gfx color depth"),
514             "gotoXHsync"            => _("HSync"),
515             "gotoXVsync"            => _("VSync"),
516             "AutoSync"              => _("Use DDC"),
517             "gotoScannerEnable"     => _("Scanner enabled"),
518             "gotoXKbModel"          => _("Keyboard model"),
519             "gotoXKbLayout"         => _("Keyboard layout"),
520             "gotoXKbVariant"        => _("Keyboard variant"),
521             "gotoXMouseType"        => _("Mouse type"),
522             "gotoXMouseport"        => _("Mouse port"),
523             "goFonHardware"         => _("Telephone hardware")) 
524             ));
525   }
528   /*! \brief  Send goto_reload event to support daemon
529    */
530   function send_goto_reload()
531   {
532     if(count($this->attrs) && class_available("DaemonEvent")){
533       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
534       $o_queue = new gosaSupportDaemon();
535       if(isset($events['TRIGGERED']['DaemonEvent_goto_reload'])){
536         $evt = $events['TRIGGERED']['DaemonEvent_goto_reload'];
537         $macs = array();
539         /* Get list of macAddresses 
540          */
541         if(isset($this->parent->by_object['ogroup'])){
543           /* If we are an object group, add all member macs 
544            */
545           $p = $this->parent->by_object['ogroup'];
546           foreach($p->memberList as $dn => $obj){
547             if(preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$dn)) continue;
548             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
549               $macs[] = $p->objcache[$dn]['macAddress'];
550             }
551           }
552         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
554           /* We are a workstation. Add current mac.
555            */
556           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
557           if(!empty($mac) && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$this->orig_dn)){
558             $macs[] = $mac;
559           }          
560         }
562         /* Trigger event for all member objects 
563          */
564         if(count($macs)){
565           $tmp = new $evt['CLASS_NAME']($this->config);
566           $tmp->set_type(TRIGGERED_EVENT);
567           $target = $o_queue->get_host().":".$o_queue->get_port();
568           $tmp->add_targets(array($target));
569           $tmp->set_macs($macs);
570           if(!$o_queue->append($tmp,TRUE)){
571             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
572           }
573         }
574       }
575     }
576   }
579   function PrepareForCopyPaste($source)
580   {
581     plugin::PrepareForCopyPaste($source);
583     if(preg_match("/\+/",$this->gotoXHsync)){
584       $this->AutoSync = true;
585       $this->gotoXHsync = preg_replace("/\+/","-",$this->gotoXHsync);
586       $this->gotoXVsync = preg_replace("/\+/","-",$this->gotoXVsync);
587     }
588   }
591 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
592 ?>