davidmj came up with a simpler (and working in all cases!) approach. :) Bow to him...

git-svn-id: file:///svn/phpbb/trunk@6642 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2006-11-23 14:54:37 +00:00
parent 945491d37d
commit 6d6f433d2e

View file

@ -1374,63 +1374,47 @@ class smtp_class
* is if the mail client does not understand this encoding the user * is if the mail client does not understand this encoding the user
* is basically doomed with an unreadable subject. * is basically doomed with an unreadable subject.
* *
* Please note that this version fully supports RFC 2045 section 6.8 to * Please note that this version fully supports RFC 2045 section 6.8.
* the expense of using more resources. It downgrades to non-compliance (but workable)
* if the string is not able to be splitted properly.
* You are able to force non-compliance.
*/ */
function mail_encode($str, $compliant = true) function mail_encode($str)
{ {
// define start delimimter, end delimiter and spacer // define start delimimter, end delimiter and spacer
$end = '?='; $start = "=?UTF-8?B?";
$start = '=?UTF-8?B?'; $end = "?=";
$spacer = "$end $start"; $spacer = $end . ' ' . $start;
$encoded_str = base64_encode($str);
$split_length = 64; $split_length = 64;
// Pass back if the encoded string does not need to be split or forced. $encoded_str = base64_encode($str);
if (!$compliant || strlen($encoded_str) <= $split_length)
// If encoded string meets the limits, we just return with the correct data.
if (strlen($encoded_str) <= $split_length)
{ {
return $start . $encoded_str . $end; return $start . $encoded_str . $end;
} }
// If there is only ASCII data, we just return what we want, no need to process. // If there is only ASCII data, we just return what we want, correctly splitting the lines.
if (strlen($str) === utf8_strlen($str)) if (strlen($str) === utf8_strlen($str))
{ {
return $start . implode($spacer, str_split($encoded_str, $split_length)) . $end; return $start . implode($spacer, str_split($encoded_str, $split_length)) . $end;
} }
// What we do is encoding/decoding forth and back and checking // UTF-8 data, compose encoded lines
// for a valid utf8 string to make sure no lines include half-baked data. $array = utf8_str_split($str);
$correct_encode = false; $str = '';
// Also quit the operation if the chunks get too small while (sizeof($array))
while (!$correct_encode || $split_length < 10)
{ {
$chunks = str_split($encoded_str, $split_length); $text = '';
$correct_encode = true;
foreach ($chunks as $chunk) while (sizeof($array) && strlen(base64_encode($text . $array[0])) <= $split_length)
{ {
// Not well-formed utf8 data? $text .= array_shift($array);
if (!preg_match('/^./u', base64_decode($chunk)))
{
$correct_encode = false;
// Always odd length
$split_length -= 2;
}
}
} }
if (!$correct_encode) $str .= $start . base64_encode($text) . $end . ' ';
{
// Not RFC-compliant, but working with all setups
return $start . $encoded_str . $end;
} }
return $start . implode($spacer, $chunks) . $end; return substr($str, 0, -1);
} }
?> ?>