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

Revision 5133, 18.6 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo ExpressoAdmin.

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