Skip to content
Open
Changes from 6 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
38 changes: 27 additions & 11 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ class Mailbox
/** @var string */
protected $mailboxFolder;

/** @var bool|false */
protected $attachmentFilenameMode = false;

public const ATTACH_FILE_NAMERANDOM = 1; // Filename is unique (random)
public const ATTACH_FILE_NAMEORIGINAL = 2; // Filename is Attachment-Filename
/** @var int */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @var int */
/** @var int */

protected $attachmentsFilenameMode = self::ATTACH_FILE_NAMERANDOM;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected $attachmentsFilenameMode = self::ATTACH_FILE_NAMERANDOM;
protected $attachmentsFilenameMode = self::ATTACH_FILE_NAMERANDOM;

/** @var resource|null */
private $imapStream;

Expand Down Expand Up @@ -326,7 +327,20 @@ public function setAttachmentsIgnore(bool $attachmentsIgnore): void
{
$this->attachmentsIgnore = $attachmentsIgnore;
}


/**
* Set $this->setAttachmentsRandomFilename param.
*
* @param int $random ATTACH_FILE_NAMERANDOM, ATTACH_FILE_NAMEORIGINAL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable in the comment should match the actual parameter variable.

Suggested change
* @param int $random ATTACH_FILE_NAMERANDOM, ATTACH_FILE_NAMEORIGINAL
* @param int $mode ATTACH_FILE_NAMERANDOM, ATTACH_FILE_NAMEORIGINAL

*
* @return Mailbox
*/
public function setAttachmentsFilenameMode(int $mode) : Mailbox
{
$this->attachmentsFilenameMode = $mode;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss a check here, which ensures, that only supported modes can be set. Something like this:

Suggested change
$this->attachmentsFilenameMode = $mode;
if (!defined($mode))
{
throw new UnexpectedValueException("The defined mode '$mode' is not supported. Supported modes: ATTACH_FILE_NAMERANDOM, ATTACH_FILE_NAMEORIGINAL");
}
$this->attachmentsFilenameMode = $mode;

Note: I haven't tested this code.

return $this;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this return here? Wouldn't be a void function enough?

}

/**
* Get $this->attachmentsIgnore param.
*
Expand Down Expand Up @@ -1401,20 +1415,22 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object

$attachmentsDir = $this->getAttachmentsDir();

if (null != $attachmentsDir) {
if (true == $this->getAttachmentFilenameMode()) {
$fileSysName = $attachment->name;
} else {
$fileSysName = \bin2hex(\random_bytes(16)).'.bin';
if (null !== $attachmentsDir) {
switch($this->attachmentsFilenameMode) {
case self::ATTACH_FILE_NAMEORIGINAL:
$fileSysName = $fileName;
break;
case self::ATTACH_FILE_NAMERANDOM:
default:
$fileSysName = \bin2hex(\random_bytes(16)).'.bin';
}

$filePath = $attachmentsDir.DIRECTORY_SEPARATOR.$fileSysName;

if (\strlen($filePath) > self::MAX_LENGTH_FILEPATH) {
$ext = \pathinfo($filePath, PATHINFO_EXTENSION);
$filePath = \substr($filePath, 0, self::MAX_LENGTH_FILEPATH - 1 - \strlen($ext)).'.'.$ext;
}

$attachment->setFilePath($filePath);
$attachment->saveToDisk();
}
Expand Down