From ecf2297a312045409f2e512e8193a04e04353ee1 Mon Sep 17 00:00:00 2001 From: Synox Date: Thu, 22 Dec 2016 21:22:34 +0100 Subject: [PATCH 01/23] REFACTOR: reduced lines of code --- src/backend.php | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/backend.php b/src/backend.php index 2212b57..a873db3 100644 --- a/src/backend.php +++ b/src/backend.php @@ -26,7 +26,13 @@ function error($status, $text) { * @param $address string email address */ function print_emails($username, $address) { - $mail_ids = _search_mails($address); + global $mailbox; + + // Search for mails with the recipient $address in TO or CC. + $mailsIdsTo = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, 'TO "' . $address . '"'); + $mailsIdsCc = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, 'CC "' . $address . '"'); + $mail_ids = array_merge($mailsIdsTo, $mailsIdsCc); + $emails = _load_emails($mail_ids, $address); print(json_encode(array("mails" => $emails, 'username' => $username, 'address' => $address))); } @@ -67,29 +73,13 @@ function _load_emails($mail_ids, $address) { foreach ($mail_ids as $id) { $mail = $mailbox->getMail($id); // imap_search also returns partials matches. The mails have to be filtered again: - if (!array_key_exists($address, $mail->to) && !array_key_exists($address, $mail->cc)) { - continue; + if (array_key_exists($address, $mail->to) || array_key_exists($address, $mail->cc)) { + $emails[] = $mail; } - $emails[] = $mail; } return $emails; } - -/** - * Search for mails with the recipient $address. - * @param $address string address that has to match TO or CC. - * @return array email ids - */ -function _search_mails($address) { - global $mailbox; - $filterTO = 'TO "' . $address . '"'; - $filterCC = 'CC "' . $address . '"'; - $mailsIdsTo = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, $filterTO); - $mailsIdsCc = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, $filterCC); - return array_merge($mailsIdsTo, $mailsIdsCc); -} - /** * Remove illegal characters from username and remove everything after the @-sign. You may extend it if your server supports them. * @param $username @@ -109,8 +99,7 @@ function _clean_username($username) { function delete_old_messages() { global $mailbox; - $date = date('d-M-Y', strtotime('30 days ago')); - $ids = $mailbox->searchMailbox('BEFORE ' . $date); + $ids = $mailbox->searchMailbox('BEFORE ' . date('d-M-Y', strtotime('30 days ago'))); foreach ($ids as $id) { $mailbox->deleteMail($id); } From 2859a7abb51e8cfd3319b566938822d0d062a8d0 Mon Sep 17 00:00:00 2001 From: Synox Date: Thu, 22 Dec 2016 21:34:43 +0100 Subject: [PATCH 02/23] REFACTOR: improved sample --- src/config.sample.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/config.sample.php b/src/config.sample.php index 97154c5..56f018a 100644 --- a/src/config.sample.php +++ b/src/config.sample.php @@ -12,9 +12,8 @@ error_reporting(E_ALL); // see https://en.wikipedia.org/wiki/Cross-origin_resource_sharing // header("Access-Control-Allow-Origin: *"); -// setup imap connection -$config['imap']['host'] = "localhost"; -$config['imap']['url'] = '{' . $config['imap']['host'] . '/imap/ssl}INBOX'; +// Change IMAP settings (check SSL flags on http://php.net/manual/en/function.imap-open.php) +$config['imap']['url'] = '{example.com/imap/ssl}INBOX'; $config['imap']['username'] = "test"; $config['imap']['password'] = "test"; From 8c1aec14c65893788b7c795e1b846f475a57c475 Mon Sep 17 00:00:00 2001 From: Synox Date: Thu, 22 Dec 2016 22:20:55 +0100 Subject: [PATCH 03/23] ignore temp patch files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2acd9c2..e810f26 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ .vagrant .idea target +*.patch \ No newline at end of file From 756015465fe9cba0524e46ebe51f3f5551fb6767 Mon Sep 17 00:00:00 2001 From: Synox Date: Thu, 22 Dec 2016 22:21:37 +0100 Subject: [PATCH 04/23] cleanup ignore list --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.gitignore b/.gitignore index e810f26..4514284 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,2 @@ -logs -node_modules/ -.vagrant .idea -target *.patch \ No newline at end of file From ff76697ead5cee4dfca8c2411d2d98f4d40a65a7 Mon Sep 17 00:00:00 2001 From: synox Date: Sat, 7 Jan 2017 22:16:57 +0100 Subject: [PATCH 05/23] copy content of source dir --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index cfb8556..1d9a3a8 100644 --- a/readme.md +++ b/readme.md @@ -32,7 +32,7 @@ 2. download a [release](https://github.com/synox/disposable-mailbox/releases) or clone this repository -3. copy the `src` directory to your web server. +3. copy the files in the `src` directory to your web server. 4. rename `config.sample.php` to `config.php` and apply the imap settings. Move `config.php` to a safe location outside the `public_html`. 5. edit `backend.php` and set the new path to `config.php`. From bad498c662b682b788d4a2163ba77f2ff14bedc5 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 20 Jan 2017 22:51:23 +0100 Subject: [PATCH 06/23] implemented EML download --- src/backend.php | 39 +++++++++++++++++++++++++++++++++++---- src/index.html | 5 +++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/backend.php b/src/backend.php index a873db3..ef9d0f1 100644 --- a/src/backend.php +++ b/src/backend.php @@ -21,7 +21,7 @@ function error($status, $text) { } /** - * print all mails for the given $user as a json string. + * print all mails for the given $user. * @param $username string username * @param $address string email address */ @@ -34,6 +34,7 @@ function print_emails($username, $address) { $mail_ids = array_merge($mailsIdsTo, $mailsIdsCc); $emails = _load_emails($mail_ids, $address); + header('Content-type: application/json'); print(json_encode(array("mails" => $emails, 'username' => $username, 'address' => $address))); } @@ -54,12 +55,42 @@ function delete_email($mailid, $address) { if (count($emails) === 1) { $mailbox->deleteMail($mailid); $mailbox->expungeDeletedMails(); + header('Content-type: application/json'); print(json_encode(array("success" => true))); } else { error(404, 'delete error: invalid username/mailid combination'); } } +/** + * download email by id and username. The $address must match the recipient in the email. + * + * @param $mailid integer imap email id (integer) + * @param $address string email address + * @internal param the $username matching username + */ + +function download_email($mailid, $address) { + global $mailbox; + + // in order to avoid https://www.owasp.org/index.php/Top_10_2013-A4-Insecure_Direct_Object_References + // the recipient in the email has to match the $address. + $emails = _load_emails(array($mailid), $address); + if (count($emails) === 1) { + + header("Content-Type: message/rfc822; charset=utf-8"); + header("Content-Disposition: attachment; filename=\"$address-$mailid.eml\""); + + $headers = imap_fetchheader($mailbox->getImapStream(), $mailid, FT_UID); + $body = imap_body($mailbox->getImapStream(), $mailid, FT_UID); + print ($headers . "\n" . $body); + + } else { + error(404, 'download error: invalid username/mailid combination'); + } +} + + /** * Load emails using the $mail_ids, the mails have to match the $address in TO or CC. * @param $mail_ids array of integer ids @@ -107,8 +138,6 @@ function delete_old_messages() { } -header('Content-type: application/json'); - // Never cache requests: header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); @@ -124,7 +153,9 @@ if (isset($_GET['username'])) { $address = $username . "@" . $config['mailHostname']; // simple router: - if (isset($_GET['delete_email_id'])) { + if (isset($_GET['download_email_id'])) { + download_email($_GET['download_email_id'], $address); + } else if (isset($_GET['delete_email_id'])) { delete_email($_GET['delete_email_id'], $address); } else { print_emails($username, $address); diff --git a/src/index.html b/src/index.html index 5bba59c..2014b3c 100644 --- a/src/index.html +++ b/src/index.html @@ -74,6 +74,11 @@