Code

c4fea2011e7c83b57bf3fba79c395c8179eb1277
[gosa.git] / plugins / personal / environment / class_environment.inc
1 <?php
3 class environment extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary          = "Manage server basic objects";
7   var $cli_description      = "Some longer text\nfor help";
8   var $cli_parameters       = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account       = FALSE;
12   var $plHeadline           = "Environment";
13   var $plDescription        = "This does something";
14   var $dialog               = false;    // Indicates that we are currently editing in an seperate dialog
15   
16   var $in_dialog            = false;
17   var $uid                  = "";
19   var $is_group             = false;
20   
21   /* Attribute definition
22    */
24   /* profile management */
25   var $useProfile         = false;  // Specifies if we want to use a Server 
26   var $gotoProfileServer  = "";     // Specifies the selected profile server
27   var $gotoProfileServers = array();// Specifies all available and selectable servers
28   var $gotoProfileFlags   = "";     // Flags enabled  ? only used to set ACL and save 
29   var $gotoProfileFlag_C  = "";     // Flag is set to C if we have the profile caching fucntion enabled 
30   
31   var $gotoXResolution    = "auto";     // The selected resolution eg: 1024x768
32   var $gotoXResolutions   = array();// Contains all available resolutions for this account
33   var $gotoProfileFlag_L  = "";     // Flag is set to L to enable runtime resolution change 
34   var $gotoProfileQuota   = "";     // User Quota Settings
36   /* Logon script section*/
37   var $gotoLogonScripts   = array();// Contains all available Logon Scripts  
38   var $gotoLogonScript    = "";     // The selected Logon Script
40   /* Printer */
41   var $gotoPrinter        = array();// All available Printer, with their configurations
42   var $gotoPrinterSel     = "";     //  The selected Printer
43   var $gosaDefaultPrinter = "";     // Default printer
45   /* Share */
46   var $gotoShares         = array();// Current Share Options
47   var $gotoShare          = "";     // currently selected Share Option
48   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
49   var $gotoAvailableShares= array();// Available Shares for this account
50   
51   /* Kiosk profile */
52   var $gotoKioskProfile   = "none";     // The selected Kiosk Profile
53   var $gotoKioskProfiles  = array();// All available Kiosk profiles
54   var $newKioskProfiles   = array();
56   /* Hotplug Devices */
57   var $gotoHotplugDevice  = array();     // Selected hotplug
58   var $gotoHotplugDevices = array();// Already configured hotplug devices 
61   /* general settings */
62   // Sets the attributes which will kept on page reload, which will be saved, ...
64   var $CopyPasteVars      = array("gotoHotplugDevices","newKioskProfiles","gotoAvailableShares","gotoShareSelections","gotoPrinterSel","gotoProfileFlag_L","gotoXResolutions","gotoProfileFlag_C","gotoProfileServers","useProfile","is_group","in_dialog","OrigCn");
66   var $attributes         = array("uid","gotoProfileServer","gotoProfileFlags",
67                                     "gotoXResolution","gotoProfileQuota",
68                                     "gotoLogonScripts","gotoLogonScript",
69                                     "gotoPrinter", "gosaDefaultPrinter",
70                                     "gotoShares","gotoShare",
71                                     "gotoKioskProfile","gotoKioskProfiles",
72                                     "gotoHotplugDevice");
73   var $objectclasses      = array("gotoEnvironment"); // Specifies the objectClass which contains the attributes edited here 
74   var $cn;
75   var $OrigCn;
77   function environment ($config, $dn= NULL)
78   {
79     plugin::plugin ($config, $dn);
81     /* Check : Are we currently editing a group or user dialog */
82     if((isset($this->attrs['cn'][0]))&&(!isset($this->attrs['uid'][0]))){
83       $suffix="Group";
84       $this->uid          = $this->attrs['cn'][0];
85       $this->attrs['uid'] = $this->attrs['cn'][0];
86       $this->OrigCn = $this->attrs['cn'][0];
87     }else{
88       $suffix="User";
89     }
91     $this->gotoKioskProfile= preg_replace("/^.*\//i","",$this->gotoKioskProfile);
93     /* Get all Printer assignments */
94     $ldap = $this->config->get_ldap_link();
95     $ldap->cd($this->config->current['BASE']);
96     $ldap->search("(&(objectClass=gotoPrinter)(goto".$suffix."Printer=".$this->uid."))",array("*"));
97     while($printer = $ldap->fetch()){
98       $this->gotoPrinter[$printer['cn'][0]]=$printer;
99       $this->gotoPrinter[$printer['cn'][0]]['mode']="user";
100     }
101     $ldap->search("(&(objectClass=gotoPrinter)(goto".$suffix."AdminPrinter=".$this->uid."))",array("*"));
102     while($printer = $ldap->fetch()){
103       $this->gotoPrinter[$printer['cn'][0]]=$printer;
104       $this->gotoPrinter[$printer['cn'][0]]['mode']="admin";
105     }
106   
107     /* prepare hotplugs */
108     if((isset($this->attrs['gotoHotplugDevice']))&&(is_array($this->attrs['gotoHotplugDevice']))){
109       unset($this->attrs['gotoHotplugDevice']['count']);
110       foreach($this->attrs['gotoHotplugDevice'] as $device){
111         $tmp = $tmp2 = array();
112         $tmp = split("\|",$device);
113         $tmp2['name']        = $tmp[0]; 
114         $tmp2['description'] = $tmp[1]; 
115         $tmp2['id']          = $tmp[2]; 
116         $this->gotoHotplugDevices[$tmp[0]]=$tmp2;
117       }
118     }
119     
120     /* prepare LogonScripts */
121     if((isset($this->attrs['gotoLogonScript']))&&(is_array($this->attrs['gotoLogonScript']))){
122       unset($this->attrs['gotoLogonScript']['count']);
123       foreach($this->attrs['gotoLogonScript'] as $device){
124         $tmp = $tmp2 = array();
125         $tmp = split("\|",$device);
126         $tmp2['LogonName']        = $tmp[0]; 
127         $tmp2['LogonPriority']    = $tmp[2]; 
128         if(preg_match("/O/i",$tmp[1])){
129           $tmp2['LogonOverload'] = "O";
130         }else{
131           $tmp2['LogonOverload'] = "";
132         }
133         if(preg_match("/L/i",$tmp[1])){
134           $tmp2['LogonLast'] = "L";
135         }else{
136           $tmp2['LogonLast'] = "";
137         }
138         $tmp2['LogonData']        = base64_decode($tmp[3]); 
139         $tmp2['LogonDescription'] = $tmp[4];
140         $this->gotoLogonScripts[$tmp[0]]=$tmp2;
141       }
142     }
143     
144     /* Prepare Shares */
145     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
146       unset($this->attrs['gotoShare']['count']);
147       foreach($this->attrs['gotoShare'] as $share){
148         $tmp = $tmp2 = array();
149         $tmp = split("\|",$share);
150         $tmp2['server']      =$tmp[0];
151         $tmp2['name']        =$tmp[1];
152         $tmp2['mountPoint']  =$tmp[2];
153         if(isset($tmp[3])){
154           $tmp2['OtherStuff']  =$tmp[3];
155         }else{
156           $tmp2['OtherStuff']  ="";
157         }
158         if(isset($tmp[4])){
159           $tmp2['Username']  =$tmp[4];
160         }else{
161           $tmp2['Username']  ="";
162         }
163         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
164       }
165     }
167     for($i = 0 ; $i < strlen($this->gotoProfileFlags) ; $i ++){
168       $chr = $this->gotoProfileFlags[$i];
169       $name = "gotoProfileFlag_".$chr;
170       $this->$name=$chr;
171     }
173     if((!empty($this->gotoProfileServer))||($this->gotoProfileFlag_C=="C")){
174       $this->useProfile = true;
175     }else{
176       $this->useProfile = false;
177     }
179     /* Set to group environment if we editing a group */
180     if(!isset($this->parent)){
181       $this->is_group = true;
182     }
184     /* Set resolutions */
185     $this->gotoXResolutions = array("auto"=>_("auto"),
186                                     "640x480"   =>  "640x480",
187                                     "800x600"   =>  "800x600",
188                                     "1024x768"  =>  "1024x768",
189                                     "1154x864"  =>  "1154x864",
190                                     "1280x768"  =>  "1280x768",
191                                     "1280x1024" =>  "1280x1024");
193     if(isset($this->config->data['MAIN']['RESOLUTION_HOOK'])){
194       $file = $this->config->data['MAIN']['RESOLUTION_HOOK'];
195   
196       if(is_readable($file)){
197         $str = file_get_contents($file);
198         $lines = split("\n",$str);
199         foreach($lines as $line){
200           $line = trim($line);
201           if(!empty($line)){
202             $this->gotoXResolutions[$line]=$line;
203           }
204         }
205         //natcasesort($this->gotoXResolutions);
206       }else{
207         print_red(sprintf(_("You have specified an external resolution hook which can't be read, please check the permission of the file '%s'."),$file));
208       }
209     }
211  
212     $this->gotoProfileServers= $config->getShareServerList() ;
213     $this->gotoShareSelections= $config->getShareList(true);
214     $this->gotoAvailableShares= $config->getShareList(false);  
216   }
218   function execute()
219   {
220         /* Call parent execute */
221         plugin::execute();
223   /* Fill templating stuff */
224   $smarty= get_smarty();
225   $display= "";
227   /* Prepare all variables for smarty */
228   foreach($this->attributes as $s_attr){
229     /* Set value*/
230     $smarty->assign($s_attr,$this->$s_attr);
232     /* Set checkbox state*/
233     if(empty($this->$s_attr)){
234       $smarty->assign($s_attr."CHK","");
235     }else{
236       $smarty->assign($s_attr."CHK"," checked ");
237     }
239     /* Prepare ACL settings*/
240     if(chkacl($this->acl,$s_attr)=="") {
241       $smarty->assign($s_attr."ACL","");
242     }else{
243       $smarty->assign($s_attr."ACL"," disabled ");
244     }
246   }
248   /* Is accout enabled | are we editing from usermenu or admin menu 
249      All these tab management is done here
250    */
252   /* Working from Usermenu an the Account is currently disbled
253      * this->parent :  is only set if we are working in a list of tabs
254      * is_account   :  is only true if the needed objectClass is given
255     */
256     if((!isset($this->parent))&&(!$this->is_account)){
257       /* We are currently editing this tab from usermenu, but this account is not enabled */
258       $smarty->assign("is_account",$this->is_account);
259       /* Load template */
260       $display .= $smarty->fetch(get_template_path('environment.tpl', TRUE));
261       /* Avoid the "You are currently editing ...." message when you leave this tab */
262       $display .= back_to_main(); 
263       /* Display our message to the user */
264       return $display;
265     
267     /* We are currently editing from group tabs, because 
268      * $this->parent is set
269      * posixAccount is not set, so we are not in usertabs.
270      */
271     }elseif((isset($this->parent))&&(!isset($this->parent->by_object['posixAccount']))){
272       $smarty->assign("is_account","true");
273       $this->is_group     = true;
274       $this->uid          = $this->cn;
275       $this->attrs['uid'] = $this->cn;
277       /* Change state if needed */
278       if (isset($_POST['modify_state'])){
279         $this->is_account= !$this->is_account;
280       }
282       /* Group Dialog with enabled environment options */
283       if ($this->is_account){
284         $display= $this->show_header(_("Remove environment extension"),
285             _("Environment extension enabled. You can disable it by clicking below."));
286       } else {
287   
288       /* Environment is disabled 
289          If theres is no posixAccount enabled, you won't be able to enable 
290          environment extensions
291        */
292         if((isset($this->parent->by_object['group']))||(isset($this->attrs['objectClass']))&&((in_array("posixAccount",$this->attrs['objectClass'])))){
293           // 4. There is a PosixAccount
294           $display= $this->show_header(_("Add environment extension"),
295               _("Environment extension disabled. You can enable it by clicking below."));
296           return $display;
297         }else{
298           // 4. There is no PosixAccount
299           $display= $this->show_header(_("Add environment extension"),
300               _("Environment extension disabled. You have to setup a posix account before you can enable this feature."));
301           return $display;
302         }
303       }
304     }else{
305       /* Editing from Usermenu 
306        *  Tell smarty that this accoutn is enabled 
307        */
308       $smarty->assign("is_account","true");
310       $this->is_group = false;
312       /* Do we need to flip is_account state? */
313       if (isset($_POST['modify_state'])){
314         $this->is_account= !$this->is_account;
315       }
317       if(isset($this->parent)){
319         // 3. Account enabled . Editing from adminmenu
320         if ($this->is_account){
321           $display= $this->show_header(_("Remove environment extension"),
322               _("Environment extension enabled. You can disable it by clicking below."));
323         } else {
325           if($this->parent->by_object['posixAccount']->is_account==true){
326             // 4. There is a PosixAccount
327             $display= $this->show_header(_("Add environment extension"),
328                 _("Environment extension disabled. You can enable it by clicking below."));
329             return $display;
330           }else{
331             // 4. There is a PosixAccount
332             $display= $this->show_header(_("Add environment extension"),
333                 _("Environment extension disabled. You have to setup a posix account before you can enable this feature."),TRUE,TRUE);
334             return $display;
335           }
336         }
337       }
338     }
339     /* Account is Account : is_accounbt=true.
340      * Else we won't reach this. 
341      */
342    
343     /* Prepare all variables for smarty */
344     foreach($this->attributes as $s_attr){
345       /* Set value*/
346       $smarty->assign($s_attr,$this->$s_attr);
347      
348       /* Set checkbox state*/
349       if(empty($this->$s_attr)){
350         $smarty->assign($s_attr."CHK","");
351       }else{
352         $smarty->assign($s_attr."CHK"," checked ");
353       }
354     
355       /* Prepare ACL settings*/
356       if(chkacl($this->acl,$s_attr)=="") {
357         $smarty->assign($s_attr."ACL","");
358       }else{
359         $smarty->assign($s_attr."ACL"," disabled ");
360       }
361  
362     }
364     foreach(array("gotoHotplugDevice","gotoPrinterSel") as $s_attr){
365       if(chkacl($this->acl,$s_attr)=="") {
366         $smarty->assign($s_attr."ACL","");
367       }else{
368         $smarty->assign($s_attr."ACL"," disabled ");
369       }
370     }
372     if(empty($this->useProfile)){
373       $smarty->assign("gotoProfileACL","disabled");
374       $smarty->assign("useProfileCHK","");
375     }else{
376       $smarty->assign("gotoProfileACL","");
377       $smarty->assign("useProfileCHK"," checked ");
378     }
380  
381     $smarty->assign("useProfileACL","");
382     if($this->acl != "#none#"){
383       $smarty->assign("useProfileACL","");
384       $smarty->assign("gotoProfileFlag_CACL"," ");
385       $smarty->assign("gotoProfileQuotaACL"," ");
386     }else{
387       $smarty->assign("gotoProfileFlag_CACL"," disabled ");
388       $smarty->assign("useProfileACL","disabled");
389       $smarty->assign("gotoProfileServer"," disabled ");
390       $smarty->assign("gotoProfileQuotaACL"," disabled ");
391     }
393     /* HANDLE Profile Settings here 
394      * Assign available Quota and resolution settings
395      * Get all available profile server
396      * Get cache checkbox
397      * Assign this all to Smarty 
398      */
400     if(empty($this->gotoProfileFlag_L)){
401       $smarty->assign("gotoProfileFlag_LCHK"," ");
402     }else{
403       $smarty->assign("gotoProfileFlag_LCHK"," checked ");
404     }
406     if(empty($this->gotoProfileFlag_C)){
407       $smarty->assign("gotoProfileFlag_CCHK"," ");
408     }else{
409       $smarty->assign("gotoProfileFlag_CCHK"," checked ");
410     }
413     $smarty->assign("gotoXResolutions"    , $this->gotoXResolutions);
414     $smarty->assign("gotoXResolutionKeys" , array_flip($this->gotoXResolutions));
416     $smarty->assign("gotoProfileServers",$this->gotoProfileServers);
417     if(!is_array($this->gotoProfileServers)){
418       $this->gotoProfileServers =array();
419     }
420     $smarty->assign("gotoProfileServerKeys",array_flip($this->gotoProfileServers));
422     /* Handle kiosk profiles 
423      * Read available from filesystem
424      * Open management if post is transmitted
425      */
427     /* Save */
428     if(isset($_POST['KioskClose'])){
429       $this->newKioskProfiles = array_merge($this->newKioskProfiles,$this->dialog->save());
430   
431       unset($this->dialog);
432       $this->dialog=NULL;
433       $this->is_dialog = false;
434     }
435     $tmp = new kioskManagementDialog($this->config,$this->dn);
436     $list = $tmp->getKioskProfiles($this->newKioskProfiles);
438     $list['none']=_("None");
440     $list = array_reverse($list);
442     if(!isset($list[$this->gotoKioskProfile])){
443       print_red(sprintf(_("The selected kiosk profile '%s' is no longer available, setting current profile to 'none'."),$this->gotoKioskProfile));
444       $this->gotoKioskProfile = 'none';
445     }
447     /* Reassign help class */
448     $_SESSION['current_class_for_help'] = get_class($this);
449  
450     /* Open Management Dialog */
451     if(isset($_POST['KioskManagementDialog'])){
452       $this->dialog = new kioskManagementDialog($this->config,$this->dn,$this->newKioskProfiles); 
453       $this->dialog->parent= $this;
454       $this->dialog->acl = $this->acl;
455       $this->is_dialog = true;
456     }
458     $smarty->assign("gotoKioskProfiles",$list);
459     $smarty->assign("gotoKioskProfileKeys",array_flip($list));
461     /* Logonscript Management
462      * Get available LogonScripts (possibly grey out (or mark) these script that are defined for the group) 
463      * Perform add Delete edit Posts 
464      */
466     /* Dialog Save */
467     if(isset($_POST['LogonSave'])){
468       $this->dialog->save_object();
469       if(count($this->dialog->check())!=0){
470         foreach($this->dialog->check() as $msg){
471           print_red($msg);
472         }
473       }else{
474         $tmp = $this->dialog->save();
475         unset($this->dialog);
476         $this->dialog=NULL;
477         $this->is_dialog=false;
478         $this->gotoLogonScripts[$tmp['LogonName']]=$tmp; 
479       }
480     }
481     
482     /* Dialog Quit without saving */
483     if(isset($_POST['LogonCancel'])){
484       $this->is_dialog= false;
485       unset($this->dialog);
486       $this->dialog= NULL;
487     }
488    
489     /* Check Edit Del New Posts for a selected LogonScript */ 
490     if(isset($_POST['gotoLogonScriptNew'])||isset($_POST['gotoLogonScriptEdit'])||isset($_POST['gotoLogonScriptDel'])){
492       /* New Logon Script: Open an edit dialog, we don't need a $_POST['gotoLogonScript'] here.
493        * In this case we create a new Logon Script.
494        */
495       if(isset($_POST['gotoLogonScriptNew'])){
496         $this->is_dialog = true;
497         $this->dialog = new logonManagementDialog($this->config,$this->dn);
498       }
500       /* If we receive a Delete request and there is a Script selected in the selectbox, delete this one.
501        * We only can delete if there is an entry selected.
502        */
503       if((isset($_POST['gotoLogonScriptDel']))&&(isset($_POST['gotoLogonScript']))){
504         unset($this->gotoLogonScripts[$_POST['gotoLogonScript']]);
505       }
506       
507       /* In this case we want to edit an existing entry, we open a new Dialog to allow editing.
508        * There must be an entry selected to perform edit request.
509        */
510       if((isset($_POST['gotoLogonScriptEdit']))&&(isset($_POST['gotoLogonScript']))){
511         $is_entry = $this->gotoLogonScripts[$_POST['gotoLogonScript']];
512         $this->is_dialog = true;
513         $this->dialog = new logonManagementDialog($this->config,$this->dn,$is_entry);
514       }
515     }
516      
517     /* Append List to smarty*/
518     $smarty->assign("gotoLogonScripts",   $this->printOutLogonScripts());
519     $smarty->assign("gotoLogonScriptKeys",array_flip($this->printOutLogonScripts()));
521     /* In this section server shares will be defined 
522      * A user can select one of the given shares and a mount point
523      *  and attach this combination to his setup.
524      */
525     
526     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
527     if(!is_array($this->gotoShareSelections)){
528       print $this->gotoShareSelections;
529       $this->gotoShareSelections = array();
530     }
531     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
533     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry 
534      * This entry will be, a combination of mountPoint and sharedefinitions 
535      */
536     if(isset($_POST['gotoShareAdd'])){
537       /* We assign a share to this user, if we don't know where to mount the share */
538       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
539         print_red(_("You must specify a valid mount point."));
540       }elseif(!(
541         preg_match("/^\//",$_POST['gotoShareMountPoint'])  ||
542         preg_match("/^~/",$_POST['gotoShareMountPoint']) ||
543         preg_match("/^\$HOME/",$_POST['gotoShareMountPoint']) ||
544         preg_match("/^.HOME/",$_POST['gotoShareMountPoint']) ||
545         preg_match("/^\$USER/",$_POST['gotoShareMountPoint']) ||
546         preg_match("/^.USER/",$_POST['gotoShareMountPoint']) ||
547         preg_match("/^%/",$_POST['gotoShareMountPoint'])
548         )
549       ){
550         print_red(_("You must specify a valid mount point.")); 
551       }else{
552         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
553         $s_mount = $_POST['gotoShareMountPoint'];
554         $s_user  = $_POST['ShareUser'];
555         /* Preparing the new assignment */ 
556         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
557         $this->gotoShares[$a_share['name']."|".$a_share['server']]['Username']=$s_user;
558         $this->gotoShares[$a_share['name']."|".$a_share['server']]['OtherStuff']="";
559         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
560       }
561     }  
563     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
564      * If there is no defined share selected, we will abort the deletion without any message 
565      */
566     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
567       unset($this->gotoShares[$_POST['gotoShare']]);
568     }
570     $smarty->assign("gotoShares",$this->printOutAssignedShares());
571     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
573     /* Hotplug devices will be handled here 
574      * There are 3 possible methods for this feature
575      * Create a new Hotplug, A Dialog will open where you can specify some hotplug information
576      * Delete will erase an entry, the entry must be selcted in the ListBox first
577      * Editing an entry will open a dialog where the informations about the selcted entry can be changed
578      */
580     /* If there is a new entry wanted, open a new entry by initilising the dialog */
581     if(isset($_POST['gotoHotplugDeviceNew'])){
582       $this->dialog = new hotplugDialog($this->config,$this->dn);
583       $this->is_dialog = true;
584     }
586     /* We have to delete the selected hotplug from the list*/
587     if((isset($_POST['gotoHotplugDeviceDel']))&&(isset($_POST['gotoHotplugDevice']))){
588       unset($this->gotoHotplugDevices[$_POST['gotoHotplugDevice']]);
589     }
591     /* There are already defined hotplugs from other users we could use */
592     if(isset($_POST['gotoHotplugDeviceUse'])){
593       $this->dialog = new hotplugDialog($this->config,$this->dn,true);
594       $this->is_dialog = true;
595     }
597     /* Dialog Aborted */
598     if(isset($_POST['HotPlugCancel'])){
599       unset($this->dialog);
600       $this->dialog= NULL;
601       $this->is_dialog = false;
602     }
603  
604     /* Dialod saved */
605     if(isset($_POST['HotPlugSave'])){
606       $this->dialog->save_object();
607       if(count($this->dialog->check())!=0){
608         foreach($this->dialog->check() as $msg){
609           print_red($msg);
610         }
611       }else{
612         $this->dialog->save_object();
613         $a_tmp = $this->dialog->save();
614         if(is_array($a_tmp)){
615           $this->gotoHotplugDevices[$a_tmp['name']]= $a_tmp; 
616         }
617         unset($this->dialog);
618         $this->dialog= NULL;
619         $this->is_dialog = false;
620       }
621     }
622     
623     $smarty->assign("gotoHotplugDevices",$this->printOutHotPlugDevices());
624     $smarty->assign("gotoHotplugDeviceKeys",array_flip($this->printOutHotPlugDevices()));
625   
626     /* Printer Assignment will managed below 
627      * A printer can be assigned in two different ways and two different types
628      * There are 2 types of users assigned to a printer : user and admin
629      * They only differ in the member attribute they will be assigned to. user: gotoUserPrinter admin: gotoadminPrinter
630      * The different types of assigning a user are : 1 assigning a user to a printer 2. assigning a group to a printer
631      */ 
632     
633     /* First handle Add Post. Open a dialog that allows us to select a printer or two */ 
634     if(isset($_POST['gotoPrinterAdd'])){
635       $this->is_dialog=true;
636       $this->dialog = new selectPrinterDialog($this->config,$this->dn,$this->gotoPrinter);
637     }
639     if(isset($_POST['PrinterCancel'])){
640         $this->is_dialog=false;
641         unset($this->dialog);
642         $this->dialog=NULL;
643     }
645     if(isset($_POST['PrinterSave'])){
646       if(count($this->dialog->check())!=0){
647         $tmp = $this->dialog->check();
648         foreach($tmp as $msg){
649           print_red($msg);
650         } 
651       }else{
652         $this->dialog->save_object();
653         $tmp = $this->dialog->save();
654         $tmp2= $this->dialog->getPrinter(true);
655   
656         foreach($tmp as $pname){
657           $this->gotoPrinter[$pname]=$tmp2[$pname];
658           $this->gotoPrinter[$pname]['mode']="user";
659         }
660   
661         $this->is_dialog=false;
662         unset($this->dialog);
663         $this->dialog   =NULL;
664       }
665     }
666   
667     if((isset($_POST['gotoPrinterDel']))&&(isset($_POST['gotoPrinterSel']))&&(!empty($_POST['gotoPrinterSel']))){
668       $printer = $_POST['gotoPrinterSel'];
669       foreach($printer as $pname){
670         unset($this->gotoPrinter[$pname]);
671       }
672     }
674     if((isset($_POST['gotoPrinterEdit']))&&(isset($_POST['gotoPrinterSel']))&&(!empty($_POST['gotoPrinterSel']))){
677       $printers = $_POST['gotoPrinterSel'];
679       foreach($printers as $printer){
680         if($this->gotoPrinter[$printer]['mode']=="user"){
681           $this->gotoPrinter[$printer]['mode']="admin";
682         }else{
683           $this->gotoPrinter[$printer]['mode']="user";
684         }
685       }
686     }
688     if((isset($_POST['gotoPrinterDefault']))&&(isset($_POST['gotoPrinterSel']))&&(!empty($_POST['gotoPrinterSel']))){
689       if ($this->gosaDefaultPrinter == $_POST['gotoPrinterSel'][0]){
690         $this->gosaDefaultPrinter= "";
691       } else {
692         $this->gosaDefaultPrinter= $_POST['gotoPrinterSel'][0];
693       }
694     }
696     $smarty->assign("gotoPrinter",$this->printOutPrinterDevices());
697     $smarty->assign("gotoPrinterKeys",array_flip($this->printOutPrinterDevices()));
698  
699     /* General behavior */
700     if((isset($this->dialog))&&($this->dialog!=NULL)&&(!empty($this->dialog))){
701       $this->dialog->save_object();
702       return ($this->dialog->execute());
703     }
704     if($this->acl != "#none#"){
705       $smarty->assign("useProfileACL","");
706     }else{
707       $smarty->assign("gotoProfileFlag_CACL"," disabled ");
708       $smarty->assign("gotoProfileServerACL"," disabled ");
709       $smarty->assign("gotoProfileQuotaACL"," disabled ");
710     }
712     if(!$this->useProfile){
713       $smarty->assign("gotoProfileFlag_CACL"," disabled ");
714       $smarty->assign("gotoProfileServerACL"," disabled ");
715       $smarty->assign("gotoProfileQuotaACL"," disabled ");
716     }
718     /* Als smarty vars are set. Get smarty template and generate output */
719     $display.= $smarty->fetch(get_template_path('environment.tpl', TRUE,dirname(__FILE__)));
720     
721     return($display);
722   }
724   function remove_from_parent()
725   {
726     /* only if it was an account*/
727     if (!$this->initially_was_account){
728       return;
729     }
731     /* include global link_info */
732     $ldap= $this->config->get_ldap_link();
734     /* Remove and write to LDAP */
735     plugin::remove_from_parent();
737     /* Don't save our template variables */
738     $skip = array("uid","gotoLogonScripts","gotoPrinter","gotoShares","gotoKioskProfiles","gotoHotplugDevices" );
740     /* Skip all these attributes */
741     foreach($skip as $del){
742       unset($this->attrs[$del]);
743     }
745     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
747     $ldap->cd($this->dn);
748     $this->cleanup();
749     $ldap->modify ($this->attrs); 
751     show_ldap_error($ldap->get_error(), _("Removing environment information failed"));
753     /* Optionally execute a command after we're done */
754     $this->handle_post_events("remove");
755   }
758   /* Save data to object */
759   function save_object()
760   {
761     /* Get all Posted vars 
762      * Setup checkboxes 
763      */
764  
765     if(isset($_POST['iamposted'])){
766       if(isset($_POST['useProfile'])){
767         $this->useProfile = true;
768       }else{
769         $this->useProfile = false;
770       }
771       if(isset($_POST['gotoProfileFlag_C'])){
772         $this->gotoProfileFlag_C = $_POST['gotoProfileFlag_C'];
773       }else{
774         $this->gotoProfileFlag_C = false;
775       }
776       if(isset($_POST['gotoProfileFlag_L'])){
777         $this->gotoProfileFlag_L = $_POST['gotoProfileFlag_L'];
778       }else{
779         $this->gotoProfileFlag_L = false;
780       }
782       $tmp= $this->gosaDefaultPrinter;
783       plugin::save_object();
784       foreach($this->attributes as $s_attr){
785         if(in_array($s_attr,array("gotoShares","gotoHotplugDevices","gotoPrinter","gotoLogonScripts","uid"))) continue;
786         if(isset($_POST[$s_attr])){
787           $this->$s_attr = $_POST[$s_attr];
788         }else{
789           $this->$s_attr = false;
790         }
791       }
792       $this->gosaDefaultPrinter= $tmp;
793     }
794   }
797   /* Check supplied data */
798   function check()
799   {
800     /* Call common method to give check the hook */
801     $message= plugin::check();
802   
803     if(preg_match("/[^0-9]/",$this->gotoProfileQuota)) {
804       $message[]=_("Please set a valid profile quota size.");
805     } 
806     if(!isset($this->attrs['objectClass'])){
807       $this->attrs['objectClass']=array();
808     } 
809     if(!$this->is_group){
810       if((!((in_array("posixAccount",$this->attrs['objectClass']))||($this->parent->by_object['posixAccount']->is_account==true)))&&(!$this->is_group)){
811         $message[]=(_("You need to setup a valid posix extension in order to enable evironment features."));  
812       }
813     }
814     return ($message);
815   }
818   /* Save to LDAP */
819   function save()
820   {
821     /* If group was renamed, all printer settings get lost
822      */ 
823     /* only save changed variables ....*/
824     if ($this->gotoKioskProfile =="none") $this->gotoKioskProfile ="";
825     if((!empty($this->gotoKioskProfile))&&($this->gotoKioskProfile != "none")){
826       if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
827         $method="https://";
828       }else{
829         $method="http://";
830       }
832       $str = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/kiosk/");
833       $this->gotoKioskProfile= $str.$this->gotoKioskProfile;
834     }else{
835       $this->gotoKioskProfile= array();
836     }
837     
838     plugin::save();
839     $ldap= $this->config->get_ldap_link();
841     $realyUsedAttrs= array();
843     $path = search_config($this->config->data,"environment", "KIOSKPATH"); 
844     /* Creating Kiosk Profiles */
845     foreach($this->newKioskProfiles as $file){
846       $contents = $file['contents'];
847       $fp = @fopen($path."/".$file['name'],"w");
848       if(!$fp){
849         print_red(_("Can't save new kiosk profiles, possibly permission denied for folder")." : ",$path);
850       }else{
851         fwrite($fp,$contents,strlen($contents));
852       }
853       @unlink($file['tmp_name']);
854     }
855  
856     /* Save already used objectClasses */
857     $ocs        = $this->attrs['objectClass'];
858     unset($ocs['count']);
859     $this->attrs = array();
860     $this->attrs['objectClass']= $ocs;
861     foreach($this->objectclasses as $objc){
862       if(!in_array($objc,$this->attrs['objectClass'])){
863         $this->attrs['objectClass'][]=$objc;
864       }
865     }
868     /* Save usersettings to Printer */
870     if(chkacl($this->acl,"gotoPrinter")!=""){
871       $this->gotoPrinter = array();
872     }  
873     
874     if($this->is_group){
875       $s_suffix = "Group";
876     }else{
877       $s_suffix = "User";
878     }
879   
881     /* 1. Search all printers that have our uid/cn as member 
882      * 2. Delete this uid/cn from every single entry and save it again.
883      * 2.1 There are different types of members: Users / Groups, this will be defined in $suffix
884      * 2.2 And each type has two modes, Admin (e.g. 'gotoUserAdminPrinter') and Normal 
885      */
886     $ldap->search("(&(objectClass=gotoPrinter)(|(goto".$s_suffix."Printer=".$this->uid.")(goto".$s_suffix."AdminPrinter=".$this->uid.")))",array("*"));
887     while($attr = $ldap->fetch()){
889       /* Walk trough all printers and check if our user id used, if so remove it.
890        * Later we will insert our uid at the right place.
891        */
893       /* Remove normal entries (User)*/
894       if(isset($attr['goto'.$s_suffix.'Printer'])) {
895         foreach($attr['goto'.$s_suffix.'Printer'] as $key => $user){
896           if($this->uid==$user){
897             unset($attr['goto'.$s_suffix.'Printer'][$key]);
898           }
899         }    
900       }
902       /* Remove administrational entries (Admin)*/
903       if(isset($attr['goto'.$s_suffix.'AdminPrinter'])){
904         foreach($attr['goto'.$s_suffix.'AdminPrinter'] as $key => $user){
905           if($this->uid==$user){
906             unset($attr['goto'.$s_suffix.'AdminPrinter'][$key]);
907           }
908         }    
909       }
911       /* Extract useable tags, to be able to save all changes 
912        */
913       $attrs_used = array();
914       foreach($attr as $key=>$val){
916         /* If index is numeric, skip it ...*/
917         if((!is_numeric($key))&&($key!="count")){
919           /* If entry contains 'count' remove it */
920           if(is_array($val)&&isset($val['count'])){
921             unset($val['count']);
922           }
923           $attrs_used[$key]=$val;
924         }
925       }
926       /* the result of cleaning the entry is 
927        *  to be able to directly save this again,
928        *  if all changes are made 
929        */
930       $attr= $attrs_used;
932 #fix : Id don't know why such an entry was set ... 
933       if(isset($attr['GOTOADMINPRINTER'])){
934         unset($attr['GOTOADMINPRINTER']);
935       }
937       /* Save changes */
938       $ldap->cd($attr['dn']);
939       unset($attr['dn']);
940 $ldap->modify ($attr); 
942       if($ldap->get_error()!="Success"){
943         print_red(_("Error while writing printer")." : ".$ldap->get_error());
944       }
945     }
947     /* All printers are cleaned, (our cn/uid removed) 
948      *  now we must add our uid / cn 
949      *  to the new configured printers.
950      */
951     foreach($this->gotoPrinter as $printer) {
952       $ldap->cat($printer['dn']);
953       $attrs= $ldap->fetch();
954       $attrs_used = array(); 
955       foreach($attrs as $key=>$val){
956         if((!is_numeric($key))&&($key!="count")){
957           if(is_array($val)&&isset($val['count'])){
958             unset($val['count']);
959           }
960           $attrs_used[$key]=$val;
961         }
962       }
963       /* $attrs contains all values 
964        * we need, to save the entry lateron 
965       */
966       $attrs= $attrs_used;
968       /* Depending on the type (User/Admin) 
969        *  switch these attributes, that makes it easier
970        */
971       if($printer['mode'] == "user"){
972         $attribute  = "goto".$s_suffix."Printer";
973         $attribute2 = "goto".$s_suffix."AdminPrinter";
974       }else{
975         $attribute  = "goto".$s_suffix."AdminPrinter";
976         $attribute2 = "goto".$s_suffix."Printer";
977       }
979       /* If this user is already assigned to $attribute2 
980        * delete user from $attribute2, to be albe to attach him to $attribute
981        * A user can't be admin and normal user for one printer
982        */
983       if(!isset($printer[$attribute2])){
984         $printer[$attribute2]=array();
985       }else{
986         if(in_array($this->uid,$printer[$attribute2])){ 
987           $tmp = array_flip($printer[$attribute2]);
988           unset($tmp[$this->uid]);
989           $attrs[$attribute2]=array_flip($tmp);
990         }
991         /* If Last entry removed, clear attribute*/
992         if(empty($attrs[$attribute2])){
993           $attrs[$attribute2]=array();
994         }
995       }
996     
997       /* Attach user to the $attribute, if he is'nt already attached
998        */
999       if(!isset($attrs[$attribute])){
1000         $attrs[$attribute]=array($this->uid);
1001       }else{
1002         unset($attrs[$attribute]['count']);
1003         if(!in_array($this->uid,$attrs[$attribute])){
1004           $attrs[$attribute][]=$this->uid;
1005         }
1006       }
1008       $ldap->cd($attrs['dn']);
1009       unset($attrs['dn']);
1010 $ldap->modify ($attrs); 
1012       if($ldap->get_error()!="Success"){
1013         print_red(_("Error while writing printer settings")." : ".$ldap->get_error());
1014       }
1015     }
1016   
1017     /* Prepare HotPlug devices */
1018     $this->attrs['gotoHotplugDevice'] = array();
1019     foreach($this->gotoHotplugDevices as $name => $device){
1020       $this->attrs['gotoHotplugDevice'][] = $device['name']."|".$device['description']."|".$device['id'];
1021     }
1023     /* Prepare LogonScripts */
1024     $this->attrs['gotoLogonScript'] = array();
1025     foreach($this->gotoLogonScripts as $name => $script){
1026       $this->attrs['gotoLogonScript'][] =   $script['LogonName']."|".
1027                                             $script['LogonOverload'].$script['LogonLast']."|".
1028                                             $script['LogonPriority']."|".
1029                                             base64_encode($script['LogonData'])."|".
1030                                             $script['LogonDescription'];
1031     }
1033     /* Prepare Shares */
1034     $this->attrs['gotoShare']=array();
1035     foreach($this->gotoShares as $name => $share){
1036       $this->attrs['gotoShare'][] =$share['server']."|".$share['name']."|".$share['mountPoint']."|".$share['OtherStuff']."|".$share['Username'];
1037     }
1040     if($this->gotoXResolution == "auto") $this->gotoXResolution ="";
1041     $saveThis = array("gotoProfileQuota","gotoXResolution","gotoProfileServer","gotoKioskProfile","gosaDefaultPrinter");
1042      
1043     foreach($saveThis as $tosave){
1044       if(!empty($this->$tosave)){
1045         $this->attrs[$tosave]=$this->$tosave;
1046       }else{
1047         $this->attrs[$tosave]=array();
1048       }
1049     }
1050  
1051     /* Prepare Flags */
1052     $this->attrs['gotoProfileFlags'] = array($this->gotoProfileFlag_C.$this->gotoProfileFlag_L);
1053     if(empty($this->attrs['gotoProfileFlags'][0])){
1054       $this->attrs['gotoProfileFlags']=array();
1055       }
1057     if($this->useProfile == false){
1058       $this->attrs['gotoProfileFlags'] = preg_replace("/C/i","",$this->attrs['gotoProfileFlags']);
1059       $this->attrs['gotoProfileServer']= array(); 
1060     }
1062     foreach($this->attributes as $s_attr){
1063       if(chkacl($this->acl,$s_attr)!="") {
1064         if(isset($this->attrs[$s_attr])){
1065           unset($this->attrs[$s_attr]);
1066         }
1067       }
1068     }
1070     $ldap->cat ($this->dn, array('dn'));
1071     if ($ldap->fetch()){
1072       $mode= "modify";
1073     } else {
1074       $mode= "add";
1075       $ldap->cd($this->config->current['BASE']);
1076       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1077     }
1079     $ldap->cd($this->dn);
1080     $this->cleanup();
1081     $ldap->$mode($this->attrs);
1082     show_ldap_error($ldap->get_error(), _("Adding environment information failed"));
1083     $this->handle_post_events($mode);
1084   }
1086 /* Generate ListBox frindly output for the defined shares 
1087  * Possibly Add or remove an attribute here, 
1088  */
1089   function printOutAssignedShares()
1090   {
1091     $a_return = array();
1092     if(is_array($this->gotoShares)){
1093       foreach($this->gotoShares as $share){
1094         if (!preg_match('/^!/', $share['server'])){
1095           $a_return[$share['name']."|".$share['server']]= $share['server']."://".$share['name']." on ".$share['mountPoint']." as ".$share['Username'];
1096         }
1097       }
1098     }
1099     return($a_return);
1100   }
1102 /* Generate ListBox frindly output for the definedhotplugs 
1103  * Possibly Add or remove an attribute here,
1104  */
1105 function printOutHotPlugDevices()
1106   {
1107     $a_return= array();
1108     if(is_array($this->gotoHotplugDevices)){
1109       foreach($this->gotoHotplugDevices as $key=>$device){
1110         $a_return[$key] = $device['name']." - ".$device['id'];
1111       }
1112     }
1113     return($a_return);
1114   }
1116   /* Generates ListBox frienly output of used printer devices 
1117    * Append ' - admin' if printer is used in admin mode
1118    */
1119   function printOutPrinterDevices()
1120   {
1121     $a_return = array();
1122     if(is_array($this->gotoPrinter)){
1123       foreach($this->gotoPrinter as $printer){
1124         if($printer['mode'] == "admin"){
1125           $a_return[$printer['cn'][0]]= $printer['cn'][0]." - "._("Administrator");
1126         }else{
1127           $a_return[$printer['cn'][0]]= $printer['cn'][0]; 
1128         }
1129         if ($printer['cn'][0] == $this->gosaDefaultPrinter){
1130           $a_return[$printer['cn'][0]].=" - "._("Default printer");
1131         }
1132       }
1133     }
1134     return($a_return);
1135   }
1137   /* Generates ListBox frienly output of used logonscripts 
1138    */
1139   function printOutLogonScripts()
1140   {
1141     $a_return = array();
1142     if(is_array($this->gotoLogonScripts)){
1143       foreach($this->gotoLogonScripts as $script){
1144         $a_return[$script['LogonName']]= $script['LogonPriority']." - ".$script['LogonName']; 
1145       }
1146     }
1147     return($a_return);
1148   }
1154 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1155 ?>