Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Mail/mimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 14 additions & 13 deletions tests/headers_without_mbstring.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Multi-test for headers encoding using base64 and quoted-printable
--INI--
disable_functions=mb_substr,mb_strlen
--SKIPIF--
<?php
if (function_exists('mb_substr') && function_exists('mb_strlen')) {
Expand Down Expand Up @@ -68,9 +70,8 @@ foreach ($headers as $header) {
[06] Cc: "Frank Do" <adresse@adresse.de>, "James Clark" <james@domain.com>
[07] Cc: <adresse@adresse.de>, =?UTF-8?B?S3XFm21pZGVyc2tpIEphbiBLcnp5c3p0b2Yg?=
=?UTF-8?B?SmFudXN6IETFgnVnYSBuYXp3YQ==?= <cris@domain.com>
[07] Cc: <adresse@adresse.de>,
=?UTF-8?Q?Ku=C5=9Bmiderski_Jan_Krzysztof_Janusz_D?=
=?UTF-8?Q?=C5=82uga_nazwa?= <cris@domain.com>
[07] Cc: <adresse@adresse.de>, =?UTF-8?Q?Ku=C5=9Bmiderski_Jan_Krzysztof_Janusz?=
=?UTF-8?Q?_D=C5=82uga_nazwa?= <cris@domain.com>
[08] From: "adresse@adresse.de" <addresse@adresse>
[08] From: "adresse@adresse.de" <addresse@adresse>
[09] From: "adresse@adresse.de" <addresse@adresse>
Expand Down Expand Up @@ -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==?= <user@domain.com>
[23] From: =?UTF-8?Q?test=40n=C3=A0me?= <user@domain.com>
[24] From: Test <"test test"@domain.com>
Expand Down
76 changes: 76 additions & 0 deletions tests/rfc2047_qp_chunk_length.phpt
Original file line number Diff line number Diff line change
@@ -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--
<?php
include("Mail/mime.php");

// encodeHeaderValue() cuts quoted-printable text with
// "(.{0,$maxLength}[^\=][^\=])". The two trailing atoms match one character
// each, so a chunk could run two characters past $maxLength, producing
// encoded-words of up to 77 characters and header lines of up to 78.
//
// mbstring is disabled above because encodeMB() splits correctly; the defect
// is in the encodeHeaderValue() fallback that runs when mbstring is missing.

// "Lorem ipsum ... magna aliqua" plus "…" (U+2026, 3 bytes in UTF-8). The
// ellipsis is what makes the value non-ASCII; a pure ASCII value is emitted
// verbatim, never reaches the RFC 2047 encoder, and is folded at RFC 5322's
// 78 characters instead
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod '
. 'tempor incididunt ut labore et dolore magna aliqua';
$ellipsis = "\xE2\x80\xA6";
$value = $text . $ellipsis;

// RFC 2047: "An 'encoded-word' may not be more than 75 characters long" and
// "each line of a header field that contains one or more 'encoded-word's is
// limited to 76 characters".
function check_limits($name, $value, $encoding)
{
$header = $name . ': '
. Mail_mimePart::encodeHeader($name, $value, 'UTF-8', $encoding);

$errors = array();

foreach (explode("\r\n", $header) as $line) {
if (strlen($line) > 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?=
Loading