1 | <?php |
---|
2 | /***************************************************************************************\ |
---|
3 | * Export EML Format Message Mail * |
---|
4 | * Written by Nilton Neto (Celepar) <niltonneto@celepar.pr.gov.br> * |
---|
5 | * ------------------------------------------------------------------------------------ * |
---|
6 | * This program is free software; you can redistribute it and/or modify it * |
---|
7 | * under the terms of the GNU General Public License as published by the * |
---|
8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
9 | * option) any later version. * |
---|
10 | \****************************************************************************************/ |
---|
11 | // BEGIN CLASS |
---|
12 | class ExportEml |
---|
13 | { |
---|
14 | var $msg; |
---|
15 | var $folder; |
---|
16 | var $mbox_stream; |
---|
17 | |
---|
18 | function connectImap(){ |
---|
19 | |
---|
20 | $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
21 | $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
22 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
23 | $imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
24 | |
---|
25 | if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') |
---|
26 | { |
---|
27 | $imap_options = '/tls/novalidate-cert'; |
---|
28 | } |
---|
29 | else |
---|
30 | { |
---|
31 | $imap_options = '/notls/novalidate-cert'; |
---|
32 | } |
---|
33 | $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password); |
---|
34 | } |
---|
35 | |
---|
36 | //export message to EML Format |
---|
37 | function parseEml($header, $body) |
---|
38 | { |
---|
39 | $sEmailHeader = $header; |
---|
40 | $sEmailBody = $body; |
---|
41 | $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody; |
---|
42 | return $sEMail; |
---|
43 | } |
---|
44 | |
---|
45 | // create EML File. |
---|
46 | function createFileEml($sEMLData, $tempDir) |
---|
47 | { |
---|
48 | $file = "email_".md5(microtime()).".eml"; |
---|
49 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
50 | |
---|
51 | if(!$f) |
---|
52 | return False; |
---|
53 | |
---|
54 | fputs($f,$sEMLData); |
---|
55 | fclose($f); |
---|
56 | |
---|
57 | return $file; |
---|
58 | } |
---|
59 | |
---|
60 | function createFileZip($files, $tempDir){ |
---|
61 | |
---|
62 | $tmp_zip_filename = "email_".md5(microtime()).".zip"; |
---|
63 | $command = "cd " . $tempDir . "; nice zip -m " . $tmp_zip_filename . " " . $files; |
---|
64 | if(!exec($command)) { |
---|
65 | $command = 'cd ' . $tempDir . '; rm '.$files; |
---|
66 | exec($command); |
---|
67 | return null; |
---|
68 | } |
---|
69 | |
---|
70 | return $tmp_zip_filename; |
---|
71 | |
---|
72 | } |
---|
73 | |
---|
74 | function export_all($params){ |
---|
75 | |
---|
76 | $this->folder = $params['folder']; |
---|
77 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
78 | $fileNames = ""; |
---|
79 | $tempDir = ini_get("session.save_path"); |
---|
80 | $this->connectImap(); |
---|
81 | |
---|
82 | $msgs = imap_search($this->mbox_stream,"ALL",SE_UID); |
---|
83 | foreach($msgs as $nMsgs){ |
---|
84 | $header = $this-> getHeader($nMsgs); |
---|
85 | $body = $this-> getBody($nMsgs); |
---|
86 | $sEMLData = $this -> parseEml($header, $body); |
---|
87 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir); |
---|
88 | if(!$fileName) { |
---|
89 | $error = True; |
---|
90 | break; |
---|
91 | } |
---|
92 | else |
---|
93 | $fileNames .= $fileName.' '; |
---|
94 | |
---|
95 | } |
---|
96 | imap_close($this->mbox_stream); |
---|
97 | |
---|
98 | $nameFileZip = 'False'; |
---|
99 | if($fileNames && !$error) { |
---|
100 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
101 | if($nameFileZip) |
---|
102 | $file = $tempDir.'/'.$nameFileZip; |
---|
103 | else { |
---|
104 | $file = false; |
---|
105 | } |
---|
106 | } |
---|
107 | else |
---|
108 | $file = false; |
---|
109 | |
---|
110 | return $file; |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | function makeAll($params) { |
---|
115 | |
---|
116 | $this-> folder = $params['folder']; |
---|
117 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
118 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
119 | $error = False; |
---|
120 | $fileNames = ""; |
---|
121 | $tempDir = ini_get("session.save_path"); |
---|
122 | $this->connectImap(); |
---|
123 | |
---|
124 | for($i = 0; $i < count($array_ids); $i++) { |
---|
125 | |
---|
126 | $header = $this-> getHeader($array_ids[$i]); |
---|
127 | $body = $this-> getBody($array_ids[$i]); |
---|
128 | $sEMLData = $this -> parseEml($header, $body); |
---|
129 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir); |
---|
130 | |
---|
131 | if(!$fileName) { |
---|
132 | $error = True; |
---|
133 | break; |
---|
134 | } |
---|
135 | else |
---|
136 | $fileNames .= $fileName.' '; |
---|
137 | |
---|
138 | } |
---|
139 | imap_close($this->mbox_stream); |
---|
140 | |
---|
141 | |
---|
142 | $nameFileZip = 'False'; |
---|
143 | if($fileNames && !$error) { |
---|
144 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
145 | if($nameFileZip) |
---|
146 | $file = $tempDir.'/'.$nameFileZip; |
---|
147 | else { |
---|
148 | $file = false; |
---|
149 | } |
---|
150 | } |
---|
151 | else |
---|
152 | $file = false; |
---|
153 | |
---|
154 | return $file; |
---|
155 | } |
---|
156 | |
---|
157 | function export_msg($params) { |
---|
158 | $this-> folder = $params['folder']; |
---|
159 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
160 | $id_number = $params['msgs_to_export']; |
---|
161 | $tempDir = ini_get("session.save_path"); |
---|
162 | |
---|
163 | $this->connectImap(); |
---|
164 | $header = $this-> getHeader($id_number); |
---|
165 | $body = $this-> getBody($id_number); |
---|
166 | |
---|
167 | $file = "source_".md5(microtime()).".txt"; |
---|
168 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
169 | fputs($f,$header ."\r\n\r\n". $body); |
---|
170 | fclose($f); |
---|
171 | |
---|
172 | imap_close($this->mbox_stream); |
---|
173 | return $tempDir.'/'.$file; |
---|
174 | } |
---|
175 | |
---|
176 | function remove_accents($string) { |
---|
177 | return strtr($string, |
---|
178 | "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ'\"", |
---|
179 | "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy__"); |
---|
180 | } |
---|
181 | |
---|
182 | function download_all_attachments($params) { |
---|
183 | |
---|
184 | $id_number = $params['num_msg']; |
---|
185 | $attachments =unserialize(rawurldecode($params['s_attachments'])); |
---|
186 | |
---|
187 | $tempDir = ini_get("session.save_path"); |
---|
188 | $tempSubDir = md5(microtime()); |
---|
189 | $fileNames = ''; |
---|
190 | exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir); |
---|
191 | $this-> folder = $params['folder']; |
---|
192 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
193 | $this->connectImap(); |
---|
194 | include("class.imap_attachment.inc.php"); |
---|
195 | $imap_attachment = new imap_attachment(); |
---|
196 | $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number); |
---|
197 | |
---|
198 | foreach($attachments as $i => $attachment){ |
---|
199 | if($i && $i == 'names') |
---|
200 | continue; |
---|
201 | |
---|
202 | |
---|
203 | $fileName = $this->remove_accents($attachment['name']); |
---|
204 | $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb"); |
---|
205 | if(!$f) |
---|
206 | return False; |
---|
207 | |
---|
208 | $fileNames .= "'".$fileName."' "; |
---|
209 | $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID); |
---|
210 | if($attachment['encoding'] == 'base64') |
---|
211 | fputs($f,imap_base64($fileContent)); |
---|
212 | else |
---|
213 | fputs($f,$fileContent); |
---|
214 | |
---|
215 | fclose($f); |
---|
216 | |
---|
217 | } |
---|
218 | imap_close($this->mbox_stream); |
---|
219 | $nameFileZip = ''; |
---|
220 | |
---|
221 | if($fileNames) { |
---|
222 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir); |
---|
223 | if($nameFileZip) |
---|
224 | $file = $tempDir . '/'.$tempSubDir.'/'.$nameFileZip; |
---|
225 | else { |
---|
226 | $file = false; |
---|
227 | } |
---|
228 | } |
---|
229 | else |
---|
230 | $file = false; |
---|
231 | |
---|
232 | return $file; |
---|
233 | } |
---|
234 | |
---|
235 | function getHeader($msg_number){ |
---|
236 | return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID); |
---|
237 | } |
---|
238 | |
---|
239 | function getBody($msg_number){ |
---|
240 | return imap_body($this->mbox_stream, $msg_number, FT_UID); |
---|
241 | } |
---|
242 | |
---|
243 | } |
---|
244 | // END CLASS |
---|
245 | ?> |
---|