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

Revision 5850, 19.3 KB checked in by acoutinho, 12 years ago (diff)

Ticket #2577 - Problemas com a criacaoo/Edicao de contas compartilhadas

  • 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               
150                return $result;
151        }
152       
153        function rename_mailbox($old_mailbox, $new_mailbox)
154        {
155                $result['status'] = true;
156
157                if (!$quota = @imap_get_quotaroot($this->imap, 'user' . $this->imapDelimiter . $old_mailbox))
158                {
159                                $result['status'] = false;
160                                $result['msg'] = $this->functions->lang("Error getting user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
161                                return $result;
162                }
163                $limit = $quota['STORAGE']['limit'];
164                $usage = $quota['STORAGE']['usage'];
165               
166                if ($usage >= $limit)
167                {
168                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $old_mailbox, (int)($usage+10240)) )
169                        {
170                                $result['status'] = false;
171                                $result['msg'] = $this->functions->lang("Error increasing user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
172                                return $result;
173                        }
174                }
175
176               
177                if (! @imap_renamemailbox($this->imap,
178                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox,
179                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox) )
180                {
181                        $result['status'] = false;
182                        $result['msg'] = $this->functions->lang('Server returns') . ': ' . imap_last_error();
183
184                }
185
186
187                if ($usage >= $limit)
188                {
189                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $new_mailbox, (int)($limit)) )
190                        {
191                                $result['status'] = false;
192                                $result['msg'] .= $this->functions->lang("Error returning user quota.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
193                               
194                                @imap_renamemailbox($this->imap,
195                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox,
196                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox);
197                        }
198                }
199
200                return $result;
201        }
202        function empty_user_inbox($params){
203               if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
204                {
205                        $result['status'] = false;
206                        $result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
207                        return $result;
208                } else return $this->empty_inbox($params);
209               
210        }
211        function empty_shared_account_inbox($params){
212            if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox'))
213                {
214                        $result['status'] = false;
215                        $result['msg'] = $this->functions->lang('You do not have right to empty an shared account inbox');
216                        return $result;
217                } else return $this->empty_inbox($params);
218        }
219        function empty_inbox($params)
220        {
221                // Verifica o acesso do gerente
222                if (!($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox') ||
223                      $this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox')
224                  ))
225                {
226                        $result['status'] = false;
227                        $result['msg'] = $this->functions->lang('You do not have access to clean an inbox');
228                        return $result;
229                }
230
231                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
232                {
233                        $imap_options = '/tls/novalidate-cert';
234                }
235                else
236                {
237                        $imap_options = '/notls/novalidate-cert';
238                }
239
240               
241                $result['status'] = true;
242                $uid = $params['uid'];
243               
244                $return_setacl = imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'lrswipcda');
245               
246                if ($return_setacl)
247                {
248                        $mbox_stream = imap_open('{'.$this->imap_server.':'.$this->imap_port.$imap_options .'}user'. $this->imapDelimiter . $uid, $this->imap_admin, $this->imap_passwd);
249                       
250                        $check = imap_mailboxmsginfo($mbox_stream);
251                        $inbox_size = (string)(round ((($check->Size)/(1024*1024)), 2));
252                       
253                        $return_imap_delete = imap_delete($mbox_stream,'1:*');
254                        imap_close($mbox_stream, CL_EXPUNGE);
255                       
256                        imap_setacl ($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, '');
257                       
258                        if ($return_imap_delete)
259                        {
260                                $result['inbox_size'] = $inbox_size;
261                               
262                                $get_user_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
263                                $result['mailquota_used'] = (string)(round(($get_user_quota['usage']/1024), 2));
264                        }
265                        else
266                        {
267                                $result['status'] = false;
268                                $result['msg'] = $this->functions->lang('It was not possible clean the inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
269                        }
270                }
271                else
272                {
273                        $result['status'] = false;
274                        $result['msg'] = $this->functions->lang('It was not possible to modify the users acl') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
275                }
276                return $result;
277        }
278        function getaclfrombox($params)
279        {
280                $boxacl = $params['uid'];
281               
282                $return = array();     
283               
284                $mbox_acl = imap_getacl($this->imap, "user" . $this->imapDelimiter . $boxacl);
285               
286                foreach ($mbox_acl as $user => $acl)
287                {
288                        if ($user != $boxacl )
289                        {
290                                $return[$user] = $acl;
291                        }
292                }
293                return $return;
294        }
295        function setaclfrombox($user, $acl, $mailbox)
296        {
297                $serverString = '{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}';
298                $mailboxes_list = imap_getmailboxes($this->imap, $serverString, "user".$this->imapDelimiter.$mailbox.$this->imapDelimiter."*");               
299                $result = Array();
300                $result['status'] = true;
301                if (is_array($mailboxes_list))
302                {
303                        $folder = str_replace($serverString, "", imap_utf7_encode("user".$this->imapDelimiter.$mailbox));
304                        $folder = str_replace("&-", "&", $folder);
305
306                        if (imap_setacl ($this->imap, $folder, $user, $acl) ) {
307                            foreach ($mailboxes_list as $key => $val)
308                            {
309                                    $folder = str_replace($serverString, "", imap_utf7_encode($val->name));
310                                    $folder = str_replace("&-", "&", $folder);
311                                    if (!imap_setacl ($this->imap, $folder, $user, $acl))
312                                    {
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                        } else {
320                            $result['status'] = false;
321                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
322                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
323                            break;
324                        }
325                }
326
327                if( $result['status'] )
328                    $this->sendACLAdvice( $user, $acl, $mailbox );
329
330                return $result;
331        }
332        function save_shared_account($params){           
333            //Rename mailbox
334            $result = Array();
335            $result['status'] = true;
336            if($params['uid'] != $params['old_uid'] ){
337                    $result = $this->rename_mailbox($params['old_uid'], $params['uid']);
338                    if(!$result['status']) return $result;
339            }
340
341            //Begin edit Quota
342            $quota = $this->get_user_info($params['uid'] );
343
344            if ( $quota['mailquota'] != $params['mailquota'] ){
345                    if(!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_quote') )
346                    {
347                                $result['status'] = false;
348                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox quota');
349                                return $result;
350                    } else $this->change_user_quota($params['old_uid'], $params['mailquota']);
351            }
352            //End Edit Quota
353 
354            /* Begin Set ACL */
355            //Old users set in the box
356            $owners_acl_old = $this->getaclfrombox($params);
357            //new settings
358            //necessario devido a um bug do serialize do javascript do connector zera uma string
359            //serializada "N;", diferente da serializada do php "a:0:{}"
360           
361            if( $params['owners_acl'] != "N;" ){
362                $owners_acl_new = unserialize($params['owners_acl']);
363            } else {
364                $owners_acl_new = Array();           
365            }
366
367            $owners_to_remove = array_diff_assoc($owners_acl_old,$owners_acl_new);
368            $owners_to_add = array_diff_assoc($owners_acl_new,$owners_acl_old);
369                       
370            //Find modified users
371            $tmp_update = array_diff_assoc($owners_acl_old,$owners_to_remove);
372            $owners_to_update = Array();
373            foreach($tmp_update  as $user => $acl){
374                if($owners_acl_old[$user] != $acl){
375                    $owners_to_update[$user] = $acl;
376                }
377            }
378           
379            //Check Modify manage acl
380           
381            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') ){
382                $result['status'] = false;
383                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox acl');
384                                return $result;
385            }
386            //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));
387            if( is_array($owners_acl_new)){
388                foreach($owners_to_remove as $user => $acl){
389                    $params['user'] = $user;
390                    $params['acl'] = "";
391                    $user = $params['user'];                 
392                    $result = $this->setaclfrombox($user,"",$params['uid']);
393                }
394                //add new users
395                foreach($owners_to_add as $user => $acl){
396                    $params['user'] = $user;
397                    $params['acl'] = $acl;
398                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
399                }
400                //update users
401                 foreach($owners_to_update as $user => $acl){
402                    $params['user'] = $user;
403                    $params['acl'] = $acl;
404                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
405                }               
406            }
407            /* End Set Acl */
408            return $result;
409        }
410       
411        function sendACLAdvice( $user, $acls, $shared_account )
412        {
413            //acl treat
414            $acl_labels = array( 'lrs' => 'read messages from this shared account',
415                                 'd' => 'delete/move messages from this shared account',
416                                 'wi' => 'create/add messages in this shared account',
417                                 'a' => 'send message by this shared account',
418                                 'p' => 'save sent messages in this shared account',
419                                 'c' => 'create or delete folders on this shared account' );
420
421            $acl_found = array();
422
423            foreach( $acl_labels as $acl => $label )
424            {
425                if( strpos( $acls, $acl ) !== false )
426                {
427                    $acl_found[] = $this->functions->lang( $label );
428                }
429            }
430
431            $acl = implode( "<br/>", $acl_found );
432
433            if( empty( $acl ) )
434            return;
435
436            //body mail template generation
437            $body = $this->getTemplate( "body_email.tpl", array( "user" => $user,
438                                                                 "acl" => $acl,
439                                                                 "shared_account" => $shared_account ) );
440
441            //ldap fetch mail to
442            require_once('class.ldap_functions.inc.php');
443
444            if( !$ldap )
445            {
446                $ldap = new ldap_functions();
447            }
448
449            $to = $ldap->uid2mailforwardingaddress( $user );
450            $to = $to['mail'];
451
452            //mail send service
453            $mail = ServiceLocator::getService( 'mail' );
454            $mail->sendMail( $to, false, $this->functions->lang("Your user was add/updated in the shared account"), $body );
455        }
456
457        function getTemplate( $tpls, $macros, $target = false )
458        {
459            require_once( ROOT.'/header.inc.php' );
460
461            $template = CreateObject( 'phpgwapi.Template', PHPGW_APP_TPL );
462
463            if( !is_array( $tpls ) )
464            {
465                $tpls = array( $tpls );
466            }
467
468            $keys = array_keys( $tpls );
469
470            if( !array_diff_key( $tpl, array_keys( $keys ) ) )
471            {
472                $newTpls = array();
473
474                foreach( $tpls as $tpl )
475                {
476                    $key = basename( $tpl );
477
478                    $dot = strrpos( $key, '.' );
479
480                    if( $dot !== false )
481                    {
482                        $key = substr( $key, 0, $dot );
483                    }
484
485                    $newTpls[ $key ] = $tpl;
486                }
487
488                $tpls = $newTpls;
489
490                $keys = array_keys( $tpls );
491            }
492
493            $template->set_file( $tpls );
494
495            $target = $target ? $tpls[ $target ] : $keys[0];
496
497            $template->set_var( $this->functions->make_dinamic_lang( $template, $target ) );
498            $template->set_var( $macros );
499
500            return $template->fp( 'out', $target );
501        }
502}
Note: See TracBrowser for help on using the repository browser.