+function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
+{
+ global $uid_support;
+
+ if ($search_charset == '')
+ $search_charset = 'US-ASCII';
+ $query = 'SORT (' . $sort_criteria . ') ' . 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 = 'SORT (' . $sort_criteria . ') 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 * SORT response */
+ foreach ($readin as $readin_part) {
+ s_debug_dump('S:', $readin_part);
+ if (substr($readin_part, 0, 7) == '* SORT ') {
+ $messagelist = preg_split("/ /", substr($readin_part, 7));
+ break; // Should be the last anyway
+ }
+ }
+
+ if (empty($messagelist)) //Empty search response, ie '* SORT'
+ return array();
+
+ $cnt = count($messagelist);
+ for ($q = 0; $q < $cnt; $q++)
+ $id[$q] = trim($messagelist[$q]);
+ return $id;
+}
+
+function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
+{
+ 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;
+}
+