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

Revision 1523, 9.9 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #690 - emailadmin alterado permitindo a integração, ou não, com anti-spam

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3include_once('class.functions.inc.php');
4
5class imap_functions
6{
7        var $functions;
8        var $imap;
9        var $imapDelimiter;
10        var $imap_admin;
11        var $imap_passwd;
12        var $imap_server;
13        var $imap_port;
14    var $imap_trashfolder;
15    var $imap_sentfolder;
16    var $imap_draftsfolder;
17    var $imap_spamfolder;
18       
19        function imap_functions(){
20                $this->functions        = new functions;
21                $this->imap_admin       = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminUsername'];
22                $this->imap_passwd      = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminPW'];
23                $this->imap_server      = $_SESSION['phpgw_info']['expresso']['email_server']['imapServer'];
24                $this->imap_port        = $_SESSION['phpgw_info']['expresso']['email_server']['imapPort'];
25                $this->imap_createspamfolder    = $_SESSION['phpgw_info']['expresso']['email_server']['imapCreateSpamFolder'];
26                $this->imap_cyrususerspam = $_SESSION['phpgw_info']['expresso']['email_server']['imapCyrusUserPostSpam'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapCyrusUserPostSpam'] : "anonymous";
27                $this->imap_trashfolder  = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultTrashFolder']  : str_replace("*","", $this->functions->lang("trash"));
28                $this->imap_sentfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSentFolder']   : str_replace("*","", $this->functions->lang("sent"));
29                $this->imap_draftsfolder = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultDraftsFolder'] : str_replace("*","", $this->functions->lang("drafts"));
30                $this->imap_spamfolder   = $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   ? $_SESSION['phpgw_info']['expresso']['email_server']['imapDefaultSpamFolder']   : str_replace("*","", $this->functions->lang("spam"));
31                $this->imapDelimiter= $_SESSION['phpgw_info']['expresso']['email_server']['imapDelimiter'];
32                $this->imap             = imap_open('{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}', $this->imap_admin, $this->imap_passwd, OP_HALFOPEN);
33        }
34       
35        function create($uid, $mailquota)
36        {
37                if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
38                {
39                        $error = imap_errors();
40                        if ($error[0] == 'Mailbox already exists')
41                        {
42                                $result['status'] = true;
43                        }
44                        else
45                        {
46                                $result['status'] = false;
47                                $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(INBOX) ($uid):" . $error[0];
48                        }
49                        return $result;
50                }
51                if ( (!empty($this->imap_sentfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_sentfolder)) )
52                {
53                        $error = imap_errors();
54                        $result['status'] = false;
55                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_sentfolder."):" . $error[0];
56                        return $result;
57                }
58                if ( (!empty($this->imap_draftsfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_draftsfolder)) )
59                {
60                        $error = imap_errors();
61                        $result['status'] = false;
62                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_draftsfolder."):" . $error[0];
63                        return $result;
64                }
65                if ( (!empty($this->imap_trashfolder)) && (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_trashfolder)) )
66                {
67                        $error = imap_errors();
68                        $result['status'] = false;
69                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_trashfolder."):" . $error[0];
70                        return $result;
71                }
72                if (!empty($this->imap_spamfolder) && $this->imap_createspamfolder == 'yes')
73                {
74                    if (!imap_createmailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid . $this->imapDelimiter . $this->imap_spamfolder))
75                        {
76                            $error = imap_errors();
77                            $result['status'] = false;
78                            $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(".$this->imap_spamfolder."):" . $error[0];
79                            return $result;
80                        } else {
81                                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid.  $this->imapDelimiter . $this->imap_spamfolder , $this->imap_cyrususerspam, 'p');                       
82         }
83                }               
84
85                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($mailquota*1024)))
86                {
87                        $error = imap_errors();
88                        $result['status'] = false;
89                        $result['msg'] = $this->functions->lang('Error on function') . " imap_functions->create(imap_set_quota):" . $error[0];
90                        return $result;
91                }
92               
93                $result['status'] = true;
94                return $result;
95        }
96       
97        function get_user_info($uid)
98        {
99                $get_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
100               
101                if (count($get_quota) == 0)
102                {
103                        $quota['mailquota'] = '-1';
104                        $quota['mailquota_used'] = '-1';
105                }       
106                else
107                {
108                        $quota['mailquota'] = round (($get_quota['limit'] / 1024), 2);
109                        $quota['mailquota_used'] = round (($get_quota['usage'] / 1024), 2);
110                }
111                return $quota;
112        }
113       
114        function change_user_quota($uid, $quota)
115        {
116                $result['status'] = true;
117               
118                if (!imap_set_quota($this->imap,"user" . $this->imapDelimiter . $uid, ($quota*1024)) )
119                {
120                        $result['status'] = false;
121                        $result['msg'] = $this->functions->lang('it was not possible to change users mailbox quota') . ".\n";
122                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
123                }
124               
125                return $result;
126        }
127       
128        function delete_user($uid)
129        {
130                $result['status'] = true;
131               
132                //Seta acl imap para poder deletar o user.
133                // Esta sem tratamento de erro, pois o retorno da funcao deve ter um bug.
134                imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'c');
135               
136                if (!imap_deletemailbox($this->imap, '{'.$this->imap_server.'}' . "user" . $this->imapDelimiter . $uid))
137                {
138                        $result['status'] = false;
139                        $result['msg'] = $this->functions->lang('it was not possible to delete users mailbox') . ".\n";
140                        $result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
141                }
142               
143                return $result;
144        }
145       
146        function rename_mailbox($old_mailbox, $new_mailbox)
147        {
148                $result['status'] = true;
149
150                if (!$quota = @imap_get_quotaroot($this->imap, 'user' . $this->imapDelimiter . $old_mailbox))
151                {
152                                $result['status'] = false;
153                                $result['msg'] = $this->functions->lang("Error getting user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
154                                return $result;
155                }
156                $limit = $quota['STORAGE']['limit'];
157                $usage = $quota['STORAGE']['usage'];
158               
159                if ($usage >= $limit)
160                {
161                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $old_mailbox, (int)($usage+10240)) )
162                        {
163                                $result['status'] = false;
164                                $result['msg'] = $this->functions->lang("Error increasing user quota. Process aborted.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
165                                return $result;
166                        }
167                }
168
169               
170                if (! @imap_renamemailbox($this->imap,
171                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox,
172                                                '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox) )
173                {
174                        $result['status'] = false;
175                        $result['msg'] = $this->functions->lang('Server returns') . ': ' . imap_last_error();
176
177                }
178
179
180                if ($usage >= $limit)
181                {
182                        if (! @imap_set_quota($this->imap, 'user' . $this->imapDelimiter . $new_mailbox, (int)($limit)) )
183                        {
184                                $result['status'] = false;
185                                $result['msg'] .= $this->functions->lang("Error returning user quota.\n") . $this->functions->lang('Server returns') . ': ' . imap_last_error();
186                               
187                                @imap_renamemailbox($this->imap,
188                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $new_mailbox,
189                                        '{'.$this->imap_server.':'.$this->imap_port.'}user' . $this->imapDelimiter . $old_mailbox);
190                        }
191                }
192
193                return $result;
194        }
195       
196        function empty_inbox($params)
197        {
198                // Verifica o acesso do gerente
199                if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
200                {
201                        $result['status'] = false;
202                        $result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
203                        return $result;
204                }
205               
206                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
207                {
208                        $imap_options = '/tls/novalidate-cert';
209                }
210                else
211                {
212                        $imap_options = '/notls/novalidate-cert';
213                }
214
215               
216                $result['status'] = true;
217                $uid = $params['uid'];
218               
219                $return_setacl = imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'lrswipcda');
220               
221                if ($return_setacl)
222                {
223                        $mbox_stream = imap_open('{'.$this->imap_server.':'.$this->imap_port.$imap_options .'}user'. $this->imapDelimiter . $uid, $this->imap_admin, $this->imap_passwd);
224                       
225                        $check = imap_mailboxmsginfo($mbox_stream);
226                        $inbox_size = (string)(round ((($check->Size)/(1024*1024)), 2));
227                       
228                        $return_imap_delete = imap_delete($mbox_stream,'1:*');
229                        imap_close($mbox_stream, CL_EXPUNGE);
230                       
231                        imap_setacl ($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, '');
232                       
233                        if ($return_imap_delete)
234                        {
235                                $result['inbox_size'] = $inbox_size;
236                               
237                                $get_user_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
238                                $result['mailquota_used'] = (string)(round(($get_user_quota['usage']/1024), 2));
239                        }
240                        else
241                        {
242                                $result['status'] = false;
243                                $result['msg'] = $this->functions->lang('It was not possible clean the users inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
244                        }
245                }
246                else
247                {
248                        $result['status'] = false;
249                        $result['msg'] = $this->functions->lang('It was not possible to modify the users acl') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
250                }
251                return $result;
252        }
253}
Note: See TracBrowser for help on using the repository browser.