From 3d63eeb79eebe1781f9b973b7df298ffe5521095 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Sun, 22 Jan 2023 16:07:49 +0100 Subject: [PATCH] Cleanup MIME part FileName handling --- .../libraries/MailSo/Imap/BodyStructure.php | 29 +++++++++++++- .../app/libraries/MailSo/Mail/Attachment.php | 35 ++--------------- .../app/libraries/MailSo/Mime/Attachment.php | 38 ++++++++----------- 3 files changed, 45 insertions(+), 57 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Imap/BodyStructure.php b/snappymail/v/0.0.0/app/libraries/MailSo/Imap/BodyStructure.php index 36e552664..c9c949081 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Imap/BodyStructure.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Imap/BodyStructure.php @@ -55,9 +55,34 @@ class BodyStructure return $this->sPartID; } - public function FileName() : string + public function FileName(bool $bCalculateOnEmpty = false) : string { - return $this->sFileName; + $sFileName = \trim($this->sFileName); + if (\strlen($sFileName) || !$bCalculateOnEmpty) { + return $sFileName; + } + + $sIdx = '-' . $this->PartID(); + + $sMimeType = \strtolower(\trim($this->ContentType())); + if ('message/rfc822' === $sMimeType) { + return "message{$sIdx}.eml"; + } + if ('text/calendar' === $sMimeType) { + return "calendar{$sIdx}.ics"; + } + if ('text/plain' === $sMimeType) { + return "part{$sIdx}.txt"; + } + if (\preg_match('@text/(vcard|html|csv|xml|css|asp)@', $sMimeType, $aMatch) + || \preg_match('@image/(png|jpeg|gif|bmp|cgm|ief|tiff|webp)@', $sMimeType, $aMatch)) { + return "part{$sIdx}.{$aMatch[1]}"; + } + if (\strlen($sMimeType)) { + return \str_replace('/', $sIdx.'.', $sMimeType); + } + + return ($this->IsInline() ? 'inline' : 'part' ) . $sIdx; } public function ContentType() : string diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Attachment.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Attachment.php index d931abed3..a7bda055e 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Attachment.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/Attachment.php @@ -53,36 +53,7 @@ class Attachment implements \JsonSerializable public function FileName(bool $bCalculateOnEmpty = false) : string { - if (!$this->oBodyStructure) { - return ''; - } - - $sFileName = \trim($this->oBodyStructure->FileName()); - if (\strlen($sFileName) || !$bCalculateOnEmpty) { - return $sFileName; - } - - $sIdx = '-' . $this->oBodyStructure->PartID(); - - $sMimeType = \strtolower(\trim($this->oBodyStructure->ContentType())); - if ('message/rfc822' === $sMimeType) { - return "message{$sIdx}.eml"; - } - if ('text/calendar' === $sMimeType) { - return "calendar{$sIdx}.ics"; - } - if ('text/plain' === $sMimeType) { - return "part{$sIdx}.txt"; - } - if (\preg_match('@text/(vcard|html|csv|xml|css|asp)@', $sMimeType, $aMatch) - || \preg_match('@image/(png|jpeg|gif|bmp|cgm|ief|tiff|webp)@', $sMimeType, $aMatch)) { - return "part{$sIdx}.{$aMatch[1]}"; - } - if (\strlen($sMimeType)) { - return \str_replace('/', $sIdx.'.', $sMimeType); - } - - return ($this->oBodyStructure->IsInline() ? 'inline' : 'part' ) . $sIdx; + return $this->oBodyStructure ? $this->oBodyStructure->FileName($bCalculateOnEmpty) : ''; } public function __call(string $name, array $arguments) //: mixed @@ -96,8 +67,8 @@ class Attachment implements \JsonSerializable return array( '@Object' => 'Object/Attachment', 'Folder' => $this->sFolder, - 'Uid' => (string) $this->iUid, - 'MimeIndex' => (string) $this->oBodyStructure->PartID(), + 'Uid' => $this->iUid, + 'MimeIndex' => $this->oBodyStructure->PartID(), 'MimeType' => $this->oBodyStructure->ContentType(), 'MimeTypeParams' => $this->oBodyStructure->ContentTypeParameters(), 'FileName' => \MailSo\Base\Utils::SecureFileName($this->FileName(true)), diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Attachment.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Attachment.php index b03a7e10b..a6c7ed850 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Attachment.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mime/Attachment.php @@ -104,22 +104,22 @@ class Attachment public function IsImage() : bool { - return 'image' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); + return 'image' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName); } public function IsArchive() : bool { - return 'archive' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); + return 'archive' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName); } public function IsPdf() : bool { - return 'pdf' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); + return 'pdf' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName); } public function IsDoc() : bool { - return 'doc' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); + return 'doc' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName); } public function IsLinked() : bool @@ -131,15 +131,14 @@ class Attachment { $oAttachmentPart = new Part; - $sFileName = $this->FileName(); + $sFileName = \trim($this->sFileName); $sCID = $this->CID(); $sContentLocation = $this->ContentLocation(); $oContentTypeParameters = null; $oContentDispositionParameters = null; - if (\strlen(\trim($sFileName))) - { + if (\strlen($sFileName)) { $oContentTypeParameters = (new ParameterCollection)->Add(new Parameter( Enumerations\Parameter::NAME, $sFileName)); @@ -163,15 +162,13 @@ class Attachment ) ); - if (\strlen($sCID)) - { + if (\strlen($sCID)) { $oAttachmentPart->Headers->append( new Header(Enumerations\Header::CONTENT_ID, $sCID) ); } - if (\strlen($sContentLocation)) - { + if (\strlen($sContentLocation)) { $oAttachmentPart->Headers->append( new Header(Enumerations\Header::CONTENT_LOCATION, $sContentLocation) ); @@ -179,8 +176,7 @@ class Attachment $oAttachmentPart->Body = $this->Resource(); - if ('message/rfc822' !== \strtolower($this->ContentType())) - { + if ('message/rfc822' !== \strtolower($this->ContentType())) { $oAttachmentPart->Headers->append( new Header( Enumerations\Header::CONTENT_TRANSFER_ENCODING, @@ -188,17 +184,13 @@ class Attachment ) ); - if (\is_resource($oAttachmentPart->Body)) - { - if (!\MailSo\Base\StreamWrappers\Binary::IsStreamRemembed($oAttachmentPart->Body)) - { - $oAttachmentPart->Body = - \MailSo\Base\StreamWrappers\Binary::CreateStream($oAttachmentPart->Body, - \MailSo\Base\StreamWrappers\Binary::GetInlineDecodeOrEncodeFunctionName( - \MailSo\Base\Enumerations\Encoding::BASE64, false)); + if (\is_resource($oAttachmentPart->Body) && !\MailSo\Base\StreamWrappers\Binary::IsStreamRemembed($oAttachmentPart->Body)) { + $oAttachmentPart->Body = + \MailSo\Base\StreamWrappers\Binary::CreateStream($oAttachmentPart->Body, + \MailSo\Base\StreamWrappers\Binary::GetInlineDecodeOrEncodeFunctionName( + \MailSo\Base\Enumerations\Encoding::BASE64, false)); - \MailSo\Base\StreamWrappers\Binary::RememberStream($oAttachmentPart->Body); - } + \MailSo\Base\StreamWrappers\Binary::RememberStream($oAttachmentPart->Body); } }