Resolve ##1403

This commit is contained in:
the-djmaze 2024-01-23 16:35:22 +01:00
parent 4b8ad326fa
commit 91595ef6d5
6 changed files with 36 additions and 45 deletions

View file

@ -219,7 +219,7 @@ abstract class Utils
return \preg_replace('/ ([\r]?[\n])/m', ' ', $sInputValue); 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; $sValue = $sEncodedValue;
if (\strlen($sIncomingCharset)) { if (\strlen($sIncomingCharset)) {
@ -276,8 +276,7 @@ abstract class Utils
} }
if (\strlen($aTempArr[0])) { if (\strlen($aTempArr[0])) {
$sCharset = \strlen($sForcedIncomingCharset) ? $sForcedIncomingCharset : $aTempArr[0]; $sCharset = static::NormalizeCharset($aTempArr[0], true);
$sCharset = static::NormalizeCharset($sCharset, true);
if ('' === $sMainCharset) { if ('' === $sMainCharset) {
$sMainCharset = $sCharset; $sMainCharset = $sCharset;

View file

@ -332,7 +332,7 @@ class MailClient
'folder' => $sFolderName, 'folder' => $sFolderName,
'uid' => $iUid, 'uid' => $iUid,
'subject' => $oHeaders->ValueByName(MimeHeader::SUBJECT, !$sContentTypeCharset), 'subject' => $oHeaders->ValueByName(MimeHeader::SUBJECT, !$sContentTypeCharset),
'from' => $oHeaders->GetAsEmailCollection(MimeHeader::FROM_, !$sContentTypeCharset) 'from' => $oHeaders->GetAsEmailCollection(MimeHeader::FROM_)
); );
} }
} }

View file

