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);
}
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;

View file

@ -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_)
);
}
}

View file

@ -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(

View file

@ -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;

View file

@ -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);
}

View file

@ -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;
}