From dc578b5d75781d2df1f6ab964a8f43a6eaba4c2f Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Fri, 26 Feb 2021 03:03:38 +0000 Subject: [PATCH 1/4] Fix Bug #80800 imap_open() fails when the flags parameter includes CL_EXPUNGE Also add a supplementary test that the CL_EXPUNGE flag does have the intended effect --- ext/imap/php_imap.c | 6 ++- ext/imap/tests/bug80800.phpt | 19 ++++++++ ext/imap/tests/imap_open_with_cl_expunge.phpt | 43 +++++++++++++++++++ ext/imap/tests/setup/imap_include.inc | 11 ++++- 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 ext/imap/tests/bug80800.phpt create mode 100644 ext/imap/tests/imap_open_with_cl_expunge.phpt diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index e62d6f7b2aea7..3854f955248ab 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -782,7 +782,11 @@ PHP_FUNCTION(imap_open) RETURN_THROWS(); } - if (flags && ((flags & ~(OP_READONLY | OP_ANONYMOUS | OP_HALFOPEN | CL_EXPUNGE | OP_DEBUG | OP_SHORTCACHE + /* Check for PHP_EXPUNGE and not CL_EXPUNGE as the user land facing CL_EXPUNGE constant is defined + * to something different to prevent clashes between CL_EXPUNGE and an OP_* constant allowing setting + * the CL_EXPUNGE flag which will expunge when the mailbox is closed (be that manually, or via the + * IMAPConnection object being destroyed naturally at the end of the PHP script */ + if (flags && ((flags & ~(OP_READONLY | OP_ANONYMOUS | OP_HALFOPEN | PHP_EXPUNGE | OP_DEBUG | OP_SHORTCACHE | OP_SILENT | OP_PROTOTYPE | OP_SECURE)) != 0)) { zend_argument_value_error(4, "must be a bitmask of the OP_* constants, and CL_EXPUNGE"); RETURN_THROWS(); diff --git a/ext/imap/tests/bug80800.phpt b/ext/imap/tests/bug80800.phpt new file mode 100644 index 0000000000000..3ff3183aaeb1b --- /dev/null +++ b/ext/imap/tests/bug80800.phpt @@ -0,0 +1,19 @@ +--TEST-- +Bug #80800: imap_open() fails when the flags parameter includes CL_EXPUNGE +--SKIPIF-- + +--FILE-- + +--EXPECT-- +Connected without any issues diff --git a/ext/imap/tests/imap_open_with_cl_expunge.phpt b/ext/imap/tests/imap_open_with_cl_expunge.phpt new file mode 100644 index 0000000000000..2ecd7668957ef --- /dev/null +++ b/ext/imap/tests/imap_open_with_cl_expunge.phpt @@ -0,0 +1,43 @@ +--TEST-- +Test imap_open() using the CL_EXPUNGE flag +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECTF-- +Create a temporary mailbox and add 3 msgs +New mailbox created + +-- Call to imap_close() -- +bool(true) +There are now 0 msgs in mailbox '%sINBOX.phpttestimapclosebasic' +bool(true) diff --git a/ext/imap/tests/setup/imap_include.inc b/ext/imap/tests/setup/imap_include.inc index 223278f077302..0c07f9ae2d7d5 100644 --- a/ext/imap/tests/setup/imap_include.inc +++ b/ext/imap/tests/setup/imap_include.inc @@ -63,12 +63,19 @@ function displayOverviewFields($resp, array $fields = MANDATORY_OVERVIEW_FIELDS) * @param int message_count number of test msgs to be written to new mailbox * @param null $new_mailbox * @param bool $simpleMessages + * @param int $flags OP_* (or CL_EXPUNGE) flags to pass to imap_open() sub-call * @return resource IMAP stream to new mailbox * @throws Exception */ -function setup_test_mailbox(string $mailbox_suffix, int $message_count, &$new_mailbox = null, bool $simpleMessages = true){ +function setup_test_mailbox( + string $mailbox_suffix, + int $message_count, + &$new_mailbox = null, + bool $simpleMessages = true, + int $flags = 0, +){ // open a stream to default mailbox - $imap_stream = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD); + $imap_stream = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD, flags: $flags); if ($imap_stream === false) { throw new Exception("Cannot connect to IMAP server " . IMAP_SERVER . ": " . imap_last_error()); From fda8cc6348863781e046223084939b8d03949a9d Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Fri, 26 Feb 2021 03:12:19 +0000 Subject: [PATCH 2/4] This also affects imap_reopen() --- ext/imap/php_imap.c | 6 ++- ext/imap/tests/bug80800.phpt | 2 + .../tests/imap_reopen_with_cl_expunge.phpt | 47 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 ext/imap/tests/imap_reopen_with_cl_expunge.phpt diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index 3854f955248ab..9a1ded643b2ac 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -905,7 +905,11 @@ PHP_FUNCTION(imap_reopen) GET_IMAP_STREAM(imap_conn_struct, imap_conn_obj); /* TODO Verify these are the only options available as they are pulled from the php.net documentation */ - if (options && ((options & ~(OP_READONLY | OP_ANONYMOUS | OP_HALFOPEN | OP_EXPUNGE | CL_EXPUNGE)) != 0)) { + /* Check for PHP_EXPUNGE and not CL_EXPUNGE as the user land facing CL_EXPUNGE constant is defined + * to something different to prevent clashes between CL_EXPUNGE and an OP_* constant allowing setting + * the CL_EXPUNGE flag which will expunge when the mailbox is closed (be that manually, or via the + * IMAPConnection object being destroyed naturally at the end of the PHP script */ + if (options && ((options & ~(OP_READONLY | OP_ANONYMOUS | OP_HALFOPEN | OP_EXPUNGE | PHP_EXPUNGE)) != 0)) { zend_argument_value_error(3, "must be a bitmask of OP_READONLY, OP_ANONYMOUS, OP_HALFOPEN, " "OP_EXPUNGE, and CL_EXPUNGE"); RETURN_THROWS(); diff --git a/ext/imap/tests/bug80800.phpt b/ext/imap/tests/bug80800.phpt index 3ff3183aaeb1b..d75c1fde80b10 100644 --- a/ext/imap/tests/bug80800.phpt +++ b/ext/imap/tests/bug80800.phpt @@ -10,10 +10,12 @@ require_once(__DIR__.'/setup/skipif.inc'); require_once __DIR__.'/setup/imap_include.inc'; $mail_box = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD, flags: CL_EXPUNGE); +var_dump(imap_reopen($mail_box, IMAP_DEFAULT_MAILBOX, flags: CL_EXPUNGE)); imap_close($mail_box); echo 'Connected without any issues', "\n"; ?> --EXPECT-- +bool(true) Connected without any issues diff --git a/ext/imap/tests/imap_reopen_with_cl_expunge.phpt b/ext/imap/tests/imap_reopen_with_cl_expunge.phpt new file mode 100644 index 0000000000000..f59273486444e --- /dev/null +++ b/ext/imap/tests/imap_reopen_with_cl_expunge.phpt @@ -0,0 +1,47 @@ +--TEST-- +Test imap_reopen() using the CL_EXPUNGE flag +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECTF-- +Create a temporary mailbox and add 3 msgs +New mailbox created + +-- Call to imap_close() -- +bool(true) +There are now 0 msgs in mailbox '%sINBOX.phpttestimapclosebasic' +bool(true) From 99039c3bb913597a586716877da891b39be8fbc0 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Fri, 26 Feb 2021 09:38:35 +0000 Subject: [PATCH 3/4] Fix tests --- ext/imap/tests/imap_open_with_cl_expunge.phpt | 2 +- ext/imap/tests/imap_reopen_with_cl_expunge.phpt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ext/imap/tests/imap_open_with_cl_expunge.phpt b/ext/imap/tests/imap_open_with_cl_expunge.phpt index 2ecd7668957ef..8ecdd4c000b51 100644 --- a/ext/imap/tests/imap_open_with_cl_expunge.phpt +++ b/ext/imap/tests/imap_open_with_cl_expunge.phpt @@ -39,5 +39,5 @@ New mailbox created -- Call to imap_close() -- bool(true) -There are now 0 msgs in mailbox '%sINBOX.phpttestimapclosebasic' +There are now 0 msgs in mailbox '%sINBOX.imapopenwithclexpunge' bool(true) diff --git a/ext/imap/tests/imap_reopen_with_cl_expunge.phpt b/ext/imap/tests/imap_reopen_with_cl_expunge.phpt index f59273486444e..a568370912748 100644 --- a/ext/imap/tests/imap_reopen_with_cl_expunge.phpt +++ b/ext/imap/tests/imap_reopen_with_cl_expunge.phpt @@ -40,8 +40,9 @@ require_once(__DIR__.'/setup/clean.inc'); --EXPECTF-- Create a temporary mailbox and add 3 msgs New mailbox created +bool(true) -- Call to imap_close() -- bool(true) -There are now 0 msgs in mailbox '%sINBOX.phpttestimapclosebasic' +There are now 0 msgs in mailbox '%sINBOX.imapreopenwithclexpunge' bool(true) From a98483d33928e35f99832fe8c7599358e0ad4562 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Fri, 26 Feb 2021 10:58:48 +0000 Subject: [PATCH 4/4] actually fix test --- ext/imap/tests/imap_open_with_cl_expunge.phpt | 2 +- ext/imap/tests/imap_reopen_with_cl_expunge.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/imap/tests/imap_open_with_cl_expunge.phpt b/ext/imap/tests/imap_open_with_cl_expunge.phpt index 8ecdd4c000b51..5d1721f861691 100644 --- a/ext/imap/tests/imap_open_with_cl_expunge.phpt +++ b/ext/imap/tests/imap_open_with_cl_expunge.phpt @@ -39,5 +39,5 @@ New mailbox created -- Call to imap_close() -- bool(true) -There are now 0 msgs in mailbox '%sINBOX.imapopenwithclexpunge' +There are now 0 msgs in mailbox '%sINBOX.phpttestimapopenwithclexpunge' bool(true) diff --git a/ext/imap/tests/imap_reopen_with_cl_expunge.phpt b/ext/imap/tests/imap_reopen_with_cl_expunge.phpt index a568370912748..78d1eddaca9e8 100644 --- a/ext/imap/tests/imap_reopen_with_cl_expunge.phpt +++ b/ext/imap/tests/imap_reopen_with_cl_expunge.phpt @@ -44,5 +44,5 @@ bool(true) -- Call to imap_close() -- bool(true) -There are now 0 msgs in mailbox '%sINBOX.imapreopenwithclexpunge' +There are now 0 msgs in mailbox '%sINBOX.phpttestimapreopenwithclexpunge' bool(true)