2011-11-29 14:58:04 +01:00
|
|
|
#!/usr/bin/env php
|
2011-05-05 08:09:42 +02:00
|
|
|
<?php
|
|
|
|
|
2013-12-12 19:59:28 +01:00
|
|
|
// NOTE: This script is very oldschool and takes the environment as an argument.
|
|
|
|
// Some day, we could take a shot at cleaning this up.
|
2011-05-06 00:11:36 +02:00
|
|
|
if ($argc > 1) {
|
2013-12-12 19:59:28 +01:00
|
|
|
foreach (array_slice($argv, 1) as $arg) {
|
|
|
|
if (!preg_match('/^-/', $arg)) {
|
|
|
|
$_SERVER['PHABRICATOR_ENV'] = $arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-06 00:11:36 +02:00
|
|
|
}
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$root = dirname(dirname(dirname(__FILE__)));
|
|
|
|
require_once $root.'/scripts/__init_script__.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/MimeMailParser.class.php';
|
|
|
|
|
2013-12-12 19:59:28 +01:00
|
|
|
$args = new PhutilArgumentParser($argv);
|
|
|
|
$args->parseStandardArguments();
|
|
|
|
$args->parse(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'process-duplicates',
|
|
|
|
'help' => pht(
|
|
|
|
"Process this message, even if it's a duplicate of another message. ".
|
|
|
|
"This is mostly useful when debugging issues with mail routing."),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'env',
|
|
|
|
'wildcard' => true,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$parser = new MimeMailParser();
|
|
|
|
$parser->setText(file_get_contents('php://stdin'));
|
|
|
|
|
Transcode the HTML part of incoming email into UTF-8 as well
Summary:
D1093 did this for just the text/plain part of incoming
email. Most text/html parts choose to either use entity encoding
//or// are already UTF-8, thus obviating the need to transcode the
HTML part. However, this is not always the case, and leads to dropped
messages, by way of:
```
EXCEPTION: (Exception) Failed to JSON encode value (#5: Malformed UTF-8 characters, possibly incorrectly encoded): Dictionary value at key "html" is not valid UTF8, and cannot be JSON encoded: [snip HTML part of message content]```
Generalize the charset transcoding to not apply to just the text/plain part, but
both text/plain and text/html parts.
Test Plan:
Fed in a Windows-1252-encoded text/html part with 0x92
bytes in it; verified that $content only contained valid UTF-8 after
this change.
Reviewers: #blessed_reviewers, epriestley
Reviewed By: #blessed_reviewers, epriestley
Subscribers: Korvin, epriestley
Differential Revision: https://secure.phabricator.com/D18776
2017-11-16 09:43:35 +01:00
|
|
|
$content = array();
|
|
|
|
foreach (array('text', 'html') as $part) {
|
|
|
|
$part_body = $parser->getMessageBody($part);
|
2017-11-22 16:35:22 +01:00
|
|
|
|
|
|
|
if (strlen($part_body) && !phutil_is_utf8($part_body)) {
|
|
|
|
$part_headers = $parser->getMessageBodyHeaders($part);
|
|
|
|
if (!is_array($part_headers)) {
|
|
|
|
$part_headers = array();
|
|
|
|
}
|
|
|
|
$content_type = idx($part_headers, 'content-type');
|
|
|
|
if (preg_match('/charset="(.*?)"/', $content_type, $matches) ||
|
|
|
|
preg_match('/charset=(\S+)/', $content_type, $matches)) {
|
|
|
|
$part_body = phutil_utf8_convert($part_body, 'UTF-8', $matches[1]);
|
|
|
|
}
|
Transcode the HTML part of incoming email into UTF-8 as well
Summary:
D1093 did this for just the text/plain part of incoming
email. Most text/html parts choose to either use entity encoding
//or// are already UTF-8, thus obviating the need to transcode the
HTML part. However, this is not always the case, and leads to dropped
messages, by way of:
```
EXCEPTION: (Exception) Failed to JSON encode value (#5: Malformed UTF-8 characters, possibly incorrectly encoded): Dictionary value at key "html" is not valid UTF8, and cannot be JSON encoded: [snip HTML part of message content]```
Generalize the charset transcoding to not apply to just the text/plain part, but
both text/plain and text/html parts.
Test Plan:
Fed in a Windows-1252-encoded text/html part with 0x92
bytes in it; verified that $content only contained valid UTF-8 after
this change.
Reviewers: #blessed_reviewers, epriestley
Reviewed By: #blessed_reviewers, epriestley
Subscribers: Korvin, epriestley
Differential Revision: https://secure.phabricator.com/D18776
2017-11-16 09:43:35 +01:00
|
|
|
}
|
2017-11-22 16:35:22 +01:00
|
|
|
|
Transcode the HTML part of incoming email into UTF-8 as well
Summary:
D1093 did this for just the text/plain part of incoming
email. Most text/html parts choose to either use entity encoding
//or// are already UTF-8, thus obviating the need to transcode the
HTML part. However, this is not always the case, and leads to dropped
messages, by way of:
```
EXCEPTION: (Exception) Failed to JSON encode value (#5: Malformed UTF-8 characters, possibly incorrectly encoded): Dictionary value at key "html" is not valid UTF8, and cannot be JSON encoded: [snip HTML part of message content]```
Generalize the charset transcoding to not apply to just the text/plain part, but
both text/plain and text/html parts.
Test Plan:
Fed in a Windows-1252-encoded text/html part with 0x92
bytes in it; verified that $content only contained valid UTF-8 after
this change.
Reviewers: #blessed_reviewers, epriestley
Reviewed By: #blessed_reviewers, epriestley
Subscribers: Korvin, epriestley
Differential Revision: https://secure.phabricator.com/D18776
2017-11-16 09:43:35 +01:00
|
|
|
$content[$part] = $part_body;
|
2011-12-02 17:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$headers = $parser->getHeaders();
|
2014-06-09 20:36:49 +02:00
|
|
|
$headers['subject'] = iconv_mime_decode($headers['subject'], 0, 'UTF-8');
|
|
|
|
$headers['from'] = iconv_mime_decode($headers['from'], 0, 'UTF-8');
|
2011-12-02 17:47:45 +01:00
|
|
|
|
2013-12-12 19:59:28 +01:00
|
|
|
if ($args->getArg('process-duplicates')) {
|
|
|
|
$headers['message-id'] = Filesystem::readRandomCharacters(64);
|
|
|
|
}
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$received = new PhabricatorMetaMTAReceivedMail();
|
2011-12-02 17:47:45 +01:00
|
|
|
$received->setHeaders($headers);
|
Transcode the HTML part of incoming email into UTF-8 as well
Summary:
D1093 did this for just the text/plain part of incoming
email. Most text/html parts choose to either use entity encoding
//or// are already UTF-8, thus obviating the need to transcode the
HTML part. However, this is not always the case, and leads to dropped
messages, by way of:
```
EXCEPTION: (Exception) Failed to JSON encode value (#5: Malformed UTF-8 characters, possibly incorrectly encoded): Dictionary value at key "html" is not valid UTF8, and cannot be JSON encoded: [snip HTML part of message content]```
Generalize the charset transcoding to not apply to just the text/plain part, but
both text/plain and text/html parts.
Test Plan:
Fed in a Windows-1252-encoded text/html part with 0x92
bytes in it; verified that $content only contained valid UTF-8 after
this change.
Reviewers: #blessed_reviewers, epriestley
Reviewed By: #blessed_reviewers, epriestley
Subscribers: Korvin, epriestley
Differential Revision: https://secure.phabricator.com/D18776
2017-11-16 09:43:35 +01:00
|
|
|
$received->setBodies($content);
|
2011-05-05 08:09:42 +02:00
|
|
|
|
|
|
|
$attachments = array();
|
2011-05-29 12:19:06 +02:00
|
|
|
foreach ($parser->getAttachments() as $attachment) {
|
2011-06-13 06:09:55 +02:00
|
|
|
if (preg_match('@text/(plain|html)@', $attachment->getContentType()) &&
|
|
|
|
$attachment->getContentDisposition() == 'inline') {
|
|
|
|
// If this is an "inline" attachment with some sort of text content-type,
|
|
|
|
// do not treat it as a file for attachment. MimeMailParser already picked
|
|
|
|
// it up in the getMessageBody() call above. We still want to treat 'inline'
|
|
|
|
// attachments with other content types (e.g., images) as attachments.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$file = PhabricatorFile::newFromFileData(
|
|
|
|
$attachment->getContent(),
|
|
|
|
array(
|
|
|
|
'name' => $attachment->getFilename(),
|
2015-04-02 22:41:39 +02:00
|
|
|
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
|
2011-05-05 08:09:42 +02:00
|
|
|
));
|
|
|
|
$attachments[] = $file->getPHID();
|
|
|
|
}
|
|
|
|
|
2011-10-28 17:05:27 +02:00
|
|
|
try {
|
|
|
|
$received->setAttachments($attachments);
|
|
|
|
$received->save();
|
|
|
|
$received->processReceivedMail();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$received
|
2015-05-22 09:27:56 +02:00
|
|
|
->setMessage(pht('EXCEPTION: %s', $e->getMessage()))
|
2011-10-28 17:05:27 +02:00
|
|
|
->save();
|
2013-12-12 19:59:28 +01:00
|
|
|
|
|
|
|
throw $e;
|
2011-10-28 17:05:27 +02:00
|
|
|
}
|