* * ------------------------------------------------------------------------------------ * * 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) { $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']; $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); 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']; $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); 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']; $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) { return strtr($string, "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ'\"", "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy__"); } 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->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){ return imap_body($this->mbox_stream, $msg_number, FT_UID); } } // END CLASS ?>