phpunit test add php 8.3, remove special code for mb_encode_mimeheader

In the past we had a special function to do mb_encode_mimeheader correctly.

Since PHP 8.2 this works perfectly fine, so all the code was removed and
replaced with just the normal "mb_encode_mimeheader" call with the same
settings as before:
- set global encoding to parameter
- run encoding with charset, 'B' for transfer and use the line break given in parameter
- reset the global encoding to previous set
This commit is contained in:
Clemens Schwaighofer
2024-04-17 10:05:53 +09:00
parent 21ac91d2e6
commit f4ff31721b
3 changed files with 28 additions and 44 deletions

View File

@@ -19,6 +19,7 @@ if [ ! -z "${1}" ]; then
# "8.0") php_bin="/usr/bin/php8.0 "; ;; # "8.0") php_bin="/usr/bin/php8.0 "; ;;
# "8.1") php_bin="/usr/bin/php8.1 "; ;; # "8.1") php_bin="/usr/bin/php8.1 "; ;;
"8.2") php_bin="/usr/bin/php8.2 "; ;; "8.2") php_bin="/usr/bin/php8.2 "; ;;
"8.3") php_bin="/usr/bin/php8.4 "; ;;
*) echo "Not support PHP: ${1}"; exit; ;; *) echo "Not support PHP: ${1}"; exit; ;;
esac; esac;
fi; fi;
@@ -29,6 +30,7 @@ if [ ! -z "${2}" ] && [ -z "${php_bin}" ]; then
# "8.0") php_bin="/usr/bin/php8.0 "; ;; # "8.0") php_bin="/usr/bin/php8.0 "; ;;
# "8.1") php_bin="/usr/bin/php8.1 "; ;; # "8.1") php_bin="/usr/bin/php8.1 "; ;;
"8.2") php_bin="/usr/bin/php8.2 "; ;; "8.2") php_bin="/usr/bin/php8.2 "; ;;
"8.3") php_bin="/usr/bin/php8.3 "; ;;
*) echo "Not support PHP: ${1}"; exit; ;; *) echo "Not support PHP: ${1}"; exit; ;;
esac; esac;
fi; fi;

View File

@@ -33,15 +33,14 @@ final class CoreLibsConvertMimeEncodeTest extends TestCase
'The quick brown fox jumps over the lazy sheep that sleeps in the ravine ' 'The quick brown fox jumps over the lazy sheep that sleeps in the ravine '
. 'and has no idea what is going on here', . 'and has no idea what is going on here',
'UTF-8', 'UTF-8',
'The quick brown fox jumps over the lazy sheep that sleeps in the ravine ' "The quick brown fox jumps over the lazy sheep that sleeps in the ravine and\r\n"
. 'and has no idea what is going on here' . ' has no idea what is going on here'
], ],
'standard with special chars UTF-8' => [ 'standard with special chars UTF-8' => [
'This is ümläßtと漢字もカタカナ!^$%&', 'This is ümläßtと漢字もカタカナ!^$%&',
'UTF-8', 'UTF-8',
'This is =?UTF-8?B?w7xtbMOkw59044Go5ryi5a2X44KC44Kr44K/44Kr44OK77yBIV4k?=' "This is =?UTF-8?B?w7xtbMOkw59044Go5ryi5a2X44KC44Kr44K/44Kr44OK77yBIV4k?=\r\n"
. "\r\n" . ' =?UTF-8?B?JSY=?='
. ' =?UTF-8?B?JQ==?=&'
], ],
'35 chars and space at the end UTF-8' => [ '35 chars and space at the end UTF-8' => [
'12345678901234567890123456789012345 ' '12345678901234567890123456789012345 '
@@ -62,9 +61,8 @@ final class CoreLibsConvertMimeEncodeTest extends TestCase
. 'is there a space?', . 'is there a space?',
'UTF-8', 'UTF-8',
"=?UTF-8?B?44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq44Kr44K/44Kr44OK44Kr?=\r\n" "=?UTF-8?B?44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq44Kr44K/44Kr44OK44Kr?=\r\n"
. " =?UTF-8?B?44K/44Kr44OK?=\r\n" . " =?UTF-8?B?44K/44Kr44OK44GL44Gq44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq?=\r\n"
. " =?UTF-8?B?44GL44Gq44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq44Kr44K/44Kr?=\r\n" . " =?UTF-8?B?44Kr44K/44Kr44OK44Kr44K/IGlzIHRoZXJlIGEgc3BhY2U/?="
. " =?UTF-8?B?44OK44Kr44K/?= is there a =?UTF-8?B?c3BhY2U/?="
] ]
]; ];
} }
@@ -85,16 +83,28 @@ final class CoreLibsConvertMimeEncodeTest extends TestCase
// print "MIME: -" . $encoded . "-\n"; // print "MIME: -" . $encoded . "-\n";
$this->assertEquals( $this->assertEquals(
$expected, $expected,
$encoded $encoded,
"__mbMimeEncode"
); );
$decoded = mb_decode_mimeheader($encoded); $decoded = mb_decode_mimeheader($encoded);
// print "INPUT : " . $input . "\n"; // print "ENCODED: " . $encoded . "\n";
// print "DECODED: " . $decoded . "\n"; // print "INPUT : " . $input . " | " . mb_strlen($input) . "\n";
// print "DECODED: " . $decoded . " | " . mb_strlen($decoded) . "\n";
// $test_enc = mb_encode_mimeheader($input, $encoding);
// $test_dec = mb_decode_mimeheader($test_enc);
// print "TEST ENC: " . $test_enc . "\n";
// back compare decoded // back compare decoded
$this->assertEquals( $this->assertEquals(
$input, $input,
$decoded $decoded,
"mb_decode_mimeheader"
); );
// $this->assertEquals(
// $input,
// $test_dec,
// 'mb_encode_to_decode'
// );
} }
} }

