implemented EML download

This commit is contained in:
Synox 2017-01-20 22:51:23 +01:00
parent ff76697ead
commit bad498c662
2 changed files with 40 additions and 4 deletions

View File

@ -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 $username string username
* @param $address string email address * @param $address string email address
*/ */
@ -34,6 +34,7 @@ function print_emails($username, $address) {
$mail_ids = array_merge($mailsIdsTo, $mailsIdsCc); $mail_ids = array_merge($mailsIdsTo, $mailsIdsCc);
$emails = _load_emails($mail_ids, $address); $emails = _load_emails($mail_ids, $address);
header('Content-type: application/json');
print(json_encode(array("mails" => $emails, 'username' => $username, 'address' => $address))); print(json_encode(array("mails" => $emails, 'username' => $username, 'address' => $address)));
} }
@ -54,12 +55,42 @@ function delete_email($mailid, $address) {
if (count($emails) === 1) { if (count($emails) === 1) {
$mailbox->deleteMail($mailid); $mailbox->deleteMail($mailid);
$mailbox->expungeDeletedMails(); $mailbox->expungeDeletedMails();
header('Content-type: application/json');
print(json_encode(array("success" => true))); print(json_encode(array("success" => true)));
} else { } else {
error(404, 'delete error: invalid username/mailid combination'); 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. * Load emails using the $mail_ids, the mails have to match the $address in TO or CC.
* @param $mail_ids array of integer ids * @param $mail_ids array of integer ids
@ -107,8 +138,6 @@ function delete_old_messages() {
} }
header('Content-type: application/json');
// Never cache requests: // Never cache requests:
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false); header("Cache-Control: post-check=0, pre-check=0", false);
@ -124,7 +153,9 @@ if (isset($_GET['username'])) {
$address = $username . "@" . $config['mailHostname']; $address = $username . "@" . $config['mailHostname'];
// simple router: // 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); delete_email($_GET['delete_email_id'], $address);
} else { } else {
print_emails($username, $address); print_emails($username, $address);

View File

@ -74,6 +74,11 @@
<div class="row sticky-header" ec-stickyfill> <div class="row sticky-header" ec-stickyfill>
<div class="col-sm-12 email-summary">{{mail.subject}} <div class="col-sm-12 email-summary">{{mail.subject}}
<form class="form-inline float-xs-right"> <form class="form-inline float-xs-right">
<a role="button" class="btn btn-sm btn-outline-primary"
href="backend.php?download_email_id={{mail.id}}&username={{$ctrl.username}}"
download="true">Download
</a>
<button ng-click="$ctrl.deleteMail(mail.id)" type="button" <button ng-click="$ctrl.deleteMail(mail.id)" type="button"
class="btn btn-sm btn-outline-danger">Delete class="btn btn-sm btn-outline-danger">Delete
</button> </button>