| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           /**
  | 
        
        
            | 
            | 
           4 | 
            +-----------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           5 | 
            | This file is part of the Roundcube Webmail client                     |
  | 
        
        
            | 
            | 
           6 | 
            |                                                                       |
  | 
        
        
            | 
            | 
           7 | 
            | Copyright (C) The Roundcube Dev Team                                  |
  | 
        
        
            | 
            | 
           8 | 
            | Copyright (C) Kolab Systems AG                                        |
  | 
        
        
            | 
            | 
           9 | 
            |                                                                       |
  | 
        
        
            | 
            | 
           10 | 
            | Licensed under the GNU General Public License version 3 or            |
  | 
        
        
            | 
            | 
           11 | 
            | any later version with exceptions for skins & plugins.                |
  | 
        
        
            | 
            | 
           12 | 
            | See the README file for a full license statement.                     |
  | 
        
        
            | 
            | 
           13 | 
            |                                                                       |
  | 
        
        
            | 
            | 
           14 | 
            | PURPOSE:                                                              |
  | 
        
        
            | 
            | 
           15 | 
            |   THREAD response handler                                             |
  | 
        
        
            | 
            | 
           16 | 
            +-----------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           17 | 
            | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  | 
        
        
            | 
            | 
           18 | 
            | Author: Aleksander Machniak <alec@alec.pl>                            |
  | 
        
        
            | 
            | 
           19 | 
            +-----------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           20 | 
           */
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * Class for accessing IMAP's THREAD result
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package    Framework
  | 
        
        
            | 
            | 
           26 | 
            * @subpackage Storage
  | 
        
        
            | 
            | 
           27 | 
            */
  | 
        
        
            | 
            | 
           28 | 
           class rcube_result_thread
  | 
        
        
            | 
            | 
           29 | 
           {
  | 
        
        
            | 
            | 
           30 | 
               public $incomplete = false;
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               protected $raw_data;
  | 
        
        
            | 
            | 
           33 | 
               protected $mailbox;
  | 
        
        
            | 
            | 
           34 | 
               protected $meta  = [];
  | 
        
        
            | 
            | 
           35 | 
               protected $order = 'ASC';
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               const SEPARATOR_ELEMENT = ' ';
  | 
        
        
            | 
            | 
           38 | 
               const SEPARATOR_ITEM    = '~';
  | 
        
        
            | 
            | 
           39 | 
               const SEPARATOR_LEVEL   = ':';
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /**
  | 
        
        
            | 
            | 
           43 | 
                * Object constructor.
  | 
        
        
            | 
            | 
           44 | 
                */
  | 
        
        
            | 
            | 
           45 | 
               public function __construct($mailbox = null, $data = null)
  | 
        
        
            | 
            | 
           46 | 
               {
  | 
        
        
            | 
            | 
           47 | 
                   $this->mailbox = $mailbox;
  | 
        
        
            | 
            | 
           48 | 
                   $this->init($data);
  | 
        
        
            | 
            | 
           49 | 
               }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               /**
  | 
        
        
            | 
            | 
           52 | 
                * Initializes object with IMAP command response
  | 
        
        
            | 
            | 
           53 | 
                *
  | 
        
        
            | 
            | 
           54 | 
                * @param string $data IMAP response string
  | 
        
        
            | 
            | 
           55 | 
                */
  | 
        
        
            | 
            | 
           56 | 
               public function init($data = null)
  | 
        
        
            | 
            | 
           57 | 
               {
  | 
        
        
            | 
            | 
           58 | 
                   $this->meta = [];
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
                   $data = explode('*', (string) $data);
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
                   // ...skip unilateral untagged server responses
  | 
        
        
            | 
            | 
           63 | 
                   for ($i = 0, $len = count($data); $i < $len; $i++) {
  | 
        
        
            | 
            | 
           64 | 
                       if (preg_match('/^ THREAD/i', $data[$i])) {
  | 
        
        
            | 
            | 
           65 | 
                           // valid response, initialize raw_data for is_error()
  | 
        
        
            | 
            | 
           66 | 
                           $this->raw_data = '';
  | 
        
        
            | 
            | 
           67 | 
                           $data[$i] = substr($data[$i], 7);
  | 
        
        
            | 
            | 
           68 | 
                           break;
  | 
        
        
            | 
            | 
           69 | 
                       }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                       unset($data[$i]);
  | 
        
        
            | 
            | 
           72 | 
                   }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   if (empty($data)) {
  | 
        
        
            | 
            | 
           75 | 
                       return;
  | 
        
        
            | 
            | 
           76 | 
                   }
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   $data = array_shift($data);
  | 
        
        
            | 
            | 
           79 | 
                   $data = trim($data);
  | 
        
        
            | 
            | 
           80 | 
                   $data = preg_replace('/[\r\n]/', '', $data);
  | 
        
        
            | 
            | 
           81 | 
                   $data = preg_replace('/\s+/', ' ', $data);
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
                   $this->raw_data = empty($data) ? '' : $this->parse_thread($data);
  | 
        
        
            | 
            | 
           84 | 
               }
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
               /**
  | 
        
        
            | 
            | 
           87 | 
                * Checks the result from IMAP command
  | 
        
        
            | 
            | 
           88 | 
                *
  | 
        
        
            | 
            | 
           89 | 
                * @return bool True if the result is an error, False otherwise
  | 
        
        
            | 
            | 
           90 | 
                */
  | 
        
        
            | 
            | 
           91 | 
               public function is_error()
  | 
        
        
            | 
            | 
           92 | 
               {
  | 
        
        
            | 
            | 
           93 | 
                   return $this->raw_data === null;
  | 
        
        
            | 
            | 
           94 | 
               }
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
               /**
  | 
        
        
            | 
            | 
           97 | 
                * Checks if the result is empty
  | 
        
        
            | 
            | 
           98 | 
                *
  | 
        
        
            | 
            | 
           99 | 
                * @return bool True if the result is empty, False otherwise
  | 
        
        
            | 
            | 
           100 | 
                */
  | 
        
        
            | 
            | 
           101 | 
               public function is_empty()
  | 
        
        
            | 
            | 
           102 | 
               {
  | 
        
        
            | 
            | 
           103 | 
                   return empty($this->raw_data);
  | 
        
        
            | 
            | 
           104 | 
               }
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
               /**
  | 
        
        
            | 
            | 
           107 | 
                * Returns number of elements (threads) in the result
  | 
        
        
            | 
            | 
           108 | 
                *
  | 
        
        
            | 
            | 
           109 | 
                * @return int Number of elements
  | 
        
        
            | 
            | 
           110 | 
                */
  | 
        
        
            | 
            | 
           111 | 
               public function count()
  | 
        
        
            | 
            | 
           112 | 
               {
  | 
        
        
            | 
            | 
           113 | 
                   if (isset($this->meta['count'])) {
  | 
        
        
            | 
            | 
           114 | 
                       return $this->meta['count'];
  | 
        
        
            | 
            | 
           115 | 
                   }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
                   if (empty($this->raw_data)) {
  | 
        
        
            | 
            | 
           118 | 
                       $this->meta['count'] = 0;
  | 
        
        
            | 
            | 
           119 | 
                   }
  | 
        
        
            | 
            | 
           120 | 
                   else {
  | 
        
        
            | 
            | 
           121 | 
                       $this->meta['count'] = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT);
  | 
        
        
            | 
            | 
           122 | 
                   }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   if (!$this->meta['count']) {
  | 
        
        
            | 
            | 
           125 | 
                       $this->meta['messages'] = 0;
  | 
        
        
            | 
            | 
           126 | 
                   }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                   return $this->meta['count'];
  | 
        
        
            | 
            | 
           129 | 
               }
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
               /**
  | 
        
        
            | 
            | 
           132 | 
                * Returns number of all messages in the result
  | 
        
        
            | 
            | 
           133 | 
                *
  | 
        
        
            | 
            | 
           134 | 
                * @return int Number of elements
  | 
        
        
            | 
            | 
           135 | 
                */
  | 
        
        
            | 
            | 
           136 | 
               public function count_messages()
  | 
        
        
            | 
            | 
           137 | 
               {
  | 
        
        
            | 
            | 
           138 | 
                   if (isset($this->meta['messages'])) {
  | 
        
        
            | 
            | 
           139 | 
                       return $this->meta['messages'];
  | 
        
        
            | 
            | 
           140 | 
                   }
  | 
        
        
            | 
            | 
           141 | 
              | 
        
        
            | 
            | 
           142 | 
                   if (empty($this->raw_data)) {
  | 
        
        
            | 
            | 
           143 | 
                       $this->meta['messages'] = 0;
  | 
        
        
            | 
            | 
           144 | 
                   }
  | 
        
        
            | 
            | 
           145 | 
                   else {
  | 
        
        
            | 
            | 
           146 | 
                       $this->meta['messages'] = 1
  | 
        
        
            | 
            | 
           147 | 
                           + substr_count($this->raw_data, self::SEPARATOR_ELEMENT)
  | 
        
        
            | 
            | 
           148 | 
                           + substr_count($this->raw_data, self::SEPARATOR_ITEM);
  | 
        
        
            | 
            | 
           149 | 
                   }
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
                   if ($this->meta['messages'] == 0 || $this->meta['messages'] == 1) {
  | 
        
        
            | 
            | 
           152 | 
                       $this->meta['count'] = $this->meta['messages'];
  | 
        
        
            | 
            | 
           153 | 
                   }
  | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
                   return $this->meta['messages'];
  | 
        
        
            | 
            | 
           156 | 
               }
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
               /**
  | 
        
        
            | 
            | 
           159 | 
                * Returns maximum message identifier in the result
  | 
        
        
            | 
            | 
           160 | 
                *
  | 
        
        
            | 
            | 
           161 | 
                * @return int|null Maximum message identifier
  | 
        
        
            | 
            | 
           162 | 
                */
  | 
        
        
            | 
            | 
           163 | 
               public function max()
  | 
        
        
            | 
            | 
           164 | 
               {
  | 
        
        
            | 
            | 
           165 | 
                   if ($this->is_empty()) {
  | 
        
        
            | 
            | 
           166 | 
                       return null;
  | 
        
        
            | 
            | 
           167 | 
                   }
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
                   if (!isset($this->meta['max'])) {
  | 
        
        
            | 
            | 
           170 | 
                       $this->meta['max'] = (int) @max($this->get());
  | 
        
        
            | 
            | 
           171 | 
                   }
  | 
        
        
            | 
            | 
           172 | 
              | 
        
        
            | 
            | 
           173 | 
                   return $this->meta['max'];
  | 
        
        
            | 
            | 
           174 | 
               }
  | 
        
        
            | 
            | 
           175 | 
              | 
        
        
            | 
            | 
           176 | 
               /**
  | 
        
        
            | 
            | 
           177 | 
                * Returns minimum message identifier in the result
  | 
        
        
            | 
            | 
           178 | 
                *
  | 
        
        
            | 
            | 
           179 | 
                * @return int|null Minimum message identifier
  | 
        
        
            | 
            | 
           180 | 
                */
  | 
        
        
            | 
            | 
           181 | 
               public function min()
  | 
        
        
            | 
            | 
           182 | 
               {
  | 
        
        
            | 
            | 
           183 | 
                   if ($this->is_empty()) {
  | 
        
        
            | 
            | 
           184 | 
                       return null;
  | 
        
        
            | 
            | 
           185 | 
                   }
  | 
        
        
            | 
            | 
           186 | 
              | 
        
        
            | 
            | 
           187 | 
                   if (!isset($this->meta['min'])) {
  | 
        
        
            | 
            | 
           188 | 
                       $this->meta['min'] = (int) @min($this->get());
  | 
        
        
            | 
            | 
           189 | 
                   }
  | 
        
        
            | 
            | 
           190 | 
              | 
        
        
            | 
            | 
           191 | 
                   return $this->meta['min'];
  | 
        
        
            | 
            | 
           192 | 
               }
  | 
        
        
            | 
            | 
           193 | 
              | 
        
        
            | 
            | 
           194 | 
               /**
  | 
        
        
            | 
            | 
           195 | 
                * Slices data set.
  | 
        
        
            | 
            | 
           196 | 
                *
  | 
        
        
            | 
            | 
           197 | 
                * @param int $offset Offset (as for PHP's array_slice())
  | 
        
        
            | 
            | 
           198 | 
                * @param int $length Number of elements (as for PHP's array_slice())
  | 
        
        
            | 
            | 
           199 | 
                */
  | 
        
        
            | 
            | 
           200 | 
               public function slice($offset, $length)
  | 
        
        
            | 
            | 
           201 | 
               {
  | 
        
        
            | 
            | 
           202 | 
                   $data = explode(self::SEPARATOR_ELEMENT, $this->raw_data);
  | 
        
        
            | 
            | 
           203 | 
                   $data = array_slice($data, $offset, $length);
  | 
        
        
            | 
            | 
           204 | 
              | 
        
        
            | 
            | 
           205 | 
                   $this->meta          = [];
  | 
        
        
            | 
            | 
           206 | 
                   $this->meta['count'] = count($data);
  | 
        
        
            | 
            | 
           207 | 
                   $this->raw_data      = implode(self::SEPARATOR_ELEMENT, $data);
  | 
        
        
            | 
            | 
           208 | 
               }
  | 
        
        
            | 
            | 
           209 | 
              | 
        
        
            | 
            | 
           210 | 
               /**
  | 
        
        
            | 
            | 
           211 | 
                * Filters data set. Removes threads not listed in $roots list.
  | 
        
        
            | 
            | 
           212 | 
                *
  | 
        
        
            | 
            | 
           213 | 
                * @param array $roots List of IDs of thread roots.
  | 
        
        
            | 
            | 
           214 | 
                */
  | 
        
        
            | 
            | 
           215 | 
               public function filter($roots)
  | 
        
        
            | 
            | 
           216 | 
               {
  | 
        
        
            | 
            | 
           217 | 
                   $datalen = strlen($this->raw_data);
  | 
        
        
            | 
            | 
           218 | 
                   $roots   = array_flip($roots);
  | 
        
        
            | 
            | 
           219 | 
                   $result  = '';
  | 
        
        
            | 
            | 
           220 | 
                   $start   = 0;
  | 
        
        
            | 
            | 
           221 | 
              | 
        
        
            | 
            | 
           222 | 
                   $this->meta = ['count' => 0];
  | 
        
        
            | 
            | 
           223 | 
              | 
        
        
            | 
            | 
           224 | 
                   while ($start < $datalen
  | 
        
        
            | 
            | 
           225 | 
                       && (($pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start)) !== false
  | 
        
        
            | 
            | 
           226 | 
                           || ($pos = $datalen))
  | 
        
        
            | 
            | 
           227 | 
                   ) {
  | 
        
        
            | 
            | 
           228 | 
                       $len   = $pos - $start;
  | 
        
        
            | 
            | 
           229 | 
                       $elem  = substr($this->raw_data, $start, $len);
  | 
        
        
            | 
            | 
           230 | 
                       $start = $pos + 1;
  | 
        
        
            | 
            | 
           231 | 
              | 
        
        
            | 
            | 
           232 | 
                       // extract root message ID
  | 
        
        
            | 
            | 
           233 | 
                       if ($npos = strpos($elem, self::SEPARATOR_ITEM)) {
  | 
        
        
            | 
            | 
           234 | 
                           $root = (int) substr($elem, 0, $npos);
  | 
        
        
            | 
            | 
           235 | 
                       }
  | 
        
        
            | 
            | 
           236 | 
                       else {
  | 
        
        
            | 
            | 
           237 | 
                           $root = $elem;
  | 
        
        
            | 
            | 
           238 | 
                       }
  | 
        
        
            | 
            | 
           239 | 
              | 
        
        
            | 
            | 
           240 | 
                       if (isset($roots[$root])) {
  | 
        
        
            | 
            | 
           241 | 
                           $this->meta['count']++;
  | 
        
        
            | 
            | 
           242 | 
                           $result .= self::SEPARATOR_ELEMENT . $elem;
  | 
        
        
            | 
            | 
           243 | 
                       }
  | 
        
        
            | 
            | 
           244 | 
                   }
  | 
        
        
            | 
            | 
           245 | 
              | 
        
        
            | 
            | 
           246 | 
                   $this->raw_data = ltrim($result, self::SEPARATOR_ELEMENT);
  | 
        
        
            | 
            | 
           247 | 
               }
  | 
        
        
            | 
            | 
           248 | 
              | 
        
        
            | 
            | 
           249 | 
               /**
  | 
        
        
            | 
            | 
           250 | 
                * Reverts order of elements in the result
  | 
        
        
            | 
            | 
           251 | 
                */
  | 
        
        
            | 
            | 
           252 | 
               public function revert()
  | 
        
        
            | 
            | 
           253 | 
               {
  | 
        
        
            | 
            | 
           254 | 
                   $this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
  | 
        
        
            | 
            | 
           255 | 
              | 
        
        
            | 
            | 
           256 | 
                   if (empty($this->raw_data)) {
  | 
        
        
            | 
            | 
           257 | 
                       return;
  | 
        
        
            | 
            | 
           258 | 
                   }
  | 
        
        
            | 
            | 
           259 | 
              | 
        
        
            | 
            | 
           260 | 
                   $data = explode(self::SEPARATOR_ELEMENT, $this->raw_data);
  | 
        
        
            | 
            | 
           261 | 
                   $data = array_reverse($data);
  | 
        
        
            | 
            | 
           262 | 
                   $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
  | 
        
        
            | 
            | 
           263 | 
              | 
        
        
            | 
            | 
           264 | 
                   $this->meta['pos'] = [];
  | 
        
        
            | 
            | 
           265 | 
               }
  | 
        
        
            | 
            | 
           266 | 
              | 
        
        
            | 
            | 
           267 | 
               /**
  | 
        
        
            | 
            | 
           268 | 
                * Check if the given message ID exists in the object
  | 
        
        
            | 
            | 
           269 | 
                *
  | 
        
        
            | 
            | 
           270 | 
                * @param int $msgid Message ID
  | 
        
        
            | 
            | 
           271 | 
                * @param bool $get_index When enabled element's index will be returned.
  | 
        
        
            | 
            | 
           272 | 
                *                        Elements are indexed starting with 0
  | 
        
        
            | 
            | 
           273 | 
                *
  | 
        
        
            | 
            | 
           274 | 
                * @return bool True on success, False if message ID doesn't exist
  | 
        
        
            | 
            | 
           275 | 
                */
  | 
        
        
            | 
            | 
           276 | 
               public function exists($msgid, $get_index = false)
  | 
        
        
            | 
            | 
           277 | 
               {
  | 
        
        
            | 
            | 
           278 | 
                   $msgid = (int) $msgid;
  | 
        
        
            | 
            | 
           279 | 
                   $begin = implode('|', [
  | 
        
        
            | 
            | 
           280 | 
                       '^',
  | 
        
        
            | 
            | 
           281 | 
                       preg_quote(self::SEPARATOR_ELEMENT, '/'),
  | 
        
        
            | 
            | 
           282 | 
                       preg_quote(self::SEPARATOR_LEVEL, '/'),
  | 
        
        
            | 
            | 
           283 | 
                   ]);
  | 
        
        
            | 
            | 
           284 | 
                   $end = implode('|', [
  | 
        
        
            | 
            | 
           285 | 
                       '$',
  | 
        
        
            | 
            | 
           286 | 
                       preg_quote(self::SEPARATOR_ELEMENT, '/'),
  | 
        
        
            | 
            | 
           287 | 
                       preg_quote(self::SEPARATOR_ITEM, '/'),
  | 
        
        
            | 
            | 
           288 | 
                   ]);
  | 
        
        
            | 
            | 
           289 | 
              | 
        
        
            | 
            | 
           290 | 
                   if (preg_match("/($begin)$msgid($end)/", $this->raw_data, $m,
  | 
        
        
            | 
            | 
           291 | 
                       $get_index ? PREG_OFFSET_CAPTURE : 0)
  | 
        
        
            | 
            | 
           292 | 
                   ) {
  | 
        
        
            | 
            | 
           293 | 
                       if ($get_index) {
  | 
        
        
            | 
            | 
           294 | 
                           $idx = 0;
  | 
        
        
            | 
            | 
           295 | 
                           if ($m[0][1]) {
  | 
        
        
            | 
            | 
           296 | 
                               $idx = substr_count($this->raw_data, self::SEPARATOR_ELEMENT, 0, $m[0][1]+1)
  | 
        
        
            | 
            | 
           297 | 
                                   + substr_count($this->raw_data, self::SEPARATOR_ITEM, 0, $m[0][1]+1);
  | 
        
        
            | 
            | 
           298 | 
                           }
  | 
        
        
            | 
            | 
           299 | 
                           // cache position of this element, so we can use it in get_element()
  | 
        
        
            | 
            | 
           300 | 
                           $this->meta['pos'][$idx] = (int)$m[0][1];
  | 
        
        
            | 
            | 
           301 | 
              | 
        
        
            | 
            | 
           302 | 
                           return $idx;
  | 
        
        
            | 
            | 
           303 | 
                       }
  | 
        
        
            | 
            | 
           304 | 
                       return true;
  | 
        
        
            | 
            | 
           305 | 
                   }
  | 
        
        
            | 
            | 
           306 | 
              | 
        
        
            | 
            | 
           307 | 
                   return false;
  | 
        
        
            | 
            | 
           308 | 
               }
  | 
        
        
            | 
            | 
           309 | 
              | 
        
        
            | 
            | 
           310 | 
               /**
  | 
        
        
            | 
            | 
           311 | 
                * Return IDs of all messages in the result. Threaded data will be flattened.
  | 
        
        
            | 
            | 
           312 | 
                *
  | 
        
        
            | 
            | 
           313 | 
                * @return array List of message identifiers
  | 
        
        
            | 
            | 
           314 | 
                */
  | 
        
        
            | 
            | 
           315 | 
               public function get()
  | 
        
        
            | 
            | 
           316 | 
               {
  | 
        
        
            | 
            | 
           317 | 
                   if (empty($this->raw_data)) {
  | 
        
        
            | 
            | 
           318 | 
                       return [];
  | 
        
        
            | 
            | 
           319 | 
                   }
  | 
        
        
            | 
            | 
           320 | 
              | 
        
        
            | 
            | 
           321 | 
                   $regexp = '/(' . preg_quote(self::SEPARATOR_ELEMENT, '/')
  | 
        
        
            | 
            | 
           322 | 
                       . '|' . preg_quote(self::SEPARATOR_ITEM, '/') . '[0-9]+' . preg_quote(self::SEPARATOR_LEVEL, '/')
  | 
        
        
            | 
            | 
           323 | 
                       .')/';
  | 
        
        
            | 
            | 
           324 | 
              | 
        
        
            | 
            | 
           325 | 
                   return preg_split($regexp, $this->raw_data);
  | 
        
        
            | 
            | 
           326 | 
               }
  | 
        
        
            | 
            | 
           327 | 
              | 
        
        
            | 
            | 
           328 | 
               /**
  | 
        
        
            | 
            | 
           329 | 
                * Return all messages in the result.
  | 
        
        
            | 
            | 
           330 | 
                *
  | 
        
        
            | 
            | 
           331 | 
                * @return array List of message identifiers
  | 
        
        
            | 
            | 
           332 | 
                */
  | 
        
        
            | 
            | 
           333 | 
               public function get_compressed()
  | 
        
        
            | 
            | 
           334 | 
               {
  | 
        
        
            | 
            | 
           335 | 
                   if (empty($this->raw_data)) {
  | 
        
        
            | 
            | 
           336 | 
                       return '';
  | 
        
        
            | 
            | 
           337 | 
                   }
  | 
        
        
            | 
            | 
           338 | 
              | 
        
        
            | 
            | 
           339 | 
                   return rcube_imap_generic::compressMessageSet($this->get());
  | 
        
        
            | 
            | 
           340 | 
               }
  | 
        
        
            | 
            | 
           341 | 
              | 
        
        
            | 
            | 
           342 | 
               /**
  | 
        
        
            | 
            | 
           343 | 
                * Return result element at specified index (all messages, not roots)
  | 
        
        
            | 
            | 
           344 | 
                *
  | 
        
        
            | 
            | 
           345 | 
                * @param int|string  $index  Element's index or "FIRST" or "LAST"
  | 
        
        
            | 
            | 
           346 | 
                *
  | 
        
        
            | 
            | 
           347 | 
                * @return int Element value
  | 
        
        
            | 
            | 
           348 | 
                */
  | 
        
        
            | 
            | 
           349 | 
               public function get_element($index)
  | 
        
        
            | 
            | 
           350 | 
               {
  | 
        
        
            | 
            | 
           351 | 
                   $count = $this->count();
  | 
        
        
            | 
            | 
           352 | 
              | 
        
        
            | 
            | 
           353 | 
                   if (!$count) {
  | 
        
        
            | 
            | 
           354 | 
                       return null;
  | 
        
        
            | 
            | 
           355 | 
                   }
  | 
        
        
            | 
            | 
           356 | 
              | 
        
        
            | 
            | 
           357 | 
                   // first element
  | 
        
        
            | 
            | 
           358 | 
                   if ($index === 0 || $index === '0' || $index === 'FIRST') {
  | 
        
        
            | 
            | 
           359 | 
                       preg_match('/^([0-9]+)/', $this->raw_data, $m);
  | 
        
        
            | 
            | 
           360 | 
                       $result = (int) $m[1];
  | 
        
        
            | 
            | 
           361 | 
                       return $result;
  | 
        
        
            | 
            | 
           362 | 
                   }
  | 
        
        
            | 
            | 
           363 | 
              | 
        
        
            | 
            | 
           364 | 
                   // last element
  | 
        
        
            | 
            | 
           365 | 
                   if ($index === 'LAST' || $index == $count-1) {
  | 
        
        
            | 
            | 
           366 | 
                       preg_match('/([0-9]+)$/', $this->raw_data, $m);
  | 
        
        
            | 
            | 
           367 | 
                       $result = (int) $m[1];
  | 
        
        
            | 
            | 
           368 | 
                       return $result;
  | 
        
        
            | 
            | 
           369 | 
                   }
  | 
        
        
            | 
            | 
           370 | 
              | 
        
        
            | 
            | 
           371 | 
                   // do we know the position of the element or the neighbour of it?
  | 
        
        
            | 
            | 
           372 | 
                   if (!empty($this->meta['pos'])) {
  | 
        
        
            | 
            | 
           373 | 
                       $element = preg_quote(self::SEPARATOR_ELEMENT, '/');
  | 
        
        
            | 
            | 
           374 | 
                       $item    = preg_quote(self::SEPARATOR_ITEM, '/') . '[0-9]+' . preg_quote(self::SEPARATOR_LEVEL, '/') .'?';
  | 
        
        
            | 
            | 
           375 | 
                       $regexp  = '(' . $element . '|' . $item . ')';
  | 
        
        
            | 
            | 
           376 | 
              | 
        
        
            | 
            | 
           377 | 
                       if (isset($this->meta['pos'][$index])) {
  | 
        
        
           | 1441 | 
           ariadna | 
           378 | 
                           if (preg_match('/([0-9]+)/', $this->raw_data, $m, 0, $this->meta['pos'][$index])) {
  | 
        
        
           | 1 | 
           efrain | 
           379 | 
                               $result = $m[1];
  | 
        
        
            | 
            | 
           380 | 
                           }
  | 
        
        
            | 
            | 
           381 | 
                       }
  | 
        
        
            | 
            | 
           382 | 
                       else if (isset($this->meta['pos'][$index-1])) {
  | 
        
        
            | 
            | 
           383 | 
                           // get chunk of data after previous element
  | 
        
        
            | 
            | 
           384 | 
                           $data = substr($this->raw_data, $this->meta['pos'][$index-1]+1, 50);
  | 
        
        
            | 
            | 
           385 | 
                           $data = preg_replace('/^[0-9]+/', '', $data); // remove UID at $index position
  | 
        
        
            | 
            | 
           386 | 
                           $data = preg_replace("/^$regexp/", '', $data); // remove separator
  | 
        
        
            | 
            | 
           387 | 
                           if (preg_match('/^([0-9]+)/', $data, $m)) {
  | 
        
        
            | 
            | 
           388 | 
                               $result = $m[1];
  | 
        
        
            | 
            | 
           389 | 
                           }
  | 
        
        
            | 
            | 
           390 | 
                       }
  | 
        
        
            | 
            | 
           391 | 
                       else if (isset($this->meta['pos'][$index+1])) {
  | 
        
        
            | 
            | 
           392 | 
                           // get chunk of data before next element
  | 
        
        
            | 
            | 
           393 | 
                           $pos  = max(0, $this->meta['pos'][$index+1] - 50);
  | 
        
        
            | 
            | 
           394 | 
                           $len  = min(50, $this->meta['pos'][$index+1]);
  | 
        
        
            | 
            | 
           395 | 
                           $data = substr($this->raw_data, $pos, $len);
  | 
        
        
            | 
            | 
           396 | 
                           $data = preg_replace("/$regexp\$/", '', $data); // remove separator
  | 
        
        
            | 
            | 
           397 | 
              | 
        
        
            | 
            | 
           398 | 
                           if (preg_match('/([0-9]+)$/', $data, $m)) {
  | 
        
        
            | 
            | 
           399 | 
                               $result = $m[1];
  | 
        
        
            | 
            | 
           400 | 
                           }
  | 
        
        
            | 
            | 
           401 | 
                       }
  | 
        
        
            | 
            | 
           402 | 
              | 
        
        
            | 
            | 
           403 | 
                       if (isset($result)) {
  | 
        
        
            | 
            | 
           404 | 
                           return (int) $result;
  | 
        
        
            | 
            | 
           405 | 
                       }
  | 
        
        
            | 
            | 
           406 | 
                   }
  | 
        
        
            | 
            | 
           407 | 
              | 
        
        
            | 
            | 
           408 | 
                   // Finally use less effective method
  | 
        
        
            | 
            | 
           409 | 
                   $data = $this->get();
  | 
        
        
            | 
            | 
           410 | 
              | 
        
        
            | 
            | 
           411 | 
                   return $data[$index] ?? null;
  | 
        
        
            | 
            | 
           412 | 
               }
  | 
        
        
            | 
            | 
           413 | 
              | 
        
        
            | 
            | 
           414 | 
               /**
  | 
        
        
            | 
            | 
           415 | 
                * Returns response parameters e.g. MAILBOX, ORDER
  | 
        
        
            | 
            | 
           416 | 
                *
  | 
        
        
            | 
            | 
           417 | 
                * @param string $param Parameter name
  | 
        
        
            | 
            | 
           418 | 
                *
  | 
        
        
            | 
            | 
           419 | 
                * @return array|string Response parameters or parameter value
  | 
        
        
            | 
            | 
           420 | 
                */
  | 
        
        
            | 
            | 
           421 | 
               public function get_parameters($param=null)
  | 
        
        
            | 
            | 
           422 | 
               {
  | 
        
        
            | 
            | 
           423 | 
                   $params = [
  | 
        
        
            | 
            | 
           424 | 
                       'MAILBOX' => $this->mailbox,
  | 
        
        
            | 
            | 
           425 | 
                       'ORDER'   => $this->order,
  | 
        
        
            | 
            | 
           426 | 
                   ];
  | 
        
        
            | 
            | 
           427 | 
              | 
        
        
            | 
            | 
           428 | 
                   if ($param !== null) {
  | 
        
        
            | 
            | 
           429 | 
                       return $params[$param];
  | 
        
        
            | 
            | 
           430 | 
                   }
  | 
        
        
            | 
            | 
           431 | 
              | 
        
        
            | 
            | 
           432 | 
                   return $params;
  | 
        
        
            | 
            | 
           433 | 
               }
  | 
        
        
            | 
            | 
           434 | 
              | 
        
        
            | 
            | 
           435 | 
               /**
  | 
        
        
            | 
            | 
           436 | 
                * THREAD=REFS sorting implementation (based on provided index)
  | 
        
        
            | 
            | 
           437 | 
                *
  | 
        
        
            | 
            | 
           438 | 
                * @param rcube_result_index $index Sorted message identifiers
  | 
        
        
            | 
            | 
           439 | 
                */
  | 
        
        
            | 
            | 
           440 | 
               public function sort($index)
  | 
        
        
            | 
            | 
           441 | 
               {
  | 
        
        
            | 
            | 
           442 | 
                   $this->order = $index->get_parameters('ORDER');
  | 
        
        
            | 
            | 
           443 | 
              | 
        
        
            | 
            | 
           444 | 
                   if (empty($this->raw_data)) {
  | 
        
        
            | 
            | 
           445 | 
                       return;
  | 
        
        
            | 
            | 
           446 | 
                   }
  | 
        
        
            | 
            | 
           447 | 
              | 
        
        
            | 
            | 
           448 | 
                   // when sorting search result it's good to make the index smaller
  | 
        
        
            | 
            | 
           449 | 
                   if ($index->count() != $this->count_messages()) {
  | 
        
        
            | 
            | 
           450 | 
                       $index->filter($this->get());
  | 
        
        
            | 
            | 
           451 | 
                   }
  | 
        
        
            | 
            | 
           452 | 
              | 
        
        
            | 
            | 
           453 | 
                   $result  = array_fill_keys($index->get(), null);
  | 
        
        
            | 
            | 
           454 | 
                   $datalen = strlen($this->raw_data);
  | 
        
        
            | 
            | 
           455 | 
                   $start   = 0;
  | 
        
        
            | 
            | 
           456 | 
              | 
        
        
            | 
            | 
           457 | 
                   // Here we're parsing raw_data twice, we want only one big array
  | 
        
        
            | 
            | 
           458 | 
                   // in memory at a time
  | 
        
        
            | 
            | 
           459 | 
              | 
        
        
            | 
            | 
           460 | 
                   // Assign roots
  | 
        
        
            | 
            | 
           461 | 
                   while (
  | 
        
        
            | 
            | 
           462 | 
                       ($start < $datalen && ($pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start)))
  | 
        
        
            | 
            | 
           463 | 
                       || ($start < $datalen && ($pos = $datalen))
  | 
        
        
            | 
            | 
           464 | 
                   ) {
  | 
        
        
            | 
            | 
           465 | 
                       $len   = $pos - $start;
  | 
        
        
            | 
            | 
           466 | 
                       $elem  = substr($this->raw_data, $start, $len);
  | 
        
        
            | 
            | 
           467 | 
                       $start = $pos + 1;
  | 
        
        
            | 
            | 
           468 | 
              | 
        
        
            | 
            | 
           469 | 
                       $items = explode(self::SEPARATOR_ITEM, $elem);
  | 
        
        
            | 
            | 
           470 | 
                       $root  = (int) array_shift($items);
  | 
        
        
            | 
            | 
           471 | 
              | 
        
        
            | 
            | 
           472 | 
                       if ($root) {
  | 
        
        
            | 
            | 
           473 | 
                           $result[$root] = $root;
  | 
        
        
            | 
            | 
           474 | 
                           foreach ($items as $item) {
  | 
        
        
            | 
            | 
           475 | 
                               list($lv, $id) = explode(self::SEPARATOR_LEVEL, $item);
  | 
        
        
            | 
            | 
           476 | 
                               $result[$id] = $root;
  | 
        
        
            | 
            | 
           477 | 
                           }
  | 
        
        
            | 
            | 
           478 | 
                       }
  | 
        
        
            | 
            | 
           479 | 
                   }
  | 
        
        
            | 
            | 
           480 | 
              | 
        
        
            | 
            | 
           481 | 
                   // get only unique roots
  | 
        
        
            | 
            | 
           482 | 
                   $result = array_filter($result); // make sure there are no nulls
  | 
        
        
            | 
            | 
           483 | 
                   $result = array_unique($result);
  | 
        
        
            | 
            | 
           484 | 
              | 
        
        
            | 
            | 
           485 | 
                   // Re-sort raw data
  | 
        
        
            | 
            | 
           486 | 
                   $result = array_fill_keys($result, null);
  | 
        
        
            | 
            | 
           487 | 
                   $start = 0;
  | 
        
        
            | 
            | 
           488 | 
              | 
        
        
            | 
            | 
           489 | 
                   while (
  | 
        
        
            | 
            | 
           490 | 
                       ($start < $datalen && ($pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start)))
  | 
        
        
            | 
            | 
           491 | 
                       || ($start < $datalen && ($pos = $datalen))
  | 
        
        
            | 
            | 
           492 | 
                   ) {
  | 
        
        
            | 
            | 
           493 | 
                       $len   = $pos - $start;
  | 
        
        
            | 
            | 
           494 | 
                       $elem  = substr($this->raw_data, $start, $len);
  | 
        
        
            | 
            | 
           495 | 
                       $start = $pos + 1;
  | 
        
        
            | 
            | 
           496 | 
              | 
        
        
            | 
            | 
           497 | 
                       $npos = strpos($elem, self::SEPARATOR_ITEM);
  | 
        
        
            | 
            | 
           498 | 
                       $root = (int) ($npos ? substr($elem, 0, $npos) : $elem);
  | 
        
        
            | 
            | 
           499 | 
              | 
        
        
            | 
            | 
           500 | 
                       $result[$root] = $elem;
  | 
        
        
            | 
            | 
           501 | 
                   }
  | 
        
        
            | 
            | 
           502 | 
              | 
        
        
            | 
            | 
           503 | 
                   $this->raw_data = implode(self::SEPARATOR_ELEMENT, $result);
  | 
        
        
            | 
            | 
           504 | 
               }
  | 
        
        
            | 
            | 
           505 | 
              | 
        
        
            | 
            | 
           506 | 
               /**
  | 
        
        
            | 
            | 
           507 | 
                * Returns data as tree
  | 
        
        
            | 
            | 
           508 | 
                *
  | 
        
        
            | 
            | 
           509 | 
                * @return array Data tree
  | 
        
        
            | 
            | 
           510 | 
                */
  | 
        
        
            | 
            | 
           511 | 
               public function get_tree()
  | 
        
        
            | 
            | 
           512 | 
               {
  | 
        
        
            | 
            | 
           513 | 
                   $datalen = strlen($this->raw_data);
  | 
        
        
            | 
            | 
           514 | 
                   $result  = [];
  | 
        
        
            | 
            | 
           515 | 
                   $start   = 0;
  | 
        
        
            | 
            | 
           516 | 
              | 
        
        
            | 
            | 
           517 | 
                   while ($start < $datalen
  | 
        
        
            | 
            | 
           518 | 
                       && (($pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start)) !== false
  | 
        
        
            | 
            | 
           519 | 
                           || ($pos = $datalen))
  | 
        
        
            | 
            | 
           520 | 
                   ) {
  | 
        
        
            | 
            | 
           521 | 
                       $len   = $pos - $start;
  | 
        
        
            | 
            | 
           522 | 
                       $elem  = substr($this->raw_data, $start, $len);
  | 
        
        
            | 
            | 
           523 | 
                       $items = explode(self::SEPARATOR_ITEM, $elem);
  | 
        
        
            | 
            | 
           524 | 
                       $result[array_shift($items)] = $this->build_thread($items);
  | 
        
        
            | 
            | 
           525 | 
                       $start = $pos + 1;
  | 
        
        
            | 
            | 
           526 | 
                   }
  | 
        
        
            | 
            | 
           527 | 
              | 
        
        
            | 
            | 
           528 | 
                   return $result;
  | 
        
        
            | 
            | 
           529 | 
               }
  | 
        
        
            | 
            | 
           530 | 
              | 
        
        
            | 
            | 
           531 | 
               /**
  | 
        
        
            | 
            | 
           532 | 
                * Returns thread depth and children data
  | 
        
        
            | 
            | 
           533 | 
                *
  | 
        
        
            | 
            | 
           534 | 
                * @return array Thread data
  | 
        
        
            | 
            | 
           535 | 
                */
  | 
        
        
            | 
            | 
           536 | 
               public function get_thread_data()
  | 
        
        
            | 
            | 
           537 | 
               {
  | 
        
        
            | 
            | 
           538 | 
                   $data     = $this->get_tree();
  | 
        
        
            | 
            | 
           539 | 
                   $depth    = [];
  | 
        
        
            | 
            | 
           540 | 
                   $children = [];
  | 
        
        
            | 
            | 
           541 | 
              | 
        
        
            | 
            | 
           542 | 
                   $this->build_thread_data($data, $depth, $children);
  | 
        
        
            | 
            | 
           543 | 
              | 
        
        
            | 
            | 
           544 | 
                   return [$depth, $children];
  | 
        
        
            | 
            | 
           545 | 
               }
  | 
        
        
            | 
            | 
           546 | 
              | 
        
        
            | 
            | 
           547 | 
               /**
  | 
        
        
            | 
            | 
           548 | 
                * Creates 'depth' and 'children' arrays from stored thread 'tree' data.
  | 
        
        
            | 
            | 
           549 | 
                */
  | 
        
        
            | 
            | 
           550 | 
               protected function build_thread_data($data, &$depth, &$children, $level = 0)
  | 
        
        
            | 
            | 
           551 | 
               {
  | 
        
        
            | 
            | 
           552 | 
                   foreach ((array)$data as $key => $val) {
  | 
        
        
            | 
            | 
           553 | 
                       $empty          = empty($val) || !is_array($val);
  | 
        
        
            | 
            | 
           554 | 
                       $children[$key] = !$empty;
  | 
        
        
            | 
            | 
           555 | 
                       $depth[$key]    = $level;
  | 
        
        
            | 
            | 
           556 | 
                       if (!$empty) {
  | 
        
        
            | 
            | 
           557 | 
                           $this->build_thread_data($val, $depth, $children, $level + 1);
  | 
        
        
            | 
            | 
           558 | 
                       }
  | 
        
        
            | 
            | 
           559 | 
                   }
  | 
        
        
            | 
            | 
           560 | 
               }
  | 
        
        
            | 
            | 
           561 | 
              | 
        
        
            | 
            | 
           562 | 
               /**
  | 
        
        
            | 
            | 
           563 | 
                * Converts part of the raw thread into an array
  | 
        
        
            | 
            | 
           564 | 
                */
  | 
        
        
            | 
            | 
           565 | 
               protected function build_thread($items, $level = 1, &$pos = 0)
  | 
        
        
            | 
            | 
           566 | 
               {
  | 
        
        
            | 
            | 
           567 | 
                   $result = [];
  | 
        
        
            | 
            | 
           568 | 
              | 
        
        
            | 
            | 
           569 | 
                   for ($len=count($items); $pos < $len; $pos++) {
  | 
        
        
            | 
            | 
           570 | 
                       list($lv, $id) = explode(self::SEPARATOR_LEVEL, $items[$pos]);
  | 
        
        
            | 
            | 
           571 | 
                       if ($level == $lv) {
  | 
        
        
            | 
            | 
           572 | 
                           $pos++;
  | 
        
        
            | 
            | 
           573 | 
                           $result[$id] = $this->build_thread($items, $level+1, $pos);
  | 
        
        
            | 
            | 
           574 | 
                       }
  | 
        
        
            | 
            | 
           575 | 
                       else {
  | 
        
        
            | 
            | 
           576 | 
                           $pos--;
  | 
        
        
            | 
            | 
           577 | 
                           break;
  | 
        
        
            | 
            | 
           578 | 
                       }
  | 
        
        
            | 
            | 
           579 | 
                   }
  | 
        
        
            | 
            | 
           580 | 
              | 
        
        
            | 
            | 
           581 | 
                   return $result;
  | 
        
        
            | 
            | 
           582 | 
               }
  | 
        
        
            | 
            | 
           583 | 
              | 
        
        
            | 
            | 
           584 | 
               /**
  | 
        
        
            | 
            | 
           585 | 
                * IMAP THREAD response parser
  | 
        
        
            | 
            | 
           586 | 
                */
  | 
        
        
            | 
            | 
           587 | 
               protected function parse_thread($str, $begin = 0, $end = 0, $depth = 0)
  | 
        
        
            | 
            | 
           588 | 
               {
  | 
        
        
            | 
            | 
           589 | 
                   // Don't be tempted to change $str to pass by reference to speed this up - it will slow it down by about
  | 
        
        
           | 1441 | 
           ariadna | 
           590 | 
                   // 7 times instead :-) See comments on https://www.php.net/references and this article:
  | 
        
        
            | 
            | 
           591 | 
                   // https://derickrethans.nl/files/phparch-php-variables-article.pdf
  | 
        
        
           | 1 | 
           efrain | 
           592 | 
                   $node = '';
  | 
        
        
            | 
            | 
           593 | 
                   if (!$end) {
  | 
        
        
            | 
            | 
           594 | 
                       $end = strlen($str);
  | 
        
        
            | 
            | 
           595 | 
                   }
  | 
        
        
            | 
            | 
           596 | 
              | 
        
        
            | 
            | 
           597 | 
                   // Let's try to store data in max. compacted structure as a string,
  | 
        
        
            | 
            | 
           598 | 
                   // arrays handling is much more expensive
  | 
        
        
            | 
            | 
           599 | 
                   // For the following structure: THREAD (2)(3 6 (4 23)(44 7 96))((11)(12))
  | 
        
        
            | 
            | 
           600 | 
                   // -- 2
  | 
        
        
            | 
            | 
           601 | 
                   // -- 3
  | 
        
        
            | 
            | 
           602 | 
                   //     \-- 6
  | 
        
        
            | 
            | 
           603 | 
                   //         |-- 4
  | 
        
        
            | 
            | 
           604 | 
                   //         |    \-- 23
  | 
        
        
            | 
            | 
           605 | 
                   //         |
  | 
        
        
            | 
            | 
           606 | 
                   //         \-- 44
  | 
        
        
            | 
            | 
           607 | 
                   //               \-- 7
  | 
        
        
            | 
            | 
           608 | 
                   //                    \-- 96
  | 
        
        
            | 
            | 
           609 | 
                   // -- 11
  | 
        
        
            | 
            | 
           610 | 
                   //      \-- 12
  | 
        
        
            | 
            | 
           611 | 
                   //
  | 
        
        
            | 
            | 
           612 | 
                   // The output will be: 2 3~1:6~2:4~3:23~2:44~3:7~4:96 11~1:12
  | 
        
        
            | 
            | 
           613 | 
                   // Note: The "11" thread has no root, we use the first message as root
  | 
        
        
            | 
            | 
           614 | 
              | 
        
        
            | 
            | 
           615 | 
                   if ($str[$begin] != '(') {
  | 
        
        
            | 
            | 
           616 | 
                       // find next bracket
  | 
        
        
            | 
            | 
           617 | 
                       $stop      = $begin + strcspn($str, '()', $begin, $end - $begin);
  | 
        
        
            | 
            | 
           618 | 
                       $messages  = explode(' ', trim(substr($str, $begin, $stop - $begin)));
  | 
        
        
            | 
            | 
           619 | 
              | 
        
        
            | 
            | 
           620 | 
                       if (empty($messages)) {
  | 
        
        
            | 
            | 
           621 | 
                           return $node;
  | 
        
        
            | 
            | 
           622 | 
                       }
  | 
        
        
            | 
            | 
           623 | 
              | 
        
        
            | 
            | 
           624 | 
                       foreach ($messages as $msg) {
  | 
        
        
            | 
            | 
           625 | 
                           if ($msg) {
  | 
        
        
            | 
            | 
           626 | 
                               $node .= ($depth ? self::SEPARATOR_ITEM.$depth.self::SEPARATOR_LEVEL : '').$msg;
  | 
        
        
            | 
            | 
           627 | 
                               if (isset($this->meta['messages'])) {
  | 
        
        
            | 
            | 
           628 | 
                                   $this->meta['messages']++;
  | 
        
        
            | 
            | 
           629 | 
                               }
  | 
        
        
            | 
            | 
           630 | 
                               else {
  | 
        
        
            | 
            | 
           631 | 
                                   $this->meta['messages'] = 1;
  | 
        
        
            | 
            | 
           632 | 
                               }
  | 
        
        
            | 
            | 
           633 | 
                               $depth++;
  | 
        
        
            | 
            | 
           634 | 
                           }
  | 
        
        
            | 
            | 
           635 | 
                       }
  | 
        
        
            | 
            | 
           636 | 
              | 
        
        
            | 
            | 
           637 | 
                       if ($stop < $end) {
  | 
        
        
            | 
            | 
           638 | 
                           $node .= $this->parse_thread($str, $stop, $end, $depth);
  | 
        
        
            | 
            | 
           639 | 
                       }
  | 
        
        
            | 
            | 
           640 | 
                   }
  | 
        
        
            | 
            | 
           641 | 
                   else {
  | 
        
        
            | 
            | 
           642 | 
                       $off = $begin;
  | 
        
        
            | 
            | 
           643 | 
                       while ($off < $end) {
  | 
        
        
            | 
            | 
           644 | 
                           $start = $off;
  | 
        
        
            | 
            | 
           645 | 
                           $off++;
  | 
        
        
            | 
            | 
           646 | 
                           $n = 1;
  | 
        
        
            | 
            | 
           647 | 
                           while ($n > 0) {
  | 
        
        
            | 
            | 
           648 | 
                               $p = strpos($str, ')', $off);
  | 
        
        
            | 
            | 
           649 | 
                               if ($p === false) {
  | 
        
        
            | 
            | 
           650 | 
                                   // error, wrong structure, mismatched brackets in IMAP THREAD response
  | 
        
        
            | 
            | 
           651 | 
                                   // @TODO: write error to the log or maybe set $this->raw_data = null;
  | 
        
        
            | 
            | 
           652 | 
                                   return $node;
  | 
        
        
            | 
            | 
           653 | 
                               }
  | 
        
        
            | 
            | 
           654 | 
              | 
        
        
            | 
            | 
           655 | 
                               $p1 = strpos($str, '(', $off);
  | 
        
        
            | 
            | 
           656 | 
                               if ($p1 !== false && $p1 < $p) {
  | 
        
        
            | 
            | 
           657 | 
                                   $off = $p1 + 1;
  | 
        
        
            | 
            | 
           658 | 
                                   $n++;
  | 
        
        
            | 
            | 
           659 | 
                               }
  | 
        
        
            | 
            | 
           660 | 
                               else {
  | 
        
        
            | 
            | 
           661 | 
                                   $off = $p + 1;
  | 
        
        
            | 
            | 
           662 | 
                                   $n--;
  | 
        
        
            | 
            | 
           663 | 
                               }
  | 
        
        
            | 
            | 
           664 | 
                           }
  | 
        
        
            | 
            | 
           665 | 
              | 
        
        
            | 
            | 
           666 | 
                           // Handle threads with missing parent by using first message as root
  | 
        
        
            | 
            | 
           667 | 
                           if (substr_compare($str, '((', $start, 2) === 0) {
  | 
        
        
            | 
            | 
           668 | 
                               // Extract the current thread, e.g. "((1)(2))"
  | 
        
        
            | 
            | 
           669 | 
                               $thread = substr($str, $start, $off - $start);
  | 
        
        
            | 
            | 
           670 | 
                               // Length of the first token, e.g. "(1)"
  | 
        
        
            | 
            | 
           671 | 
                               $len = strspn($thread, '(0123456789', 1) + 1;
  | 
        
        
            | 
            | 
           672 | 
                               // Extract the token and modify it to look like a thread root
  | 
        
        
            | 
            | 
           673 | 
                               $token = substr($thread, 1, $len);
  | 
        
        
            | 
            | 
           674 | 
                               // Warning: The order is important
  | 
        
        
            | 
            | 
           675 | 
                               $token = str_replace('(', '', $token);
  | 
        
        
            | 
            | 
           676 | 
                               $token = str_replace(' ', ' (', $token);
  | 
        
        
            | 
            | 
           677 | 
                               $token = str_replace(')', ' ', $token);
  | 
        
        
            | 
            | 
           678 | 
                               $thread  = substr_replace($thread, $token, 1, $len);
  | 
        
        
            | 
            | 
           679 | 
                               // Parse the thread
  | 
        
        
            | 
            | 
           680 | 
                               $thread = $this->parse_thread($thread, 0, 0, $depth);
  | 
        
        
            | 
            | 
           681 | 
                           }
  | 
        
        
            | 
            | 
           682 | 
                           else {
  | 
        
        
            | 
            | 
           683 | 
                               $thread = $this->parse_thread($str, $start + 1, $off - 1, $depth);
  | 
        
        
            | 
            | 
           684 | 
                           }
  | 
        
        
            | 
            | 
           685 | 
              | 
        
        
            | 
            | 
           686 | 
                           if ($thread) {
  | 
        
        
            | 
            | 
           687 | 
                               if (!$depth) {
  | 
        
        
            | 
            | 
           688 | 
                                   if ($node) {
  | 
        
        
            | 
            | 
           689 | 
                                       $node .= self::SEPARATOR_ELEMENT;
  | 
        
        
            | 
            | 
           690 | 
                                   }
  | 
        
        
            | 
            | 
           691 | 
                               }
  | 
        
        
            | 
            | 
           692 | 
                               $node .= $thread;
  | 
        
        
            | 
            | 
           693 | 
                           }
  | 
        
        
            | 
            | 
           694 | 
                       }
  | 
        
        
            | 
            | 
           695 | 
                   }
  | 
        
        
            | 
            | 
           696 | 
              | 
        
        
            | 
            | 
           697 | 
                   return $node;
  | 
        
        
            | 
            | 
           698 | 
               }
  | 
        
        
            | 
            | 
           699 | 
           }
  |