View File

@@ -15,6 +15,7 @@ class MimeEncode
/** /**
* wrapper function for mb mime convert * wrapper function for mb mime convert
* for correct conversion with long strings * for correct conversion with long strings
* NOTE: This is only a wrapper for mb_encode_mimeheader to stay compatible
* *
* @param string $string string to encode * @param string $string string to encode
* @param string $encoding target encoding * @param string $encoding target encoding
@@ -29,38 +30,9 @@ class MimeEncode
$current_internal_encoding = mb_internal_encoding(); $current_internal_encoding = mb_internal_encoding();
// set internal encoding, so the mimeheader encode works correctly // set internal encoding, so the mimeheader encode works correctly
mb_internal_encoding($encoding); mb_internal_encoding($encoding);
// if a subject, make a work around for the broken mb_mimencode // use the internal convert to mime header
$pos = 0; // it works from PHP 8.2 on
// after 36 single bytes characters, $string = mb_encode_mimeheader($string, $encoding, 'B', $line_break);
// if then comes MB, it is broken
// has to 2 x 36 < 74 so the mb_encode_mimeheader
// 74 hardcoded split does not get triggered
$split = 36;
$_string = '';
while ($pos < mb_strlen($string, $encoding)) {
$output = mb_strimwidth($string, $pos, $split, "", $encoding);
$pos += mb_strlen($output, $encoding);
// if the strinlen is 0 here, get out of the loop
if (!mb_strlen($output, $encoding)) {
$pos += mb_strlen($string, $encoding);
}
$_string_encoded = mb_encode_mimeheader($output, $encoding);
// only make linebreaks if we have mime encoded code inside
// the space only belongs in the second line
if ($_string && preg_match("/^=\?/", $_string_encoded)) {
$_string .= $line_break . " ";
} elseif (
// hack for plain text with space at the end
mb_strlen($output, $encoding) == $split &&
mb_substr($output, -1, 1, $encoding) == " "
) {
// if output ends with space, add one more
$_string_encoded .= " ";
}
$_string .= $_string_encoded;
}
// strip out any spaces BEFORE a line break
$string = str_replace(" " . $line_break, $line_break, $_string);
// before we end, reset internal encoding // before we end, reset internal encoding
mb_internal_encoding($current_internal_encoding); mb_internal_encoding($current_internal_encoding);
// return mime encoded string // return mime encoded string