Issue #2822334 by jungle, mbovan, longwave, Kristen Pol, quietone: Unicode::mimeHeaderDecode() doesn't support lowercased encoding

merge-requests/7/head
catch 2020-09-01 20:24:53 +01:00
parent c5a0d92f64
commit 1c7065df88
2 changed files with 48 additions and 10 deletions

View File

@ -426,16 +426,16 @@ EOD;
*/ */
public static function mimeHeaderDecode($header) { public static function mimeHeaderDecode($header) {
$callback = function ($matches) { $callback = function ($matches) {
$data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3])); $data = (strtolower($matches[2]) == 'b') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
if (strtolower($matches[1]) != 'utf-8') { if (strtolower($matches[1]) != 'utf-8') {
$data = static::convertToUtf8($data, $matches[1]); $data = static::convertToUtf8($data, $matches[1]);
} }
return $data; return $data;
}; };
// First step: encoded chunks followed by other encoded chunks (need to collapse whitespace) // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
$header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', $callback, $header); $header = preg_replace_callback('/=\?([^?]+)\?([Qq]|[Bb])\?([^?]+|\?(?!=))\?=\s+(?==\?)/', $callback, $header);
// Second step: remaining chunks (do not collapse whitespace) // Second step: remaining chunks (do not collapse whitespace)
return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', $callback, $header); return preg_replace_callback('/=\?([^?]+)\?([Qq]|[Bb])\?([^?]+|\?(?!=))\?=/', $callback, $header);
} }
/** /**

View File

@ -15,15 +15,13 @@ use PHPUnit\Framework\TestCase;
class UnicodeTest extends TestCase { class UnicodeTest extends TestCase {
/** /**
* Tests multibyte encoding and decoding. * Tests multibyte encoding.
* *
* @dataProvider providerTestMimeHeader * @dataProvider providerTestMimeHeader
* @covers ::mimeHeaderEncode * @covers ::mimeHeaderEncode
* @covers ::mimeHeaderDecode
*/ */
public function testMimeHeader($value, $encoded) { public function testMimeHeaderEncode($value, $encoded) {
$this->assertEquals($encoded, Unicode::mimeHeaderEncode($value)); $this->assertEquals($encoded, Unicode::mimeHeaderEncode($value));
$this->assertEquals($value, Unicode::mimeHeaderDecode($encoded));
} }
/** /**
@ -36,9 +34,49 @@ class UnicodeTest extends TestCase {
*/ */
public function providerTestMimeHeader() { public function providerTestMimeHeader() {
return [ return [
['tést.txt', '=?UTF-8?B?dMOpc3QudHh0?='], "Base64 encoding" => ['tést.txt', '=?UTF-8?B?dMOpc3QudHh0?='],
// Simple ASCII characters. "ASCII characters only" => ['test.txt', 'test.txt'],
['ASCII', 'ASCII'], ];
}
/**
* Tests multibyte decoding.
*
* @dataProvider providerTestMimeHeaderDecode
* @covers ::mimeHeaderDecode
*/
public function testMimeHeaderDecode($value, $encoded) {
$this->assertEquals($value, Unicode::mimeHeaderDecode($encoded));
}
/**
* Data provider for testMimeHeaderDecode().
*
* @return array
* An array containing a string and its encoded value.
*/
public function providerTestMimeHeaderDecode() {
return [
'Uppercase base64 encoding' => [
'tést.txt',
'=?utf-8?B?dMOpc3QudHh0?=',
],
'Uppercase quoted-printable encoding' => [
'tést.txt',
'=?UTF-8?Q?t=C3=A9st.txt?=',
],
'Lowercase base64 encoding' => [
'tést.txt',
'=?utf-8?b?dMOpc3QudHh0?=',
],
'Lowercase quoted-printable encoding' => [
'tést.txt',
'=?UTF-8?q?t=C3=A9st.txt?=',
],
'ASCII characters only' => [
'test.txt',
'test.txt',
],
]; ];
} }