* * ------------------------------------------------------------------------------------ * * 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. // Funcao alterada para tratar a exportacao // de mensagens arquivadas localmente. // Rommel Cysne (rommel.cysne@serpro.gov.br) // em 17/12/2008. function createFileEml($sEMLData, $tempDir, $id, $subject=false, $i) { 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); $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%¨&*()-=+´`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; $to = "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; $subject = strtr($subject,$from,$to); $file = $subject."_".$id.".eml"; } else{ // Se mensagem for arquivada localmente, $subject (assunto da mensagem) // sera passado para compor o nome do arquivo .eml; if($subject){ $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%¨&*()-=+´`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ"; $to = "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC"; $subject = strtr($subject,$from,$to); //$subject = ereg_replace(':', '_', $subject); //$subject = ereg_replace('/', '_', $subject); $file = $subject."_".$i.".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; } // Funcao alterada para tratar a exportacao // de mensagens arquivadas localmente. // Rommel Cysne (rommel.cysne@serpro.gov.br) // em 17/12/2008. function makeAll($params) { // Exportacao de mensagens arquivadas localmente if($params['l_msg'] == "t"){ // Recebe todos os subjects e bodies das mensagens locais selecionadas para exportacao // e gera arrays com os conteudos separados; $array_mesgs = explode('@@',$params['mesgs']); $array_subjects = explode('@@',$params['subjects']); $array_ids = explode(',', $params['msgs_to_export']); $tempDir = ini_get("session.save_path"); // Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma; include_once("class.imap_functions.inc.php"); $imapf = new imap_functions(); foreach($array_ids as $i=>$id) { $sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]); $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[$i],$i); if(!$fileName){ $error = True; break; } else{ $fileNames .= "\"".$fileName."\" "; } } $nameFileZip = 'False'; if($fileNames && !$error) { $nameFileZip = $this -> createFileZip($fileNames, $tempDir); if($nameFileZip){ $file = $tempDir.'/'.$nameFileZip; } else{ $file = false; } } else{ $file = false; } return $file; // Exportacao de mensagens da caixa de entrada (imap) - processo original do Expresso } else{ $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 export_msg_data($id_msg,$folder) { $this->folder = $folder; $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); $this->connectImap(); $header = $this-> getHeader($id_msg); $body = $this-> getBody($id_msg); $msg_data = $header ."\r\n\r\n". $body; imap_close($this->mbox_stream); return $msg_data; } function export_to_archive($id_msg,$folder) { $this->folder = $folder; $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8"); $tempDir = dirname( __FILE__ ) . '/../tmpLclAtt'; $phpheader = ""; $this->connectImap(); $header = $this-> getHeader($id_msg); $body = $this-> getBody($id_msg); $file = "source_".md5(microtime()).".php"; $f = fopen($tempDir.'/'.$file,"w"); fputs($f,$phpheader.$header ."\r\n\r\n". $body); fclose($f); $urlPath = 'tmpLclAtt/'.$file; imap_close($this->mbox_stream); return $urlPath; } 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 get_attachments_in_array($params) { $return_attachments = array(); $id_number = $params['num_msg']; $attachments =unserialize(rawurldecode($params['s_attachments'])); $tempDir = dirname( __FILE__ ) . '/../tmpLclAtt'; // $tempDir = $_SERVER["DOCUMENT_ROOT"]."tmpLclAtt"; $tempSubDir = md5(microtime()); 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_once("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; $fileNameReal = $this->remove_accents($attachment['name']); $ContentType = $this->getFileType($fileNameReal); $fileName = $fileNameReal . ".php"; $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb"); if(!$f) return $tempDir . '/'.$tempSubDir.'/'.$fileName; $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID); $urlPath = 'tmpLclAtt/'.$tempSubDir.'/'.$fileName; $headers = ""; } else if($attachment['encoding'] == 'quoted_printable_decode') { $headers.=" quoted_printable_decode('".$fileContent."');?>"; } else { $headers.=" '".$fileContent."';?>"; } fputs($f,$headers); array_push($return_attachments,array('name' => $fileName,'url' => $urlPath,'pid' =>$attachment['pid'])); fclose($f); } imap_close($this->mbox_stream); return $return_attachments; } private function getFileType($nameFile) { $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4)); $ContentType = "application/octet-stream"; if ($strFileType == ".asf") $ContentType = "video/x-ms-asf"; if ($strFileType == ".avi") $ContentType = "video/avi"; if ($strFileType == ".doc") $ContentType = "application/msword"; if ($strFileType == ".zip") $ContentType = "application/zip"; if ($strFileType == ".xls") $ContentType = "application/vnd.ms-excel"; if ($strFileType == ".gif") $ContentType = "image/gif"; if ($strFileType == ".jpg" || $strFileType == "jpeg") $ContentType = "image/jpeg"; if ($strFileType == ".wav") $ContentType = "audio/wav"; if ($strFileType == ".mp3") $ContentType = "audio/mpeg3"; if ($strFileType == ".mpg" || $strFileType == "mpeg") $ContentType = "video/mpeg"; if ($strFileType == ".rtf") $ContentType = "application/rtf"; if ($strFileType == ".htm" || $strFileType == "html") $ContentType = "text/html"; if ($strFileType == ".xml") $ContentType = "text/xml"; if ($strFileType == ".xsl") $ContentType = "text/xsl"; if ($strFileType == ".css") $ContentType = "text/css"; if ($strFileType == ".php") $ContentType = "text/php"; if ($strFileType == ".asp") $ContentType = "text/asp"; if ($strFileType == ".pdf") $ContentType = "application/pdf"; if ($strFileType == ".txt") $ContentType = "text/plain"; if ($strFileType == ".log") $ContentType = "text/plain"; if ($strFileType == ".wmv") $ContentType = "video/x-ms-wmv"; if ($strFileType == ".sxc") $ContentType = "application/vnd.sun.xml.calc"; if ($strFileType == ".odt") $ContentType = "application/vnd.oasis.opendocument.text"; if ($strFileType == ".stc") $ContentType = "application/vnd.sun.xml.calc.template"; if ($strFileType == ".sxd") $ContentType = "application/vnd.sun.xml.draw"; if ($strFileType == ".std") $ContentType = "application/vnd.sun.xml.draw.template"; if ($strFileType == ".sxi") $ContentType = "application/vnd.sun.xml.impress"; if ($strFileType == ".sti") $ContentType = "application/vnd.sun.xml.impress.template"; if ($strFileType == ".sxm") $ContentType = "application/vnd.sun.xml.math"; if ($strFileType == ".sxw") $ContentType = "application/vnd.sun.xml.writer"; if ($strFileType == ".sxq") $ContentType = "application/vnd.sun.xml.writer.global"; if ($strFileType == ".stw") $ContentType = "application/vnd.sun.xml.writer.template"; if ($strFileType == ".ps") $ContentType = "application/postscript"; if ($strFileType == ".pps") $ContentType = "application/vnd.ms-powerpoint"; if ($strFileType == ".odt") $ContentType = "application/vnd.oasis.opendocument.text"; if ($strFileType == ".ott") $ContentType = "application/vnd.oasis.opendocument.text-template"; if ($strFileType == ".oth") $ContentType = "application/vnd.oasis.opendocument.text-web"; if ($strFileType == ".odm") $ContentType = "application/vnd.oasis.opendocument.text-master"; if ($strFileType == ".odg") $ContentType = "application/vnd.oasis.opendocument.graphics"; if ($strFileType == ".otg") $ContentType = "application/vnd.oasis.opendocument.graphics-template"; if ($strFileType == ".odp") $ContentType = "application/vnd.oasis.opendocument.presentation"; if ($strFileType == ".otp") $ContentType = "application/vnd.oasis.opendocument.presentation-template"; if ($strFileType == ".ods") $ContentType = "application/vnd.oasis.opendocument.spreadsheet"; if ($strFileType == ".ots") $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template"; if ($strFileType == ".odc") $ContentType = "application/vnd.oasis.opendocument.chart"; if ($strFileType == ".odf") $ContentType = "application/vnd.oasis.opendocument.formula"; if ($strFileType == ".odi") $ContentType = "application/vnd.oasis.opendocument.image"; if ($strFileType == ".ndl") $ContentType = "application/vnd.lotus-notes"; return $ContentType; } 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 ?>