- for ($i=0;$i<count($thread_new);$i++) {
- /* first grab the parent, it does not indent */
-
- if (isset($thread_new[$i][0])) {
- if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
- $parent = $regs[1];
- }
- }
- $indent_array[$parent] = 0;
-
- /*
- * now the children, checking each thread portion for
- * ),(, and space, adjusting the level and space values
- * to get the indent level
- */
- $level = 0;
- $spaces = array();
- $spaces_total = 0;
- $indent = 0;
- $fake = FALSE;
- for ($k=1,$iCnt=count($thread_new[$i])-1;$k<$iCnt;++$k) {
- $chars = count_chars($thread_new[$i][$k], 1);
- if (isset($chars['40'])) { /* testing for ( */
- $level += $chars['40'];
- }
- if (isset($chars['41'])) { /* testing for ) */
- $level -= $chars['41'];
- $spaces[$level] = 0;
- /* if we were faking lets stop, this portion
- * of the thread is over
- */
- if ($level == $cutoff) {
- $fake = FALSE;
- }
- }
- if (isset($chars['32'])) { /* testing for space */
- if (!isset($spaces[$level])) {
- $spaces[$level] = 0;
- }
- $spaces[$level] += $chars['32'];
- }
- for ($x=0;$x<=$level;$x++) {
- if (isset($spaces[$x])) {
- $spaces_total += $spaces[$x];
- }
- }
- $indent = $level + $spaces_total;
- /* must have run into a message that broke the thread
- * so we are adjusting for that portion
- */
- if ($fake == TRUE) {
- $indent = $indent +1;
- }
- if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
- $child = $regs[1];
- }
- /* the thread must be broken if $indent == 0
- * so indent the message once and start faking it
- */
- if ($indent == 0) {
- $indent = 1;
- $fake = TRUE;
- $cutoff = $level;
- }
- /* dont need abs but if indent was negative
- * errors would occur
- */
- $indent_array[$child] = ($indent < 0) ? 0 : $indent;
- $spaces_total = 0;
- }
- }
- return $indent_array;
-}
-
-
-/**
- * Returns an array with each element as a string representing one
- * message-thread as returned by the IMAP server.
- * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
- */
-function get_thread_sort($imap_stream, $search='ALL') {
- global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $indent_array;
-
- $thread_temp = array ();
- if ($sort_by_ref == 1) {
- $sort_type = 'REFERENCES';
- } else {
- $sort_type = 'ORDEREDSUBJECT';
- }
- $query = "THREAD $sort_type ".strtoupper($default_charset)." $search";
-
- $thread_test = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE);
- /* fallback to default charset */
- if ($response == 'NO' && strpos($message,'[BADCHARSET]') !== false) {
- $query = "THREAD $sort_type US-ASCII $search";
- $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
- }
- if (isset($thread_test[0])) {
- for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) {
- if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) {
- $thread_list = trim($regs[1]);
- break;
- }
- }
- } else {
- $thread_list = "";
- }
- if (!preg_match("/OK/", $response)) {
- $server_sort_array = 'no';
- return $server_sort_array;
- }
- if (isset($thread_list)) {
- $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
- }
-
- $counter = 0;
- $thread_new = array();