Thomas Bruederli
2015-02-07 2f8b1036da42ec3d15a51c6b17a473f9f4df71d3
program/lib/Roundcube/rcube_result_thread.php
@@ -2,8 +2,6 @@
/*
 +-----------------------------------------------------------------------+
 | program/include/rcube_result_thread.php                               |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
 | Copyright (C) 2011, Kolab Systems AG                                  |
@@ -14,13 +12,11 @@
 |                                                                       |
 | PURPOSE:                                                              |
 |   THREAD response handler                                             |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
*/
/**
 * Class for accessing IMAP's THREAD result
@@ -30,6 +26,8 @@
 */
class rcube_result_thread
{
    public $incomplete = false;
    protected $raw_data;
    protected $mailbox;
    protected $meta = array();
@@ -457,7 +455,7 @@
        // when sorting search result it's good to make the index smaller
        if ($index->count() != $this->count_messages()) {
            $index->intersect($this->get());
            $index->filter($this->get());
        }
        $result  = array_fill_keys($index->get(), null);
@@ -610,33 +608,39 @@
        // arrays handling is much more expensive
        // For the following structure: THREAD (2)(3 6 (4 23)(44 7 96))
        // -- 2
        //
        // -- 3
        //     \-- 6
        //         |-- 4
        //         |    \-- 23
        //         |
        //         \-- 44
        //              \-- 7
        //                   \-- 96
        //               \-- 7
        //                    \-- 96
        //
        // The output will be: 2,3^1:6^2:4^3:23^2:44^3:7^4:96
        if ($str[$begin] != '(') {
            $stop = $begin + strspn($str, '1234567890', $begin, $end - $begin);
            $msg  = substr($str, $begin, $stop - $begin);
            if (!$msg) {
            // find next bracket
            $stop      = $begin + strcspn($str, '()', $begin, $end - $begin);
            $messages  = explode(' ', trim(substr($str, $begin, $stop - $begin)));
            if (empty($messages)) {
                return $node;
            }
            $this->meta['messages']++;
            $node .= ($depth ? self::SEPARATOR_ITEM.$depth.self::SEPARATOR_LEVEL : '').$msg;
            if ($stop + 1 < $end) {
                $node .= $this->parse_thread($str, $stop + 1, $end, $depth + 1);
            foreach ($messages as $msg) {
                if ($msg) {
                    $node .= ($depth ? self::SEPARATOR_ITEM.$depth.self::SEPARATOR_LEVEL : '').$msg;
                    $this->meta['messages']++;
                    $depth++;
                }
            }
        } else {
            if ($stop < $end) {
                $node .= $this->parse_thread($str, $stop, $end, $depth);
            }
        }
        else {
            $off = $begin;
            while ($off < $end) {
                $start = $off;
@@ -653,7 +657,8 @@
                    if ($p1 !== false && $p1 < $p) {
                        $off = $p1 + 1;
                        $n++;
                    } else {
                    }
                    else {
                        $off = $p + 1;
                        $n--;
                    }