From 91595ef6d5a527c6e521be51bade558e395fe6c0 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 23 Jan 2024 16:35:22 +0100 Subject: [PATCH] Resolve ##1403 --- .../0.0.0/app/libraries/MailSo/Base/Utils.php | 5 ++- .../app/libraries/MailSo/Mail/MailClient.php | 2 +- .../app/libraries/MailSo/Mail/Message.php | 18 +++++------ .../0.0.0/app/libraries/MailSo/Mime/Email.php | 1 + .../app/libraries/MailSo/Mime/Header.php | 23 +++++++------ .../MailSo/Mime/HeaderCollection.php | 32 ++++++------------- 6 files changed, 36 insertions(+), 45 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Base/Utils.php b/snappymail/v/0.0.0/app/libraries/MailSo/Base/Utils.php index 3d8793147..1d794ae95 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Base/Utils.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Base/Utils.php @@ -219,7 +219,7 @@ abstract class Utils return \preg_replace('/ ([\r]?[\n])/m', ' ', $sInputValue); } - public static function DecodeHeaderValue(string $sEncodedValue, string $sIncomingCharset = '', string $sForcedIncomingCharset = '') : string + public static function DecodeHeaderValue(string $sEncodedValue, string $sIncomingCharset = '') : string { $sValue = $sEncodedValue; if (\strlen($sIncomingCharset)) { @@ -276,8 +276,7 @@ abstract class Utils } if (\strlen($aTempArr[0])) { - $sCharset = \strlen($sForcedIncomingCharset) ? $sForcedIncomingCharset : $aTempArr[0]; - $sCharset = static::NormalizeCharset($sCharset, true); + $sCharset = static::NormalizeCharset($aTempArr[0], true); if ('' === $sMainCharset) { $sMainCharset = $sCharset; diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php index 78fe31e07..b70e49906 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php @@ -332,7 +332,7 @@ class MailClient 'folder' => $sFolderName, 'uid' => $iUid, 'subject' => $oHeaders->ValueByName(MimeHeader::SUBJECT, !$sContentTypeCharset), - 'from' => $oHeaders->GetAsEmailCollection(MimeHeader::FROM_, !$sContentTypeCharset) + 'from' => $oHeaders->GetAsEmailCollection(MimeHeader::FROM_) ); } } diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Message.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Message.php index 1e0907e17..cae8fc10d 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Message.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Message.php @@ -151,7 +151,7 @@ class Message implements \JsonSerializable $sCharset = $oBodyStructure ? Utils::NormalizeCharset($oBodyStructure->SearchCharset()) : ''; $sHeaders = $oFetchResponse->GetHeaderFieldsValue(); - $oHeaders = \strlen($sHeaders) ? new \MailSo\Mime\HeaderCollection($sHeaders, false, $sCharset) : null; + $oHeaders = \strlen($sHeaders) ? new \MailSo\Mime\HeaderCollection($sHeaders, $sCharset) : null; if ($oHeaders) { $oMessage->Headers = $oHeaders; @@ -159,11 +159,9 @@ class Message implements \JsonSerializable MimeHeader::CONTENT_TYPE, \MailSo\Mime\Enumerations\Parameter::CHARSET ); - if (\strlen($sContentTypeCharset)) { $sCharset = Utils::NormalizeCharset($sContentTypeCharset); } - if (\strlen($sCharset)) { $oHeaders->SetParentCharset($sCharset); } @@ -174,14 +172,14 @@ class Message implements \JsonSerializable $oMessage->sMessageId = $oHeaders->ValueByName(MimeHeader::MESSAGE_ID); $oMessage->sContentType = $oHeaders->ValueByName(MimeHeader::CONTENT_TYPE); - $oMessage->oFrom = $oHeaders->GetAsEmailCollection(MimeHeader::FROM_, $bCharsetAutoDetect); - $oMessage->oTo = $oHeaders->GetAsEmailCollection(MimeHeader::TO_, $bCharsetAutoDetect); - $oMessage->oCc = $oHeaders->GetAsEmailCollection(MimeHeader::CC, $bCharsetAutoDetect); - $oMessage->oBcc = $oHeaders->GetAsEmailCollection(MimeHeader::BCC, $bCharsetAutoDetect); + $oMessage->oFrom = $oHeaders->GetAsEmailCollection(MimeHeader::FROM_); + $oMessage->oTo = $oHeaders->GetAsEmailCollection(MimeHeader::TO_); + $oMessage->oCc = $oHeaders->GetAsEmailCollection(MimeHeader::CC); + $oMessage->oBcc = $oHeaders->GetAsEmailCollection(MimeHeader::BCC); - $oMessage->oSender = $oHeaders->GetAsEmailCollection(MimeHeader::SENDER, $bCharsetAutoDetect); - $oMessage->oReplyTo = $oHeaders->GetAsEmailCollection(MimeHeader::REPLY_TO, $bCharsetAutoDetect); - $oMessage->oDeliveredTo = $oHeaders->GetAsEmailCollection(MimeHeader::DELIVERED_TO, $bCharsetAutoDetect); + $oMessage->oSender = $oHeaders->GetAsEmailCollection(MimeHeader::SENDER); + $oMessage->oReplyTo = $oHeaders->GetAsEmailCollection(MimeHeader::REPLY_TO); + $oMessage->oDeliveredTo = $oHeaders->GetAsEmailCollection(MimeHeader::DELIVERED_TO); $oMessage->InReplyTo = $oHeaders->ValueByName(MimeHeader::IN_REPLY_TO); $oMessage->References = Utils::StripSpaces( diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Email.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Email.php index 88559eff7..66371f9c4 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Email.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Email.php @@ -43,6 +43,7 @@ class Email implements \JsonSerializable */ public static function Parse(string $sEmailAddress) : self { + $sEmailAddress = \MailSo\Base\Utils::DecodeHeaderValue($sEmailAddress); $sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress); if (!\strlen(\trim($sEmailAddress))) { throw new \ValueError; diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Header.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Header.php index aeb60de49..200d387cf 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Header.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Header.php @@ -23,7 +23,7 @@ class Header implements \JsonSerializable private string $sFullValue; - private string $sEncodedValueForReparse; + private string $sEncodedValue; private ?ParameterCollection $oParameters = null; @@ -39,10 +39,10 @@ class Header implements \JsonSerializable { $this->sName = \trim($sName); $this->sFullValue = \trim($sValue); - $this->sEncodedValueForReparse = ''; + $this->sEncodedValue = ''; if (\strlen($sEncodedValueForReparse) && ($this->IsEmail() || $this->IsSubject() || $this->IsParameterized())) { - $this->sEncodedValueForReparse = \trim($sEncodedValueForReparse); + $this->sEncodedValue = \trim($sEncodedValueForReparse); } if (\strlen($this->sFullValue) && $this->IsParameterized()) { @@ -101,13 +101,18 @@ class Header implements \JsonSerializable return $this->sFullValue; } + public function EncodedValue() : string + { + return $this->sEncodedValue ?: $this->sFullValue; + } + public function SetParentCharset(string $sParentCharset) : Header { - if ($this->sParentCharset !== $sParentCharset && \strlen($this->sEncodedValueForReparse)) { + if ($this->sParentCharset !== $sParentCharset && \strlen($this->sEncodedValue)) { $this->initInputData( $this->sName, - \trim(\MailSo\Base\Utils::DecodeHeaderValue($this->sEncodedValueForReparse, $sParentCharset)), - $this->sEncodedValueForReparse + \trim(\MailSo\Base\Utils::DecodeHeaderValue($this->sEncodedValue, $sParentCharset)), + $this->sEncodedValue ); } @@ -187,9 +192,9 @@ class Header implements \JsonSerializable public function ValueWithCharsetAutoDetect() : string { if (!\MailSo\Base\Utils::IsAscii($this->Value()) - && \strlen($this->sEncodedValueForReparse) - && !\MailSo\Base\Utils::IsAscii($this->sEncodedValueForReparse) - && ($mEncoding = \mb_detect_encoding($this->sEncodedValueForReparse, 'auto', true)) + && \strlen($this->sEncodedValue) + && !\MailSo\Base\Utils::IsAscii($this->sEncodedValue) + && ($mEncoding = \mb_detect_encoding($this->sEncodedValue, 'auto', true)) ) { $this->SetParentCharset($mEncoding); } diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/HeaderCollection.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/HeaderCollection.php index bd385f87c..5b3674dbe 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/HeaderCollection.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/HeaderCollection.php @@ -17,16 +17,13 @@ namespace MailSo\Mime; */ class HeaderCollection extends \MailSo\Base\Collection { - - protected string $sRawHeaders = ''; - protected string $sParentCharset = ''; - function __construct(string $sRawHeaders = '', bool $bStoreRawHeaders = true, string $sParentCharset = '') + function __construct(string $sRawHeaders = '', string $sParentCharset = '') { parent::__construct(); if (\strlen($sRawHeaders)) { - $this->Parse($sRawHeaders, $bStoreRawHeaders, $sParentCharset); + $this->Parse($sRawHeaders, $sParentCharset); } } @@ -53,13 +50,13 @@ class HeaderCollection extends \MailSo\Base\Collection return $oHeader ? ($bCharsetAutoDetect ? $oHeader->ValueWithCharsetAutoDetect() : $oHeader->Value()) : ''; } - public function ValuesByName(string $sHeaderName, bool $bCharsetAutoDetect = false) : array + public function ValuesByName(string $sHeaderName) : array { $aResult = array(); $sHeaderNameLower = \strtolower($sHeaderName); foreach ($this as $oHeader) { if ($sHeaderNameLower === \strtolower($oHeader->Name())) { - $aResult[] = $bCharsetAutoDetect ? $oHeader->ValueWithCharsetAutoDetect() : $oHeader->Value(); + $aResult[] = $oHeader->Value(); } } return $aResult; @@ -74,11 +71,12 @@ class HeaderCollection extends \MailSo\Base\Collection return $this; } - public function GetAsEmailCollection(string $sHeaderName, bool $bCharsetAutoDetect = false) : ?EmailCollection + public function GetAsEmailCollection(string $sHeaderName) : ?EmailCollection { - return new EmailCollection( - $this->ValueByName($sHeaderName, $bCharsetAutoDetect) - ); + if ($oHeader = $this->GetByName($sHeaderName)) { + return new EmailCollection($oHeader->EncodedValue()); + } + return new EmailCollection(); } public function ParameterValue(string $sHeaderName, string $sParamName) : string @@ -110,20 +108,10 @@ class HeaderCollection extends \MailSo\Base\Collection return $this; } - public function Clear() : void - { - parent::Clear(); - $this->sRawHeaders = ''; - } - - public function Parse(string $sRawHeaders, bool $bStoreRawHeaders = false, string $sParentCharset = '') : self + public function Parse(string $sRawHeaders, string $sParentCharset = '') : self { $this->Clear(); - if ($bStoreRawHeaders) { - $this->sRawHeaders = $sRawHeaders; - } - if (\strlen($this->sParentCharset)) { $this->sParentCharset = $sParentCharset; }