source: trunk/expressoAdmin1_2/inc/class.imap_functions.inc.php @ 7551

Revision 7551, 19.4 KB checked in by angelo, 11 years ago (diff)

Ticket #3197 - Reduzir tempo de carregamento do modulo Expresso MailexpressoMail1_2/js/draw_api.min.js

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2                /***************************************************************************
3                * Expresso Livre                                                           *
4                * http://www.expressolivre.org                                             *
5                * --------------------------------------------                             *
6                *  This program is free software; you can redistribute it and/or modify it *
7                *  under the terms of the GNU General Public License as published by the   *
8                *  Free Software Foundation; either version 2 of the License, or (at your  *
9                *  option) any later version.                                              *
10                \**************************************************************************/
11               
12include_once('class.functions.inc.php');
13
14class imap_functions
15{
16        var $functions;
17        var $imap;
18        var $imapDelimiter;
19        var $imap_admin;
20        var $imap_passwd;
21        var $imap_server;
22        var $imap_port;
23    var $imap_trashfolder;
24    var $imap_sentfolder;
25    var $imap_draftsfolder;
26    var $imap_spamfolder;
27       
28        function imap_functions(){
29                $this->functions        = new functions;
30                $this->imap_admin       = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminUsername'];
31                $this->imap_passwd      = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminPW'];
32                $this->imap_server      = $_SESSION['phpgw_info']['expresso']['email_server']['imapServer'];
33                $this->imap_port        = $_SESSION['phpgw_info']['expresso']['email_server']['imapPort'];
34                $this->imap_trashfolder  = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  : str_replace("*","", $this->functions->lang("trash"));
35                $this->imap_sentfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   : str_replace("*","", $this->functions->lang("sent"));
36                $this->imap_draftsfolder = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] : str_replace("*","", $this->functions->lang("drafts"));
37                $this->imap_spamfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   : str_replace("*","", $this->functions->lang("spam"));
38                $this->imapDelimiter= $_SESSION['phpgw_info']['expresso']['email_server']['imapDelimiter'];
39                $this->imap             = imap_open('{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}', $this->imap_admin, $this->imap_passwd, OP_HALFOPEN);
40        }
41       
42        function create($uid, $mailquota)
43        {
44                if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
45                {
46                        $error = imap_errors();
47                        if ($error[0] == 'Mailbox already exists')
48                        {
49                                $result['status'] = true;
50                        }
51                        else
52                        {
53                                $result['status'] = false;
54                                $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(INBOX) ($uid):" . $error[0];
55                        }
56                        return $result;
57                }
58                if ( (!empty($this->imap_sentfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_sentfolder)) )
59                {
60                        $error = imap_errors();
61                        $result['status'] = false;
62                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_sentfolder."):" . $error[0];
63                        return $result;
64                }
65                if ( (!empty($this->imap_draftsfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_draftsfolder)) )
66                {
67                        $error = imap_errors();
68                        $result['status'] = false;
69                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_draftsfolder."):" . $error[0];
70                        return $result;
71                }
72                if ( (!empty($this->imap_trashfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_trashfolder)) )
73                {
74                        $error = imap_errors();
75                        $result['status'] = false;
76                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_trashfolder."):" . $error[0];
77                        return $result;
78                }
79                /* Esperando correção do william (prognus) sobre a utilização do DSPAM
80                if (!empty($this->imap_sentfolder))
81                {
82                    if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_spamfolder))
83                        {
84                            $error = imap_errors();
85                            $result['status'] = false;
86                            $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_spamfolder."):" . $error[0];
87                            return $result;
88         }
89                }               
90                */
91
92                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($mailquota*1024)))
93                {
94                        $error = imap_errors();
95                        $result['status'] = false;
96                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(imap_set_quota):" . $error[0];
97                        return $result;
98                }
99               
100                $result['status'] = true;
101                return $result;
102        }
103       
104        function get_user_info($uid)
105        {
106                $get_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
107               
108                if (count($get_quota) == 0)
109                {
110                        $quota['mailquota'] = '-1';
111                        $quota['mailquota_used'] = '-1';
112                }       
113                else
114                {
115                        $quota['mailquota'] = round (($get_quota['limit'] / 1024), 2);
116                        $quota['mailquota_used'] = round (($get_quota['usage'] / 1024), 2);
117                }
118                return $quota;
119        }
120       
121        function change_user_quota($uid, $quota)
122        {
123                $result['status'] = true;
124               
125                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($quota*1024)) )
126                {
127                        $result['status'] = false;
128                        $result['msg'] = $this->functions->lang('it was not possible to change users mailbox quota') . ".\n";
129                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
130                }
131               
132                return $result;
133        }
134       
135        function delete_mailbox($uid)
136        {
137                $result['status'] = true;
138               
139                //Seta acl imap para poder deletar o user.
140                // Esta sem tratamento de erro, pois o retorno da funcao deve ter um bug.
141                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'c');
142               
143                if (!imap_deletemailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
144                {
145                        $result['status'] = false;
146                        $result['msg'] = $this->functions->lang('it was not possible to delete mailbox') . ".\n";
147                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
148                }
149                ob_start();
150                print_r($result);
151                $output = ob_get_clean();
152                file_put_contents( "/tmp/angelo.log",  $output , FILE_APPEND);         
153                return $result;
154        }
155       
156        function rename_mailbox($old_mailbox, $new_mailbox)
157        {
158                $result['status'] = true;
159
160                if (!$quota = @imap_get_quotaroot($this->imap, 'user' . $this->imapDelimiter . $old_mailbox))
161                {
162                                $result['status'] = false;
163                                $result['msg'] = $this->functions->lang("Error getting user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
164                                return $result;
165                }
166                $limit = $quota['STORAGE']['limit'];
167                $usage = $quota['STORAGE']['usage'];
168               
169                if ($usage >= $limit)
170                {
171                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $old_mailbox, (int)($usage+10240)) )
172                        {
173                                $result['status'] = false;
174                                $result['msg'] = $this->functions->lang("Error increasing user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
175                                return $result;
176                        }
177                }
178
179               
180                if (! @imap_renamemailbox($this->imap,
181                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox,
182                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox) )
183                {
184                        $result['status'] = false;
185                        $result['msg'] = $this->functions->lang('Server returns') . ': ' . imap_last_error();
186
187                }
188
189
190                if ($usage >= $limit)
191                {
192                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $new_mailbox, (int)($limit)) )
193                        {
194                                $result['status'] = false;
195                                $result['msg'] .= $this->functions->lang("Error returning user quota.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
196                               
197                                @imap_renamemailbox($this->imap,
198                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox,
199                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox);
200                        }
201                }
202
203                return $result;
204        }
205        function empty_user_inbox($params){
206               if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
207                {
208                        $result['status'] = false;
209                        $result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
210                        return $result;
211                } else return $this->empty_inbox($params);
212               
213        }
214        function empty_shared_account_inbox($params){
215            if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox'))
216                {
217                        $result['status'] = false;
218                        $result['msg'] = $this->functions->lang('You do not have right to empty an shared account inbox');
219                        return $result;
220                } else return $this->empty_inbox($params);
221        }
222        function empty_inbox($params)
223        {
224                // Verifica o acesso do gerente
225                if (!($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox') ||
226                      $this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox')
227                  ))
228                {
229                        $result['status'] = false;
230                        $result['msg'] = $this->functions->lang('You do not have access to clean an inbox');
231                        return $result;
232                }
233
234                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
235                {
236                        $imap_options = '/tls/novalidate-cert';
237                }
238                else
239                {
240                        $imap_options = '/notls/novalidate-cert';
241                }
242
243               
244                $result['status'] = true;
245                $uid = $params['uid'];
246               
247                $return_setacl = imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'lrswipcda');
248               
249                if ($return_setacl)
250                {
251                        $mbox_stream = imap_open('{'.$this->imap_server.':'.$this->imap_port.$imap_options .'}user'. $this->imapDelimiter . $uid, $this->imap_admin, $this->imap_passwd);
252                       
253                        $check = imap_mailboxmsginfo($mbox_stream);
254                        $inbox_size = (string)(round ((($check->Size)/(1024*1024)), 2));
255                       
256                        $return_imap_delete = imap_delete($mbox_stream,'1:*');
257                        imap_close($mbox_stream, CL_EXPUNGE);
258                       
259                        imap_setacl ($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, '');
260                       
261                        if ($return_imap_delete)
262                        {
263                                $result['inbox_size'] = $inbox_size;
264                               
265                                $get_user_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
266                                $result['mailquota_used'] = (string)(round(($get_user_quota['usage']/1024), 2));
267                        }
268                        else
269                        {
270                                $result['status'] = false;
271                                $result['msg'] = $this->functions->lang('It was not possible clean the inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
272                        }
273                }
274                else
275                {
276                        $result['status'] = false;
277                        $result['msg'] = $this->functions->lang('It was not possible to modify the users acl') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
278                }
279                return $result;
280        }
281        function getaclfrombox($params)
282        {
283                $boxacl = $params['uid'];
284               
285                $return = array();     
286               
287                $mbox_acl = imap_getacl($this->imap, "user" . $this->imapDelimiter . $boxacl);
288               
289                foreach ($mbox_acl as $user => $acl)
290                {
291                        if ($user != $boxacl )
292                        {
293                                $return[$user] = $acl;
294                        }
295                }
296                return $return;
297        }
298        function setaclfrombox($user, $acl, $mailbox)
299        {
300                $serverString = '{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}';
301                $mailboxes_list = imap_getmailboxes($this->imap, $serverString, "user".$this->imapDelimiter.$mailbox.$this->imapDelimiter."*");               
302                $result = Array();
303                $result['status'] = true;
304                if (is_array($mailboxes_list))
305                {
306                        $folder = str_replace($serverString, "", imap_utf7_encode("user".$this->imapDelimiter.$mailbox));
307                        $folder = str_replace("&-", "&", $folder);
308
309                        if (imap_setacl ($this->imap, $folder, $user, $acl) ) {
310                            foreach ($mailboxes_list as $key => $val)
311                            {
312                                    $folder = str_replace($serverString, "", imap_utf7_encode($val->name));
313                                    $folder = str_replace("&-", "&", $folder);
314                                    if (!imap_setacl ($this->imap, $folder, $user, $acl))
315                                    {
316                                            $result['status'] = false;
317                                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
318                                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
319                                            break;
320                                    }
321                            }
322                        } else {
323                            $result['status'] = false;
324                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
325                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
326                            break;
327                        }
328                }
329
330                if( $result['status'] )
331                    $this->sendACLAdvice( $user, $acl, $mailbox );
332
333                return $result;
334        }
335        function save_shared_account($params){           
336            //Rename mailbox
337            $result = Array();
338            $result['status'] = true;
339            if($params['uid'] != $params['old_uid'] ){
340                    $result = $this->rename_mailbox($params['old_uid'], $params['uid']);
341                    if(!$result['status']) return $result;
342            }
343
344            //Begin edit Quota
345            $quota = $this->get_user_info($params['uid'] );
346
347            if ( $quota['mailquota'] != $params['mailquota'] ){
348                    if(!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_quote') )
349                    {
350                                $result['status'] = false;
351                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox quota');
352                                return $result;
353                    } else $this->change_user_quota($params['old_uid'], $params['mailquota']);
354            }
355            //End Edit Quota
356 
357            /* Begin Set ACL */
358            //Old users set in the box
359            $owners_acl_old = $this->getaclfrombox($params);
360            //new settings
361            //necessario devido a um bug do serialize do javascript do connector zera uma string
362            //serializada "N;", diferente da serializada do php "a:0:{}"
363           
364            if( $params['owners_acl'] != "N;" ){
365                $owners_acl_new = unserialize($params['owners_acl']);
366            } else {
367                $owners_acl_new = Array();           
368            }
369
370            $owners_to_remove = array_diff_assoc($owners_acl_old,$owners_acl_new);
371            $owners_to_add = array_diff_assoc($owners_acl_new,$owners_acl_old);
372                       
373            //Find modified users
374            $tmp_update = array_diff_assoc($owners_acl_old,$owners_to_remove);
375            $owners_to_update = Array();
376            foreach($tmp_update  as $user => $acl){
377                if($owners_acl_old[$user] != $acl){
378                    $owners_to_update[$user] = $acl;
379                }
380            }
381           
382            //Check Modify manage acl
383           
384            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') ){
385                $result['status'] = false;
386                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox acl');
387                                return $result;
388            }
389            //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));
390            if( is_array($owners_acl_new)){
391                foreach($owners_to_remove as $user => $acl){
392                    $params['user'] = $user;
393                    $params['acl'] = "";
394                    $user = $params['user'];                 
395                    $result = $this->setaclfrombox($user,"",$params['uid']);
396                }
397                //add new users
398                foreach($owners_to_add as $user => $acl){
399                    $params['user'] = $user;
400                    $params['acl'] = $acl;
401                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
402                }
403                //update users
404                 foreach($owners_to_update as $user => $acl){
405                    $params['user'] = $user;
406                    $params['acl'] = $acl;
407                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
408                }               
409            }
410            /* End Set Acl */
411            return $result;
412        }
413       
414        function sendACLAdvice( $user, $acls, $shared_account )
415        {
416            //acl treat
417            $acl_labels = array( 'lrs' => 'read messages from this shared account',
418                                 'd' => 'delete/move messages from this shared account',
419                                 'wi' => 'create/add messages in this shared account',
420                                 'a' => 'send message by this shared account',
421                                 'p' => 'save sent messages in this shared account',
422                                 'c' => 'create or delete folders on this shared account' );
423
424            $acl_found = array();
425
426            foreach( $acl_labels as $acl => $label )
427            {
428                if( strpos( $acls, $acl ) !== false )
429                {
430                    $acl_found[] = $this->functions->lang( $label );
431                }
432            }
433
434            $acl = implode( "<br/>", $acl_found );
435
436            if( empty( $acl ) )
437            return;
438
439            //body mail template generation
440            $body = $this->getTemplate( "body_email.tpl", array( "user" => $user,
441                                                                 "acl" => $acl,
442                                                                 "shared_account" => $shared_account ) );
443
444            //ldap fetch mail to
445            require_once('class.ldap_functions.inc.php');
446
447            if( !$ldap )
448            {
449                $ldap = new ldap_functions();
450            }
451
452            $to = $ldap->uid2mailforwardingaddress( $user );
453            $to = $to['mail'];
454
455            //mail send service
456            $mail = ServiceLocator::getService( 'mail' );
457            $mail->sendMail( $to, false, $this->functions->lang("Your user was add/updated in the shared account"), $body );
458        }
459
460        function getTemplate( $tpls, $macros, $target = false )
461        {
462            require_once( ROOT.'/header.inc.php' );
463
464            $template = CreateObject( 'phpgwapi.Template', PHPGW_APP_TPL );
465
466            if( !is_array( $tpls ) )
467            {
468                $tpls = array( $tpls );
469            }
470
471            $keys = array_keys( $tpls );
472
473            if( !array_diff_key( $tpl, array_keys( $keys ) ) )
474            {
475                $newTpls = array();
476
477                foreach( $tpls as $tpl )
478                {
479                    $key = basename( $tpl );
480
481                    $dot = strrpos( $key, '.' );
482
483                    if( $dot !== false )
484                    {
485                        $key = substr( $key, 0, $dot );
486                    }
487
488                    $newTpls[ $key ] = $tpl;
489                }
490
491                $tpls = $newTpls;
492
493                $keys = array_keys( $tpls );
494            }
495
496            $template->set_file( $tpls );
497
498            $target = $target ? $tpls[ $target ] : $keys[0];
499
500            $template->set_var( $this->functions->make_dinamic_lang( $template, $target ) );
501            $template->set_var( $macros );
502
503            return $template->fp( 'out', $target );
504        }
505}
Note: See TracBrowser for help on using the repository browser.