[2] | 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. |
---|
[177] | 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 | } |
---|
[2] | 72 | |
---|
[64] | 73 | function createFileZip($files, $tempDir){ |
---|
[2] | 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']; |
---|
[51] | 89 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
[2] | 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); |
---|
[64] | 99 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs); |
---|
[2] | 100 | if(!$fileName) { |
---|
| 101 | $error = True; |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | else |
---|
[64] | 105 | $fileNames .= "\"".$fileName."\" "; |
---|
[2] | 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']; |
---|
[51] | 129 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
[2] | 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 | |
---|
[64] | 138 | $header = $this-> getHeader($array_ids[$i]); |
---|
[2] | 139 | $body = $this-> getBody($array_ids[$i]); |
---|
[64] | 140 | $sEMLData = $this -> parseEml($header, $body); |
---|
| 141 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]); |
---|
[2] | 142 | |
---|
| 143 | if(!$fileName) { |
---|
| 144 | $error = True; |
---|
| 145 | break; |
---|
| 146 | } |
---|
| 147 | else |
---|
[64] | 148 | $fileNames .= "\"".$fileName."\" "; |
---|
[2] | 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']; |
---|
[51] | 171 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
[2] | 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) { |
---|
[177] | 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"); |
---|
[2] | 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']; |
---|
[51] | 209 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
[2] | 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 | return imap_body($this->mbox_stream, $msg_number, FT_UID); |
---|
| 258 | } |
---|
| 259 | |
---|
[64] | 260 | function decode_subject($string){ |
---|
| 261 | if ((strpos(strtolower($string), '=?iso-8859-1') !== false) |
---|
| 262 | || (strpos(strtolower($string), '=?windows-1252') !== false)){ |
---|
| 263 | $elements = imap_mime_header_decode($string); |
---|
| 264 | foreach ($elements as $el) |
---|
| 265 | $return .= $el->text; |
---|
| 266 | } |
---|
| 267 | else if (strpos(strtolower($string), '=?utf-8') !== false) { |
---|
| 268 | $elements = imap_mime_header_decode($string); |
---|
| 269 | foreach ($elements as $el){ |
---|
| 270 | $charset = $el->charset; |
---|
| 271 | $text = $el->text; |
---|
| 272 | if(!strcasecmp($charset, "utf-8") || |
---|
| 273 | !strcasecmp($charset, "utf-7")) { |
---|
| 274 | $text = iconv($charset, "ISO-8859-1", $text); |
---|
| 275 | } |
---|
| 276 | $return .= $text; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | $return = $string; |
---|
| 281 | |
---|
| 282 | return $this->remove_accents($return); |
---|
| 283 | } |
---|
[2] | 284 | } |
---|
| 285 | // END CLASS |
---|
[91] | 286 | ?> |
---|