annotate program/lib/Roundcube/rcube_result_thread.php @ 27:c32b53434b47

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