1 | <?php |
---|
2 | /***************************************************************************************\ |
---|
3 | * Export EML Format Message Mail * |
---|
4 | * Written by Nilton Neto (Celepar) <niltonneto@celepar.pr.gov.br> * |
---|
5 | * ------------------------------------------------------------------------------------ * |
---|
6 | * This program is free software; you can redistribute it and/or modify it * |
---|
7 | * under the terms of the GNU General Public License as published by the * |
---|
8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
9 | * option) any later version. * |
---|
10 | \****************************************************************************************/ |
---|
11 | // BEGIN CLASS |
---|
12 | class ExportEml |
---|
13 | { |
---|
14 | var $msg; |
---|
15 | var $folder; |
---|
16 | var $mbox_stream; |
---|
17 | var $tempDir; |
---|
18 | |
---|
19 | function ExportEml() { |
---|
20 | |
---|
21 | //TODO: modificar o caminho hardcodificado '/tmp' para o definido na configuracao do expresso |
---|
22 | //$this->tempDir = $GLOBALS['phpgw_info']['server']['temp_dir']; |
---|
23 | $this->tempDir = '/tmp'; |
---|
24 | } |
---|
25 | |
---|
26 | function connectImap(){ |
---|
27 | |
---|
28 | $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
29 | $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
30 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
31 | $imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
32 | |
---|
33 | if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') |
---|
34 | { |
---|
35 | $imap_options = '/tls/novalidate-cert'; |
---|
36 | } |
---|
37 | else |
---|
38 | { |
---|
39 | $imap_options = '/notls/novalidate-cert'; |
---|
40 | } |
---|
41 | $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password); |
---|
42 | } |
---|
43 | |
---|
44 | //export message to EML Format |
---|
45 | function parseEml($header, $body) |
---|
46 | { |
---|
47 | $sEmailHeader = $header; |
---|
48 | $sEmailBody = $body; |
---|
49 | $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody; |
---|
50 | return $sEMail; |
---|
51 | } |
---|
52 | |
---|
53 | // create EML File. |
---|
54 | // Funcao alterada para tratar a exportacao |
---|
55 | // de mensagens arquivadas localmente. |
---|
56 | // Rommel Cysne (rommel.cysne@serpro.gov.br) |
---|
57 | // em 17/12/2008. |
---|
58 | |
---|
59 | function createFileEml_by_localFolder($sEMLData, $tempDir, $file){ |
---|
60 | |
---|
61 | $file = "{$file}.eml"; |
---|
62 | |
---|
63 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
64 | if(!$f) |
---|
65 | return False; |
---|
66 | |
---|
67 | fputs($f,$sEMLData); |
---|
68 | fclose($f); |
---|
69 | |
---|
70 | return $file; |
---|
71 | } |
---|
72 | |
---|
73 | function createFileEml($sEMLData, $tempDir, $id, $subject=false, $i=false) |
---|
74 | { |
---|
75 | if($id) |
---|
76 | { |
---|
77 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $id), 80, 255); |
---|
78 | $subject = $this->decode_subject($header->fetchsubject); |
---|
79 | |
---|
80 | if (strlen($subject) > 60) |
---|
81 | $subject = substr($subject, 0, 59); |
---|
82 | |
---|
83 | //$subject = preg_replace('/\//', '\'', $subject); |
---|
84 | $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; |
---|
85 | $to = "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; |
---|
86 | $subject = strtr($subject,$from,$to); |
---|
87 | |
---|
88 | $subject = preg_replace('/[^a-zA-Z0-9_]/i', '_', $subject); |
---|
89 | $file = $subject."_".$id.".eml"; |
---|
90 | } else{ |
---|
91 | // Se mensagem for arquivada localmente, $subject (assunto da mensagem) |
---|
92 | // sera passado para compor o nome do arquivo .eml; |
---|
93 | |
---|
94 | if($subject && $i){ |
---|
95 | $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; |
---|
96 | $to = "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; |
---|
97 | $subject = strtr($subject,$from,$to); |
---|
98 | |
---|
99 | $subject = preg_replace('/[^a-zA-Z0-9_]/i', '_', $subject); |
---|
100 | |
---|
101 | // é necessário que a sessão faça parte do nome do arquivo para que o mesmo não venha vazio o.O |
---|
102 | $file = $subject."_".$i."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; |
---|
103 | } else{ |
---|
104 | $file = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
109 | if(!$f) |
---|
110 | return False; |
---|
111 | |
---|
112 | fputs($f,$sEMLData); |
---|
113 | fclose($f); |
---|
114 | |
---|
115 | return $file; |
---|
116 | } |
---|
117 | |
---|
118 | function createFileZip($files, $tempDir){ |
---|
119 | |
---|
120 | $tmp_zip_filename = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".zip"; |
---|
121 | if (!empty($files)) |
---|
122 | { |
---|
123 | if (is_array($files)) |
---|
124 | { |
---|
125 | for ($i=0; $i < count($files); $i++) |
---|
126 | { |
---|
127 | $files[$i] = escapeshellarg($files[$i]); |
---|
128 | } |
---|
129 | $files = implode(' ', $files); |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | $files = escapeshellcmd($files); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | $command = "cd " . escapeshellarg($tempDir) . " && nice zip -m9 " . escapeshellarg($tmp_zip_filename) . " " . $files; |
---|
139 | |
---|
140 | |
---|
141 | if(!exec($command)) { |
---|
142 | $command = "cd " . escapeshellarg($tempDir) . " && rm ".$files." ". escapeshellarg($tmp_zip_filename); |
---|
143 | exec($command); |
---|
144 | return null; |
---|
145 | } |
---|
146 | |
---|
147 | return $tmp_zip_filename; |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | function export_all_byLocalFolder($params){ |
---|
152 | $error = false; |
---|
153 | |
---|
154 | if(empty($params['messages'])){ |
---|
155 | $file["empty_folder"] = true; |
---|
156 | } else { |
---|
157 | foreach($params['messages'] as $eachMsg){ |
---|
158 | $header = ""; |
---|
159 | $body = ""; |
---|
160 | $tempDir = $this->tempDir; |
---|
161 | |
---|
162 | $this->get_header_byLocalFolder($eachMsg, $header); |
---|
163 | $this->get_body_byLocalFolder($eachMsg, $body); |
---|
164 | $sEMLData = $this->parseEml($header, $body); |
---|
165 | $fileName = $this->createFileEml_by_localFolder($sEMLData, $tempDir,$eachMsg['msg_number']); |
---|
166 | |
---|
167 | if(!$fileName){ |
---|
168 | $error = true; |
---|
169 | break; |
---|
170 | } else |
---|
171 | $fileNames .= "\"".$fileName."\" "; |
---|
172 | } |
---|
173 | |
---|
174 | $nameFileZip = 'False'; |
---|
175 | if($fileNames && !$error) { |
---|
176 | $nameFileZip = $this->createFileZip($fileNames, $tempDir); |
---|
177 | ($nameFileZip) ? ($file = $tempDir.'/'.$nameFileZip) : ($file = false); |
---|
178 | } else |
---|
179 | $file = false; |
---|
180 | } |
---|
181 | |
---|
182 | return $file; |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | function get_header_byLocalFolder($eachMsg, &$header){ |
---|
187 | $header = <<<header |
---|
188 | MIME-Version: 1.0 |
---|
189 | Content-Type: text/html; charset=ISO-8859-1 |
---|
190 | Content-Transfer-Encoding: quoted-printable |
---|
191 | From: "{$eachMsg['from']['name']}" <{$eachMsg['from']['email']}> |
---|
192 | To: {$eachMsg['to']['email']} |
---|
193 | Subject: {$eachMsg['subject']} |
---|
194 | Message-Id: {$eachMsg['msg_number']} |
---|
195 | Date: {$eachMsg['date']} |
---|
196 | header; |
---|
197 | } |
---|
198 | |
---|
199 | function get_body_byLocalFolder($eachMsg, &$body){ |
---|
200 | $body = $eachMsg['msg_sample']['body']; |
---|
201 | } |
---|
202 | |
---|
203 | function export_all($params){ |
---|
204 | |
---|
205 | $this->folder = $params['folder']; |
---|
206 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
207 | $fileNames = ""; |
---|
208 | $tempDir = $this->tempDir; |
---|
209 | $this->connectImap(); |
---|
210 | |
---|
211 | $msgs = imap_search($this->mbox_stream,"ALL",SE_UID); |
---|
212 | |
---|
213 | if($msgs){ |
---|
214 | foreach($msgs as $nMsgs){ |
---|
215 | |
---|
216 | $header = $this-> getHeader($nMsgs); |
---|
217 | $body = $this-> getBody($nMsgs); |
---|
218 | |
---|
219 | $sEMLData = $this -> parseEml($header, $body); |
---|
220 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs); |
---|
221 | if(!$fileName) { |
---|
222 | $error = True; |
---|
223 | break; |
---|
224 | } |
---|
225 | else |
---|
226 | $fileNames .= "\"".$fileName."\" "; |
---|
227 | |
---|
228 | } |
---|
229 | |
---|
230 | imap_close($this->mbox_stream); |
---|
231 | |
---|
232 | $nameFileZip = 'False'; |
---|
233 | if($fileNames && !$error) { |
---|
234 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
235 | if($nameFileZip) |
---|
236 | $file = $tempDir.'/'.$nameFileZip; |
---|
237 | else { |
---|
238 | $file = false; |
---|
239 | } |
---|
240 | } |
---|
241 | else |
---|
242 | $file = false; |
---|
243 | }else{ |
---|
244 | $file["empty_folder"] = true; |
---|
245 | } |
---|
246 | return $file; |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | // Funcao alterada para tratar a exportacao |
---|
251 | // de mensagens arquivadas localmente. |
---|
252 | // Rommel Cysne (rommel.cysne@serpro.gov.br) |
---|
253 | // em 17/12/2008. |
---|
254 | // |
---|
255 | // Funcao alterada para que, quando houver |
---|
256 | // apenas um arquivo a ser exportado, |
---|
257 | // não seja criado em zip |
---|
258 | // |
---|
259 | // Funcao altarada para exportar uma ou |
---|
260 | // varia mensagens de um pesquisa |
---|
261 | |
---|
262 | function makeAll($params) { |
---|
263 | //Exporta menssagens selecionadas na pesquisa |
---|
264 | if($params['folder'] === 'false'){ |
---|
265 | |
---|
266 | $this->folder = $params['folder']; |
---|
267 | $error = False; |
---|
268 | $fileNames = ""; |
---|
269 | |
---|
270 | $sel_msgs = explode(",", $params['msgs_to_export']); |
---|
271 | @reset($sel_msgs); |
---|
272 | $sorted_msgs = array(); |
---|
273 | foreach($sel_msgs as $idx => $sel_msg) { |
---|
274 | $sel_msg = explode(";", $sel_msg); |
---|
275 | if(array_key_exists($sel_msg[0], $sorted_msgs)){ |
---|
276 | $sorted_msgs[$sel_msg[0]] .= ",".$sel_msg[1]; |
---|
277 | } |
---|
278 | else { |
---|
279 | $sorted_msgs[$sel_msg[0]] = $sel_msg[1]; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | unset($sorted_msgs['']); |
---|
284 | |
---|
285 | |
---|
286 | // Verifica se as n mensagens selecionadas |
---|
287 | // se encontram em um mesmo folder |
---|
288 | if (count($sorted_msgs)==1){ |
---|
289 | $array_names_keys = array_keys($sorted_msgs); |
---|
290 | $this->folder = mb_convert_encoding($array_names_keys[0], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
291 | $msg_number = explode(',', $sorted_msgs[$array_names_keys[0]]); |
---|
292 | $tempDir = $this->tempDir; |
---|
293 | $this->connectImap(); |
---|
294 | |
---|
295 | //verifica se apenas uma mensagem foi selecionada e exportar em .eml |
---|
296 | if(count($msg_number) == 1){ |
---|
297 | $header = $this->getHeader($msg_number[0]); |
---|
298 | $body = $this->getBody($msg_number[0]); |
---|
299 | $sEMLData = $this->parseEml($header, $body); |
---|
300 | $fileName = $this->CreateFileEml($sEMLData, $tempDir, $msg_number[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]); |
---|
301 | |
---|
302 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number[0]), 80, 255); |
---|
303 | $subject = $this->decode_subject(html_entity_decode($header->fetchsubject)); |
---|
304 | |
---|
305 | imap_close($this->mbox_stream); |
---|
306 | if (!$fileName) { |
---|
307 | return false; |
---|
308 | }else{ |
---|
309 | $return = array(); |
---|
310 | $return[] = $tempDir.'/'.$fileName; |
---|
311 | $return[] = $subject; |
---|
312 | return $return; |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | //cria um .zip com as mensagens selecionadas |
---|
317 | for($i = 0; $i < count($msg_number); $i++) |
---|
318 | { |
---|
319 | $header = $this-> getHeader($msg_number[$i]); |
---|
320 | $body = $this-> getBody($msg_number[$i]); |
---|
321 | $sEMLData = $this -> parseEml($header, $body); |
---|
322 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $msg_number[$i]); |
---|
323 | |
---|
324 | if(!$fileName) |
---|
325 | { |
---|
326 | $error = True; |
---|
327 | break; |
---|
328 | } else{ |
---|
329 | $fileNames .= "\"".$fileName."\" "; |
---|
330 | } |
---|
331 | } |
---|
332 | imap_close($this->mbox_stream); |
---|
333 | |
---|
334 | $nameFileZip = 'False'; |
---|
335 | if($fileNames && !$error) |
---|
336 | { |
---|
337 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
338 | if($nameFileZip) |
---|
339 | { |
---|
340 | $file = $tempDir.'/'.$nameFileZip; |
---|
341 | } else { |
---|
342 | $file = false; |
---|
343 | } |
---|
344 | } |
---|
345 | else |
---|
346 | { |
---|
347 | $file = false; |
---|
348 | } |
---|
349 | |
---|
350 | return $file; |
---|
351 | |
---|
352 | //exporta mensagens de diferentes pastas |
---|
353 | }else{ |
---|
354 | $array_names_keys = array_keys($sorted_msgs); |
---|
355 | |
---|
356 | for($i = 0; $i < count($array_names_keys); $i++){ |
---|
357 | $this->folder = mb_convert_encoding($array_names_keys[$i], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
358 | $msg_number = explode(',', $sorted_msgs[$array_names_keys[$i]]); |
---|
359 | $tempDir = $this->tempDir; |
---|
360 | $this->connectImap(); |
---|
361 | |
---|
362 | for($j = 0; $j < count($msg_number); $j++) |
---|
363 | { |
---|
364 | $header = $this-> getHeader($msg_number[$j]); |
---|
365 | $body = $this-> getBody($msg_number[$j]); |
---|
366 | $sEMLData = $this -> parseEml($header, $body); |
---|
367 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $msg_number[$j]); |
---|
368 | |
---|
369 | if(!$fileName) |
---|
370 | { |
---|
371 | $error = True; |
---|
372 | break; |
---|
373 | } else{ |
---|
374 | $fileNames .= "\"".$fileName."\" "; |
---|
375 | } |
---|
376 | } |
---|
377 | imap_close($this->mbox_stream); |
---|
378 | } |
---|
379 | $nameFileZip = 'False'; |
---|
380 | if($fileNames && !$error) |
---|
381 | { |
---|
382 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
383 | if($nameFileZip) |
---|
384 | { |
---|
385 | $file = $tempDir.'/'.$nameFileZip; |
---|
386 | } else { |
---|
387 | $file = false; |
---|
388 | } |
---|
389 | } |
---|
390 | else |
---|
391 | { |
---|
392 | $file = false; |
---|
393 | } |
---|
394 | return $file; |
---|
395 | } |
---|
396 | }else{ |
---|
397 | // Exportacao de mensagens arquivadas localmente |
---|
398 | if($params['l_msg'] == "t") |
---|
399 | { |
---|
400 | // Recebe todos os subjects e bodies das mensagens locais selecionadas para exportacao |
---|
401 | // e gera arrays com os conteudos separados; |
---|
402 | $array_mesgs = explode('@@',$params['mesgs']); |
---|
403 | $array_subjects = explode('@@',$params['subjects']); |
---|
404 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
405 | $tempDir = $this->tempDir; |
---|
406 | |
---|
407 | include_once("class.imap_functions.inc.php"); |
---|
408 | $imapf = new imap_functions(); |
---|
409 | |
---|
410 | // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip |
---|
411 | if (count($array_ids)==1) |
---|
412 | { |
---|
413 | $sEMLData=$imapf->treat_base64_from_post($array_mesgs[0]); |
---|
414 | $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[0],"offline"); |
---|
415 | return $tempDir.'/'.$fileName; |
---|
416 | } |
---|
417 | |
---|
418 | // Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma; |
---|
419 | foreach($array_ids as $i=>$id) { |
---|
420 | $sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]); |
---|
421 | $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[$i],$i); |
---|
422 | if(!$fileName){ |
---|
423 | $error = True; |
---|
424 | break; |
---|
425 | } else{ |
---|
426 | $fileNames .= "\"".$fileName."\" "; |
---|
427 | } |
---|
428 | } |
---|
429 | $nameFileZip = 'False'; |
---|
430 | if($fileNames && !$error) { |
---|
431 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
432 | if($nameFileZip){ |
---|
433 | $file = $tempDir.'/'.$nameFileZip; |
---|
434 | } else{ |
---|
435 | $file = false; |
---|
436 | } |
---|
437 | |
---|
438 | } else{ |
---|
439 | $file = false; |
---|
440 | } |
---|
441 | return $file; |
---|
442 | |
---|
443 | } else |
---|
444 | // Exportacao de mensagens da caixa de entrada (imap) - processo original do Expresso |
---|
445 | { |
---|
446 | $this-> folder = $params['folder']; |
---|
447 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
448 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
449 | $error = False; |
---|
450 | $fileNames = ""; |
---|
451 | $tempDir = $this->tempDir; |
---|
452 | $this->connectImap(); |
---|
453 | |
---|
454 | // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip |
---|
455 | if (count($array_ids)==1) |
---|
456 | { |
---|
457 | $header = $this->getHeader($array_ids[0]); |
---|
458 | $body = $this->getBody($array_ids[0]); |
---|
459 | $sEMLData = $this->parseEml($header, $body); |
---|
460 | $fileName = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]); |
---|
461 | |
---|
462 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $array_ids[0]), 80, 255); |
---|
463 | $subject = $this->decode_subject(html_entity_decode($header->fetchsubject)); |
---|
464 | |
---|
465 | imap_close($this->mbox_stream); |
---|
466 | if (!$fileName) { |
---|
467 | return false; |
---|
468 | } else { |
---|
469 | $return = array(); |
---|
470 | $return[] = $tempDir.'/'.$fileName; |
---|
471 | $return[] = $subject; |
---|
472 | return $return; |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | for($i = 0; $i < count($array_ids); $i++) |
---|
477 | { |
---|
478 | $header = $this-> getHeader($array_ids[$i]); |
---|
479 | $body = $this-> getBody($array_ids[$i]); |
---|
480 | $sEMLData = $this -> parseEml($header, $body); |
---|
481 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]); |
---|
482 | |
---|
483 | if(!$fileName) |
---|
484 | { |
---|
485 | $error = True; |
---|
486 | break; |
---|
487 | } else { |
---|
488 | $fileNames .= "\"".$fileName."\" "; |
---|
489 | } |
---|
490 | } |
---|
491 | imap_close($this->mbox_stream); |
---|
492 | |
---|
493 | $nameFileZip = 'False'; |
---|
494 | if($fileNames && !$error) |
---|
495 | { |
---|
496 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
497 | if($nameFileZip) |
---|
498 | { |
---|
499 | $file = $tempDir.'/'.$nameFileZip; |
---|
500 | $ret[] = $file; |
---|
501 | return $ret; |
---|
502 | } else { |
---|
503 | $file = false; |
---|
504 | } |
---|
505 | } |
---|
506 | else |
---|
507 | { |
---|
508 | $file = false; |
---|
509 | } |
---|
510 | return $file; |
---|
511 | } |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | function export_eml( $params ){ |
---|
516 | |
---|
517 | return $this->export_msg_data( $params['msgs_to_export'], |
---|
518 | $params['folder'] ); |
---|
519 | } |
---|
520 | |
---|
521 | function export_msg($params) { |
---|
522 | $this-> folder = $params['folder']; |
---|
523 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
524 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
525 | $error = False; |
---|
526 | $fileNames = ""; |
---|
527 | $tempDir = $this->tempDir; |
---|
528 | $this->connectImap(); |
---|
529 | |
---|
530 | // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip |
---|
531 | if (count($array_ids)==1) |
---|
532 | { |
---|
533 | $header = $this->getHeader($array_ids[0]); |
---|
534 | $body = $this->getBody($array_ids[0]); |
---|
535 | $sEMLData = $this->parseEml($header, $body); |
---|
536 | $fileName = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]); |
---|
537 | |
---|
538 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $array_ids[0]), 80, 255); |
---|
539 | $subject = $this->decode_subject(html_entity_decode($header->fetchsubject)); |
---|
540 | |
---|
541 | imap_close($this->mbox_stream); |
---|
542 | if (!$fileName) { |
---|
543 | return false; |
---|
544 | } else { |
---|
545 | $return = array(); |
---|
546 | $return[] = $tempDir.'/'.$fileName; |
---|
547 | $return[] = $subject; |
---|
548 | return $return; |
---|
549 | } |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | //MAILARCHIVER |
---|
554 | function js_source_var($params) { |
---|
555 | $this-> folder = $params['folder']; |
---|
556 | if(!$this->folder){ |
---|
557 | $aux = explode(';',$params['msgs_to_export']); |
---|
558 | $this->folder = $aux[0]; |
---|
559 | $id_number = $aux[1]; |
---|
560 | } |
---|
561 | else{ |
---|
562 | $id_number = $params['msgs_to_export']; |
---|
563 | } |
---|
564 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
565 | $tempDir = ini_get("session.save_path"); |
---|
566 | |
---|
567 | $this->connectImap(); |
---|
568 | $header = $this-> getHeader($id_number); |
---|
569 | $body = $this-> getBody($id_number); |
---|
570 | |
---|
571 | if(!strpos($header,"Date: ")){ |
---|
572 | $header = "Date: " . $this->getHeaderInfo($id_number)->Date . "\r\n" .$header ; |
---|
573 | } |
---|
574 | |
---|
575 | imap_close($this->mbox_stream); |
---|
576 | |
---|
577 | $input = $header . "\r\n\r\n" . $body; |
---|
578 | $input = preg_replace('/\x1d/', '', $input); //remove special char control detected (hex 1D) |
---|
579 | |
---|
580 | return($input); |
---|
581 | } |
---|
582 | |
---|
583 | function export_msg_data($id_msg,$folder) { |
---|
584 | $this->folder = $folder; |
---|
585 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
586 | |
---|
587 | $this->connectImap(); |
---|
588 | $header = $this-> getHeader($id_msg); |
---|
589 | $body = $this-> getBody($id_msg); |
---|
590 | |
---|
591 | $msg_data = $header ."\r\n\r\n". $body; |
---|
592 | |
---|
593 | imap_close($this->mbox_stream); |
---|
594 | return $msg_data; |
---|
595 | } |
---|
596 | |
---|
597 | function export_to_archive($id_msg,$folder) { |
---|
598 | $this->folder = $folder; |
---|
599 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
600 | $tempDir = $this->tempDir; |
---|
601 | |
---|
602 | $this->connectImap(); |
---|
603 | $header = $this-> getHeader($id_msg); |
---|
604 | $body = $this-> getBody($id_msg); |
---|
605 | |
---|
606 | $file = tempnam ($tempDir, 'source_#'.$id_msg); |
---|
607 | $file .= '.php'; |
---|
608 | $fileName = basename ($file); |
---|
609 | $f = fopen($file, "w"); |
---|
610 | fputs($f,$phpheader.$header ."\r\n\r\n". $body); |
---|
611 | fclose($f); |
---|
612 | $urlPath = 'tmpLclAtt/' . $fileName; |
---|
613 | |
---|
614 | imap_close($this->mbox_stream); |
---|
615 | return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt"; |
---|
616 | } |
---|
617 | |
---|
618 | function remove_accents($string) { |
---|
619 | /* |
---|
620 | $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "š", "&", "*", "(", ")", "-", "=", "+", "Ž", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç"); |
---|
621 | $array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C"); |
---|
622 | return str_replace( $array1, $array2, $string ); |
---|
623 | */ |
---|
624 | return strtr($string, |
---|
625 | "áàâãäéèêëíìîïóòôõöúùûüç?\"'!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ", |
---|
626 | "aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC"); |
---|
627 | } |
---|
628 | |
---|
629 | function get_attachments_headers( $folder, $id_number ){ |
---|
630 | |
---|
631 | $this->folder = mb_convert_encoding($folder, "UTF7-IMAP","UTF-8"); |
---|
632 | |
---|
633 | $return_attachments = array(); |
---|
634 | |
---|
635 | include_once("class.attachment.inc.php"); |
---|
636 | |
---|
637 | $imap_attachment = new attachment(); |
---|
638 | $imap_attachment->setStructureFromMail( $folder, $id_number ); |
---|
639 | $attachments = $imap_attachment->getAttachmentsInfo(); |
---|
640 | |
---|
641 | foreach($attachments as $i => $attachment){ |
---|
642 | |
---|
643 | $fileContent = $imap_attachment->getAttachment( $attachment['pid'] ); |
---|
644 | |
---|
645 | $headers = "<?php header('Content-Type: {$attachment['type']}'); |
---|
646 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
---|
647 | header('Pragma: public'); |
---|
648 | header('Expires: 0'); // set expiration time |
---|
649 | header('Content-Disposition: attachment; filename=\"{$attachment['name']}\"');\n |
---|
650 | echo '$fileContent';?>"; |
---|
651 | |
---|
652 | $return_attachments[ $attachment['name'] ] = array( "content" => $headers, "pid" => $attachment['pid'] ); |
---|
653 | } |
---|
654 | |
---|
655 | return( $return_attachments ); |
---|
656 | } |
---|
657 | |
---|
658 | function get_attachments_in_array($params) { |
---|
659 | $return_attachments = array(); |
---|
660 | |
---|
661 | $attachments = $this->get_attachments_headers( $params['folder'], $params['num_msg'] ); |
---|
662 | |
---|
663 | if( !empty( $attachments ) ) |
---|
664 | { |
---|
665 | foreach($attachments as $fileNameReal => $attachment){ |
---|
666 | |
---|
667 | array_push($return_attachments,array('name' => $fileNameReal, 'pid' =>$attachment['pid'], 'contentType' => $this->getFileType( $fileNameReal ) )); |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | return $return_attachments; |
---|
672 | |
---|
673 | } |
---|
674 | |
---|
675 | private function getFileType($nameFile) { |
---|
676 | $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4)); |
---|
677 | $ContentType = "application/octet-stream"; |
---|
678 | if ($strFileType == ".asf") |
---|
679 | $ContentType = "video/x-ms-asf"; |
---|
680 | if ($strFileType == ".avi") |
---|
681 | $ContentType = "video/avi"; |
---|
682 | if ($strFileType == ".doc") |
---|
683 | $ContentType = "application/msword"; |
---|
684 | if ($strFileType == ".zip") |
---|
685 | $ContentType = "application/zip"; |
---|
686 | if ($strFileType == ".xls") |
---|
687 | $ContentType = "application/vnd.ms-excel"; |
---|
688 | if ($strFileType == ".gif") |
---|
689 | $ContentType = "image/gif"; |
---|
690 | if ($strFileType == ".png") |
---|
691 | $ContentType = "image/png"; |
---|
692 | if ($strFileType == ".jpg" || $strFileType == "jpeg") |
---|
693 | $ContentType = "image/jpeg"; |
---|
694 | if ($strFileType == ".wav") |
---|
695 | $ContentType = "audio/wav"; |
---|
696 | if ($strFileType == ".mp3") |
---|
697 | $ContentType = "audio/mpeg3"; |
---|
698 | if ($strFileType == ".mpg" || $strFileType == "mpeg") |
---|
699 | $ContentType = "video/mpeg"; |
---|
700 | if ($strFileType == ".rtf") |
---|
701 | $ContentType = "application/rtf"; |
---|
702 | if ($strFileType == ".htm" || $strFileType == "html") |
---|
703 | $ContentType = "text/html"; |
---|
704 | if ($strFileType == ".xml") |
---|
705 | $ContentType = "text/xml"; |
---|
706 | if ($strFileType == ".xsl") |
---|
707 | $ContentType = "text/xsl"; |
---|
708 | if ($strFileType == ".css") |
---|
709 | $ContentType = "text/css"; |
---|
710 | if ($strFileType == ".php") |
---|
711 | $ContentType = "text/php"; |
---|
712 | if ($strFileType == ".asp") |
---|
713 | $ContentType = "text/asp"; |
---|
714 | if ($strFileType == ".pdf") |
---|
715 | $ContentType = "application/pdf"; |
---|
716 | if ($strFileType == ".txt") |
---|
717 | $ContentType = "text/plain"; |
---|
718 | if ($strFileType == ".log") |
---|
719 | $ContentType = "text/plain"; |
---|
720 | if ($strFileType == ".wmv") |
---|
721 | $ContentType = "video/x-ms-wmv"; |
---|
722 | if ($strFileType == ".sxc") |
---|
723 | $ContentType = "application/vnd.sun.xml.calc"; |
---|
724 | if ($strFileType == ".odt") |
---|
725 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
726 | if ($strFileType == ".stc") |
---|
727 | $ContentType = "application/vnd.sun.xml.calc.template"; |
---|
728 | if ($strFileType == ".sxd") |
---|
729 | $ContentType = "application/vnd.sun.xml.draw"; |
---|
730 | if ($strFileType == ".std") |
---|
731 | $ContentType = "application/vnd.sun.xml.draw.template"; |
---|
732 | if ($strFileType == ".sxi") |
---|
733 | $ContentType = "application/vnd.sun.xml.impress"; |
---|
734 | if ($strFileType == ".sti") |
---|
735 | $ContentType = "application/vnd.sun.xml.impress.template"; |
---|
736 | if ($strFileType == ".sxm") |
---|
737 | $ContentType = "application/vnd.sun.xml.math"; |
---|
738 | if ($strFileType == ".sxw") |
---|
739 | $ContentType = "application/vnd.sun.xml.writer"; |
---|
740 | if ($strFileType == ".sxq") |
---|
741 | $ContentType = "application/vnd.sun.xml.writer.global"; |
---|
742 | if ($strFileType == ".stw") |
---|
743 | $ContentType = "application/vnd.sun.xml.writer.template"; |
---|
744 | if ($strFileType == ".ps") |
---|
745 | $ContentType = "application/postscript"; |
---|
746 | if ($strFileType == ".pps") |
---|
747 | $ContentType = "application/vnd.ms-powerpoint"; |
---|
748 | if ($strFileType == ".odt") |
---|
749 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
750 | if ($strFileType == ".ott") |
---|
751 | $ContentType = "application/vnd.oasis.opendocument.text-template"; |
---|
752 | if ($strFileType == ".oth") |
---|
753 | $ContentType = "application/vnd.oasis.opendocument.text-web"; |
---|
754 | if ($strFileType == ".odm") |
---|
755 | $ContentType = "application/vnd.oasis.opendocument.text-master"; |
---|
756 | if ($strFileType == ".odg") |
---|
757 | $ContentType = "application/vnd.oasis.opendocument.graphics"; |
---|
758 | if ($strFileType == ".otg") |
---|
759 | $ContentType = "application/vnd.oasis.opendocument.graphics-template"; |
---|
760 | if ($strFileType == ".odp") |
---|
761 | $ContentType = "application/vnd.oasis.opendocument.presentation"; |
---|
762 | if ($strFileType == ".otp") |
---|
763 | $ContentType = "application/vnd.oasis.opendocument.presentation-template"; |
---|
764 | if ($strFileType == ".ods") |
---|
765 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet"; |
---|
766 | if ($strFileType == ".ots") |
---|
767 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template"; |
---|
768 | if ($strFileType == ".odc") |
---|
769 | $ContentType = "application/vnd.oasis.opendocument.chart"; |
---|
770 | if ($strFileType == ".odf") |
---|
771 | $ContentType = "application/vnd.oasis.opendocument.formula"; |
---|
772 | if ($strFileType == ".odi") |
---|
773 | $ContentType = "application/vnd.oasis.opendocument.image"; |
---|
774 | if ($strFileType == ".ndl") |
---|
775 | $ContentType = "application/vnd.lotus-notes"; |
---|
776 | if ($strFileType == ".eml") |
---|
777 | $ContentType = "text/plain"; |
---|
778 | if ($strFileType == ".png") |
---|
779 | $ContentType = "image/png"; |
---|
780 | return $ContentType; |
---|
781 | } |
---|
782 | |
---|
783 | function download_all_attachments($params) { |
---|
784 | |
---|
785 | require_once dirname(__FILE__).'/class.attachment.inc.php'; |
---|
786 | $atObj = new attachment(); |
---|
787 | $atObj->setStructureFromMail($params['folder'],$params['num_msg']); |
---|
788 | $attachments = $atObj->getAttachmentsInfo(); |
---|
789 | $id_number = $params['num_msg']; |
---|
790 | $tempDir = $this->tempDir; |
---|
791 | $tempSubDir = $_SESSION['phpgw_session']['session_id']; |
---|
792 | $fileNames = ''; |
---|
793 | exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir); |
---|
794 | $this-> folder = $params['folder']; |
---|
795 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
796 | |
---|
797 | $fileNames = Array(); |
---|
798 | |
---|
799 | for ($i = 0; $i < count($attachments); $i++) |
---|
800 | { |
---|
801 | $attachments[$i]['name'] = $this->remove_accents($attachments[$i]['name']); |
---|
802 | $fileNames[$i] = $attachments[$i]['name']; |
---|
803 | } |
---|
804 | |
---|
805 | for ($i = 0; $i < count($attachments); $i++) |
---|
806 | { |
---|
807 | $fileName = $attachments[$i]['name']; |
---|
808 | $result = array_keys($fileNames, $fileName); |
---|
809 | |
---|
810 | // Detecta duplicatas |
---|
811 | if (count($result) > 1) |
---|
812 | { |
---|
813 | for ($j = 1; $j < count($result); $j++) |
---|
814 | { |
---|
815 | $replacement = '('.$j.')$0'; |
---|
816 | if (preg_match('/\.\w{2,4}$/', $fileName)) |
---|
817 | { |
---|
818 | $fileNames[$result[$j]] = preg_replace('/\.\w{2,4}$/', $replacement, $fileName); |
---|
819 | } |
---|
820 | else |
---|
821 | { |
---|
822 | $fileNames[$result[$j]] .= "($j)"; |
---|
823 | } |
---|
824 | $attachments[$result[$j]]['name'] = $fileNames[$result[$j]]; |
---|
825 | } |
---|
826 | } |
---|
827 | // Fim detecta duplicatas |
---|
828 | |
---|
829 | $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb"); |
---|
830 | if(!$f) |
---|
831 | return False; |
---|
832 | $fileContent = $atObj->getAttachment( $attachments[$i]['pid'] ); |
---|
833 | fputs($f,$fileContent); |
---|
834 | |
---|
835 | fclose($f); |
---|
836 | |
---|
837 | } |
---|
838 | imap_close($this->mbox_stream); |
---|
839 | $nameFileZip = ''; |
---|
840 | |
---|
841 | if(!empty($fileNames)) { |
---|
842 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir); |
---|
843 | if($nameFileZip) |
---|
844 | $file = $tempDir . '/'.$tempSubDir.'/'.$nameFileZip; |
---|
845 | else { |
---|
846 | $file = false; |
---|
847 | } |
---|
848 | } |
---|
849 | else |
---|
850 | $file = false; |
---|
851 | return $file; |
---|
852 | } |
---|
853 | |
---|
854 | function getHeader($msg_number){ |
---|
855 | return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID); |
---|
856 | } |
---|
857 | |
---|
858 | function getHeaderInfo($msg_number){ |
---|
859 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255); |
---|
860 | return $header; |
---|
861 | } |
---|
862 | |
---|
863 | function getBody($msg_number){ |
---|
864 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255); |
---|
865 | $body = imap_body($this->mbox_stream, $msg_number, FT_UID); |
---|
866 | if(($header->Unseen == 'U') || ($header->Recent == 'N')){ |
---|
867 | imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID); |
---|
868 | } |
---|
869 | return $body; |
---|
870 | } |
---|
871 | |
---|
872 | function decode_subject($string){ |
---|
873 | if ((strpos(strtolower($string), '=?iso-8859-1') !== false) |
---|
874 | || (strpos(strtolower($string), '=?windows-1252') !== false)){ |
---|
875 | $elements = imap_mime_header_decode($string); |
---|
876 | foreach ($elements as $el) |
---|
877 | $return .= $el->text; |
---|
878 | } |
---|
879 | else if (strpos(strtolower($string), '=?utf-8') !== false) { |
---|
880 | $elements = imap_mime_header_decode($string); |
---|
881 | foreach ($elements as $el){ |
---|
882 | $charset = $el->charset; |
---|
883 | $text = $el->text; |
---|
884 | if(!strcasecmp($charset, "utf-8") || |
---|
885 | !strcasecmp($charset, "utf-7")) { |
---|
886 | $text = iconv($charset, "ISO-8859-1", $text); |
---|
887 | } |
---|
888 | $return .= $text; |
---|
889 | } |
---|
890 | } |
---|
891 | else |
---|
892 | $return = $string; |
---|
893 | |
---|
894 | return $this->remove_accents($return); |
---|
895 | } |
---|
896 | } |
---|
897 | // END CLASS |
---|
898 | ?> |
---|