Skip to content

Commit b59a58e

Browse files
committed
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
1 parent 13e4ce3 commit b59a58e

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

ext/imap/php_imap.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,11 @@ PHP_FUNCTION(imap_open)
782782
RETURN_THROWS();
783783
}
784784

785-
if (flags && ((flags & ~(OP_READONLY | OP_ANONYMOUS | OP_HALFOPEN | CL_EXPUNGE | OP_DEBUG | OP_SHORTCACHE
785+
/* Check for PHP_EXPUNGE and not CL_EXPUNGE as the user land facing CL_EXPUNGE constant is defined
786+
* to something different to prevent clashes between CL_EXPUNGE and an OP_* constant allowing setting
787+
* the CL_EXPUNGE flag which will expunge when the mailbox is closed (be that manually, or via the
788+
* IMAPConnection object being destroyed naturally at the end of the PHP script */
789+
if (flags && ((flags & ~(OP_READONLY | OP_ANONYMOUS | OP_HALFOPEN | PHP_EXPUNGE | OP_DEBUG | OP_SHORTCACHE
786790
| OP_SILENT | OP_PROTOTYPE | OP_SECURE)) != 0)) {
787791
zend_argument_value_error(4, "must be a bitmask of the OP_* constants, and CL_EXPUNGE");
788792
RETURN_THROWS();

ext/imap/tests/bug80800.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #80800: imap_open() fails when the flags parameter includes CL_EXPUNGE
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__.'/setup/skipif.inc');
6+
?>
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__.'/setup/imap_include.inc';
11+
12+
$mail_box = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD, flags: CL_EXPUNGE);
13+
imap_close($mail_box);
14+
15+
echo 'Connected without any issues', "\n";
16+
17+
?>
18+
--EXPECT--
19+
Connected without any issues
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Test imap_open() using the CL_EXPUNGE flag
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__.'/setup/skipif.inc');
6+
?>
7+
--FILE--
8+
<?php
9+
10+
// include file for required variables in imap_open()
11+
require_once(__DIR__.'/setup/imap_include.inc');
12+
13+
// set up temp mailbox with 3 messages
14+
$stream_id = setup_test_mailbox('imapopenwithclexpunge', 3, $mailbox, flags: CL_EXPUNGE);
15+
16+
// mark messages in inbox for deletion
17+
for ($i = 1; $i < 4; $i++) {
18+
imap_delete($stream_id, $i);
19+
}
20+
21+
echo "\n-- Call to imap_close() --\n";
22+
var_dump( imap_close($stream_id) );
23+
24+
// check that CL_EXPUNGE in previous imap_open() call worked
25+
$stream_id = imap_open($mailbox, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD);
26+
echo "There are now " . imap_num_msg($stream_id) . " msgs in mailbox '$mailbox'\n";
27+
28+
// Close connection
29+
var_dump( imap_close($stream_id) );
30+
?>
31+
--CLEAN--
32+
<?php
33+
$mailbox_suffix = 'imapopenwithclexpunge';
34+
require_once(__DIR__.'/setup/clean.inc');
35+
?>
36+
--EXPECTF--
37+
Create a temporary mailbox and add 3 msgs
38+
New mailbox created
39+
40+
-- Call to imap_close() --
41+
bool(true)
42+
There are now 0 msgs in mailbox '%sINBOX.phpttestimapclosebasic'
43+
bool(true)

ext/imap/tests/setup/imap_include.inc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,19 @@ function displayOverviewFields($resp, array $fields = MANDATORY_OVERVIEW_FIELDS)
6363
* @param int message_count number of test msgs to be written to new mailbox
6464
* @param null $new_mailbox
6565
* @param bool $simpleMessages
66+
* @param int $flags OP_* (or CL_EXPUNGE) flags to pass to imap_open() sub-call
6667
* @return resource IMAP stream to new mailbox
6768
* @throws Exception
6869
*/
69-
function setup_test_mailbox(string $mailbox_suffix, int $message_count, &$new_mailbox = null, bool $simpleMessages = true){
70+
function setup_test_mailbox(
71+
string $mailbox_suffix,
72+
int $message_count,
73+
&$new_mailbox = null,
74+
bool $simpleMessages = true,
75+
int $flags = 0,
76+
){
7077
// open a stream to default mailbox
71-
$imap_stream = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD);
78+
$imap_stream = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD, flags: $flags);
7279

7380
if ($imap_stream === false) {
7481
throw new Exception("Cannot connect to IMAP server " . IMAP_SERVER . ": " . imap_last_error());

0 commit comments

Comments
 (0)