source: sandbox/expressoMail1_2/corretor_ortografico/inc/class.exporteml.inc.php @ 2109

Revision 2109, 20.9 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #901 - Implementação da melhoria descrita no ticket em questão.

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