2017-10-22 21:37:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
class Piler_Mime_Decode {
|
2023-06-07 07:33:20 +02:00
|
|
|
const HEADER_FIELDS = ['from', 'sender', 'to', 'cc', 'subject', 'date'];
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
public static function normalize_message($message) {
|
|
|
|
$a = preg_split("/\r?\n/", $message);
|
|
|
|
return implode(EOL, $a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-22 21:37:40 +02:00
|
|
|
public static function parseMessage($message, &$result) {
|
|
|
|
|
2018-12-23 20:50:14 +01:00
|
|
|
self::splitMessage($message, $headers, $body);
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
$boundary = self::getBoundary($headers);
|
|
|
|
|
|
|
|
// No boundary defined
|
|
|
|
|
|
|
|
if($boundary == '') {
|
|
|
|
if($headers['content-type']['type'] == "message/rfc822") {
|
|
|
|
self::parseMessage($body, $result);
|
|
|
|
}
|
|
|
|
else {
|
2023-06-07 07:33:20 +02:00
|
|
|
$result[] = [
|
2017-10-22 21:37:40 +02:00
|
|
|
'headers' => $headers,
|
|
|
|
'body' => $body
|
2023-06-07 07:33:20 +02:00
|
|
|
];
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = self::splitMime($body, $boundary);
|
|
|
|
|
|
|
|
for($i=0; $i<count($parts); $i++) {
|
|
|
|
|
2018-12-23 20:50:14 +01:00
|
|
|
self::splitMessage($parts[$i], $headers, $body);
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
$boundary = self::getBoundary($headers);
|
|
|
|
if($boundary) {
|
|
|
|
self::parseMessage($parts[$i], $result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(in_array($headers['content-type']['type'], ["text/plain", "text/html"])) {
|
2023-06-07 07:33:20 +02:00
|
|
|
$result[] = ['headers' => $headers, 'body' => $body];
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
else if($headers['content-type']['type'] == "message/rfc822") {
|
|
|
|
self::parseMessage($body, $result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function splitMime($body, $boundary) {
|
|
|
|
$start = 0;
|
2023-06-07 07:33:20 +02:00
|
|
|
$res = [];
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
// Extract the mime parts excluding the boundary itself
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
$p = strpos($body, '--' . $boundary . EOL, $start);
|
2017-10-22 21:37:40 +02:00
|
|
|
if($p === false) {
|
|
|
|
// no parts found!
|
2023-06-07 07:33:20 +02:00
|
|
|
return [];
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position after first boundary line
|
|
|
|
|
|
|
|
$start = $p + 3 + strlen($boundary);
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
while(($p = strpos($body, '--' . $boundary . EOL, $start)) !== false) {
|
2017-10-22 21:37:40 +02:00
|
|
|
$res[] = substr($body, $start, $p-$start);
|
|
|
|
$start = $p + 3 + strlen($boundary);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No more parts, find end boundary
|
|
|
|
|
|
|
|
$p = strpos($body, '--' . $boundary . '--', $start);
|
|
|
|
if($p === false) {
|
2023-06-07 07:33:20 +02:00
|
|
|
return [];
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The remaining part also needs to be parsed:
|
|
|
|
$res[] = substr($body, $start, $p - $start);
|
|
|
|
|
|
|
|
return $res;
|
2020-02-25 10:00:11 +01:00
|
|
|
}
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
public static function splitMessage($message, &$headers, &$body) {
|
2018-12-23 21:00:22 +01:00
|
|
|
self::splitMessageRaw($message, $headers, $journal, $body);
|
2017-10-22 21:37:40 +02:00
|
|
|
$headers = self::splitHeaders($headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
public static function splitMessageRaw($message, &$headers, &$journal, &$body) {
|
2017-10-22 21:37:40 +02:00
|
|
|
$headers = [];
|
|
|
|
$body = '';
|
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
$message = self::normalize_message($message);
|
|
|
|
|
2017-10-22 21:37:40 +02:00
|
|
|
// Find an empty line between headers and body, otherwise we got a header-only message
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
if(strpos($message, EOL . EOL)) {
|
|
|
|
list($headers, $body) = explode(EOL . EOL, $message, 2);
|
2018-12-23 21:00:22 +01:00
|
|
|
|
|
|
|
// Check if the header is actually a journal header
|
|
|
|
$headers_array = self::splitHeaders($headers);
|
|
|
|
|
|
|
|
if(isset($headers_array['x-ms-journal-report']) && isset($headers_array['content-type']['boundary'])) {
|
|
|
|
$boundary = $headers_array['content-type']['boundary'];
|
|
|
|
$parts = self::splitMime($body, $boundary);
|
|
|
|
|
|
|
|
if(count($parts) >= 2) {
|
|
|
|
self::splitMessageRaw($parts[0], $s, $j, $journal);
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
$i = strpos($parts[1], EOL . EOL);
|
2018-12-23 21:00:22 +01:00
|
|
|
$msg = substr($parts[1], $i);
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
while(ctype_space($msg[$i])) { $i++; }
|
|
|
|
if($i > 0) { $msg = substr($msg, $i); }
|
|
|
|
|
|
|
|
self::splitMessageRaw($msg, $headers, $j, $body);
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 12:40:37 +01:00
|
|
|
|
|
|
|
// If the message has a single binary attachment, then drop the body part
|
|
|
|
if(isset($headers_array['content-type']['type'])) {
|
|
|
|
foreach(['application/', 'image/'] as $type) {
|
|
|
|
if(strstr($headers_array['content-type']['type'], $type)) {
|
|
|
|
$body = '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$headers = $message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
public static function removeJournal(&$message) {
|
2018-12-23 21:00:22 +01:00
|
|
|
$has_journal = 0;
|
|
|
|
|
2021-11-20 17:01:49 +01:00
|
|
|
self::splitMessageRaw($message, $headers, $journal, $body);
|
2018-12-23 21:00:22 +01:00
|
|
|
|
2021-11-20 17:01:49 +01:00
|
|
|
if($journal) {
|
|
|
|
$has_journal = 1;
|
2018-12-23 21:00:22 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
$message = $headers . EOL . EOL . $body;
|
2021-11-20 17:01:49 +01:00
|
|
|
|
2018-12-23 21:00:22 +01:00
|
|
|
return $has_journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-22 21:37:40 +02:00
|
|
|
public static function splitHeaders($headers) {
|
|
|
|
$headers = self::headersToArray($headers);
|
|
|
|
|
|
|
|
// normalize header names
|
|
|
|
foreach ($headers as $name => $header) {
|
|
|
|
$lower = strtolower($name);
|
|
|
|
if($lower == $name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($headers[$name]);
|
|
|
|
|
|
|
|
if(!isset($headers[$lower])) {
|
|
|
|
$headers[$lower] = $header;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($headers[$lower])) {
|
|
|
|
$headers[$lower][] = $header;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
$headers[$lower] = [$headers[$lower], $header];
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add some default values, if they are missing
|
|
|
|
|
|
|
|
if(!isset($headers['content-type'])) { $headers['content-type'] = 'text/plain'; }
|
|
|
|
|
2017-10-28 12:32:30 +02:00
|
|
|
// I saw a dumb email (it was a spam, though) having two Date: lines.
|
|
|
|
// In this case we take the first date, and discard the rest
|
2018-03-30 08:09:35 +02:00
|
|
|
if(isset($headers[self::HEADER_FIELDS[4]]) && is_array($headers[self::HEADER_FIELDS[4]])) {
|
|
|
|
$headers[self::HEADER_FIELDS[4]] = $headers[self::HEADER_FIELDS[4]][0];
|
|
|
|
}
|
2017-10-28 12:32:30 +02:00
|
|
|
|
2017-10-22 21:37:40 +02:00
|
|
|
for($i=0; $i<count(self::HEADER_FIELDS); $i++) {
|
|
|
|
if(!isset($headers[self::HEADER_FIELDS[$i]])) { $headers[self::HEADER_FIELDS[$i]] = ''; }
|
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
// If the mail header features the same field more than once, eg.
|
|
|
|
// Date: Wed, 23 Mar 2016 21:26:53 +0100
|
|
|
|
// Date: Wed, 23 Mar 2016 21:26:53 +0100
|
|
|
|
// then take the first occurance
|
|
|
|
|
|
|
|
$header = $headers[self::HEADER_FIELDS[$i]];
|
|
|
|
if(is_array($header)) {
|
|
|
|
$header = $header[0];
|
|
|
|
}
|
2021-08-13 12:17:26 +02:00
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
if(ENABLE_GB2312_FIX) {
|
|
|
|
$header = preg_replace("/gb2312/i", "GBK", $header);
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers[self::HEADER_FIELDS[$i]] = iconv_mime_decode($header, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$headers['content-type'] = self::splitContentType($headers['content-type']);
|
|
|
|
|
|
|
|
$headers['content-type']['type'] = strtolower($headers['content-type']['type']);
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function headersToArray($headers = '') {
|
|
|
|
$token = '';
|
|
|
|
$last_token = '';
|
2023-06-07 07:33:20 +02:00
|
|
|
$result = [];
|
2017-10-22 21:37:40 +02:00
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
$headers = explode(EOL, $headers);
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
foreach($headers as $h) {
|
|
|
|
|
|
|
|
// Handle cases when there's no whitespace between the header key and value
|
|
|
|
// eg. Subject:som
|
|
|
|
|
|
|
|
$h = preg_replace("/^([\S]+):(\S)/", '${1}: ${2}', $h);
|
|
|
|
$h = preg_replace("/\s{1,}/", " ", $h);
|
|
|
|
|
|
|
|
$line = preg_split("/\s/", $h);
|
|
|
|
|
|
|
|
// Skip line if it doesn't have a colon (:) and the 1st character is not a whitespace
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
if($h && !ctype_space($h[0]) && !strchr($h, ':')) { continue; }
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
if($line) {
|
|
|
|
if(substr($line[0], -1) == ':') {
|
|
|
|
$token = array_shift($line);
|
|
|
|
$token = rtrim($token, ':');
|
|
|
|
|
|
|
|
$last_token = $token;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$token = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$line_str = implode(" ", $line);
|
|
|
|
|
|
|
|
if(!isset($result[$last_token])) {
|
|
|
|
$result[$last_token] = $line_str;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if($token) {
|
2023-06-04 18:24:18 +02:00
|
|
|
$result[$last_token] .= EOL;
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$result[$last_token] .= ' ' . $line_str;
|
|
|
|
}
|
2020-02-25 10:00:11 +01:00
|
|
|
}
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:40:42 +01:00
|
|
|
foreach($result as $k => $v) {
|
2023-06-04 18:24:18 +02:00
|
|
|
if(strchr($v, EOL)) {
|
|
|
|
$result[$k] = explode(EOL, $v);
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function splitContentType($field = '') {
|
2023-06-07 07:33:20 +02:00
|
|
|
$split = [];
|
2017-10-22 21:37:40 +02:00
|
|
|
$what = 'type';
|
|
|
|
|
|
|
|
$field = $what . '=' . $field;
|
|
|
|
if(!preg_match_all('%([^=\s]+)\s*=\s*("[^"]+"|[^;]+)(;\s*|$)%', $field, $matches)) {
|
|
|
|
return $split;
|
|
|
|
}
|
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
$split = [];
|
2017-10-22 21:37:40 +02:00
|
|
|
foreach ($matches[1] as $key => $name) {
|
|
|
|
$name = strtolower($name);
|
|
|
|
if($matches[2][$key][0] == '"') {
|
|
|
|
$split[$name] = substr($matches[2][$key], 1, -1);
|
|
|
|
} else {
|
|
|
|
$split[$name] = $matches[2][$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $split;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
public static function getBoundary($headers = []) {
|
2017-10-22 21:37:40 +02:00
|
|
|
if(isset($headers['content-type']['boundary'])) {
|
|
|
|
return $headers['content-type']['boundary'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 07:33:20 +02:00
|
|
|
public static function fixMimeBodyPart($headers = [], $body = '') {
|
2017-10-22 21:37:40 +02:00
|
|
|
|
|
|
|
if(isset($headers['content-transfer-encoding'])) {
|
2021-08-26 16:43:15 +02:00
|
|
|
if(strtolower($headers['content-transfer-encoding']) == 'quoted-printable') {
|
2017-10-22 21:37:40 +02:00
|
|
|
$body = quoted_printable_decode($body);
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:43:15 +02:00
|
|
|
if(strtolower($headers['content-transfer-encoding']) == 'base64') {
|
2017-10-22 21:37:40 +02:00
|
|
|
$body = base64_decode($body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($headers['content-type']['charset'])) {
|
2023-06-07 07:33:20 +02:00
|
|
|
if(ENABLE_GB2312_FIX && strtolower($headers['content-type']['charset']) == 'gb2312') {
|
2021-08-13 12:17:26 +02:00
|
|
|
$headers['content-type']['charset'] = 'GBK';
|
|
|
|
}
|
2023-06-07 07:33:20 +02:00
|
|
|
|
2017-10-22 21:37:40 +02:00
|
|
|
$body = iconv($headers['content-type']['charset'], 'utf-8' . '//IGNORE', $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strtolower($headers['content-type']['type']) == 'text/plain') {
|
|
|
|
$body = self::escape_lt_gt_symbols($body);
|
2023-06-04 18:24:18 +02:00
|
|
|
$body = preg_replace("/\n/", "THE_BREAK_HTML_TAG\n", $body);
|
|
|
|
$body = EOL . self::printNicely($body);
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function escape_lt_gt_symbols($s = '') {
|
|
|
|
$s = preg_replace("/</", "<", $s);
|
|
|
|
$s = preg_replace("/>/", ">", $s);
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function printNicely($s = '') {
|
|
|
|
$k = 0;
|
|
|
|
$nice = "";
|
|
|
|
|
|
|
|
$x = explode(" ", $s);
|
|
|
|
|
|
|
|
for($i=0; $i<count($x); $i++){
|
|
|
|
$nice .= $x[$i] . " ";
|
|
|
|
$k += strlen($x[$i]);
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
if(strstr($x[$i], EOL)){ $k = 0; }
|
2017-10-22 21:37:40 +02:00
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
if($k > 70){ $nice .= EOL; $k = 0; }
|
2017-10-22 21:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $nice;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|