Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 79f0f72

Browse files
committed
Merge branch 'hotfix/204-multiline-multibyte-headers'
Close #204 Fixes #194
2 parents c8303cf + 9689e04 commit 79f0f72

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ All notable changes to this project will be documented in this file, in reverse
4242

4343
### Fixed
4444

45+
- [#204](https://github.com/zendframework/zend-mail/pull/204) fixes `HeaderWrap::mimeDecodeValue()` behavior when handling a multiline UTF-8
46+
header split across a character. The fix will only work when ext-imap is present, however.
47+
4548
- [#164](https://github.com/zendframework/zend-mail/pull/164) fixes the return value from `Zend\Mail\Protocol\Imap::capability()` when no response is
4649
returned from the server; previously, it returned `false`, but now correctly returns an empty array.
4750

src/Header/HeaderWrap.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,27 @@ public static function mimeDecodeValue($value)
110110

111111
$decodedValue = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
112112

113+
// imap (unlike iconv) can handle multibyte headers which are splitted across multiple line
114+
if (self::isNotDecoded($value, $decodedValue) && extension_loaded('imap')) {
115+
return array_reduce(
116+
imap_mime_header_decode(imap_utf8($value)),
117+
function ($accumulator, $headerPart) {
118+
return $accumulator . $headerPart->text;
119+
},
120+
''
121+
);
122+
}
123+
113124
return $decodedValue;
114125
}
115126

127+
private static function isNotDecoded($originalValue, $value)
128+
{
129+
return 0 === strpos($value, '=?')
130+
&& strlen($value) - 2 === strpos($value, '?=')
131+
&& false !== strpos($originalValue, $value);
132+
}
133+
116134
/**
117135
* Test if is possible apply MIME-encoding
118136
*

test/Header/HeaderWrapTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,24 @@ public function testCanBeEncoded()
113113
$res = HeaderWrap::canBeEncoded($value);
114114
$this->assertTrue($res);
115115
}
116+
117+
/**
118+
* @requires extension imap
119+
*/
120+
public function testMultilineWithMultibyteSplitAcrossCharacter()
121+
{
122+
$originalValue = 'аф';
123+
124+
$this->assertEquals(strlen($originalValue), 4);
125+
126+
$part1 = base64_encode(substr($originalValue, 0, 3));
127+
$part2 = base64_encode(substr($originalValue, 3));
128+
129+
$header = '=?utf-8?B?' . $part1 . '?==?utf-8?B?' . $part2 . '?=';
130+
131+
$this->assertEquals(
132+
$originalValue,
133+
HeaderWrap::mimeDecodeValue($header)
134+
);
135+
}
116136
}

0 commit comments

Comments
 (0)