* * ------------------------------------------------------------------------------------ * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * \****************************************************************************************/ // BEGIN CLASS class ExportEml { var $msg; var $folder; var $mbox_stream; function connectImap(){ $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; $imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') { $imap_options = '/tls/novalidate-cert'; } else { $imap_options = '/notls/novalidate-cert'; } $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password); } //export message to EML Format function parseEml($header, $body) { $sEmailHeader = $header; $sEmailBody = $body; $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody; return $sEMail; } // create EML File. function createFileEml($sEMLData, $tempDir, $id) { if($id) { $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $id), 80, 255); $subject = $this->decode_subject($header->fetchsubject); if (strlen($subject) > 60) $subject = substr($subject, 0, 59); $subject = ereg_replace('/', '\'', $subject); $file = $subject."_".$id.".eml"; } else{ $file = "email_".md5(microtime()).".eml"; } $f = fopen($tempDir.'/'.$file,"w"); if(!$f) return False; fputs($f,$sEMLData); fclose($f); return $file; } function createFileZip($files, $tempDir){ $tmp_zip_filename = "email_".md5(microtime()).".zip"; $command = "cd " . $tempDir . "; nice zip -m " . $tmp_zip_filename . " " . $files; if(!exec($command)) { $command = 'cd ' . $tempDir . '; rm '.$files; exec($command); return null; } return $tmp_zip_filename; } function export_all($params){ $this->folder = $params['folder']; $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); $fileNames = ""; $tempDir = ini_get("session.save_path"); $this->connectImap(); $msgs = imap_search($this->mbox_stream,"ALL",SE_UID); foreach($msgs as $nMsgs){ $header = $this-> getHeader($nMsgs); $body = $this-> getBody($nMsgs); $sEMLData = $this -> parseEml($header, $body); $fileName = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs); if(!$fileName) { $error = True; break; } else $fileNames .= "\"".$fileName."\" "; } imap_close($this->mbox_stream); $nameFileZip = 'False'; if($fileNames && !$error) { $nameFileZip = $this -> createFileZip($fileNames, $tempDir); if($nameFileZip) $file = $tempDir.'/'.$nameFileZip; else { $file = false; } } else $file = false; return $file; } function makeAll($params) { $this-> folder = $params['folder']; $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); $array_ids = explode(',', $params['msgs_to_export']); $error = False; $fileNames = ""; $tempDir = ini_get("session.save_path"); $this->connectImap(); for($i = 0; $i < count($array_ids); $i++) { $header = $this-> getHeader($array_ids[$i]); $body = $this-> getBody($array_ids[$i]); $sEMLData = $this -> parseEml($header, $body); $fileName = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]); if(!$fileName) { $error = True; break; } else $fileNames .= "\"".$fileName."\" "; } imap_close($this->mbox_stream); $nameFileZip = 'False'; if($fileNames && !$error) { $nameFileZip = $this -> createFileZip($fileNames, $tempDir); if($nameFileZip) $file = $tempDir.'/'.$nameFileZip; else { $file = false; } } else $file = false; return $file; } function export_msg($params) { $this-> folder = $params['folder']; $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); $id_number = $params['msgs_to_export']; $tempDir = ini_get("session.save_path"); $this->connectImap(); $header = $this-> getHeader($id_number); $body = $this-> getBody($id_number); $file = "source_".md5(microtime()).".txt"; $f = fopen($tempDir.'/'.$file,"w"); fputs($f,$header ."\r\n\r\n". $body); fclose($f); imap_close($this->mbox_stream); return $tempDir.'/'.$file; } function remove_accents($string) { /* $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "¨", "&", "*", "(", ")", "-", "=", "+", "´", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç"); $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"); return str_replace( $array1, $array2, $string ); */ return strtr($string, "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%¨&*()-=+´`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ", "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC"); } function download_all_attachments($params) { $id_number = $params['num_msg']; $attachments =unserialize(rawurldecode($params['s_attachments'])); $tempDir = ini_get("session.save_path"); $tempSubDir = md5(microtime()); $fileNames = ''; exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir); $this-> folder = $params['folder']; $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); $this->connectImap(); include("class.imap_attachment.inc.php"); $imap_attachment = new imap_attachment(); $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number); foreach($attachments as $i => $attachment){ if($i && $i == 'names') continue; $fileName = $this->remove_accents($attachment['name']); $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb"); if(!$f) return False; $fileNames .= "'".$fileName."' "; $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID); if($attachment['encoding'] == 'base64') fputs($f,imap_base64($fileContent)); else fputs($f,$fileContent); fclose($f); } imap_close($this->mbox_stream); $nameFileZip = ''; if($fileNames) { $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir); if($nameFileZip) $file = $tempDir . '/'.$tempSubDir.'/'.$nameFileZip; else { $file = false; } } else $file = false; return $file; } function getHeader($msg_number){ return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID); } function getBody($msg_number){ $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255); $body = imap_body($this->mbox_stream, $msg_number, FT_UID); if(($header->Unseen == 'U') || ($header->Recent == 'N')){ imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID); } return $body; } function decode_subject($string){ if ((strpos(strtolower($string), '=?iso-8859-1') !== false) || (strpos(strtolower($string), '=?windows-1252') !== false)){ $elements = imap_mime_header_decode($string); foreach ($elements as $el) $return .= $el->text; } else if (strpos(strtolower($string), '=?utf-8') !== false) { $elements = imap_mime_header_decode($string); foreach ($elements as $el){ $charset = $el->charset; $text = $el->text; if(!strcasecmp($charset, "utf-8") || !strcasecmp($charset, "utf-7")) { $text = iconv($charset, "ISO-8859-1", $text); } $return .= $text; } } else $return = $string; return $this->remove_accents($return); } } // END CLASS ?>