2017-12-09 13:17:15 +01:00
|
|
|
<?php
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
|
2019-05-01 00:56:12 +02:00
|
|
|
|
2017-12-09 13:17:15 +01:00
|
|
|
function rrmdir($src) {
|
|
|
|
$dir = opendir($src);
|
|
|
|
while(false !== ( $file = readdir($dir)) ) {
|
|
|
|
if (( $file != '.' ) && ( $file != '..' )) {
|
|
|
|
$full = $src . '/' . $file;
|
|
|
|
if ( is_dir($full) ) {
|
|
|
|
rrmdir($full);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unlink($full);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
rmdir($src);
|
|
|
|
}
|
2020-06-06 13:25:32 +02:00
|
|
|
|
2019-05-01 00:56:12 +02:00
|
|
|
function addAddresses(&$list, $mail, $headerName) {
|
|
|
|
$addresses = $mail->getAddresses($headerName);
|
|
|
|
foreach ($addresses as $address) {
|
2020-06-06 14:13:46 +02:00
|
|
|
if (filter_var($address['address'], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$list[] = array('address' => $address['address'], 'type' => $headerName);
|
|
|
|
}
|
2019-05-01 00:56:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 13:25:32 +02:00
|
|
|
if (!empty($_GET['hash']) && ctype_alnum($_GET['hash'])) {
|
|
|
|
$mailc = quarantine('hash_details', $_GET['hash']);
|
|
|
|
if ($mailc === false) {
|
|
|
|
echo json_encode(array('error' => 'Message invalid'));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (strlen($mailc['msg']) > 10485760) {
|
|
|
|
echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (!empty($mailc['msg'])) {
|
|
|
|
// Init message array
|
|
|
|
$data = array();
|
|
|
|
// Init parser
|
|
|
|
$mail_parser = new PhpMimeMailParser\Parser();
|
|
|
|
$html2text = new Html2Text\Html2Text();
|
|
|
|
// Load msg to parser
|
|
|
|
$mail_parser->setText($mailc['msg']);
|
|
|
|
// Get mail recipients
|
|
|
|
{
|
|
|
|
$recipientsList = array();
|
|
|
|
addAddresses($recipientsList, $mail_parser, 'to');
|
|
|
|
addAddresses($recipientsList, $mail_parser, 'cc');
|
|
|
|
addAddresses($recipientsList, $mail_parser, 'bcc');
|
2020-06-07 10:45:40 +02:00
|
|
|
$recipientsList[] = array('address' => $mailc['rcpt'], 'type' => 'smtp');
|
2020-06-06 13:25:32 +02:00
|
|
|
$data['recipients'] = $recipientsList;
|
|
|
|
}
|
|
|
|
// Get from
|
|
|
|
$data['header_from'] = $mail_parser->getHeader('from');
|
|
|
|
$data['env_from'] = $mailc['sender'];
|
|
|
|
// Get rspamd score
|
|
|
|
$data['score'] = $mailc['score'];
|
2020-11-06 14:26:48 +01:00
|
|
|
// Get rspamd action
|
|
|
|
$data['action'] = $mailc['action'];
|
2020-06-06 13:25:32 +02:00
|
|
|
// Get rspamd symbols
|
|
|
|
$data['symbols'] = json_decode($mailc['symbols']);
|
2020-10-24 16:27:31 +02:00
|
|
|
// Get fuzzy hashes
|
|
|
|
$data['fuzzy_hashes'] = json_decode($mailc['fuzzy_hashes']);
|
2020-06-06 13:25:32 +02:00
|
|
|
$data['subject'] = $mail_parser->getHeader('subject');
|
|
|
|
(empty($data['subject'])) ? $data['subject'] = '-' : null;
|
|
|
|
echo json_encode($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (!empty($_GET['id']) && ctype_alnum($_GET['id'])) {
|
|
|
|
if (!isset($_SESSION['mailcow_cc_role'])) {
|
|
|
|
echo json_encode(array('error' => 'Access denied'));
|
|
|
|
exit();
|
|
|
|
}
|
2017-12-09 13:17:15 +01:00
|
|
|
$tmpdir = '/tmp/' . $_GET['id'] . '/';
|
2018-02-08 20:13:36 +01:00
|
|
|
$mailc = quarantine('details', $_GET['id']);
|
2020-06-06 14:13:46 +02:00
|
|
|
if ($mailc === false) {
|
|
|
|
echo json_encode(array('error' => 'Access denied'));
|
|
|
|
exit;
|
|
|
|
}
|
2017-12-09 13:17:15 +01:00
|
|
|
if (strlen($mailc['msg']) > 10485760) {
|
|
|
|
echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (!empty($mailc['msg'])) {
|
2020-06-06 13:25:32 +02:00
|
|
|
if (isset($_GET['quick_release'])) {
|
|
|
|
$hash = hash('sha256', $mailc['id'] . $mailc['qid']);
|
|
|
|
header('Location: /qhandler/release/' . $hash);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (isset($_GET['quick_delete'])) {
|
|
|
|
$hash = hash('sha256', $mailc['id'] . $mailc['qid']);
|
|
|
|
header('Location: /qhandler/delete/' . $hash);
|
|
|
|
exit;
|
|
|
|
}
|
2017-12-09 13:17:15 +01:00
|
|
|
// Init message array
|
|
|
|
$data = array();
|
|
|
|
// Init parser
|
|
|
|
$mail_parser = new PhpMimeMailParser\Parser();
|
2018-01-18 10:09:30 +01:00
|
|
|
$html2text = new Html2Text\Html2Text();
|
2017-12-09 13:17:15 +01:00
|
|
|
// Load msg to parser
|
|
|
|
$mail_parser->setText($mailc['msg']);
|
2019-05-01 00:56:12 +02:00
|
|
|
|
|
|
|
// Get mail recipients
|
|
|
|
{
|
|
|
|
$recipientsList = array();
|
|
|
|
addAddresses($recipientsList, $mail_parser, 'to');
|
|
|
|
addAddresses($recipientsList, $mail_parser, 'cc');
|
|
|
|
addAddresses($recipientsList, $mail_parser, 'bcc');
|
2020-06-07 10:45:40 +02:00
|
|
|
$recipientsList[] = array('address' => $mailc['rcpt'], 'type' => 'smtp');
|
2019-05-01 00:56:12 +02:00
|
|
|
$data['recipients'] = $recipientsList;
|
|
|
|
}
|
2020-06-06 13:25:32 +02:00
|
|
|
// Get from
|
|
|
|
$data['header_from'] = $mail_parser->getHeader('from');
|
|
|
|
$data['env_from'] = $mailc['sender'];
|
2019-10-09 22:07:26 +02:00
|
|
|
// Get rspamd score
|
|
|
|
$data['score'] = $mailc['score'];
|
2020-11-06 14:26:48 +01:00
|
|
|
// Get rspamd action
|
|
|
|
$data['action'] = $mailc['action'];
|
2019-10-09 22:07:26 +02:00
|
|
|
// Get rspamd symbols
|
|
|
|
$data['symbols'] = json_decode($mailc['symbols']);
|
2020-10-24 16:27:31 +02:00
|
|
|
// Get fuzzy hashes
|
|
|
|
$data['fuzzy_hashes'] = json_decode($mailc['fuzzy_hashes']);
|
2017-12-09 13:17:15 +01:00
|
|
|
// Get text/plain content
|
|
|
|
$data['text_plain'] = $mail_parser->getMessageBody('text');
|
2018-01-18 10:09:30 +01:00
|
|
|
// Get html content and convert to text
|
|
|
|
$data['text_html'] = $html2text->convert($mail_parser->getMessageBody('html'));
|
2018-10-14 10:12:21 +02:00
|
|
|
if (empty($data['text_plain']) && empty($data['text_html'])) {
|
|
|
|
// Failed to parse content, try raw
|
|
|
|
$text = trim(substr($mailc['msg'], strpos($mailc['msg'], "\r\n\r\n") + 1));
|
|
|
|
// Only return html->text
|
|
|
|
$data['text_plain'] = 'Parser failed, assuming HTML';
|
|
|
|
$data['text_html'] = $html2text->convert($text);
|
|
|
|
}
|
2018-01-18 10:09:30 +01:00
|
|
|
(empty($data['text_plain'])) ? $data['text_plain'] = '-' : null;
|
2017-12-09 13:17:15 +01:00
|
|
|
// Get subject
|
|
|
|
$data['subject'] = $mail_parser->getHeader('subject');
|
2018-01-18 10:09:30 +01:00
|
|
|
(empty($data['subject'])) ? $data['subject'] = '-' : null;
|
2017-12-09 13:17:15 +01:00
|
|
|
// Get attachments
|
|
|
|
if (is_dir($tmpdir)) {
|
|
|
|
rrmdir($tmpdir);
|
|
|
|
}
|
|
|
|
mkdir('/tmp/' . $_GET['id']);
|
|
|
|
$mail_parser->saveAttachments($tmpdir, true);
|
|
|
|
$atts = $mail_parser->getAttachments(true);
|
|
|
|
if (count($atts) > 0) {
|
|
|
|
foreach ($atts as $key => $val) {
|
|
|
|
$data['attachments'][$key] = array(
|
|
|
|
// Index
|
|
|
|
// 0 => file name
|
|
|
|
// 1 => mime type
|
|
|
|
// 2 => file size
|
|
|
|
// 3 => vt link by sha256
|
|
|
|
$val->getFilename(),
|
|
|
|
$val->getContentType(),
|
|
|
|
filesize($tmpdir . $val->getFilename()),
|
|
|
|
'https://www.virustotal.com/file/' . hash_file('SHA256', $tmpdir . $val->getFilename()) . '/analysis/'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-10-09 20:18:21 +02:00
|
|
|
if (isset($_GET['eml'])) {
|
2020-05-18 21:15:51 +02:00
|
|
|
$dl_filename = filter_var($data['subject'], FILTER_SANITIZE_STRING);
|
2020-05-17 21:27:18 +02:00
|
|
|
$dl_filename = strlen($dl_filename) > 30 ? substr($dl_filename,0,30) : $dl_filename;
|
2019-10-09 20:18:21 +02:00
|
|
|
header('Pragma: public');
|
|
|
|
header('Expires: 0');
|
|
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
|
|
header('Cache-Control: private', false);
|
|
|
|
header('Content-Type: message/rfc822');
|
|
|
|
header('Content-Disposition: attachment; filename="'. $dl_filename . '.eml";');
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
|
|
header('Content-Length: ' . strlen($mailc['msg']));
|
|
|
|
echo $mailc['msg'];
|
|
|
|
exit;
|
|
|
|
}
|
2017-12-09 13:17:15 +01:00
|
|
|
if (isset($_GET['att'])) {
|
2019-01-29 00:20:39 +01:00
|
|
|
if ($_SESSION['acl']['quarantine_attachments'] == 0) {
|
|
|
|
exit(json_encode('Forbidden'));
|
|
|
|
}
|
2017-12-09 13:17:15 +01:00
|
|
|
$dl_id = intval($_GET['att']);
|
2020-05-18 21:15:51 +02:00
|
|
|
$dl_filename = filter_var($data['attachments'][$dl_id][0], FILTER_SANITIZE_STRING);
|
2020-05-17 21:27:18 +02:00
|
|
|
$dl_filename = strlen($dl_filename) > 30 ? substr($dl_filename,0,30) : $dl_filename;
|
2017-12-09 13:17:15 +01:00
|
|
|
if (!is_dir($tmpdir . $dl_filename) && file_exists($tmpdir . $dl_filename)) {
|
|
|
|
header('Pragma: public');
|
|
|
|
header('Expires: 0');
|
|
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
|
|
header('Cache-Control: private', false);
|
|
|
|
header('Content-Type: ' . $data['attachments'][$dl_id][1]);
|
|
|
|
header('Content-Disposition: attachment; filename="'. $dl_filename . '";');
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
|
|
header('Content-Length: ' . $data['attachments'][$dl_id][2]);
|
|
|
|
readfile($tmpdir . $dl_filename);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 14:42:26 +01:00
|
|
|
$data = mb_convert_encoding($data, "UTF-8", "auto");
|
2017-12-09 13:17:15 +01:00
|
|
|
echo json_encode($data);
|
|
|
|
}
|
2020-11-09 14:42:26 +01:00
|
|
|
|
2017-12-09 13:17:15 +01:00
|
|
|
}
|
|
|
|
?>
|