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

Revision 64, 7.6 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

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