Example #1
0
function displayMessageArray($imapConnection, $num_msgs, $start_msg, $msort, $mailbox, $sort, $color, $show_num, $where = 0, $what = 0)
{
    global $imapServerAddress, $use_mailbox_cache, $index_order, $indent_array, $thread_sort_messages, $allow_server_sort, $server_sort_order, $PHP_SELF;
    $res = getEndMessage($start_msg, $show_num, $num_msgs);
    $start_msg = $res[0];
    $end_msg = $res[1];
    $urlMailbox = urlencode($mailbox);
    /* get indent level for subject display */
    if ($thread_sort_messages == 1 && $num_msgs) {
        $indent_array = get_parent_level($imapConnection);
    }
    $real_startMessage = $start_msg;
    if ($sort == 6) {
        if ($end_msg - $start_msg < $show_num - 1) {
            $end_msg = $end_msg - $start_msg + 1;
            $start_msg = 1;
        } else {
            if ($start_msg > $show_num) {
                $end_msg = $show_num;
                $start_msg = 1;
            }
        }
    }
    $endVar = $end_msg + 1;
    /*
     * Loop through and display the info for each message.
     * ($t is used for the checkbox number)
     */
    $t = 0;
    /* messages display */
    if (!$num_msgs) {
        /* if there's no messages in this folder */
        echo html_tag('tr', html_tag('td', "<br><b>" . _("THIS FOLDER IS EMPTY") . "</b><br>&nbsp;", 'center', $color[4], 'colspan="' . count($index_order) . '"'));
    } elseif ($start_msg == $end_msg) {
        /* if there's only one message in the box, handle it differently. */
        if ($sort != 6) {
            $i = $start_msg;
        } else {
            $i = 1;
        }
        reset($msort);
        $k = 0;
        do {
            $key = key($msort);
            next($msort);
            $k++;
        } while (isset($key) && $k < $i);
        printMessageInfo($imapConnection, $t, true, $key, $mailbox, $real_startMessage, $where, $what);
    } else {
        $i = $start_msg;
        reset($msort);
        $k = 0;
        do {
            $key = key($msort);
            next($msort);
            $k++;
        } while (isset($key) && $k < $i);
        $not_last = true;
        do {
            if (!$i || $i == $endVar - 1) {
                $not_last = false;
            }
            printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox, $real_startMessage, $where, $what);
            $key = key($msort);
            $t++;
            $i++;
            next($msort);
        } while ($i && $i < $endVar);
    }
}
/**
 * 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);
    }
    $char_count = count($thread_temp);
    $counter = 0;
    $thread_new = array();
    $k = 0;
    $thread_new[0] = "";
    /*
     * parse the thread response into separate threads
     *
     * example:
     *         [0] => (540)
     *         [1] => (1386)
     *         [2] => (1599 759 959 37)
     *         [3] => (492 1787)
     *         [4] => ((933)(1891))
     *         [5] => (1030 (1497)(845)(1637))
     */
    for ($i = 0, $iCnt = count($thread_temp); $i < $iCnt; $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;
            }
        }
    }
    $thread_new = array_reverse($thread_new);
    /* place the threads after each other in one string */
    $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;
    $indent_array = get_parent_level($thread_new);
    return array($thread_list, $indent_array);
}