diff --git a/Mail/mimePart.php b/Mail/mimePart.php index bc37c48..afb3221 100644 --- a/Mail/mimePart.php +++ b/Mail/mimePart.php @@ -1126,9 +1126,13 @@ public static function encodeHeaderValue($value, $charset, $encoding, $prefix_le $value = Mail_mimePart::encodeQP($value); // This regexp will break QP-encoded text at every $maxLength - // but will not break any encoded letters. - $reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|"; - $reg2nd = "|(.{0,$maxLength}[^\=][^\=])|"; + // but will not break any encoded letters. The two trailing atoms + // match one character each, so the repetition has to stop two + // characters short for the whole match to stay within the limit. + $reg1stLen = $maxLength1stLine - 2; + $reg2ndLen = $maxLength - 2; + $reg1st = "|(.{0,$reg1stLen}[^\=][^\=])|"; + $reg2nd = "|(.{0,$reg2ndLen}[^\=][^\=])|"; if (strlen($value) > $maxLength1stLine) { // Begin with the regexp for the first line. diff --git a/tests/headers_without_mbstring.phpt b/tests/headers_without_mbstring.phpt index 893847c..d7ec2c9 100644 --- a/tests/headers_without_mbstring.phpt +++ b/tests/headers_without_mbstring.phpt @@ -1,5 +1,7 @@ --TEST-- Multi-test for headers encoding using base64 and quoted-printable +--INI-- +disable_functions=mb_substr,mb_strlen --SKIPIF-- , "James Clark" [07] Cc: , =?UTF-8?B?S3XFm21pZGVyc2tpIEphbiBLcnp5c3p0b2Yg?= =?UTF-8?B?SmFudXN6IETFgnVnYSBuYXp3YQ==?= -[07] Cc: , - =?UTF-8?Q?Ku=C5=9Bmiderski_Jan_Krzysztof_Janusz_D?= - =?UTF-8?Q?=C5=82uga_nazwa?= +[07] Cc: , =?UTF-8?Q?Ku=C5=9Bmiderski_Jan_Krzysztof_Janusz?= + =?UTF-8?Q?_D=C5=82uga_nazwa?= [08] From: "adresse@adresse.de" [08] From: "adresse@adresse.de" [09] From: "adresse@adresse.de" @@ -119,18 +120,18 @@ foreach ($headers as $header) { =?UTF-8?B?ciBTw7xwZXIgZ3LDtnNlIHRvbGxlIGdyw7zDn2Ugdm9uIG1pciBTw7xwZXIg?= =?UTF-8?B?Z3LDtnNlIHRvbGxlIGdyw7zDn2Ugdm9uIG1pciBTw7xwZXIgZ3LDtnNlIHRv?= =?UTF-8?B?bGxlIGdyw7zDn2Ugdm9uIG1pciEhIT8=?= -[21] Subject: =?UTF-8?Q?TEST_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir?= - =?UTF-8?Q?_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper_g?= - =?UTF-8?Q?r=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_tol?= - =?UTF-8?Q?le_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC?= - =?UTF-8?Q?=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_m?= - =?UTF-8?Q?ir_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper?= - =?UTF-8?Q?_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_t?= - =?UTF-8?Q?olle_gr=C3=BC=C3=9Fe_von_mir!!!=3F?= +[21] Subject: =?UTF-8?Q?TEST_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_m?= + =?UTF-8?Q?ir_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCp?= + =?UTF-8?Q?er_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6?= + =?UTF-8?Q?se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_tolle_gr?= + =?UTF-8?Q?=C3=BC=C3=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3?= + =?UTF-8?Q?=9Fe_von_mir_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mi?= + =?UTF-8?Q?r_S=C3=BCper_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir_S=C3=BCpe?= + =?UTF-8?Q?r_gr=C3=B6se_tolle_gr=C3=BC=C3=9Fe_von_mir!!!=3F?= [22] Subject: =?UTF-8?B?VXBkYXRlOiBNaWNyb3NvZnQgV2luZG93cy1Ub29sIHp1bSBFbnRm?= =?UTF-8?B?ZXJuZW4gYsO2c2FydGlnZXIgU29mdHdhcmUgMy42?= -[22] Subject: =?UTF-8?Q?Update=3A_Microsoft_Windows-Tool_zum_Entfernen_b=C3=B6sa?= - =?UTF-8?Q?rtiger_Software_3=2E6?= +[22] Subject: =?UTF-8?Q?Update=3A_Microsoft_Windows-Tool_zum_Entfernen_b=C3=B6?= + =?UTF-8?Q?sartiger_Software_3=2E6?= [23] From: =?UTF-8?B?dGVzdEBuw6BtZQ==?= [23] From: =?UTF-8?Q?test=40n=C3=A0me?= [24] From: Test <"test test"@domain.com> diff --git a/tests/rfc2047_qp_chunk_length.phpt b/tests/rfc2047_qp_chunk_length.phpt new file mode 100644 index 0000000..ff7bc83 --- /dev/null +++ b/tests/rfc2047_qp_chunk_length.phpt @@ -0,0 +1,76 @@ +--TEST-- +RFC 2047: quoted-printable chunks must not run past the encoded-word limit +--INI-- +disable_functions=mb_substr,mb_strlen +--FILE-- + 76) { + $errors[] = sprintf('line of %d chars: %s', strlen($line), $line); + } + } + + preg_match_all('/=\?[^?]+\?[QB]\?[^?]*\?=/', $header, $matches); + foreach ($matches[0] as $word) { + if (strlen($word) > 75) { + $errors[] = sprintf('encoded-word of %d chars: %s', strlen($word), $word); + } + } + + return $errors; +} + +// Sweep the cut point across the value: every truncation has to stay inside +// the limits, whichever character the chunk boundary lands on. The ellipsis +// is kept on every case so the value is always non-ASCII +$failed = 0; + +foreach (array('quoted-printable', 'base64') as $encoding) { + for ($n = 0; $n <= strlen($text); $n++) { + $errors = check_limits('Subject', substr($text, 0, $n) . $ellipsis, $encoding); + foreach ($errors as $error) { + printf('FAIL [%s n=%d]: %s' . PHP_EOL, $encoding, $n, $error); + $failed++; + } + } +} + +printf('failures: %d' . PHP_EOL, $failed); + +// Used to be cut two characters late, giving a 77-character encoded-word +echo Mail_mimePart::encodeHeaderValue($value, 'UTF-8', 'quoted-printable', 9) . PHP_EOL; +?> +--EXPECT-- +failures: 0 +=?UTF-8?Q?Lorem_ipsum_dolor_sit_amet=2C_consectetur_adipiscing_e?= + =?UTF-8?Q?lit=2C_sed_do_eiusmod_tempor_incididunt_ut_labore_et_dolore_mag?= + =?UTF-8?Q?na_aliqua=E2=80=A6?=