From 8abf8844bcf23033d4de69c0c162592406e24381 Mon Sep 17 00:00:00 2001 From: djmaze Date: Thu, 16 Sep 2021 14:04:26 +0200 Subject: [PATCH] Replace InlineBase64Decode with default convert.base64-decode and a WhitespaceFilter --- .../MailSo/Base/StreamWrappers/Binary.php | 58 ++++++++----------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Base/StreamWrappers/Binary.php b/snappymail/v/0.0.0/app/libraries/MailSo/Base/StreamWrappers/Binary.php index b12c56127..dd250520f 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Base/StreamWrappers/Binary.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Base/StreamWrappers/Binary.php @@ -11,6 +11,20 @@ namespace MailSo\Base\StreamWrappers; + +class WhitespaceFilter extends \php_user_filter +{ + function filter($in, $out, &$consumed, $closing) + { + while ($bucket = \stream_bucket_make_writeable($in)) { + $bucket->data = \str_replace(array("\r", "\n", "\t"), '', $bucket->data); + $consumed += $bucket->datalen; + \stream_bucket_append($out, $bucket); + } + return PSFS_PASS_ON; + } +} + /** * @category MailSo * @package Base @@ -70,20 +84,15 @@ class Binary public static function GetInlineDecodeOrEncodeFunctionName(string $sContentTransferEncoding, bool $bDecode = true) : string { - $sFunctionName = ''; switch (strtolower($sContentTransferEncoding)) { case \MailSo\Base\Enumerations\Encoding::BASE64_LOWER: - // InlineBase64Decode - $sFunctionName = $bDecode ? 'InlineBase64Decode' : 'convert.base64-encode'; - break; + return $bDecode ? 'convert.base64-decode' : 'convert.base64-encode'; case \MailSo\Base\Enumerations\Encoding::QUOTED_PRINTABLE_LOWER: - // InlineQuotedPrintableDecode - $sFunctionName = $bDecode ? 'convert.quoted-printable-decode' : 'convert.quoted-printable-encode'; - break; + return $bDecode ? 'convert.quoted-printable-decode' : 'convert.quoted-printable-encode'; } - return $sFunctionName; + return ''; } public static function InlineNullDecode(string $sBodyString, string &$sEndBuffer) : string @@ -92,33 +101,6 @@ class Binary return $sBodyString; } - public static function InlineBase64Decode(string $sBaseString, string &$sEndBuffer) : string - { - $sEndBuffer = ''; - $sBaseString = str_replace(array("\r", "\n", "\t"), '', $sBaseString); - $iBaseStringLen = strlen($sBaseString); - $iBaseStringNormFloorLen = floor($iBaseStringLen / 4) * 4; - if ($iBaseStringNormFloorLen < $iBaseStringLen) - { - $sEndBuffer = substr($sBaseString, $iBaseStringNormFloorLen); - $sBaseString = substr($sBaseString, 0, $iBaseStringNormFloorLen); - } - return \MailSo\Base\Utils::Base64Decode($sBaseString); - } - - public static function InlineQuotedPrintableDecode(string $sQuotedPrintableString, string &$sEndBuffer) : string - { - $sEndBuffer = ''; - $sQuotedPrintableLen = strlen($sQuotedPrintableString); - $iLastSpace = strrpos($sQuotedPrintableString, ' '); - if (false !== $iLastSpace && $iLastSpace + 1 < $sQuotedPrintableLen) - { - $sEndBuffer = substr($sQuotedPrintableString, $iLastSpace + 1); - $sQuotedPrintableString = substr($sQuotedPrintableString, 0, $iLastSpace + 1); - } - return quoted_printable_decode($sQuotedPrintableString); - } - public static function InlineConvertDecode(string $sEncodedString, string &$sEndBuffer, string $sFromEncoding, string $sToEncoding) : string { $sEndBuffer = ''; @@ -190,6 +172,12 @@ class Binary 'convert.quoted-printable-decode', 'convert.quoted-printable-encode' ))) { + if ('convert.base64-decode' === $sUtilsDecodeOrEncodeFunctionName) { + \stream_filter_register('mailsowhitespace', '\\MailSo\\Base\\StreamWrappers\\WhitespaceFilter'); + if (!\stream_filter_append($rStream, 'mailsowhitespace')) { + return false; + } + } $rFilter = \stream_filter_append($rStream, $sUtilsDecodeOrEncodeFunctionName, STREAM_FILTER_READ, array( 'line-length' => \MailSo\Mime\Enumerations\Constants::LINE_LENGTH,