Code

Updated ldap action handling for installable devices
[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("environment","resolutions") != ""){
65             $file = $this->config->get_cfg_value("environment","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")."]");
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("phoneGeneric", "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("group", "ogroupRDN"),
159                 get_ou("servgeneric", "serverRDN"),
160                 get_ou("termgeneric", "terminalRDN"),
161                 get_ou("workgeneric", "workstationRDN"),
162                 get_ou("printgeneric", "printerRDN"),
163                 get_ou("componentGeneric", "componentRDN"),
164                 get_ou("phoneGeneric", "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                 }
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", set_post($this->$val));
277         }
278         $smarty->assign("XKbLayouts"    ,set_post($this->XKbLayouts));
279         $smarty->assign("XKbLayoutKeys" ,set_post(array_flip($this->XKbLayouts)));
281         $smarty->assign("xdmcpservers",set_post($this->config->data['SERVERS']['TERMINAL']));
282         $smarty->assign("nfsservers",  set_post($this->config->data['SERVERS']['NFS']));
284         /* Variables - select */
285         foreach(array(
286                     "gotoXDriver", "gotoXResolution", "gotoXColordepth", 
287                     "gotoXKbModel", "gotoXKbLayout","gotoScannerEnable",
288                     "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
290             $smarty->assign($val."_select", set_post($this->$val));
291         }
293         /* Variables */
294         foreach(array("gotoXHsync", "gotoXVsync") as $val){
295             $smarty->assign($val, set_post($this->$val));
296         }
298         /* Checkboxes */
299         foreach(array("gotoScannerEnable") as $val){
300             if ($this->$val == TRUE) {
301                 $smarty->assign("$val", "checked");
302             } else {
303                 $smarty->assign("$val", "");
304             }
305         }
307         /* Phone stuff */
308         $smarty->assign ("goFonHardware", $this->goFonHardware);
310         $perms = "";
311         if(!$this->acl_is_writeable("goFonHardware")){
312             $perms = " disabled ";
313         }
315         $hl= "<select size=\"1\" name=\"goFonHardware\" ".$perms." title=\"".
316             _("Choose the phone located at the current terminal")."\" >";
317         foreach ($this->hardware_list as $cn => $description){
318             if ($cn == $this->goFonHardware){
319                 $selected= "selected";
320             } else {
321                 $selected= "";
322             }
323             if (isset($this->used_hardware[$cn])){
324                 $color= "style=\"color:#A0A0A0\"";
325             } else {
326                 $color= "";
327             }
328             $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
329         }
330         $hl.= "</select>\n";
331         $smarty->assign ("hardware_list", $hl);
332         $smarty->assign ("gotoXMonitor", set_post($this->gotoXMonitor));
334         if($this->AutoSync){
335             $smarty->assign("AutoSyncCHK"," checked ");
336             $smarty->assign("hiddenState"," disabled ");
337         }else{
338             $smarty->assign("AutoSyncCHK"," ");
339             $smarty->assign("hiddenState","");
340         }
342         /* Show main page */
343         return($smarty->fetch (get_template_path('workstationService.tpl',TRUE,dirname(__FILE__))));
344     }
346     function remove_from_parent()
347     {
348         $this->handle_post_events("remove");
349         new log("remove","workstation/".get_class($this),$this->dn);
350     }
352     /* Save data to object */
353     function save_object()
354     {
355         plugin::save_object();
357         if((isset($_POST['workservicePosted'])) && $this->acl_is_writeable("AutoSync")) {
358             if(isset($_POST['AutoSync'])){
359                 $this->AutoSync = true;
360             }else{
361                 $this->AutoSync = false;
362             }
363         }
364     }
366     /* Check supplied data */
367     function check()
368     {
369         /* Call common method to give check the hook */
370         $message= plugin::check();
372         /* Default entries can use blank hsync/vsync entries */
373         if ($this->dn != "" && $this->cn != "default" && $this->cn != "wdefault"){
375             /* But only if no auto sync is enabled... */
376             if (!$this->AutoSync){
378                 /* Check vsync for correct usage */
379                 $val= preg_replace ("/\s/", "", $this->gotoXVsync);
381                 if($this->acl_is_writeable("gotoXVsync")){
382                     if(empty($val)){
383                         $message[]= msgPool::required(_("Vertical synchronization"));
384                     }elseif (!preg_match ("/^\d+(\.\d+)?([+-]\d+(\.\d+)?)?$/", $val)){
385                         $message[]= msgPool::invalid(_("Vertical synchronization"));
386                     } else{
387                         if (!preg_match('/^\d+$/', $val)){
388                             list($v1,$v2)= preg_split ("/[-+]/", $val);
389                             if ($v2 != ""){
390                                 if ($v1 > $v2){
391                                     $message[]= msgPool::invalid(_("Vertical synchronization"));
392                                 }
393                             }
394                         }
395                     }
396                 }
398                 /* Check hsync for correct usage */
399                 $val= preg_replace ("/\s/", "", $this->gotoXHsync);
400                 if($this->acl_is_writeable("gotoXHsync")){
401                     if(empty($val)){
402                         $message[]= msgPool::required(_("Horizontal synchronization"));
403                     }elseif (!preg_match ("/^\d+(\.\d+)?([-]\d+(\.\d+)?)?$/", $val)){
404                         $message[]= msgPool::invalid(_("Horizontal synchronization"));
405                     } else{
406                         if (!preg_match('/^\d+$/', $val)){
407                             list($v1,$v2)= preg_split ("/[-+]/", $val);
408                             if ($v2 != ""){
409                                 if ($v1 > $v2){
410                                     $message[]= msgPool::invalid(_("Horizontal synchronization"));
411                                 }
412                             }
413                         }
414                     }
415                 }
416             }
417         }
419         return ($message);
420     }
423     /* Save to LDAP */
424     function save()
425     {
426         /* remove objectclass GOhard if this is an ogroup tab */
427         if(isset($this->parent->by_object['ogroup'])){
428             $this->objectclasses = array();
429         }
431         plugin::save();
433         /* Strip out 'default' values */
434         foreach(array(
435                     "gotoXDriver", "gotoXResolution", "gotoXColordepth",
436                     "gotoXKbModel", "gotoXKbLayout",
437                     "gotoXKbVariant", "gotoXMouseType", "gotoXMouseport") as $val){
439             if ($this->attrs[$val] == "default"){
440                 $this->attrs[$val]= array();
441             }
442         }
444         if($this->AutoSync){
445             $this->attrs['gotoXHsync'] = "30+55";
446             $this->attrs['gotoXVsync'] = "50+70";
447         }
449         /* Write back to ldap */
450         $ldap= $this->config->get_ldap_link();
451         $ldap->cd($this->dn);
452         $this->cleanup();
455         $ldap->modify ($this->attrs); 
456         new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
457         if (!$ldap->success()){
458             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
459         }
460         $this->handle_post_events("modify");
462         /* Send goto reload event to gosaSupportDaemon */
463         if(count($this->attrs)){
464             $this->send_goto_reload(); 
465         }
467     }
470     function getListOfXDrivers()
471     {
472         /* Generate a list of xdrivers from CONFIG_DIR./xdrivers */
473         $drivers = array();
474         if (file_exists(CONFIG_DIR.'/xdrivers')){
475             $xdrivers = file (CONFIG_DIR.'/xdrivers');
476             foreach ($xdrivers as $line){
477                 if (!preg_match ("/^#/", $line)){
478                     $drivers[]= trim($line);
479                 }
480             }
481         } else {
482             $drivers = array("ati", "atimisc", "chips", "cirrus", "cyrix", "fbdev", "fglrx",
483                     "i128", "i740", "i810", "intel", "imstt", "mga", "neomagic", "newport", "nsc",  "nv", "nvidia",
484                     "r128", "radeonhd", "radeon", "rendition", "s3", "s3virge", "savage", "siliconmotion",
485                     "sis", "tdfx", "tga", "trident", "tseng", "vesa", "vga", "vmware");
486         } 
487         $tmp = array();
488         foreach($drivers as $name){
489             $tmp[$name] = $name;
490         }
492         return($tmp);
493     }
496     /* Return plugin informations for acl handling */
497     static function plInfo()
498     {
499         return (array(
500                     "plShortName"   => _("Service"),
501                     "plDescription" => _("Workstation service"),
502                     "plSelfModify"  => FALSE,
503                     "plDepends"     => array(),
504                     "plPriority"    => 10,
505                     "plSection"     => array("administration"),
506                     "plCategory"    => array("workstation","ogroups"), 
508                     "plProvidedAcls"=> array(
509                         "gotoXMonitor"          => _("Monitor"),
510                         "gotoXDriver"           => _("Graphic driver"),
511                         "gotoXResolution"       => _("Graphic resolution"),
512                         "gotoXColordepth"       => _("Graphic color depth"),
513                         "gotoXHsync"            => _("Horizontal synchronization"),
514                         "gotoXVsync"            => _("Vertical synchronization"),
515                         "AutoSync"              => _("Use DDC"),
516                         "gotoScannerEnable"     => _("Scanner enabled"),
517                         "gotoXKbModel"          => _("Keyboard model"),
518                         "gotoXKbLayout"         => _("Keyboard layout"),
519                         "gotoXKbVariant"        => _("Keyboard variant"),
520                         "gotoXMouseType"        => _("Mouse type"),
521                         "gotoXMouseport"        => _("Mouse port"),
522                         "goFonHardware"         => _("Telephone hardware")) 
523                         ));
524     }
527     /*! \brief  Send goto_reload event to support daemon
528      */
529     function send_goto_reload()
530     {
531         if(count($this->attrs) && class_available("DaemonEvent")){
532             $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
533             $o_queue = new gosaSupportDaemon();
534             if(isset($events['TRIGGERED']['DaemonEvent_goto_reload'])){
535                 $evt = $events['TRIGGERED']['DaemonEvent_goto_reload'];
536                 $macs = array();
538                 /* Get list of macAddresses 
539                  */
540                 if(isset($this->parent->by_object['ogroup'])){
542                     /* If we are an object group, add all member macs 
543                      */
544                     $p = $this->parent->by_object['ogroup'];
545                     foreach($p->memberList as $dn => $obj){
546                         if(preg_match("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/",$dn)) continue;
547                         if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
548                             $macs[] = $p->objcache[$dn]['macAddress'];
549                         }
550                     }
551                 }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
553                     /* We are a workstation. Add current mac.
554                      */
555                     $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
556                     if(!empty($mac) && !preg_match("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/",$this->orig_dn)){
557                         $macs[] = $mac;
558                     }          
559                 }
561                 /* Trigger event for all member objects 
562                  */
563                 if(count($macs)){
564                     $tmp = new $evt['CLASS_NAME']($this->config);
565                     $tmp->set_type(TRIGGERED_EVENT);
566                     $target = $o_queue->get_host().":".$o_queue->get_port();
567                     $tmp->add_targets(array($target));
568                     $tmp->set_macs($macs);
569                     if(!$o_queue->append($tmp,TRUE)){
570                         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
571                     }
572                 }
573             }
574         }
575     }
578     function PrepareForCopyPaste($source)
579     {
580         plugin::PrepareForCopyPaste($source);
582         if(preg_match("/\+/",$this->gotoXHsync)){
583             $this->AutoSync = true;
584             $this->gotoXHsync = preg_replace("/\+/","-",$this->gotoXHsync);
585             $this->gotoXVsync = preg_replace("/\+/","-",$this->gotoXVsync);
586         }
587     }
590 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
591 ?>