Code

Updated get_ou it receives values from the config registry now.
[gosa.git] / 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("core","resolutions") != ""){
65       $file = $this->config->get_cfg_value("core","resolutions");
67       if(is_readable($file)){
68         $str = file_get_contents($file);
69         $lines = preg_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 = preg_split("/\n/",$str);
124         foreach($tmp as $entry){
125           if((!empty($entry)) && (!preg_match("/^#/",$entry))){
126             $entry = trim($entry);
127             $tmp2 = explode(":",$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("group", "ogroupRDN") ),
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("group", "ogroupRDN") ,
159         get_ou("group", "ogroupRDN") ,
160         get_ou("group", "ogroupRDN") ,
161         get_ou("group", "ogroupRDN") ,
162         get_ou("group", "ogroupRDN") ,
163         get_ou("group", "ogroupRDN") ,
164         get_ou("group", "ogroupRDN") );
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->XResolutions= array('default' => _("inherited"));
230       $this->XColordepths= array('default' => _("inherited"));
231       $this->XKbModels= array('default' => _("inherited"));
232       $this->XKbLayouts= array('default' => _("inherited"));
233       $this->XKbVariants= array('default' => _("inherited"));
234       $this->MouseTypes= array('default' => _("inherited"));
235       $this->MousePorts= array('default' => _("inherited"));
236     }
237   }
239   function execute()
240   {
241     /* Call parent execute */
242     plugin::execute();
244     if($this->is_account && !$this->view_logged){
245       $this->view_logged = TRUE;
246       new log("view","workstation/".get_class($this),$this->dn);
247     }
249     /* Do we need to flip is_account state? */
250     if (isset($_POST['modify_state'])){
251       $this->is_account= !$this->is_account;
252     }
254     /* Do we need to flip is_account state? */
255     if(isset($_POST['modify_state'])){
256       if($this->is_account && $this->acl_is_removeable()){
257         $this->is_account= FALSE;
258       }elseif(!$this->is_account && $this->acl_is_createable()){
259         $this->is_account= TRUE;
260       }
261     }
263     /* Show main page */
264     $smarty= get_smarty();
266     /* Assign ACLs */
267     $tmp = $this->plInfo();
268     foreach($tmp['plProvidedAcls'] as $name => $translated){
269       $smarty->assign($name."ACL",$this->getacl($name));
270     }
272     /* Arrays */ 
273     foreach(array("XDrivers", "XResolutions", "XColordepths",
274           "XKbModels", "XKbVariants",
275           "MouseTypes", "MousePorts") as $val){
276       $smarty->assign("$val", $this->$val);
277     }
278     $smarty->assign("XKbLayouts"    ,$this->XKbLayouts);
279     $smarty->assign("XKbLayoutKeys" ,array_flip($this->XKbLayouts));
281     $smarty->assign("xdmcpservers", $this->config->data['SERVERS']['TERMINAL']);
282     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
283     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
285     /* Variables - select */
286     foreach(array(
287           "gotoXDriver", "gotoXResolution", "gotoXColordepth", 
288           "gotoXKbModel", "gotoXKbLayout","gotoScannerEnable",
289           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
291       $smarty->assign($val."_select", $this->$val);
292     }
294     /* Variables */
295     foreach(array("gotoXHsync", "gotoXVsync") as $val){
296       $smarty->assign($val, $this->$val);
297     }
299     /* Checkboxes */
300     foreach(array("gotoScannerEnable") as $val){
301       if ($this->$val == TRUE) {
302         $smarty->assign("$val", "checked");
303       } else {
304         $smarty->assign("$val", "");
305       }
306     }
308     /* Phone stuff */
309     $smarty->assign ("goFonHardware", $this->goFonHardware);
311     $perms = "";
312     if(!$this->acl_is_writeable("goFonHardware")){
313       $perms = " disabled ";
314     }
316     $hl= "<select size=\"1\" name=\"goFonHardware\" ".$perms." title=\"".
317       _("Choose the phone located at the current terminal")."\" >";
318     foreach ($this->hardware_list as $cn => $description){
319       if ($cn == $this->goFonHardware){
320         $selected= "selected";
321       } else {
322         $selected= "";
323       }
324       if (isset($this->used_hardware[$cn])){
325         $color= "style=\"color:#A0A0A0\"";
326       } else {
327         $color= "";
328       }
329       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
330     }
331     $hl.= "</select>\n";
332     $smarty->assign ("hardware_list", $hl);
333     $smarty->assign ("gotoXMonitor", $this->gotoXMonitor);
335     if($this->AutoSync){
336       $smarty->assign("AutoSyncCHK"," checked ");
337       $smarty->assign("hiddenState"," disabled ");
338     }else{
339       $smarty->assign("AutoSyncCHK"," ");
340       $smarty->assign("hiddenState","");
341     }
343     /* Show main page */
344     return($smarty->fetch (get_template_path('workstationService.tpl',TRUE,dirname(__FILE__))));
345   }
347   function remove_from_parent()
348   {
349     $this->handle_post_events("remove");
350     new log("remove","workstation/".get_class($this),$this->dn);
351   }
353   /* Save data to object */
354   function save_object()
355   {
356     plugin::save_object();
358     if((isset($_POST['workservicePosted'])) && $this->acl_is_writeable("AutoSync")) {
359       if(isset($_POST['AutoSync'])){
360         $this->AutoSync = true;
361       }else{
362         $this->AutoSync = false;
363       }
364     }
365   }
367   /* Check supplied data */
368   function check()
369   {
370     /* Call common method to give check the hook */
371     $message= plugin::check();
373     /* Default entries can use blank hsync/vsync entries */
374     if ($this->dn != "" && $this->cn != "default" && $this->cn != "wdefault"){
376       /* But only if no auto sync is enabled... */
377       if (!$this->AutoSync){
379         /* Check vsync for correct usage */
380         $val= preg_replace ("/\s/", "", $this->gotoXVsync);
382         if($this->acl_is_writeable("gotoXVsync")){
383           if(empty($val)){
384             $message[]= msgPool::required(_("VSync"));
385           }elseif (!preg_match ("/^\d+(\.\d+)?([+-]\d+(\.\d+)?)?$/", $val)){
386             $message[]= msgPool::invalid(_("VSync"));
387           } else{
388             if (!preg_match('/^\d+$/', $val)){
389               list($v1,$v2)= preg_split ("/[-+]/", $val);
390               if ($v2 != ""){
391                 if ($v1 > $v2){
392                   $message[]= msgPool::invalid(_("VSync"));
393                 }
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             if (!preg_match('/^\d+$/', $val)){
408               list($v1,$v2)= preg_split ("/[-+]/", $val);
409               if ($v2 != ""){
410                 if ($v1 > $v2){
411                   $message[]= msgPool::invalid(_("HSync"));
412                 }
413               }
414             }
415           }
416         }
417       }
418     }
420     return ($message);
421   }
424   /* Save to LDAP */
425   function save()
426   {
427     /* remove objectclass GOhard if this is an ogroup tab */
428     if(isset($this->parent->by_object['ogroup'])){
429       $this->objectclasses = array();
430     }
432     plugin::save();
434     /* Strip out 'default' values */
435     foreach(array(
436           "gotoXDriver", "gotoXResolution", "gotoXColordepth",
437           "gotoXKbModel", "gotoXKbLayout",
438           "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
440       if ($this->attrs[$val] == "default"){
441         $this->attrs[$val]= array();
442       }
443     }
445     if($this->AutoSync){
446       $this->attrs['gotoXHsync'] = "30+55";
447       $this->attrs['gotoXVsync'] = "50+70";
448     }
450     /* Write back to ldap */
451     $ldap= $this->config->get_ldap_link();
452     $ldap->cd($this->dn);
453     $this->cleanup();
456     $ldap->modify ($this->attrs); 
457     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
458     if (!$ldap->success()){
459       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
460     }
461     $this->handle_post_events("modify");
463     /* Send goto reload event to gosaSupportDaemon */
464     if(count($this->attrs)){
465       $this->send_goto_reload(); 
466     }
468   }
471   function getListOfXDrivers()
472   {
473     /* Generate a list of xdrivers from CONFIG_DIR./xdrivers */
474     $drivers = array();
475     if (file_exists(CONFIG_DIR.'/xdrivers')){
476       $xdrivers = file (CONFIG_DIR.'/xdrivers');
477       foreach ($xdrivers as $line){
478         if (!preg_match ("/^#/", $line)){
479           $drivers[]= trim($line);
480         }
481       }
482     } else {
483       $drivers = array("ati", "atimisc", "chips", "cirrus", "cyrix", "fbdev", "fglrx",
484           "i128", "i740", "i810", "intel", "imstt", "mga", "neomagic", "newport", "nsc",  "nv", "nvidia",
485           "r128", "radeon", "rendition", "s3", "s3virge", "savage", "siliconmotion",
486           "sis", "tdfx", "tga", "trident", "tseng", "vesa", "vga", "vmware");
487     } 
488     $tmp = array();
489     foreach($drivers as $name){
490       $tmp[$name] = $name;
491     }
493     return($tmp);
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("group", "ogroupRDN") , '/')."/",$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("group", "ogroupRDN") , '/')."/",$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 ?>