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 | |
---|
18 | function connectImap(){ |
---|
19 | |
---|
20 | $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
21 | $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
22 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
23 | $imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
24 | |
---|
25 | if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') |
---|
26 | { |
---|
27 | $imap_options = '/tls/novalidate-cert'; |
---|
28 | } |
---|
29 | else |
---|
30 | { |
---|
31 | $imap_options = '/notls/novalidate-cert'; |
---|
32 | } |
---|
33 | $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password); |
---|
34 | } |
---|
35 | |
---|
36 | //export message to EML Format |
---|
37 | function parseEml($header, $body) |
---|
38 | { |
---|
39 | $sEmailHeader = $header; |
---|
40 | $sEmailBody = $body; |
---|
41 | $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody; |
---|
42 | return $sEMail; |
---|
43 | } |
---|
44 | |
---|
45 | // create EML File. |
---|
46 | // Funcao alterada para tratar a exportacao |
---|
47 | // de mensagens arquivadas localmente. |
---|
48 | // Rommel Cysne (rommel.cysne@serpro.gov.br) |
---|
49 | // em 17/12/2008. |
---|
50 | function createFileEml($sEMLData, $tempDir, $id, $subject=false, $i=false) |
---|
51 | { |
---|
52 | if($id) |
---|
53 | { |
---|
54 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $id), 80, 255); |
---|
55 | $subject = $this->decode_subject($header->fetchsubject); |
---|
56 | |
---|
57 | if (strlen($subject) > 60) |
---|
58 | $subject = substr($subject, 0, 59); |
---|
59 | |
---|
60 | //$subject = ereg_replace('/', '\'', $subject); |
---|
61 | $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; |
---|
62 | $to = "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; |
---|
63 | $subject = strtr($subject,$from,$to); |
---|
64 | $file = $subject."_".$id.".eml"; |
---|
65 | } else{ |
---|
66 | // Se mensagem for arquivada localmente, $subject (assunto da mensagem) |
---|
67 | // sera passado para compor o nome do arquivo .eml; |
---|
68 | |
---|
69 | if($subject && $i){ |
---|
70 | $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; |
---|
71 | $to = "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; |
---|
72 | $subject = strtr($subject,$from,$to); |
---|
73 | //$subject = ereg_replace(':', '_', $subject); |
---|
74 | //$subject = ereg_replace('/', '_', $subject); |
---|
75 | $file = $subject."_".$i.".eml"; |
---|
76 | } else{ |
---|
77 | $file = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
82 | if(!$f) |
---|
83 | return False; |
---|
84 | |
---|
85 | fputs($f,$sEMLData); |
---|
86 | fclose($f); |
---|
87 | |
---|
88 | return $file; |
---|
89 | } |
---|
90 | |
---|
91 | function createFileZip($files, $tempDir){ |
---|
92 | $tmp_zip_filename = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".zip"; |
---|
93 | |
---|
94 | if (!empty($files)) |
---|
95 | { |
---|
96 | if (is_array($files)) |
---|
97 | { |
---|
98 | for ($i=0; $i < count($files); $i++) |
---|
99 | { |
---|
100 | $files[$i] = escapeshellarg($files[$i]); |
---|
101 | } |
---|
102 | $files = implode(' ', $files); |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | $files = escapeshellcmd($files); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | $command = "cd " . escapeshellarg($tempDir) . " && nice zip -m9 " . escapeshellarg($tmp_zip_filename) . " " . $files; |
---|
111 | if(!exec($command)) { |
---|
112 | $command = "cd " . escapeshellarg($tempDir) . " && rm ".$files." ". escapeshellarg($tmp_zip_filename); |
---|
113 | exec($command); |
---|
114 | return null; |
---|
115 | } |
---|
116 | |
---|
117 | return $tmp_zip_filename; |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | function export_all($params){ |
---|
122 | |
---|
123 | $this->folder = $params['folder']; |
---|
124 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
125 | $fileNames = ""; |
---|
126 | $tempDir = ini_get("session.save_path"); |
---|
127 | $this->connectImap(); |
---|
128 | |
---|
129 | $msgs = imap_search($this->mbox_stream,"ALL",SE_UID); |
---|
130 | if($msgs){ |
---|
131 | foreach($msgs as $nMsgs){ |
---|
132 | $header = $this-> getHeader($nMsgs); |
---|
133 | $body = $this-> getBody($nMsgs); |
---|
134 | $sEMLData = $this -> parseEml($header, $body); |
---|
135 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs); |
---|
136 | if(!$fileName) { |
---|
137 | $error = True; |
---|
138 | break; |
---|
139 | } |
---|
140 | else |
---|
141 | $fileNames .= "\"".$fileName."\" "; |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | imap_close($this->mbox_stream); |
---|
146 | |
---|
147 | $nameFileZip = 'False'; |
---|
148 | if($fileNames && !$error) { |
---|
149 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
150 | if($nameFileZip) |
---|
151 | $file = $tempDir.'/'.$nameFileZip; |
---|
152 | else { |
---|
153 | $file = false; |
---|
154 | } |
---|
155 | } |
---|
156 | else |
---|
157 | $file = false; |
---|
158 | }else{ |
---|
159 | $file["empty_folder"] = true; |
---|
160 | } |
---|
161 | return $file; |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | // Funcao alterada para tratar a exportacao |
---|
166 | // de mensagens arquivadas localmente. |
---|
167 | // Rommel Cysne (rommel.cysne@serpro.gov.br) |
---|
168 | // em 17/12/2008. |
---|
169 | function makeAll($params) { |
---|
170 | // Exportacao de mensagens arquivadas localmente |
---|
171 | if($params['l_msg'] == "t"){ |
---|
172 | // Recebe todos os subjects e bodies das mensagens locais selecionadas para exportacao |
---|
173 | // e gera arrays com os conteudos separados; |
---|
174 | $array_mesgs = explode('@@',$params['mesgs']); |
---|
175 | $array_subjects = explode('@@',$params['subjects']); |
---|
176 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
177 | $tempDir = ini_get("session.save_path"); |
---|
178 | // Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma; |
---|
179 | include_once("class.imap_functions.inc.php"); |
---|
180 | $imapf = new imap_functions(); |
---|
181 | foreach($array_ids as $i=>$id) { |
---|
182 | $sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]); |
---|
183 | $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[$i],$i); |
---|
184 | if(!$fileName){ |
---|
185 | $error = True; |
---|
186 | break; |
---|
187 | } else{ |
---|
188 | $fileNames .= "\"".$fileName."\" "; |
---|
189 | } |
---|
190 | } |
---|
191 | $nameFileZip = 'False'; |
---|
192 | if($fileNames && !$error) { |
---|
193 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
194 | if($nameFileZip){ |
---|
195 | $file = $tempDir.'/'.$nameFileZip; |
---|
196 | } else{ |
---|
197 | $file = false; |
---|
198 | } |
---|
199 | |
---|
200 | } else{ |
---|
201 | $file = false; |
---|
202 | } |
---|
203 | |
---|
204 | return $file; |
---|
205 | |
---|
206 | // Exportacao de mensagens da caixa de entrada (imap) - processo original do Expresso |
---|
207 | } else{ |
---|
208 | $this-> folder = $params['folder']; |
---|
209 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
210 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
211 | $error = False; |
---|
212 | $fileNames = ""; |
---|
213 | $tempDir = ini_get("session.save_path"); |
---|
214 | $this->connectImap(); |
---|
215 | for($i = 0; $i < count($array_ids); $i++) { |
---|
216 | $header = $this-> getHeader($array_ids[$i]); |
---|
217 | $body = $this-> getBody($array_ids[$i]); |
---|
218 | $sEMLData = $this -> parseEml($header, $body); |
---|
219 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]); |
---|
220 | |
---|
221 | if(!$fileName) { |
---|
222 | $error = True; |
---|
223 | break; |
---|
224 | } |
---|
225 | else |
---|
226 | $fileNames .= "\"".$fileName."\" "; |
---|
227 | |
---|
228 | } |
---|
229 | imap_close($this->mbox_stream); |
---|
230 | |
---|
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 | |
---|
244 | return $file; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | function export_msg($params) { |
---|
249 | $this-> folder = $params['folder']; |
---|
250 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
251 | $id_number = $params['msgs_to_export']; |
---|
252 | $tempDir = ini_get("session.save_path"); |
---|
253 | |
---|
254 | $this->connectImap(); |
---|
255 | $header = $this-> getHeader($id_number); |
---|
256 | $body = $this-> getBody($id_number); |
---|
257 | |
---|
258 | $file = "source_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; |
---|
259 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
260 | fputs($f,$header ."\r\n\r\n". $body); |
---|
261 | fclose($f); |
---|
262 | |
---|
263 | imap_close($this->mbox_stream); |
---|
264 | return $tempDir.'/'.$file; |
---|
265 | } |
---|
266 | |
---|
267 | function export_msg_data($id_msg,$folder) { |
---|
268 | $this->folder = $folder; |
---|
269 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
270 | |
---|
271 | $this->connectImap(); |
---|
272 | $header = $this-> getHeader($id_msg); |
---|
273 | $body = $this-> getBody($id_msg); |
---|
274 | |
---|
275 | $msg_data = $header ."\r\n\r\n". $body; |
---|
276 | |
---|
277 | imap_close($this->mbox_stream); |
---|
278 | return $msg_data; |
---|
279 | } |
---|
280 | |
---|
281 | function export_to_archive($id_msg,$folder) { |
---|
282 | $this-> folder = $folder; |
---|
283 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
284 | |
---|
285 | $tempDir = $_SESSION['phpgw_info']['server']['temp_dir']; |
---|
286 | |
---|
287 | $this->connectImap(); |
---|
288 | $header = $this-> getHeader($id_msg); |
---|
289 | $body = $this-> getBody($id_msg); |
---|
290 | $name = md5($_SESSION[ 'phpgw_session' ][ 'session_id' ].microtime()); |
---|
291 | |
---|
292 | $file = $_SESSION[ 'phpgw_session' ][ 'session_id' ].$name; |
---|
293 | $f = fopen($tempDir . '/'.$file,"w"); |
---|
294 | fputs($f,utf8_encode($header) ."\r\n\r\n". utf8_encode($body)); |
---|
295 | fclose($f); |
---|
296 | |
---|
297 | imap_close($this->mbox_stream); |
---|
298 | return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt"; |
---|
299 | |
---|
300 | } |
---|
301 | |
---|
302 | function get_attachments_in_array($params) { |
---|
303 | $return_attachments = array(); |
---|
304 | |
---|
305 | $id_number = $params['num_msg']; |
---|
306 | $attachments =unserialize(rawurldecode($params['s_attachments'])); |
---|
307 | |
---|
308 | $tempDir = $_SESSION['phpgw_info']['server']['temp_dir']; |
---|
309 | $this-> folder = $params['folder']; |
---|
310 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
311 | $this->connectImap(); |
---|
312 | |
---|
313 | include_once("class.imap_attachment.inc.php"); |
---|
314 | |
---|
315 | $imap_attachment = new imap_attachment(); |
---|
316 | $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number); |
---|
317 | foreach($attachments as $i => $attachment){ |
---|
318 | if($i && $i == 'names') |
---|
319 | continue; |
---|
320 | |
---|
321 | $fileName = $_SESSION[ 'phpgw_session' ][ 'session_id' ].md5($attachment['name'].microtime()); |
---|
322 | $ContentType = $this->getFileType($fileNameReal); |
---|
323 | $f = fopen($tempDir . '/'.$fileName,"wb"); |
---|
324 | if(!$f) |
---|
325 | return false; |
---|
326 | |
---|
327 | $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID); |
---|
328 | |
---|
329 | if($attachment['encoding'] == 'base64') { |
---|
330 | $headers=imap_base64($fileContent); |
---|
331 | } |
---|
332 | else if($attachment['encoding'] == 'quoted_printable_decode') { |
---|
333 | $headers=quoted_printable_decode($fileContent); |
---|
334 | } |
---|
335 | else { |
---|
336 | $headers=$fileContent; |
---|
337 | } |
---|
338 | |
---|
339 | fputs($f,$headers); |
---|
340 | |
---|
341 | $url = "inc/gotodownload.php?idx_file=".$tempDir .'/'.$fileName."&newfilename=".$attachment['name']; |
---|
342 | array_push($return_attachments,array('name' => $fileName,'url' => $url,'pid' =>$attachment['pid'])); |
---|
343 | fclose($f); |
---|
344 | } |
---|
345 | imap_close($this->mbox_stream); |
---|
346 | return $return_attachments; |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | function remove_accents($string) { |
---|
351 | /* |
---|
352 | $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "š", "&", "*", "(", ")", "-", "=", "+", "Ž", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç"); |
---|
353 | $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"); |
---|
354 | return str_replace( $array1, $array2, $string ); |
---|
355 | */ |
---|
356 | return strtr($string, |
---|
357 | "áàâãäéèêëíìîïóòôõöúùûüç?\"'!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ", |
---|
358 | "aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC"); |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | private function getFileType($nameFile) { |
---|
363 | $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4)); |
---|
364 | $ContentType = "application/octet-stream"; |
---|
365 | if ($strFileType == ".asf") |
---|
366 | $ContentType = "video/x-ms-asf"; |
---|
367 | if ($strFileType == ".avi") |
---|
368 | $ContentType = "video/avi"; |
---|
369 | if ($strFileType == ".doc") |
---|
370 | $ContentType = "application/msword"; |
---|
371 | if ($strFileType == ".zip") |
---|
372 | $ContentType = "application/zip"; |
---|
373 | if ($strFileType == ".xls") |
---|
374 | $ContentType = "application/vnd.ms-excel"; |
---|
375 | if ($strFileType == ".gif") |
---|
376 | $ContentType = "image/gif"; |
---|
377 | if ($strFileType == ".png") |
---|
378 | $ContentType = "image/png"; |
---|
379 | if ($strFileType == ".jpg" || $strFileType == "jpeg") |
---|
380 | $ContentType = "image/jpeg"; |
---|
381 | if ($strFileType == ".wav") |
---|
382 | $ContentType = "audio/wav"; |
---|
383 | if ($strFileType == ".mp3") |
---|
384 | $ContentType = "audio/mpeg3"; |
---|
385 | if ($strFileType == ".mpg" || $strFileType == "mpeg") |
---|
386 | $ContentType = "video/mpeg"; |
---|
387 | if ($strFileType == ".rtf") |
---|
388 | $ContentType = "application/rtf"; |
---|
389 | if ($strFileType == ".htm" || $strFileType == "html") |
---|
390 | $ContentType = "text/html"; |
---|
391 | if ($strFileType == ".xml") |
---|
392 | $ContentType = "text/xml"; |
---|
393 | if ($strFileType == ".xsl") |
---|
394 | $ContentType = "text/xsl"; |
---|
395 | if ($strFileType == ".css") |
---|
396 | $ContentType = "text/css"; |
---|
397 | if ($strFileType == ".php") |
---|
398 | $ContentType = "text/php"; |
---|
399 | if ($strFileType == ".asp") |
---|
400 | $ContentType = "text/asp"; |
---|
401 | if ($strFileType == ".pdf") |
---|
402 | $ContentType = "application/pdf"; |
---|
403 | if ($strFileType == ".txt") |
---|
404 | $ContentType = "text/plain"; |
---|
405 | if ($strFileType == ".log") |
---|
406 | $ContentType = "text/plain"; |
---|
407 | if ($strFileType == ".wmv") |
---|
408 | $ContentType = "video/x-ms-wmv"; |
---|
409 | if ($strFileType == ".sxc") |
---|
410 | $ContentType = "application/vnd.sun.xml.calc"; |
---|
411 | if ($strFileType == ".odt") |
---|
412 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
413 | if ($strFileType == ".stc") |
---|
414 | $ContentType = "application/vnd.sun.xml.calc.template"; |
---|
415 | if ($strFileType == ".sxd") |
---|
416 | $ContentType = "application/vnd.sun.xml.draw"; |
---|
417 | if ($strFileType == ".std") |
---|
418 | $ContentType = "application/vnd.sun.xml.draw.template"; |
---|
419 | if ($strFileType == ".sxi") |
---|
420 | $ContentType = "application/vnd.sun.xml.impress"; |
---|
421 | if ($strFileType == ".sti") |
---|
422 | $ContentType = "application/vnd.sun.xml.impress.template"; |
---|
423 | if ($strFileType == ".sxm") |
---|
424 | $ContentType = "application/vnd.sun.xml.math"; |
---|
425 | if ($strFileType == ".sxw") |
---|
426 | $ContentType = "application/vnd.sun.xml.writer"; |
---|
427 | if ($strFileType == ".sxq") |
---|
428 | $ContentType = "application/vnd.sun.xml.writer.global"; |
---|
429 | if ($strFileType == ".stw") |
---|
430 | $ContentType = "application/vnd.sun.xml.writer.template"; |
---|
431 | if ($strFileType == ".ps") |
---|
432 | $ContentType = "application/postscript"; |
---|
433 | if ($strFileType == ".pps") |
---|
434 | $ContentType = "application/vnd.ms-powerpoint"; |
---|
435 | if ($strFileType == ".odt") |
---|
436 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
437 | if ($strFileType == ".ott") |
---|
438 | $ContentType = "application/vnd.oasis.opendocument.text-template"; |
---|
439 | if ($strFileType == ".oth") |
---|
440 | $ContentType = "application/vnd.oasis.opendocument.text-web"; |
---|
441 | if ($strFileType == ".odm") |
---|
442 | $ContentType = "application/vnd.oasis.opendocument.text-master"; |
---|
443 | if ($strFileType == ".odg") |
---|
444 | $ContentType = "application/vnd.oasis.opendocument.graphics"; |
---|
445 | if ($strFileType == ".otg") |
---|
446 | $ContentType = "application/vnd.oasis.opendocument.graphics-template"; |
---|
447 | if ($strFileType == ".odp") |
---|
448 | $ContentType = "application/vnd.oasis.opendocument.presentation"; |
---|
449 | if ($strFileType == ".otp") |
---|
450 | $ContentType = "application/vnd.oasis.opendocument.presentation-template"; |
---|
451 | if ($strFileType == ".ods") |
---|
452 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet"; |
---|
453 | if ($strFileType == ".ots") |
---|
454 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template"; |
---|
455 | if ($strFileType == ".odc") |
---|
456 | $ContentType = "application/vnd.oasis.opendocument.chart"; |
---|
457 | if ($strFileType == ".odf") |
---|
458 | $ContentType = "application/vnd.oasis.opendocument.formula"; |
---|
459 | if ($strFileType == ".odi") |
---|
460 | $ContentType = "application/vnd.oasis.opendocument.image"; |
---|
461 | if ($strFileType == ".ndl") |
---|
462 | $ContentType = "application/vnd.lotus-notes"; |
---|
463 | if ($strFileType == ".eml") |
---|
464 | $ContentType = "text/plain"; |
---|
465 | return $ContentType; |
---|
466 | } |
---|
467 | |
---|
468 | function download_all_attachments($params) { |
---|
469 | |
---|
470 | $id_number = $params['num_msg']; |
---|
471 | $attachments =unserialize(rawurldecode($params['s_attachments'])); |
---|
472 | |
---|
473 | $tempDir = ini_get("session.save_path"); |
---|
474 | $tempSubDir = $_SESSION['phpgw_session']['session_id']; |
---|
475 | $fileNames = ''; |
---|
476 | exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir); |
---|
477 | $this-> folder = $params['folder']; |
---|
478 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
479 | $this->connectImap(); |
---|
480 | include("class.imap_attachment.inc.php"); |
---|
481 | $imap_attachment = new imap_attachment(); |
---|
482 | $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number); |
---|
483 | |
---|
484 | $fileNames = Array(); |
---|
485 | |
---|
486 | for ($i = 0; $i < count($attachments); $i++) |
---|
487 | { |
---|
488 | $attachments[$i]['name'] = $this->remove_accents($attachments[$i]['name']); |
---|
489 | $fileNames[$i] = $attachments[$i]['name']; |
---|
490 | } |
---|
491 | |
---|
492 | for ($i = 0; $i < count($attachments); $i++) |
---|
493 | { |
---|
494 | $fileName = $attachments[$i]['name']; |
---|
495 | $result = array_keys($fileNames, $fileName); |
---|
496 | |
---|
497 | // Detecta duplicatas |
---|
498 | if (count($result) > 1) |
---|
499 | { |
---|
500 | for ($j = 1; $j < count($result); $j++) |
---|
501 | { |
---|
502 | $replacement = '('.$j.')$0'; |
---|
503 | if (preg_match('/\.\w{2,4}$/', $fileName)) |
---|
504 | { |
---|
505 | $fileNames[$result[$j]] = preg_replace('/\.\w{2,4}$/', $replacement, $fileName); |
---|
506 | } |
---|
507 | else |
---|
508 | { |
---|
509 | $fileNames[$result[$j]] .= "($j)"; |
---|
510 | } |
---|
511 | $attachments[$result[$j]]['name'] = $fileNames[$result[$j]]; |
---|
512 | } |
---|
513 | } |
---|
514 | // Fim detecta duplicatas |
---|
515 | |
---|
516 | $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb"); |
---|
517 | if(!$f) |
---|
518 | return False; |
---|
519 | |
---|
520 | $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachments[$i]['pid'], FT_UID); |
---|
521 | if($attachments[$i]['encoding'] == 'base64') |
---|
522 | fputs($f,imap_base64($fileContent)); |
---|
523 | else |
---|
524 | fputs($f,$fileContent); |
---|
525 | |
---|
526 | fclose($f); |
---|
527 | |
---|
528 | } |
---|
529 | imap_close($this->mbox_stream); |
---|
530 | $nameFileZip = ''; |
---|
531 | |
---|
532 | if(!empty($fileNames)) { |
---|
533 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir); |
---|
534 | if($nameFileZip) |
---|
535 | $file = $tempDir . '/'.$tempSubDir.'/'.$nameFileZip; |
---|
536 | else { |
---|
537 | $file = false; |
---|
538 | } |
---|
539 | } |
---|
540 | else |
---|
541 | $file = false; |
---|
542 | return $file; |
---|
543 | } |
---|
544 | |
---|
545 | function getHeader($msg_number){ |
---|
546 | return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID); |
---|
547 | } |
---|
548 | |
---|
549 | function getBody($msg_number){ |
---|
550 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255); |
---|
551 | $body = imap_body($this->mbox_stream, $msg_number, FT_UID); |
---|
552 | if(($header->Unseen == 'U') || ($header->Recent == 'N')){ |
---|
553 | imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID); |
---|
554 | } |
---|
555 | return $body; |
---|
556 | } |
---|
557 | |
---|
558 | function decode_subject($string){ |
---|
559 | if ((strpos(strtolower($string), '=?iso-8859-1') !== false) |
---|
560 | || (strpos(strtolower($string), '=?windows-1252') !== false)){ |
---|
561 | $elements = imap_mime_header_decode($string); |
---|
562 | foreach ($elements as $el) |
---|
563 | $return .= $el->text; |
---|
564 | } |
---|
565 | else if (strpos(strtolower($string), '=?utf-8') !== false) { |
---|
566 | $elements = imap_mime_header_decode($string); |
---|
567 | foreach ($elements as $el){ |
---|
568 | $charset = $el->charset; |
---|
569 | $text = $el->text; |
---|
570 | if(!strcasecmp($charset, "utf-8") || |
---|
571 | !strcasecmp($charset, "utf-7")) { |
---|
572 | $text = iconv($charset, "ISO-8859-1", $text); |
---|
573 | } |
---|
574 | $return .= $text; |
---|
575 | } |
---|
576 | } |
---|
577 | else |
---|
578 | $return = $string; |
---|
579 | |
---|
580 | return $this->remove_accents($return); |
---|
581 | } |
---|
582 | } |
---|
583 | // END CLASS |
---|
584 | ?> |
---|