From a70bed9c01de71bc3cf08ba320c03969dd144f97 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 09:25:46 +0100 Subject: [PATCH 01/32] rename barbushin/php-imap#509 test to follow pattern of other tests --- tests/unit/{Issue509.php => Issue509Test.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/unit/{Issue509.php => Issue509Test.php} (98%) diff --git a/tests/unit/Issue509.php b/tests/unit/Issue509Test.php similarity index 98% rename from tests/unit/Issue509.php rename to tests/unit/Issue509Test.php index 89c2e1e2..739b8a40 100644 --- a/tests/unit/Issue509.php +++ b/tests/unit/Issue509Test.php @@ -12,7 +12,7 @@ use PHPUnit\Framework\TestCase; -class Issue509 extends TestCase +class Issue509Test extends TestCase { const base64 = 'vsiz58fPvcq0z7HuLiC05MDlx9jB1rzFvK0gsKi758fVtM+02S4NCsDMt7EgwM/AuiC16b7uILq7 From 087aba4262cdb255fe9b34622d1860b406e36570 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 11:09:36 +0100 Subject: [PATCH 02/32] bumping minimum version of psalm --- composer.json | 2 +- psalm.baseline.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 14603fe2..62043055 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "psalm/plugin-phpunit": "^0.10.0", "roave/security-advisories": "dev-master", "sebastian/phpcpd": "^4.1", - "vimeo/psalm": "^3.11.5" + "vimeo/psalm": "^3.11.6" }, "scripts": { "static-analysis": [ diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 76fee147..a237f656 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -1,5 +1,5 @@ - + $mailbox From 909046dbbf62b897161ac689bd7c1083f4740303 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 11:09:47 +0100 Subject: [PATCH 03/32] updating psalm baseline --- psalm.baseline.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/psalm.baseline.xml b/psalm.baseline.xml index a237f656..dbf7e12a 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -13,9 +13,6 @@ - - replaceInternalLinks - $this->dataInfo From 05ebc5a87446e6c657d0628db0ab6c82dd0a4951 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 11:10:03 +0100 Subject: [PATCH 04/32] partially satisfying psalm --- src/PhpImap/Imap.php | 12 +++++++++--- src/PhpImap/Mailbox.php | 1 - 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/PhpImap/Imap.php b/src/PhpImap/Imap.php index 3762410f..f2ea3477 100644 --- a/src/PhpImap/Imap.php +++ b/src/PhpImap/Imap.php @@ -181,7 +181,6 @@ public static function clearflag_full( * @param false|resource $imap_stream * * @psalm-param value-of $flag - * @psalm-param 0|32768 $flag * * @return true */ @@ -189,6 +188,9 @@ public static function close($imap_stream, int $flag = 0): bool { \imap_errors(); // flush errors + /** @var int */ + $flag = $flag; + $result = \imap_close(self::EnsureConnection($imap_stream, __METHOD__, 1), $flag); if (false === $result) { @@ -890,7 +892,6 @@ public static function setflag_full( * @param false|resource $imap_stream * * @psalm-param value-of $criteria - * @psalm-param 1|5|0|2|6|3|4 $criteria * * @return int[] * @@ -909,6 +910,9 @@ public static function sort( $imap_stream = self::EnsureConnection($imap_stream, __METHOD__, 1); $reverse = (int) $reverse; + /** @var int */ + $criteria = $criteria; + if (null !== $search_criteria && null !== $charset) { $result = \imap_sort( $imap_stream, @@ -990,7 +994,6 @@ public static function subscribe( /** * @psalm-param value-of $timeout_type - * @psalm-param 4|1|2|3 $timeout_type * * @return true|int */ @@ -1000,6 +1003,9 @@ public static function timeout( ) { \imap_errors(); // flush errors + /** @var int */ + $timeout_type = $timeout_type; + $result = \imap_timeout( $timeout_type, $timeout diff --git a/src/PhpImap/Mailbox.php b/src/PhpImap/Mailbox.php index a50befbf..a3e47d74 100644 --- a/src/PhpImap/Mailbox.php +++ b/src/PhpImap/Mailbox.php @@ -945,7 +945,6 @@ public function getMailboxInfo(): object * @param string|null $searchCriteria See http://php.net/imap_search for a complete list of available criteria * * @psalm-param value-of $criteria - * @psalm-param 1|5|0|2|6|3|4 $criteria * * @return array Mails ids */ From 0e267998edaa7240e80e1575fb3e340396d83c97 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 29 Jun 2020 10:54:21 +0100 Subject: [PATCH 05/32] adding test for barbushin/php-imap#523 --- tests/unit/ImapTest.php | 99 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/unit/ImapTest.php diff --git a/tests/unit/ImapTest.php b/tests/unit/ImapTest.php new file mode 100644 index 00000000..ed50e967 --- /dev/null +++ b/tests/unit/ImapTest.php @@ -0,0 +1,99 @@ + + * } $args + */ +class ImapTest extends Base +{ + /** + * @psalm-return Generator, + * 1:string, + * 2:PSALM_OPEN_ARGS, + * 3?:bool + * }> + */ + public function OpenFailure(): Generator + { + yield 'empty mailbox/username/password' => [ + UnexpectedValueException::class, + 'IMAP error:Can\'t open mailbox : no such mailbox', + [ + new HiddenString(''), + new HiddenString(''), + new HiddenString(''), + 0, + 0, + [], + ], + ]; + + $imapPath = \getenv('PHPIMAP_IMAP_PATH'); + $login = \getenv('PHPIMAP_LOGIN'); + $password = \getenv('PHPIMAP_PASSWORD'); + + if (\is_string($imapPath) && \is_string($login) && \is_string($password)) { + yield 'CI ENV with invalid password' => [ + UnexpectedValueException::class, + 'IMAP error:[AUTHENTICATIONFAILED] Authentication failed.', + [ + new HiddenString($imapPath, true, true), + new HiddenString($login, true, true), + new HiddenString(\strrev($password), true, true), + 0, + 0, + [], + ], + ]; + } + } + + /** + * @dataProvider OpenFailure + * + * @psalm-param class-string $exception + * @psalm-param PSALM_OPEN_ARGS $args + */ + public function testOpenFailure( + string $exception, + string $message, + array $args, + bool $message_as_regex = false + ): void { + $this->expectException($exception); + + if ($message_as_regex) { + $this->expectExceptionMessageMatches($message); + } else { + $this->expectExceptionMessage($message); + } + + Imap::open( + $args[0]->getString(), + $args[1]->getString(), + $args[2]->getString(), + $args[3], + $args[4], + $args[5] + ); + } +} From e1c5000589bcbe5922b7cd8c3d0f6ac11633de3a Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 29 Jun 2020 11:07:36 +0100 Subject: [PATCH 06/32] applies suggested fix for barbushin/php-imap#523 ref: https://github.com/barbushin/php-imap/issues/523#issue-646947905 --- src/PhpImap/Imap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/Imap.php b/src/PhpImap/Imap.php index f2ea3477..c2821292 100644 --- a/src/PhpImap/Imap.php +++ b/src/PhpImap/Imap.php @@ -701,7 +701,7 @@ public static function open( \imap_errors(); // flush errors - $result = \imap_open($mailbox, $username, $password, $options, $n_retries, $params); + $result = @\imap_open($mailbox, $username, $password, $options, $n_retries, $params); if (!$result) { $lastError = \imap_last_error(); From d0e49dbbc1a922c1c85bd957b6ffbde15fc37222 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 29 Jun 2020 11:22:48 +0100 Subject: [PATCH 07/32] switch to regex match for differing error formats --- tests/unit/ImapTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/ImapTest.php b/tests/unit/ImapTest.php index ed50e967..77ab3c66 100644 --- a/tests/unit/ImapTest.php +++ b/tests/unit/ImapTest.php @@ -54,7 +54,7 @@ public function OpenFailure(): Generator if (\is_string($imapPath) && \is_string($login) && \is_string($password)) { yield 'CI ENV with invalid password' => [ UnexpectedValueException::class, - 'IMAP error:[AUTHENTICATIONFAILED] Authentication failed.', + '/^IMAP error:.*\[AUTHENTICATIONFAILED\].*/', [ new HiddenString($imapPath, true, true), new HiddenString($login, true, true), @@ -63,6 +63,7 @@ public function OpenFailure(): Generator 0, [], ], + true, ]; } } From e23f31dc808af1058e11af38e668d6c6215bfcaf Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 11:18:24 +0100 Subject: [PATCH 08/32] setting parameter type --- src/PhpImap/IncomingMailAttachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/IncomingMailAttachment.php b/src/PhpImap/IncomingMailAttachment.php index 66a60784..48e59f5d 100644 --- a/src/PhpImap/IncomingMailAttachment.php +++ b/src/PhpImap/IncomingMailAttachment.php @@ -129,7 +129,7 @@ public function addDataPartInfo(DataPartInfo $dataInfo): void * * @psalm-param fileinfoconst $fileinfo_const */ - public function getFileInfo($fileinfo_const = FILEINFO_NONE): string + public function getFileInfo(int $fileinfo_const = FILEINFO_NONE): string { if ((FILEINFO_MIME == $fileinfo_const) and (false != $this->mimeType)) { return $this->mimeType; From 46b021cec65e54c4fb02225c89f9b64fdbb7d8df Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 11:20:41 +0100 Subject: [PATCH 09/32] adding test for barbushin/php-imap#519 --- .../unit/Fixtures/IncomingMailAttachment.php | 27 +++ tests/unit/Issue519Test.php | 182 ++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 tests/unit/Fixtures/IncomingMailAttachment.php create mode 100644 tests/unit/Issue519Test.php diff --git a/tests/unit/Fixtures/IncomingMailAttachment.php b/tests/unit/Fixtures/IncomingMailAttachment.php new file mode 100644 index 00000000..2791354b --- /dev/null +++ b/tests/unit/Fixtures/IncomingMailAttachment.php @@ -0,0 +1,27 @@ +override_getFileInfo_mime) + ) { + return $this->override_getFileInfo_mime; + } + + return parent::getFileInfo($fileinfo_const); + } +} diff --git a/tests/unit/Issue519Test.php b/tests/unit/Issue519Test.php new file mode 100644 index 00000000..f79caf1f --- /dev/null +++ b/tests/unit/Issue519Test.php @@ -0,0 +1,182 @@ +'; + + const MIME = 'image/jpeg'; + + const EXPECTED_ATTACHMENT_COUNT = 1; + + const EXPECTED_ATTACHMENT_COUNT_AFTER_EMBED = 0; + + /** + * @psalm-return array + */ + public function provider(): array + { + $out = []; + + foreach (self::HEADER_VALUES as $value) { + $out[$value] = [$value]; + } + + return $out; + } + + /** + * @dataProvider provider + */ + public function test(string $header_value): void + { + $mailbox = new Mailbox('', '', ''); + $mail = new IncomingMail(); + $attachment = new Fixtures\IncomingMailAttachment(); + $part = new Fixtures\DataPartInfo( + $mailbox, + 0, + 0, + ENCBASE64, + 0 + ); + + $html = new Fixtures\DataPartInfo( + $mailbox, + 0, + 0, + ENC8BIT, + 0 + ); + + $html_string = ''; + + $html->setData($html_string); + $part->setData(''); + + $attachment->id = self::ID; + $attachment->contentId = self::ID; + $attachment->type = TYPEIMAGE; + $attachment->encoding = ENCBASE64; + $attachment->subtype = self::SUBTYPE; + $attachment->description = self::ID; + $attachment->name = self::ID; + $attachment->sizeInBytes = self::SIZE_IN_BYTES; + $attachment->disposition = $header_value; + $attachment->override_getFileInfo_mime = self::MIME; + + $attachment->addDataPartInfo($part); + + $mail->addDataPartInfo($html, DataPartInfo::TEXT_HTML); + $mail->addAttachment($attachment); + + $this->assertTrue($mail->hasAttachments()); + + $this->assertCount( + self::EXPECTED_ATTACHMENT_COUNT, + $mail->getAttachments() + ); + + $this->assertSame($html_string, $mail->textHtml); + + $mail->embedImageAttachments(); + + $this->assertCount( + self::EXPECTED_ATTACHMENT_COUNT_AFTER_EMBED, + $mail->getAttachments() + ); + + $this->assertSame(self::HTML_EMBED, $mail->textHtml); + } +} From 3d4566f094522639a84e29e1c1c2eadca4d9af87 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 22 Jun 2020 11:28:06 +0100 Subject: [PATCH 10/32] applying fix for barbushin/php-imap#519 --- src/PhpImap/IncomingMail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/IncomingMail.php b/src/PhpImap/IncomingMail.php index 02188357..42684702 100644 --- a/src/PhpImap/IncomingMail.php +++ b/src/PhpImap/IncomingMail.php @@ -219,7 +219,7 @@ public function embedImageAttachments(): void $cid = \str_replace('cid:', '', $match); foreach ($attachments as $attachment) { - if ($attachment->contentId == $cid && 'inline' == $attachment->disposition) { + if ($attachment->contentId == $cid && 'inline' == \mb_strtolower((string) $attachment->disposition)) { $contents = $attachment->getContents(); $contentType = (string) $attachment->getFileInfo(FILEINFO_MIME); From 1c0190e11cf42a76bd03613fa41cc6385c2a5a3e Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 11:21:44 +0100 Subject: [PATCH 11/32] updating psalm --- composer.json | 2 +- psalm.baseline.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 62043055..83bf5c83 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "psalm/plugin-phpunit": "^0.10.0", "roave/security-advisories": "dev-master", "sebastian/phpcpd": "^4.1", - "vimeo/psalm": "^3.11.6" + "vimeo/psalm": "^3.12" }, "scripts": { "static-analysis": [ diff --git a/psalm.baseline.xml b/psalm.baseline.xml index dbf7e12a..bdacd2e5 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -1,5 +1,5 @@ - + $mailbox From 225d8cd8aee0bd78a323021ac804e322ef30afa1 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 11:22:08 +0100 Subject: [PATCH 12/32] updating psalm baseline --- psalm.baseline.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/psalm.baseline.xml b/psalm.baseline.xml index bdacd2e5..37c95993 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -12,11 +12,6 @@ \is_resource($maybe) - - - $this->dataInfo - - \in_array($imapSearchOption, $supported_options, true) From 37bc33bdfdecf0cc113b0df6d0aff2c976617d51 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 11:25:14 +0100 Subject: [PATCH 13/32] updating baseline --- psalm.baseline.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 37c95993..12cc8fb8 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -18,11 +18,8 @@ \in_array($key, $supported_params, true) - $element->charset $element->charset $element->text - $element->charset - $element->charset $element->text From 9b4e29e66aa6c85e7f58804874bba7377df79ccd Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 11:25:36 +0100 Subject: [PATCH 14/32] supress issues relating to nikic/php-parser#690 --- psalm.baseline.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 12cc8fb8..49aac0ba 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -54,4 +54,11 @@ self::ANYTHING + + + Match + ; + ; + + From ec565d4c1a9c3557488626dd358a2d198419aca0 Mon Sep 17 00:00:00 2001 From: Marv Date: Fri, 31 Jul 2020 09:33:32 +0100 Subject: [PATCH 15/32] refer to method present since 1.0 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d97e108d..4efea0fd 100644 --- a/README.md +++ b/README.md @@ -121,12 +121,12 @@ echo "\n\nAttachments:\n"; print_r($mail->getAttachments()); ``` -Method imap() allows to call any imap function in a context of the the instance: +Checking the currently selected mailbox: ```php // Call imap_check(); // http://php.net/manual/en/function.imap-check.php -$info = $mailbox->imap('check'); // +$info = $mailbox->checkMailbox(); // // Show current time for the mailbox $currentServerTime = isset($info->Date) && $info->Date ? date('Y-m-d H:i:s', strtotime($info->Date)) : 'Unknown'; From 882daf09c85278fd65260711c45787d7f6b5b960 Mon Sep 17 00:00:00 2001 From: Marv Date: Fri, 31 Jul 2020 09:44:32 +0100 Subject: [PATCH 16/32] adding note for users upgrading from 3.x --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 4efea0fd..12942857 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,30 @@ foreach($folders as $folder) { print_r($mails_ids); ``` +### Upgrading from 3.x + +Prior to 3.1, `Mailbox` used a "magic" method (`Mailbox::imap()`), with the +class `Imap` now performing it's purpose to call many `imap_*` functions with +automated string encoding/decoding of arguments and return values: + +Before: + +```php + public function checkMailbox() + { + return $this->imap('check'); + } +``` + +After: + +```php + public function checkMailbox(): object + { + return Imap::check($this->getImapStream()); + } +``` + ### Recommended * Google Chrome extension [PHP Console](https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef) From 63cdfa049209cdce577b0c93a79debeecd5455cb Mon Sep 17 00:00:00 2001 From: Marv Date: Fri, 31 Jul 2020 09:49:52 +0100 Subject: [PATCH 17/32] restricting compatible versions of nikic/php-parser as per vimeo/psalm@3f06d4f706e556f5535ba2acba4168ca775299df --- composer.json | 1 + psalm.baseline.xml | 7 ------- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 83bf5c83..c82f2281 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "friendsofphp/php-cs-fixer": "^2.16", "jakub-onderka/php-parallel-lint": "^1.0", "maglnet/composer-require-checker": "^2.0", + "nikic/php-parser": "^4.3,<4.7", "paragonie/hidden-string": "^1.0", "phpunit/phpunit": "^8.5", "povils/phpmnd": "^2.2", diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 49aac0ba..12cc8fb8 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -54,11 +54,4 @@ self::ANYTHING - - - Match - ; - ; - - From 41ab2614c8ea6037de8b65e7bc14ee7b2c024733 Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 14:12:03 +0100 Subject: [PATCH 18/32] fork methods to trait for reuse --- tests/unit/AbstractLiveMailboxTest.php | 72 +-------------------- tests/unit/LiveMailboxTestingTrait.php | 88 ++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 69 deletions(-) create mode 100644 tests/unit/LiveMailboxTestingTrait.php diff --git a/tests/unit/AbstractLiveMailboxTest.php b/tests/unit/AbstractLiveMailboxTest.php index cae4f8a3..450b8209 100644 --- a/tests/unit/AbstractLiveMailboxTest.php +++ b/tests/unit/AbstractLiveMailboxTest.php @@ -16,13 +16,8 @@ use Throwable; /** - * @psalm-type MAILBOX_ARGS = array{ - * 0:HiddenString, - * 1:HiddenString, - * 2:HiddenString, - * 3:string, - * 4?:string - * } + * @psalm-import-type MAILBOX_ARGS from LiveMailboxTestingTrait + * * @psalm-type COMPOSE_ENVELOPE = array{ * subject?:string * } @@ -41,25 +36,7 @@ */ abstract class AbstractLiveMailboxTest extends TestCase { - /** - * Provides constructor arguments for a live mailbox. - * - * @psalm-return MAILBOX_ARGS[] - */ - public function MailBoxProvider(): array - { - $sets = []; - - $imapPath = \getenv('PHPIMAP_IMAP_PATH'); - $login = \getenv('PHPIMAP_LOGIN'); - $password = \getenv('PHPIMAP_PASSWORD'); - - if (\is_string($imapPath) && \is_string($login) && \is_string($password)) { - $sets['CI ENV'] = [new HiddenString($imapPath), new HiddenString($login), new HiddenString($password, true, true), \sys_get_temp_dir()]; - } - - return $sets; - } + use LiveMailboxTestingTrait; /** * @psalm-return Generator @@ -193,49 +170,6 @@ public function testAppend( } } - /** - * Get instance of Mailbox, pre-set to a random mailbox. - * - * @param string $attachmentsDir - * @param string $serverEncoding - * - * @return mixed[] - * - * @psalm-return array{0:Mailbox, 1:string, 2:HiddenString} - */ - protected function getMailbox(HiddenString $imapPath, HiddenString $login, HiddenString $password, $attachmentsDir, $serverEncoding = 'UTF-8') - { - $mailbox = new Mailbox($imapPath->getString(), $login->getString(), $password->getString(), $attachmentsDir, $serverEncoding); - - $random = 'test-box-'.\date('c').\bin2hex(\random_bytes(4)); - - $mailbox->createMailbox($random); - - $mailbox->switchMailbox($random, false); - - return [$mailbox, $random, $imapPath]; - } - - /** - * @psalm-param MAILBOX_ARGS $mailbox_args - * - * @return mixed[] - * - * @psalm-return array{0:Mailbox, 1:string, 2:HiddenString} - */ - protected function getMailboxFromArgs(array $mailbox_args): array - { - list($path, $username, $password, $attachments_dir) = $mailbox_args; - - return $this->getMailbox( - $path, - $username, - $password, - $attachments_dir, - isset($mailbox_args[4]) ? $mailbox_args[4] : 'UTF-8' - ); - } - /** * Get subject search criteria and subject. * diff --git a/tests/unit/LiveMailboxTestingTrait.php b/tests/unit/LiveMailboxTestingTrait.php new file mode 100644 index 00000000..6f55d533 --- /dev/null +++ b/tests/unit/LiveMailboxTestingTrait.php @@ -0,0 +1,88 @@ +getString(), $login->getString(), $password->getString(), $attachmentsDir, $serverEncoding); + + $random = 'test-box-'.\date('c').\bin2hex(\random_bytes(4)); + + $mailbox->createMailbox($random); + + $mailbox->switchMailbox($random, false); + + return [$mailbox, $random, $imapPath]; + } + + /** + * @psalm-param MAILBOX_ARGS $mailbox_args + * + * @return mixed[] + * + * @psalm-return array{0:Mailbox, 1:string, 2:HiddenString} + */ + protected function getMailboxFromArgs(array $mailbox_args): array + { + list($path, $username, $password, $attachments_dir) = $mailbox_args; + + return $this->getMailbox( + $path, + $username, + $password, + $attachments_dir, + isset($mailbox_args[4]) ? $mailbox_args[4] : 'UTF-8' + ); + } +} From 470a2fda83cfd353ee8d88c8cf8113b0c6144c0f Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 14:12:56 +0100 Subject: [PATCH 19/32] implementing test for barbushin/php-imap#526 --- tests/unit/ImapTest.php | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/unit/ImapTest.php b/tests/unit/ImapTest.php index 77ab3c66..e9164a5b 100644 --- a/tests/unit/ImapTest.php +++ b/tests/unit/ImapTest.php @@ -9,10 +9,13 @@ use Generator; use ParagonIE\HiddenString\HiddenString; use PHPUnit\Framework\TestCase as Base; +use const SORTARRIVAL; use Throwable; use UnexpectedValueException; /** + * @psalm-import-type MAILBOX_ARGS from LiveMailboxTestingTrait + * * @psalm-type PSALM_OPEN_ARGS = array{ * 0:HiddenString, * 1:HiddenString, @@ -24,6 +27,8 @@ */ class ImapTest extends Base { + use LiveMailboxTestingTrait; + /** * @psalm-return Generator, @@ -97,4 +102,53 @@ public function testOpenFailure( $args[5] ); } + + /** + * @dataProvider MailBoxProvider + * + * @group live + * + * + */ + public function test_sort_empty( + HiddenString $path, + HiddenString $login, + HiddenString $password + ): void { + list($mailbox, $remove_mailbox, $path) = $this->getMailboxFromArgs([ + $path, + $login, + $password, + \sys_get_temp_dir() + ]); + + /** @var Throwable|null */ + $exception = null; + + $mailboxDeleted = false; + + try { + $this->assertSame( + [], + Imap::sort( + $mailbox->getImapStream(), + SORTARRIVAL, + false, + 0 + ) + ); + } catch (Throwable $ex) { + $exception = $ex; + } finally { + $mailbox->switchMailbox($path->getString()); + if (!$mailboxDeleted) { + $mailbox->deleteMailbox($remove_mailbox); + } + $mailbox->disconnect(); + } + + if (null !== $exception) { + throw $exception; + } + } } From 94df2b678e301ae18d32911d06d9129767008f2f Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 14:21:01 +0100 Subject: [PATCH 20/32] satisfy php-cs-fixer --- tests/unit/AbstractLiveMailboxTest.php | 1 - tests/unit/ImapTest.php | 8 +++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/unit/AbstractLiveMailboxTest.php b/tests/unit/AbstractLiveMailboxTest.php index 450b8209..8c570329 100644 --- a/tests/unit/AbstractLiveMailboxTest.php +++ b/tests/unit/AbstractLiveMailboxTest.php @@ -11,7 +11,6 @@ namespace PhpImap; use Generator; -use ParagonIE\HiddenString\HiddenString; use PHPUnit\Framework\TestCase; use Throwable; diff --git a/tests/unit/ImapTest.php b/tests/unit/ImapTest.php index e9164a5b..e0153235 100644 --- a/tests/unit/ImapTest.php +++ b/tests/unit/ImapTest.php @@ -107,10 +107,8 @@ public function testOpenFailure( * @dataProvider MailBoxProvider * * @group live - * - * */ - public function test_sort_empty( + public function testSortEmpty( HiddenString $path, HiddenString $login, HiddenString $password @@ -119,7 +117,7 @@ public function test_sort_empty( $path, $login, $password, - \sys_get_temp_dir() + \sys_get_temp_dir(), ]); /** @var Throwable|null */ @@ -137,7 +135,7 @@ public function test_sort_empty( 0 ) ); - } catch (Throwable $ex) { + } catch (Throwable $ex) { $exception = $ex; } finally { $mailbox->switchMailbox($path->getString()); From c3726775a14bb7ce8538c17842ef7271a9db237b Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 14:24:08 +0100 Subject: [PATCH 21/32] satisfy psalm --- tests/unit/AbstractLiveMailboxTest.php | 10 ++++++++-- tests/unit/ImapTest.php | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/unit/AbstractLiveMailboxTest.php b/tests/unit/AbstractLiveMailboxTest.php index 8c570329..9f7bf316 100644 --- a/tests/unit/AbstractLiveMailboxTest.php +++ b/tests/unit/AbstractLiveMailboxTest.php @@ -11,12 +11,18 @@ namespace PhpImap; use Generator; +use ParagonIE\HiddenString\HiddenString; use PHPUnit\Framework\TestCase; use Throwable; /** - * @psalm-import-type MAILBOX_ARGS from LiveMailboxTestingTrait - * + * @psalm-type MAILBOX_ARGS = array{ + * 0:HiddenString, + * 1:HiddenString, + * 2:HiddenString, + * 3:string, + * 4?:string + * } * @psalm-type COMPOSE_ENVELOPE = array{ * subject?:string * } diff --git a/tests/unit/ImapTest.php b/tests/unit/ImapTest.php index e0153235..e7355820 100644 --- a/tests/unit/ImapTest.php +++ b/tests/unit/ImapTest.php @@ -14,8 +14,13 @@ use UnexpectedValueException; /** - * @psalm-import-type MAILBOX_ARGS from LiveMailboxTestingTrait - * + * @psalm-type MAILBOX_ARGS = array{ + * 0:HiddenString, + * 1:HiddenString, + * 2:HiddenString, + * 3:string, + * 4?:string + * } * @psalm-type PSALM_OPEN_ARGS = array{ * 0:HiddenString, * 1:HiddenString, From f60647bdc79110056b5f7bc4b2025052f6306d9f Mon Sep 17 00:00:00 2001 From: Marv Date: Mon, 27 Jul 2020 14:30:15 +0100 Subject: [PATCH 22/32] perform strict check, fixes barbushin/php-imap#526 --- src/PhpImap/Imap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/Imap.php b/src/PhpImap/Imap.php index c2821292..d7824d46 100644 --- a/src/PhpImap/Imap.php +++ b/src/PhpImap/Imap.php @@ -939,7 +939,7 @@ public static function sort( ); } - if (!$result) { + if (false === $result) { throw new UnexpectedValueException('Could not sort messages!', 0, self::HandleErrors(\imap_errors(), 'imap_sort')); } From 13b5307d31a9d7ebaba0a589c92b47765e2457bd Mon Sep 17 00:00:00 2001 From: Jeroen Noten Date: Thu, 13 Aug 2020 09:16:50 +0200 Subject: [PATCH 23/32] Add false and null to possible (magic) property filePath property type in IncomingMailAttachment As the __get() method could also return null or false for the filePath property, these types should be added to the @property annotation. --- src/PhpImap/IncomingMailAttachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/IncomingMailAttachment.php b/src/PhpImap/IncomingMailAttachment.php index 48e59f5d..28f30e2d 100644 --- a/src/PhpImap/IncomingMailAttachment.php +++ b/src/PhpImap/IncomingMailAttachment.php @@ -14,7 +14,7 @@ * * @author Barbushin Sergey http://linkedin.com/in/barbushin * - * @property string $filePath lazy attachment data file + * @property string|false|null $filePath lazy attachment data file * * @psalm-type fileinfoconst = 0|2|16|1024|1040|8|32|128|256|16777216 */ From 83c487fad58df0c9fdf9d185244e537a55e09666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Tue, 9 Feb 2021 17:54:52 +0100 Subject: [PATCH 24/32] Composer.json: Add support for PHP 8 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 14603fe2..307ce334 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "sort-packages": true }, "require": { - "php": "^7.2", + "php": "^7.2 || ^8.0", "ext-fileinfo": "*", "ext-iconv": "*", "ext-imap": "*", From 68d46669de553dadc90e3dacf31f0aaac2476e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Tue, 9 Feb 2021 18:05:54 +0100 Subject: [PATCH 25/32] Use version from PHP 8.0 to <8.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 307ce334..76ddc926 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "sort-packages": true }, "require": { - "php": "^7.2 || ^8.0", + "php": "^7.2 || ^8.0 <8.1", "ext-fileinfo": "*", "ext-iconv": "*", "ext-imap": "*", From 301186bb71b3fb7b17811fbe81c90ea82145cff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=A4tzig?= Date: Tue, 23 Feb 2021 21:25:49 +0100 Subject: [PATCH 26/32] 543: Fix incorrect MIME decoding --- src/PhpImap/DataPartInfo.php | 5 ++- src/PhpImap/Mailbox.php | 72 ++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/PhpImap/DataPartInfo.php b/src/PhpImap/DataPartInfo.php index b02c1c94..a58b3e95 100644 --- a/src/PhpImap/DataPartInfo.php +++ b/src/PhpImap/DataPartInfo.php @@ -108,8 +108,9 @@ protected function decodeAfterFetch(): string protected function convertEncodingAfterFetch(): string { if (isset($this->charset) and !empty(\trim($this->charset))) { - $this->data = $this->mail->decodeMimeStr( - (string) $this->data // Data to convert + $this->data = $this->mail->convertToUtf8( + (string) $this->data, // Data to convert + $this->charset ); } diff --git a/src/PhpImap/Mailbox.php b/src/PhpImap/Mailbox.php index a50befbf..9dfa987b 100644 --- a/src/PhpImap/Mailbox.php +++ b/src/PhpImap/Mailbox.php @@ -1329,6 +1329,49 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object return $attachment; } + /** + * Converts a string to UTF-8 + * + * @param string $string MIME string to decode + * @param string $fromCharset Charset to convert from + * + * @return string Converted string if conversion was successful, or the original string if not + */ + public function convertToUtf8(string $string, string $fromCharset): string + { + $fromCharset = mb_strtolower($fromCharset); + $newString = ''; + + if ('default' === $fromCharset) { + $fromCharset = $this->decodeMimeStrDefaultCharset; + } + + switch ($fromCharset) { + case 'default': // Charset default is already ASCII (not encoded) + case 'utf-8': // Charset UTF-8 is OK + $newString .= $string; + break; + default: + // If charset exists in mb_list_encodings(), convert using mb_convert function + if (\in_array($fromCharset, $this->lowercase_mb_list_encodings(), true)) { + $newString .= \mb_convert_encoding($string, 'UTF-8', $fromCharset); + } else { + // Fallback: Try to convert with iconv() + $iconv_converted_string = @\iconv($fromCharset, 'UTF-8', $string); + if (!$iconv_converted_string) { + // If iconv() could also not convert, return string as it is + // (unknown charset) + $newString .= $string; + } else { + $newString .= $iconv_converted_string; + } + } + break; + } + + return $newString; + } + /** * Decodes a mime string. * @@ -1351,34 +1394,7 @@ public function decodeMimeStr(string $string): string } foreach ($elements as $element) { - $charset = \strtolower($element->charset); - - if ('default' === $charset) { - $charset = $this->decodeMimeStrDefaultCharset; - } - - switch ($charset) { - case 'default': // Charset default is already ASCII (not encoded) - case 'utf-8': // Charset UTF-8 is OK - $newString .= $element->text; - break; - default: - // If charset exists in mb_list_encodings(), convert using mb_convert function - if (\in_array($charset, $this->lowercase_mb_list_encodings())) { - $newString .= \mb_convert_encoding($element->text, 'UTF-8', $charset); - } else { - // Fallback: Try to convert with iconv() - $iconv_converted_string = @\iconv($charset, 'UTF-8', $element->text); - if (!$iconv_converted_string) { - // If iconv() could also not convert, return string as it is - // (unknown charset) - $newString .= $element->text; - } else { - $newString .= $iconv_converted_string; - } - } - break; - } + $newString .= $this->convertToUtf8($element->text, $element->charset); } return $newString; From 792ff48af0c763d9f8637b558cc2130458417800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=A4tzig?= Date: Tue, 23 Feb 2021 22:05:36 +0100 Subject: [PATCH 27/32] 569: Fix broken inline images when header does not contain "Content-Disposition: inline" --- src/PhpImap/IncomingMail.php | 6 +++++- src/PhpImap/Mailbox.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/PhpImap/IncomingMail.php b/src/PhpImap/IncomingMail.php index 42684702..1dae0593 100644 --- a/src/PhpImap/IncomingMail.php +++ b/src/PhpImap/IncomingMail.php @@ -219,7 +219,11 @@ public function embedImageAttachments(): void $cid = \str_replace('cid:', '', $match); foreach ($attachments as $attachment) { - if ($attachment->contentId == $cid && 'inline' == \mb_strtolower((string) $attachment->disposition)) { + /** + * Inline images can contain a "Content-Disposition: inline", but only a "Content-ID" is also enough. + * See https://github.com/barbushin/php-imap/issues/569 + */ + if ($attachment->contentId == $cid || 'inline' == \mb_strtolower((string) $attachment->disposition)) { $contents = $attachment->getContents(); $contentType = (string) $attachment->getFileInfo(FILEINFO_MIME); diff --git a/src/PhpImap/Mailbox.php b/src/PhpImap/Mailbox.php index a3e47d74..dda54d2d 100644 --- a/src/PhpImap/Mailbox.php +++ b/src/PhpImap/Mailbox.php @@ -1636,7 +1636,7 @@ protected function initMailPart(IncomingMail $mail, object $partStructure, $part } } - $isAttachment = isset($params['filename']) || isset($params['name']); + $isAttachment = isset($params['filename']) || isset($params['name']) || isset($partStructure->id); $dispositionAttachment = ( isset($partStructure->disposition) && From 94775bb05f74a2a0011becd738ff3e11b3099b29 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Fri, 19 Nov 2021 00:25:46 +0100 Subject: [PATCH 28/32] Adding link to PHP docs (#601) Co-authored-by: TS3tools --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 12942857..99b4d843 100644 --- a/README.md +++ b/README.md @@ -121,12 +121,12 @@ echo "\n\nAttachments:\n"; print_r($mail->getAttachments()); ``` -Checking the currently selected mailbox: +Method `imap()` allows to call any [PHP IMAP function](https://www.php.net/manual/ref.imap.php) in a context of the instance. Example: ```php -// Call imap_check(); -// http://php.net/manual/en/function.imap-check.php -$info = $mailbox->checkMailbox(); // +// Call imap_check() - see http://php.net/manual/function.imap-check.php +$info = $mailbox->imap('check'); + // Show current time for the mailbox $currentServerTime = isset($info->Date) && $info->Date ? date('Y-m-d H:i:s', strtotime($info->Date)) : 'Unknown'; From a3a55704f1fa2b0e97ebe225832895e306bbb70f Mon Sep 17 00:00:00 2001 From: Vladimir Luchaninov Date: Fri, 19 Nov 2021 01:46:20 +0200 Subject: [PATCH 29/32] support php 8.0 (#588) * support php 8.0 Co-authored-by: TS3tools --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 99b4d843..66be67d2 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Initially released in December 2012, the PHP IMAP Mailbox is a powerful and open | 7.2 | 3.x, 4.x | | 7.3 | 3.x, 4.x | | 7.4 | >3.0.33, 4.x | +| 8.0 | >3.0.33, 4.x | * PHP `fileinfo` extension must be present; so make sure this line is active in your php.ini: `extension=php_fileinfo.dll` * PHP `iconv` extension must be present; so make sure this line is active in your php.ini: `extension=php_iconv.dll` From 60b3f342f379bcd61a1d8f633d9c15563c6f7379 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 19 Nov 2021 01:09:39 +0100 Subject: [PATCH 30/32] Respect ServerEncoding for attachment Filenames (#614) refs #613 --- src/PhpImap/Mailbox.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/Mailbox.php b/src/PhpImap/Mailbox.php index bc9e487f..2ff4fefe 100644 --- a/src/PhpImap/Mailbox.php +++ b/src/PhpImap/Mailbox.php @@ -1259,7 +1259,7 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object $fileName = \strtolower($partStructure->subtype); } else { $fileName = (isset($params['filename']) and !empty(\trim($params['filename']))) ? $params['filename'] : $params['name']; - $fileName = $this->decodeMimeStr($fileName); + $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding); $fileName = $this->decodeRFC2231($fileName); } From 00b2592f8e40e29ffbe2da09dc326163cf7d0355 Mon Sep 17 00:00:00 2001 From: Rafael Montagud Date: Fri, 19 Nov 2021 01:10:17 +0100 Subject: [PATCH 31/32] Avoid TypeError due to $lastError being boolean/false instead of a string (strict types) (#608) --- src/PhpImap/Imap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpImap/Imap.php b/src/PhpImap/Imap.php index d7824d46..4d1ac296 100644 --- a/src/PhpImap/Imap.php +++ b/src/PhpImap/Imap.php @@ -706,7 +706,7 @@ public static function open( if (!$result) { $lastError = \imap_last_error(); - if ('' !== \trim($lastError)) { + if ((\is_string($lastError)) && ('' !== \trim($lastError))) { throw new UnexpectedValueException('IMAP error:'.$lastError); } From 35737d835a8ca1a90f0ca62729752f4ca8356770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20L=C3=B6sken?= Date: Sat, 20 Nov 2021 18:53:51 +0100 Subject: [PATCH 32/32] Fix deprecated methods (#595) Co-authored-by: TS3tools --- .php_cs.dist | 73 ++++++++++--------- ...parse_all_emails_with_matching_subject.php | 4 +- ..._all_emails_without_saving_attachments.php | 4 +- examples/get_and_parse_unseen_emails.php | 4 +- ...een_emails_save_attachments_one_by_one.php | 4 +- src/PhpImap/DataPartInfo.php | 8 +- src/PhpImap/IncomingMailAttachment.php | 2 +- src/PhpImap/Mailbox.php | 30 ++++---- 8 files changed, 67 insertions(+), 62 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index c2827a2b..8afdc144 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -1,38 +1,43 @@ setRules([ - '@Symfony' => true, - '@Symfony:risky' => true, - 'array_syntax' => [ - 'syntax' => 'short' - ], - 'ordered_imports' => true, - 'phpdoc_to_comment' => false, - 'no_superfluous_phpdoc_tags' => true, - 'declare_strict_types' => true, - 'void_return' => true, - 'ordered_class_elements' => true, - 'global_namespace_import' => [ - 'import_classes' => true, - 'import_constants' => true, - 'import_functions' => false, - ], - 'native_constant_invocation' => true, - 'native_function_invocation' => true, - 'php_unit_test_case_static_method_calls' => [ - 'call_type' => 'this', - ], - 'php_unit_method_casing' => true, - 'php_unit_dedicate_assert' => [ - 'target' => 'newest', - ], - ]) +declare(strict_types=1); + +$finder = PhpCsFixer\Finder::create() + ->in(__DIR__.'/src') + ->in(__DIR__.'/tests') + ->in(__DIR__.'/examples') + ->append([__FILE__]); + +$config = new PhpCsFixer\Config(); +$config->setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + 'array_syntax' => [ + 'syntax' => 'short', + ], + 'ordered_imports' => true, + 'phpdoc_to_comment' => false, + 'no_superfluous_phpdoc_tags' => true, + 'declare_strict_types' => true, + 'void_return' => true, + 'ordered_class_elements' => true, + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => false, + ], + 'native_constant_invocation' => true, + 'native_function_invocation' => true, + 'php_unit_test_case_static_method_calls' => [ + 'call_type' => 'this', + ], + 'php_unit_method_casing' => true, + 'php_unit_dedicate_assert' => [ + 'target' => 'newest', + ], +]) ->setRiskyAllowed(true) - ->setFinder( - PhpCsFixer\Finder::create() - ->in(__DIR__.'/src') - ->in(__DIR__.'/tests') - ->in(__DIR__.'/examples') - ) + ->setFinder($finder) ; + +return $config; diff --git a/examples/get_and_parse_all_emails_with_matching_subject.php b/examples/get_and_parse_all_emails_with_matching_subject.php index 7cb8e52b..5ad13361 100644 --- a/examples/get_and_parse_all_emails_with_matching_subject.php +++ b/examples/get_and_parse_all_emails_with_matching_subject.php @@ -23,9 +23,9 @@ try { $mail_ids = $mailbox->searchMailbox('SUBJECT "part of the subject"'); } catch (ConnectionException $ex) { - die('IMAP connection failed: '.$ex->getMessage()); + exit('IMAP connection failed: '.$ex->getMessage()); } catch (Exception $ex) { - die('An error occured: '.$ex->getMessage()); + exit('An error occured: '.$ex->getMessage()); } foreach ($mail_ids as $mail_id) { diff --git a/examples/get_and_parse_all_emails_without_saving_attachments.php b/examples/get_and_parse_all_emails_without_saving_attachments.php index 6913edb6..b5db0886 100644 --- a/examples/get_and_parse_all_emails_without_saving_attachments.php +++ b/examples/get_and_parse_all_emails_without_saving_attachments.php @@ -33,9 +33,9 @@ try { $mail_ids = $mailbox->searchMailbox('UNSEEN'); } catch (ConnectionException $ex) { - die('IMAP connection failed: '.$ex->getMessage()); + exit('IMAP connection failed: '.$ex->getMessage()); } catch (Exception $ex) { - die('An error occured: '.$ex->getMessage()); + exit('An error occured: '.$ex->getMessage()); } foreach ($mail_ids as $mail_id) { diff --git a/examples/get_and_parse_unseen_emails.php b/examples/get_and_parse_unseen_emails.php index 76462366..1070ac31 100644 --- a/examples/get_and_parse_unseen_emails.php +++ b/examples/get_and_parse_unseen_emails.php @@ -23,9 +23,9 @@ try { $mail_ids = $mailbox->searchMailbox('UNSEEN'); } catch (ConnectionException $ex) { - die('IMAP connection failed: '.$ex->getMessage()); + exit('IMAP connection failed: '.$ex->getMessage()); } catch (Exception $ex) { - die('An error occured: '.$ex->getMessage()); + exit('An error occured: '.$ex->getMessage()); } foreach ($mail_ids as $mail_id) { diff --git a/examples/get_and_parse_unseen_emails_save_attachments_one_by_one.php b/examples/get_and_parse_unseen_emails_save_attachments_one_by_one.php index 86cd1fca..11192bf6 100644 --- a/examples/get_and_parse_unseen_emails_save_attachments_one_by_one.php +++ b/examples/get_and_parse_unseen_emails_save_attachments_one_by_one.php @@ -21,9 +21,9 @@ try { $mail_ids = $mailbox->searchMailbox('UNSEEN'); } catch (ConnectionException $ex) { - die('IMAP connection failed: '.$ex->getMessage()); + exit('IMAP connection failed: '.$ex->getMessage()); } catch (Exception $ex) { - die('An error occured: '.$ex->getMessage()); + exit('An error occured: '.$ex->getMessage()); } foreach ($mail_ids as $mail_id) { diff --git a/src/PhpImap/DataPartInfo.php b/src/PhpImap/DataPartInfo.php index a58b3e95..df491751 100644 --- a/src/PhpImap/DataPartInfo.php +++ b/src/PhpImap/DataPartInfo.php @@ -107,10 +107,10 @@ protected function decodeAfterFetch(): string protected function convertEncodingAfterFetch(): string { - if (isset($this->charset) and !empty(\trim($this->charset))) { - $this->data = $this->mail->convertToUtf8( - (string) $this->data, // Data to convert - $this->charset + if (isset($this->charset) && !empty(\trim($this->charset))) { + $this->data = $this->mail->decodeMimeStr( + (string) $this->data // Data to convert + \trim($this->charset) ); } diff --git a/src/PhpImap/IncomingMailAttachment.php b/src/PhpImap/IncomingMailAttachment.php index 28f30e2d..a15783a2 100644 --- a/src/PhpImap/IncomingMailAttachment.php +++ b/src/PhpImap/IncomingMailAttachment.php @@ -131,7 +131,7 @@ public function addDataPartInfo(DataPartInfo $dataInfo): void */ public function getFileInfo(int $fileinfo_const = FILEINFO_NONE): string { - if ((FILEINFO_MIME == $fileinfo_const) and (false != $this->mimeType)) { + if ((FILEINFO_MIME == $fileinfo_const) && (false != $this->mimeType)) { return $this->mimeType; } diff --git a/src/PhpImap/Mailbox.php b/src/PhpImap/Mailbox.php index 2ff4fefe..5e1f2753 100644 --- a/src/PhpImap/Mailbox.php +++ b/src/PhpImap/Mailbox.php @@ -363,7 +363,7 @@ public function setConnectionArgs(int $options = 0, int $retriesNum = 0, array $ $this->imapRetriesNum = $retriesNum; } - if (\is_array($params) and \count($params) > 0) { + if (\is_array($params) && \count($params) > 0) { $supported_params = ['DISABLE_AUTHENTICATOR']; foreach (\array_keys($params) as $key) { @@ -875,16 +875,16 @@ public function getMailsInfo(array $mailsIds): array throw new UnexpectedValueException('to property at index '.(string) $index.' of argument 1 passed to '.__METHOD__.'() was not a string!'); } - if (isset($mail->subject) and !empty(\trim($mail->subject))) { + if (isset($mail->subject) && !empty(\trim($mail->subject))) { $mail->subject = $this->decodeMimeStr($mail->subject); } - if (isset($mail->from) and !empty(\trim($mail->from))) { + if (isset($mail->from) && !empty(\trim($mail->from))) { $mail->from = $this->decodeMimeStr($mail->from); } - if (isset($mail->sender) and !empty(\trim($mail->sender))) { + if (isset($mail->sender) && !empty(\trim($mail->sender))) { $mail->sender = $this->decodeMimeStr($mail->sender); } - if (isset($mail->to) and !empty(\trim($mail->to))) { + if (isset($mail->to) && !empty(\trim($mail->to))) { $mail->to = $this->decodeMimeStr($mail->to); } } @@ -951,7 +951,7 @@ public function getMailboxInfo(): object public function sortMails( int $criteria = SORTARRIVAL, bool $reverse = true, - ? string $searchCriteria = 'ALL', + ?string $searchCriteria = 'ALL', string $charset = null ): array { return Imap::sort( @@ -1100,22 +1100,22 @@ public function getMailHeader(int $mailId): IncomingMailHeader $header->precedence = (\preg_match("/Precedence\:(.*)/i", $headersRaw, $matches)) ? \trim($matches[1]) : ''; $header->failedRecipients = (\preg_match("/Failed-Recipients\:(.*)/i", $headersRaw, $matches)) ? \trim($matches[1]) : ''; - if (isset($head->date) and !empty(\trim($head->date))) { + if (isset($head->date) && !empty(\trim($head->date))) { $header->date = self::parseDateTime($head->date); - } elseif (isset($head->Date) and !empty(\trim($head->Date))) { + } elseif (isset($head->Date) && !empty(\trim($head->Date))) { $header->date = self::parseDateTime($head->Date); } else { $now = new DateTime(); $header->date = self::parseDateTime($now->format('Y-m-d H:i:s')); } - $header->subject = (isset($head->subject) and !empty(\trim($head->subject))) ? $this->decodeMimeStr($head->subject) : null; - if (isset($head->from) and !empty($head->from)) { + $header->subject = (isset($head->subject) && !empty(\trim($head->subject))) ? $this->decodeMimeStr($head->subject) : null; + if (isset($head->from) && !empty($head->from)) { list($header->fromHost, $header->fromName, $header->fromAddress) = $this->possiblyGetHostNameAndAddress($head->from); } elseif (\preg_match('/smtp.mailfrom=[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}/', $headersRaw, $matches)) { $header->fromAddress = \substr($matches[0], 14); } - if (isset($head->sender) and !empty($head->sender)) { + if (isset($head->sender) && !empty($head->sender)) { list($header->senderHost, $header->senderName, $header->senderAddress) = $this->possiblyGetHostNameAndAddress($head->sender); } if (isset($head->to)) { @@ -1255,10 +1255,10 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object $fileName = \strtolower($partStructure->subtype).'.eml'; } elseif ('ALTERNATIVE' == $partStructure->subtype) { $fileName = \strtolower($partStructure->subtype).'.eml'; - } elseif ((!isset($params['filename']) or empty(\trim($params['filename']))) && (!isset($params['name']) or empty(\trim($params['name'])))) { + } elseif ((!isset($params['filename']) || empty(\trim($params['filename']))) && (!isset($params['name']) || empty(\trim($params['name'])))) { $fileName = \strtolower($partStructure->subtype); } else { - $fileName = (isset($params['filename']) and !empty(\trim($params['filename']))) ? $params['filename'] : $params['name']; + $fileName = (isset($params['filename']) && !empty(\trim($params['filename']))) ? $params['filename'] : $params['name']; $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding); $fileName = $this->decodeRFC2231($fileName); } @@ -1300,7 +1300,7 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object if (isset($charset) && !\is_string($charset)) { throw new InvalidArgumentException('Argument 2 passed to '.__METHOD__.'() must specify charset as a string when specified!'); } - $attachment->charset = (isset($charset) and !empty(\trim($charset))) ? $charset : null; + $attachment->charset = (isset($charset) && !empty(\trim($charset))) ? $charset : null; $attachment->emlOrigin = $emlOrigin; $attachment->addDataPartInfo($dataInfo); @@ -1807,7 +1807,7 @@ protected function possiblyGetEmailAndNameFromRecipient(object $recipient): ?arr if ('' !== \trim($recipientMailbox) && '' !== \trim($recipientHost)) { $recipientEmail = \strtolower($recipientMailbox.'@'.$recipientHost); - $recipientName = (\is_string($recipientPersonal) and '' !== \trim($recipientPersonal)) ? $this->decodeMimeStr($recipientPersonal) : null; + $recipientName = (\is_string($recipientPersonal) && '' !== \trim($recipientPersonal)) ? $this->decodeMimeStr($recipientPersonal) : null; return [ $recipientEmail,