source: companies/celepar/expressoAdmin1_2/inc/class.imap_functions.inc.php @ 763

Revision 763, 9.5 KB checked in by niltonneto, 15 years ago (diff)

Importação inicial do Expresso da Celepar

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_user($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 users 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       
194        function empty_inbox($params)
195        {
196                // Verifica o acesso do gerente
197                if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
198                {
199                        $result['status'] = false;
200                        $result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
201                        return $result;
202                }
203               
204                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
205                {
206                        $imap_options = '/tls/novalidate-cert';
207                }
208                else
209                {
210                        $imap_options = '/notls/novalidate-cert';
211                }
212
213               
214                $result['status'] = true;
215                $uid = $params['uid'];
216               
217                $return_setacl = imap_setacl($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, 'lrswipcda');
218               
219                if ($return_setacl)
220                {
221                        $mbox_stream = imap_open('{'.$this->imap_server.':'.$this->imap_port.$imap_options .'}user'. $this->imapDelimiter . $uid, $this->imap_admin, $this->imap_passwd);
222                       
223                        $check = imap_mailboxmsginfo($mbox_stream);
224                        $inbox_size = (string)(round ((($check->Size)/(1024*1024)), 2));
225                       
226                        $return_imap_delete = imap_delete($mbox_stream,'1:*');
227                        imap_close($mbox_stream, CL_EXPUNGE);
228                       
229                        imap_setacl ($this->imap, "user" . $this->imapDelimiter . $uid, $this->imap_admin, '');
230                       
231                        if ($return_imap_delete)
232                        {
233                                $result['inbox_size'] = $inbox_size;
234                               
235                                $get_user_quota = @imap_get_quotaroot($this->imap,"user" . $this->imapDelimiter . $uid);
236                                $result['mailquota_used'] = (string)(round(($get_user_quota['usage']/1024), 2));
237                        }
238                        else
239                        {
240                                $result['status'] = false;
241                                $result['msg'] = $this->functions->lang('It was not possible clean the users inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
242                        }
243                }
244                else
245                {
246                        $result['status'] = false;
247                        $result['msg'] = $this->functions->lang('It was not possible to modify the users acl') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
248                }
249                return $result;
250        }
251}
Note: See TracBrowser for help on using the repository browser.