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