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

Revision 2364, 17.5 KB checked in by amuller, 14 years ago (diff)

Ticket #1008 - Adicionando licença aos arquivos php

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3        /***************************************************************************
4        * Expresso Livre                                                           *
5        * http://www.expressolivre.org                                             *
6        * --------------------------------------------                             *
7        *  This program is free software; you can redistribute it and/or modify it *
8        *  under the terms of the GNU General Public License as published by the   *
9        *  Free Software Foundation; either version 2 of the License, or (at your  *
10        *  option) any later version.                                              *
11        \**************************************************************************/
12
13include_once('class.functions.inc.php');
14
15class imap_functions
16{
17        var $functions;
18        var $imap;
19        var $imapDelimiter;
20        var $imap_admin;
21        var $imap_passwd;
22        var $imap_server;
23        var $imap_port;
24    var $imap_trashfolder;
25    var $imap_sentfolder;
26    var $imap_draftsfolder;
27    var $imap_spamfolder;
28       
29        function imap_functions(){
30                $this->functions        = new functions;
31                $this->imap_admin       = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminUsername'];
32                $this->imap_passwd      = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminPW'];
33                $this->imap_server      = $_SESSION['phpgw_info']['expresso']['email_server']['imapServer'];
34                $this->imap_port        = $_SESSION['phpgw_info']['expresso']['email_server']['imapPort'];
35                $this->imap_createspamfolder    = $_SESSION['phpgw_info']['expresso']['email_server']['imapCreateSpamFolder'];
36                $this->imap_cyrususerspam = $_SESSION['phpgw_info']['expresso']['email_server']['imapCyrusUserPostSpam'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapCyrusUserPostSpam'] : "anonymous";
37                $this->imap_trashfolder  = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  : str_replace("*","", $this->functions->lang("trash"));
38                $this->imap_sentfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   : str_replace("*","", $this->functions->lang("sent"));
39                $this->imap_draftsfolder = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] : str_replace("*","", $this->functions->lang("drafts"));
40                $this->imap_spamfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   : str_replace("*","", $this->functions->lang("spam"));
41                $this->imapDelimiter= $_SESSION['phpgw_info']['expresso']['email_server']['imapDelimiter'];
42                $this->imap             = imap_open('{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}', $this->imap_admin, $this->imap_passwd, OP_HALFOPEN);
43        }
44       
45        function create($uid, $mailquota)
46        {
47                if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
48                {
49                        $error = imap_errors();
50                        if ($error[0] == 'Mailbox already exists')
51                        {
52                                $result['status'] = true;
53                        }
54                        else
55                        {
56                                $result['status'] = false;
57                                $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(INBOX) ($uid):" . $error[0];
58                        }
59                        return $result;
60                }
61                if ( (!empty($this->imap_sentfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_sentfolder)) )
62                {
63                        $error = imap_errors();
64                        $result['status'] = false;
65                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_sentfolder."):" . $error[0];
66                        return $result;
67                }
68                if ( (!empty($this->imap_draftsfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_draftsfolder)) )
69                {
70                        $error = imap_errors();
71                        $result['status'] = false;
72                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_draftsfolder."):" . $error[0];
73                        return $result;
74                }
75                if ( (!empty($this->imap_trashfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_trashfolder)) )
76                {
77                        $error = imap_errors();
78                        $result['status'] = false;
79                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_trashfolder."):" . $error[0];
80                        return $result;
81                }
82                if (!empty($this->imap_spamfolder) && $this->imap_createspamfolder == 'yes')
83                {
84                    if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_spamfolder))
85                        {
86                            $error = imap_errors();
87                            $result['status'] = false;
88                            $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_spamfolder."):" . $error[0];
89                            return $result;
90                        } else {
91                                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid.  $this->imapDelimiter . $this->imap_spamfolder , $this->imap_cyrususerspam, 'p');                       
92         }
93                }               
94
95                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($mailquota*1024)))
96                {
97                        $error = imap_errors();
98                        $result['status'] = false;
99                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(imap_set_quota):" . $error[0];
100                        return $result;
101                }
102               
103                $result['status'] = true;
104                return $result;
105        }
106       
107        function get_user_info($uid)
108        {
109                $get_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
110               
111                if (count($get_quota) == 0)
112                {
113                        $quota['mailquota'] = '-1';
114                        $quota['mailquota_used'] = '-1';
115                }       
116                else
117                {
118                        $quota['mailquota'] = round (($get_quota['limit'] / 1024), 2);
119                        $quota['mailquota_used'] = round (($get_quota['usage'] / 1024), 2);
120                }
121                return $quota;
122        }
123       
124        function change_user_quota($uid, $quota)
125        {
126                $result['status'] = true;
127               
128                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($quota*1024)) )
129                {
130                        $result['status'] = false;
131                        $result['msg'] = $this->functions->lang('it was not possible to change users mailbox quota') . ".\n";
132                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
133                }
134               
135                return $result;
136        }
137       
138        function delete_mailbox($uid)
139        {
140                $result['status'] = true;
141               
142                //Seta acl imap para poder deletar o user.
143                // Esta sem tratamento de erro, pois o retorno da funcao deve ter um bug.
144                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'c');
145               
146                if (!imap_deletemailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
147                {
148                        $result['status'] = false;
149                        $result['msg'] = $this->functions->lang('it was not possible to delete mailbox') . ".\n";
150                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
151                }
152               
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                return $result;
330        }
331        function save_shared_account($params){           
332            //Rename mailbox
333            $result = Array();
334            $result['status'] = true;
335            if($params['uid'] != $params['old_uid'] ){
336                    $result = $this->rename_mailbox($params['old_uid'], $params['uid']);
337                    if(!$result['status']) return $result;
338            }
339
340            //Begin edit Quota
341            $quota = $this->get_user_info($params['uid'] );
342
343            if ( $quota['mailquota'] != $params['mailquota'] ){
344                    if(!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_quote') )
345                    {
346                                $result['status'] = false;
347                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox quota');
348                                return $result;
349                    } else $this->change_user_quota($params['old_uid'], $params['mailquota']);
350            }
351            //End Edit Quota
352 
353            /* Begin Set ACL */
354            //Old users set in the box
355            $owners_acl_old = $this->getaclfrombox($params);
356            //new settings
357            //necessario devido a um bug do serialize do javascript do connector zera uma string
358            //serializada "N;", diferente da serializada do php "a:0:{}"
359           
360            if( $params['owners_acl'] != "N;" ){
361                $owners_acl_new = unserialize($params['owners_acl']);
362            } else {
363                $owners_acl_new = Array();           
364            }
365
366            $owners_to_remove = array_diff_assoc($owners_acl_old,$owners_acl_new);
367            $owners_to_add = array_diff_assoc($owners_acl_new,$owners_acl_old);
368                       
369            //Find modified users
370            $tmp_update = array_diff_assoc($owners_acl_old,$owners_to_remove);
371            $owners_to_update = Array();
372            foreach($tmp_update  as $user => $acl){
373                if($owners_acl_old[$user] != $acl){
374                    $owners_to_update[$user] = $acl;
375                }
376            }
377           
378            //Check Modify manage acl
379           
380            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') ){
381                $result['status'] = false;
382                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox acl');
383                                return $result;
384            }
385            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));
386            if( is_array($owners_acl_new)){
387                foreach($owners_to_remove as $user => $acl){
388                    $params['user'] = $user;
389                    $params['acl'] = "";
390                    $user = $params['user'];                 
391                    $result = $this->setaclfrombox($user,"",$params['uid']);
392                }
393                //add new users
394                foreach($owners_to_add as $user => $acl){
395                    $params['user'] = $user;
396                    $params['acl'] = $acl;
397                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
398                }
399                //update users
400                 foreach($owners_to_update as $user => $acl){
401                    $params['user'] = $user;
402                    $params['acl'] = $acl;
403                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
404                }               
405            }
406            /* End Set Acl */
407            return $result;
408        }
409}
Note: See TracBrowser for help on using the repository browser.