@ -151,7 +151,7 @@ class Message implements \JsonSerializable
$sCharset = $oBodyStructure ? Utils::NormalizeCharset($oBodyStructure->SearchCharset()) : ''; $sCharset = $oBodyStructure ? Utils::NormalizeCharset($oBodyStructure->SearchCharset()) : '';
$sHeaders = $oFetchResponse->GetHeaderFieldsValue(); $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) { if ($oHeaders) {
$oMessage->Headers = $oHeaders; $oMessage->Headers = $oHeaders;
@ -159,11 +159,9 @@ class Message implements \JsonSerializable
MimeHeader::CONTENT_TYPE, MimeHeader::CONTENT_TYPE,
\MailSo\Mime\Enumerations\Parameter::CHARSET \MailSo\Mime\Enumerations\Parameter::CHARSET
); );
if (\strlen($sContentTypeCharset)) { if (\strlen($sContentTypeCharset)) {
$sCharset = Utils::NormalizeCharset($sContentTypeCharset); $sCharset = Utils::NormalizeCharset($sContentTypeCharset);
} }
if (\strlen($sCharset)) { if (\strlen($sCharset)) {
$oHeaders->SetParentCharset($sCharset); $oHeaders->SetParentCharset($sCharset);
} }
@ -174,14 +172,14 @@ class Message implements \JsonSerializable
$oMessage->sMessageId = $oHeaders->ValueByName(MimeHeader::MESSAGE_ID); $oMessage->sMessageId = $oHeaders->ValueByName(MimeHeader::MESSAGE_ID);
$oMessage->sContentType = $oHeaders->ValueByName(MimeHeader::CONTENT_TYPE); $oMessage->sContentType = $oHeaders->ValueByName(MimeHeader::CONTENT_TYPE);
$oMessage->oFrom = $oHeaders->GetAsEmailCollection(MimeHeader::FROM_, $bCharsetAutoDetect); $oMessage->oFrom = $oHeaders->GetAsEmailCollection(MimeHeader::FROM_);
$oMessage->oTo = $oHeaders->GetAsEmailCollection(MimeHeader::TO_, $bCharsetAutoDetect); $oMessage->oTo = $oHeaders->GetAsEmailCollection(MimeHeader::TO_);
$oMessage->oCc = $oHeaders->GetAsEmailCollection(MimeHeader::CC, $bCharsetAutoDetect); $oMessage->oCc = $oHeaders->GetAsEmailCollection(MimeHeader::CC);
$oMessage->oBcc = $oHeaders->GetAsEmailCollection(MimeHeader::BCC, $bCharsetAutoDetect); $oMessage->oBcc = $oHeaders->GetAsEmailCollection(MimeHeader::BCC);
$oMessage->oSender = $oHeaders->GetAsEmailCollection(MimeHeader::SENDER, $bCharsetAutoDetect); $oMessage->oSender = $oHeaders->GetAsEmailCollection(MimeHeader::SENDER);
$oMessage->oReplyTo = $oHeaders->GetAsEmailCollection(MimeHeader::REPLY_TO, $bCharsetAutoDetect); $oMessage->oReplyTo = $oHeaders->GetAsEmailCollection(MimeHeader::REPLY_TO);
$oMessage->oDeliveredTo = $oHeaders->GetAsEmailCollection(MimeHeader::DELIVERED_TO, $bCharsetAutoDetect); $oMessage->oDeliveredTo = $oHeaders->GetAsEmailCollection(MimeHeader::DELIVERED_TO);
$oMessage->InReplyTo = $oHeaders->ValueByName(MimeHeader::IN_REPLY_TO); $oMessage->InReplyTo = $oHeaders->ValueByName(MimeHeader::IN_REPLY_TO);
$oMessage->References = Utils::StripSpaces( $oMessage->References = Utils::StripSpaces(

View file

@ -43,6 +43,7 @@ class Email implements \JsonSerializable
*/ */
public static function Parse(string $sEmailAddress) : self public static function Parse(string $sEmailAddress) : self
{ {
$sEmailAddress = \MailSo\Base\Utils::DecodeHeaderValue($sEmailAddress);
$sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress); $sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress);
if (!\strlen(\trim($sEmailAddress))) { if (!\strlen(\trim($sEmailAddress))) {
throw new \ValueError; throw new \ValueError;

View file

@ -23,7 +23,7 @@ class Header implements \JsonSerializable
private string $sFullValue; private string $sFullValue;
private string $sEncodedValueForReparse; private string $sEncodedValue;
private ?ParameterCollection $oParameters = null; private ?ParameterCollection $oParameters = null;
@ -39,10 +39,10 @@ class Header implements \JsonSerializable
{ {
$this->sName = \trim($sName); $this->sName = \trim($sName);
$this->sFullValue = \trim($sValue); $this->sFullValue = \trim($sValue);
$this->sEncodedValueForReparse = ''; $this->sEncodedValue = '';
if (\strlen($sEncodedValueForReparse) && ($this->IsEmail() || $this->IsSubject() || $this->IsParameterized())) { if (\strlen($sEncodedValueForReparse) && ($this->IsEmail() || $this->IsSubject() || $this->IsParameterized())) {
$this->sEncodedValueForReparse = \trim($sEncodedValueForReparse); $this->sEncodedValue = \trim($sEncodedValueForReparse);
} }
if (\strlen($this->sFullValue) && $this->IsParameterized()) { if (\strlen($this->sFullValue) && $this->IsParameterized()) {
@ -101,13 +101,18 @@ class Header implements \JsonSerializable
return $this->sFullValue; return $this->sFullValue;
} }
public function EncodedValue() : string
{
return $this->sEncodedValue ?: $this->sFullValue;
}
public function SetParentCharset(string $sParentCharset) : Header public function SetParentCharset(string $sParentCharset) : Header
{ {
if ($this->sParentCharset !== $sParentCharset && \strlen($this->sEncodedValueForReparse)) { if ($this->sParentCharset !== $sParentCharset && \strlen($this->sEncodedValue)) {
$this->initInputData( $this->initInputData(
$this->sName, $this->sName,
\trim(\MailSo\Base\Utils::DecodeHeaderValue($this->sEncodedValueForReparse, $sParentCharset)), \trim(\MailSo\Base\Utils::DecodeHeaderValue($this->sEncodedValue, $sParentCharset)),
$this->sEncodedValueForReparse $this->sEncodedValue
); );
} }
@ -187,9 +192,9 @@ class Header implements \JsonSerializable
public function ValueWithCharsetAutoDetect() : string public function ValueWithCharsetAutoDetect() : string
{ {
if (!\MailSo\Base\Utils::IsAscii($this->Value()) if (!\MailSo\Base\Utils::IsAscii($this->Value())
&& \strlen($this->sEncodedValueForReparse) && \strlen($this->sEncodedValue)
&& !\MailSo\Base\Utils::IsAscii($this->sEncodedValueForReparse) && !\MailSo\Base\Utils::IsAscii($this->sEncodedValue)
&& ($mEncoding = \mb_detect_encoding($this->sEncodedValueForReparse, 'auto', true)) && ($mEncoding = \mb_detect_encoding($this->sEncodedValue, 'auto', true))
) { ) {
$this->SetParentCharset($mEncoding); $this->SetParentCharset($mEncoding);
} }

View file

@ -17,16 +17,13 @@ namespace MailSo\Mime;
*/ */
class HeaderCollection extends \MailSo\Base\Collection class HeaderCollection extends \MailSo\Base\Collection
{ {
protected string $sRawHeaders = '';
protected string $sParentCharset = ''; protected string $sParentCharset = '';
function __construct(string $sRawHeaders = '', bool $bStoreRawHeaders = true, string $sParentCharset = '') function __construct(string $sRawHeaders = '', string $sParentCharset = '')
{ {
parent::__construct(); parent::__construct();
if (\strlen($sRawHeaders)) { 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()) : ''; return $oHeader ? ($bCharsetAutoDetect ? $oHeader->ValueWithCharsetAutoDetect() : $oHeader->Value()) : '';
} }
public function ValuesByName(string $sHeaderName, bool $bCharsetAutoDetect = false) : array public function ValuesByName(string $sHeaderName) : array
{ {
$aResult = array(); $aResult = array();
$sHeaderNameLower = \strtolower($sHeaderName); $sHeaderNameLower = \strtolower($sHeaderName);
foreach ($this as $oHeader) { foreach ($this as $oHeader) {
if ($sHeaderNameLower === \strtolower($oHeader->Name())) { if ($sHeaderNameLower === \strtolower($oHeader->Name())) {
$aResult[] = $bCharsetAutoDetect ? $oHeader->ValueWithCharsetAutoDetect() : $oHeader->Value(); $aResult[] = $oHeader->Value();
} }
} }
return $aResult; return $aResult;
@ -74,11 +71,12 @@ class HeaderCollection extends \MailSo\Base\Collection
return $this; return $this;
} }
public function GetAsEmailCollection(string $sHeaderName, bool $bCharsetAutoDetect = false) : ?EmailCollection public function GetAsEmailCollection(string $sHeaderName) : ?EmailCollection
{ {
return new EmailCollection( if ($oHeader = $this->GetByName($sHeaderName)) {
$this->ValueByName($sHeaderName, $bCharsetAutoDetect) return new EmailCollection($oHeader->EncodedValue());
); }
return new EmailCollection();
} }
public function ParameterValue(string $sHeaderName, string $sParamName) : string public function ParameterValue(string $sHeaderName, string $sParamName) : string
@ -110,20 +108,10 @@ class HeaderCollection extends \MailSo\Base\Collection
return $this; return $this;
} }
public function Clear() : void public function Parse(string $sRawHeaders, string $sParentCharset = '') : self
{
parent::Clear();
$this->sRawHeaders = '';
}
public function Parse(string $sRawHeaders, bool $bStoreRawHeaders = false, string $sParentCharset = '') : self
{ {
$this->Clear(); $this->Clear();
if ($bStoreRawHeaders) {
$this->sRawHeaders = $sRawHeaders;
}
if (\strlen($this->sParentCharset)) { if (\strlen($this->sParentCharset)) {
$this->sParentCharset = $sParentCharset; $this->sParentCharset = $sParentCharset;
} }