source: sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/inc/class.exporteml.inc.php @ 4660

Revision 4660, 21.4 KB checked in by fernando-alberto, 13 years ago (diff)

Ticket #1269 - Desenvolvimento da nova solucao de arquivamento local MailArchiver?, geracao do fonte da mensagem

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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
12class 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        //MAILARCHIVER
268        function js_source_var($params) {
269                $this-> folder = $params['folder'];
270                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
271                $id_number = $params['msgs_to_export'];
272                $tempDir = ini_get("session.save_path");
273
274                $this->connectImap();
275                $header         = $this-> getHeader($id_number);
276                $body           = $this-> getBody($id_number);
277
278                imap_close($this->mbox_stream);
279
280                return $header ."\r\n\r\n". $body;
281        }
282       
283
284    function export_msg_data($id_msg,$folder) {
285                $this->folder = $folder;
286                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
287
288                $this->connectImap();
289                $header         = $this-> getHeader($id_msg);
290                $body           = $this-> getBody($id_msg);
291
292                $msg_data = $header ."\r\n\r\n". $body;
293
294                imap_close($this->mbox_stream);
295                return $msg_data;
296        }
297
298                function export_to_archive($id_msg,$folder) {
299                $this-> folder = $folder;
300                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
301                   
302                $tempDir = $_SESSION['phpgw_info']['server']['temp_dir'];
303                                 
304                $this->connectImap();
305                $header         = $this-> getHeader($id_msg);
306                $body           = $this-> getBody($id_msg);
307                $name = md5($_SESSION[ 'phpgw_session' ][ 'session_id' ].microtime());
308               
309                $file = $_SESSION[ 'phpgw_session' ][ 'session_id' ].$name;
310                $f = fopen($tempDir . '/'.$file,"w");
311                fputs($f,utf8_encode($header) ."\r\n\r\n". utf8_encode($body));
312                fclose($f);
313                                 
314                imap_close($this->mbox_stream);
315                return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt";
316                                 
317        }
318
319        function get_attachments_in_array($params) {
320                $return_attachments = array();
321
322                $id_number = $params['num_msg'];
323                $attachments =unserialize(rawurldecode($params['s_attachments']));
324
325                $tempDir = $_SESSION['phpgw_info']['server']['temp_dir'];
326                $this-> folder = $params['folder'];
327                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
328                $this->connectImap();
329
330                include_once("class.imap_attachment.inc.php");
331
332                $imap_attachment = new imap_attachment();
333                $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number);
334                foreach($attachments as $i => $attachment){
335                        if($i && $i == 'names')
336                                continue;
337
338                        $fileName = $_SESSION[ 'phpgw_session' ][ 'session_id' ].md5($attachment['name'].microtime());
339                        $ContentType = $this->getFileType($fileNameReal);
340                        $f = fopen($tempDir . '/'.$fileName,"wb");
341                        if(!$f)
342                                return false;
343
344                        $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID);
345
346                        if($attachment['encoding'] == 'base64') {
347                                $headers=imap_base64($fileContent);
348                        }
349                        else if($attachment['encoding'] == 'quoted_printable_decode') {
350                                $headers=quoted_printable_decode($fileContent);
351                        }
352                        else {
353                                $headers=$fileContent;
354                        }
355
356                        fputs($f,$headers);
357
358                        $url =  "inc/gotodownload.php?idx_file=".$tempDir .'/'.$fileName."&newfilename=".$attachment['name'];
359                        array_push($return_attachments,array('name' => $fileName,'url' => $url,'pid' =>$attachment['pid']));
360                        fclose($f);
361                }
362                imap_close($this->mbox_stream);
363                return $return_attachments;
364        }
365
366
367        function remove_accents($string) {
368                /*
369                        $array1 = array("ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ" , "?", "\"", "!", "@", "#", "$", "%", "ï¿œ", "&", "*", "(", ")", "-", "=", "+", "ï¿œ", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ", "ï¿œ");
370                        $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");
371                        return str_replace( $array1, $array2, $string );
372                */
373                return strtr($string,
374                        "ᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵ?\"'!@#$%ï¿œ&*()-=+ï¿œ`[]{}~^,<>;:/?\\|ᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵᅵ",
375                        "aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC");
376        }
377       
378
379        private function getFileType($nameFile) {
380                $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4));
381                $ContentType = "application/octet-stream";
382                if ($strFileType == ".asf")
383                        $ContentType = "video/x-ms-asf";
384                if ($strFileType == ".avi")
385                        $ContentType = "video/avi";
386                if ($strFileType == ".doc")
387                        $ContentType = "application/msword";
388                if ($strFileType == ".zip")
389                        $ContentType = "application/zip";
390                if ($strFileType == ".xls")
391                        $ContentType = "application/vnd.ms-excel";
392                if ($strFileType == ".gif")
393                        $ContentType = "image/gif";
394                if ($strFileType == ".png")
395                        $ContentType = "image/png";
396                if ($strFileType == ".jpg" || $strFileType == "jpeg")
397                        $ContentType = "image/jpeg";
398                if ($strFileType == ".wav")
399                        $ContentType = "audio/wav";
400                if ($strFileType == ".mp3")
401                        $ContentType = "audio/mpeg3";
402                if ($strFileType == ".mpg" || $strFileType == "mpeg")
403                        $ContentType = "video/mpeg";
404                if ($strFileType == ".rtf")
405                        $ContentType = "application/rtf";
406                if ($strFileType == ".htm" || $strFileType == "html")
407                        $ContentType = "text/html";
408                if ($strFileType == ".xml")
409                        $ContentType = "text/xml";
410                if ($strFileType == ".xsl")
411                        $ContentType = "text/xsl";
412                if ($strFileType == ".css")
413                        $ContentType = "text/css";
414                if ($strFileType == ".php")
415                        $ContentType = "text/php";
416                if ($strFileType == ".asp")
417                        $ContentType = "text/asp";
418                if ($strFileType == ".pdf")
419                        $ContentType = "application/pdf";
420                if ($strFileType == ".txt")
421                        $ContentType = "text/plain";
422                if ($strFileType == ".log")
423                        $ContentType = "text/plain";
424                if ($strFileType == ".wmv")
425                        $ContentType = "video/x-ms-wmv";
426                if ($strFileType == ".sxc")
427                        $ContentType = "application/vnd.sun.xml.calc";
428                if ($strFileType == ".odt")
429                        $ContentType = "application/vnd.oasis.opendocument.text";
430                if ($strFileType == ".stc")
431                        $ContentType = "application/vnd.sun.xml.calc.template";
432                if ($strFileType == ".sxd")
433                        $ContentType = "application/vnd.sun.xml.draw";
434                if ($strFileType == ".std")
435                        $ContentType = "application/vnd.sun.xml.draw.template";
436                if ($strFileType == ".sxi")
437                        $ContentType = "application/vnd.sun.xml.impress";
438                if ($strFileType == ".sti")
439                        $ContentType = "application/vnd.sun.xml.impress.template";
440                if ($strFileType == ".sxm")
441                        $ContentType = "application/vnd.sun.xml.math";
442                if ($strFileType == ".sxw")
443                        $ContentType = "application/vnd.sun.xml.writer";
444                if ($strFileType == ".sxq")
445                        $ContentType = "application/vnd.sun.xml.writer.global";
446                if ($strFileType == ".stw")
447                        $ContentType = "application/vnd.sun.xml.writer.template";
448                if ($strFileType == ".ps")
449                        $ContentType = "application/postscript";
450                if ($strFileType == ".pps")
451                        $ContentType = "application/vnd.ms-powerpoint";
452                if ($strFileType == ".odt")
453                        $ContentType = "application/vnd.oasis.opendocument.text";
454                if ($strFileType == ".ott")
455                        $ContentType = "application/vnd.oasis.opendocument.text-template";
456                if ($strFileType == ".oth")
457                        $ContentType = "application/vnd.oasis.opendocument.text-web";
458                if ($strFileType == ".odm")
459                        $ContentType = "application/vnd.oasis.opendocument.text-master";
460                if ($strFileType == ".odg")
461                        $ContentType = "application/vnd.oasis.opendocument.graphics";
462                if ($strFileType == ".otg")
463                        $ContentType = "application/vnd.oasis.opendocument.graphics-template";
464                if ($strFileType == ".odp")
465                        $ContentType = "application/vnd.oasis.opendocument.presentation";
466                if ($strFileType == ".otp")
467                        $ContentType = "application/vnd.oasis.opendocument.presentation-template";
468                if ($strFileType == ".ods")
469                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
470                if ($strFileType == ".ots")
471                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
472                if ($strFileType == ".odc")
473                        $ContentType = "application/vnd.oasis.opendocument.chart";
474                if ($strFileType == ".odf")
475                        $ContentType = "application/vnd.oasis.opendocument.formula";
476                if ($strFileType == ".odi")
477                        $ContentType = "application/vnd.oasis.opendocument.image";
478                if ($strFileType == ".ndl")
479                        $ContentType = "application/vnd.lotus-notes";
480                if ($strFileType == ".eml")
481                        $ContentType = "text/plain";
482                return $ContentType;
483        }
484       
485        function download_all_attachments($params) {
486               
487                $id_number = $params['num_msg'];               
488                $attachments =unserialize(rawurldecode($params['s_attachments']));
489               
490                $tempDir = ini_get("session.save_path");
491                $tempSubDir = $_SESSION['phpgw_session']['session_id'];
492                $fileNames = '';
493                exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir);
494                $this-> folder = $params['folder'];
495                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
496                $this->connectImap();
497                include("class.imap_attachment.inc.php");
498                $imap_attachment = new imap_attachment();
499                $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number);
500               
501                $fileNames = Array();
502                       
503                for ($i = 0; $i < count($attachments); $i++)
504                {
505                   $attachments[$i]['name'] = $this->remove_accents($attachments[$i]['name']);
506                   $fileNames[$i] = $attachments[$i]['name'];
507                }
508
509                for ($i = 0; $i < count($attachments); $i++)
510                {
511                        $fileName = $attachments[$i]['name'];
512                        $result = array_keys($fileNames, $fileName);
513
514                        // Detecta duplicatas
515                        if (count($result) > 1)
516                        {
517                            for ($j = 1; $j < count($result); $j++)
518                            {
519                                $replacement = '('.$j.')$0';
520                                if (preg_match('/\.\w{2,4}$/', $fileName))
521                                {
522                                    $fileNames[$result[$j]] = preg_replace('/\.\w{2,4}$/', $replacement, $fileName);
523                                }
524                                else
525                                {
526                                    $fileNames[$result[$j]] .= "($j)";
527                                }
528                                $attachments[$result[$j]]['name'] = $fileNames[$result[$j]];
529                            }
530                        }
531                        // Fim detecta duplicatas
532
533                        $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb");
534                        if(!$f)
535                                return False;                   
536                                               
537                        $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachments[$i]['pid'], FT_UID);
538                        if($attachments[$i]['encoding'] == 'base64')
539                                fputs($f,imap_base64($fileContent));
540                        else           
541                                fputs($f,$fileContent);
542                               
543                        fclose($f);
544               
545                }
546                imap_close($this->mbox_stream);
547                $nameFileZip = '';
548               
549                if(!empty($fileNames)) {
550                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir);                                         
551                        if($nameFileZip)
552                                $file =  $tempDir . '/'.$tempSubDir.'/'.$nameFileZip;
553                        else {
554                                $file = false;
555                        }
556                }
557                else
558                        $file = false; 
559                return $file;
560        }
561
562        function getHeader($msg_number){                       
563                return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID);
564        }
565       
566        function getBody($msg_number){
567                $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255);
568                $body = imap_body($this->mbox_stream, $msg_number, FT_UID);
569                if(($header->Unseen == 'U') || ($header->Recent == 'N')){
570                        imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID);
571                }
572                return $body;
573        }
574
575        function decode_subject($string){
576                if ((strpos(strtolower($string), '=?iso-8859-1') !== false)
577                        || (strpos(strtolower($string), '=?windows-1252') !== false)){
578                        $elements = imap_mime_header_decode($string);
579                        foreach ($elements as $el)
580                                $return .= $el->text;
581                }
582                else if (strpos(strtolower($string), '=?utf-8') !== false) {
583                        $elements = imap_mime_header_decode($string);
584                        foreach ($elements as $el){
585                                $charset = $el->charset;
586                                $text    = $el->text;
587                                if(!strcasecmp($charset, "utf-8") ||
588                                !strcasecmp($charset, "utf-7")) {
589                                $text = iconv($charset, "ISO-8859-1", $text);
590                        }
591                        $return .= $text;
592                        }
593                }
594                else
595                        $return = $string;
596
597                return $this->remove_accents($return);         
598        }
599}
600// END CLASS
601?>
Note: See TracBrowser for help on using the repository browser.