Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.11.2@d470903722cfcbc1cd04744c5491d3e6d13ec3d9">
<files psalm-version="3.11.4@58e1d8e68e5098bf4fbfdfb420c38d563f882549">
<file src="examples/get_and_parse_all_emails_without_saving_attachments.php">
<UnusedVariable occurrences="1">
<code>$mailbox</code>
Expand Down Expand Up @@ -34,13 +34,6 @@
<code>$element-&gt;charset</code>
<code>$element-&gt;text</code>
</InvalidArgument>
<MixedInferredReturnType occurrences="1">
<code>array</code>
</MixedInferredReturnType>
<PossiblyUndefinedVariable occurrences="2">
<code>$lowercase_encodings</code>
<code>$lowercase_encodings</code>
</PossiblyUndefinedVariable>
<PossiblyUnusedMethod occurrences="24">
<code>setConnectionRetry</code>
<code>setConnectionRetryDelay</code>
Expand Down
2 changes: 1 addition & 1 deletion src/PhpImap/IncomingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function embedImageAttachments(): void
foreach ($this->getAttachments() as $attachment) {
if ($attachment->contentId == $cid && 'inline' == $attachment->disposition) {
$contents = $attachment->getContents();
$contentType = $attachment->getFileInfo(FILEINFO_MIME);
$contentType = (string) $attachment->getFileInfo(FILEINFO_MIME);

if (!\strstr($contentType, 'image')) {
continue;
Expand Down
30 changes: 17 additions & 13 deletions src/PhpImap/IncomingMailAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace PhpImap;

use Exception;
use const FILEINFO_MIME;
use const FILEINFO_NONE;
use finfo;
use Throwable;
use UnexpectedValueException;

/**
Expand All @@ -14,6 +16,8 @@
* @author Barbushin Sergey http://linkedin.com/in/barbushin
*
* @property string $filePath lazy attachment data file
*
* @psalm-type fileinfoconst = 0|2|16|1024|1040|8|32|128|256|16777216
*/
class IncomingMailAttachment
{
Expand All @@ -23,10 +27,10 @@ class IncomingMailAttachment
/** @var string|null */
public $contentId;

/** @var integer|null */
/** @var int|null */
public $type;

/** @var integer|null */
/** @var int|null */
public $encoding;

/** @var string|null */
Expand All @@ -38,7 +42,7 @@ class IncomingMailAttachment
/** @var string|null */
public $name;

/** @var integer|null */
/** @var int|null */
public $sizeInBytes;

/** @var string|null */
Expand Down Expand Up @@ -122,25 +126,25 @@ public function addDataPartInfo(DataPartInfo $dataInfo): void
/**
* Gets information about a file.
*
* @param const $fileinfo_const Any predefined constant. See https://www.php.net/manual/en/fileinfo.constants.php
* @param int $fileinfo_const Any predefined constant. See https://www.php.net/manual/en/fileinfo.constants.php
*
* @psalm-param fileinfoconst $fileinfo_const
*
* @return string|null
*/
public function getFileInfo($fileinfo_const = FILEINFO_NONE): string
public function getFileInfo($fileinfo_const = FILEINFO_NONE)
{
if (($fileinfo_const == FILEINFO_MIME) AND ($this->mimeType != false)) {
if ((FILEINFO_MIME == $fileinfo_const) and (false != $this->mimeType)) {
return $this->mimeType;
}

try {
$finfo = new finfo($fileinfo_const);
} catch (Exception $ex) {
return null;
}

if (!$finfo) {
return $finfo->buffer($this->getContents());
} catch (Throwable $ex) {
return null;
}

return $finfo->buffer($this->getContents());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/PhpImap/IncomingMailHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class IncomingMailHeader
/** @var int|null The IMAP message ID - not the "Message-ID:"-header of the email */
public $id;

/** @var string */
/** @var string|null */
public $imapPath;

/** @var string */
/** @var string|null */
public $mailboxFolder;

/** @var bool */
Expand Down
30 changes: 23 additions & 7 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,10 @@ public function decodeStringFromUtf7ImapToUtf8(string $str): string

/**
* Sets the folder of the current mailbox.
*
* @return void
*/
public function setMailboxFolder(): void
{
$imapPathParts = explode('}', $this->imapPath);
$imapPathParts = \explode('}', $this->imapPath);
$this->mailboxFolder = (!empty($imapPathParts[1])) ? $imapPathParts[1] : 'INBOX';
}

Expand Down Expand Up @@ -1219,17 +1217,35 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object
$fileName = $this->decodeRFC2231($fileName);
}

/** @var scalar|array|object|null */
$sizeInBytes = isset($partStructure->bytes) ? $partStructure->bytes : null;

/** @var scalar|array|object|null */
$encoding = isset($partStructure->encoding) ? $partStructure->encoding : null;

if (null !== $sizeInBytes && !\is_int($sizeInBytes)) {
throw new UnexpectedValueException('Supplied part structure specifies a non-integer, non-null bytes header!');
}
if (null !== $encoding && !\is_int($encoding)) {
throw new UnexpectedValueException('Supplied part structure specifies a non-integer, non-null encoding header!');
}
if (isset($partStructure->type) && !\is_int($partStructure->type)) {
throw new UnexpectedValueException('Supplied part structure specifies a non-integer, non-null type header!');
}

$partStructure_id = ($partStructure->ifid && isset($partStructure->id)) ? \trim($partStructure->id) : null;

$attachment = new IncomingMailAttachment();
$attachment->id = \bin2hex(\random_bytes(20));
$attachment->contentId = isset($partStructure_id) ? \trim($partStructure_id, ' <>') : null;
$attachment->type = isset($partStructure->type) ? $partStructure->type : null;
$attachment->encoding = isset($partStructure->encoding) ? $partStructure->encoding : null;
if (isset($partStructure->type)) {
$attachment->type = $partStructure->type;
}
$attachment->encoding = $encoding;
$attachment->subtype = ($partStructure->ifsubtype && isset($partStructure->subtype)) ? \trim($partStructure->subtype) : null;
$attachment->description = ($partStructure->ifdescription && isset($partStructure->description)) ? \trim($partStructure->description) : null;
$attachment->description = ($partStructure->ifdescription && isset($partStructure->description)) ? \trim((string) $partStructure->description) : null;
$attachment->name = $fileName;
$attachment->sizeInBytes = isset($partStructure->bytes) ? $partStructure->bytes : null;
$attachment->sizeInBytes = $sizeInBytes;
$attachment->disposition = (isset($partStructure->disposition) && \is_string($partStructure->disposition)) ? $partStructure->disposition : null;

/** @var scalar|array|object|resource|null */
Expand Down