1 | <?php |
---|
2 | |
---|
3 | include_once("class.functions.inc.php"); |
---|
4 | include_once("class.ldap_functions.inc.php"); |
---|
5 | include_once("class.exporteml.inc.php"); |
---|
6 | |
---|
7 | class imap_functions |
---|
8 | { |
---|
9 | var $public_functions = array |
---|
10 | ( |
---|
11 | 'get_range_msgs' => True, |
---|
12 | 'get_info_msg' => True, |
---|
13 | 'get_info_msgs' => True, |
---|
14 | 'get_folders_list' => True, |
---|
15 | 'import_msgs' => True, |
---|
16 | 'msgs_to_archive' => True |
---|
17 | ); |
---|
18 | |
---|
19 | var $ldap; |
---|
20 | var $mbox; |
---|
21 | var $imap_port; |
---|
22 | var $has_cid; |
---|
23 | var $imap_options = ''; |
---|
24 | var $functions; |
---|
25 | var $prefs; |
---|
26 | var $foldersLimit; |
---|
27 | var $imap_sentfolder; |
---|
28 | |
---|
29 | function imap_functions (){ |
---|
30 | $this->foldersLimit = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['imap_max_folders'] ? $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['imap_max_folders'] : 20000; //Limit of folders (mailboxes) user can see |
---|
31 | $this->username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
32 | $this->password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
33 | $this->imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
34 | $this->imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
35 | $this->imap_delimiter = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDelimiter']; |
---|
36 | $this->functions = new functions(); |
---|
37 | $this->imap_sentfolder = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder'] ? $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder'] : str_replace("*","", $this->functions->getLang("Sent")); |
---|
38 | $this->has_cid = false; |
---|
39 | $this->prefs = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']; |
---|
40 | |
---|
41 | |
---|
42 | if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') |
---|
43 | { |
---|
44 | $this->imap_options = '/tls/novalidate-cert'; |
---|
45 | } |
---|
46 | else |
---|
47 | { |
---|
48 | $this->imap_options = '/notls/novalidate-cert'; |
---|
49 | } |
---|
50 | } |
---|
51 | // BEGIN of functions. |
---|
52 | function open_mbox($folder = False,$force_die=true) |
---|
53 | { |
---|
54 | $folder = mb_convert_encoding($folder, "UTF7-IMAP","ISO_8859-1"); |
---|
55 | if (is_resource($this->mbox)) |
---|
56 | { |
---|
57 | if ($force_die) |
---|
58 | { |
---|
59 | @imap_reopen($this->mbox, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error())))); |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | @imap_reopen($this->mbox, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder); |
---|
64 | } |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | if($force_die) |
---|
69 | { |
---|
70 | $this->mbox = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder, $this->username, $this->password) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error())))); |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | $this->mbox = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder, $this->username, $this->password); |
---|
75 | } |
---|
76 | |
---|
77 | } |
---|
78 | return $this->mbox; |
---|
79 | } |
---|
80 | |
---|
81 | function parse_error($error){ |
---|
82 | // This error is returned from Imap. |
---|
83 | if(strstr($error,'Connection refused')) { |
---|
84 | return str_replace("%1", $this->functions->getLang("Mail"), $this->functions->getLang("Connection failed with %1 Server. Try later.")); |
---|
85 | } |
---|
86 | elseif(strstr($error,'virus')) { |
---|
87 | return str_replace("%1", $this->functions->getLang("Mail"), $this->functions->getLang("Your message was rejected by antivirus. Perhaps your attachment has been infected.")); |
---|
88 | } |
---|
89 | // This condition verifies if SESSION is expired. |
---|
90 | elseif(!count($_SESSION)) |
---|
91 | return "nosession"; |
---|
92 | |
---|
93 | return $error; |
---|
94 | } |
---|
95 | |
---|
96 | function get_range_msgs2($params) |
---|
97 | { |
---|
98 | // Free others requests |
---|
99 | session_write_close(); |
---|
100 | $folder = $params['folder']; |
---|
101 | $msg_range_begin = $params['msg_range_begin']; |
---|
102 | $msg_range_end = $params['msg_range_end']; |
---|
103 | $sort_box_type = $params['sort_box_type']; |
---|
104 | $sort_box_reverse = $params['sort_box_reverse']; |
---|
105 | $search_box_type = $params['search_box_type'] != "ALL" && $params['search_box_type'] != "" ? $params['search_box_type'] : false; |
---|
106 | |
---|
107 | $folder = $params['folder']; |
---|
108 | $msg_range_begin = $params['msg_range_begin']; |
---|
109 | $msg_range_end = $params['msg_range_end']; |
---|
110 | $sort_box_type = $params['sort_box_type']; |
---|
111 | $sort_box_reverse = $params['sort_box_reverse']; |
---|
112 | $search_box_type = $params['search_box_type'] != "ALL" && $params['search_box_type'] != "" ? $params['search_box_type'] : false; |
---|
113 | |
---|
114 | if( !$this->mbox || !is_resource( $this->mbox ) ) |
---|
115 | $this->mbox = $this->open_mbox($folder); |
---|
116 | |
---|
117 | $return = array(); |
---|
118 | |
---|
119 | $return['folder'] = $folder; |
---|
120 | |
---|
121 | //Para enviar o offset entre o timezone definido pelo usuário e GMT |
---|
122 | $return['offsetToGMT'] = $this->functions->CalculateDateOffset(); |
---|
123 | |
---|
124 | if(!$search_box_type || $search_box_type=="UNSEEN" || $search_box_type=="SEEN") { |
---|
125 | $msgs_info = imap_status($this->mbox,"{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".mb_convert_encoding( $folder, "UTF7-IMAP", "ISO_8859-1" ) ,SA_ALL); |
---|
126 | |
---|
127 | |
---|
128 | $return['tot_unseen'] = $search_box_type == "SEEN" ? 0 : $msgs_info->unseen; |
---|
129 | |
---|
130 | $sort_array_msg = $this-> get_msgs($folder, $sort_box_type, $search_box_type, $sort_box_reverse,$msg_range_begin,$msg_range_end); |
---|
131 | |
---|
132 | $num_msgs = ($search_box_type=="UNSEEN") ? $msgs_info->unseen : (($search_box_type=="SEEN") ? ($msgs_info->messages - $msgs_info->unseen) : $msgs_info->messages); |
---|
133 | |
---|
134 | $i = 0; |
---|
135 | if(is_array($sort_array_msg)){ |
---|
136 | foreach($sort_array_msg as $msg_number => $value) |
---|
137 | { |
---|
138 | $temp = $this->get_info_head_msg($msg_number); |
---|
139 | //$temp['msg_sample'] = $this->get_msg_sample($msg_number,$folder); |
---|
140 | if(!$temp) |
---|
141 | return false; |
---|
142 | |
---|
143 | $return[$i] = $temp; |
---|
144 | $i++; |
---|
145 | } |
---|
146 | } |
---|
147 | $return['num_msgs'] = $num_msgs; |
---|
148 | } |
---|
149 | else { |
---|
150 | $num_msgs = imap_num_msg($this->mbox); |
---|
151 | $sort_array_msg = $this-> get_msgs($folder, $sort_box_type, $search_box_type, $sort_box_reverse,$msg_range_begin,$num_msgs); |
---|
152 | |
---|
153 | |
---|
154 | $return['tot_unseen'] = 0; |
---|
155 | $i = 0; |
---|
156 | |
---|
157 | if(is_array($sort_array_msg)){ |
---|
158 | |
---|
159 | foreach($sort_array_msg as $msg_number => $value) |
---|
160 | { |
---|
161 | $temp = $this->get_info_head_msg($msg_number); |
---|
162 | if(!$temp) |
---|
163 | return false; |
---|
164 | |
---|
165 | if($temp['Unseen'] == 'U' || $temp['Recent'] == 'N'){ |
---|
166 | $return['tot_unseen']++; |
---|
167 | } |
---|
168 | |
---|
169 | if($i <= ($msg_range_end-$msg_range_begin)) |
---|
170 | $return[$i] = $temp; |
---|
171 | $i++; |
---|
172 | } |
---|
173 | } |
---|
174 | $return['num_msgs'] = count($sort_array_msg)+($msg_range_begin-1); |
---|
175 | } |
---|
176 | return $return; |
---|
177 | } |
---|
178 | |
---|
179 | function get_info_head_msg($msg_number) |
---|
180 | { |
---|
181 | $head_array = array(); |
---|
182 | include_once("class.imap_attachment.inc.php"); |
---|
183 | |
---|
184 | $imap_attachment = new imap_attachment(); |
---|
185 | //if ($this->prefs['use_important_flag'] ) |
---|
186 | //{ |
---|
187 | /*Como eu preciso do atributo Importance para saber se o email é |
---|
188 | * importante ou não, uso abaixo a função imap_fetchheader e busco |
---|
189 | * o atributo importance nela. Isso faz com que eu acesse o cabeçalho |
---|
190 | * duas vezes e de duas formas diferentes, mas em contrapartida, eu |
---|
191 | * não preciso reimplementar o método utilizando o fetchheader. |
---|
192 | * Como as mensagens são renderizadas em um número pequeno por vez, |
---|
193 | * não parece ter perda considerável de performance. |
---|
194 | */ |
---|
195 | |
---|
196 | $tempHeader = imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number)); |
---|
197 | $flag = preg_match('/importance *: *(.*)\r/i', $tempHeader, $importance); |
---|
198 | //} |
---|
199 | // Reimplementado código para identificação dos e-mails assinados e cifrados |
---|
200 | // no método getMessageType(). Mário César Kolling <mario.kolling@serpro.gov.br> |
---|
201 | $head_array['ContentType'] = $this->getMessageType($msg_number, $tempHeader); |
---|
202 | $head_array['Importance'] = $flag==0?"Normal":$importance[1]; |
---|
203 | |
---|
204 | $header = $this->get_header($msg_number); |
---|
205 | if (!is_object($header)) |
---|
206 | return false; |
---|
207 | $head_array['Recent'] = $header->Recent; |
---|
208 | $head_array['Unseen'] = $header->Unseen; |
---|
209 | if($header->Answered =='A' && $header->Draft == 'X'){ |
---|
210 | $head_array['Forwarded'] = 'F'; |
---|
211 | } |
---|
212 | else { |
---|
213 | $head_array['Answered'] = $header->Answered; |
---|
214 | $head_array['Draft'] = $header->Draft; |
---|
215 | } |
---|
216 | $head_array['Deleted'] = $header->Deleted; |
---|
217 | $head_array['Flagged'] = $header->Flagged; |
---|
218 | $head_array['msg_number'] = $msg_number; |
---|
219 | $head_array['udate'] = $header->udate; |
---|
220 | $head_array['offsetToGMT'] = $this->functions->CalculateDateOffset(); |
---|
221 | |
---|
222 | $msgTimestamp = $header->udate + $head_array['offsetToGMT']; |
---|
223 | $head_array['timestamp'] = $msgTimestamp; |
---|
224 | |
---|
225 | $date_msg = gmdate("d/m/Y",$msgTimestamp); |
---|
226 | // if (date("d/m/Y") == $date_msg) |
---|
227 | // $return['udate'] = $header->udate; |
---|
228 | // else |
---|
229 | |
---|
230 | if (date("d/m/Y") == $date_msg) //no dia |
---|
231 | { |
---|
232 | $head_array['smalldate'] = gmdate("H:i",$msgTimestamp); |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | $head_array['smalldate'] = gmdate("d/m/Y",$msgTimestamp); |
---|
237 | } |
---|
238 | |
---|
239 | $from = $header->from; |
---|
240 | $head_array['from'] = array(); |
---|
241 | $head_array['from']['name'] = ( isset( $from[0]->personal ) ) ? $this->decode_string($from[0]->personal) : NULL; |
---|
242 | $head_array['from']['email'] = $this->decode_string($from[0]->mailbox) . "@" . $from[0]->host; |
---|
243 | if(!$head_array['from']['name']) |
---|
244 | $head_array['from']['name'] = $head_array['from']['email']; |
---|
245 | $to = $header->to; |
---|
246 | $head_array['to'] = array(); |
---|
247 | if($to[1] && $to[1]->host == ".SYNTAX-ERROR.") { //E-mails que não possuem o campo "para", vêm com o recipiente preenchido, porém com um recipiente a mais alegando erro de sintaxe. |
---|
248 | $head_array['to']['name'] = $head_array['to']['email'] = NULL; |
---|
249 | } |
---|
250 | else { |
---|
251 | $tmp = ( isset( $to[0]->personal ) ) ? imap_mime_header_decode($to[0]->personal) : NULL; |
---|
252 | $head_array['to']['name'] = ( isset( $tmp[0]->text ) ) ? $this->decode_string($this->decode_string($tmp[0]->text)) : NULL; |
---|
253 | $head_array['to']['email'] = ( isset( $to[0]->mailbox ) ) ? ( $this->decode_string($to[0]->mailbox) . "@" . ( ( isset( $to[0]->host ) ) ? $to[0]->host : '' ) ) : NULL; |
---|
254 | if(!$head_array['to']['name']) |
---|
255 | $head_array['to']['name'] = $head_array['to']['email']; |
---|
256 | } |
---|
257 | $cc = $header->cc; |
---|
258 | $cco = $header->bcc; |
---|
259 | if ( ($cc) && (!$head_array['to']['name']) ){ |
---|
260 | $head_array['to']['name'] = ( isset( $cc[0]->personal ) ) ? $this->decode_string($cc[0]->personal) : NULL; |
---|
261 | $head_array['to']['email'] = $this->decode_string($cc[0]->mailbox) . "@" . $cc[0]->host; |
---|
262 | if(!$head_array['to']['name']) |
---|
263 | $head_array['to']['name'] = $head_array['from']['email']; |
---|
264 | } |
---|
265 | else if ( ($cco) && (!$head_array['to']['name']) ){ |
---|
266 | $head_array['to']['name'] = ( isset( $cco[0]->personal ) ) ? $this->decode_string($cco[0]->personal) : NULL; |
---|
267 | $head_array['to']['email'] = $this->decode_string($cco[0]->mailbox) . "@" . $cco[0]->host; |
---|
268 | if(!$head_array['to']['name']) |
---|
269 | $head_array['to']['name'] = $head_array['from']['email']; |
---|
270 | } |
---|
271 | $head_array['subject'] = ( isset( $header->fetchsubject ) ) ? $this->decode_string($header->fetchsubject) : ''; |
---|
272 | |
---|
273 | $head_array['Size'] = $header->Size; |
---|
274 | |
---|
275 | $head_array['attachment'] = array(); |
---|
276 | $head_array['attachment'] = $imap_attachment->get_attachment_headerinfo($this->mbox, $msg_number); |
---|
277 | |
---|
278 | return $head_array; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * |
---|
283 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
284 | * @param string $string String a ser decodificada |
---|
285 | * @return string |
---|
286 | * @todo Verificar a possibilidade de se utilizar a função iconv_mime_decode, que é capaz de identificar a codificação por si só, mas que pode ser interpretada de forma diversa dependendo da implementação do sistema |
---|
287 | * @todo Executar testes suficientes para validar a funçao iconv_mime_decode em substituição à este método decode_string |
---|
288 | */ |
---|
289 | function decode_string($string) |
---|
290 | { |
---|
291 | if ((strpos(strtolower($string), '=?iso-8859-1') !== false) || (strpos(strtolower($string), '=?windows-1252') !== false)) |
---|
292 | { |
---|
293 | $retun = ''; |
---|
294 | $tmp = imap_mime_header_decode($string); |
---|
295 | foreach ($tmp as $tmp1) |
---|
296 | { |
---|
297 | $return .= $this->htmlspecialchars_encode($tmp1->text); |
---|
298 | } |
---|
299 | |
---|
300 | return str_replace("\t", "", $return); |
---|
301 | } |
---|
302 | else if (strpos(strtolower($string), '=?utf-8') !== false) |
---|
303 | { |
---|
304 | $elements = imap_mime_header_decode($string); |
---|
305 | |
---|
306 | for($i = 0;$i < count($elements);$i++) |
---|
307 | { |
---|
308 | $charset = strtolower($elements[$i]->charset); |
---|
309 | $text = $elements[$i]->text; |
---|
310 | if(!strcasecmp($charset, "utf-8") || !strcasecmp($charset, "utf-7")) |
---|
311 | $decoded .= $this->functions->utf8_to_ncr($text); |
---|
312 | else |
---|
313 | { |
---|
314 | if( strcasecmp($charset,"default") ) |
---|
315 | $decoded .= $this->htmlspecialchars_encode(iconv($charset, "iso-8859-1", $text)); |
---|
316 | else |
---|
317 | $decoded .= $this->htmlspecialchars_encode($text); |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | return str_replace("\t", "", $decoded); |
---|
322 | } |
---|
323 | else if(strpos(strtolower($string), '=?us-ascii') !== false) |
---|
324 | { |
---|
325 | $retun = ''; |
---|
326 | $tmp = imap_mime_header_decode($string); |
---|
327 | foreach ($tmp as $tmp1) |
---|
328 | $return .= $this->htmlspecialchars_encode(quoted_printable_decode($tmp1->text)); |
---|
329 | |
---|
330 | return str_replace("\t", "", $return); |
---|
331 | |
---|
332 | } |
---|
333 | else if (eregi('=?', $string)) |
---|
334 | return iconv_mime_decode($string); |
---|
335 | |
---|
336 | |
---|
337 | return $this->htmlspecialchars_encode($string); |
---|
338 | } |
---|
339 | /** |
---|
340 | * Função que importa arquivos .eml exportados pelo expresso para a caixa do usuário. Testado apenas |
---|
341 | * com .emls gerados pelo expresso, e o arquivo pode ser um zip contendo vários emls ou um .eml. |
---|
342 | */ |
---|
343 | function import_msgs($params) { |
---|
344 | if(!$this->mbox) |
---|
345 | $this->mbox = $this->open_mbox(); |
---|
346 | |
---|
347 | if( preg_match('/local_/',$params["folder"]) ) |
---|
348 | { |
---|
349 | // PLEASE, BE CAREFULL!!! YOU SHOULD USE EMAIL CONFIGURATION VALUES (EMAILADMIN MODULE) |
---|
350 | $tmp_box = mb_convert_encoding('INBOX'.$this->imap_delimiter.$_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultTrashFolder'].$this->imap_delimiter.'tmpMoveToLocal', "UTF7-IMAP", "UTF-8"); |
---|
351 | if ( ! imap_createmailbox( $this -> mbox,"{".$this -> imap_server."}$tmp_box" ) ) |
---|
352 | return $this->functions->getLang( 'Import to Local : fail...' ); |
---|
353 | imap_reopen($this->mbox, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$tmp_box); |
---|
354 | $params["folder"] = $tmp_box; |
---|
355 | } |
---|
356 | $errors = array(); |
---|
357 | $invalid_format = false; |
---|
358 | $filename = $params['FILES'][0]['name']; |
---|
359 | $params["folder"] = mb_convert_encoding($params["folder"], "UTF7-IMAP","ISO_8859-1"); |
---|
360 | $quota = imap_get_quotaroot($this->mbox, $params["folder"]); |
---|
361 | if((($quota['limit'] - $quota['usage'])*1024) <= $params['FILES'][0]['size']){ |
---|
362 | return array( 'error' => $this->functions->getLang("fail in import:"). |
---|
363 | " ".$this->functions->getLang("Over quota")); |
---|
364 | } |
---|
365 | if(substr($filename,strlen($filename)-4)==".zip") { |
---|
366 | $zip = zip_open($params['FILES'][0]['tmp_name']); |
---|
367 | |
---|
368 | if ($zip) { |
---|
369 | while ($zip_entry = zip_read($zip)) { |
---|
370 | |
---|
371 | if (zip_entry_open($zip, $zip_entry, "r")) { |
---|
372 | $email = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); |
---|
373 | $status = @imap_append($this->mbox, |
---|
374 | "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$params["folder"], |
---|
375 | $email |
---|
376 | ); |
---|
377 | if(!$status) |
---|
378 | array_push($errors,zip_entry_name($zip_entry)); |
---|
379 | zip_entry_close($zip_entry); |
---|
380 | } |
---|
381 | } |
---|
382 | zip_close($zip); |
---|
383 | } |
---|
384 | |
---|
385 | if ( isset( $tmp_box ) && ! sizeof( $errors ) ) |
---|
386 | { |
---|
387 | |
---|
388 | $mc = imap_check($this->mbox); |
---|
389 | |
---|
390 | $result = imap_fetch_overview( $this -> mbox, "1:{$mc -> Nmsgs}", 0 ); |
---|
391 | |
---|
392 | $ids = array( ); |
---|
393 | foreach ($result as $overview) |
---|
394 | $ids[ ] = $overview -> uid; |
---|
395 | |
---|
396 | return implode( ',', $ids ); |
---|
397 | } |
---|
398 | } |
---|
399 | else if(substr($filename,strlen($filename)-4)==".eml") { |
---|
400 | $email = implode("",file($params['FILES'][0]['tmp_name'])); |
---|
401 | $status = @imap_append($this->mbox, |
---|
402 | "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$params["folder"], |
---|
403 | $email |
---|
404 | ); |
---|
405 | if(!$status){ |
---|
406 | array_push($errors,zip_entry_name($zip_entry)); |
---|
407 | zip_entry_close($zip_entry); |
---|
408 | } |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | if ( isset( $tmp_box ) ) |
---|
413 | imap_deletemailbox( $this->mbox,"{".$this -> imap_server."}$tmp_box" ); |
---|
414 | |
---|
415 | return array("error" => $this->functions->getLang("wrong file format")); |
---|
416 | $invalid_format = true; |
---|
417 | } |
---|
418 | |
---|
419 | if(!$invalid_format) { |
---|
420 | if(count($errors)>0) { |
---|
421 | $message = $this->functions->getLang("fail in import:")."\n"; |
---|
422 | foreach($errors as $arquivo) { |
---|
423 | $message.=$arquivo."\n"; |
---|
424 | } |
---|
425 | return array("error" => $message); |
---|
426 | } |
---|
427 | else |
---|
428 | return $this->functions->getLang("The import was executed successfully."); |
---|
429 | } |
---|
430 | } |
---|
431 | /* |
---|
432 | Remove os anexos de uma mensagem. A estratégia para isso é criar uma mensagem nova sem os anexos, mantendo apenas |
---|
433 | a primeira parte do e-mail, que é o texto, sem anexos. |
---|
434 | O método considera que o email é multpart. |
---|
435 | */ |
---|
436 | function remove_attachments($params) { |
---|
437 | include_once("class.message_components.inc.php"); |
---|
438 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
439 | $this->mbox = $this->open_mbox($params["folder"]); |
---|
440 | $return["status"] = true; |
---|
441 | $header = ""; |
---|
442 | |
---|
443 | $headertemp = explode("\n",imap_fetchheader($this->mbox, imap_msgno($this->mbox, $params["msg_num"]))); |
---|
444 | foreach($headertemp as $head) {//Se eu colocar todo o header do email dá pau no append, então procuro apenas o que interessa. |
---|
445 | $head1 = explode(":",$head); |
---|
446 | if ( (strtoupper($head1[0]) == "TO") || |
---|
447 | (strtoupper($head1[0]) == "FROM") || |
---|
448 | (strtoupper($head1[0]) == "SUBJECT") || |
---|
449 | (strtoupper($head1[0]) == "DATE") ) |
---|
450 | $header .= $head."\r\n"; |
---|
451 | } |
---|
452 | |
---|
453 | $msg = &new message_components($this->mbox); |
---|
454 | $msg->fetch_structure($params["msg_num"]);/* O fetchbody tava trazendo o email com problemas na acentuação. |
---|
455 | Então uso essa classe para verificar a codificação e o charset, |
---|
456 | para que o método decodeBody do expresso possa trazer tudo certinho*/ |
---|
457 | |
---|
458 | $all_body_type = strtolower($msg->file_type[$params["msg_num"]][0]); |
---|
459 | $all_body_encoding = $msg->encoding[$params["msg_num"]][0]; |
---|
460 | $all_body_charset = $msg->charset[$params["msg_num"]][0]; |
---|
461 | |
---|
462 | if($all_body_type=='multipart/alternative') { |
---|
463 | if(strtolower($msg->file_type[$params["msg_num"]][2]=='text/html') && |
---|
464 | $msg->pid[$params["msg_num"]][2] == '1.2') { |
---|
465 | $body_part_to_show = '1.2'; |
---|
466 | $all_body_type = strtolower($msg->file_type[$params["msg_num"]][2]); |
---|
467 | $all_body_encoding = $msg->encoding[$params["msg_num"]][2]; |
---|
468 | $all_body_charset = $msg->charset[$params["msg_num"]][2]; |
---|
469 | } |
---|
470 | else { |
---|
471 | $body_part_to_show = '1.1'; |
---|
472 | $all_body_type = strtolower($msg->file_type[$params["msg_num"]][1]); |
---|
473 | $all_body_encoding = $msg->encoding[$params["msg_num"]][1]; |
---|
474 | $all_body_charset = $msg->charset[$params["msg_num"]][1]; |
---|
475 | } |
---|
476 | } |
---|
477 | else |
---|
478 | $body_part_to_show = '1'; |
---|
479 | |
---|
480 | $status = imap_append($this->mbox, |
---|
481 | "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$params["folder"], |
---|
482 | $header. |
---|
483 | "Content-Type: ".$all_body_type."; charset = \"".$all_body_charset."\"". |
---|
484 | "\r\n". |
---|
485 | "Content-Transfer-Encoding: ".$all_body_encoding. |
---|
486 | "\r\n". |
---|
487 | "\r\n". |
---|
488 | str_replace("\n","\r\n",$this->decodeBody( |
---|
489 | imap_fetchbody($this->mbox,imap_msgno($this->mbox, $params["msg_num"]),$body_part_to_show), |
---|
490 | $all_body_encoding, $all_body_charset |
---|
491 | ) |
---|
492 | ), "\\Seen"); //Append do novo email, só com header e conteúdo sem anexos. |
---|
493 | |
---|
494 | if(!$status) |
---|
495 | { |
---|
496 | $return["status"] = false; |
---|
497 | $return["msg"] = lang("error appending mail on delete attachments"); |
---|
498 | } |
---|
499 | else |
---|
500 | { |
---|
501 | $status = imap_status($this->mbox, "{".$this->imap_server.":".$this->imap_port."}".$params['folder'], SA_UIDNEXT); |
---|
502 | $return['msg_no'] = $status->uidnext - 1; |
---|
503 | imap_delete($this->mbox, imap_msgno($this->mbox, $params["msg_num"])); |
---|
504 | imap_expunge($this->mbox); |
---|
505 | } |
---|
506 | |
---|
507 | return $return; |
---|
508 | |
---|
509 | } |
---|
510 | |
---|
511 | function msgs_to_archive($params) { |
---|
512 | |
---|
513 | $folder = $params['folder']; |
---|
514 | $all_ids = $this-> get_msgs($folder, 'SORTARRIVAL', false, 0,-1,-1); |
---|
515 | |
---|
516 | $messages_not_to_copy = explode(",",$params['mails']); |
---|
517 | $ids = array(); |
---|
518 | |
---|
519 | $cont = 0; |
---|
520 | |
---|
521 | foreach($all_ids as $each_id=>$value) { |
---|
522 | if(!in_array($each_id,$messages_not_to_copy)) { |
---|
523 | array_push($ids,$each_id); |
---|
524 | $cont++; |
---|
525 | } |
---|
526 | if($cont>=100) |
---|
527 | break; |
---|
528 | } |
---|
529 | |
---|
530 | if (empty($ids)) |
---|
531 | return array(); |
---|
532 | |
---|
533 | $params = array("folder"=>$folder,"msgs_number"=>implode(",",$ids)); |
---|
534 | |
---|
535 | |
---|
536 | return $this->get_info_msgs($params); |
---|
537 | |
---|
538 | |
---|
539 | } |
---|
540 | |
---|
541 | /** |
---|
542 | * |
---|
543 | * @return |
---|
544 | * @param $params Object |
---|
545 | */ |
---|
546 | function get_info_msgs($params) { |
---|
547 | include_once("class.exporteml.inc.php"); |
---|
548 | $return = array(); |
---|
549 | $new_params = array(); |
---|
550 | $attach_params = array(); |
---|
551 | $new_params["msg_folder"]=$params["folder"]; |
---|
552 | $attach_params["folder"] = $params["folder"]; |
---|
553 | $msgs = explode(",",$params["msgs_number"]); |
---|
554 | $exporteml = new ExportEml(); |
---|
555 | $unseen_msgs = array(); |
---|
556 | foreach($msgs as $msg_number) { |
---|
557 | $new_params["msg_number"] = $msg_number; |
---|
558 | //ini_set("display_errors","1"); |
---|
559 | $msg_info = $this->get_info_msg($new_params); |
---|
560 | |
---|
561 | $this->mbox = $this->open_mbox($params['folder']); //Não sei porque, mas se não abrir de novo a caixa dá erro. |
---|
562 | $msg_info['header'] = $this->get_info_head_msg($msg_number); |
---|
563 | |
---|
564 | $attach_params["num_msg"] = $msg_number; |
---|
565 | $msg_info['array_attach'] = $exporteml->get_attachments_in_array($attach_params); |
---|
566 | $msg_info['url_export_file'] = $exporteml->export_to_archive($msg_number,$params["folder"]); |
---|
567 | imap_close($this->mbox); |
---|
568 | $this->mbox=false; |
---|
569 | array_push($return,serialize($msg_info)); |
---|
570 | |
---|
571 | if($msg_info['Unseen'] == "U" || $msg_info['Recent'] == "N"){ |
---|
572 | array_push($unseen_msgs,$msg_number); |
---|
573 | } |
---|
574 | } |
---|
575 | if($unseen_msgs){ |
---|
576 | $msgs_list = implode(",",$unseen_msgs); |
---|
577 | $array_msgs = array('folder' => $new_params["msg_folder"], "msgs_to_set" => $msgs_list, "flag" => "unseen"); |
---|
578 | $this->set_messages_flag($array_msgs); |
---|
579 | } |
---|
580 | |
---|
581 | return $return; |
---|
582 | } |
---|
583 | |
---|
584 | /** |
---|
585 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
586 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
587 | * @param $msg_number numero da mensagem |
---|
588 | */ |
---|
589 | function getRawHeader($msg_number) |
---|
590 | { |
---|
591 | return imap_fetchheader($this->mbox, $msg_number, FT_UID); |
---|
592 | } |
---|
593 | |
---|
594 | /** |
---|
595 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
596 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
597 | * @param $msg_number numero da mensagem |
---|
598 | */ |
---|
599 | function getRawBody($msg_number) |
---|
600 | { |
---|
601 | return imap_body($this->mbox, $msg_number, FT_UID); |
---|
602 | } |
---|
603 | |
---|
604 | |
---|
605 | /** |
---|
606 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
607 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
608 | * @param $msg mensagem |
---|
609 | */ |
---|
610 | function builderMsgHeader($msg) |
---|
611 | { |
---|
612 | |
---|
613 | |
---|
614 | $fromMail = str_replace('<','', str_replace('>','',$msg->headers['from'])); |
---|
615 | $tosMails = str_replace('<','', str_replace('>','',$msg->headers['to'])); |
---|
616 | |
---|
617 | $tos = explode(',',$tosMails); |
---|
618 | $to = ''; |
---|
619 | foreach ($tos as $value) |
---|
620 | { |
---|
621 | $to .= '<a href="mailto:'.str_replace(' ','',$value).'">'.$value.'</a>, '; |
---|
622 | } |
---|
623 | |
---|
624 | $header = ' |
---|
625 | <table style="margin: 2px; border: 1px solid black; background: none repeat scroll 0% 0% rgb(234, 234, 234);"> |
---|
626 | <tbody> |
---|
627 | <tr><td><b>'.$this->functions->getLang('Subject').':</b></td><td>'.$msg->headers['subject'].'</td></tr> |
---|
628 | <tr><td><b>'.$this->functions->getLang('From').':</b></td><td><a href="mailto:'.$fromMail.'">'.$fromMail.'</a></td></tr> |
---|
629 | <tr><td><b>'.$this->functions->getLang('Date').':</b></td><td>'.$msg->headers['date'].'</td></tr> |
---|
630 | <tr><td><b>'.$this->functions->getLang('To').':</b></td><td>'.$to.'</td></tr> |
---|
631 | </tbody> |
---|
632 | </table> |
---|
633 | <br />' |
---|
634 | ; |
---|
635 | |
---|
636 | return $header; |
---|
637 | } |
---|
638 | |
---|
639 | /** |
---|
640 | * Constroe o corpo da msg direto na variavel de conteudo |
---|
641 | * @param Mail_mimeDecode $structure |
---|
642 | * @param <type> $content Ponteiro para Variavel de conteudo da msg |
---|
643 | */ |
---|
644 | function builderMsgBody($structure , &$content , $printHeader = false) |
---|
645 | { |
---|
646 | if(strtolower($structure->ctype_primary) == 'multipart' && strtolower($structure->ctype_secondary) == 'alternative') |
---|
647 | { |
---|
648 | $numParts = count($structure->parts) - 1; |
---|
649 | |
---|
650 | for($i = $numParts; $i >= 0; $i--) |
---|
651 | { |
---|
652 | $part = $structure->parts[$i]; |
---|
653 | |
---|
654 | switch (strtolower($part->ctype_primary)) |
---|
655 | { |
---|
656 | case 'text': |
---|
657 | $disposition = strtolower($part->disposition); |
---|
658 | if($disposition != 'attachment') |
---|
659 | { |
---|
660 | if(strtolower($part->ctype_secondary) == 'html') |
---|
661 | { |
---|
662 | if($printHeader) |
---|
663 | $content .= $this->builderMsgHeader($part); |
---|
664 | |
---|
665 | $content .= $this->decodeMailPart($part->body,$part->ctype_parameters['charset']); |
---|
666 | } |
---|
667 | |
---|
668 | if(strtolower($part->ctype_secondary) == 'plain' ) |
---|
669 | { |
---|
670 | if($printHeader) |
---|
671 | $content .= $this->builderMsgHeader($part); |
---|
672 | |
---|
673 | $content .= '<pre>'. htmlentities($this->decodeMailPart($part->body,$part->ctype_parameters['charset'],false)).'</pre>'; |
---|
674 | } |
---|
675 | |
---|
676 | |
---|
677 | |
---|
678 | } |
---|
679 | |
---|
680 | $i = -1; |
---|
681 | break; |
---|
682 | |
---|
683 | case 'multipart': |
---|
684 | |
---|
685 | if($printHeader) |
---|
686 | $content .= $this->builderMsgHeader($part); |
---|
687 | |
---|
688 | $this->builderMsgBody($part,$content); |
---|
689 | |
---|
690 | $i = -1; |
---|
691 | break; |
---|
692 | |
---|
693 | case 'message': |
---|
694 | |
---|
695 | if(!is_array($part->parts)) |
---|
696 | { |
---|
697 | $content .= "<hr align='left' width='95%' style='border:1px solid #DCDCDC'>"; |
---|
698 | $content .= '<pre>'. htmlentities($this->decodeMailPart($part->body, $structure->ctype_parameters['charset'],false)).'</pre>'; |
---|
699 | $content .= "<hr align='left' width='95%' style='border:1px solid #DCDCDC'>"; |
---|
700 | } |
---|
701 | else |
---|
702 | $this->builderMsgBody($part,$content,true); |
---|
703 | |
---|
704 | $i = -1; |
---|
705 | break; |
---|
706 | } |
---|
707 | } |
---|
708 | } |
---|
709 | else |
---|
710 | { |
---|
711 | foreach ($structure->parts as $index => $part) |
---|
712 | { |
---|
713 | switch (strtolower($part->ctype_primary)) |
---|
714 | { |
---|
715 | case 'text': |
---|
716 | |
---|
717 | $disposition = strtolower($part->disposition); |
---|
718 | if($disposition != 'attachment') |
---|
719 | { |
---|
720 | if(strtolower($part->ctype_secondary) == 'html') |
---|
721 | { |
---|
722 | if($printHeader) |
---|
723 | $content .= $this->builderMsgHeader($part); |
---|
724 | |
---|
725 | $content .= $this->decodeMailPart($part->body,$part->ctype_parameters['charset']); |
---|
726 | } |
---|
727 | |
---|
728 | if(strtolower($part->ctype_secondary) == 'plain') |
---|
729 | { |
---|
730 | if($printHeader) |
---|
731 | $content .= $this->builderMsgHeader($part); |
---|
732 | |
---|
733 | $content .= '<pre>'. htmlentities($this->decodeMailPart($part->body,$part->ctype_parameters['charset'],false)).'</pre>'; |
---|
734 | } |
---|
735 | |
---|
736 | |
---|
737 | } |
---|
738 | break; |
---|
739 | case 'multipart': |
---|
740 | |
---|
741 | if($printHeader) |
---|
742 | $content .= $this->builderMsgHeader($part); |
---|
743 | |
---|
744 | $this->builderMsgBody($part,$content); |
---|
745 | |
---|
746 | break; |
---|
747 | case 'message': |
---|
748 | |
---|
749 | if(!is_array($part->parts)) |
---|
750 | { |
---|
751 | $content .= "<hr align='left' width='95%' style='border:1px solid #DCDCDC'>"; |
---|
752 | $content .= '<pre>'. htmlentities($this->decodeMailPart($part->body, $structure->ctype_parameters['charset'],false)).'</pre>'; |
---|
753 | $content .= "<hr align='left' width='95%' style='border:1px solid #DCDCDC'>"; |
---|
754 | } |
---|
755 | else |
---|
756 | $this->builderMsgBody($part,$content,true); |
---|
757 | break; |
---|
758 | } |
---|
759 | } |
---|
760 | } |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | |
---|
765 | /** |
---|
766 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
767 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
768 | * @param $msg_number numero da mensagem |
---|
769 | */ |
---|
770 | function get_msg_sample($msg_number) |
---|
771 | { |
---|
772 | |
---|
773 | $return = ""; |
---|
774 | if( (!isset($this->prefs['preview_msg_subject']) || ($this->prefs['preview_msg_subject'] != "1")) && |
---|
775 | (!isset($this->prefs['preview_msg_tip'] ) || ($this->prefs['preview_msg_tip'] != "1")) ) |
---|
776 | { |
---|
777 | $return['body'] = ""; |
---|
778 | return $return; |
---|
779 | } |
---|
780 | |
---|
781 | include_once("class.message_components.inc.php"); |
---|
782 | $msg = &new message_components($this->mbox); |
---|
783 | $msg->fetch_structure($msg_number); |
---|
784 | |
---|
785 | if(!$msg->structure[$msg_number]->parts) |
---|
786 | { |
---|
787 | $content = ''; |
---|
788 | if (strtolower($msg->structure[$msg_number]->subtype) == "plain" || strtolower($msg->structure[$msg_number]->subtype) == "html") |
---|
789 | { |
---|
790 | $content = $this->decodeBody(imap_body($this->mbox, $msg_number, FT_UID|FT_PEEK), $msg->encoding[$msg_number][0], $msg->charset[$msg_number][0]); |
---|
791 | } |
---|
792 | } |
---|
793 | else |
---|
794 | { |
---|
795 | foreach($msg->pid[$msg_number] as $values => $msg_part) |
---|
796 | { |
---|
797 | |
---|
798 | $file_type = strtolower($msg->file_type[$msg_number][$values]); |
---|
799 | if($file_type == "text/plain" || $file_type == "text/html") { |
---|
800 | $content = $this->decodeBody(imap_fetchbody($this->mbox, $msg_number, $msg_part, FT_UID|FT_PEEK), $msg->encoding[$msg_number][$values], $msg->charset[$msg_number][$values]); |
---|
801 | break; |
---|
802 | } |
---|
803 | } |
---|
804 | } |
---|
805 | $content = $this->replace_special_characters($content); |
---|
806 | $tags_replace = array("<br>","<br/>","<br />"); |
---|
807 | $content = str_replace($tags_replace," ", $content); |
---|
808 | $content = strip_tags($content); |
---|
809 | $content = str_replace(array("{","}"," "), " ", $content); |
---|
810 | $content = trim($content); |
---|
811 | $content = html_entity_decode(substr($content,0,300)); |
---|
812 | $content != "" ? $return['body'] = " - " . $content: $return['body'] = ""; |
---|
813 | return $return; |
---|
814 | } |
---|
815 | |
---|
816 | |
---|
817 | function get_info_msg($params) |
---|
818 | { |
---|
819 | $return = array(); |
---|
820 | $msg_number = $params['msg_number']; |
---|
821 | $msg_folder = urldecode($params['msg_folder']); |
---|
822 | |
---|
823 | if(preg_match('(.+)(_[a-zA-Z0-9]+)',$msg_number,$matches)) { //Verifies if it comes from a tab diferent of the main one. |
---|
824 | $msg_number = $matches[1]; |
---|
825 | $plus_id = $matches[2]; |
---|
826 | } |
---|
827 | else { |
---|
828 | $plus_id = ''; |
---|
829 | } |
---|
830 | |
---|
831 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
832 | $this->mbox = $this->open_mbox($msg_folder); |
---|
833 | |
---|
834 | $header = $this->get_header($msg_number); |
---|
835 | if (!$header) { |
---|
836 | $return['status_get_msg_info'] = "false"; |
---|
837 | return $return; |
---|
838 | } |
---|
839 | |
---|
840 | $header_ = imap_fetchheader($this->mbox, $msg_number, FT_UID); |
---|
841 | $return_get_body = $this->get_body_msg($msg_number, $msg_folder); |
---|
842 | $body = $return_get_body['body']; |
---|
843 | |
---|
844 | if($return_get_body['body']=='isCripted'){ |
---|
845 | $exporteml = new ExportEml(); |
---|
846 | $return['source']=$exporteml->export_msg_data($msg_number,$msg_folder); |
---|
847 | $return['body'] = ""; |
---|
848 | $return['attachments'] = ""; |
---|
849 | $return['thumbs'] = ""; |
---|
850 | $return['signature'] = ""; |
---|
851 | //return $return; |
---|
852 | }else{ |
---|
853 | $return['body'] = $body; |
---|
854 | $return['attachments'] = $return_get_body['attachments']; |
---|
855 | $return['thumbs'] = $return_get_body['thumbs']; |
---|
856 | $return['signature'] = $return_get_body['signature']; |
---|
857 | } |
---|
858 | $pattern = '/^[ \t]*Disposition-Notification-To:[ ]*<?[[:alnum:]\._-]+@[[:alnum:]_-]+[\.[:alnum:]]+>?/sm'; |
---|
859 | if (preg_match($pattern, $header_, $fields)) |
---|
860 | { |
---|
861 | if(preg_match('/[[:alnum:]\._\-]+@[[:alnum:]_\-\.]+/',$fields[0], $matches)){ |
---|
862 | $return['DispositionNotificationTo'] = "<".$matches[0].">"; |
---|
863 | } |
---|
864 | } |
---|
865 | |
---|
866 | $return['Recent'] = $header->Recent; |
---|
867 | $return['Unseen'] = $header->Unseen; |
---|
868 | $return['Deleted'] = $header->Deleted; |
---|
869 | $return['Flagged'] = $header->Flagged; |
---|
870 | |
---|
871 | if($header->Answered =='A' && $header->Draft == 'X'){ |
---|
872 | $return['Forwarded'] = 'F'; |
---|
873 | } |
---|
874 | |
---|
875 | else { |
---|
876 | $return['Answered'] = $header->Answered; |
---|
877 | $return['Draft'] = $header->Draft; |
---|
878 | } |
---|
879 | |
---|
880 | $return['msg_number'] = $msg_number.$plus_id; |
---|
881 | $return['msg_folder'] = $msg_folder; |
---|
882 | |
---|
883 | $date_msg = gmdate("d/m/Y",$header->udate); |
---|
884 | if (date("d/m/Y") == $date_msg) |
---|
885 | $return['udate'] = gmdate("H:i",$header->udate); |
---|
886 | else |
---|
887 | $return['udate'] = $date_msg; |
---|
888 | |
---|
889 | $return['msg_day'] = $date_msg; |
---|
890 | $return['msg_hour'] = gmdate("H:i",$header->udate); |
---|
891 | |
---|
892 | if (date("d/m/Y") == $date_msg) //no dia |
---|
893 | { |
---|
894 | $return['fulldate'] = gmdate("d/m/Y H:i",$header->udate); |
---|
895 | $return['smalldate'] = gmdate("H:i",$header->udate); |
---|
896 | |
---|
897 | $timestamp_now = strtotime("now") + $offset; |
---|
898 | $timestamp_msg_time = $header->udate; |
---|
899 | // $timestamp_now is GMT and $timestamp_msg_time is MailDate TZ. |
---|
900 | // The variable $timestamp_diff is calculated without MailDate TZ. |
---|
901 | $pdate = date_parse($header->MailDate); |
---|
902 | $timestamp_diff = $timestamp_now - $timestamp_msg_time + ($pdate['zone']*(-60)); |
---|
903 | |
---|
904 | if (gmdate("H",$timestamp_diff) > 0) |
---|
905 | { |
---|
906 | $return['fulldate'] .= " (" . gmdate("H:i", $timestamp_diff) . ' ' . $this->functions->getLang('hours ago') . ')'; |
---|
907 | } |
---|
908 | else |
---|
909 | { |
---|
910 | if (gmdate("i",$timestamp_diff) == 0){ |
---|
911 | $return['fulldate'] .= ' ('. $this->functions->getLang('now').')'; |
---|
912 | } |
---|
913 | elseif (gmdate("i",$timestamp_diff) == 1){ |
---|
914 | $return['fulldate'] .= ' (1 '. $this->functions->getLang('minute ago').')'; |
---|
915 | } |
---|
916 | else{ |
---|
917 | $return['fulldate'] .= " (" . gmdate("i",$timestamp_diff) .' '. $this->functions->getLang('minutes ago') . ')'; |
---|
918 | } |
---|
919 | } |
---|
920 | } |
---|
921 | else{ |
---|
922 | $return['fulldate'] = gmdate("d/m/Y H:i",$header->udate); |
---|
923 | $return['smalldate'] = gmdate("d/m/Y",$header->udate); |
---|
924 | } |
---|
925 | |
---|
926 | $from = $header->from; |
---|
927 | $return['from'] = array(); |
---|
928 | $return['from']['name'] = $this->decode_string($from[0]->personal); |
---|
929 | $return['from']['email'] = $this->decode_string($from[0]->mailbox . "@" . $from[0]->host); |
---|
930 | if ($return['from']['name']) |
---|
931 | { |
---|
932 | if (substr($return['from']['name'], 0, 1) == '"') |
---|
933 | $return['from']['full'] = $return['from']['name'] . ' ' . '<' . $return['from']['email'] . '>'; |
---|
934 | else |
---|
935 | $return['from']['full'] = '"' . $return['from']['name'] . '" ' . '<' . $return['from']['email'] . '>'; |
---|
936 | } |
---|
937 | else |
---|
938 | $return['from']['full'] = $return['from']['email']; |
---|
939 | |
---|
940 | // Sender attribute |
---|
941 | $sender = $header->sender; |
---|
942 | $return['sender'] = array(); |
---|
943 | $return['sender']['name'] = $this->decode_string($sender[0]->personal); |
---|
944 | $return['sender']['email'] = $this->decode_string($sender[0]->mailbox . "@" . $sender[0]->host); |
---|
945 | if ($return['sender']['name']) |
---|
946 | { |
---|
947 | if (substr($return['sender']['name'], 0, 1) == '"') |
---|
948 | $return['sender']['full'] = $return['sender']['name'] . ' ' . '<' . $return['sender']['email'] . '>'; |
---|
949 | else |
---|
950 | $return['sender']['full'] = '"' . $return['sender']['name'] . '" ' . '<' . $return['sender']['email'] . '>'; |
---|
951 | } |
---|
952 | else |
---|
953 | $return['sender']['full'] = $return['sender']['email']; |
---|
954 | |
---|
955 | if($return['from']['full'] == $return['sender']['full']) |
---|
956 | $return['sender'] = null; |
---|
957 | $to = $header->to; |
---|
958 | $return['toaddress2'] = ""; |
---|
959 | if (!empty($to)) |
---|
960 | { |
---|
961 | foreach ($to as $tmp) |
---|
962 | { |
---|
963 | if (!empty($tmp->personal)) |
---|
964 | { |
---|
965 | $personal_tmp = imap_mime_header_decode($tmp->personal); |
---|
966 | $return['toaddress2'] .= '"' . $personal_tmp[0]->text . '"'; |
---|
967 | $return['toaddress2'] .= " "; |
---|
968 | $return['toaddress2'] .= "<"; |
---|
969 | if ($tmp->host != 'unspecified-domain') |
---|
970 | $return['toaddress2'] .= $tmp->mailbox . "@" . $tmp->host; |
---|
971 | else |
---|
972 | $return['toaddress2'] .= $tmp->mailbox; |
---|
973 | $return['toaddress2'] .= ">"; |
---|
974 | $return['toaddress2'] .= ", "; |
---|
975 | } |
---|
976 | else |
---|
977 | { |
---|
978 | if ($tmp->host != 'unspecified-domain') |
---|
979 | $return['toaddress2'] .= $tmp->mailbox . "@" . $tmp->host; |
---|
980 | else |
---|
981 | $return['toaddress2'] .= $tmp->mailbox; |
---|
982 | $return['toaddress2'] .= ", "; |
---|
983 | } |
---|
984 | } |
---|
985 | $return['toaddress2'] = $this->del_last_two_caracters($return['toaddress2']); |
---|
986 | } |
---|
987 | else |
---|
988 | { |
---|
989 | $return['toaddress2'] = ""; |
---|
990 | } |
---|
991 | |
---|
992 | $cc = $header->cc; |
---|
993 | $return['cc'] = ""; |
---|
994 | if (!empty($cc)) |
---|
995 | { |
---|
996 | foreach ($cc as $tmp_cc) |
---|
997 | { |
---|
998 | if (!empty($tmp_cc->personal)) |
---|
999 | { |
---|
1000 | $personal_tmp_cc = imap_mime_header_decode($tmp_cc->personal); |
---|
1001 | $return['cc'] .= '"' . $personal_tmp_cc[0]->text . '"'; |
---|
1002 | $return['cc'] .= " "; |
---|
1003 | $return['cc'] .= "<"; |
---|
1004 | $return['cc'] .= $tmp_cc->mailbox . "@" . $tmp_cc->host; |
---|
1005 | $return['cc'] .= ">"; |
---|
1006 | $return['cc'] .= ", "; |
---|
1007 | } |
---|
1008 | else |
---|
1009 | { |
---|
1010 | $return['cc'] .= $tmp_cc->mailbox . "@" . $tmp_cc->host; |
---|
1011 | $return['cc'] .= ", "; |
---|
1012 | } |
---|
1013 | } |
---|
1014 | $return['cc'] = $this->del_last_two_caracters($return['cc']); |
---|
1015 | } |
---|
1016 | else |
---|
1017 | { |
---|
1018 | $return['cc'] = ""; |
---|
1019 | } |
---|
1020 | |
---|
1021 | ## |
---|
1022 | # @AUTHOR Rodrigo Souza dos Santos |
---|
1023 | # @DATE 2008/09/12 |
---|
1024 | # @BRIEF Adding the BCC field. |
---|
1025 | ## |
---|
1026 | |
---|
1027 | $bcc = $header->bcc; |
---|
1028 | $return['bcc'] = ""; |
---|
1029 | if (!empty($bcc)) |
---|
1030 | { |
---|
1031 | foreach ($bcc as $tmp_bcc) |
---|
1032 | { |
---|
1033 | if (!empty($tmp_bcc->personal)) |
---|
1034 | { |
---|
1035 | $personal_tmp_bcc = imap_mime_header_decode($tmp_bcc->personal); |
---|
1036 | $return['bcc'] .= '"' . $personal_tmp_bcc[0]->text . '"'; |
---|
1037 | $return['bcc'] .= " "; |
---|
1038 | $return['bcc'] .= "<"; |
---|
1039 | $return['bcc'] .= $tmp_bcc->mailbox . "@" . $tmp_bcc->host; |
---|
1040 | $return['bcc'] .= ">"; |
---|
1041 | $return['bcc'] .= ", "; |
---|
1042 | } |
---|
1043 | else |
---|
1044 | { |
---|
1045 | $return['bcc'] .= $tmp_bcc->mailbox . "@" . $tmp_bcc->host; |
---|
1046 | $return['bcc'] .= ", "; |
---|
1047 | } |
---|
1048 | } |
---|
1049 | $return['bcc'] = $this->del_last_two_caracters($return['bcc']); |
---|
1050 | } |
---|
1051 | else |
---|
1052 | { |
---|
1053 | $return['bcc'] = ""; |
---|
1054 | } |
---|
1055 | |
---|
1056 | $reply_to = $header->reply_to; |
---|
1057 | $return['reply_to'] = ""; |
---|
1058 | if (is_object($reply_to[0])) |
---|
1059 | { |
---|
1060 | if ($return['from']['email'] != ($reply_to[0]->mailbox."@".$reply_to[0]->host)) |
---|
1061 | { |
---|
1062 | if (!empty($reply_to[0]->personal)) |
---|
1063 | { |
---|
1064 | $personal_reply_to = imap_mime_header_decode($tmp_reply_to->personal); |
---|
1065 | if(!empty($personal_reply_to[0]->text)) { |
---|
1066 | $return['reply_to'] .= '"' . $personal_reply_to[0]->text . '"'; |
---|
1067 | $return['reply_to'] .= " "; |
---|
1068 | $return['reply_to'] .= "<"; |
---|
1069 | $return['reply_to'] .= $reply_to[0]->mailbox . "@" . $reply_to[0]->host; |
---|
1070 | $return['reply_to'] .= ">"; |
---|
1071 | } |
---|
1072 | else { |
---|
1073 | $return['reply_to'] .= $reply_to[0]->mailbox . "@" . $reply_to[0]->host; |
---|
1074 | } |
---|
1075 | } |
---|
1076 | else |
---|
1077 | { |
---|
1078 | $return['reply_to'] .= $reply_to[0]->mailbox . "@" . $reply_to[0]->host; |
---|
1079 | } |
---|
1080 | } |
---|
1081 | } |
---|
1082 | $return['reply_to'] = $this->decode_string($return['reply_to']); |
---|
1083 | $return['subject'] = $this->decode_string($header->fetchsubject); |
---|
1084 | $return['Size'] = $header->Size; |
---|
1085 | $return['reply_toaddress'] = $header->reply_toaddress; |
---|
1086 | |
---|
1087 | //All this is to help in local messages |
---|
1088 | $return['timestamp'] = $header->udate; |
---|
1089 | $return['login'] = $_SESSION['phpgw_info']['expressomail']['user']['account_id'];//$GLOBALS['phpgw_info']['user']['account_id']; |
---|
1090 | $return['reply_toaddress'] = $header->reply_toaddress; |
---|
1091 | |
---|
1092 | return $return; |
---|
1093 | } |
---|
1094 | |
---|
1095 | /* |
---|
1096 | * Converte textos utf8 para o padrão html. |
---|
1097 | * @link http://php.net/manual/en/function.utf8-decode.php |
---|
1098 | * @author luka8088 <luka8088@gmail.com> |
---|
1099 | */ |
---|
1100 | function utf8_to_html ($data) |
---|
1101 | { |
---|
1102 | return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '$this->_utf8_to_html("\\1")', $data); |
---|
1103 | } |
---|
1104 | |
---|
1105 | function _utf8_to_html ($data) |
---|
1106 | { |
---|
1107 | $ret = 0; |
---|
1108 | foreach((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v) |
---|
1109 | $ret += (ord($v) % 128) * pow(64, $k); |
---|
1110 | return "&#$ret;"; |
---|
1111 | } |
---|
1112 | //------------------------------------------------------------------------------// |
---|
1113 | |
---|
1114 | |
---|
1115 | |
---|
1116 | |
---|
1117 | /** |
---|
1118 | * Decodifica uma part da mensagem para iso-8859-1 |
---|
1119 | * @param <type> $part parte do email |
---|
1120 | * @param <type> $encode codificação da parte |
---|
1121 | * @return <type> string decodificada |
---|
1122 | */ |
---|
1123 | function decodeMailPart($part, $encode, $html = true) |
---|
1124 | { |
---|
1125 | switch (strtolower($encode)) |
---|
1126 | { |
---|
1127 | case 'iso-8859-1': |
---|
1128 | return $part; |
---|
1129 | break; |
---|
1130 | |
---|
1131 | case 'utf-8': |
---|
1132 | if ($html) |
---|
1133 | return $this->utf8_to_html($part); |
---|
1134 | else |
---|
1135 | return utf8_decode ($part); |
---|
1136 | break; |
---|
1137 | |
---|
1138 | default: |
---|
1139 | return mb_convert_encoding($part, 'iso-8859-1'); |
---|
1140 | break; |
---|
1141 | } |
---|
1142 | } |
---|
1143 | |
---|
1144 | |
---|
1145 | function get_body_msg($msg_number, $msg_folder) |
---|
1146 | { |
---|
1147 | /* |
---|
1148 | * Requires of librarys |
---|
1149 | */ |
---|
1150 | require_once $_SESSION['rootPath'].'/library/mime/mimePart.php'; |
---|
1151 | require_once $_SESSION['rootPath'].'/library/mime/mimeDecode.php'; |
---|
1152 | require_once $_SESSION['rootPath'].'/expressoMail1_2/inc/class.attachment.inc.php'; |
---|
1153 | include_once ("class.message_components.inc.php"); |
---|
1154 | //--------------------------------------------------------------------// |
---|
1155 | |
---|
1156 | $return = array(); |
---|
1157 | |
---|
1158 | $msg = &new message_components($this->mbox); |
---|
1159 | $msg->fetch_structure($msg_number); |
---|
1160 | |
---|
1161 | $content = ''; |
---|
1162 | |
---|
1163 | $rawMessageData = $this->getRawHeader($msg_number).$this->getRawBody($msg_number); |
---|
1164 | |
---|
1165 | $decoder = new Mail_mimeDecode($rawMessageData); |
---|
1166 | |
---|
1167 | $params['include_bodies'] = true; |
---|
1168 | $params['decode_bodies'] = true; |
---|
1169 | $params['decode_headers'] = true; |
---|
1170 | $structure = $decoder->decode($params); |
---|
1171 | |
---|
1172 | /* |
---|
1173 | * Inicia Gerenciador de Anexos |
---|
1174 | */ |
---|
1175 | $attachmentManager = new attachment(); |
---|
1176 | $attachmentManager->setStructure($structure); |
---|
1177 | //----------------------------------------------// |
---|
1178 | |
---|
1179 | /* |
---|
1180 | * Monta informações dos anexos para o cabecalhos |
---|
1181 | */ |
---|
1182 | $attachments = $attachmentManager->getAttachmentsInfo(); |
---|
1183 | $return['attachments'] = $attachments; |
---|
1184 | //----------------------------------------------// |
---|
1185 | |
---|
1186 | /* |
---|
1187 | * Monta informações das imagens |
---|
1188 | */ |
---|
1189 | $images = $attachmentManager->getEmbeddedImagesInfo(); |
---|
1190 | //----------------------------------------------// |
---|
1191 | |
---|
1192 | if(!$this->has_cid) |
---|
1193 | { |
---|
1194 | $return['thumbs'] = $this->get_thumbs($images,$msg_number,$msg_folder); |
---|
1195 | $return['signature'] = $this->get_signature($msg,$msg_number,$msg_folder); |
---|
1196 | } |
---|
1197 | |
---|
1198 | switch (strtolower($structure->ctype_primary)) |
---|
1199 | { |
---|
1200 | case 'text': |
---|
1201 | if(strtolower($structure->ctype_secondary) == 'x-pkcs7-mime') |
---|
1202 | { |
---|
1203 | $return['body']='isCripted'; |
---|
1204 | return $return; |
---|
1205 | } |
---|
1206 | $attachment = array(); |
---|
1207 | |
---|
1208 | $msg_subtype = strtolower($structure->ctype_secondary); |
---|
1209 | $disposition = strtolower($structure->disposition); |
---|
1210 | |
---|
1211 | if(($msg_subtype == "html" || $msg_subtype == 'plain') && ($disposition != 'attachment')) |
---|
1212 | { |
---|
1213 | |
---|
1214 | |
---|
1215 | if(strtolower($msg_subtype) == 'plain') |
---|
1216 | { |
---|
1217 | $content = $this->decodeMailPart($structure->body, $structure->ctype_parameters['charset'],false); |
---|
1218 | $content = str_replace(array('<', '>'), array(' #$<$# ', ' #$>$# '), $content); |
---|
1219 | $content = htmlentities($content); |
---|
1220 | $this->replace_links($content); |
---|
1221 | $content = str_replace(array(' #$<$# ', ' #$>$# '), array('<', '>'), $content); |
---|
1222 | $content = '<pre>' . $content . '</pre>'; |
---|
1223 | $return[ 'body' ] = $content; |
---|
1224 | return $return; |
---|
1225 | } |
---|
1226 | $content = $this->decodeMailPart($structure->body, $structure->ctype_parameters['charset']); |
---|
1227 | } |
---|
1228 | |
---|
1229 | break; |
---|
1230 | |
---|
1231 | case 'multipart': |
---|
1232 | $this-> builderMsgBody($structure , $content); |
---|
1233 | |
---|
1234 | break; |
---|
1235 | |
---|
1236 | case 'message': |
---|
1237 | if(!is_array($structure->parts)) |
---|
1238 | { |
---|
1239 | $content .= "<hr align='left' width='95%' style='border:1px solid #DCDCDC'>"; |
---|
1240 | $content .= '<pre>'.htmlentities($this->decodeMailPart($structure->body, $structure->ctype_parameters['charset'],false)).'</pre>'; |
---|
1241 | $content .= "<hr align='left' width='95%' style='border:1px solid #DCDCDC'>"; |
---|
1242 | } |
---|
1243 | else |
---|
1244 | $this->builderMsgBody($structure , $content,true); |
---|
1245 | |
---|
1246 | break; |
---|
1247 | |
---|
1248 | default: |
---|
1249 | if(count($attachments) > 0) |
---|
1250 | $content .= ''; |
---|
1251 | else |
---|
1252 | { |
---|
1253 | $content .= $this->functions->getLang('Message not supported') . '. '; |
---|
1254 | $content .= $this->functions->getLang('Type') . ': ' . $structure->ctype_primary . '/' . $structure->ctype_secondary; |
---|
1255 | } |
---|
1256 | break; |
---|
1257 | } |
---|
1258 | |
---|
1259 | $params = array('folder' => $msg_folder, "msgs_to_set" => $msg_number, "flag" => "seen"); |
---|
1260 | $this->set_messages_flag($params); |
---|
1261 | $content = $this->process_embedded_images($images,$msg_number,$content, $msg_folder); |
---|
1262 | $content = $this->replace_special_characters($content); |
---|
1263 | $this->replace_links($content); |
---|
1264 | $return['body'] = &$content; |
---|
1265 | |
---|
1266 | return $return; |
---|
1267 | } |
---|
1268 | |
---|
1269 | function htmlfilter($body) |
---|
1270 | { |
---|
1271 | require_once('htmlfilter.inc'); |
---|
1272 | |
---|
1273 | $tag_list = Array( |
---|
1274 | false, |
---|
1275 | 'blink', |
---|
1276 | 'object', |
---|
1277 | 'meta', |
---|
1278 | 'html', |
---|
1279 | 'link', |
---|
1280 | 'frame', |
---|
1281 | 'iframe', |
---|
1282 | 'layer', |
---|
1283 | 'ilayer', |
---|
1284 | 'plaintext' |
---|
1285 | ); |
---|
1286 | |
---|
1287 | /** |
---|
1288 | * A very exclusive set: |
---|
1289 | */ |
---|
1290 | // $tag_list = Array(true, "b", "a", "i", "img", "strong", "em", "p"); |
---|
1291 | $rm_tags_with_content = Array( |
---|
1292 | 'script', |
---|
1293 | 'style', |
---|
1294 | 'applet', |
---|
1295 | 'embed', |
---|
1296 | 'head', |
---|
1297 | 'frameset', |
---|
1298 | 'xml', |
---|
1299 | 'xmp' |
---|
1300 | ); |
---|
1301 | |
---|
1302 | $self_closing_tags = Array( |
---|
1303 | 'img', |
---|
1304 | 'br', |
---|
1305 | 'hr', |
---|
1306 | 'input' |
---|
1307 | ); |
---|
1308 | |
---|
1309 | $force_tag_closing = true; |
---|
1310 | |
---|
1311 | $rm_attnames = Array( |
---|
1312 | '/.*/' => |
---|
1313 | Array( |
---|
1314 | '/target/i', |
---|
1315 | //'/^on.*/i', -> onClick, dos compromissos da agenda. |
---|
1316 | '/^dynsrc/i', |
---|
1317 | '/^datasrc/i', |
---|
1318 | '/^data.*/i', |
---|
1319 | '/^lowsrc/i' |
---|
1320 | ) |
---|
1321 | ); |
---|
1322 | |
---|
1323 | /** |
---|
1324 | * Yeah-yeah, so this looks horrible. Check out htmlfilter.inc for |
---|
1325 | * some idea of what's going on here. :) |
---|
1326 | */ |
---|
1327 | |
---|
1328 | $bad_attvals = Array( |
---|
1329 | '/.*/' => |
---|
1330 | Array( |
---|
1331 | '/.*/' => |
---|
1332 | Array( |
---|
1333 | Array( |
---|
1334 | '/^([\'\"])\s*\S+\s*script\s*:*(.*)([\'\"])/si', |
---|
1335 | //'/^([\'\"])\s*https*\s*:(.*)([\'\"])/si', -> doclinks notes |
---|
1336 | '/^([\'\"])\s*mocha\s*:*(.*)([\'\"])/si', |
---|
1337 | '/^([\'\"])\s*about\s*:(.*)([\'\"])/si' |
---|
1338 | ), |
---|
1339 | Array( |
---|
1340 | '\\1oddjob:\\2\\1', |
---|
1341 | //'\\1uucp:\\2\\1', -> doclinks notes |
---|
1342 | '\\1amaretto:\\2\\1', |
---|
1343 | '\\1round:\\2\\1' |
---|
1344 | ) |
---|
1345 | ), |
---|
1346 | |
---|
1347 | '/^style/i' => |
---|
1348 | Array( |
---|
1349 | Array( |
---|
1350 | '/expression/i', |
---|
1351 | '/behaviou*r/i', |
---|
1352 | '/binding/i', |
---|
1353 | '/include-source/i', |
---|
1354 | '/url\s*\(\s*([\'\"]*)\s*https*:.*([\'\"]*)\s*\)/si', |
---|
1355 | '/url\s*\(\s*([\'\"]*)\s*\S+\s*script:.*([\'\"]*)\s*\)/si' |
---|
1356 | ), |
---|
1357 | Array( |
---|
1358 | 'idiocy', |
---|
1359 | 'idiocy', |
---|
1360 | 'idiocy', |
---|
1361 | 'idiocy', |
---|
1362 | 'url(\\1http://securityfocus.com/\\1)', |
---|
1363 | 'url(\\1http://securityfocus.com/\\1)' |
---|
1364 | ) |
---|
1365 | ) |
---|
1366 | ) |
---|
1367 | ); |
---|
1368 | |
---|
1369 | $add_attr_to_tag = Array( |
---|
1370 | '/^a$/i' => Array('target' => '"_new"') |
---|
1371 | ); |
---|
1372 | |
---|
1373 | |
---|
1374 | $trusted_body = sanitize($body, |
---|
1375 | $tag_list, |
---|
1376 | $rm_tags_with_content, |
---|
1377 | $self_closing_tags, |
---|
1378 | $force_tag_closing, |
---|
1379 | $rm_attnames, |
---|
1380 | $bad_attvals, |
---|
1381 | $add_attr_to_tag |
---|
1382 | ); |
---|
1383 | |
---|
1384 | return $trusted_body; |
---|
1385 | } |
---|
1386 | |
---|
1387 | function decodeBody($body, $encoding, $charset=null) |
---|
1388 | { |
---|
1389 | |
---|
1390 | if ($encoding == 'quoted-printable') |
---|
1391 | { |
---|
1392 | $body = quoted_printable_decode($body); |
---|
1393 | |
---|
1394 | } |
---|
1395 | else if ($encoding == 'base64') |
---|
1396 | { |
---|
1397 | $body = base64_decode($body); |
---|
1398 | } |
---|
1399 | // All other encodings are returned raw. |
---|
1400 | if (strtolower($charset) == "utf-8") |
---|
1401 | return utf8_decode($body); |
---|
1402 | else |
---|
1403 | return $body; |
---|
1404 | } |
---|
1405 | |
---|
1406 | |
---|
1407 | /** |
---|
1408 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
1409 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
1410 | * @param $images |
---|
1411 | * @param $msgno |
---|
1412 | * @param $body |
---|
1413 | * @param $msg_folder |
---|
1414 | */ |
---|
1415 | function process_embedded_images($images, $msgno, $body, $msg_folder) |
---|
1416 | { |
---|
1417 | |
---|
1418 | foreach ($images as $image) |
---|
1419 | { |
---|
1420 | $image['cid'] = eregi_replace("<", "", $image['cid']); |
---|
1421 | $image['cid'] = eregi_replace(">", "", $image['cid']); |
---|
1422 | $body = eregi_replace("<br/>", "", $body); |
---|
1423 | $body = str_replace("src=\"cid:".$image['cid']."\"", " src=\"./inc/show_embedded_attach.php?msg_folder=$msg_folder&msg_num=$msgno&msg_part=".$image['pid']."\" ", $body); |
---|
1424 | $body = str_replace("src='cid:".$image['cid']."'", " src=\"./inc/show_embedded_attach.php?msg_folder=$msg_folder&msg_num=$msgno&msg_part=".$image['pid']."\" ", $body); |
---|
1425 | $body = str_replace("src=cid:".$image['cid'], " src=\"./inc/show_embedded_attach.php?msg_folder=$msg_folder&msg_num=$msgno&msg_part=".$image['pid']."\" ", $body); |
---|
1426 | } |
---|
1427 | return $body; |
---|
1428 | } |
---|
1429 | |
---|
1430 | function replace_special_characters($body) |
---|
1431 | { |
---|
1432 | // Suspected TAGS! |
---|
1433 | // $tag_list = Array('blink','object','meta','html','link','frame','iframe','layer','ilayer','plaintext','script','style','img','applet','embed','head','frameset','xml','xmp'); |
---|
1434 | |
---|
1435 | // remove MS Office's proprietary tag |
---|
1436 | //$body = mb_ereg_replace('<!\-\-\[if [^!]* mso .*\]>.*<!\[endif\]\-\->', '', $body); |
---|
1437 | |
---|
1438 | // Layout problem: Change html elements |
---|
1439 | // with absolute position to relate position, CASE INSENSITIVE. |
---|
1440 | $body = @mb_eregi_replace("POSITION: ABSOLUTE;","",$body); |
---|
1441 | |
---|
1442 | // tags to be removed doe to security reasons |
---|
1443 | $tag_list = Array( |
---|
1444 | 'head','blink','object','frame','iframe', |
---|
1445 | 'layer','ilayer','plaintext','script', |
---|
1446 | 'applet','embed','frameset','xml','xmp' |
---|
1447 | ); |
---|
1448 | |
---|
1449 | foreach($tag_list as $index => $tag) { |
---|
1450 | $body = @mb_eregi_replace("<$tag\\b[^>]*>(.*?)</$tag>", "<!-- TAG <$tag> Removed by ExpressoMail -->", $body); |
---|
1451 | } |
---|
1452 | |
---|
1453 | //try to wrap CSS code instead of remove STYLE tags |
---|
1454 | require_once('../library/csstidy/class.csstidy.php'); |
---|
1455 | $css = new csstidy(); |
---|
1456 | $css->set_cfg('preserve_css', false); |
---|
1457 | |
---|
1458 | $regs_found = array(); |
---|
1459 | $tags_found = @mb_eregi("<style\b[^>]*>(.*?)</style>", $body, $regs_found); |
---|
1460 | |
---|
1461 | foreach ($regs_found as $block_found) { |
---|
1462 | $n_start = strpos($block_found, '>')+1; |
---|
1463 | $n_length = strrpos($block_found, '<')-$n_start; |
---|
1464 | $bf_innerHTML = substr($block_found, $n_start, $n_length); |
---|
1465 | |
---|
1466 | $bf_innerHTML = mb_ereg_replace('<!--', '', $bf_innerHTML); |
---|
1467 | $bf_innerHTML = mb_ereg_replace('-->', '', $bf_innerHTML); |
---|
1468 | |
---|
1469 | $css->parse($bf_innerHTML); |
---|
1470 | |
---|
1471 | $prefix = ".$wrapper_class "; |
---|
1472 | foreach ($css->css[41] as $key => $value) { |
---|
1473 | //explode multiple selectors per block |
---|
1474 | $selectors = explode(',', $key); |
---|
1475 | |
---|
1476 | foreach ($selectors as $selector) { |
---|
1477 | if (ereg('\*', $key)) { |
---|
1478 | //skip selecto '*' |
---|
1479 | continue; |
---|
1480 | } |
---|
1481 | |
---|
1482 | $selector = eregi_replace('[^#\.]*body.*', '', $selector); |
---|
1483 | $css->css[41][$prefix.trim($selector)] = $value; |
---|
1484 | } |
---|
1485 | unset($css->css[41][$key]); |
---|
1486 | } |
---|
1487 | |
---|
1488 | $body = str_replace($block_found, '<style>'.$css->print->plain().'</style>', $body); |
---|
1489 | } |
---|
1490 | |
---|
1491 | |
---|
1492 | // Malicious Code Remove |
---|
1493 | $dirtyCodePattern = "/(<([\w]+[\w0-9]*)(.*)on(mouse(move|over|down|up)|load|blur|change|error|click|dblclick|focus|key(down|up|press)|select)([\n\ ]*)=([\n\ ]*)[\"'][^>\"']*[\"']([^>]*)>)(.*)(<\/\\2>)?/misU"; |
---|
1494 | preg_match_all($dirtyCodePattern,$body,$rest,PREG_PATTERN_ORDER); |
---|
1495 | foreach($rest[0] as $i => $val) { |
---|
1496 | if (!(preg_match("/javascript:window\.open\(\"([^'\"]*)\/index\.php\?menuaction=calendar\.uicalendar\.set_action\&cal_id=([^;'\"]+);?['\"]/i",$rest[1][$i]) && strtoupper($rest[4][$i]) == "CLICK" )) //Calendar events |
---|
1497 | $body = str_replace($rest[1][$i],"<".$rest[2][$i].$rest[3][$i].$rest[7][$i].">",$body); |
---|
1498 | } |
---|
1499 | |
---|
1500 | /* |
---|
1501 | * Remove deslocamento a esquerda colocado pelo Outlook. |
---|
1502 | * Este delocamento faz com que algumas palavras fiquem escondidas atras da barra lateral do expresso. |
---|
1503 | */ |
---|
1504 | $body = mb_ereg_replace("(<p[^>]*)(text-indent:[^>;]*-[^>;]*;)([^>]*>)","\\1\\3",$body); |
---|
1505 | $body = mb_ereg_replace("(<p[^>]*)(margin-right:[^>;]*-[^>;]*;)([^>]*>)","\\1\\3",$body); |
---|
1506 | $body = mb_ereg_replace("(<p[^>]*)(margin-left:[^>;]*-[^>;]*;)([^>]*>)","\\1\\3",$body); |
---|
1507 | //--------------------------------------------------------------------------------------------// |
---|
1508 | |
---|
1509 | //$body = $this->replace_links($body); |
---|
1510 | |
---|
1511 | //Remoção de tags <span></span> para correção de erro no firefox |
---|
1512 | $body = mb_eregi_replace("<span><span>","",$body); |
---|
1513 | $body = mb_eregi_replace("</span></span>","",$body); |
---|
1514 | $body = preg_replace("/text-indent:.*;/i","", $body); |
---|
1515 | |
---|
1516 | //Correção para compatibilização com Outlook, ao visualizar a mensagem |
---|
1517 | $body = mb_ereg_replace('<!--\[','<!-- [',$body); |
---|
1518 | $body = mb_ereg_replace('<!\[endif\]-->', '<![endif]-->', $body); |
---|
1519 | |
---|
1520 | return "<div class=\"$wrapper_class\"><span>".$body.'</span></div>'; |
---|
1521 | } |
---|
1522 | |
---|
1523 | |
---|
1524 | |
---|
1525 | function replace_links_callback($matches) |
---|
1526 | { |
---|
1527 | if($matches[3]) |
---|
1528 | $pref = $matches[3]; |
---|
1529 | else |
---|
1530 | $pref = $matches[3] = 'http'; |
---|
1531 | |
---|
1532 | return '<a href="'.$pref.'://'.$matches[4].$matches[5].'" target="_blank">'.$matches[4].$matches[5].'</a>'; |
---|
1533 | } |
---|
1534 | |
---|
1535 | |
---|
1536 | /** |
---|
1537 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
1538 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
1539 | * @param $body corpo da mensagem |
---|
1540 | */ |
---|
1541 | function replace_links(&$body) |
---|
1542 | { |
---|
1543 | // Trata urls do tipo aaaa.bbb.empresa |
---|
1544 | // Usadas na intranet. |
---|
1545 | $pattern = '/(?<=[\s|(<br>)|\n|\r|;])(((http|https|ftp|ftps)?:\/\/((?:[\w]\.?)+(?::[\d]+)?[:\/.\-~&=?%;@#,+\w]*))|((?:www?\.)(?:\w\.?)*(?::\d+)?[\:\/\w.\-~&=?%;@+]*))/i'; |
---|
1546 | $replacement = '<a href="://$4$5" target="_blank">$4$5</a>'; |
---|
1547 | $body = preg_replace_callback($pattern,array( &$this, 'replace_links_callback'), $body); |
---|
1548 | // E-mail address in the text should create a new e-mail on ExpressoMail |
---|
1549 | $pattern = '/( |<|<|>)([A-Za-z0-9\.~?\/_=#\-]*@[A-Za-z0-9\.~?\/_=#\-]*)( |>|>|<)/im'; |
---|
1550 | $replacement = '$1<a href="mailto:$2">$2</a>$3'; |
---|
1551 | $body = preg_replace($pattern, $replacement, $body); |
---|
1552 | |
---|
1553 | } |
---|
1554 | |
---|
1555 | function get_signature($msg, $msg_number, $msg_folder) |
---|
1556 | { |
---|
1557 | include_once(dirname( __FILE__ ) ."/../../security/classes/CertificadoB.php"); |
---|
1558 | include_once("class.db_functions.inc.php"); |
---|
1559 | foreach ($msg->file_type[$msg_number] as $index => $file_type) |
---|
1560 | { |
---|
1561 | $sign = array(); |
---|
1562 | $temp = $this->get_info_head_msg($msg_number); |
---|
1563 | if($temp['ContentType'] =='normal') return $sign; |
---|
1564 | $file_type = strtolower($file_type); |
---|
1565 | if(strtolower($msg->encoding[$msg_number][$index]) == 'base64') |
---|
1566 | { |
---|
1567 | if ($temp['ContentType'] == 'signature') |
---|
1568 | { |
---|
1569 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
1570 | $this->mbox = $this->open_mbox($msg_folder); |
---|
1571 | |
---|
1572 | $header = @imap_headerinfo($this->mbox, imap_msgno($this->mbox, $msg_number), 80, 255); |
---|
1573 | |
---|
1574 | $imap_msg = @imap_fetchheader($this->mbox, $msg_number, FT_UID); |
---|
1575 | $imap_msg .= @imap_body($this->mbox, $msg_number, FT_UID); |
---|
1576 | |
---|
1577 | $certificado = new certificadoB(); |
---|
1578 | $validade = $certificado->verificar($imap_msg); |
---|
1579 | $sign[] = $certificado->msg_sem_assinatura; |
---|
1580 | if ($certificado->apresentado) |
---|
1581 | { |
---|
1582 | $from = $header->from; |
---|
1583 | foreach ($from as $id => $object) |
---|
1584 | { |
---|
1585 | $fromname = $object->personal; |
---|
1586 | $fromaddress = $object->mailbox . "@" . $object->host; |
---|
1587 | } |
---|
1588 | foreach ($certificado->erros_ssl as $item) |
---|
1589 | { |
---|
1590 | $sign[] = $item . "#@#"; |
---|
1591 | } |
---|
1592 | |
---|
1593 | if (count($certificado->erros_ssl) < 1) |
---|
1594 | { |
---|
1595 | $check_msg = 'Message untouched'; |
---|
1596 | if(strtoupper($fromaddress) == strtoupper($certificado->dados['EMAIL'])) |
---|
1597 | { |
---|
1598 | $check_msg .= ' and authentic###'; |
---|
1599 | } |
---|
1600 | else |
---|
1601 | { |
---|
1602 | $check_msg .= ' with signer different from sender#@#'; |
---|
1603 | } |
---|
1604 | $sign[] = $check_msg; |
---|
1605 | } |
---|
1606 | |
---|
1607 | $sign[] = 'Message signed by: ###' . $certificado->dados['NOME']; |
---|
1608 | $sign[] = 'Certificate email: ###' . $certificado->dados['EMAIL']; |
---|
1609 | $sign[] = 'Mail from: ###' . $fromaddress; |
---|
1610 | $sign[] = 'Certificate Authority: ###' . $certificado->dados['EMISSOR']; |
---|
1611 | $sign[] = 'Validity of certificate: ###' . gmdate('r',openssl_to_timestamp($certificado->dados['FIM_VALIDADE'])); |
---|
1612 | $sign[] = 'Message date: ###' . $header->Date; |
---|
1613 | |
---|
1614 | $cert = openssl_x509_parse($certificado->cert_assinante); |
---|
1615 | |
---|
1616 | $sign_alert = array(); |
---|
1617 | $sign_alert[] = 'Certificate Owner###:\n'; |
---|
1618 | $sign_alert[] = 'Common Name (CN)### ' . $cert[subject]['CN'] . '\n'; |
---|
1619 | $X = substr($certificado->dados['NASCIMENTO'] ,0,2) . '-' . substr($certificado->dados['NASCIMENTO'] ,2,2) . '-' . substr($certificado->dados['NASCIMENTO'] ,4,4); |
---|
1620 | $sign_alert[]= 'Organization (O)### ' . $cert[subject]['O'] . '\n'; |
---|
1621 | $sign_alert[]= 'Organizational Unit (OU)### ' . $cert[subject]['OU'][0] . '\n'; |
---|
1622 | //$sign_alert[] = 'Serial Number### ' . $cert['serialNumber'] . '\n'; |
---|
1623 | $sign_alert[] = 'Personal Data###:' . '\n'; |
---|
1624 | $sign_alert[] = 'Birthday### ' . $X . '\n'; |
---|
1625 | $sign_alert[]= 'Fiscal Id### ' . $certificado->dados['CPF'] . '\n'; |
---|
1626 | $sign_alert[]= 'Identification### ' . $certificado->dados['RG'] . '\n\n'; |
---|
1627 | $sign_alert[]= 'Certificate Issuer###:\n'; |
---|
1628 | $sign_alert[]= 'Common Name (CN)### ' . $cert[issuer]['CN'] . '\n'; |
---|
1629 | $sign_alert[]= 'Organization (O)### ' . $cert[issuer]['O'] . '\n'; |
---|
1630 | $sign_alert[]= 'Organizational Unit (OU)### ' . $cert[issuer]['OU'][0] . '\n\n'; |
---|
1631 | $sign_alert[]= 'Validity###:\n'; |
---|
1632 | $H = data_hora($cert[validFrom]); |
---|
1633 | $X = substr($H,6,2) . '-' . substr($H,4,2) . '-' . substr($H,0,4); |
---|
1634 | $sign_alert[]= 'Valid From### ' . $X . '\n'; |
---|
1635 | $H = data_hora($cert[validTo]); |
---|
1636 | $X = substr($H,6,2) . '-' . substr($H,4,2) . '-' . substr($H,0,4); |
---|
1637 | $sign_alert[]= 'Valid Until### ' . $X; |
---|
1638 | $sign[] = $sign_alert; |
---|
1639 | |
---|
1640 | $this->db = new db_functions(); |
---|
1641 | |
---|
1642 | // TODO: testar se existe um certificado no banco e verificar qual ᅵ o mais atual. |
---|
1643 | if(!$certificado->dados['EXPIRADO'] && !$certificado->dados['REVOGADO'] && count($certificado->erros_ssl) < 1) |
---|
1644 | $this->db->insert_certificate(strtolower($certificado->dados['EMAIL']), $certificado->cert_assinante, $certificado->dados['SERIALNUMBER'], $certificado->dados['AUTHORITYKEYIDENTIFIER']); |
---|
1645 | } |
---|
1646 | else |
---|
1647 | { |
---|
1648 | $sign[] = "<span style=color:red>" . $this->functions->getLang('Invalid signature') . "</span>"; |
---|
1649 | foreach($certificado->erros_ssl as $item) |
---|
1650 | $sign[] = "<span style=color:red>" . $this->functions->getLang($item) . "</span>"; |
---|
1651 | } |
---|
1652 | } |
---|
1653 | } |
---|
1654 | } |
---|
1655 | return $sign; |
---|
1656 | } |
---|
1657 | |
---|
1658 | |
---|
1659 | /** |
---|
1660 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
1661 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
1662 | * @param $images |
---|
1663 | * @param $msg_number |
---|
1664 | * @param $msg_folder |
---|
1665 | */ |
---|
1666 | function get_thumbs($images, $msg_number, $msg_folder) |
---|
1667 | { |
---|
1668 | |
---|
1669 | $thumbs_array = array(); |
---|
1670 | $i = 0; |
---|
1671 | |
---|
1672 | foreach ($images as $x => $image) |
---|
1673 | { |
---|
1674 | if($x && $x == 'names') |
---|
1675 | continue; |
---|
1676 | |
---|
1677 | $img = "<img id='".$msg_folder.";;".$msg_number.";;".$i.";;".$image['pid'].";;".$image['encoding']."' title='".$this->functions->getLang("Click here do view (+)")."'src=./inc/show_thumbs.php?&msg_num=".$msg_number."&msg_folder=".$msg_folder."&msg_part=".$image['pid']." />"; |
---|
1678 | $href = "./inc/show_img.php?msg_num=".$msg_number."&msg_folder=".$msg_folder."&msg_part=".$image['pid']; |
---|
1679 | $anchor = "<a class=\"expressomail-thumbs-link\" onMouseDown='save_image(event,this,\"".$image['type']."\")' href=\"".$href."\" onclick=\"window.open('".$href."','mywindow','width=700,height=600,scrollbars=yes');return false;\">". $img ."</a>"; |
---|
1680 | $thumbs_array[] = $anchor; |
---|
1681 | $i++; |
---|
1682 | |
---|
1683 | } |
---|
1684 | return $thumbs_array; |
---|
1685 | } |
---|
1686 | |
---|
1687 | /*function delete_msg($params) |
---|
1688 | { |
---|
1689 | $folder = $params['folder']; |
---|
1690 | $msgs_to_delete = explode(",",$params['msgs_to_delete']); |
---|
1691 | |
---|
1692 | $mbox_stream = $this->open_mbox($folder); |
---|
1693 | |
---|
1694 | foreach ($msgs_to_delete as $msg_number){ |
---|
1695 | imap_delete($mbox_stream, $msg_number, FT_UID); |
---|
1696 | } |
---|
1697 | imap_close($mbox_stream, CL_EXPUNGE); |
---|
1698 | return $params['msgs_to_delete']; |
---|
1699 | }*/ |
---|
1700 | |
---|
1701 | // Novo |
---|
1702 | function delete_msgs($params) |
---|
1703 | { |
---|
1704 | |
---|
1705 | $folder = $params['folder']; |
---|
1706 | $folder = mb_convert_encoding($folder, "UTF7-IMAP","ISO-8859-1"); |
---|
1707 | $msgs_number = explode(",",$params['msgs_number']); |
---|
1708 | $border_ID = $params['border_ID']; |
---|
1709 | |
---|
1710 | $return = array(); |
---|
1711 | |
---|
1712 | if ($params['get_previous_msg']){ |
---|
1713 | $return['previous_msg'] = $this->get_info_previous_msg($params); |
---|
1714 | // Fix problem in unserialize function JS. |
---|
1715 | $return['previous_msg']['body'] = str_replace(array('{','}'), array('{','}'), $return['previous_msg']['body']); |
---|
1716 | } |
---|
1717 | |
---|
1718 | //$mbox_stream = $this->open_mbox($folder); |
---|
1719 | $mbox_stream = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder, $this->username, $this->password) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error())))); |
---|
1720 | |
---|
1721 | foreach ($msgs_number as $msg_number) |
---|
1722 | { |
---|
1723 | if (imap_delete($mbox_stream, $msg_number, FT_UID)); |
---|
1724 | $return['msgs_number'][] = $msg_number; |
---|
1725 | } |
---|
1726 | |
---|
1727 | $return['folder'] = $folder; |
---|
1728 | $return['border_ID'] = $border_ID; |
---|
1729 | |
---|
1730 | if($mbox_stream) |
---|
1731 | imap_close($mbox_stream, CL_EXPUNGE); |
---|
1732 | return $return; |
---|
1733 | } |
---|
1734 | |
---|
1735 | |
---|
1736 | function refresh($params) |
---|
1737 | { |
---|
1738 | |
---|
1739 | $folder = $params['folder']; |
---|
1740 | $msg_range_begin = $params['msg_range_begin']; |
---|
1741 | $msg_range_end = $params['msg_range_end']; |
---|
1742 | $msgs_existent = $params['msgs_existent']; |
---|
1743 | $sort_box_type = $params['sort_box_type']; |
---|
1744 | $sort_box_reverse = $params['sort_box_reverse']; |
---|
1745 | $msgs_in_the_server = array(); |
---|
1746 | $search_box_type = $params['search_box_type'] != "ALL" && $params['search_box_type'] != "" ? $params['search_box_type'] : false; |
---|
1747 | $msgs_in_the_server = $this->get_msgs($folder, $sort_box_type, $search_box_type, $sort_box_reverse,$msg_range_begin,$msg_range_end); |
---|
1748 | $msgs_in_the_server = array_keys($msgs_in_the_server); |
---|
1749 | if(!count($msgs_in_the_server)) |
---|
1750 | return array(); |
---|
1751 | |
---|
1752 | $num_msgs = (count($msgs_in_the_server) - imap_num_recent($this->mbox)); |
---|
1753 | $msgs_in_the_client = explode(",", $msgs_existent); |
---|
1754 | |
---|
1755 | $msg_to_insert = array_diff($msgs_in_the_server, $msgs_in_the_client); |
---|
1756 | $msg_to_delete = array_diff($msgs_in_the_client, $msgs_in_the_server); |
---|
1757 | |
---|
1758 | $msgs_to_exec = array(); |
---|
1759 | foreach($msg_to_insert as $msg_number) |
---|
1760 | $msgs_to_exec[] = $msg_number; |
---|
1761 | //sort($msgs_to_exec); |
---|
1762 | |
---|
1763 | $return = array(); |
---|
1764 | $i = 0; |
---|
1765 | foreach($msgs_to_exec as $msg_number) |
---|
1766 | { |
---|
1767 | /*A função imap_headerinfo não traz o cabeçalho completo, e sim alguns |
---|
1768 | * atributos do cabeçalho. Como eu preciso do atributo Importance |
---|
1769 | * para saber se o email é importante ou não, uso abaixo a função |
---|
1770 | * imap_fetchheader e busco o atributo importance nela para passar |
---|
1771 | * para as funções ajax. Isso faz com que eu acesse o cabeçalho |
---|
1772 | * duas vezes e de duas formas diferentes, mas em contrapartida, eu |
---|
1773 | * não preciso reimplementar o método utilizando o fetchheader. |
---|
1774 | */ |
---|
1775 | |
---|
1776 | $tempHeader = @imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number)); |
---|
1777 | $flag = preg_match('/importance *: *(.*)\r/i', $tempHeader, $importance); |
---|
1778 | $return[$i]['Importance'] = $flag==0?"Normal":$importance[1]; |
---|
1779 | |
---|
1780 | $msg_sample = $this->get_msg_sample($msg_number); |
---|
1781 | $return[$i]['msg_sample'] = $msg_sample; |
---|
1782 | |
---|
1783 | $header = $this->get_header($msg_number); |
---|
1784 | if (!is_object($header)) |
---|
1785 | continue; |
---|
1786 | |
---|
1787 | $return[$i]['msg_number'] = $msg_number; |
---|
1788 | |
---|
1789 | //get the next msg number to append this msg in the view in a correct place |
---|
1790 | $msg_key_position = array_search($msg_number, $msgs_in_the_server); |
---|
1791 | |
---|
1792 | $return[$i]['msg_key_position'] = $msg_key_position; |
---|
1793 | if($msg_key_position !== false && array_key_exists($msg_key_position + 1,$msgs_in_the_server) !== false) |
---|
1794 | $return[$i]['next_msg_number'] = $msgs_in_the_server[$msg_key_position + 1]; |
---|
1795 | else |
---|
1796 | $return[$i]['next_msg_number'] = $msgs_in_the_server[$msg_key_position]; |
---|
1797 | |
---|
1798 | $return[$i]['msg_folder'] = $folder; |
---|
1799 | // Atribui o tipo (normal, signature ou cipher) ao campo Content-Type |
---|
1800 | $return[$i]['ContentType'] = $this->getMessageType($msg_number, $tempHeader); |
---|
1801 | $return[$i]['Recent'] = $header->Recent; |
---|
1802 | $return[$i]['Unseen'] = $header->Unseen; |
---|
1803 | $return[$i]['Answered'] = $header->Answered; |
---|
1804 | $return[$i]['Deleted'] = $header->Deleted; |
---|
1805 | $return[$i]['Draft'] = $header->Draft; |
---|
1806 | $return[$i]['Flagged'] = $header->Flagged; |
---|
1807 | |
---|
1808 | $return[$i]['udate'] = $header->udate; |
---|
1809 | |
---|
1810 | $from = $header->from; |
---|
1811 | $return[$i]['from'] = array(); |
---|
1812 | $tmp = imap_mime_header_decode($from[0]->personal); |
---|
1813 | $return[$i]['from']['name'] = $tmp[0]->text; |
---|
1814 | $return[$i]['from']['email'] = $from[0]->mailbox . "@" . $from[0]->host; |
---|
1815 | //$return[$i]['from']['full'] ='"' . $return[$i]['from']['name'] . '" ' . '<' . $return[$i]['from']['email'] . '>'; |
---|
1816 | if(!$return[$i]['from']['name']) |
---|
1817 | $return[$i]['from']['name'] = $return[$i]['from']['email']; |
---|
1818 | |
---|
1819 | /*$toaddress = imap_mime_header_decode($header->toaddress); |
---|
1820 | $return[$i]['toaddress'] = ''; |
---|
1821 | foreach ($toaddress as $tmp) |
---|
1822 | $return[$i]['toaddress'] .= $tmp->text;*/ |
---|
1823 | $to = $header->to; |
---|
1824 | $return[$i]['to'] = array(); |
---|
1825 | $tmp = imap_mime_header_decode($to[0]->personal); |
---|
1826 | $return[$i]['to']['name'] = $tmp[0]->text; |
---|
1827 | $return[$i]['to']['email'] = $to[0]->mailbox . "@" . $to[0]->host; |
---|
1828 | $return[$i]['to']['full'] ='"' . $return[$i]['to']['name'] . '" ' . '<' . $return[$i]['to']['email'] . '>'; |
---|
1829 | $cc = $header->cc; |
---|
1830 | if ( ($cc) && (!$return[$i]['to']['name']) ){ |
---|
1831 | $return[$i]['to']['name'] = $cc[0]->personal; |
---|
1832 | $return[$i]['to']['email'] = $cc[0]->mailbox . "@" . $cc[0]->host; |
---|
1833 | } |
---|
1834 | $return[$i]['subject'] = $this->decode_string($header->fetchsubject); |
---|
1835 | |
---|
1836 | $return[$i]['Size'] = $header->Size; |
---|
1837 | $return[$i]['reply_toaddress'] = $header->reply_toaddress; |
---|
1838 | |
---|
1839 | $return[$i]['attachment'] = array(); |
---|
1840 | if (!isset($imap_attachment)) |
---|
1841 | { |
---|
1842 | include_once("class.imap_attachment.inc.php"); |
---|
1843 | $imap_attachment = new imap_attachment(); |
---|
1844 | } |
---|
1845 | $return[$i]['attachment'] = $imap_attachment->get_attachment_headerinfo($this->mbox, $msg_number); |
---|
1846 | $i++; |
---|
1847 | } |
---|
1848 | $return['quota'] = $this->get_quota(array('folder_id' => $folder)); |
---|
1849 | $return['sort_box_type'] = $params['sort_box_type']; |
---|
1850 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
1851 | { |
---|
1852 | $this->open_mbox($folder); |
---|
1853 | } |
---|
1854 | $return['new_msgs'] = imap_num_recent($this->mbox); |
---|
1855 | $return['msgs_to_delete'] = $msg_to_delete; |
---|
1856 | $return['offsetToGMT'] = $this->functions->CalculateDateOffset(); |
---|
1857 | if($this->mbox && is_resource($this->mbox)) |
---|
1858 | imap_close($this->mbox); |
---|
1859 | |
---|
1860 | return $return; |
---|
1861 | } |
---|
1862 | |
---|
1863 | /** |
---|
1864 | * Método que faz a verificação do Content-Type do e-mail e verifica se é um e-mail normal, |
---|
1865 | * assinado ou cifrado. |
---|
1866 | * @author Mário César Kolling <mario.kolling@serpro.gov.br> |
---|
1867 | * @param $headers Uma String contendo os Headers do e-mail retornados pela função imap_imap_fetchheader |
---|
1868 | * @param $msg_number O número da mesagem |
---|
1869 | * @return Retorna o tipo da mensagem (normal, signature, cipher). |
---|
1870 | */ |
---|
1871 | function getMessageType($msg_number, $headers = false){ |
---|
1872 | include_once(dirname( __FILE__ ) ."/../../security/classes/CertificadoB.php"); |
---|
1873 | $contentType = "normal"; |
---|
1874 | if (!$headers){ |
---|
1875 | $headers = imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number)); |
---|
1876 | } |
---|
1877 | |
---|
1878 | if (preg_match("/pkcs7-signature/i", $headers) == 1){ |
---|
1879 | $contentType = "signature"; |
---|
1880 | } else if (preg_match("/pkcs7-mime/i", $headers) == 1){ |
---|
1881 | $contentType = testa_p7m( imap_body($this->mbox, imap_msgno($this->mbox, $msg_number)) ); |
---|
1882 | } |
---|
1883 | |
---|
1884 | return $contentType; |
---|
1885 | } |
---|
1886 | |
---|
1887 | /** |
---|
1888 | * Metodo que retorna todas as pastas do usuario logado. |
---|
1889 | * @param $params array opcional para repassar os argumentos ao metodo. |
---|
1890 | * Se usar $params['noSharedFolders'] = true, ira retornar todas as pastas do usuário logado, |
---|
1891 | * excluindo as compartilhadas para ele. |
---|
1892 | * Se usar $params['folderType'] = "default" irá retornar somente as pastas defaults |
---|
1893 | * Se usar $params['folderType'] = "personal" irá retornar somente as pastas pessoais |
---|
1894 | * Se usar $params['folderType'] = null irá retornar todas as pastas |
---|
1895 | * @return Retorna um array contendo as seguintes informacoes de cada pasta: folder_unseen, |
---|
1896 | * folder_id, folder_name, folder_parent e folder_hasChildren. |
---|
1897 | */ |
---|
1898 | function get_folders_list($params = null) |
---|
1899 | { |
---|
1900 | $mbox_stream = $this->open_mbox(); |
---|
1901 | if($params && $params['onload'] && $_SESSION['phpgw_info']['expressomail']['server']['certificado']){ |
---|
1902 | $this->delete_mailbox(array("del_past" => "INBOX".$this->imap_delimiter."decifradas")); |
---|
1903 | } |
---|
1904 | |
---|
1905 | $inbox = 'INBOX'; |
---|
1906 | $trash = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultTrashFolder']; |
---|
1907 | $drafts = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultDraftsFolder']; |
---|
1908 | $spam = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSpamFolder']; |
---|
1909 | $sent = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder']; |
---|
1910 | $uid2cn = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn']; |
---|
1911 | // Free others requests |
---|
1912 | session_write_close(); |
---|
1913 | |
---|
1914 | $serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}"; |
---|
1915 | |
---|
1916 | if ( $params && $params['noSharedFolders'] ) |
---|
1917 | $folders_list = array_merge(imap_getmailboxes($mbox_stream, $serverString, 'INBOX' ), imap_getmailboxes($mbox_stream, $serverString, 'INBOX/*' ) ); |
---|
1918 | else |
---|
1919 | $folders_list = imap_getmailboxes($mbox_stream, $serverString, '*' ); |
---|
1920 | |
---|
1921 | $folders_list = array_slice($folders_list,0,$this->foldersLimit); |
---|
1922 | |
---|
1923 | $tmp = array(); |
---|
1924 | $resultMine = array(); |
---|
1925 | $resultDefault = array(); |
---|
1926 | |
---|
1927 | if (is_array($folders_list)) { |
---|
1928 | reset($folders_list); |
---|
1929 | $this->ldap = new ldap_functions(); |
---|
1930 | |
---|
1931 | $i = 0; |
---|
1932 | while (list($key, $val) = each($folders_list)) { |
---|
1933 | $status = imap_status($mbox_stream, $val->name, SA_UNSEEN); |
---|
1934 | |
---|
1935 | //$tmp_folder_id = explode("}", imap_utf7_decode($val->name)); |
---|
1936 | $tmp_folder_id = explode("}", mb_convert_encoding($val->name, "ISO_8859-1", "UTF7-IMAP" )); |
---|
1937 | |
---|
1938 | if($tmp_folder_id[1]=='INBOX'.$this->imap_delimiter.'decifradas') { |
---|
1939 | //error_log('passou', 3,'/tmp/imap_get_list.log'); |
---|
1940 | //imap_deletemailbox($mbox_stream,imap_utf7_encode("{".$this->imap_server."}".'INBOX/decifradas')); |
---|
1941 | continue; |
---|
1942 | } |
---|
1943 | $result[$i]['folder_unseen'] = $status->unseen; |
---|
1944 | $folder_id = $tmp_folder_id[1]; |
---|
1945 | $result[$i]['folder_id'] = $folder_id; |
---|
1946 | |
---|
1947 | $tmp_folder_parent = explode($this->imap_delimiter, $folder_id); |
---|
1948 | $result[$i]['folder_name'] = array_pop($tmp_folder_parent); |
---|
1949 | $result[$i]['folder_name'] = $result[$i]['folder_name'] == 'INBOX' ? 'Inbox' : $result[$i]['folder_name']; |
---|
1950 | |
---|
1951 | if ($uid2cn && substr($folder_id,0,4) == 'user') { |
---|
1952 | //$this->ldap = new ldap_functions(); |
---|
1953 | if ($cn = $this->ldap->uid2cn($result[$i]['folder_name'])) { |
---|
1954 | $result[$i]['folder_name'] = $cn; |
---|
1955 | } |
---|
1956 | } |
---|
1957 | |
---|
1958 | $tmp_folder_parent = implode($this->imap_delimiter, $tmp_folder_parent); |
---|
1959 | $result[$i]['folder_parent'] = $tmp_folder_parent == 'INBOX' ? '' : $tmp_folder_parent; |
---|
1960 | |
---|
1961 | if (($val->attributes == 32) && ($result[$i]['folder_name'] != 'Inbox')) |
---|
1962 | $result[$i]['folder_hasChildren'] = 1; |
---|
1963 | else |
---|
1964 | $result[$i]['folder_hasChildren'] = 0; |
---|
1965 | |
---|
1966 | switch ($tmp_folder_id[1]) { |
---|
1967 | case $inbox: |
---|
1968 | case $sent: |
---|
1969 | case $drafts: |
---|
1970 | case $spam: |
---|
1971 | case $trash: |
---|
1972 | $resultDefault[]=$result[$i]; |
---|
1973 | break; |
---|
1974 | default: |
---|
1975 | $resultMine[]=$result[$i]; |
---|
1976 | } |
---|
1977 | |
---|
1978 | $i++; |
---|
1979 | } |
---|
1980 | } |
---|
1981 | |
---|
1982 | if ( $params && !$params['noQuotaInfo'] ) { |
---|
1983 | //Get quota info of current folder |
---|
1984 | $current_folder = "INBOX"; |
---|
1985 | if($params && $params['folder']) |
---|
1986 | $current_folder = $params['folder']; |
---|
1987 | |
---|
1988 | $arr_quota_info = $this->get_quota(array('folder_id' => $current_folder)); |
---|
1989 | } else { |
---|
1990 | $arr_quota_info = array(); |
---|
1991 | } |
---|
1992 | |
---|
1993 | // Sorting resultMine |
---|
1994 | foreach ($resultMine as $folder_info) |
---|
1995 | { |
---|
1996 | $array_tmp[] = $folder_info['folder_id']; |
---|
1997 | } |
---|
1998 | |
---|
1999 | natcasesort($array_tmp); |
---|
2000 | |
---|
2001 | $result2 = array(); |
---|
2002 | |
---|
2003 | foreach ($array_tmp as $key => $folder_id) |
---|
2004 | { |
---|
2005 | $result2[] = $resultMine[$key]; |
---|
2006 | } |
---|
2007 | |
---|
2008 | // Sorting resultDefault |
---|
2009 | foreach ($resultDefault as $key => $folder_id) |
---|
2010 | { |
---|
2011 | switch ($resultDefault[$key]['folder_id']) { |
---|
2012 | case $inbox: |
---|
2013 | $resultDefault2[0] = $resultDefault[$key]; |
---|
2014 | break; |
---|
2015 | case $sent: |
---|
2016 | $resultDefault2[1] = $resultDefault[$key]; |
---|
2017 | break; |
---|
2018 | case $drafts: |
---|
2019 | $resultDefault2[2] = $resultDefault[$key]; |
---|
2020 | break; |
---|
2021 | case $spam: |
---|
2022 | $resultDefault2[3] = $resultDefault[$key]; |
---|
2023 | break; |
---|
2024 | case $trash: |
---|
2025 | $resultDefault2[4] = $resultDefault[$key]; |
---|
2026 | break; |
---|
2027 | } |
---|
2028 | } |
---|
2029 | |
---|
2030 | if ( $params && $params['folderType'] && $params['folderType'] == 'default' ) |
---|
2031 | return array_merge($resultDefault2, $arr_quota_info); |
---|
2032 | |
---|
2033 | if ( $params && $params['folderType'] && $params['folderType'] == 'personal' ) |
---|
2034 | return array_merge($result2, $arr_quota_info); |
---|
2035 | |
---|
2036 | // Merge default folders and personal |
---|
2037 | $result2 = array_merge($resultDefault2, $result2); |
---|
2038 | |
---|
2039 | return array_merge($result2, $arr_quota_info); |
---|
2040 | } |
---|
2041 | |
---|
2042 | function create_mailbox($arr) |
---|
2043 | { |
---|
2044 | $namebox = $arr['newp']; |
---|
2045 | $mbox_stream = $this->open_mbox(); |
---|
2046 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
2047 | $namebox = mb_convert_encoding($namebox, "UTF7-IMAP", "UTF-8"); |
---|
2048 | |
---|
2049 | $result = "Ok"; |
---|
2050 | if(!imap_createmailbox($mbox_stream,"{".$imap_server."}$namebox")) |
---|
2051 | { |
---|
2052 | $result = implode("<br />\n", imap_errors()); |
---|
2053 | } |
---|
2054 | |
---|
2055 | if($mbox_stream) |
---|
2056 | imap_close($mbox_stream); |
---|
2057 | |
---|
2058 | return $result; |
---|
2059 | |
---|
2060 | } |
---|
2061 | |
---|
2062 | function create_extra_mailbox($arr) |
---|
2063 | { |
---|
2064 | $nameboxs = explode(";",$arr['nw_folders']); |
---|
2065 | $result = ""; |
---|
2066 | $mbox_stream = $this->open_mbox(); |
---|
2067 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
2068 | foreach($nameboxs as $key=>$tmp){ |
---|
2069 | if($tmp != ""){ |
---|
2070 | if(!imap_createmailbox($mbox_stream,imap_utf7_encode("{".$imap_server."}$tmp"))){ |
---|
2071 | $result = implode("<br />\n", imap_errors()); |
---|
2072 | if($mbox_stream) |
---|
2073 | imap_close($mbox_stream); |
---|
2074 | return $result; |
---|
2075 | } |
---|
2076 | } |
---|
2077 | } |
---|
2078 | if($mbox_stream) |
---|
2079 | imap_close($mbox_stream); |
---|
2080 | return true; |
---|
2081 | } |
---|
2082 | |
---|
2083 | function delete_mailbox($arr) |
---|
2084 | { |
---|
2085 | $namebox = $arr['del_past']; |
---|
2086 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
2087 | $mbox_stream = $this->mbox ? $this->mbox : $this->open_mbox(); |
---|
2088 | //$del_folder = imap_deletemailbox($mbox_stream,"{".$imap_server."}INBOX.$namebox"); |
---|
2089 | |
---|
2090 | $result = "Ok"; |
---|
2091 | $namebox = mb_convert_encoding($namebox, "UTF7-IMAP","UTF-8"); |
---|
2092 | if(!imap_deletemailbox($mbox_stream,"{".$imap_server."}$namebox")) |
---|
2093 | { |
---|
2094 | $result = implode("<br />\n", imap_errors()); |
---|
2095 | } |
---|
2096 | /* |
---|
2097 | if($mbox_stream) |
---|
2098 | imap_close($mbox_stream); |
---|
2099 | */ |
---|
2100 | return $result; |
---|
2101 | } |
---|
2102 | |
---|
2103 | function ren_mailbox($arr) |
---|
2104 | { |
---|
2105 | $namebox = $arr['current']; |
---|
2106 | $new_box = $arr['rename']; |
---|
2107 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
2108 | $mbox_stream = $this->open_mbox(); |
---|
2109 | //$ren_folder = imap_renamemailbox($mbox_stream,"{".$imap_server."}INBOX.$namebox","{".$imap_server."}INBOX.$new_box"); |
---|
2110 | |
---|
2111 | $result = "Ok"; |
---|
2112 | $namebox = mb_convert_encoding($namebox, "UTF7-IMAP","UTF-8"); |
---|
2113 | $new_box = mb_convert_encoding($new_box, "UTF7-IMAP","UTF-8"); |
---|
2114 | |
---|
2115 | if(!imap_renamemailbox($mbox_stream,"{".$imap_server."}$namebox","{".$imap_server."}$new_box")) |
---|
2116 | { |
---|
2117 | $result = imap_errors(); |
---|
2118 | } |
---|
2119 | if($mbox_stream) |
---|
2120 | imap_close($mbox_stream); |
---|
2121 | return $result; |
---|
2122 | |
---|
2123 | } |
---|
2124 | |
---|
2125 | function get_num_msgs($params) |
---|
2126 | { |
---|
2127 | $folder = $params['folder']; |
---|
2128 | if(!$this->mbox || !is_resource($this->mbox)) { |
---|
2129 | $this->mbox = $this->open_mbox($folder); |
---|
2130 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
2131 | return imap_last_error(); |
---|
2132 | } |
---|
2133 | $num_msgs = imap_num_msg($this->mbox); |
---|
2134 | if($this->mbox && is_resource($this->mbox)) |
---|
2135 | imap_close($this->mbox); |
---|
2136 | |
---|
2137 | return $num_msgs; |
---|
2138 | } |
---|
2139 | |
---|
2140 | function folder_exists($folder){ |
---|
2141 | $mbox = $this->open_mbox(); |
---|
2142 | $serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}"; |
---|
2143 | $list = imap_getmailboxes($mbox,$serverString, $folder); |
---|
2144 | $return = is_array($list); |
---|
2145 | imap_close($mbox); |
---|
2146 | return $return; |
---|
2147 | } |
---|
2148 | |
---|
2149 | function send_mail($params) |
---|
2150 | { |
---|
2151 | include_once("class.phpmailer.php"); |
---|
2152 | $mail = new PHPMailer(); |
---|
2153 | include_once("class.db_functions.inc.php"); |
---|
2154 | $db = new db_functions(); |
---|
2155 | $fromaddress = $params['input_from'] ? explode(';',$params['input_from']) : ""; |
---|
2156 | ## |
---|
2157 | # @AUTHOR Rodrigo Souza dos Santos |
---|
2158 | # @DATE 2008/09/17$fileName |
---|
2159 | # @BRIEF Checks if the user has permission to send an email with the email address used. |
---|
2160 | ## |
---|
2161 | if ( is_array($fromaddress) && ($fromaddress[1] != $_SESSION['phpgw_info']['expressomail']['user']['email']) ) |
---|
2162 | { |
---|
2163 | $deny = true; |
---|
2164 | foreach( $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] as $key => $val ) |
---|
2165 | if ( array_key_exists('mail', $val) && $val['mail'][0] == $fromaddress[1] ) |
---|
2166 | $deny = false and end($_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes']); |
---|
2167 | |
---|
2168 | if ( $deny ) |
---|
2169 | return "The server denied your request to send a mail, you cannot use this mail address."; |
---|
2170 | } |
---|
2171 | |
---|
2172 | $toaddress = implode(',',$db->getAddrs(explode(',',$params['input_to']))); |
---|
2173 | $ccaddress = implode(',',$db->getAddrs(explode(',',$params['input_cc']))); |
---|
2174 | $ccoaddress = implode(',',$db->getAddrs(explode(',',$params['input_cco']))); |
---|
2175 | $replytoaddress = $params['input_replyto']; |
---|
2176 | $subject = $params['input_subject']; |
---|
2177 | $msg_uid = $params['msg_id']; |
---|
2178 | $return_receipt = $params['input_return_receipt']; |
---|
2179 | $is_important = $params['input_important_message']; |
---|
2180 | $encrypt = $params['input_return_cripto']; |
---|
2181 | $signed = $params['input_return_digital']; |
---|
2182 | |
---|
2183 | if($params['smime']) |
---|
2184 | { |
---|
2185 | $body = $params['smime']; |
---|
2186 | $mail->SMIME = true; |
---|
2187 | // A MSG assinada deve ser testada neste ponto. |
---|
2188 | // Testar o certificado e a integridade da msg.... |
---|
2189 | include_once(dirname( __FILE__ ) ."/../../security/classes/CertificadoB.php"); |
---|
2190 | $erros_acumulados = ''; |
---|
2191 | $certificado = new certificadoB(); |
---|
2192 | $validade = $certificado->verificar($body); |
---|
2193 | if(!$validade) |
---|
2194 | { |
---|
2195 | foreach($certificado->erros_ssl as $linha_erro) |
---|
2196 | { |
---|
2197 | $erros_acumulados .= $linha_erro; |
---|
2198 | } |
---|
2199 | } |
---|
2200 | else |
---|
2201 | { |
---|
2202 | // Testa o CERTIFICADO: se o CPF he o do usuario logado, se pode assinar msgs e se nao esta expirado... |
---|
2203 | if ($certificado->apresentado) |
---|
2204 | { |
---|
2205 | if($certificado->dados['EXPIRADO']) $erros_acumulados .='Certificado expirado.'; |
---|
2206 | $this->cpf = isset($GLOBALS['phpgw_info']['server']['certificado_atributo_cpf'])&&$GLOBALS['phpgw_info']['server']['certificado_atributo_cpf']!=''?$_SESSION['phpgw_info']['expressomail']['user'][$GLOBALS['phpgw_info']['server']['certificado_atributo_cpf']]:$this->username; |
---|
2207 | if($certificado->dados['CPF'] != $this->cpf) $erros_acumulados .=' CPF no certificado diferente do logado no expresso.'; |
---|
2208 | if(!($certificado->dados['KEYUSAGE']['digitalSignature'] && $certificado->dados['EXTKEYUSAGE']['emailProtection'])) $erros_acumulados .=' Certificado nao permite assinar mensagens.'; |
---|
2209 | } |
---|
2210 | else |
---|
2211 | { |
---|
2212 | $$erros_acumulados .= 'Nao foi possivel usar o certificado para assinar a msg'; |
---|
2213 | } |
---|
2214 | } |
---|
2215 | if(!$erros_acumulados =='') |
---|
2216 | { |
---|
2217 | return $erros_acumulados; |
---|
2218 | } |
---|
2219 | } |
---|
2220 | else |
---|
2221 | { |
---|
2222 | $body = $params['body']; |
---|
2223 | //Compatibilização com Outlook, ao encaminhar a mensagem |
---|
2224 | $body = mb_ereg_replace('<!--\[','<!-- [',$body); |
---|
2225 | } |
---|
2226 | //echo "<script language=\"javascript\">javascript:alert('".$body."');</script>"; |
---|
2227 | $attachments = $_FILES; |
---|
2228 | $forwarding_attachments = $params['forwarding_attachments']; |
---|
2229 | $local_attachments = $params['local_attachments']; |
---|
2230 | |
---|
2231 | //Test if must be saved in shared folder and change if necessary |
---|
2232 | if( $fromaddress[2] == 'y' ){ |
---|
2233 | //build shared folder path |
---|
2234 | $newfolder = "user".$this->imap_delimiter.$fromaddress[3].$this->imap_delimiter.$this->imap_sentfolder; |
---|
2235 | if( $this->folder_exists($newfolder) ) $folder = $newfolder; |
---|
2236 | else $folder = $params['folder']; |
---|
2237 | } else { |
---|
2238 | $folder = $params['folder']; |
---|
2239 | } |
---|
2240 | |
---|
2241 | $folder = mb_convert_encoding($folder, "UTF7-IMAP","ISO_8859-1"); |
---|
2242 | $folder_name = $params['folder_name']; |
---|
2243 | // Fix problem with cyrus delimiter changes. |
---|
2244 | // Dots in names: enabled/disabled. |
---|
2245 | $folder = @eregi_replace("INBOX/", "INBOX".$this->imap_delimiter, $folder); |
---|
2246 | $folder = @eregi_replace("INBOX.", "INBOX".$this->imap_delimiter, $folder); |
---|
2247 | // End Fix. |
---|
2248 | if ($folder != 'null'){ |
---|
2249 | $mail->SaveMessageInFolder = $folder; |
---|
2250 | } |
---|
2251 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2252 | $mail->SMTPDebug = false; |
---|
2253 | |
---|
2254 | if($signed && !$params['smime']) |
---|
2255 | { |
---|
2256 | $mail->Mailer = "smime"; |
---|
2257 | $mail->SignedBody = true; |
---|
2258 | } |
---|
2259 | else |
---|
2260 | $mail->IsSMTP(); |
---|
2261 | |
---|
2262 | $mail->Host = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpServer']; |
---|
2263 | $mail->Port = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpPort']; |
---|
2264 | $mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
2265 | $mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname']; |
---|
2266 | if($fromaddress){ |
---|
2267 | $mail->Sender = $mail->From; |
---|
2268 | $mail->SenderName = $mail->FromName; |
---|
2269 | $mail->FromName = $fromaddress[0]; |
---|
2270 | $mail->From = $fromaddress[1]; |
---|
2271 | } |
---|
2272 | |
---|
2273 | $this->add_recipients("to", $toaddress, &$mail); |
---|
2274 | $this->add_recipients("cc", $ccaddress, &$mail); |
---|
2275 | $this->add_recipients("cco", $ccoaddress, &$mail); |
---|
2276 | $mail->AddReplyTo($replytoaddress); |
---|
2277 | $mail->Subject = $subject; |
---|
2278 | $mail->IsHTML( ( array_key_exists( 'type', $params ) && in_array( strtolower( $params[ 'type' ] ), array( 'html', 'plain' ) ) ) ? strtolower( $params[ 'type' ] ) != 'plain' : true ); |
---|
2279 | $mail->Body = $body; |
---|
2280 | |
---|
2281 | if (($encrypt && $signed && $params['smime']) || ($encrypt && !$signed)) // a msg deve ser enviada cifrada... |
---|
2282 | { |
---|
2283 | $email = $this->add_recipients_cert($toaddress . ',' . $ccaddress. ',' .$ccoaddress); |
---|
2284 | $email = explode(",",$email); |
---|
2285 | // Deve ser testado se foram obtidos os certificados de todos os destinatarios. |
---|
2286 | // Deve ser verificado um numero limite de destinatarios. |
---|
2287 | // Deve ser verificado se os certificados sao validos. |
---|
2288 | // Se uma das verificacoes falhar, nao enviar o e-mail e avisar o usuario. |
---|
2289 | // O array $mail->Certs_crypt soh deve ser preenchido se os certificados passarem nas verificacoes. |
---|
2290 | $numero_maximo = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['num_max_certs_to_cipher']; // Este valor dever ser configurado pelo administrador do site .... |
---|
2291 | $erros_acumulados = ""; |
---|
2292 | $aux_mails = array(); |
---|
2293 | $mail_list = array(); |
---|
2294 | if(count($email) > $numero_maximo) |
---|
2295 | { |
---|
2296 | $erros_acumulados .= "Excedido o numero maximo (" . $numero_maximo . ") de destinatarios para uma msg cifrada...." . chr(0x0A); |
---|
2297 | return $erros_acumulados; |
---|
2298 | } |
---|
2299 | // adiciona o email do remetente. eh para cifrar a msg para ele tambem. Assim vai poder visualizar a msg na pasta enviados.. |
---|
2300 | $email[] = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
2301 | foreach($email as $item) |
---|
2302 | { |
---|
2303 | $certificate = $db->get_certificate(strtolower($item)); |
---|
2304 | if(!$certificate) |
---|
2305 | { |
---|
2306 | $erros_acumulados .= "Chamada com parametro invalido. e-Mail nao pode ser vazio." . chr(0x0A); |
---|
2307 | return $erros_acumulados; |
---|
2308 | } |
---|
2309 | |
---|
2310 | if (array_key_exists("dberr1", $certificate)) |
---|
2311 | { |
---|
2312 | |
---|
2313 | $erros_acumulados .= "Ocorreu um erro quando pesquisava certificados dos destinatarios para cifrar a msg." . chr(0x0A); |
---|
2314 | return $erros_acumulados; |
---|
2315 | } |
---|
2316 | if (array_key_exists("dberr2", $certificate)) |
---|
2317 | { |
---|
2318 | $erros_acumulados .= $item . ' : Nao pode cifrar a msg. Certificado nao localizado.' . chr(0x0A); |
---|
2319 | //continue; |
---|
2320 | } |
---|
2321 | /* Retirado este teste para evitar mensagem de erro duplicada. |
---|
2322 | if (!array_key_exists("certs", $certificate)) |
---|
2323 | { |
---|
2324 | $erros_acumulados .= $item . ' : Nao pode cifrar a msg. Certificado nao localizado.' . chr(0x0A); |
---|
2325 | continue; |
---|
2326 | } |
---|
2327 | */ |
---|
2328 | include_once(dirname( __FILE__ ) ."/../../security/classes/CertificadoB.php"); |
---|
2329 | |
---|
2330 | foreach ($certificate['certs'] as $registro) |
---|
2331 | { |
---|
2332 | $c1 = new certificadoB(); |
---|
2333 | $c1->certificado($registro['chave_publica']); |
---|
2334 | if ($c1->apresentado) |
---|
2335 | { |
---|
2336 | $c2 = new Verifica_Certificado($c1->dados,$registro['chave_publica']); |
---|
2337 | if (!$c1->dados['EXPIRADO'] && !$c2->revogado && $c2->status) |
---|
2338 | { |
---|
2339 | $aux_mails[] = $registro['chave_publica']; |
---|
2340 | $mail_list[] = strtolower($item); |
---|
2341 | } |
---|
2342 | else |
---|
2343 | { |
---|
2344 | if ($c1->dados['EXPIRADO'] || $c2->revogado) |
---|
2345 | { |
---|
2346 | $db->update_certificate($c1->dados['SERIALNUMBER'],$c1->dados['EMAIL'],$c1->dados['AUTHORITYKEYIDENTIFIER'], |
---|
2347 | $c1->dados['EXPIRADO'],$c2->revogado); |
---|
2348 | } |
---|
2349 | |
---|
2350 | $erros_acumulados .= $item . ': ' . $c2->msgerro . chr(0x0A); |
---|
2351 | foreach($c2->erros_ssl as $linha) |
---|
2352 | { |
---|
2353 | $erros_acumulados .= $linha . chr(0x0A); |
---|
2354 | } |
---|
2355 | $erros_acumulados .= 'Emissor: ' . $c1->dados['EMISSOR'] . chr(0x0A); |
---|
2356 | $erros_acumulados .= $c1->dados['CRLDISTRIBUTIONPOINTS'] . chr(0x0A); |
---|
2357 | } |
---|
2358 | } |
---|
2359 | else |
---|
2360 | { |
---|
2361 | $erros_acumulados .= $item . ' : Nao pode cifrar a msg. Certificado invalido.' . chr(0x0A); |
---|
2362 | } |
---|
2363 | } |
---|
2364 | if(!(in_array(strtolower($item),$mail_list)) && !empty($erros_acumulados)) |
---|
2365 | { |
---|
2366 | return $erros_acumulados; |
---|
2367 | } |
---|
2368 | } |
---|
2369 | |
---|
2370 | $mail->Certs_crypt = $aux_mails; |
---|
2371 | } |
---|
2372 | // Build CID images |
---|
2373 | $this->buildEmbeddedImages($mail,$msg_uid,$forwarding_attachments); |
---|
2374 | |
---|
2375 | // Build Uploading Attachments!!! |
---|
2376 | if (count($attachments)>0) //Caso seja forward normal... |
---|
2377 | { |
---|
2378 | $total_uploaded_size = 0; |
---|
2379 | $upload_max_filesize = str_replace("M","",$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['max_attachment_size']) * 1024 * 1024; |
---|
2380 | foreach ($attachments as $attach) |
---|
2381 | { |
---|
2382 | if($attach['error'] == UPLOAD_ERR_INI_SIZE) |
---|
2383 | return $this->parse_error("message file too big"); |
---|
2384 | if($attach['name']=='Unknown') |
---|
2385 | continue; |
---|
2386 | $mail->AddAttachment($attach['tmp_name'], $attach['name'], "base64", $this->get_file_type($attach['name'])); // optional name |
---|
2387 | $total_uploaded_size = $total_uploaded_size + $attach['size']; |
---|
2388 | } |
---|
2389 | if( $total_uploaded_size > $upload_max_filesize){ |
---|
2390 | return $this->parse_error("message file too big"); |
---|
2391 | } |
---|
2392 | } |
---|
2393 | if(count($local_attachments)>0) { //Caso seja forward de mensagens locais |
---|
2394 | |
---|
2395 | $total_uploaded_size = 0; |
---|
2396 | $upload_max_filesize = str_replace("M","",ini_get('upload_max_filesize')) * 1024 * 1024; |
---|
2397 | foreach($local_attachments as $local_attachment) { |
---|
2398 | $file_description = unserialize(rawurldecode($local_attachment)); |
---|
2399 | $tmp = array_values($file_description); |
---|
2400 | foreach($file_description as $i => $descriptor){ |
---|
2401 | $tmp[$i] = eregi_replace('\'*\'','',$descriptor); |
---|
2402 | } |
---|
2403 | $mail->AddAttachment($_FILES[$tmp[1]]['tmp_name'], $tmp[2], "base64", $this->get_file_type($tmp[2])); // optional name |
---|
2404 | $total_uploaded_size = $total_uploaded_size + $_FILES[$tmp[1]]['size']; |
---|
2405 | } |
---|
2406 | if( $total_uploaded_size > $upload_max_filesize) |
---|
2407 | return 'false'; |
---|
2408 | } |
---|
2409 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2410 | // Build Forwarding Attachments!!! |
---|
2411 | if (count($forwarding_attachments) > 0) |
---|
2412 | { |
---|
2413 | // Bug fixed for array_search function |
---|
2414 | $name_cid_files = array(); |
---|
2415 | if(count($name_cid_files) > 0) { |
---|
2416 | $name_cid_files[count($name_cid_files)] = $name_cid_files[0]; |
---|
2417 | $name_cid_files[0] = null; |
---|
2418 | } |
---|
2419 | |
---|
2420 | foreach($forwarding_attachments as $forwarding_attachment) |
---|
2421 | { |
---|
2422 | $file_description = unserialize(rawurldecode($forwarding_attachment)); |
---|
2423 | $tmp = array_values($file_description); |
---|
2424 | foreach($file_description as $i => $descriptor){ |
---|
2425 | $tmp[$i] = eregi_replace('\'*\'','',$descriptor); |
---|
2426 | } |
---|
2427 | $file_description = $tmp; |
---|
2428 | $fileContent = $this->get_forwarding_attachment($file_description[0], $file_description[1], $file_description[3],$file_description[4]); |
---|
2429 | $fileName = $file_description[2]; |
---|
2430 | if(!array_search(trim($fileName),$name_cid_files)) { |
---|
2431 | $mail->AddStringAttachment($fileContent,html_entity_decode(rawurldecode($fileName)), $file_description[4], $this->get_file_type($file_description[2])); |
---|
2432 | } |
---|
2433 | } |
---|
2434 | } |
---|
2435 | |
---|
2436 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2437 | // Important message |
---|
2438 | if($is_important) |
---|
2439 | $mail->isImportant(); |
---|
2440 | |
---|
2441 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2442 | // Disposition-Notification-To |
---|
2443 | if ($return_receipt) |
---|
2444 | $mail->ConfirmReadingTo = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
2445 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2446 | |
---|
2447 | $sent = $mail->Send(); |
---|
2448 | |
---|
2449 | if(!$sent) |
---|
2450 | { |
---|
2451 | return $this->parse_error($mail->ErrorInfo); |
---|
2452 | } |
---|
2453 | else |
---|
2454 | { |
---|
2455 | if ($signed && !$params['smime']) |
---|
2456 | { |
---|
2457 | return $sent; |
---|
2458 | } |
---|
2459 | if($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True") |
---|
2460 | { |
---|
2461 | $userid = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
2462 | $userip = $_SESSION['phpgw_info']['expressomail']['user']['session_ip']; |
---|
2463 | $now = date("d/m/y H:i:s"); |
---|
2464 | $addrs = $toaddress.$ccaddress.$ccoaddress; |
---|
2465 | $sent = trim($sent); |
---|
2466 | error_log("$now - $userip - $sent [$subject] - $userid => $addrs\r\n", 3, "/home/expressolivre/mail_senders.log"); |
---|
2467 | } |
---|
2468 | if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'] && |
---|
2469 | $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_dynamic_contacts']) { |
---|
2470 | $contacts = new dynamic_contacts(); |
---|
2471 | $new_contacts = $contacts->add_dynamic_contacts($toaddress.",".$ccaddress.",".$ccoaddress); |
---|
2472 | return array("success" => true, "new_contacts" => $new_contacts); |
---|
2473 | } |
---|
2474 | return array("success" => true); |
---|
2475 | } |
---|
2476 | } |
---|
2477 | |
---|
2478 | |
---|
2479 | /** |
---|
2480 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
2481 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
2482 | * @param $mail email |
---|
2483 | * @param $msg_uid uid da mensagem |
---|
2484 | * @param $forwarding_attachments anexos |
---|
2485 | */ |
---|
2486 | function buildEmbeddedImages(&$mail,$msg_uid,&$forwarding_attachments) |
---|
2487 | { |
---|
2488 | //Build CID for embedded Images!!! |
---|
2489 | $pattern = '/src="([^"]*?show_embedded_attach.php\?msg_folder=(.+)?&(amp;)?msg_num=(.+)?&(amp;)?msg_part=(.+)?)"/isU'; |
---|
2490 | $cid_imgs = ''; |
---|
2491 | preg_match_all($pattern,$mail->Body,$cid_imgs,PREG_PATTERN_ORDER); |
---|
2492 | $cid_array = array(); |
---|
2493 | |
---|
2494 | foreach($cid_imgs[6] as $j => $val){ |
---|
2495 | if ( !array_key_exists($cid_imgs[4][$j].$val, $cid_array) ) |
---|
2496 | { |
---|
2497 | $cid_array[$cid_imgs[4][$j].$val] = base_convert(microtime(), 10, 36); |
---|
2498 | } |
---|
2499 | $cid = $cid_array[$cid_imgs[4][$j].$val]; |
---|
2500 | |
---|
2501 | $mail->Body = str_replace($cid_imgs[1][$j], "cid:".$cid, $mail->Body); |
---|
2502 | |
---|
2503 | $count = strlen($cid_imgs[6][$j]); |
---|
2504 | $position = substr($cid_imgs[6][$j], 2, $count); |
---|
2505 | $position--; |
---|
2506 | |
---|
2507 | $attach_img = $forwarding_attachments[$position]; |
---|
2508 | $file_description = unserialize(rawurldecode($attach_img)); |
---|
2509 | |
---|
2510 | if (is_array($file_description)) |
---|
2511 | foreach($file_description as $i => $descriptor) |
---|
2512 | $file_description[$i] = eregi_replace('\'*\'','',$descriptor); |
---|
2513 | |
---|
2514 | // The image is not in the same mail? |
---|
2515 | if ($msg_uid != $cid_imgs[4][$j]) |
---|
2516 | { |
---|
2517 | $fileContent = $this->get_forwarding_attachment($cid_imgs[2][$j], $cid_imgs[4][$j], $cid_imgs[6][$j], 'base64'); |
---|
2518 | $fileName = ($msg_uid != 'undefined') ? "image_".($j).".jpg" : $file_description[2]; |
---|
2519 | $fileCode = "base64"; |
---|
2520 | $fileType = "image/jpg"; |
---|
2521 | $file_attached[0] = $cid_imgs[2][$j]; |
---|
2522 | $file_attached[1] = $cid_imgs[4][$j]; |
---|
2523 | $file_attached[2] = $fileName; |
---|
2524 | $file_attached[3] = '0.'.($j+1); |
---|
2525 | $file_attached[4] = 'base64'; |
---|
2526 | $file_attached[5] = strlen($fileContent); //Size of file |
---|
2527 | $file_attached[6] = $cid_imgs[6][$j]; |
---|
2528 | $return_forward[] = $file_attached; |
---|
2529 | |
---|
2530 | if ($file_attached[3] == $file_description[3] || $msg_uid == 'undefined') |
---|
2531 | unset($forwarding_attachments[$position]); |
---|
2532 | |
---|
2533 | } |
---|
2534 | else |
---|
2535 | { |
---|
2536 | $fileContent = $this->get_forwarding_attachment($file_description[0], $msg_uid, $file_description[3], 'base64'); |
---|
2537 | $fileName = $file_description[2]; |
---|
2538 | $fileCode = $file_description[4]; |
---|
2539 | $file_description[3] = '0.'.($j+1); |
---|
2540 | $file_description[6] = $cid_imgs[6][$j]; |
---|
2541 | $fileType = $this->get_file_type($file_description[2]); |
---|
2542 | unset($forwarding_attachments[$position]); |
---|
2543 | if (!empty($file_description)) |
---|
2544 | { |
---|
2545 | $file_description[5] = strlen($fileContent); //Size of file |
---|
2546 | $return_forward[] = $file_description; |
---|
2547 | } |
---|
2548 | } |
---|
2549 | $tempDir = '/tmp'; |
---|
2550 | $file = "cidimage_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].$cid_imgs[6][$j].".dat"; |
---|
2551 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
2552 | fputs($f,$fileContent); |
---|
2553 | fclose($f); |
---|
2554 | |
---|
2555 | if ($fileContent) |
---|
2556 | $mail->AddEmbeddedImage($tempDir.'/'.$file, $cid, $fileName, $fileCode, $fileType); |
---|
2557 | } |
---|
2558 | |
---|
2559 | return $return_forward; |
---|
2560 | } |
---|
2561 | function add_recipients_cert($full_address) |
---|
2562 | { |
---|
2563 | $result = ""; |
---|
2564 | $parse_address = imap_rfc822_parse_adrlist($full_address, ""); |
---|
2565 | foreach ($parse_address as $val) |
---|
2566 | { |
---|
2567 | //echo "<script language=\"javascript\">javascript:alert('".$val->mailbox."@".$val->host."');</script>"; |
---|
2568 | if ($val->mailbox == "INVALID_ADDRESS") |
---|
2569 | continue; |
---|
2570 | if ($val->mailbox == "UNEXPECTED_DATA_AFTER_ADDRESS") |
---|
2571 | continue; |
---|
2572 | if (empty($val->personal)) |
---|
2573 | $result .= $val->mailbox."@".$val->host . ","; |
---|
2574 | else |
---|
2575 | $result .= $val->mailbox."@".$val->host . ","; |
---|
2576 | } |
---|
2577 | |
---|
2578 | return substr($result,0,-1); |
---|
2579 | } |
---|
2580 | |
---|
2581 | function add_recipients($recipient_type, $full_address, $mail) |
---|
2582 | { |
---|
2583 | //remove a comma if is given two unexpected commas |
---|
2584 | $full_address = preg_replace("/, ?,/",",",$full_address); |
---|
2585 | $parse_address = imap_rfc822_parse_adrlist($full_address, ""); |
---|
2586 | foreach ($parse_address as $val) |
---|
2587 | { |
---|
2588 | //echo "<script language=\"javascript\">javascript:alert('".$val->mailbox."@".$val->host."');</script>"; |
---|
2589 | if ($val->mailbox == "INVALID_ADDRESS") |
---|
2590 | continue; |
---|
2591 | |
---|
2592 | if (empty($val->personal)) |
---|
2593 | { |
---|
2594 | switch($recipient_type) |
---|
2595 | { |
---|
2596 | case "to": |
---|
2597 | $mail->AddAddress($val->mailbox."@".$val->host); |
---|
2598 | break; |
---|
2599 | case "cc": |
---|
2600 | $mail->AddCC($val->mailbox."@".$val->host); |
---|
2601 | break; |
---|
2602 | case "cco": |
---|
2603 | $mail->AddBCC($val->mailbox."@".$val->host); |
---|
2604 | break; |
---|
2605 | } |
---|
2606 | } |
---|
2607 | else |
---|
2608 | { |
---|
2609 | switch($recipient_type) |
---|
2610 | { |
---|
2611 | case "to": |
---|
2612 | $mail->AddAddress($val->mailbox."@".$val->host, $val->personal); |
---|
2613 | break; |
---|
2614 | case "cc": |
---|
2615 | $mail->AddCC($val->mailbox."@".$val->host, $val->personal); |
---|
2616 | break; |
---|
2617 | case "cco": |
---|
2618 | $mail->AddBCC($val->mailbox."@".$val->host, $val->personal); |
---|
2619 | break; |
---|
2620 | } |
---|
2621 | } |
---|
2622 | } |
---|
2623 | return true; |
---|
2624 | } |
---|
2625 | |
---|
2626 | function get_forwarding_attachment($msg_folder, $msg_number, $msg_part, $encoding) |
---|
2627 | { |
---|
2628 | include_once $_SESSION['rootPath'].'/expressoMail1_2/inc/class.attachment.inc.php'; |
---|
2629 | $attachment = new attachment(); |
---|
2630 | $attachment->setStructureFromMail($msg_folder, $msg_number); |
---|
2631 | return $attachment->getAttachment($msg_part); |
---|
2632 | } |
---|
2633 | |
---|
2634 | function del_last_caracter($string) |
---|
2635 | { |
---|
2636 | $string = substr($string,0,(strlen($string) - 1)); |
---|
2637 | return $string; |
---|
2638 | } |
---|
2639 | |
---|
2640 | function del_last_two_caracters($string) |
---|
2641 | { |
---|
2642 | $string = substr($string,0,(strlen($string) - 2)); |
---|
2643 | return $string; |
---|
2644 | } |
---|
2645 | |
---|
2646 | function messages_sort($sort_box_type,$sort_box_reverse, $search_box_type,$offsetBegin,$offsetEnd) |
---|
2647 | { |
---|
2648 | if ($sort_box_type != "SORTFROM" && $search_box_type!= "FLAGGED"){ |
---|
2649 | $imapsort = imap_sort($this->mbox,constant($sort_box_type),$sort_box_reverse,SE_UID,$search_box_type); |
---|
2650 | foreach($imapsort as $iuid) |
---|
2651 | $sort[$iuid] = ""; |
---|
2652 | |
---|
2653 | if ($offsetBegin == -1 && $offsetEnd ==-1 ) |
---|
2654 | $slice_array = false; |
---|
2655 | else |
---|
2656 | $slice_array = true; |
---|
2657 | } |
---|
2658 | else |
---|
2659 | { |
---|
2660 | $sort = array(); |
---|
2661 | if ($offsetBegin > $offsetEnd) {$temp=$offsetEnd; $offsetEnd=$offsetBegin; $offsetBegin=$temp;} |
---|
2662 | $num_msgs = imap_num_msg($this->mbox); |
---|
2663 | if ($offsetEnd > $num_msgs) {$offsetEnd = $num_msgs;} |
---|
2664 | $slice_array = true; |
---|
2665 | |
---|
2666 | for ($i=$num_msgs; $i>0; $i--) |
---|
2667 | { |
---|
2668 | if ($sort_box_type == "SORTARRIVAL" && $sort_box_reverse && count($sort) >= $offsetEnd) |
---|
2669 | break; |
---|
2670 | $iuid = @imap_uid($this->mbox,$i); |
---|
2671 | $header = $this->get_header($iuid); |
---|
2672 | // List UNSEEN messages. |
---|
2673 | if($search_box_type == "UNSEEN" && (!trim($header->Recent) && !trim($header->Unseen))){ |
---|
2674 | continue; |
---|
2675 | } |
---|
2676 | // List SEEN messages. |
---|
2677 | elseif($search_box_type == "SEEN" && (trim($header->Recent) || trim($header->Unseen))){ |
---|
2678 | continue; |
---|
2679 | } |
---|
2680 | // List ANSWERED messages. |
---|
2681 | elseif($search_box_type == "ANSWERED" && !trim($header->Answered)){ |
---|
2682 | continue; |
---|
2683 | } |
---|
2684 | // List FLAGGED messages. |
---|
2685 | elseif($search_box_type == "FLAGGED" && !trim($header->Flagged)){ |
---|
2686 | continue; |
---|
2687 | } |
---|
2688 | |
---|
2689 | if($sort_box_type=='SORTFROM') { |
---|
2690 | if (($header->from[0]->mailbox . "@" . $header->from[0]->host) == $_SESSION['phpgw_info']['expressomail']['user']['email']) |
---|
2691 | $from = $header->to; |
---|
2692 | else |
---|
2693 | $from = $header->from; |
---|
2694 | |
---|
2695 | $tmp = imap_mime_header_decode($from[0]->personal); |
---|
2696 | |
---|
2697 | if ($tmp[0]->text != "") |
---|
2698 | $sort[$iuid] = $tmp[0]->text; |
---|
2699 | else |
---|
2700 | $sort[$iuid] = $from[0]->mailbox . "@" . $from[0]->host; |
---|
2701 | } |
---|
2702 | else if($sort_box_type=='SORTSUBJECT') { |
---|
2703 | $sort[$iuid] = $header->subject; |
---|
2704 | } |
---|
2705 | else if($sort_box_type=='SORTSIZE') { |
---|
2706 | $sort[$iuid] = $header->Size; |
---|
2707 | } |
---|
2708 | else { |
---|
2709 | $sort[$iuid] = $header->udate; |
---|
2710 | } |
---|
2711 | |
---|
2712 | } |
---|
2713 | natcasesort($sort); |
---|
2714 | |
---|
2715 | if ($sort_box_reverse) |
---|
2716 | $sort = array_reverse($sort,true); |
---|
2717 | } |
---|
2718 | |
---|
2719 | if(!is_array($sort)) |
---|
2720 | $sort = array(); |
---|
2721 | |
---|
2722 | |
---|
2723 | if ($slice_array) |
---|
2724 | $sort = array_slice($sort,$offsetBegin-1,$offsetEnd-($offsetBegin-1),true); |
---|
2725 | |
---|
2726 | |
---|
2727 | return $sort; |
---|
2728 | |
---|
2729 | } |
---|
2730 | |
---|
2731 | |
---|
2732 | function move_search_messages($params){ |
---|
2733 | $params['selected_messages'] = urldecode($params['selected_messages']); |
---|
2734 | $params['new_folder'] = urldecode($params['new_folder']); |
---|
2735 | $params['new_folder_name'] = urldecode($params['new_folder_name']); |
---|
2736 | $sel_msgs = explode(",", $params['selected_messages']); |
---|
2737 | @reset($sel_msgs); |
---|
2738 | $sorted_msgs = array(); |
---|
2739 | foreach($sel_msgs as $idx => $sel_msg) { |
---|
2740 | $sel_msg = explode(";", $sel_msg); |
---|
2741 | if(array_key_exists($sel_msg[0], $sorted_msgs)){ |
---|
2742 | $sorted_msgs[$sel_msg[0]] .= ",".$sel_msg[1]; |
---|
2743 | } |
---|
2744 | else { |
---|
2745 | $sorted_msgs[$sel_msg[0]] = $sel_msg[1]; |
---|
2746 | } |
---|
2747 | } |
---|
2748 | @ksort($sorted_msgs); |
---|
2749 | $last_return = false; |
---|
2750 | foreach($sorted_msgs as $folder => $msgs_number) { |
---|
2751 | $params['msgs_number'] = $msgs_number; |
---|
2752 | $params['folder'] = $folder; |
---|
2753 | if($params['new_folder'] && $folder != $params['new_folder']){ |
---|
2754 | $last_return = $this -> move_messages($params); |
---|
2755 | } |
---|
2756 | elseif(!$params['new_folder'] || $params['delete'] ){ |
---|
2757 | $last_return = $this -> delete_msgs($params); |
---|
2758 | $last_return['deleted'] = true; |
---|
2759 | } |
---|
2760 | } |
---|
2761 | return $last_return; |
---|
2762 | } |
---|
2763 | |
---|
2764 | function move_messages($params) |
---|
2765 | { |
---|
2766 | $folder = $params['folder']; |
---|
2767 | $mbox_stream = $this->open_mbox($folder); |
---|
2768 | $newmailbox = ($params['new_folder']); |
---|
2769 | $newmailbox = mb_convert_encoding($newmailbox, "UTF7-IMAP","ISO_8859-1"); |
---|
2770 | $new_folder_name = $params['new_folder_name']; |
---|
2771 | $msgs_number = $params['msgs_number']; |
---|
2772 | $return = array('msgs_number' => $msgs_number, |
---|
2773 | 'folder' => $folder, |
---|
2774 | 'new_folder_name' => $new_folder_name, |
---|
2775 | 'border_ID' => $params['border_ID'], |
---|
2776 | 'status' => true); //Status foi adicionado para validar as permissoes ACL |
---|
2777 | |
---|
2778 | //Este bloco tem a finalidade de averiguar as permissoes para pastas compartilhadas |
---|
2779 | if (substr($folder,0,4) == 'user'){ |
---|
2780 | $acl = $this->getacltouser($folder); |
---|
2781 | /* |
---|
2782 | * l - lookup (mailbox is visible to LIST/LSUB commands) |
---|
2783 | * r - read (SELECT the mailbox, perform CHECK, FETCH, PARTIAL, SEARCH, COPY from mailbox) |
---|
2784 | * s - keep seen/unseen information across sessions (STORE SEEN flag) |
---|
2785 | * w - write (STORE flags other than SEEN and DELETED) |
---|
2786 | * i - insert (perform APPEND, COPY into mailbox) |
---|
2787 | * p - post (send mail to submission address for mailbox, not enforced by IMAP4 itself) |
---|
2788 | * c - create (CREATE new sub-mailboxes in any implementation-defined hierarchy) |
---|
2789 | * d - delete (STORE DELETED flag, perform EXPUNGE) |
---|
2790 | * a - administer (perform SETACL) |
---|
2791 | */ |
---|
2792 | if (strpos($acl, "d") === false){ |
---|
2793 | $return['status'] = false; |
---|
2794 | return $return; |
---|
2795 | } |
---|
2796 | } |
---|
2797 | //Este bloco tem a finalidade de transformar o CPF das pastas compartilhadas em common name |
---|
2798 | if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn']){ |
---|
2799 | if (substr($new_folder_name,0,4) == 'user'){ |
---|
2800 | $this->ldap = new ldap_functions(); |
---|
2801 | $tmp_folder_name = explode($this->imap_delimiter, $new_folder_name); |
---|
2802 | $return['new_folder_name'] = array_pop($tmp_folder_name); |
---|
2803 | if( $cn = $this->ldap->uid2cn($return['new_folder_name'])) |
---|
2804 | { |
---|
2805 | $return['new_folder_name'] = $cn; |
---|
2806 | } |
---|
2807 | } |
---|
2808 | } |
---|
2809 | |
---|
2810 | // Caso estejamos no box principal, nao eh necessario pegar a informacao da mensagem anterior. |
---|
2811 | if (($params['get_previous_msg']) && ($params['border_ID'] != 'null') && ($params['border_ID'] != '')) |
---|
2812 | { |
---|
2813 | $return['previous_msg'] = $this->get_info_previous_msg($params); |
---|
2814 | // Fix problem in unserialize function JS. |
---|
2815 | $return['previous_msg']['body'] = str_replace(array('{','}'), array('{','}'), $return['previous_msg']['body']); |
---|
2816 | } |
---|
2817 | |
---|
2818 | $mbox_stream = $this->open_mbox($folder); |
---|
2819 | if(imap_mail_move($mbox_stream, $msgs_number, $newmailbox, CP_UID)) { |
---|
2820 | imap_expunge($mbox_stream); |
---|
2821 | if($mbox_stream) |
---|
2822 | imap_close($mbox_stream); |
---|
2823 | return $return; |
---|
2824 | }else { |
---|
2825 | if(strstr(imap_last_error(),'Over quota')) { |
---|
2826 | $accountID = $_SESSION['phpgw_info']['expressomail']['email_server']['imapAdminUsername']; |
---|
2827 | $pass = $_SESSION['phpgw_info']['expressomail']['email_server']['imapAdminPW']; |
---|
2828 | $userID = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
2829 | $server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
2830 | $mbox = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}INBOX", $accountID, $pass) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error())))); |
---|
2831 | if(!$mbox) |
---|
2832 | return imap_last_error(); |
---|
2833 | $quota = imap_get_quotaroot($mbox_stream, "INBOX"); |
---|
2834 | if(! imap_set_quota($mbox, "user".$this->imap_delimiter.$userID, 2.1 * $quota['usage'])) { |
---|
2835 | if($mbox_stream) |
---|
2836 | imap_close($mbox_stream); |
---|
2837 | if($mbox) |
---|
2838 | imap_close($mbox); |
---|
2839 | return "move_messages(): Error setting quota for MOVE or DELETE!! ". "user".$this->imap_delimiter.$userID." line ".__LINE__."\n"; |
---|
2840 | } |
---|
2841 | if(imap_mail_move($mbox_stream, $msgs_number, $newmailbox, CP_UID)) { |
---|
2842 | imap_expunge($mbox_stream); |
---|
2843 | if($mbox_stream) |
---|
2844 | imap_close($mbox_stream); |
---|
2845 | // return to original quota limit. |
---|
2846 | if(!imap_set_quota($mbox, "user".$this->imap_delimiter.$userID, $quota['limit'])) { |
---|
2847 | if($mbox) |
---|
2848 | imap_close($mbox); |
---|
2849 | return "move_messages(): Error setting quota for MOVE or DELETE!! line ".__LINE__."\n"; |
---|
2850 | } |
---|
2851 | return $return; |
---|
2852 | } |
---|
2853 | else { |
---|
2854 | if($mbox_stream) |
---|
2855 | imap_close($mbox_stream); |
---|
2856 | if(!imap_set_quota($mbox, "user".$this->imap_delimiter.$userID, $quota['limit'])) { |
---|
2857 | if($mbox) |
---|
2858 | imap_close($mbox); |
---|
2859 | return "move_messages(): Error setting quota for MOVE or DELETE!! line ".__LINE__."\n"; |
---|
2860 | } |
---|
2861 | return imap_last_error(); |
---|
2862 | } |
---|
2863 | |
---|
2864 | } |
---|
2865 | else { |
---|
2866 | if($mbox_stream) |
---|
2867 | imap_close($mbox_stream); |
---|
2868 | return "move_messages() line ".__LINE__.": ". imap_last_error()." folder:".$newmailbox; |
---|
2869 | } |
---|
2870 | } |
---|
2871 | } |
---|
2872 | |
---|
2873 | function save_msg($params) |
---|
2874 | { |
---|
2875 | |
---|
2876 | include_once("class.phpmailer.php"); |
---|
2877 | $mail = new PHPMailer(); |
---|
2878 | include_once("class.db_functions.inc.php"); |
---|
2879 | $toaddress = $params['input_to']; |
---|
2880 | $ccaddress = $params['input_cc']; |
---|
2881 | $ccoaddress = $params['input_cco']; |
---|
2882 | $return_receipt = $params['input_return_receipt']; |
---|
2883 | $is_important = $params['input_important_message']; |
---|
2884 | $subject = $params['input_subject']; |
---|
2885 | $msg_uid = $params['msg_id']; |
---|
2886 | $body = $params['body']; |
---|
2887 | $body = str_replace("%nbsp;"," ",$body); |
---|
2888 | $body = preg_replace("/\n/"," ",$body); |
---|
2889 | $body = preg_replace("/\r/","",$body); |
---|
2890 | $forwarding_attachments = $params['forwarding_attachments']; |
---|
2891 | $attachments = $params['FILES']; |
---|
2892 | $return_files = $params['FILES']; |
---|
2893 | |
---|
2894 | |
---|
2895 | $folder = $params['folder']; |
---|
2896 | $folder = mb_convert_encoding($folder, "UTF7-IMAP","ISO_8859-1"); |
---|
2897 | // Fix problem with cyrus delimiter changes. |
---|
2898 | // Dots in names: enabled/disabled. |
---|
2899 | $folder = @eregi_replace("INBOX/", "INBOX".$this->imap_delimiter, $folder); |
---|
2900 | $folder = @eregi_replace("INBOX.", "INBOX".$this->imap_delimiter, $folder); |
---|
2901 | // End Fix. |
---|
2902 | |
---|
2903 | $mail->SaveMessageInFolder = $folder; |
---|
2904 | $mail->SMTPDebug = false; |
---|
2905 | |
---|
2906 | $mail->IsSMTP(); |
---|
2907 | $mail->Host = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpServer']; |
---|
2908 | $mail->Port = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpPort']; |
---|
2909 | $mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
2910 | $mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname']; |
---|
2911 | |
---|
2912 | $mail->Sender = $mail->From; |
---|
2913 | $mail->SenderName = $mail->FromName; |
---|
2914 | $mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname']; |
---|
2915 | $mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
2916 | |
---|
2917 | $this->add_recipients("to", $toaddress, &$mail); |
---|
2918 | $this->add_recipients("cc", $ccaddress, &$mail); |
---|
2919 | $mail->Subject = $subject; |
---|
2920 | $mail->IsHTML(true); |
---|
2921 | $mail->Body = $body; |
---|
2922 | |
---|
2923 | $return_forward = $this->buildEmbeddedImages($mail,$msg_uid,$forwarding_attachments); |
---|
2924 | $imagesParts = array(); |
---|
2925 | |
---|
2926 | foreach ($return_forward as $value) |
---|
2927 | $imagesParts[$value[6]] = $value[3]; |
---|
2928 | |
---|
2929 | //Build Forwarding Attachments!!! |
---|
2930 | foreach($forwarding_attachments as $forwarding_attachment) |
---|
2931 | { |
---|
2932 | $file_description = unserialize(rawurldecode($forwarding_attachment)); |
---|
2933 | $file_description = array_values($file_description); |
---|
2934 | |
---|
2935 | foreach($file_description as $i => $descriptor){ |
---|
2936 | $file_description[$i] = eregi_replace('\'*\'','',$descriptor); |
---|
2937 | } |
---|
2938 | $fileContent = $this->get_forwarding_attachment($file_description[0], $file_description[1], $file_description[3],$file_description[4]); |
---|
2939 | $fileName = $file_description[2]; |
---|
2940 | |
---|
2941 | $file_description[5] = strlen($fileContent); //Size of file |
---|
2942 | $return_forward[] = $file_description; |
---|
2943 | |
---|
2944 | $mail->AddStringAttachment($fileContent, $fileName, $file_description[4], $this->get_file_type($file_description[2])); |
---|
2945 | } |
---|
2946 | |
---|
2947 | if ((count($return_forward) > 0) && (count($return_files) > 0)) |
---|
2948 | { |
---|
2949 | $return_files = array_merge_recursive($return_forward,$return_files); |
---|
2950 | } |
---|
2951 | else if (count($return_files) < 1) |
---|
2952 | { |
---|
2953 | $return_files = $return_forward; |
---|
2954 | } |
---|
2955 | |
---|
2956 | //Build Uploading Attachments!!! |
---|
2957 | $sizeof_attachments = count($attachments); |
---|
2958 | if ($sizeof_attachments) |
---|
2959 | { |
---|
2960 | foreach ($attachments as $numb => $attach) |
---|
2961 | { |
---|
2962 | if ($numb == ($sizeof_attachments-1) && $params['insertImg'] == 'true') |
---|
2963 | { // Auto-resize image |
---|
2964 | list($width, $height,$image_type) = getimagesize($attach['tmp_name']); |
---|
2965 | switch ($image_type) |
---|
2966 | { |
---|
2967 | // Do not corrupt animated gif |
---|
2968 | //case 1: $image_big = imagecreatefromgif($attach['tmp_name']);break; |
---|
2969 | case 2: |
---|
2970 | $image_big = imagecreatefromjpeg($attach['tmp_name']); break; |
---|
2971 | case 3: |
---|
2972 | $image_big = imagecreatefrompng($attach['tmp_name']); break; |
---|
2973 | case 6: |
---|
2974 | require_once("gd_functions.php"); |
---|
2975 | $image_big = imagecreatefrombmp($attach['tmp_name']); break; |
---|
2976 | default: |
---|
2977 | $mail->AddAttachment($attach['tmp_name'], $attach['name'], "base64", $this->get_file_type($attach['name'])); |
---|
2978 | break; |
---|
2979 | } |
---|
2980 | header('Content-type: image/jpeg'); |
---|
2981 | $max_resolution = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['image_size']; |
---|
2982 | $max_resolution = ($max_resolution==""?'65536':$max_resolution); |
---|
2983 | if ($width < $max_resolution && $height < $max_resolution) |
---|
2984 | { |
---|
2985 | $new_width = $width; |
---|
2986 | $new_height = $height; |
---|
2987 | } |
---|
2988 | else if ($width > $max_resolution) |
---|
2989 | { |
---|
2990 | $new_width = $max_resolution; |
---|
2991 | $new_height = $height*($new_width/$width); |
---|
2992 | } |
---|
2993 | else |
---|
2994 | { |
---|
2995 | $new_height = $max_resolution; |
---|
2996 | $new_width = $width*($new_height/$height); |
---|
2997 | } |
---|
2998 | $image_new = imagecreatetruecolor($new_width, $new_height); |
---|
2999 | imagecopyresampled($image_new, $image_big, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
---|
3000 | $tmpDir = '/tmp'; |
---|
3001 | |
---|
3002 | // $tmpDir = ini_get("session.save_path"); |
---|
3003 | $_file = "/cidimage_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".dat"; |
---|
3004 | imagejpeg($image_new,$tmpDir.$_file, 85); |
---|
3005 | $mail->AddAttachment($tmpDir.$_file, $attach['name'], "base64", $this->get_file_type($tmpDir.$_file)); |
---|
3006 | } |
---|
3007 | else |
---|
3008 | $mail->AddAttachment($attach['tmp_name'], $attach['name'], "base64", $this->get_file_type($attach['name'])); |
---|
3009 | |
---|
3010 | } |
---|
3011 | } |
---|
3012 | |
---|
3013 | if(!empty($mail->AltBody)) |
---|
3014 | $mail->ContentType = "multipart/alternative"; |
---|
3015 | |
---|
3016 | $mail->error_count = 0; // reset errors |
---|
3017 | $mail->SetMessageType(); |
---|
3018 | $header = $mail->CreateHeader(); |
---|
3019 | $body = $mail->CreateBody(); |
---|
3020 | |
---|
3021 | $mbox_stream = $this->open_mbox($folder); |
---|
3022 | $new_header = str_replace("\n", "\r\n", $header); |
---|
3023 | $new_body = str_replace("\n", "\r\n", $body); |
---|
3024 | $return['append'] = imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $new_header . $new_body, "\\Seen \\Draft"); |
---|
3025 | $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT); |
---|
3026 | $return['msg_no'] = $status->uidnext - 1; |
---|
3027 | $return['folder_id'] = $folder; |
---|
3028 | $return['imagesParts'] = $imagesParts; |
---|
3029 | |
---|
3030 | if($mbox_stream) |
---|
3031 | imap_close($mbox_stream); |
---|
3032 | |
---|
3033 | if (is_array($return_files)) |
---|
3034 | { |
---|
3035 | foreach ($return_files as $index => $_attachment) |
---|
3036 | { |
---|
3037 | if (array_key_exists("name", $_attachment)) |
---|
3038 | { |
---|
3039 | unset($return_files[$index]); |
---|
3040 | $return_files[$index] = $_attachment['name']."_SIZE_".$return_files[$index][1] = $_attachment['size']; |
---|
3041 | } |
---|
3042 | else |
---|
3043 | { |
---|
3044 | unset($return_files[$index]); |
---|
3045 | $return_files[$index] = $_attachment[2]."_SIZE_". $return_files[$index][1] = $_attachment[5]; |
---|
3046 | } |
---|
3047 | } |
---|
3048 | } |
---|
3049 | |
---|
3050 | $return['files'] = serialize($return_files); |
---|
3051 | $return["subject"] = $subject; |
---|
3052 | |
---|
3053 | if (!$return['append']) |
---|
3054 | $return['append'] = imap_last_error(); |
---|
3055 | |
---|
3056 | return $return; |
---|
3057 | } |
---|
3058 | |
---|
3059 | function set_messages_flag($params) |
---|
3060 | { |
---|
3061 | $folder = $params['folder']; |
---|
3062 | $msgs_to_set = $params['msgs_to_set']; |
---|
3063 | $flag = $params['flag']; |
---|
3064 | $return = array(); |
---|
3065 | $return["msgs_to_set"] = $msgs_to_set; |
---|
3066 | $return["flag"] = $flag; |
---|
3067 | |
---|
3068 | if(!$this->mbox && !is_resource($this->mbox)) |
---|
3069 | $this->mbox = $this->open_mbox($folder); |
---|
3070 | |
---|
3071 | if ($flag == "unseen") |
---|
3072 | $return["status"] = imap_clearflag_full($this->mbox, $msgs_to_set, "\\Seen", ST_UID); |
---|
3073 | elseif ($flag == "seen") |
---|
3074 | $return["status"] = imap_setflag_full($this->mbox, $msgs_to_set, "\\Seen", ST_UID); |
---|
3075 | elseif ($flag == "answered"){ |
---|
3076 | $return["status"] = imap_setflag_full($this->mbox, $msgs_to_set, "\\Answered", ST_UID); |
---|
3077 | imap_clearflag_full($this->mbox, $msgs_to_set, "\\Draft", ST_UID); |
---|
3078 | } |
---|
3079 | elseif ($flag == "forwarded") |
---|
3080 | $return["status"] = imap_setflag_full($this->mbox, $msgs_to_set, "\\Answered \\Draft", ST_UID); |
---|
3081 | elseif ($flag == "flagged") |
---|
3082 | $return["status"] = imap_setflag_full($this->mbox, $msgs_to_set, "\\Flagged", ST_UID); |
---|
3083 | elseif ($flag == "unflagged") { |
---|
3084 | $flag_importance = false; |
---|
3085 | $msgs_number = explode(",",$msgs_to_set); |
---|
3086 | $unflagged_msgs = ""; |
---|
3087 | foreach($msgs_number as $msg_number) { |
---|
3088 | preg_match('/importance *: *(.*)\r/i', |
---|
3089 | imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number)) |
---|
3090 | ,$importance); |
---|
3091 | if(strtolower($importance[1])=="high" && $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) { |
---|
3092 | $flag_importance=true; |
---|
3093 | } |
---|
3094 | else { |
---|
3095 | $unflagged_msgs.=$msg_number.","; |
---|
3096 | } |
---|
3097 | } |
---|
3098 | |
---|
3099 | if($unflagged_msgs!="") { |
---|
3100 | imap_clearflag_full($this->mbox,substr($unflagged_msgs,0,strlen($unflagged_msgs)-1), "\\Flagged", ST_UID); |
---|
3101 | $return["msgs_unflageds"] = substr($unflagged_msgs,0,strlen($unflagged_msgs)-1); |
---|
3102 | } |
---|
3103 | else { |
---|
3104 | $return["msgs_unflageds"] = false; |
---|
3105 | } |
---|
3106 | |
---|
3107 | if($flag_importance && $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) { |
---|
3108 | $return["status"] = false; |
---|
3109 | $return["msg"] = $this->functions->getLang("At least one of selected message cant be marked as normal"); |
---|
3110 | } |
---|
3111 | else { |
---|
3112 | $return["status"] = true; |
---|
3113 | } |
---|
3114 | } |
---|
3115 | |
---|
3116 | if($this->mbox && is_resource($this->mbox)) |
---|
3117 | imap_close($this->mbox); |
---|
3118 | return $return; |
---|
3119 | } |
---|
3120 | |
---|
3121 | function get_file_type($file_name) |
---|
3122 | { |
---|
3123 | $file_name = strtolower($file_name); |
---|
3124 | $strFileType = strrev(substr(strrev($file_name),0,4)); |
---|
3125 | if ($strFileType == ".asf") |
---|
3126 | return "video/x-ms-asf"; |
---|
3127 | if ($strFileType == ".avi") |
---|
3128 | return "video/avi"; |
---|
3129 | if ($strFileType == ".doc") |
---|
3130 | return "application/msword"; |
---|
3131 | if ($strFileType == ".zip") |
---|
3132 | return "application/zip"; |
---|
3133 | if ($strFileType == ".xls") |
---|
3134 | return "application/vnd.ms-excel"; |
---|
3135 | if ($strFileType == ".gif") |
---|
3136 | return "image/gif"; |
---|
3137 | if ($strFileType == ".jpg" || $strFileType == "jpeg") |
---|
3138 | return "image/jpeg"; |
---|
3139 | if ($strFileType == ".png") |
---|
3140 | return "image/png"; |
---|
3141 | if ($strFileType == ".wav") |
---|
3142 | return "audio/wav"; |
---|
3143 | if ($strFileType == ".mp3") |
---|
3144 | return "audio/mpeg3"; |
---|
3145 | if ($strFileType == ".mpg" || $strFileType == "mpeg") |
---|
3146 | return "video/mpeg"; |
---|
3147 | if ($strFileType == ".rtf") |
---|
3148 | return "application/rtf"; |
---|
3149 | if ($strFileType == ".htm" || $strFileType == "html") |
---|
3150 | return "text/html"; |
---|
3151 | if ($strFileType == ".xml") |
---|
3152 | return "text/xml"; |
---|
3153 | if ($strFileType == ".xsl") |
---|
3154 | return "text/xsl"; |
---|
3155 | if ($strFileType == ".css") |
---|
3156 | return "text/css"; |
---|
3157 | if ($strFileType == ".php") |
---|
3158 | return "text/php"; |
---|
3159 | if ($strFileType == ".asp") |
---|
3160 | return "text/asp"; |
---|
3161 | if ($strFileType == ".pdf") |
---|
3162 | return "application/pdf"; |
---|
3163 | if ($strFileType == ".txt") |
---|
3164 | return "text/plain"; |
---|
3165 | if ($strFileType == ".wmv") |
---|
3166 | return "video/x-ms-wmv"; |
---|
3167 | if ($strFileType == ".sxc") |
---|
3168 | return "application/vnd.sun.xml.calc"; |
---|
3169 | if ($strFileType == ".stc") |
---|
3170 | return "application/vnd.sun.xml.calc.template"; |
---|
3171 | if ($strFileType == ".sxd") |
---|
3172 | return "application/vnd.sun.xml.draw"; |
---|
3173 | if ($strFileType == ".std") |
---|
3174 | return "application/vnd.sun.xml.draw.template"; |
---|
3175 | if ($strFileType == ".sxi") |
---|
3176 | return "application/vnd.sun.xml.impress"; |
---|
3177 | if ($strFileType == ".sti") |
---|
3178 | return "application/vnd.sun.xml.impress.template"; |
---|
3179 | if ($strFileType == ".sxm") |
---|
3180 | return "application/vnd.sun.xml.math"; |
---|
3181 | if ($strFileType == ".sxw") |
---|
3182 | return "application/vnd.sun.xml.writer"; |
---|
3183 | if ($strFileType == ".sxq") |
---|
3184 | return "application/vnd.sun.xml.writer.global"; |
---|
3185 | if ($strFileType == ".stw") |
---|
3186 | return "application/vnd.sun.xml.writer.template"; |
---|
3187 | |
---|
3188 | |
---|
3189 | return "application/octet-stream"; |
---|
3190 | } |
---|
3191 | |
---|
3192 | function htmlspecialchars_encode($str) |
---|
3193 | { |
---|
3194 | return str_replace( array('&', '"','\'','<','>','{','}'), array('&','"',''','<','>','{','}'), $str); |
---|
3195 | } |
---|
3196 | function htmlspecialchars_decode($str) |
---|
3197 | { |
---|
3198 | return str_replace( array('&','"',''','<','>','{','}'), array('&', '"','\'','<','>','{','}'), $str); |
---|
3199 | } |
---|
3200 | |
---|
3201 | function get_msgs($folder, $sort_box_type, $search_box_type, $sort_box_reverse,$offsetBegin = 0,$offsetEnd = 0) |
---|
3202 | { |
---|
3203 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
3204 | $this->mbox = $this->open_mbox($folder); |
---|
3205 | |
---|
3206 | return $this->messages_sort($sort_box_type,$sort_box_reverse, $search_box_type,$offsetBegin,$offsetEnd); |
---|
3207 | } |
---|
3208 | |
---|
3209 | function get_info_next_msg($params) |
---|
3210 | { |
---|
3211 | $msg_number = $params['msg_number']; |
---|
3212 | $folder = $params['msg_folder']; |
---|
3213 | $sort_box_type = $params['sort_box_type']; |
---|
3214 | $sort_box_reverse = $params['sort_box_reverse']; |
---|
3215 | $reuse_border = $params['reuse_border']; |
---|
3216 | $search_box_type = $params['search_box_type'] != "ALL" && $params['search_box_type'] != "" ? $params['search_box_type'] : false; |
---|
3217 | $sort_array_msg = $this -> get_msgs($folder, $sort_box_type, $search_box_type, $sort_box_reverse); |
---|
3218 | |
---|
3219 | $success = false; |
---|
3220 | if (is_array($sort_array_msg)) |
---|
3221 | { |
---|
3222 | foreach ($sort_array_msg as $i => $value){ |
---|
3223 | if ($value == $msg_number) |
---|
3224 | { |
---|
3225 | $success = true; |
---|
3226 | break; |
---|
3227 | } |
---|
3228 | } |
---|
3229 | } |
---|
3230 | |
---|
3231 | if (! $success || $i >= sizeof($sort_array_msg)-1) |
---|
3232 | { |
---|
3233 | $params['status'] = 'false'; |
---|
3234 | $params['command_to_exec'] = "delete_border('". $reuse_border ."');"; |
---|
3235 | return $params; |
---|
3236 | } |
---|
3237 | |
---|
3238 | $params = array(); |
---|
3239 | $params['msg_number'] = $sort_array_msg[($i+1)]; |
---|
3240 | $params['msg_folder'] = $folder; |
---|
3241 | |
---|
3242 | $return = $this->get_info_msg($params); |
---|
3243 | $return["reuse_border"] = $reuse_border; |
---|
3244 | return $return; |
---|
3245 | } |
---|
3246 | |
---|
3247 | function get_info_previous_msg($params) |
---|
3248 | { |
---|
3249 | $msg_number = $params['msgs_number']; |
---|
3250 | $folder = $params['folder']; |
---|
3251 | $sort_box_type = $params['sort_box_type']; |
---|
3252 | $sort_box_reverse = $params['sort_box_reverse']; |
---|
3253 | $reuse_border = $params['reuse_border']; |
---|
3254 | $search_box_type = $params['search_box_type'] != "ALL" && $params['search_box_type'] != "" ? $params['search_box_type'] : false; |
---|
3255 | $sort_array_msg = $this -> get_msgs($folder, $sort_box_type, $search_box_type, $sort_box_reverse); |
---|
3256 | |
---|
3257 | $success = false; |
---|
3258 | if (is_array($sort_array_msg)) |
---|
3259 | { |
---|
3260 | foreach ($sort_array_msg as $i => $value){ |
---|
3261 | if ($value == $msg_number) |
---|
3262 | { |
---|
3263 | $success = true; |
---|
3264 | break; |
---|
3265 | } |
---|
3266 | } |
---|
3267 | } |
---|
3268 | if (! $success || $i == 0) |
---|
3269 | { |
---|
3270 | $params['status'] = 'false'; |
---|
3271 | $params['command_to_exec'] = "delete_border('". $reuse_border ."');"; |
---|
3272 | return $params; |
---|
3273 | } |
---|
3274 | |
---|
3275 | $params = array(); |
---|
3276 | $params['msg_number'] = $sort_array_msg[($i-1)]; |
---|
3277 | $params['msg_folder'] = $folder; |
---|
3278 | |
---|
3279 | $return = $this->get_info_msg($params); |
---|
3280 | $return["reuse_border"] = $reuse_border; |
---|
3281 | return $return; |
---|
3282 | } |
---|
3283 | |
---|
3284 | // This function updates the values: quota, paging and new messages menu. |
---|
3285 | function get_menu_values($params){ |
---|
3286 | $return_array = array(); |
---|
3287 | $return_array = $this->get_quota($params); |
---|
3288 | |
---|
3289 | $mbox_stream = $this->open_mbox($params['folder']); |
---|
3290 | $return_array['num_msgs'] = imap_num_msg($mbox_stream); |
---|
3291 | if($mbox_stream) |
---|
3292 | imap_close($mbox_stream); |
---|
3293 | |
---|
3294 | return $return_array; |
---|
3295 | } |
---|
3296 | |
---|
3297 | function get_quota($params){ |
---|
3298 | // folder_id = user/{uid} for shared folders |
---|
3299 | if(substr($params['folder_id'],0,5) != 'INBOX' && preg_match('/user\\'.$this->imap_delimiter.'/i', $params['folder_id'])){ |
---|
3300 | $array_folder = explode($this->imap_delimiter,$params['folder_id']); |
---|
3301 | $folder_id = "user".$this->imap_delimiter.$array_folder[1]; |
---|
3302 | } |
---|
3303 | // folder_id = INBOX for inbox folders |
---|
3304 | else |
---|
3305 | $folder_id = "INBOX"; |
---|
3306 | |
---|
3307 | if(!$this->mbox || !is_resource($this->mbox)) |
---|
3308 | $this->mbox = $this->open_mbox(); |
---|
3309 | |
---|
3310 | $quota = imap_get_quotaroot($this->mbox, $folder_id); |
---|
3311 | if($this->mbox && is_resource($this->mbox)) |
---|
3312 | imap_close($this->mbox); |
---|
3313 | |
---|
3314 | if (!$quota){ |
---|
3315 | return array( |
---|
3316 | 'quota_percent' => 0, |
---|
3317 | 'quota_used' => 0, |
---|
3318 | 'quota_limit' => 0 |
---|
3319 | ); |
---|
3320 | } |
---|
3321 | |
---|
3322 | if(count($quota) && $quota['limit']) { |
---|
3323 | $quota_limit = $quota['limit']; |
---|
3324 | $quota_used = $quota['usage']; |
---|
3325 | if($quota_used >= $quota_limit) |
---|
3326 | { |
---|
3327 | $quotaPercent = 100; |
---|
3328 | } |
---|
3329 | else |
---|
3330 | { |
---|
3331 | $quotaPercent = ($quota_used / $quota_limit)*100; |
---|
3332 | $quotaPercent = (($quotaPercent)* 100 + .5 )* .01; |
---|
3333 | } |
---|
3334 | return array( |
---|
3335 | 'quota_percent' => floor($quotaPercent), |
---|
3336 | 'quota_used' => $quota_used, |
---|
3337 | 'quota_limit' => $quota_limit |
---|
3338 | ); |
---|
3339 | } |
---|
3340 | else |
---|
3341 | return array(); |
---|
3342 | } |
---|
3343 | |
---|
3344 | function send_notification($params){ |
---|
3345 | include("../header.inc.php"); |
---|
3346 | require_once("class.phpmailer.php"); |
---|
3347 | $mail = new PHPMailer(); |
---|
3348 | |
---|
3349 | $toaddress = $params['notificationto']; |
---|
3350 | |
---|
3351 | $subject = lang("Read receipt: %1",$params['subject']); |
---|
3352 | $body = lang("Your message: %1",$params['subject']) . '<br>'; |
---|
3353 | $body .= lang("Received in: %1",date("d/m/Y H:i",$params['date'])) . '<br>'; |
---|
3354 | $body .= lang("Has been read by: %1 < %2 > at %3", $_SESSION['phpgw_info']['expressomail']['user']['fullname'], $_SESSION['phpgw_info']['expressomail']['user']['email'], date("d/m/Y H:i")); |
---|
3355 | $mail->SMTPDebug = false; |
---|
3356 | $mail->IsSMTP(); |
---|
3357 | $mail->Host = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpServer']; |
---|
3358 | $mail->Port = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpPort']; |
---|
3359 | $mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
3360 | $mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname']; |
---|
3361 | $mail->AddAddress($toaddress); |
---|
3362 | $mail->Subject = $this->htmlspecialchars_decode($subject); |
---|
3363 | |
---|
3364 | $mail->IsHTML(true); |
---|
3365 | $mail->Body = $body; |
---|
3366 | |
---|
3367 | if(!$mail->Send()){ |
---|
3368 | return $mail->ErrorInfo; |
---|
3369 | } |
---|
3370 | else |
---|
3371 | return true; |
---|
3372 | } |
---|
3373 | |
---|
3374 | function empty_folder($params) |
---|
3375 | { |
---|
3376 | $folder = 'INBOX' . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server'][$params['clean_folder']]; |
---|
3377 | $mbox_stream = $this->open_mbox($folder); |
---|
3378 | $return = imap_delete($mbox_stream,'1:*'); |
---|
3379 | if($mbox_stream) |
---|
3380 | imap_close($mbox_stream, CL_EXPUNGE); |
---|
3381 | return $return; |
---|
3382 | } |
---|
3383 | |
---|
3384 | function search($params) |
---|
3385 | { |
---|
3386 | include("class.imap_attachment.inc.php"); |
---|
3387 | $imap_attachment = new imap_attachment(); |
---|
3388 | $criteria = $params['criteria']; |
---|
3389 | $return = array(); |
---|
3390 | $folders = $this->get_folders_list(); |
---|
3391 | |
---|
3392 | $j = 0; |
---|
3393 | foreach($folders as $folder) |
---|
3394 | { |
---|
3395 | $mbox_stream = $this->open_mbox($folder); |
---|
3396 | $messages = imap_search($mbox_stream, $criteria, SE_UID); |
---|
3397 | |
---|
3398 | if ($messages == '') |
---|
3399 | continue; |
---|
3400 | |
---|
3401 | $i = 0; |
---|
3402 | $return[$j] = array(); |
---|
3403 | $return[$j]['folder_name'] = $folder['name']; |
---|
3404 | |
---|
3405 | foreach($messages as $msg_number) |
---|
3406 | { |
---|
3407 | $header = $this->get_header($msg_number); |
---|
3408 | if (!is_object($header)) |
---|
3409 | return false; |
---|
3410 | |
---|
3411 | $return[$j][$i]['msg_folder'] = $folder['name']; |
---|
3412 | $return[$j][$i]['msg_number'] = $msg_number; |
---|
3413 | $return[$j][$i]['Recent'] = $header->Recent; |
---|
3414 | $return[$j][$i]['Unseen'] = $header->Unseen; |
---|
3415 | $return[$j][$i]['Answered'] = $header->Answered; |
---|
3416 | $return[$j][$i]['Deleted'] = $header->Deleted; |
---|
3417 | $return[$j][$i]['Draft'] = $header->Draft; |
---|
3418 | $return[$j][$i]['Flagged'] = $header->Flagged; |
---|
3419 | |
---|
3420 | $date_msg = gmdate("d/m/Y",$header->udate); |
---|
3421 | if (gmdate("d/m/Y") == $date_msg) |
---|
3422 | $return[$j][$i]['udate'] = gmdate("H:i",$header->udate); |
---|
3423 | else |
---|
3424 | $return[$j][$i]['udate'] = $date_msg; |
---|
3425 | |
---|
3426 | $fromaddress = imap_mime_header_decode($header->fromaddress); |
---|
3427 | $return[$j][$i]['fromaddress'] = ''; |
---|
3428 | foreach ($fromaddress as $tmp) |
---|
3429 | $return[$j][$i]['fromaddress'] .= $this->replace_maior_menor($tmp->text); |
---|
3430 | |
---|
3431 | $from = $header->from; |
---|
3432 | $return[$j][$i]['from'] = array(); |
---|
3433 | $tmp = imap_mime_header_decode($from[0]->personal); |
---|
3434 | $return[$j][$i]['from']['name'] = $tmp[0]->text; |
---|
3435 | $return[$j][$i]['from']['email'] = $from[0]->mailbox . "@" . $from[0]->host; |
---|
3436 | $return[$j][$i]['from']['full'] ='"' . $return[$j][$i]['from']['name'] . '" ' . '<' . $return[$j][$i]['from']['email'] . '>'; |
---|
3437 | |
---|
3438 | $to = $header->to; |
---|
3439 | $return[$j][$i]['to'] = array(); |
---|
3440 | $tmp = imap_mime_header_decode($to[0]->personal); |
---|
3441 | $return[$j][$i]['to']['name'] = $tmp[0]->text; |
---|
3442 | $return[$j][$i]['to']['email'] = $to[0]->mailbox . "@" . $to[0]->host; |
---|
3443 | $return[$j][$i]['to']['full'] ='"' . $return[$i]['to']['name'] . '" ' . '<' . $return[$i]['to']['email'] . '>'; |
---|
3444 | |
---|
3445 | $subject = imap_mime_header_decode($header->fetchsubject); |
---|
3446 | $return[$j][$i]['subject'] = ''; |
---|
3447 | foreach ($subject as $tmp) |
---|
3448 | $return[$j][$i]['subject'] .= $tmp->text; |
---|
3449 | |
---|
3450 | $return[$j][$i]['Size'] = $header->Size; |
---|
3451 | $return[$j][$i]['reply_toaddress'] = $header->reply_toaddress; |
---|
3452 | |
---|
3453 | $return[$j][$i]['attachment'] = array(); |
---|
3454 | $return[$j][$i]['attachment'] = $imap_attachment->get_attachment_headerinfo($mbox_stream, $msg_number); |
---|
3455 | |
---|
3456 | $i++; |
---|
3457 | } |
---|
3458 | $j++; |
---|
3459 | if($mbox_stream) |
---|
3460 | imap_close($mbox_stream); |
---|
3461 | } |
---|
3462 | |
---|
3463 | return $return; |
---|
3464 | } |
---|
3465 | |
---|
3466 | |
---|
3467 | function mobile_search($params) |
---|
3468 | { |
---|
3469 | include("class.imap_attachment.inc.php"); |
---|
3470 | $imap_attachment = new imap_attachment(); |
---|
3471 | $criterias = array ("TO","SUBJECT","FROM","CC"); |
---|
3472 | $return = array(); |
---|
3473 | if(!isset($params['folder'])) { |
---|
3474 | $folder_params = array("noSharedFolders"=>1); |
---|
3475 | if(isset($params['folderType'])) |
---|
3476 | $folder_params['folderType'] = $params['folderType']; |
---|
3477 | $folders = $this->get_folders_list($folder_params); |
---|
3478 | } |
---|
3479 | else |
---|
3480 | $folders = array(0=>array('folder_id'=>$params['folder'])); |
---|
3481 | $num_msgs = 0; |
---|
3482 | $max_msgs = $params['max_msgs'] + 1; //get one more because mobile paginate |
---|
3483 | $return["msgs"] = array(); |
---|
3484 | |
---|
3485 | //get max_msgs of each folder order by date and later order all messages together and retur only max_msgs msgs |
---|
3486 | foreach($folders as $id =>$folder) |
---|
3487 | { |
---|
3488 | if(strpos($folder['folder_id'],'user')===false && is_array($folder)) { |
---|
3489 | foreach($criterias as $criteria_fixed) |
---|
3490 | { |
---|
3491 | $_filter = $criteria_fixed . ' "'.$params['filter'].'"'; |
---|
3492 | |
---|
3493 | $mbox_stream = $this->open_mbox($folder['folder_id']); |
---|
3494 | |
---|
3495 | $messages = imap_sort($mbox_stream,SORTARRIVAL,1,SE_UID,$_filter); |
---|
3496 | |
---|
3497 | if ($messages == ''){ |
---|
3498 | if($mbox_stream) |
---|
3499 | imap_close($mbox_stream); |
---|
3500 | continue; |
---|
3501 | } |
---|
3502 | |
---|
3503 | foreach($messages as $msg_number) |
---|
3504 | { |
---|
3505 | $temp = $this->get_info_head_msg($msg_number); |
---|
3506 | if(!$temp) |
---|
3507 | return false; |
---|
3508 | $temp['msg_folder'] = $folder['folder_id']; |
---|
3509 | $return["msgs"][$num_msgs] = $temp; |
---|
3510 | $num_msgs++; |
---|
3511 | } |
---|
3512 | |
---|
3513 | if($mbox_stream) |
---|
3514 | imap_close($mbox_stream); |
---|
3515 | } |
---|
3516 | } |
---|
3517 | } |
---|
3518 | |
---|
3519 | if(!function_exists("cmp_date")) { |
---|
3520 | function cmp_date($obj1, $obj2){ |
---|
3521 | if($obj1['timestamp'] == $obj2['timestamp']) return 0; |
---|
3522 | return ($obj1['timestamp'] < $obj2['timestamp']) ? 1 : -1; |
---|
3523 | } |
---|
3524 | } |
---|
3525 | usort($return["msgs"], "cmp_date"); |
---|
3526 | $return["has_more_msg"] = (sizeof($return["msgs"]) > $max_msgs); |
---|
3527 | $return["msgs"] = array_slice($return["msgs"], 0, $max_msgs); |
---|
3528 | $return["msgs"]['num_msgs'] = $num_msgs; |
---|
3529 | |
---|
3530 | return $return; |
---|
3531 | } |
---|
3532 | |
---|
3533 | function delete_and_show_previous_message($params) |
---|
3534 | { |
---|
3535 | $return = $this->get_info_previous_msg($params); |
---|
3536 | |
---|
3537 | $params_tmp1 = array(); |
---|
3538 | $params_tmp1['msgs_to_delete'] = $params['msg_number']; |
---|
3539 | $params_tmp1['folder'] = $params['msg_folder']; |
---|
3540 | $return_tmp1 = $this->delete_msg($params_tmp1); |
---|
3541 | |
---|
3542 | $return['msg_number_deleted'] = $return_tmp1; |
---|
3543 | |
---|
3544 | return $return; |
---|
3545 | } |
---|
3546 | |
---|
3547 | |
---|
3548 | function automatic_trash_cleanness($params) |
---|
3549 | { |
---|
3550 | $before_date = date("m/d/Y", strtotime("-".$params['before_date']." day")); |
---|
3551 | $criteria = 'BEFORE "'.$before_date.'"'; |
---|
3552 | $mbox_stream = $this->open_mbox('INBOX'.$this->imap_delimiter.$_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultTrashFolder']); |
---|
3553 | // Free others requests |
---|
3554 | session_write_close(); |
---|
3555 | $messages = imap_search($mbox_stream, $criteria, SE_UID); |
---|
3556 | if (is_array($messages)){ |
---|
3557 | foreach ($messages as $msg_number){ |
---|
3558 | imap_delete($mbox_stream, $msg_number, FT_UID); |
---|
3559 | } |
---|
3560 | } |
---|
3561 | if($mbox_stream) |
---|
3562 | imap_close($mbox_stream, CL_EXPUNGE); |
---|
3563 | return $messages; |
---|
3564 | } |
---|
3565 | // Fix the search problem with special characters!!!! |
---|
3566 | function remove_accents($string) { |
---|
3567 | return strtr($string, |
---|
3568 | "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ", |
---|
3569 | "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy"); |
---|
3570 | } |
---|
3571 | |
---|
3572 | function make_search_date($date,$before = false){ |
---|
3573 | |
---|
3574 | //TODO: Adaptar a data de acordo com o locale do sistema. |
---|
3575 | list($day,$month,$year) = explode("/", $date); |
---|
3576 | $before?$day=(int)$day+1:$day=(int)$day; |
---|
3577 | $timestamp = mktime(0,0,0,(int)$month,$day,(int)$year); |
---|
3578 | $search_date = date('d-M-Y',$timestamp); |
---|
3579 | return $search_date; |
---|
3580 | |
---|
3581 | } |
---|
3582 | |
---|
3583 | function search_msg( $params = false ) |
---|
3584 | { |
---|
3585 | $mbox_stream = ""; |
---|
3586 | |
---|
3587 | if(strpos($params['condition'],"#")===false) |
---|
3588 | { //local messages |
---|
3589 | $search=false; |
---|
3590 | } |
---|
3591 | else |
---|
3592 | { |
---|
3593 | $search = explode(",",$params['condition']); |
---|
3594 | } |
---|
3595 | |
---|
3596 | $params['page'] = $params['page'] * 1; |
---|
3597 | |
---|
3598 | if( is_array($search) ) |
---|
3599 | { |
---|
3600 | $search = array_unique($search); // Remove duplicated folders |
---|
3601 | $search_criteria = ''; |
---|
3602 | $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number']; |
---|
3603 | foreach($search as $tmp) |
---|
3604 | { |
---|
3605 | $tmp1 = explode("##",$tmp); |
---|
3606 | $sum = 0; |
---|
3607 | $name_box = $tmp1[0]; |
---|
3608 | unset($filter); |
---|
3609 | foreach($tmp1 as $index => $criteria) |
---|
3610 | { |
---|
3611 | if ($index != 0 && strlen($criteria) != 0) |
---|
3612 | { |
---|
3613 | $filter_array = explode("<=>",html_entity_decode(rawurldecode($criteria))); |
---|
3614 | $filter .= " ".$filter_array[0]; |
---|
3615 | if (strlen($filter_array[1]) != 0) |
---|
3616 | { |
---|
3617 | if ( trim($filter_array[0]) != 'BEFORE' && |
---|
3618 | trim($filter_array[0]) != 'SINCE' && |
---|
3619 | trim($filter_array[0]) != 'ON') |
---|
3620 | { |
---|
3621 | $filter .= '"'.$filter_array[1].'"'; |
---|
3622 | } |
---|
3623 | if(trim($filter_array[0]) == 'BEFORE' ) |
---|
3624 | { |
---|
3625 | $filter .= '"'.$this->make_search_date($filter_array[1],true).'"'; |
---|
3626 | }else{ |
---|
3627 | $filter .= '"'.$this->make_search_date($filter_array[1]).'"'; |
---|
3628 | } |
---|
3629 | } |
---|
3630 | } |
---|
3631 | } |
---|
3632 | |
---|
3633 | $name_box = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO_8859-1" ); |
---|
3634 | $filter = $this->remove_accents($filter); |
---|
3635 | |
---|
3636 | //Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name |
---|
3637 | if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn'] && substr($name_box,0,4) == 'user') |
---|
3638 | { |
---|
3639 | $folder_name = explode($this->imap_delimiter,$name_box); |
---|
3640 | $this->ldap = new ldap_functions(); |
---|
3641 | |
---|
3642 | if ($cn = $this->ldap->uid2cn($folder_name[1])) |
---|
3643 | { |
---|
3644 | $folder_name[1] = $cn; |
---|
3645 | } |
---|
3646 | $folder_name = implode($this->imap_delimiter,$folder_name); |
---|
3647 | } |
---|
3648 | else |
---|
3649 | $folder_name = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO_8859-1" ); |
---|
3650 | |
---|
3651 | if(!is_resource($mbox_stream)) |
---|
3652 | $mbox_stream = $this->open_mbox($name_box); |
---|
3653 | else |
---|
3654 | imap_reopen($mbox_stream, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$name_box); |
---|
3655 | |
---|
3656 | if (preg_match("/^.?\bALL\b/", $filter)) |
---|
3657 | { |
---|
3658 | // Quick Search, note: this ALL isn't the same ALL from imap_search |
---|
3659 | $all_criterias = array ("TO","SUBJECT","FROM","CC"); |
---|
3660 | |
---|
3661 | foreach($all_criterias as $criteria_fixed) |
---|
3662 | { |
---|
3663 | $_filter = $criteria_fixed . substr($filter,4); |
---|
3664 | |
---|
3665 | $search_criteria = imap_search($mbox_stream, $_filter, SE_UID); |
---|
3666 | |
---|
3667 | if(is_array($search_criteria)) |
---|
3668 | { |
---|
3669 | foreach($search_criteria as $new_search) |
---|
3670 | { |
---|
3671 | $elem = $this->get_msg_detail($new_search,$name_box,$mbox_stream); |
---|
3672 | $elem['boxname'] = mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ); |
---|
3673 | $elem['uid'] = $new_search; |
---|
3674 | /* compare dates in ordering */ |
---|
3675 | $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2); |
---|
3676 | $retorno[] = $elem; |
---|
3677 | } |
---|
3678 | } |
---|
3679 | } |
---|
3680 | } |
---|
3681 | else{ |
---|
3682 | $search_criteria = imap_search($mbox_stream, $filter, SE_UID); |
---|
3683 | if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) |
---|
3684 | { |
---|
3685 | if((!strpos($filter,"FLAGGED") === false) || (!strpos($filter,"UNFLAGGED") === false)) |
---|
3686 | { |
---|
3687 | $num_msgs = imap_num_msg($mbox_stream); |
---|
3688 | $flagged_msgs = array(); |
---|
3689 | for ($i=$num_msgs; $i>0; $i--) |
---|
3690 | { |
---|
3691 | $iuid = @imap_uid($this->mbox,$i); |
---|
3692 | $header = $this->get_header($iuid); |
---|
3693 | if(trim($header->Flagged)) |
---|
3694 | { |
---|
3695 | $flagged_msgs[$i] = $iuid; |
---|
3696 | } |
---|
3697 | } |
---|
3698 | if((count($flagged_msgs) >0) && (strpos($filter,"UNFLAGGED") === false)) |
---|
3699 | { |
---|
3700 | $arry_diff = is_array($search_criteria) ? array_diff($flagged_msgs,$search_criteria):$flagged_msgs; |
---|
3701 | foreach($arry_diff as $msg) |
---|
3702 | { |
---|
3703 | $search_criteria[] = $msg; |
---|
3704 | } |
---|
3705 | } |
---|
3706 | else if((count($flagged_msgs) >0) && (is_array($search_criteria)) && (!strpos($filter,"UNFLAGGED") === false)) |
---|
3707 | { |
---|
3708 | $search_criteria = array_diff($search_criteria,$flagged_msgs); |
---|
3709 | } |
---|
3710 | } |
---|
3711 | } |
---|
3712 | |
---|
3713 | if( is_array( $search_criteria) ) |
---|
3714 | { |
---|
3715 | foreach($search_criteria as $new_search) |
---|
3716 | { |
---|
3717 | $elem = $this->get_msg_detail($new_search,$name_box,$mbox_stream); |
---|
3718 | $elem['boxname'] = mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ); |
---|
3719 | $elem['uid'] = $new_search; |
---|
3720 | /* compare dates in ordering */ |
---|
3721 | $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2); |
---|
3722 | $retorno[] = $elem; |
---|
3723 | } |
---|
3724 | } |
---|
3725 | } |
---|
3726 | } |
---|
3727 | } |
---|
3728 | |
---|
3729 | if($mbox_stream) |
---|
3730 | { |
---|
3731 | imap_close($mbox_stream); |
---|
3732 | } |
---|
3733 | |
---|
3734 | $num_msgs = count($retorno); |
---|
3735 | |
---|
3736 | /* Comparison functions, descendent is ascendent with parms inverted */ |
---|
3737 | function SORTDATE($a, $b){ return ($a['udatecomp'] < $b['udatecomp']); } |
---|
3738 | function SORTDATE_REVERSE($b, $a) { return SORTDATE($a,$b); } |
---|
3739 | |
---|
3740 | function SORTWHO($a, $b) { return (strtoupper($a['from']) > strtoupper($b['from'])); } |
---|
3741 | function SORTWHO_REVERSE($b, $a) { return SORTWHO($a,$b); } |
---|
3742 | |
---|
3743 | function SORTSUBJECT($a, $b) { return (strtoupper($a['subject']) > strtoupper($b['subject'])); } |
---|
3744 | function SORTSUBJECT_REVERSE($b, $a) { return SORTSUBJECT($a,$b); } |
---|
3745 | |
---|
3746 | function SORTBOX($a, $b) { return ($a['boxname'] > $b['boxname']); } |
---|
3747 | function SORTBOX_REVERSE($b, $a) { return SORTBOX($a,$b); } |
---|
3748 | |
---|
3749 | function SORTSIZE($a, $b) { return ($a['size'] > $b['size']); } |
---|
3750 | function SORTSIZE_REVERSE($b, $a) { return SORTSIZE($a,$b); } |
---|
3751 | |
---|
3752 | usort( $retorno, $params['sort_type']); |
---|
3753 | $pageret = array_slice( $retorno, $params['page'] * $this->prefs['max_email_per_page'], $this->prefs['max_email_per_page']); |
---|
3754 | |
---|
3755 | $arrayRetorno['num_msgs'] = $num_msgs; |
---|
3756 | $arrayRetorno['data'] = $pageret; |
---|
3757 | $arrayRetorno['currentTab'] = $params['current_tab']; |
---|
3758 | |
---|
3759 | if ($pageret) |
---|
3760 | { |
---|
3761 | return $arrayRetorno; |
---|
3762 | } |
---|
3763 | else |
---|
3764 | { |
---|
3765 | return 'none'; |
---|
3766 | } |
---|
3767 | } |
---|
3768 | |
---|
3769 | function get_msg_detail($uid_msg,$name_box, $mbox_stream ) |
---|
3770 | { |
---|
3771 | $header = $this->get_header($uid_msg); |
---|
3772 | require_once("class.imap_attachment.inc.php"); |
---|
3773 | $imap_attachment = new imap_attachment(); |
---|
3774 | $attachments = $imap_attachment->get_attachment_headerinfo($mbox_stream, $uid_msg); |
---|
3775 | $attachments = $attachments['number_attachments'] > 0?"T".$attachments['number_attachments']:""; |
---|
3776 | $flag = $header->Unseen |
---|
3777 | .$header->Recent |
---|
3778 | .$header->Flagged |
---|
3779 | .$header->Draft |
---|
3780 | .$header->Answered |
---|
3781 | .$header->Deleted |
---|
3782 | .$attachments; |
---|
3783 | |
---|
3784 | |
---|
3785 | $subject = $this->decode_string($header->fetchsubject); |
---|
3786 | $from = $header->from[0]->mailbox; |
---|
3787 | if($header->from[0]->personal != "") |
---|
3788 | $from = $header->from[0]->personal; |
---|
3789 | $ret_msg['from'] = $this->decode_string($from); |
---|
3790 | $ret_msg['subject'] = $subject; |
---|
3791 | $ret_msg['udate'] = gmdate("d/m/Y",$header->udate + $this->functions->CalculateDateOffset()); |
---|
3792 | $ret_msg['size'] = $header->Size; |
---|
3793 | $ret_msg['flag'] = $flag; |
---|
3794 | return $ret_msg; |
---|
3795 | } |
---|
3796 | |
---|
3797 | |
---|
3798 | function size_msg($size){ |
---|
3799 | $var = floor($size/1024); |
---|
3800 | if($var >= 1){ |
---|
3801 | return $var." kb"; |
---|
3802 | }else{ |
---|
3803 | return $size ." b"; |
---|
3804 | } |
---|
3805 | } |
---|
3806 | |
---|
3807 | function ob_array($the_object) |
---|
3808 | { |
---|
3809 | $the_array=array(); |
---|
3810 | if(!is_scalar($the_object)) |
---|
3811 | { |
---|
3812 | foreach($the_object as $id => $object) |
---|
3813 | { |
---|
3814 | if(is_scalar($object)) |
---|
3815 | { |
---|
3816 | $the_array[$id]=$object; |
---|
3817 | } |
---|
3818 | else |
---|
3819 | { |
---|
3820 | $the_array[$id]=$this->ob_array($object); |
---|
3821 | } |
---|
3822 | } |
---|
3823 | return $the_array; |
---|
3824 | } |
---|
3825 | else |
---|
3826 | { |
---|
3827 | return $the_object; |
---|
3828 | } |
---|
3829 | } |
---|
3830 | |
---|
3831 | function getacl() |
---|
3832 | { |
---|
3833 | $this->ldap = new ldap_functions(); |
---|
3834 | |
---|
3835 | $return = array(); |
---|
3836 | $mbox_stream = $this->open_mbox(); |
---|
3837 | $mbox_acl = imap_getacl($mbox_stream, 'INBOX'); |
---|
3838 | |
---|
3839 | $i = 0; |
---|
3840 | foreach ($mbox_acl as $user => $acl) |
---|
3841 | { |
---|
3842 | if ($user != $this->username) |
---|
3843 | { |
---|
3844 | $return[$i]['uid'] = $user; |
---|
3845 | $return[$i]['cn'] = $this->ldap->uid2cn($user); |
---|
3846 | } |
---|
3847 | $i++; |
---|
3848 | } |
---|
3849 | return $return; |
---|
3850 | } |
---|
3851 | |
---|
3852 | function setacl($params) |
---|
3853 | { |
---|
3854 | $old_users = $this->getacl(); |
---|
3855 | if (!count($old_users)) |
---|
3856 | $old_users = array(); |
---|
3857 | |
---|
3858 | $tmp_array = array(); |
---|
3859 | foreach ($old_users as $index => $user_info) |
---|
3860 | { |
---|
3861 | $tmp_array[$index] = $user_info['uid']; |
---|
3862 | } |
---|
3863 | $old_users = $tmp_array; |
---|
3864 | |
---|
3865 | $users = unserialize($params['users']); |
---|
3866 | if (!count($users)) |
---|
3867 | $users = array(); |
---|
3868 | |
---|
3869 | //$add_share = array_diff($users, $old_users); |
---|
3870 | $remove_share = array_diff($old_users, $users); |
---|
3871 | |
---|
3872 | $mbox_stream = $this->open_mbox(); |
---|
3873 | |
---|
3874 | $serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}"; |
---|
3875 | $mailboxes_list = imap_getmailboxes($mbox_stream, $serverString, "user".$this->imap_delimiter.$this->username."*"); |
---|
3876 | |
---|
3877 | /*if (count($add_share)) |
---|
3878 | { |
---|
3879 | foreach ($add_share as $index=>$uid) |
---|
3880 | { |
---|
3881 | if (is_array($mailboxes_list)) |
---|
3882 | { |
---|
3883 | foreach ($mailboxes_list as $key => $val) |
---|
3884 | { |
---|
3885 | $folder = str_replace($serverString, "", imap_utf7_decode($val->name)); |
---|
3886 | imap_setacl ($mbox_stream, $folder, "$uid", "lrswipcda"); |
---|
3887 | } |
---|
3888 | } |
---|
3889 | } |
---|
3890 | }*/ |
---|
3891 | |
---|
3892 | if (count($remove_share)) |
---|
3893 | { |
---|
3894 | foreach ($remove_share as $index=>$uid) |
---|
3895 | { |
---|
3896 | if (is_array($mailboxes_list)) |
---|
3897 | { |
---|
3898 | foreach ($mailboxes_list as $key => $val) |
---|
3899 | { |
---|
3900 | $folder = str_replace($serverString, "", imap_utf7_decode($val->name)); |
---|
3901 | $folder = str_replace("&-", "&", $folder); |
---|
3902 | imap_setacl ($mbox_stream, $folder, "$uid", ""); |
---|
3903 | } |
---|
3904 | } |
---|
3905 | } |
---|
3906 | } |
---|
3907 | |
---|
3908 | return true; |
---|
3909 | } |
---|
3910 | |
---|
3911 | function getaclfromuser($params) |
---|
3912 | { |
---|
3913 | $useracl = $params['user']; |
---|
3914 | |
---|
3915 | $return = array(); |
---|
3916 | $return[$useracl] = 'false'; |
---|
3917 | $mbox_stream = $this->open_mbox(); |
---|
3918 | $mbox_acl = imap_getacl($mbox_stream, 'INBOX'); |
---|
3919 | |
---|
3920 | foreach ($mbox_acl as $user => $acl) |
---|
3921 | { |
---|
3922 | if (($user != $this->username) && ($user == $useracl)) |
---|
3923 | { |
---|
3924 | $return[$user] = $acl; |
---|
3925 | } |
---|
3926 | } |
---|
3927 | return $return; |
---|
3928 | } |
---|
3929 | |
---|
3930 | function getacltouser($user) |
---|
3931 | { |
---|
3932 | $return = array(); |
---|
3933 | $mbox_stream = $this->open_mbox(); |
---|
3934 | //Alterado, antes era 'imap_getacl($mbox_stream, 'user'.$this->imap_delimiter.$user); |
---|
3935 | //Afim de tratar as pastas compartilhadas, verificandos as permissoes de operacao sobre as mesmas |
---|
3936 | //No caso de se tratar da caixa do proprio usuario logado, utiliza a sintaxe abaixo |
---|
3937 | if(substr($user,0,4) != 'user') |
---|
3938 | $mbox_acl = imap_getacl($mbox_stream, 'user'.$this->imap_delimiter.$user); |
---|
3939 | else |
---|
3940 | $mbox_acl = imap_getacl($mbox_stream, $user); |
---|
3941 | return $mbox_acl[$this->username]; |
---|
3942 | } |
---|
3943 | |
---|
3944 | |
---|
3945 | function setaclfromuser($params) |
---|
3946 | { |
---|
3947 | $user = $params['user']; |
---|
3948 | $acl = $params['acl']; |
---|
3949 | |
---|
3950 | $mbox_stream = $this->open_mbox(); |
---|
3951 | |
---|
3952 | $serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}"; |
---|
3953 | $mailboxes_list = imap_getmailboxes($mbox_stream, $serverString, "user".$this->imap_delimiter.$this->username."*"); |
---|
3954 | |
---|
3955 | if (is_array($mailboxes_list)) |
---|
3956 | { |
---|
3957 | foreach ($mailboxes_list as $key => $val) |
---|
3958 | { |
---|
3959 | $folder = str_replace($serverString, "", imap_utf7_encode($val->name)); |
---|
3960 | $folder = str_replace("&-", "&", $folder); |
---|
3961 | if (!imap_setacl ($mbox_stream, $folder, $user, $acl)) |
---|
3962 | { |
---|
3963 | $return = imap_last_error(); |
---|
3964 | } |
---|
3965 | } |
---|
3966 | } |
---|
3967 | if (isset($return)) |
---|
3968 | return $return; |
---|
3969 | else |
---|
3970 | return true; |
---|
3971 | } |
---|
3972 | |
---|
3973 | function download_attachment($msg,$msgno) |
---|
3974 | { |
---|
3975 | $array_parts_attachments = array(); |
---|
3976 | //$array_parts_attachments['names'] = ''; |
---|
3977 | include_once("class.imap_attachment.inc.php"); |
---|
3978 | $imap_attachment = new imap_attachment(); |
---|
3979 | |
---|
3980 | if (count($msg->fname[$msgno]) > 0) |
---|
3981 | { |
---|
3982 | $i = 0; |
---|
3983 | foreach ($msg->fname[$msgno] as $index=>$fname) |
---|
3984 | { |
---|
3985 | $array_parts_attachments[$i]['pid'] = $msg->pid[$msgno][$index]; |
---|
3986 | $array_parts_attachments[$i]['name'] = $imap_attachment->flat_mime_decode($this->decode_string($fname)); |
---|
3987 | $array_parts_attachments[$i]['name'] = $array_parts_attachments[$i]['name'] ? $array_parts_attachments[$i]['name'] : "attachment.bin"; |
---|
3988 | $array_parts_attachments[$i]['encoding'] = $msg->encoding[$msgno][$index]; |
---|
3989 | //$array_parts_attachments['names'] .= $array_parts_attachments[$i]['name'] . ', '; |
---|
3990 | $array_parts_attachments[$i]['fsize'] = $msg->fsize[$msgno][$index]; |
---|
3991 | $i++; |
---|
3992 | } |
---|
3993 | } |
---|
3994 | //$array_parts_attachments['names'] = substr($array_parts_attachments['names'],0,(strlen($array_parts_attachments['names']) - 2)); |
---|
3995 | return $array_parts_attachments; |
---|
3996 | } |
---|
3997 | |
---|
3998 | |
---|
3999 | /** |
---|
4000 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
4001 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
4002 | * @param $params |
---|
4003 | */ |
---|
4004 | function spam($params) |
---|
4005 | { |
---|
4006 | |
---|
4007 | $mbox_stream = $this->open_mbox($params['folder']); |
---|
4008 | $msgs_number = explode(',',$params['msgs_number']); |
---|
4009 | |
---|
4010 | $user = Array(); |
---|
4011 | |
---|
4012 | if(substr($params['folder'], 0, 4) == 'user') |
---|
4013 | { |
---|
4014 | $ldapObject = new ldap_functions(); |
---|
4015 | |
---|
4016 | $folderArray = Array(); |
---|
4017 | $folderArray = explode($this->imap_delimiter, $params['folder']); |
---|
4018 | |
---|
4019 | $user['name'] = $folderArray[1]; |
---|
4020 | $user['email'] = $ldapObject->getMailByUid($user['name']); |
---|
4021 | |
---|
4022 | } |
---|
4023 | else |
---|
4024 | { |
---|
4025 | $user['name'] = $this->username; |
---|
4026 | $user['email'] = $_SESSION['phpgw_info']['expressomail']['user']['email']; |
---|
4027 | } |
---|
4028 | |
---|
4029 | foreach($msgs_number as $msg_number) |
---|
4030 | { |
---|
4031 | $imap_msg_number = imap_msgno($mbox_stream, $msg_number); |
---|
4032 | $header = imap_fetchheader($mbox_stream, $imap_msg_number); |
---|
4033 | $body = imap_body($mbox_stream, $imap_msg_number); |
---|
4034 | $msg = $header . $body; |
---|
4035 | strtok($user['email'], '@'); |
---|
4036 | $domain = strtok('@'); |
---|
4037 | |
---|
4038 | |
---|
4039 | |
---|
4040 | //Encontrar a assinatura do dspam no cabecalho |
---|
4041 | $v = explode("\r\n", $header); |
---|
4042 | foreach ($v as $linha){ |
---|
4043 | if (eregi("^Message-ID", $linha)) { |
---|
4044 | $args = explode(" ", $linha); |
---|
4045 | $msg_id = "'$args[1]'"; |
---|
4046 | } elseif (eregi("^X-DSPAM-Signature", $linha)) { |
---|
4047 | $args = explode(" ",$linha); |
---|
4048 | $signature = $args[1]; |
---|
4049 | } |
---|
4050 | } |
---|
4051 | |
---|
4052 | // Seleciona qual comando a ser executado |
---|
4053 | switch($params['spam']){ |
---|
4054 | case 'true': $cmd = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_command_for_spam']; break; |
---|
4055 | case 'false': $cmd = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_command_for_ham']; break; |
---|
4056 | } |
---|
4057 | |
---|
4058 | |
---|
4059 | $tags = array('##EMAIL##', '##USERNAME##', '##DOMAIN##', '##SIGNATURE##', '##MSGID##'); |
---|
4060 | $cmd = str_replace($tags, array($user['email'], $user['name'], $domain, $signature, $msg_id), $cmd); |
---|
4061 | |
---|
4062 | system($cmd); |
---|
4063 | } |
---|
4064 | |
---|
4065 | imap_close($mbox_stream); |
---|
4066 | return false; |
---|
4067 | } |
---|
4068 | |
---|
4069 | |
---|
4070 | function get_header($msg_number) |
---|
4071 | { |
---|
4072 | $header = @imap_headerinfo($this->mbox, imap_msgno($this->mbox, $msg_number), 80, 255); |
---|
4073 | if (!is_object($header)) |
---|
4074 | return false; |
---|
4075 | |
---|
4076 | if($header->Flagged != "F" && $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) { |
---|
4077 | $flag = preg_match('/importance *: *(.*)\r/i', |
---|
4078 | imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number)) |
---|
4079 | ,$importance); |
---|
4080 | $header->Flagged = $flag==0?false:strtolower($importance[1])=="high"?"F":false; |
---|
4081 | } |
---|
4082 | |
---|
4083 | return $header; |
---|
4084 | } |
---|
4085 | |
---|
4086 | //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Insere emails no imap a partir do fonte do mesmo. Se o argumento timestamp for passado ele utiliza do script python |
---|
4087 | ///expressoMail1_2/imap.py para inserir uma msg com o horário correto pois isso não é porssível com a função imap_append do php. |
---|
4088 | |
---|
4089 | function insert_email($source,$folder,$timestamp,$flags){ |
---|
4090 | $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
4091 | $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
4092 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
4093 | $imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
4094 | $imap_options = '/notls/novalidate-cert'; |
---|
4095 | $mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$folder, $username, $password); |
---|
4096 | if(imap_last_error()) |
---|
4097 | { |
---|
4098 | imap_createmailbox($mbox_stream,imap_utf7_encode("{".$imap_server."}".$folder)); |
---|
4099 | } |
---|
4100 | if($timestamp){ |
---|
4101 | $pdate = date_parse(date('r')); // pega a data atual do servidor (TODO: pegar a data da mensagem local) |
---|
4102 | $timestamp += $pdate['zone']*(60); //converte a data da mensagem para o fuso horário GMT 0. Isto é feito devido ao Expresso Mail armazenar a data no fuso horário GMT 0 e para exibi-la converte ela para o fuso horário local. |
---|
4103 | /* TODO: o diretorio /tmp deve ser substituido pelo diretorio temporario configurado no setup */ |
---|
4104 | $file = "/tmp/sess_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]; |
---|
4105 | |
---|
4106 | $f = fopen($file,"w"); |
---|
4107 | fputs($f,base64_encode($source)); |
---|
4108 | fclose($f); |
---|
4109 | $command = "python ".$_SESSION['rootPatch']."/expressoMail1_2/imap.py ".escapeshellarg($imap_server)." ".escapeshellarg($imap_port)." ".escapeshellarg($username)." ".escapeshellarg($password)." ".escapeshellarg($timestamp)." ".escapeshellarg($folder)." ".escapeshellarg($file); |
---|
4110 | $return['command']=exec(escapeshellcmd($command)); |
---|
4111 | }else{ |
---|
4112 | $return['append'] = imap_append($mbox_stream, "{".$imap_server.":".$imap_port."}".$folder, $source, "\\Seen"); |
---|
4113 | } |
---|
4114 | $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT); |
---|
4115 | |
---|
4116 | $return['msg_no'] = $status->uidnext - 1; |
---|
4117 | $return['error'] = imap_last_error(); |
---|
4118 | if(!$return['error'] && $flags != '' ){ |
---|
4119 | |
---|
4120 | $flags_array=explode(':',$flags); |
---|
4121 | //"Answered","Draft","Flagged","Unseen" |
---|
4122 | $flags_fixed = ""; |
---|
4123 | if($flags_array[0] == 'A') |
---|
4124 | $flags_fixed.="\\Answered "; |
---|
4125 | if($flags_array[1] == 'X') |
---|
4126 | $flags_fixed.="\\Draft "; |
---|
4127 | if($flags_array[2] == 'F') |
---|
4128 | $flags_fixed.="\\Flagged "; |
---|
4129 | if($flags_array[3] != 'U') |
---|
4130 | $flags_fixed.="\\Seen "; |
---|
4131 | |
---|
4132 | imap_setflag_full($mbox_stream, $return['msg_no'], $flags_fixed, ST_UID); |
---|
4133 | } |
---|
4134 | if($mbox_stream) |
---|
4135 | imap_close($mbox_stream); |
---|
4136 | return $return; |
---|
4137 | } |
---|
4138 | |
---|
4139 | function show_decript($params){ |
---|
4140 | $source = $params['source']; |
---|
4141 | //error_log("source: $source\nversao: " . PHP_VERSION, 3, '/tmp/teste.log'); |
---|
4142 | $source = str_replace(" ", "+", $source,$i); |
---|
4143 | |
---|
4144 | if (version_compare(PHP_VERSION, '5.2.0', '>=')){ |
---|
4145 | if(!$source = base64_decode($source,true)) |
---|
4146 | return "error ".$source."Espaços ".$i; |
---|
4147 | |
---|
4148 | } |
---|
4149 | else { |
---|
4150 | if(!$source = base64_decode($source)) |
---|
4151 | return "error ".$source."Espaços ".$i; |
---|
4152 | } |
---|
4153 | |
---|
4154 | $insert = $this->insert_email($source,'INBOX'.$this->imap_delimiter.'decifradas'); |
---|
4155 | |
---|
4156 | $get['msg_number'] = $insert['msg_no']; |
---|
4157 | $get['msg_folder'] = 'INBOX'.$this->imap_delimiter.'decifradas'; |
---|
4158 | $return = $this->get_info_msg($get); |
---|
4159 | $get['msg_number'] = $params['ID']; |
---|
4160 | $get['msg_folder'] = $params['folder']; |
---|
4161 | $tmp = $this->get_info_msg($get); |
---|
4162 | if(!$tmp['status_get_msg_info']) |
---|
4163 | { |
---|
4164 | $return['msg_day']=$tmp['msg_day']; |
---|
4165 | $return['msg_hour']=$tmp['msg_hour']; |
---|
4166 | $return['fulldate']=$tmp['fulldate']; |
---|
4167 | $return['smalldate']=$tmp['smalldate']; |
---|
4168 | } |
---|
4169 | else |
---|
4170 | { |
---|
4171 | $return['msg_day']=''; |
---|
4172 | $return['msg_hour']=''; |
---|
4173 | $return['fulldate']=''; |
---|
4174 | $return['smalldate']=''; |
---|
4175 | } |
---|
4176 | $return['msg_no'] =$insert['msg_no']; |
---|
4177 | $return['error'] = $insert['error']; |
---|
4178 | $return['folder'] = $params['folder']; |
---|
4179 | //$return['acls'] = $insert['acls']; |
---|
4180 | $return['original_ID'] = $params['ID']; |
---|
4181 | |
---|
4182 | return $return; |
---|
4183 | |
---|
4184 | } |
---|
4185 | |
---|
4186 | //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Trata fontes de emails enviados via POST para o servidor por um xmlhttprequest, as partes codificados com |
---|
4187 | //Base64 os "+" são substituidos por " " no envio e essa função arruma esse efeito. |
---|
4188 | |
---|
4189 | function treat_base64_from_post($source){ |
---|
4190 | $offset = 0; |
---|
4191 | do |
---|
4192 | { |
---|
4193 | if($inicio = strpos($source, 'Content-Transfer-Encoding: base64', $offset)) |
---|
4194 | { |
---|
4195 | $inicio = strpos($source, "\n\r", $inicio); |
---|
4196 | $fim = strpos($source, '--', $inicio); |
---|
4197 | if(!$fim) |
---|
4198 | $fim = strpos($source,"\n\r", $inicio); |
---|
4199 | $length = $fim-$inicio; |
---|
4200 | $parte = substr( $source,$inicio,$length-1); |
---|
4201 | $parte = str_replace(" ", "+", $parte); |
---|
4202 | $source = substr_replace($source, $parte, $inicio, $length-1); |
---|
4203 | } |
---|
4204 | if($offset > $inicio) |
---|
4205 | $offset=FALSE; |
---|
4206 | else |
---|
4207 | $offset = $inicio; |
---|
4208 | } |
---|
4209 | while($offset); |
---|
4210 | return $source; |
---|
4211 | } |
---|
4212 | |
---|
4213 | //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Recebe os fontes dos emails a serem desarquivados, separa e envia cada um para função insert_mail. |
---|
4214 | |
---|
4215 | function unarchive_mail($params) |
---|
4216 | { |
---|
4217 | $dest_folder = $params['folder']; |
---|
4218 | $sources = explode("#@#@#@",$params['source']); |
---|
4219 | $timestamps = explode("#@#@#@",$params['timestamp']); |
---|
4220 | $flags = explode("#@#@#@",$params['flags']); |
---|
4221 | |
---|
4222 | foreach($sources as $index=>$src) |
---|
4223 | { |
---|
4224 | if($src!="") |
---|
4225 | { |
---|
4226 | $source = $this->treat_base64_from_post($src); |
---|
4227 | $insert = $this->insert_email($source,$dest_folder,$timestamps[$index],$flags[$index]); |
---|
4228 | } |
---|
4229 | } |
---|
4230 | |
---|
4231 | return $insert; |
---|
4232 | } |
---|
4233 | |
---|
4234 | function download_all_local_attachments($params) |
---|
4235 | { |
---|
4236 | $source = $params['source']; |
---|
4237 | $source = $this->treat_base64_from_post($source); |
---|
4238 | $insert = $this->insert_email($source,'INBOX'.$this->imap_delimiter.'decifradas'); |
---|
4239 | $exporteml = new ExportEml(); |
---|
4240 | $params['num_msg']=$insert['msg_no']; |
---|
4241 | $params['folder']='INBOX'.$this->imap_delimiter.'decifradas'; |
---|
4242 | return $exporteml->download_all_attachments($params); |
---|
4243 | } |
---|
4244 | function get_quota_folders(){ |
---|
4245 | |
---|
4246 | // Additional Imap Class for not-implemented functions into PHP-IMAP extension. |
---|
4247 | include_once("class.imapfp.inc.php"); |
---|
4248 | $imapfp = new imapfp(); |
---|
4249 | |
---|
4250 | if(!$imapfp->open($this->imap_server,$this->imap_port)) |
---|
4251 | return $imapfp->get_error(); |
---|
4252 | if (!$imapfp->login( $this->username,$this->password )) |
---|
4253 | return $imapfp->get_error(); |
---|
4254 | |
---|
4255 | $response_array = $imapfp->get_mailboxes_size(); |
---|
4256 | if ($imapfp->error) |
---|
4257 | return $imapfp->get_error(); |
---|
4258 | |
---|
4259 | $data = array(); |
---|
4260 | $quota_root = $this->get_quota(array('folder_id' => "INBOX")); |
---|
4261 | $data["quota_root"] = $quota_root; |
---|
4262 | |
---|
4263 | foreach ($response_array as $idx=>$line) { |
---|
4264 | $line2 = str_replace('"', "", $line); |
---|
4265 | $line2 = str_replace(" /vendor/cmu/cyrus-imapd/size (value.shared ",";",str_replace("* ANNOTATION ","",$line2)); |
---|
4266 | list($folder,$size) = explode(";",$line2); |
---|
4267 | $quota_used = str_replace(")","",$size); |
---|
4268 | $quotaPercent = (($quota_used / 1024) / $data["quota_root"]["quota_limit"])*100; |
---|
4269 | $folder = mb_convert_encoding($folder, "ISO_8859-1", "UTF7-IMAP"); |
---|
4270 | if(!preg_match('/user\\'.$this->imap_delimiter.$this->username.'\\'.$this->imap_delimiter.'/i',$folder)){ |
---|
4271 | $folder = $this->functions->getLang("Inbox"); |
---|
4272 | } |
---|
4273 | else |
---|
4274 | $folder = preg_replace('/user\\'.$this->imap_delimiter.$this->username.'\\'.$this->imap_delimiter.'/i','', $folder); |
---|
4275 | |
---|
4276 | $data[$folder] = array("quota_percent" => sprintf("%.1f",round($quotaPercent,1)), "quota_used" => $quota_used); |
---|
4277 | } |
---|
4278 | $imapfp->close(); |
---|
4279 | return $data; |
---|
4280 | } |
---|
4281 | } |
---|
4282 | ?> |
---|