- global $thread_new, $server_sort_array;
-
- if (sqsession_is_registered('thread_new'))
- sqsession_unregister('thread_new');
- if (sqsession_is_registered('server_sort_array'))
- sqsession_unregister('server_sort_array');
-
- $thread_new = array();
- $thread_new[0] = "";
-
- $server_sort_array = array();
-
- global $uid_support;
-
- if ($search_charset == '')
- $search_charset = 'US-ASCII';
- $query = 'THREAD ' . $thread_algorithm . ' ' . strtoupper($search_charset) . ' ALL ' . $search_string;
- s_debug_dump('C:', $query);
- $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
-
- /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
- if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
- $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string;
- s_debug_dump('C:', $query);
- $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
- }
-
- if (strtoupper($response) != 'OK') {
-// sqimap_asearch_error_box($response, $query, $message);
-// return array();
- return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
- }
-
- /* Keep going till we find the * THREAD response */
- foreach ($readin as $readin_part) {
- s_debug_dump('S:', $readin_part);
- if (substr($readin_part, 0, 9) == '* THREAD ') {
- $thread_temp = preg_split("//", substr($readin_part, 9), -1, PREG_SPLIT_NO_EMPTY);
- break; // Should be the last anyway
- }
- }
-
- if (empty($thread_temp)) //Empty search response, ie '* THREAD'
- return array();
-
- $char_count = count($thread_temp);
- $counter = 0;
- $k = 0;
- for ($i=0;$i<$char_count;$i++) {
- if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
- $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
- }
- elseif ($thread_temp[$i] == '(') {
- $thread_new[$k] .= $thread_temp[$i];
- $counter++;
- }
- elseif ($thread_temp[$i] == ')') {
- if ($counter > 1) {
- $thread_new[$k] .= $thread_temp[$i];
- $counter = $counter - 1;
- }
- else {
- $thread_new[$k] .= $thread_temp[$i];
- $k++;
- $thread_new[$k] = "";
- $counter = $counter - 1;
- }
- }
- }
- sqsession_register($thread_new, 'thread_new');
- $thread_new = array_reverse($thread_new);
- $thread_list = implode(" ", $thread_new);
- $thread_list = str_replace("(", " ", $thread_list);
- $thread_list = str_replace(")", " ", $thread_list);
- $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
- $server_sort_array = $thread_list;
- sqsession_register($server_sort_array, 'server_sort_array');
- return $thread_list;
+ //For some reason, this seems to happen and forbids searching servers not allowing OPTIONAL [CHARSET]
+ if (strtoupper($search_charset) == 'US-ASCII')
+ $search_charset = '';
+ /* 6.4.4 try OPTIONAL [CHARSET] specification first */
+ if ($search_charset != '')
+ $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ' . $search_string;
+ else
+ $query = 'SEARCH ' . $search_string;
+ $readin = sqimap_run_command_list($imapConnection, $query, false, $response, $message, TRUE);
+
+ /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
+ if (($search_charset != '') && (strtoupper($response) == 'NO')) {
+ $query = 'SEARCH CHARSET US-ASCII ' . $search_string;
+ $readin = sqimap_run_command_list($imapConnection, $query, false, $response, $message, TRUE);
+ }
+ if (strtoupper($response) != 'OK') {
+ sqimap_asearch_error_box($response, $query, $message);
+ return array();
+ }
+ $messagelist = parseUidList($readin,'SEARCH');
+
+ if (empty($messagelist)) //Empty search response, ie '* SEARCH'
+ return array();
+
+ $cnt = count($messagelist);
+ for ($q = 0; $q < $cnt; $q++)
+ $id[$q] = trim($messagelist[$q]);
+ return $id;