From b1bd138c3dba30458c59599533b5b71a41b9e297 Mon Sep 17 00:00:00 2001 From: Steven Ngesera Date: Fri, 9 Aug 2024 03:20:49 +0300 Subject: [PATCH] checking if the server supports custom headers --- modules/imap/hm-imap.php | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/modules/imap/hm-imap.php b/modules/imap/hm-imap.php index 97261ddd7a..b8bbd4ece1 100644 --- a/modules/imap/hm-imap.php +++ b/modules/imap/hm-imap.php @@ -1099,7 +1099,7 @@ public function search($target='ALL', $uids=false, $terms=array(), $esearch=arra if ($only_auto_bcc) { $fld .= ' HEADER X-Auto-Bcc cypht'; } - if (!mb_strstr($this->server, 'yahoo') && $exclude_auto_bcc) { + if ($this->server_supports_custom_headers() && !mb_strstr($this->server, 'yahoo') && $exclude_auto_bcc) { $fld .= ' NOT HEADER X-Auto-Bcc cypht'; } $esearch_enabled = false; @@ -2213,5 +2213,42 @@ public function get_folder_list_by_level($level='', $only_subscribed=false, $wit } return $result; } + + /** + * test if the server supports searching by custom headers + * @return boolean true is the server supports searching by custom headers + */ + protected function server_supports_custom_headers() { + $test_command = 'UID SEARCH HEADER "X-NonExistent-Header" "test"'."\r\n"; + $this->send_command($test_command); + $response = $this->get_response(false, true); + $status = $this->check_response($response, true); + + // Keywords that indicate the header search is not supported + $keywords = ['is', 'not', 'supported.']; + + if (!$status) { + return false; + } + + // Flatten the response array to a single array of strings + $flattened_response = array_reduce($response, 'array_merge', []); + + // Check if all keywords are present in the flattened response + $sequence_match = true; + foreach ($keywords as $keyword) { + if (!in_array($keyword, $flattened_response)) { + $sequence_match = false; + break; + } + } + + // If all keywords are found, the header search is not supported + if ($sequence_match) { + return false; + } + + return true; + } } }