source: branches/2.2/expressoAdmin1_2/inc/class.imap_functions.inc.php @ 1983

Revision 1983, 16.7 KB checked in by valmir.sena, 14 years ago (diff)

Ticket #882 - Falha ao tentar criar uma conta compartilhada no IE

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3include_once('class.functions.inc.php');
4
5class imap_functions
6{
7        var $functions;
8        var $imap;
9        var $imapDelimiter;
10        var $imap_admin;
11        var $imap_passwd;
12        var $imap_server;
13        var $imap_port;
14    var $imap_trashfolder;
15    var $imap_sentfolder;
16    var $imap_draftsfolder;
17    var $imap_spamfolder;
18       
19        function imap_functions(){
20                $this->functions        = new functions;
21                $this->imap_admin       = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminUsername'];
22                $this->imap_passwd      = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminPW'];
23                $this->imap_server      = $_SESSION['phpgw_info']['expresso']['email_server']['imapServer'];
24                $this->imap_port        = $_SESSION['phpgw_info']['expresso']['email_server']['imapPort'];
25                $this->imap_createspamfolder    = $_SESSION['phpgw_info']['expresso']['email_server']['imapCreateSpamFolder'];
26                $this->imap_cyrususerspam = $_SESSION['phpgw_info']['expresso']['email_server']['imapCyrusUserPostSpam'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapCyrusUserPostSpam'] : "anonymous";
27                $this->imap_trashfolder  = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  : str_replace("*","", $this->functions->lang("trash"));
28                $this->imap_sentfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   : str_replace("*","", $this->functions->lang("sent"));
29                $this->imap_draftsfolder = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] : str_replace("*","", $this->functions->lang("drafts"));
30                $this->imap_spamfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   : str_replace("*","", $this->functions->lang("spam"));
31                $this->imapDelimiter= $_SESSION['phpgw_info']['expresso']['email_server']['imapDelimiter'];
32                $this->imap             = imap_open('{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}', $this->imap_admin, $this->imap_passwd, OP_HALFOPEN);
33        }
34       
35        function create($uid, $mailquota)
36        {
37                if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
38                {
39                        $error = imap_errors();
40                        if ($error[0] == 'Mailbox already exists')
41                        {
42                                $result['status'] = true;
43                        }
44                        else
45                        {
46                                $result['status'] = false;
47                                $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(INBOX) ($uid):" . $error[0];
48                        }
49                        return $result;
50                }
51                if ( (!empty($this->imap_sentfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_sentfolder)) )
52                {
53                        $error = imap_errors();
54                        $result['status'] = false;
55                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_sentfolder."):" . $error[0];
56                        return $result;
57                }
58                if ( (!empty($this->imap_draftsfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_draftsfolder)) )
59                {
60                        $error = imap_errors();
61                        $result['status'] = false;
62                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_draftsfolder."):" . $error[0];
63                        return $result;
64                }
65                if ( (!empty($this->imap_trashfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_trashfolder)) )
66                {
67                        $error = imap_errors();
68                        $result['status'] = false;
69                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_trashfolder."):" . $error[0];
70                        return $result;
71                }
72                if (!empty($this->imap_spamfolder) && $this->imap_createspamfolder == 'yes')
73                {
74                    if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_spamfolder))
75                        {
76                            $error = imap_errors();
77                            $result['status'] = false;
78                            $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_spamfolder."):" . $error[0];
79                            return $result;
80                        } else {
81                                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid.  $this->imapDelimiter . $this->imap_spamfolder , $this->imap_cyrususerspam, 'p');                       
82         }
83                }               
84
85                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($mailquota*1024)))
86                {
87                        $error = imap_errors();
88                        $result['status'] = false;
89                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(imap_set_quota):" . $error[0];
90                        return $result;
91                }
92               
93                $result['status'] = true;
94                return $result;
95        }
96       
97        function get_user_info($uid)
98        {
99                $get_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
100               
101                if (count($get_quota) == 0)
102                {
103                        $quota['mailquota'] = '-1';
104                        $quota['mailquota_used'] = '-1';
105                }       
106                else
107                {
108                        $quota['mailquota'] = round (($get_quota['limit'] / 1024), 2);
109                        $quota['mailquota_used'] = round (($get_quota['usage'] / 1024), 2);
110                }
111                return $quota;
112        }
113       
114        function change_user_quota($uid, $quota)
115        {
116                $result['status'] = true;
117               
118                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($quota*1024)) )
119                {
120                        $result['status'] = false;
121                        $result['msg'] = $this->functions->lang('it was not possible to change users mailbox quota') . ".\n";
122                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
123                }
124               
125                return $result;
126        }
127       
128        function delete_mailbox($uid)
129        {
130                $result['status'] = true;
131               
132                //Seta acl imap para poder deletar o user.
133                // Esta sem tratamento de erro, pois o retorno da funcao deve ter um bug.
134                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'c');
135               
136                if (!imap_deletemailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
137                {
138                        $result['status'] = false;
139                        $result['msg'] = $this->functions->lang('it was not possible to delete mailbox') . ".\n";
140                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
141                }
142               
143                return $result;
144        }
145       
146        function rename_mailbox($old_mailbox, $new_mailbox)
147        {
148                $result['status'] = true;
149
150                if (!$quota = @imap_get_quotaroot($this->imap, 'user' . $this->imapDelimiter . $old_mailbox))
151                {
152                                $result['status'] = false;
153                                $result['msg'] = $this->functions->lang("Error getting user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
154                                return $result;
155                }
156                $limit = $quota['STORAGE']['limit'];
157                $usage = $quota['STORAGE']['usage'];
158               
159                if ($usage >= $limit)
160                {
161                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $old_mailbox, (int)($usage+10240)) )
162                        {
163                                $result['status'] = false;
164                                $result['msg'] = $this->functions->lang("Error increasing user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
165                                return $result;
166                        }
167                }
168
169               
170                if (! @imap_renamemailbox($this->imap,
171                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox,
172                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox) )
173                {
174                        $result['status'] = false;
175                        $result['msg'] = $this->functions->lang('Server returns') . ': ' . imap_last_error();
176
177                }
178
179
180                if ($usage >= $limit)
181                {
182                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $new_mailbox, (int)($limit)) )
183                        {
184                                $result['status'] = false;
185                                $result['msg'] .= $this->functions->lang("Error returning user quota.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
186                               
187                                @imap_renamemailbox($this->imap,
188                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox,
189                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox);
190                        }
191                }
192
193                return $result;
194        }
195        function empty_user_inbox($params){
196               if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
197                {
198                        $result['status'] = false;
199                        $result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
200                        return $result;
201                } else return $this->empty_inbox($params);
202               
203        }
204        function empty_shared_account_inbox($params){
205            if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox'))
206                {
207                        $result['status'] = false;
208                        $result['msg'] = $this->functions->lang('You do not have right to empty an shared account inbox');
209                        return $result;
210                } else return $this->empty_inbox($params);
211        }
212        function empty_inbox($params)
213        {
214                // Verifica o acesso do gerente
215                if (!($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox') ||
216                      $this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox')
217                  ))
218                {
219                        $result['status'] = false;
220                        $result['msg'] = $this->functions->lang('You do not have access to clean an inbox');
221                        return $result;
222                }
223
224                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
225                {
226                        $imap_options = '/tls/novalidate-cert';
227                }
228                else
229                {
230                        $imap_options = '/notls/novalidate-cert';
231                }
232
233               
234                $result['status'] = true;
235                $uid = $params['uid'];
236               
237                $return_setacl = imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'lrswipcda');
238               
239                if ($return_setacl)
240                {
241                        $mbox_stream = imap_open('{'.$this->imap_server.':'.$this->imap_port.$imap_options .'}user'. $this->imapDelimiter . $uid, $this->imap_admin, $this->imap_passwd);
242                       
243                        $check = imap_mailboxmsginfo($mbox_stream);
244                        $inbox_size = (string)(round ((($check->Size)/(1024*1024)), 2));
245                       
246                        $return_imap_delete = imap_delete($mbox_stream,'1:*');
247                        imap_close($mbox_stream, CL_EXPUNGE);
248                       
249                        imap_setacl ($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, '');
250                       
251                        if ($return_imap_delete)
252                        {
253                                $result['inbox_size'] = $inbox_size;
254                               
255                                $get_user_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
256                                $result['mailquota_used'] = (string)(round(($get_user_quota['usage']/1024), 2));
257                        }
258                        else
259                        {
260                                $result['status'] = false;
261                                $result['msg'] = $this->functions->lang('It was not possible clean the inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
262                        }
263                }
264                else
265                {
266                        $result['status'] = false;
267                        $result['msg'] = $this->functions->lang('It was not possible to modify the users acl') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
268                }
269                return $result;
270        }
271        function getaclfrombox($params)
272        {
273                $boxacl = $params['uid'];
274               
275                $return = array();     
276               
277                $mbox_acl = imap_getacl($this->imap, "user" . $this->imapDelimiter . $boxacl);
278               
279                foreach ($mbox_acl as $user => $acl)
280                {
281                        if ($user != $boxacl )
282                        {
283                                $return[$user] = $acl;
284                        }
285                }
286                return $return;
287        }
288        function setaclfrombox($user, $acl, $mailbox)
289        {
290                $serverString = '{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}';
291                $mailboxes_list = imap_getmailboxes($this->imap, $serverString, "user".$this->imapDelimiter.$mailbox.$this->imapDelimiter."*");               
292                $result = Array();
293                $result['status'] = true;
294                if (is_array($mailboxes_list))
295                {
296                        $folder = str_replace($serverString, "", imap_utf7_encode("user".$this->imapDelimiter.$mailbox));
297                        $folder = str_replace("&-", "&", $folder);
298
299                        if (imap_setacl ($this->imap, $folder, $user, $acl) ) {
300                            foreach ($mailboxes_list as $key => $val)
301                            {
302                                    $folder = str_replace($serverString, "", imap_utf7_encode($val->name));
303                                    $folder = str_replace("&-", "&", $folder);
304                                    if (!imap_setacl ($this->imap, $folder, $user, $acl))
305                                    {
306                                            $result['status'] = false;
307                                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
308                                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
309                                            break;
310                                    }
311                            }
312                        } else {
313                            $result['status'] = false;
314                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
315                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
316                            break;
317                        }
318                }
319                return $result;
320        }
321        function save_shared_account($params){           
322            //Rename mailbox
323            $result = Array();
324            $result['status'] = true;
325            if($params['uid'] != $params['old_uid'] ){
326                    $result = $this->rename_mailbox($params['old_uid'], $params['uid']);
327                    if(!$result['status']) return $result;
328            }
329
330            //Begin edit Quota
331            $quota = $this->get_user_info($params['uid'] );
332
333            if ( $quota['mailquota'] != $params['mailquota'] ){
334                    if(!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_quote') )
335                    {
336                                $result['status'] = false;
337                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox quota');
338                                return $result;
339                    } else $this->change_user_quota($params['old_uid'], $params['mailquota']);
340            }
341            //End Edit Quota
342 
343            /* Begin Set ACL */
344            //Old users set in the box
345            $owners_acl_old = $this->getaclfrombox($params);
346            //new settings
347            //necessario devido a um bug do serialize do javascript do connector zera uma string
348            //serializada "N;", diferente da serializada do php "a:0:{}"
349           
350            if( $params['owners_acl'] != "N;" ){
351                $owners_acl_new = unserialize($params['owners_acl']);
352            } else {
353                $owners_acl_new = Array();           
354            }
355
356            $owners_to_remove = array_diff_assoc($owners_acl_old,$owners_acl_new);
357            $owners_to_add = array_diff_assoc($owners_acl_new,$owners_acl_old);
358                       
359            //Find modified users
360            $tmp_update = array_diff_assoc($owners_acl_old,$owners_to_remove);
361            $owners_to_update = Array();
362            foreach($tmp_update  as $user => $acl){
363                if($owners_acl_old[$user] != $acl){
364                    $owners_to_update[$user] = $acl;
365                }
366            }
367           
368            //Check Modify manage acl
369           
370            if( (count($owners_to_remove) > 0 || count($owners_to_add) > 0 || count($owners_to_update) > 0) && !$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_acl') ){
371                $result['status'] = false;
372                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox acl');
373                                return $result;
374            }
375            file_put_contents("/tmp/saida", "old ".print_r($owners_acl_old, true)."remove ".print_r($owners_to_remove, true)."add ".print_r($owners_to_add, true)."update ".print_r($owners_to_update, true));
376            if( is_array($owners_acl_new)){
377                foreach($owners_to_remove as $user => $acl){
378                    $params['user'] = $user;
379                    $params['acl'] = "";
380                    $user = $params['user'];                 
381                    $result = $this->setaclfrombox($user,"",$params['uid']);
382                }
383                //add new users
384                foreach($owners_to_add as $user => $acl){
385                    $params['user'] = $user;
386                    $params['acl'] = $acl;
387                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
388                }
389                //update users
390                 foreach($owners_to_update as $user => $acl){
391                    $params['user'] = $user;
392                    $params['acl'] = $acl;
393                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
394                }               
395            }
396            /* End Set Acl */
397            return $result;
398        }
399}
Note: See TracBrowser for help on using the repository browser.