| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
+-----------------------------------------------------------------------+
|
|
|
5 |
| This file is part of the Roundcube Webmail client |
|
|
|
6 |
| |
|
|
|
7 |
| Copyright (C) The Roundcube Dev Team |
|
|
|
8 |
| Copyright (C) Kolab Systems AG |
|
|
|
9 |
| |
|
|
|
10 |
| Licensed under the GNU General Public License version 3 or |
|
|
|
11 |
| any later version with exceptions for skins & plugins. |
|
|
|
12 |
| See the README file for a full license statement. |
|
|
|
13 |
| |
|
|
|
14 |
| PURPOSE: |
|
|
|
15 |
| SORT/SEARCH/ESEARCH response handler |
|
|
|
16 |
+-----------------------------------------------------------------------+
|
|
|
17 |
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
|
18 |
| Author: Aleksander Machniak <alec@alec.pl> |
|
|
|
19 |
+-----------------------------------------------------------------------+
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Class for accessing IMAP's SORT/SEARCH/ESEARCH result
|
|
|
24 |
*
|
|
|
25 |
* @package Framework
|
|
|
26 |
* @subpackage Storage
|
|
|
27 |
*/
|
|
|
28 |
class rcube_result_index
|
|
|
29 |
{
|
|
|
30 |
public $incomplete = false;
|
|
|
31 |
|
|
|
32 |
protected $raw_data;
|
|
|
33 |
protected $mailbox;
|
|
|
34 |
protected $meta = [];
|
|
|
35 |
protected $params = [];
|
|
|
36 |
protected $order = 'ASC';
|
|
|
37 |
|
|
|
38 |
const SEPARATOR_ELEMENT = ' ';
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Object constructor.
|
|
|
43 |
*/
|
|
|
44 |
public function __construct($mailbox = null, $data = null, $order = null)
|
|
|
45 |
{
|
|
|
46 |
$this->mailbox = $mailbox;
|
|
|
47 |
$this->order = $order == 'DESC' ? 'DESC' : 'ASC';
|
|
|
48 |
$this->init($data);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Initializes object with SORT command response
|
|
|
53 |
*
|
|
|
54 |
* @param string $data IMAP response string
|
|
|
55 |
*/
|
|
|
56 |
public function init($data = null)
|
|
|
57 |
{
|
|
|
58 |
$this->meta = [];
|
|
|
59 |
|
|
|
60 |
$data = explode('*', (string)$data);
|
|
|
61 |
|
|
|
62 |
// ...skip unilateral untagged server responses
|
|
|
63 |
for ($i=0, $len=count($data); $i<$len; $i++) {
|
|
|
64 |
$data_item = &$data[$i];
|
|
|
65 |
if (preg_match('/^ SORT/i', $data_item)) {
|
|
|
66 |
// valid response, initialize raw_data for is_error()
|
|
|
67 |
$this->raw_data = '';
|
|
|
68 |
$data_item = substr($data_item, 5);
|
|
|
69 |
break;
|
|
|
70 |
}
|
|
|
71 |
else if (preg_match('/^ (E?SEARCH)/i', $data_item, $m)) {
|
|
|
72 |
// valid response, initialize raw_data for is_error()
|
|
|
73 |
$this->raw_data = '';
|
|
|
74 |
$data_item = substr($data_item, strlen($m[0]));
|
|
|
75 |
|
|
|
76 |
if (strtoupper($m[1]) == 'ESEARCH') {
|
|
|
77 |
$data_item = trim($data_item);
|
|
|
78 |
// remove MODSEQ response
|
|
|
79 |
if (preg_match('/\(MODSEQ ([0-9]+)\)$/i', $data_item, $m)) {
|
|
|
80 |
$data_item = substr($data_item, 0, -strlen($m[0]));
|
|
|
81 |
$this->params['MODSEQ'] = $m[1];
|
|
|
82 |
}
|
|
|
83 |
// remove TAG response part
|
|
|
84 |
if (preg_match('/^\(TAG ["a-z0-9]+\)\s*/i', $data_item, $m)) {
|
|
|
85 |
$data_item = substr($data_item, strlen($m[0]));
|
|
|
86 |
}
|
|
|
87 |
// remove UID
|
|
|
88 |
$data_item = preg_replace('/^UID\s*/i', '', $data_item);
|
|
|
89 |
|
|
|
90 |
// ESEARCH parameters
|
|
|
91 |
while (preg_match('/^([a-z]+) ([0-9:,]+)\s*/i', $data_item, $m)) {
|
|
|
92 |
$param = strtoupper($m[1]);
|
|
|
93 |
$value = $m[2];
|
|
|
94 |
|
|
|
95 |
$this->params[$param] = $value;
|
|
|
96 |
$data_item = substr($data_item, strlen($m[0]));
|
|
|
97 |
|
|
|
98 |
if (in_array($param, ['COUNT', 'MIN', 'MAX'])) {
|
|
|
99 |
$this->meta[strtolower($param)] = (int) $value;
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// @TODO: Implement compression using compressMessageSet() in __sleep() and __wakeup() ?
|
|
|
104 |
// @TODO: work with compressed result?!
|
|
|
105 |
if (isset($this->params['ALL'])) {
|
|
|
106 |
$data_item = implode(self::SEPARATOR_ELEMENT,
|
|
|
107 |
rcube_imap_generic::uncompressMessageSet($this->params['ALL']));
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
break;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
unset($data[$i]);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$data = array_filter($data);
|
|
|
118 |
|
|
|
119 |
if (empty($data)) {
|
|
|
120 |
return;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
$data = array_shift($data);
|
|
|
124 |
$data = trim($data);
|
|
|
125 |
$data = preg_replace('/[\r\n]/', '', $data);
|
|
|
126 |
$data = preg_replace('/\s+/', ' ', $data);
|
|
|
127 |
|
|
|
128 |
$this->raw_data = $data;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Checks the result from IMAP command
|
|
|
133 |
*
|
|
|
134 |
* @return bool True if the result is an error, False otherwise
|
|
|
135 |
*/
|
|
|
136 |
public function is_error()
|
|
|
137 |
{
|
|
|
138 |
return $this->raw_data === null;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Checks if the result is empty
|
|
|
143 |
*
|
|
|
144 |
* @return bool True if the result is empty, False otherwise
|
|
|
145 |
*/
|
|
|
146 |
public function is_empty()
|
|
|
147 |
{
|
|
|
148 |
return empty($this->raw_data)
|
|
|
149 |
&& empty($this->meta['max']) && empty($this->meta['min']) && empty($this->meta['count']);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Returns number of elements in the result
|
|
|
154 |
*
|
|
|
155 |
* @return int Number of elements
|
|
|
156 |
*/
|
|
|
157 |
public function count()
|
|
|
158 |
{
|
|
|
159 |
if (isset($this->meta['count'])) {
|
|
|
160 |
return $this->meta['count'];
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
if (empty($this->raw_data)) {
|
|
|
164 |
$this->meta['count'] = 0;
|
|
|
165 |
$this->meta['length'] = 0;
|
|
|
166 |
}
|
|
|
167 |
else {
|
|
|
168 |
$this->meta['count'] = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
return $this->meta['count'];
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Returns number of elements in the result.
|
|
|
176 |
* Alias for count() for compatibility with rcube_result_thread
|
|
|
177 |
*
|
|
|
178 |
* @return int Number of elements
|
|
|
179 |
*/
|
|
|
180 |
public function count_messages()
|
|
|
181 |
{
|
|
|
182 |
return $this->count();
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Returns maximal message identifier in the result
|
|
|
187 |
*
|
|
|
188 |
* @return int|null Maximal message identifier
|
|
|
189 |
*/
|
|
|
190 |
public function max()
|
|
|
191 |
{
|
|
|
192 |
if ($this->is_empty()) {
|
|
|
193 |
return null;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if (!isset($this->meta['max'])) {
|
|
|
197 |
$this->meta['max'] = null;
|
|
|
198 |
$all = $this->get();
|
|
|
199 |
if (!empty($all)) {
|
|
|
200 |
$this->meta['max'] = (int) max($all);
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
return $this->meta['max'];
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Returns minimal message identifier in the result
|
|
|
209 |
*
|
|
|
210 |
* @return int|null Minimal message identifier
|
|
|
211 |
*/
|
|
|
212 |
public function min()
|
|
|
213 |
{
|
|
|
214 |
if ($this->is_empty()) {
|
|
|
215 |
return null;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
if (!isset($this->meta['min'])) {
|
|
|
219 |
$this->meta['min'] = null;
|
|
|
220 |
$all = $this->get();
|
|
|
221 |
if (!empty($all)) {
|
|
|
222 |
$this->meta['min'] = (int) min($all);
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
return $this->meta['min'];
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Slices data set.
|
|
|
231 |
*
|
|
|
232 |
* @param int $offset Offset (as for PHP's array_slice())
|
|
|
233 |
* @param int $length Number of elements (as for PHP's array_slice())
|
|
|
234 |
*/
|
|
|
235 |
public function slice($offset, $length)
|
|
|
236 |
{
|
|
|
237 |
$data = $this->get();
|
|
|
238 |
$data = array_slice($data, $offset, $length);
|
|
|
239 |
|
|
|
240 |
$this->meta = [];
|
|
|
241 |
$this->meta['count'] = count($data);
|
|
|
242 |
$this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Filters data set. Removes elements not listed in $ids list.
|
|
|
247 |
*
|
|
|
248 |
* @param array $ids List of IDs to remove.
|
|
|
249 |
*/
|
|
|
250 |
public function filter($ids = [])
|
|
|
251 |
{
|
|
|
252 |
$data = $this->get();
|
|
|
253 |
$data = array_intersect($data, $ids);
|
|
|
254 |
|
|
|
255 |
$this->meta = [];
|
|
|
256 |
$this->meta['count'] = count($data);
|
|
|
257 |
$this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Reverts order of elements in the result
|
|
|
262 |
*/
|
|
|
263 |
public function revert()
|
|
|
264 |
{
|
|
|
265 |
$this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
|
|
|
266 |
|
|
|
267 |
if (empty($this->raw_data)) {
|
|
|
268 |
return;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
$data = $this->get();
|
|
|
272 |
$data = array_reverse($data);
|
|
|
273 |
$this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
|
|
|
274 |
|
|
|
275 |
$this->meta['pos'] = [];
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Check if the given message ID exists in the object
|
|
|
280 |
*
|
|
|
281 |
* @param int $msgid Message ID
|
|
|
282 |
* @param bool $get_index When enabled element's index will be returned.
|
|
|
283 |
* Elements are indexed starting with 0
|
|
|
284 |
*
|
|
|
285 |
* @return mixed False if message ID doesn't exist, True if exists or
|
|
|
286 |
* index of the element if $get_index=true
|
|
|
287 |
*/
|
|
|
288 |
public function exists($msgid, $get_index = false)
|
|
|
289 |
{
|
|
|
290 |
if (empty($this->raw_data)) {
|
|
|
291 |
return false;
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
$msgid = (int) $msgid;
|
|
|
295 |
$begin = implode('|', ['^', preg_quote(self::SEPARATOR_ELEMENT, '/')]);
|
|
|
296 |
$end = implode('|', ['$', preg_quote(self::SEPARATOR_ELEMENT, '/')]);
|
|
|
297 |
|
|
|
298 |
if (preg_match("/($begin)$msgid($end)/", $this->raw_data, $m,
|
|
|
299 |
$get_index ? PREG_OFFSET_CAPTURE : 0)
|
|
|
300 |
) {
|
|
|
301 |
if ($get_index) {
|
|
|
302 |
$idx = 0;
|
|
|
303 |
if (!empty($m[0][1])) {
|
|
|
304 |
$idx = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT, 0, $m[0][1]);
|
|
|
305 |
}
|
|
|
306 |
// cache position of this element, so we can use it in get_element()
|
|
|
307 |
$this->meta['pos'][$idx] = (int)$m[0][1];
|
|
|
308 |
|
|
|
309 |
return $idx;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
return true;
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
return false;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Return all messages in the result.
|
|
|
320 |
*
|
|
|
321 |
* @return array List of message IDs
|
|
|
322 |
*/
|
|
|
323 |
public function get()
|
|
|
324 |
{
|
|
|
325 |
if (empty($this->raw_data)) {
|
|
|
326 |
return [];
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
return explode(self::SEPARATOR_ELEMENT, $this->raw_data);
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Return all messages in the result.
|
|
|
334 |
*
|
|
|
335 |
* @return array List of message IDs
|
|
|
336 |
*/
|
|
|
337 |
public function get_compressed()
|
|
|
338 |
{
|
|
|
339 |
if (empty($this->raw_data)) {
|
|
|
340 |
return '';
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
return rcube_imap_generic::compressMessageSet($this->get());
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Return result element at specified index
|
|
|
348 |
*
|
|
|
349 |
* @param int|string $index Element's index or "FIRST" or "LAST"
|
|
|
350 |
*
|
|
|
351 |
* @return int|null Element value
|
|
|
352 |
*/
|
|
|
353 |
public function get_element($index)
|
|
|
354 |
{
|
|
|
355 |
if (empty($this->raw_data)) {
|
|
|
356 |
return null;
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
$count = $this->count();
|
|
|
360 |
|
|
|
361 |
// first element
|
|
|
362 |
if ($index === 0 || $index === '0' || $index === 'FIRST') {
|
|
|
363 |
$pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT);
|
|
|
364 |
if ($pos === false) {
|
|
|
365 |
$result = (int) $this->raw_data;
|
|
|
366 |
}
|
|
|
367 |
else {
|
|
|
368 |
$result = (int) substr($this->raw_data, 0, $pos);
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
return $result;
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
// last element
|
|
|
375 |
if ($index === 'LAST' || $index == $count-1) {
|
|
|
376 |
$pos = strrpos($this->raw_data, self::SEPARATOR_ELEMENT);
|
|
|
377 |
if ($pos === false) {
|
|
|
378 |
$result = (int) $this->raw_data;
|
|
|
379 |
}
|
|
|
380 |
else {
|
|
|
381 |
$result = (int) substr($this->raw_data, $pos);
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
return $result;
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
// do we know the position of the element or the neighbour of it?
|
|
|
388 |
if (!empty($this->meta['pos'])) {
|
|
|
389 |
if (isset($this->meta['pos'][$index])) {
|
|
|
390 |
$pos = $this->meta['pos'][$index];
|
|
|
391 |
}
|
|
|
392 |
else if (isset($this->meta['pos'][$index-1])) {
|
|
|
393 |
$pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT,
|
|
|
394 |
$this->meta['pos'][$index-1] + 1);
|
|
|
395 |
}
|
|
|
396 |
else if (isset($this->meta['pos'][$index+1])) {
|
|
|
397 |
$pos = strrpos($this->raw_data, self::SEPARATOR_ELEMENT,
|
|
|
398 |
$this->meta['pos'][$index+1] - $this->length() - 1);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
if (isset($pos) && preg_match('/([0-9]+)/', $this->raw_data, $m, 0, $pos)) {
|
|
|
402 |
return (int) $m[1];
|
|
|
403 |
}
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
// Finally use less effective method
|
|
|
407 |
$data = explode(self::SEPARATOR_ELEMENT, $this->raw_data);
|
|
|
408 |
|
|
|
409 |
return (int) $data[$index];
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
/**
|
|
|
413 |
* Returns response parameters, e.g. ESEARCH's MIN/MAX/COUNT/ALL/MODSEQ
|
|
|
414 |
* or internal data e.g. MAILBOX, ORDER
|
|
|
415 |
*
|
|
|
416 |
* @param ?string $param Parameter name
|
|
|
417 |
*
|
|
|
418 |
* @return array|string Response parameters or parameter value
|
|
|
419 |
*/
|
|
|
420 |
public function get_parameters($param = null)
|
|
|
421 |
{
|
|
|
422 |
$params = $this->params;
|
|
|
423 |
$params['MAILBOX'] = $this->mailbox;
|
|
|
424 |
$params['ORDER'] = $this->order;
|
|
|
425 |
|
|
|
426 |
if ($param !== null) {
|
|
|
427 |
return $params[$param] ?? null;
|
|
|
428 |
}
|
|
|
429 |
|
|
|
430 |
return $params;
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
/**
|
|
|
434 |
* Returns length of internal data representation
|
|
|
435 |
*
|
|
|
436 |
* @return int Data length
|
|
|
437 |
*/
|
|
|
438 |
protected function length()
|
|
|
439 |
{
|
|
|
440 |
if (!isset($this->meta['length'])) {
|
|
|
441 |
$this->meta['length'] = strlen($this->raw_data);
|
|
|
442 |
}
|
|
|
443 |
|
|
|
444 |
return $this->meta['length'];
|
|
|
445 |
}
|
|
|
446 |
}
|