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