source: trunk/expressoMail1_2/inc/class.exporteml.inc.php @ 601

Revision 601, 9.1 KB checked in by niltonneto, 15 years ago (diff)

Limpeza de offdate (cleanup) de alguns arquivos.

  • 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    function createFileEml($sEMLData, $tempDir, $id) 
47    {
48        if($id)
49        {
50            $header    = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $id), 80, 255);
51            $subject = $this->decode_subject($header->fetchsubject);
52           
53            if (strlen($subject) > 60)
54                $subject = substr($subject, 0, 59);
55 
56            $subject = ereg_replace('/', '\'', $subject);           
57            $file =    $subject."_".$id.".eml";
58        }   
59        else{
60            $file =    "email_".md5(microtime()).".eml";
61        }
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 createFileZip($files, $tempDir){               
74                $tmp_zip_filename =     "email_".md5(microtime()).".zip";
75                $command = "cd " . $tempDir . "; nice zip -m " . $tmp_zip_filename . " " . $files;                     
76                if(!exec($command)) {
77                        $command = 'cd ' . $tempDir . '; rm '.$files;
78                        exec($command);
79                        return null;
80                }
81               
82                return $tmp_zip_filename;
83                               
84        }
85
86        function export_all($params){
87               
88                $this->folder = $params['folder'];
89                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
90                $fileNames = "";
91                $tempDir = ini_get("session.save_path");
92                $this->connectImap();
93               
94                $msgs = imap_search($this->mbox_stream,"ALL",SE_UID);
95                foreach($msgs as $nMsgs){
96                        $header         = $this-> getHeader($nMsgs);                                                                   
97                        $body           = $this-> getBody($nMsgs);                     
98                        $sEMLData       = $this -> parseEml($header, $body);
99                        $fileName       = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs);
100                        if(!$fileName)  {
101                                $error = True;                                 
102                                break;
103                        }
104                        else
105                                $fileNames .= "\"".$fileName."\" ";                     
106                       
107                }
108                imap_close($this->mbox_stream);
109               
110                $nameFileZip = 'False';                 
111                if($fileNames && !$error) {                     
112                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
113                        if($nameFileZip)                       
114                                $file = $tempDir.'/'.$nameFileZip;
115                        else {
116                                $file = false;
117                        }                                                               
118                }
119                else
120                        $file = false;
121                       
122                return $file;
123               
124        }
125
126        function makeAll($params) {
127               
128                $this-> folder = $params['folder'];
129                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
130                $array_ids = explode(',', $params['msgs_to_export']);   
131                $error = False;         
132                $fileNames = "";
133                $tempDir = ini_get("session.save_path");
134                $this->connectImap();
135                               
136                for($i = 0; $i < count($array_ids); $i++) {
137                               
138                        $header         = $this-> getHeader($array_ids[$i]);                                                                                   
139                        $body           = $this-> getBody($array_ids[$i]);                     
140                        $sEMLData       = $this -> parseEml($header, $body);                   
141                        $fileName       = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]);
142                       
143                        if(!$fileName)  {
144                                $error = True;                                 
145                                break;
146                        }
147                        else
148                                $fileNames .= "\"".$fileName."\" ";                     
149                       
150                }
151                imap_close($this->mbox_stream);
152               
153               
154                $nameFileZip = 'False';                 
155                if($fileNames && !$error) {                     
156                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
157                        if($nameFileZip)                       
158                                $file = $tempDir.'/'.$nameFileZip;
159                        else {
160                                $file = false;
161                        }                                                               
162                }
163                else
164                        $file = false;
165                       
166                return $file;
167        }
168
169        function export_msg($params) {
170                $this-> folder = $params['folder'];
171                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
172                $id_number = $params['msgs_to_export'];
173                $tempDir = ini_get("session.save_path");
174               
175                $this->connectImap();
176                $header         = $this-> getHeader($id_number);
177                $body           = $this-> getBody($id_number);
178               
179                $file = "source_".md5(microtime()).".txt";
180                $f = fopen($tempDir.'/'.$file,"w");
181                fputs($f,$header ."\r\n\r\n". $body);
182                fclose($f);
183               
184                imap_close($this->mbox_stream);
185                return $tempDir.'/'.$file;
186        }
187
188        function remove_accents($string) {
189                /*
190                        $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "š", "&", "*", "(", ")", "-", "=", "+", "Ž", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
191                        $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");
192                        return str_replace( $array1, $array2, $string );
193                */
194                return strtr($string,
195                        "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ",
196                        "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC");
197        }
198       
199        function download_all_attachments($params) {
200               
201                $id_number = $params['num_msg'];               
202                $attachments =unserialize(rawurldecode($params['s_attachments']));
203               
204                $tempDir = ini_get("session.save_path");
205                $tempSubDir = md5(microtime());
206                $fileNames = '';
207                exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir);
208                $this-> folder = $params['folder'];
209                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
210                $this->connectImap();
211                include("class.imap_attachment.inc.php");
212                $imap_attachment = new imap_attachment();
213                $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number);
214               
215                foreach($attachments as $i => $attachment){
216                        if($i && $i == 'names')
217                                continue;
218                                                                                                                                               
219                       
220                        $fileName = $this->remove_accents($attachment['name']);
221                        $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb");
222                        if(!$f)
223                                return False;                   
224                                               
225                        $fileNames .= "'".$fileName."' ";
226                        $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID);
227                        if($attachment['encoding'] == 'base64')
228                                fputs($f,imap_base64($fileContent));
229                        else           
230                                fputs($f,$fileContent);
231                               
232                        fclose($f);
233               
234                }
235                imap_close($this->mbox_stream);
236                $nameFileZip = '';
237               
238                if($fileNames) {
239                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir);                                         
240                        if($nameFileZip)
241                                $file =  $tempDir . '/'.$tempSubDir.'/'.$nameFileZip;
242                        else {
243                                $file = false;
244                        }
245                }
246                else
247                        $file = false;
248                               
249                return $file;
250        }
251
252        function getHeader($msg_number){                       
253                return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID);
254        }
255       
256        function getBody($msg_number){
257                $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255);
258                $body = imap_body($this->mbox_stream, $msg_number, FT_UID);
259                if(($header->Unseen == 'U') || ($header->Recent == 'N')){
260                        imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID);
261                }
262                return $body;
263        }
264
265        function decode_subject($string){
266                if ((strpos(strtolower($string), '=?iso-8859-1') !== false)
267                        || (strpos(strtolower($string), '=?windows-1252') !== false)){
268                        $elements = imap_mime_header_decode($string);
269                        foreach ($elements as $el)
270                                $return .= $el->text;
271                }
272                else if (strpos(strtolower($string), '=?utf-8') !== false) {
273                        $elements = imap_mime_header_decode($string);
274                        foreach ($elements as $el){
275                                $charset = $el->charset;
276                                $text    = $el->text;
277                                if(!strcasecmp($charset, "utf-8") ||
278                                !strcasecmp($charset, "utf-7")) {
279                                $text = iconv($charset, "ISO-8859-1", $text);
280                        }
281                        $return .= $text;
282                        }
283                }
284                else
285                        $return = $string;
286
287                return $this->remove_accents($return);         
288        }
289}
290// END CLASS
291?>
Note: See TracBrowser for help on using the repository browser.