[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; |
---|
[4348] | 17 | var $tempDir; |
---|
[2] | 18 | |
---|
[4348] | 19 | function ExportEml() { |
---|
| 20 | |
---|
| 21 | //TODO: modificar o caminho hardcodificado '/tmp' para o definido na configuracao do expresso |
---|
| 22 | //$this->tempDir = $GLOBALS['phpgw_info']['server']['temp_dir']; |
---|
| 23 | $this->tempDir = '/tmp'; |
---|
| 24 | } |
---|
| 25 | |
---|
[2] | 26 | function connectImap(){ |
---|
| 27 | |
---|
| 28 | $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
| 29 | $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
| 30 | $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
| 31 | $imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
| 32 | |
---|
| 33 | if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') |
---|
| 34 | { |
---|
| 35 | $imap_options = '/tls/novalidate-cert'; |
---|
| 36 | } |
---|
| 37 | else |
---|
| 38 | { |
---|
| 39 | $imap_options = '/notls/novalidate-cert'; |
---|
| 40 | } |
---|
| 41 | $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | //export message to EML Format |
---|
| 45 | function parseEml($header, $body) |
---|
| 46 | { |
---|
| 47 | $sEmailHeader = $header; |
---|
| 48 | $sEmailBody = $body; |
---|
| 49 | $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody; |
---|
| 50 | return $sEMail; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | // create EML File. |
---|
[1000] | 54 | // Funcao alterada para tratar a exportacao |
---|
| 55 | // de mensagens arquivadas localmente. |
---|
| 56 | // Rommel Cysne (rommel.cysne@serpro.gov.br) |
---|
| 57 | // em 17/12/2008. |
---|
[1377] | 58 | function createFileEml($sEMLData, $tempDir, $id, $subject=false, $i=false) |
---|
[177] | 59 | { |
---|
| 60 | if($id) |
---|
| 61 | { |
---|
| 62 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $id), 80, 255); |
---|
| 63 | $subject = $this->decode_subject($header->fetchsubject); |
---|
| 64 | |
---|
| 65 | if (strlen($subject) > 60) |
---|
| 66 | $subject = substr($subject, 0, 59); |
---|
| 67 | |
---|
[5934] | 68 | //$subject = preg_replace('/\//', '\'', $subject); |
---|
[4348] | 69 | $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; |
---|
| 70 | $to = "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; |
---|
[1000] | 71 | $subject = strtr($subject,$from,$to); |
---|
[4348] | 72 | |
---|
[5773] | 73 | $subject = preg_replace('/[^a-zA-Z0-9_]/i', '_', $subject); |
---|
[4348] | 74 | $file = $subject."_".$id.".eml"; |
---|
[1000] | 75 | } else{ |
---|
| 76 | // Se mensagem for arquivada localmente, $subject (assunto da mensagem) |
---|
| 77 | // sera passado para compor o nome do arquivo .eml; |
---|
| 78 | |
---|
[1377] | 79 | if($subject && $i){ |
---|
[4348] | 80 | $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; |
---|
| 81 | $to = "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; |
---|
[1000] | 82 | $subject = strtr($subject,$from,$to); |
---|
[4348] | 83 | |
---|
[5773] | 84 | $subject = preg_replace('/[^a-zA-Z0-9_]/i', '_', $subject); |
---|
[4348] | 85 | |
---|
| 86 | // é necessário que a sessão faça parte do nome do arquivo para que o mesmo não venha vazio o.O |
---|
| 87 | $file = $subject."_".$i."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; |
---|
[1000] | 88 | } else{ |
---|
[1247] | 89 | $file = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; |
---|
[1377] | 90 | } |
---|
[177] | 91 | } |
---|
| 92 | |
---|
| 93 | $f = fopen($tempDir.'/'.$file,"w"); |
---|
| 94 | if(!$f) |
---|
| 95 | return False; |
---|
| 96 | |
---|
| 97 | fputs($f,$sEMLData); |
---|
| 98 | fclose($f); |
---|
| 99 | |
---|
| 100 | return $file; |
---|
| 101 | } |
---|
[2] | 102 | |
---|
[64] | 103 | function createFileZip($files, $tempDir){ |
---|
[1247] | 104 | $tmp_zip_filename = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".zip"; |
---|
[3289] | 105 | |
---|
| 106 | if (!empty($files)) |
---|
| 107 | { |
---|
| 108 | if (is_array($files)) |
---|
| 109 | { |
---|
| 110 | for ($i=0; $i < count($files); $i++) |
---|
| 111 | { |
---|
| 112 | $files[$i] = escapeshellarg($files[$i]); |
---|
| 113 | } |
---|
| 114 | $files = implode(' ', $files); |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
| 118 | $files = escapeshellcmd($files); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | $command = "cd " . escapeshellarg($tempDir) . " && nice zip -m9 " . escapeshellarg($tmp_zip_filename) . " " . $files; |
---|
[2] | 123 | if(!exec($command)) { |
---|
[3289] | 124 | $command = "cd " . escapeshellarg($tempDir) . " && rm ".$files." ". escapeshellarg($tmp_zip_filename); |
---|
[2] | 125 | exec($command); |
---|
| 126 | return null; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return $tmp_zip_filename; |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | function export_all($params){ |
---|
| 134 | |
---|
| 135 | $this->folder = $params['folder']; |
---|
[51] | 136 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
[2] | 137 | $fileNames = ""; |
---|
[4348] | 138 | $tempDir = $this->tempDir; |
---|
[2] | 139 | $this->connectImap(); |
---|
| 140 | |
---|
| 141 | $msgs = imap_search($this->mbox_stream,"ALL",SE_UID); |
---|
[1558] | 142 | if($msgs){ |
---|
| 143 | foreach($msgs as $nMsgs){ |
---|
| 144 | $header = $this-> getHeader($nMsgs); |
---|
| 145 | $body = $this-> getBody($nMsgs); |
---|
| 146 | $sEMLData = $this -> parseEml($header, $body); |
---|
| 147 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs); |
---|
| 148 | if(!$fileName) { |
---|
| 149 | $error = True; |
---|
| 150 | break; |
---|
| 151 | } |
---|
| 152 | else |
---|
| 153 | $fileNames .= "\"".$fileName."\" "; |
---|
| 154 | |
---|
[2] | 155 | } |
---|
| 156 | |
---|
[1558] | 157 | imap_close($this->mbox_stream); |
---|
| 158 | |
---|
| 159 | $nameFileZip = 'False'; |
---|
| 160 | if($fileNames && !$error) { |
---|
| 161 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
| 162 | if($nameFileZip) |
---|
| 163 | $file = $tempDir.'/'.$nameFileZip; |
---|
| 164 | else { |
---|
| 165 | $file = false; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | else |
---|
[2] | 169 | $file = false; |
---|
[1558] | 170 | }else{ |
---|
| 171 | $file["empty_folder"] = true; |
---|
[2] | 172 | } |
---|
| 173 | return $file; |
---|
| 174 | |
---|
| 175 | } |
---|
| 176 | |
---|
[1000] | 177 | // Funcao alterada para tratar a exportacao |
---|
| 178 | // de mensagens arquivadas localmente. |
---|
| 179 | // Rommel Cysne (rommel.cysne@serpro.gov.br) |
---|
| 180 | // em 17/12/2008. |
---|
[4348] | 181 | // |
---|
| 182 | // Funcao alterada para que, quando houver |
---|
| 183 | // apenas um arquivo a ser exportado, |
---|
| 184 | // não seja criado em zip |
---|
[5172] | 185 | // |
---|
| 186 | // Funcao altarada para exportar uma ou |
---|
| 187 | // varia mensagens de um pesquisa |
---|
| 188 | |
---|
[2] | 189 | function makeAll($params) { |
---|
[5172] | 190 | //Exporta menssagens selecionadas na pesquisa |
---|
| 191 | if($params['folder'] === 'false'){ |
---|
| 192 | |
---|
| 193 | $this->folder = $params['folder']; |
---|
| 194 | $error = False; |
---|
| 195 | $fileNames = ""; |
---|
| 196 | |
---|
| 197 | $sel_msgs = explode(",", $params['msgs_to_export']); |
---|
| 198 | @reset($sel_msgs); |
---|
| 199 | $sorted_msgs = array(); |
---|
| 200 | foreach($sel_msgs as $idx => $sel_msg) { |
---|
| 201 | $sel_msg = explode(";", $sel_msg); |
---|
| 202 | if(array_key_exists($sel_msg[0], $sorted_msgs)){ |
---|
| 203 | $sorted_msgs[$sel_msg[0]] .= ",".$sel_msg[1]; |
---|
| 204 | } |
---|
| 205 | else { |
---|
| 206 | $sorted_msgs[$sel_msg[0]] = $sel_msg[1]; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | unset($sorted_msgs['']); |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | // Verifica se as n mensagens selecionadas |
---|
| 214 | // se encontram em um mesmo folder |
---|
| 215 | if (count($sorted_msgs)==1){ |
---|
| 216 | |
---|
| 217 | $array_names_keys = array_keys($sorted_msgs); |
---|
| 218 | $this->folder = mb_convert_encoding($array_names_keys[0], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
| 219 | $msg_number = explode(',', $sorted_msgs[$array_names_keys[0]]); |
---|
| 220 | $tempDir = $this->tempDir; |
---|
| 221 | $this->connectImap(); |
---|
| 222 | |
---|
| 223 | //verifica se apenas uma mensagem foi selecionada e exportar em .eml |
---|
| 224 | if(count($msg_number) == 1){ |
---|
| 225 | $header = $this->getHeader($msg_number[0]); |
---|
| 226 | $body = $this->getBody($msg_number[0]); |
---|
| 227 | $sEMLData = $this->parseEml($header, $body); |
---|
| 228 | $fileName = $this->CreateFileEml($sEMLData, $tempDir, $msg_number[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]); |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | imap_close($this->mbox_stream); |
---|
| 232 | if (!$fileName) { |
---|
| 233 | return false; |
---|
| 234 | }else{ |
---|
| 235 | return $tempDir.'/'.$fileName; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | //cria um .zip com as mensagens selecionadas |
---|
| 240 | for($i = 0; $i < count($msg_number); $i++) |
---|
| 241 | { |
---|
| 242 | $header = $this-> getHeader($msg_number[$i]); |
---|
| 243 | $body = $this-> getBody($msg_number[$i]); |
---|
| 244 | $sEMLData = $this -> parseEml($header, $body); |
---|
| 245 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $msg_number[$i]); |
---|
| 246 | |
---|
| 247 | if(!$fileName) |
---|
| 248 | { |
---|
| 249 | $error = True; |
---|
| 250 | break; |
---|
| 251 | } else{ |
---|
| 252 | $fileNames .= "\"".$fileName."\" "; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | imap_close($this->mbox_stream); |
---|
| 256 | |
---|
| 257 | $nameFileZip = 'False'; |
---|
| 258 | if($fileNames && !$error) |
---|
| 259 | { |
---|
| 260 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
| 261 | if($nameFileZip) |
---|
| 262 | { |
---|
| 263 | $file = $tempDir.'/'.$nameFileZip; |
---|
| 264 | } else { |
---|
| 265 | $file = false; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
| 270 | $file = false; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | return $file; |
---|
| 274 | |
---|
| 275 | //exporta mensagens de diferentes pastas |
---|
| 276 | }else{ |
---|
| 277 | $array_names_keys = array_keys($sorted_msgs); |
---|
| 278 | |
---|
| 279 | for($i = 0; $i < count($array_names_keys); $i++){ |
---|
| 280 | $this->folder = mb_convert_encoding($array_names_keys[$i], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
| 281 | $msg_number = explode(',', $sorted_msgs[$array_names_keys[$i]]); |
---|
| 282 | $tempDir = $this->tempDir; |
---|
| 283 | $this->connectImap(); |
---|
| 284 | |
---|
| 285 | for($j = 0; $j < count($msg_number); $j++) |
---|
| 286 | { |
---|
| 287 | $header = $this-> getHeader($msg_number[$j]); |
---|
| 288 | $body = $this-> getBody($msg_number[$j]); |
---|
| 289 | $sEMLData = $this -> parseEml($header, $body); |
---|
| 290 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $msg_number[$j]); |
---|
| 291 | |
---|
| 292 | if(!$fileName) |
---|
| 293 | { |
---|
| 294 | $error = True; |
---|
| 295 | break; |
---|
| 296 | } else{ |
---|
| 297 | $fileNames .= "\"".$fileName."\" "; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | imap_close($this->mbox_stream); |
---|
| 301 | } |
---|
| 302 | $nameFileZip = 'False'; |
---|
| 303 | if($fileNames && !$error) |
---|
| 304 | { |
---|
| 305 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
| 306 | if($nameFileZip) |
---|
| 307 | { |
---|
| 308 | $file = $tempDir.'/'.$nameFileZip; |
---|
| 309 | } else { |
---|
| 310 | $file = false; |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | else |
---|
| 314 | { |
---|
| 315 | $file = false; |
---|
| 316 | } |
---|
| 317 | return $file; |
---|
| 318 | } |
---|
| 319 | }else{ |
---|
[1000] | 320 | // Exportacao de mensagens arquivadas localmente |
---|
[4348] | 321 | if($params['l_msg'] == "t") |
---|
| 322 | { |
---|
[1000] | 323 | // Recebe todos os subjects e bodies das mensagens locais selecionadas para exportacao |
---|
| 324 | // e gera arrays com os conteudos separados; |
---|
| 325 | $array_mesgs = explode('@@',$params['mesgs']); |
---|
| 326 | $array_subjects = explode('@@',$params['subjects']); |
---|
| 327 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
[4348] | 328 | $tempDir = $this->tempDir; |
---|
| 329 | |
---|
[1000] | 330 | include_once("class.imap_functions.inc.php"); |
---|
| 331 | $imapf = new imap_functions(); |
---|
[4348] | 332 | |
---|
| 333 | // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip |
---|
| 334 | if (count($array_ids)==1) |
---|
| 335 | { |
---|
[4457] | 336 | $sEMLData=$imapf->treat_base64_from_post($array_mesgs[0]); |
---|
| 337 | $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[0],"offline"); |
---|
[4348] | 338 | return $tempDir.'/'.$fileName; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | // Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma; |
---|
[1000] | 342 | foreach($array_ids as $i=>$id) { |
---|
| 343 | $sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]); |
---|
| 344 | $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[$i],$i); |
---|
| 345 | if(!$fileName){ |
---|
| 346 | $error = True; |
---|
| 347 | break; |
---|
| 348 | } else{ |
---|
| 349 | $fileNames .= "\"".$fileName."\" "; |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | $nameFileZip = 'False'; |
---|
| 353 | if($fileNames && !$error) { |
---|
| 354 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
| 355 | if($nameFileZip){ |
---|
| 356 | $file = $tempDir.'/'.$nameFileZip; |
---|
| 357 | } else{ |
---|
| 358 | $file = false; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | } else{ |
---|
| 362 | $file = false; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | return $file; |
---|
[4348] | 366 | |
---|
| 367 | } else |
---|
| 368 | // Exportacao de mensagens da caixa de entrada (imap) - processo original do Expresso |
---|
| 369 | { |
---|
| 370 | $this-> folder = $params['folder']; |
---|
[5172] | 371 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
[4348] | 372 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
| 373 | $error = False; |
---|
| 374 | $fileNames = ""; |
---|
| 375 | $tempDir = $this->tempDir; |
---|
| 376 | $this->connectImap(); |
---|
[1000] | 377 | |
---|
[4348] | 378 | // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip |
---|
| 379 | if (count($array_ids)==1) |
---|
| 380 | { |
---|
| 381 | $header = $this->getHeader($array_ids[0]); |
---|
| 382 | $body = $this->getBody($array_ids[0]); |
---|
| 383 | $sEMLData = $this->parseEml($header, $body); |
---|
| 384 | $fileName = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]); |
---|
| 385 | |
---|
| 386 | imap_close($this->mbox_stream); |
---|
| 387 | if (!$fileName) { |
---|
| 388 | return false; |
---|
| 389 | } else { |
---|
| 390 | return $tempDir.'/'.$fileName; |
---|
| 391 | } |
---|
[2] | 392 | } |
---|
[4348] | 393 | |
---|
| 394 | for($i = 0; $i < count($array_ids); $i++) |
---|
| 395 | { |
---|
| 396 | $header = $this-> getHeader($array_ids[$i]); |
---|
| 397 | $body = $this-> getBody($array_ids[$i]); |
---|
| 398 | $sEMLData = $this -> parseEml($header, $body); |
---|
| 399 | $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]); |
---|
| 400 | |
---|
| 401 | if(!$fileName) |
---|
| 402 | { |
---|
| 403 | $error = True; |
---|
| 404 | break; |
---|
| 405 | } else { |
---|
| 406 | $fileNames .= "\"".$fileName."\" "; |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | imap_close($this->mbox_stream); |
---|
| 410 | |
---|
| 411 | $nameFileZip = 'False'; |
---|
| 412 | if($fileNames && !$error) |
---|
| 413 | { |
---|
| 414 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir); |
---|
| 415 | if($nameFileZip) |
---|
| 416 | { |
---|
| 417 | $file = $tempDir.'/'.$nameFileZip; |
---|
| 418 | } else { |
---|
| 419 | $file = false; |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | else |
---|
| 423 | { |
---|
[2] | 424 | $file = false; |
---|
[4348] | 425 | } |
---|
| 426 | |
---|
| 427 | return $file; |
---|
[2] | 428 | } |
---|
[1000] | 429 | } |
---|
[5172] | 430 | } |
---|
[2] | 431 | |
---|
[5134] | 432 | function export_eml( $params ){ |
---|
| 433 | |
---|
| 434 | return $this->export_msg_data( $params['msgs_to_export'], |
---|
| 435 | $params['folder'] ); |
---|
| 436 | } |
---|
| 437 | |
---|
[2] | 438 | function export_msg($params) { |
---|
[5181] | 439 | $this-> folder = $params['folder']; |
---|
| 440 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
| 441 | $array_ids = explode(',', $params['msgs_to_export']); |
---|
| 442 | $error = False; |
---|
| 443 | $fileNames = ""; |
---|
[4348] | 444 | $tempDir = $this->tempDir; |
---|
[5181] | 445 | $this->connectImap(); |
---|
| 446 | |
---|
| 447 | // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip |
---|
| 448 | if (count($array_ids)==1) |
---|
| 449 | { |
---|
| 450 | $header = $this->getHeader($array_ids[0]); |
---|
| 451 | $body = $this->getBody($array_ids[0]); |
---|
| 452 | $sEMLData = $this->parseEml($header, $body); |
---|
| 453 | $fileName = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]); |
---|
| 454 | |
---|
| 455 | imap_close($this->mbox_stream); |
---|
| 456 | if (!$fileName) { |
---|
| 457 | return false; |
---|
| 458 | } else { |
---|
| 459 | return $tempDir.'/'.$fileName; |
---|
| 460 | } |
---|
| 461 | } |
---|
[2] | 462 | } |
---|
| 463 | |
---|
[1035] | 464 | function export_msg_data($id_msg,$folder) { |
---|
| 465 | $this->folder = $folder; |
---|
[3273] | 466 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
[1035] | 467 | |
---|
| 468 | $this->connectImap(); |
---|
| 469 | $header = $this-> getHeader($id_msg); |
---|
| 470 | $body = $this-> getBody($id_msg); |
---|
| 471 | |
---|
| 472 | $msg_data = $header ."\r\n\r\n". $body; |
---|
| 473 | |
---|
| 474 | imap_close($this->mbox_stream); |
---|
| 475 | return $msg_data; |
---|
| 476 | } |
---|
| 477 | |
---|
[3240] | 478 | function export_to_archive($id_msg,$folder) { |
---|
[5134] | 479 | $this->folder = $folder; |
---|
[3256] | 480 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1"); |
---|
[4348] | 481 | $tempDir = $this->tempDir; |
---|
[3240] | 482 | |
---|
| 483 | $this->connectImap(); |
---|
| 484 | $header = $this-> getHeader($id_msg); |
---|
| 485 | $body = $this-> getBody($id_msg); |
---|
| 486 | |
---|
[5134] | 487 | $file = tempnam ($tempDir, 'source_#'.$id_msg); |
---|
| 488 | $file .= '.php'; |
---|
| 489 | $fileName = basename ($file); |
---|
| 490 | $f = fopen($file, "w"); |
---|
| 491 | fputs($f,$phpheader.$header ."\r\n\r\n". $body); |
---|
[3240] | 492 | fclose($f); |
---|
[5134] | 493 | $urlPath = 'tmpLclAtt/' . $fileName; |
---|
[3240] | 494 | |
---|
| 495 | imap_close($this->mbox_stream); |
---|
| 496 | return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt"; |
---|
[5134] | 497 | } |
---|
[3240] | 498 | |
---|
[5134] | 499 | function remove_accents($string) { |
---|
| 500 | /* |
---|
| 501 | $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "š", "&", "*", "(", ")", "-", "=", "+", "Ž", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç"); |
---|
| 502 | $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"); |
---|
| 503 | return str_replace( $array1, $array2, $string ); |
---|
| 504 | */ |
---|
| 505 | return strtr($string, |
---|
| 506 | "áàâãäéèêëíìîïóòôõöúùûüç?\"'!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ", |
---|
| 507 | "aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC"); |
---|
[1000] | 508 | } |
---|
[689] | 509 | |
---|
[5134] | 510 | function get_attachments_headers( $folder, $id_number ){ |
---|
[689] | 511 | |
---|
[5134] | 512 | $this->folder = mb_convert_encoding($folder, "UTF7-IMAP","UTF-8"); |
---|
[4416] | 513 | |
---|
[5134] | 514 | $return_attachments = array(); |
---|
[4416] | 515 | |
---|
[5134] | 516 | include_once("class.attachment.inc.php"); |
---|
[689] | 517 | |
---|
[5134] | 518 | $imap_attachment = new attachment(); |
---|
| 519 | $imap_attachment->setStructureFromMail( $folder, $id_number ); |
---|
| 520 | $attachments = $imap_attachment->getAttachmentsInfo(); |
---|
[689] | 521 | |
---|
| 522 | foreach($attachments as $i => $attachment){ |
---|
| 523 | |
---|
[5134] | 524 | $fileContent = $imap_attachment->getAttachment( $attachment['pid'] ); |
---|
[4416] | 525 | |
---|
[5134] | 526 | $headers = "<?php header('Content-Type: {$attachment['type']}'); |
---|
[4416] | 527 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
---|
| 528 | header('Pragma: public'); |
---|
| 529 | header('Expires: 0'); // set expiration time |
---|
[5134] | 530 | header('Content-Disposition: attachment; filename=\"{$attachment['name']}\"');\n |
---|
| 531 | echo '$fileContent';?>"; |
---|
[4416] | 532 | |
---|
[5134] | 533 | $return_attachments[ $attachment['name'] ] = array( "content" => $headers, "pid" => $attachment['pid'] ); |
---|
[689] | 534 | } |
---|
[5134] | 535 | |
---|
| 536 | return( $return_attachments ); |
---|
[3240] | 537 | } |
---|
[4416] | 538 | |
---|
[5134] | 539 | function get_attachments_in_array($params) { |
---|
| 540 | $return_attachments = array(); |
---|
[689] | 541 | |
---|
[5134] | 542 | $attachments = $this->get_attachments_headers( $params['folder'], $params['num_msg'] ); |
---|
| 543 | |
---|
| 544 | if( !empty( $attachments ) ) |
---|
| 545 | { |
---|
| 546 | foreach($attachments as $fileNameReal => $attachment){ |
---|
| 547 | |
---|
| 548 | array_push($return_attachments,array('name' => $fileNameReal, 'pid' =>$attachment['pid'], 'contentType' => $this->getFileType( $fileNameReal ) )); |
---|
[689] | 549 | } |
---|
[1000] | 550 | } |
---|
[3240] | 551 | |
---|
[5134] | 552 | return $return_attachments; |
---|
[3240] | 553 | |
---|
| 554 | } |
---|
[689] | 555 | |
---|
| 556 | private function getFileType($nameFile) { |
---|
| 557 | $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4)); |
---|
| 558 | $ContentType = "application/octet-stream"; |
---|
| 559 | if ($strFileType == ".asf") |
---|
| 560 | $ContentType = "video/x-ms-asf"; |
---|
| 561 | if ($strFileType == ".avi") |
---|
| 562 | $ContentType = "video/avi"; |
---|
| 563 | if ($strFileType == ".doc") |
---|
| 564 | $ContentType = "application/msword"; |
---|
| 565 | if ($strFileType == ".zip") |
---|
| 566 | $ContentType = "application/zip"; |
---|
| 567 | if ($strFileType == ".xls") |
---|
| 568 | $ContentType = "application/vnd.ms-excel"; |
---|
| 569 | if ($strFileType == ".gif") |
---|
| 570 | $ContentType = "image/gif"; |
---|
[1518] | 571 | if ($strFileType == ".png") |
---|
| 572 | $ContentType = "image/png"; |
---|
[689] | 573 | if ($strFileType == ".jpg" || $strFileType == "jpeg") |
---|
| 574 | $ContentType = "image/jpeg"; |
---|
| 575 | if ($strFileType == ".wav") |
---|
| 576 | $ContentType = "audio/wav"; |
---|
| 577 | if ($strFileType == ".mp3") |
---|
| 578 | $ContentType = "audio/mpeg3"; |
---|
| 579 | if ($strFileType == ".mpg" || $strFileType == "mpeg") |
---|
| 580 | $ContentType = "video/mpeg"; |
---|
| 581 | if ($strFileType == ".rtf") |
---|
| 582 | $ContentType = "application/rtf"; |
---|
| 583 | if ($strFileType == ".htm" || $strFileType == "html") |
---|
| 584 | $ContentType = "text/html"; |
---|
| 585 | if ($strFileType == ".xml") |
---|
| 586 | $ContentType = "text/xml"; |
---|
| 587 | if ($strFileType == ".xsl") |
---|
| 588 | $ContentType = "text/xsl"; |
---|
| 589 | if ($strFileType == ".css") |
---|
| 590 | $ContentType = "text/css"; |
---|
| 591 | if ($strFileType == ".php") |
---|
| 592 | $ContentType = "text/php"; |
---|
| 593 | if ($strFileType == ".asp") |
---|
| 594 | $ContentType = "text/asp"; |
---|
| 595 | if ($strFileType == ".pdf") |
---|
| 596 | $ContentType = "application/pdf"; |
---|
| 597 | if ($strFileType == ".txt") |
---|
| 598 | $ContentType = "text/plain"; |
---|
| 599 | if ($strFileType == ".log") |
---|
| 600 | $ContentType = "text/plain"; |
---|
| 601 | if ($strFileType == ".wmv") |
---|
| 602 | $ContentType = "video/x-ms-wmv"; |
---|
| 603 | if ($strFileType == ".sxc") |
---|
| 604 | $ContentType = "application/vnd.sun.xml.calc"; |
---|
| 605 | if ($strFileType == ".odt") |
---|
| 606 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
| 607 | if ($strFileType == ".stc") |
---|
| 608 | $ContentType = "application/vnd.sun.xml.calc.template"; |
---|
| 609 | if ($strFileType == ".sxd") |
---|
| 610 | $ContentType = "application/vnd.sun.xml.draw"; |
---|
| 611 | if ($strFileType == ".std") |
---|
| 612 | $ContentType = "application/vnd.sun.xml.draw.template"; |
---|
| 613 | if ($strFileType == ".sxi") |
---|
| 614 | $ContentType = "application/vnd.sun.xml.impress"; |
---|
| 615 | if ($strFileType == ".sti") |
---|
| 616 | $ContentType = "application/vnd.sun.xml.impress.template"; |
---|
| 617 | if ($strFileType == ".sxm") |
---|
| 618 | $ContentType = "application/vnd.sun.xml.math"; |
---|
| 619 | if ($strFileType == ".sxw") |
---|
| 620 | $ContentType = "application/vnd.sun.xml.writer"; |
---|
| 621 | if ($strFileType == ".sxq") |
---|
| 622 | $ContentType = "application/vnd.sun.xml.writer.global"; |
---|
| 623 | if ($strFileType == ".stw") |
---|
| 624 | $ContentType = "application/vnd.sun.xml.writer.template"; |
---|
| 625 | if ($strFileType == ".ps") |
---|
| 626 | $ContentType = "application/postscript"; |
---|
| 627 | if ($strFileType == ".pps") |
---|
| 628 | $ContentType = "application/vnd.ms-powerpoint"; |
---|
| 629 | if ($strFileType == ".odt") |
---|
| 630 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
| 631 | if ($strFileType == ".ott") |
---|
| 632 | $ContentType = "application/vnd.oasis.opendocument.text-template"; |
---|
| 633 | if ($strFileType == ".oth") |
---|
| 634 | $ContentType = "application/vnd.oasis.opendocument.text-web"; |
---|
| 635 | if ($strFileType == ".odm") |
---|
| 636 | $ContentType = "application/vnd.oasis.opendocument.text-master"; |
---|
| 637 | if ($strFileType == ".odg") |
---|
| 638 | $ContentType = "application/vnd.oasis.opendocument.graphics"; |
---|
| 639 | if ($strFileType == ".otg") |
---|
| 640 | $ContentType = "application/vnd.oasis.opendocument.graphics-template"; |
---|
| 641 | if ($strFileType == ".odp") |
---|
| 642 | $ContentType = "application/vnd.oasis.opendocument.presentation"; |
---|
| 643 | if ($strFileType == ".otp") |
---|
| 644 | $ContentType = "application/vnd.oasis.opendocument.presentation-template"; |
---|
| 645 | if ($strFileType == ".ods") |
---|
| 646 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet"; |
---|
| 647 | if ($strFileType == ".ots") |
---|
| 648 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template"; |
---|
| 649 | if ($strFileType == ".odc") |
---|
| 650 | $ContentType = "application/vnd.oasis.opendocument.chart"; |
---|
| 651 | if ($strFileType == ".odf") |
---|
| 652 | $ContentType = "application/vnd.oasis.opendocument.formula"; |
---|
| 653 | if ($strFileType == ".odi") |
---|
| 654 | $ContentType = "application/vnd.oasis.opendocument.image"; |
---|
| 655 | if ($strFileType == ".ndl") |
---|
| 656 | $ContentType = "application/vnd.lotus-notes"; |
---|
[1899] | 657 | if ($strFileType == ".eml") |
---|
| 658 | $ContentType = "text/plain"; |
---|
[5134] | 659 | if ($strFileType == ".png") |
---|
| 660 | $ContentType = "image/png"; |
---|
[689] | 661 | return $ContentType; |
---|
| 662 | } |
---|
| 663 | |
---|
[2] | 664 | function download_all_attachments($params) { |
---|
| 665 | |
---|
[5316] | 666 | require_once dirname(__FILE__).'/class.attachment.inc.php'; |
---|
[5134] | 667 | $atObj = new attachment(); |
---|
| 668 | $atObj->setStructureFromMail($params['folder'],$params['num_msg']); |
---|
| 669 | $attachments = $atObj->getAttachmentsInfo(); |
---|
[2] | 670 | $id_number = $params['num_msg']; |
---|
[4348] | 671 | $tempDir = $this->tempDir; |
---|
[1597] | 672 | $tempSubDir = $_SESSION['phpgw_session']['session_id']; |
---|
[2] | 673 | $fileNames = ''; |
---|
| 674 | exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir); |
---|
| 675 | $this-> folder = $params['folder']; |
---|
[51] | 676 | $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); |
---|
[3289] | 677 | |
---|
| 678 | $fileNames = Array(); |
---|
[2] | 679 | |
---|
[3289] | 680 | for ($i = 0; $i < count($attachments); $i++) |
---|
| 681 | { |
---|
| 682 | $attachments[$i]['name'] = $this->remove_accents($attachments[$i]['name']); |
---|
| 683 | $fileNames[$i] = $attachments[$i]['name']; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | for ($i = 0; $i < count($attachments); $i++) |
---|
| 687 | { |
---|
| 688 | $fileName = $attachments[$i]['name']; |
---|
| 689 | $result = array_keys($fileNames, $fileName); |
---|
| 690 | |
---|
| 691 | // Detecta duplicatas |
---|
| 692 | if (count($result) > 1) |
---|
| 693 | { |
---|
| 694 | for ($j = 1; $j < count($result); $j++) |
---|
| 695 | { |
---|
| 696 | $replacement = '('.$j.')$0'; |
---|
| 697 | if (preg_match('/\.\w{2,4}$/', $fileName)) |
---|
| 698 | { |
---|
| 699 | $fileNames[$result[$j]] = preg_replace('/\.\w{2,4}$/', $replacement, $fileName); |
---|
| 700 | } |
---|
| 701 | else |
---|
| 702 | { |
---|
| 703 | $fileNames[$result[$j]] .= "($j)"; |
---|
| 704 | } |
---|
| 705 | $attachments[$result[$j]]['name'] = $fileNames[$result[$j]]; |
---|
| 706 | } |
---|
| 707 | } |
---|
| 708 | // Fim detecta duplicatas |
---|
| 709 | |
---|
[2] | 710 | $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb"); |
---|
| 711 | if(!$f) |
---|
| 712 | return False; |
---|
[5134] | 713 | $fileContent = $atObj->getAttachment( $attachments[$i]['pid'] ); |
---|
[2] | 714 | fputs($f,$fileContent); |
---|
| 715 | |
---|
| 716 | fclose($f); |
---|
| 717 | |
---|
| 718 | } |
---|
| 719 | imap_close($this->mbox_stream); |
---|
| 720 | $nameFileZip = ''; |
---|
| 721 | |
---|
[3289] | 722 | if(!empty($fileNames)) { |
---|
[2] | 723 | $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir); |
---|
| 724 | if($nameFileZip) |
---|
| 725 | $file = $tempDir . '/'.$tempSubDir.'/'.$nameFileZip; |
---|
| 726 | else { |
---|
| 727 | $file = false; |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | else |
---|
[1597] | 731 | $file = false; |
---|
[2] | 732 | return $file; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | function getHeader($msg_number){ |
---|
| 736 | return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID); |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | function getBody($msg_number){ |
---|
[592] | 740 | $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255); |
---|
| 741 | $body = imap_body($this->mbox_stream, $msg_number, FT_UID); |
---|
| 742 | if(($header->Unseen == 'U') || ($header->Recent == 'N')){ |
---|
| 743 | imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID); |
---|
| 744 | } |
---|
| 745 | return $body; |
---|
[2] | 746 | } |
---|
| 747 | |
---|
[64] | 748 | function decode_subject($string){ |
---|
| 749 | if ((strpos(strtolower($string), '=?iso-8859-1') !== false) |
---|
| 750 | || (strpos(strtolower($string), '=?windows-1252') !== false)){ |
---|
| 751 | $elements = imap_mime_header_decode($string); |
---|
| 752 | foreach ($elements as $el) |
---|
| 753 | $return .= $el->text; |
---|
| 754 | } |
---|
| 755 | else if (strpos(strtolower($string), '=?utf-8') !== false) { |
---|
| 756 | $elements = imap_mime_header_decode($string); |
---|
| 757 | foreach ($elements as $el){ |
---|
| 758 | $charset = $el->charset; |
---|
| 759 | $text = $el->text; |
---|
| 760 | if(!strcasecmp($charset, "utf-8") || |
---|
| 761 | !strcasecmp($charset, "utf-7")) { |
---|
| 762 | $text = iconv($charset, "ISO-8859-1", $text); |
---|
| 763 | } |
---|
| 764 | $return .= $text; |
---|
| 765 | } |
---|
| 766 | } |
---|
| 767 | else |
---|
| 768 | $return = $string; |
---|
| 769 | |
---|
| 770 | return $this->remove_accents($return); |
---|
| 771 | } |
---|
[2] | 772 | } |
---|
| 773 | // END CLASS |
---|
[689] | 774 | ?> |
---|