Skip to content

Commit

Permalink
checking if the server supports custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Aug 9, 2024
1 parent ec03343 commit b1bd138
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit b1bd138

Please sign in to comment.