From 9d6a2d5190555af1bff122cb86e64bfc92569319 Mon Sep 17 00:00:00 2001 From: webklex Date: Fri, 23 Jun 2023 21:05:27 +0200 Subject: [PATCH] Header value decoding improved #410 --- CHANGELOG.md | 1 + src/Header.php | 14 +++++----- .../ImapMimeHeaderDecodeReturnsFalseTest.php | 2 +- tests/issues/Issue410Test.php | 27 +++++++++++++++++++ tests/messages/issue-410.eml | 9 +++++++ 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 tests/issues/Issue410Test.php create mode 100644 tests/messages/issue-410.eml diff --git a/CHANGELOG.md b/CHANGELOG.md index f451f40..a1a0f0b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ## [UNRELEASED] ### Fixed - Legacy protocol support fixed (object to array conversion) #411 +- Header value decoding improved #410 ### Added - NaN diff --git a/src/Header.php b/src/Header.php index 4bd4390..e9ff162 100644 --- a/src/Header.php +++ b/src/Header.php @@ -421,7 +421,7 @@ public function decode(mixed $value): mixed { $decoder = $this->config['decoder']['message']; if ($value !== null) { - if ($decoder === 'utf-8' && extension_loaded('imap')) { + if ($decoder === 'utf-8') { $decoded_values = $this->mime_header_decode($value); $tempValue = ""; foreach ($decoded_values as $decoded_value) { @@ -429,14 +429,16 @@ public function decode(mixed $value): mixed { } if ($tempValue) { $value = $tempValue; - } else { + } else if (extension_loaded('imap')) { $value = \imap_utf8($value); + }else if (function_exists('iconv_mime_decode')){ + $value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8"); + }else{ + $value = mb_decode_mimeheader($value); } - } elseif ($decoder === 'iconv' && $this->is_uft8($value)) { + }elseif ($decoder === 'iconv') { $value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8"); - } - - if ($this->is_uft8($value)) { + }else if ($this->is_uft8($value)) { $value = mb_decode_mimeheader($value); } diff --git a/tests/fixtures/ImapMimeHeaderDecodeReturnsFalseTest.php b/tests/fixtures/ImapMimeHeaderDecodeReturnsFalseTest.php index 6f2629f..7f90a4e 100644 --- a/tests/fixtures/ImapMimeHeaderDecodeReturnsFalseTest.php +++ b/tests/fixtures/ImapMimeHeaderDecodeReturnsFalseTest.php @@ -27,7 +27,7 @@ class ImapMimeHeaderDecodeReturnsFalseTest extends FixtureTestCase { public function testFixture() : void { $message = $this->getFixture("imap_mime_header_decode_returns_false.eml"); - self::assertEquals("?p?#]ݰ?[??W̌ N? ?LL?̍L??NL˜", $message->subject->first()); + self::assertEquals("=?UTF-8?B?nnDusSNdG92w6Fuw61fMjAxOF8wMy0xMzMyNTMzMTkzLnBkZg==?=", $message->subject->first()); self::assertEquals("Hi", $message->getTextBody()); self::assertFalse($message->hasHTMLBody()); self::assertEquals("2017-09-13 11:05:45", $message->date->first()->setTimezone('UTC')->format("Y-m-d H:i:s")); diff --git a/tests/issues/Issue410Test.php b/tests/issues/Issue410Test.php new file mode 100644 index 0000000..7c3e6dc --- /dev/null +++ b/tests/issues/Issue410Test.php @@ -0,0 +1,27 @@ +subject); + } + +} \ No newline at end of file diff --git a/tests/messages/issue-410.eml b/tests/messages/issue-410.eml new file mode 100644 index 0000000..05ddeff --- /dev/null +++ b/tests/messages/issue-410.eml @@ -0,0 +1,9 @@ +From: from@there.com +To: to@here.com +Subject: =?ISO-2022-JP?B?GyRCIXlCaBsoQjEzMhskQjlmISEhViUsITwlRyVzGyhCJhskQiUoJS8lOSVGJWolIiFXQGxMZ0U5JE4kPyRhJE4jURsoQiYbJEIjQSU1JW0lcyEhIVo3bjQpJSglLyU5JUYlaiUiISYlbyE8JS8hWxsoQg==?= +Date: Wed, 13 Sep 2017 13:05:45 +0200 +Content-Type: text/plain; + charset="us-ascii" +Content-Transfer-Encoding: quoted-printable + +Hi \ No newline at end of file