php-imap
php-imap copied to clipboard
Fetching flags causes permanent hang
Describe the bug Trying to fetch the flags for a message causes the library to hang forever.
Used config
$cm = new ClientManager($options = []);
$client = $cm->make([
'host' => $w['server'],
'port' => $w['port'],
'encryption' => $w['secure'] ? 'ssl' : 'none',
'validate_cert' => true,
'username' => $w['username'],
'password' => $w['password'],
'protocol' => 'imap'
]);
Code to Reproduce The troubling code section which produces the reported bug.
printf("Fetching flags\n");
$flags = $client->getConnection()->flags((int) $f['uid'], 1)->validatedData();
printf("Done fetching flags\n");
It never gets to "Done fetching flags". This happens every single time.
I'm fetching the flags per message manually since $overview = $client->getConnection()->overview($sequence, 3)->validatedData(); omits a lot of mandatory message info, e.g. the UID, INTERNALDATE, flags, size, etc. Not very efficient, but that's another issue to deal with separately.
Expected behavior Given the FETCH call is issued and successful, the function should return immediately.
Desktop / Server (please complete the following information):
- OS: Debian 11
- PHP: 8.2
- Version: GIT (master) from yesterday
Seems to be a bug with the reading/parsing, as it did issue a FETCH to the IMAP server, but the function call above never returns. IMAP logs for the above function call:
[2023-05-23 12:46:38.578] DEBUG[922738]: net_imap.c:9096 handle_client: 0x7f26577fb5d0 => TAG35 UID FETCH 2154 (FLAGS)
[2023-05-23 12:46:38.578] DEBUG[922738]: net_imap.c:4643 process_fetch: 0x7f26577fb5d0 <= * 59 FETCH (FLAGS (\Seen) UID 2154)
[2023-05-23 12:46:38.578] DEBUG[922738]: net_imap.c:4676 process_fetch: 0x7f26577fb5d0 <= TAG35 OK UID FETCH Completed
The bug appears to be here: https://github.com/Webklex/php-imap/blob/6ea5a94f2f8936ac5c2c40a46fbbd88f2bdeb16b/src/Connection/Protocols/ImapProtocol.php#L703
Rewriting the code using an array, with a single element, it works properly:
$flags = $client->getConnection()->flags(array((int) $f['uid']), 1)->validatedData();
However, if a number is provided directly, the logic is incorrect. It continues iterating the outer loop, but it shouldn't be. I confirmed this by tracking what it was doing in the fetch function.
Hi @InterLinked1,
Thanks a lot for reporting this issue. I really appreciate it! I was able to reproduce the issue and just pushed a fix.
Once again, thanks for taking the time and effort to make this library better! If you have any more questions or need further assistance, feel free to let me know.
Best regards and happy coding!