Mercurial > hg > rc2
annotate program/lib/Roundcube/rcube_result_index.php @ 28:6e8e57ef139f default tip
merge
| author | Charlie Root |
|---|---|
| date | Sun, 18 Jan 2026 14:25:54 -0500 |
| parents | 303b85d5b561 |
| children |
| rev | line source |
|---|---|
| 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 | SORT/SEARCH/ESEARCH 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 SORT/SEARCH/ESEARCH result | |
| 23 * | |
| 24 * @package Framework | |
| 25 * @subpackage Storage | |
| 26 */ | |
| 27 class rcube_result_index | |
| 28 { | |
| 29 public $incomplete = false; | |
| 22 | 30 public $multi; # HST: is this ever set anywhere??? |
| 0 | 31 |
| 32 protected $raw_data; | |
| 33 protected $mailbox; | |
| 34 protected $meta = array(); | |
| 35 protected $params = array(); | |
| 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 = array(); | |
| 5 | 59 #rcube::write_log('mail',"rri: ".print_r($data,true)); |
| 0 | 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, array('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 } | |
| 150 | |
| 151 /** | |
| 152 * Returns number of elements in the result | |
| 153 * | |
| 154 * @return int Number of elements | |
| 155 */ | |
| 156 public function count() | |
| 157 { | |
|
17
dd5ed6ef69c9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
5
diff
changeset
|
158 if (($this->meta['count']??null) !== null) |
| 0 | 159 return $this->meta['count']; |
| 160 | |
| 161 if (empty($this->raw_data)) { | |
| 162 $this->meta['count'] = 0; | |
| 163 $this->meta['length'] = 0; | |
| 164 } | |
| 165 else { | |
| 166 $this->meta['count'] = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT); | |
| 167 } | |
| 168 | |
| 169 return $this->meta['count']; | |
| 170 } | |
| 171 | |
| 172 /** | |
| 173 * Returns number of elements in the result. | |
| 174 * Alias for count() for compatibility with rcube_result_thread | |
| 175 * | |
| 176 * @return int Number of elements | |
| 177 */ | |
| 178 public function count_messages() | |
| 179 { | |
| 180 return $this->count(); | |
| 181 } | |
| 182 | |
| 183 /** | |
| 184 * Returns maximal message identifier in the result | |
| 185 * | |
| 186 * @return int Maximal message identifier | |
| 187 */ | |
| 188 public function max() | |
| 189 { | |
| 190 if (!isset($this->meta['max'])) { | |
| 191 $this->meta['max'] = (int) @max($this->get()); | |
| 192 } | |
| 193 | |
| 194 return $this->meta['max']; | |
| 195 } | |
| 196 | |
| 197 /** | |
| 198 * Returns minimal message identifier in the result | |
| 199 * | |
| 200 * @return int Minimal message identifier | |
| 201 */ | |
| 202 public function min() | |
| 203 { | |
| 204 if (!isset($this->meta['min'])) { | |
| 205 $this->meta['min'] = (int) @min($this->get()); | |
| 206 } | |
| 207 | |
| 208 return $this->meta['min']; | |
| 209 } | |
| 210 | |
| 211 /** | |
| 212 * Slices data set. | |
| 213 * | |
| 214 * @param $offset Offset (as for PHP's array_slice()) | |
| 215 * @param $length Number of elements (as for PHP's array_slice()) | |
| 216 */ | |
| 217 public function slice($offset, $length) | |
| 218 { | |
| 219 $data = $this->get(); | |
| 220 $data = array_slice($data, $offset, $length); | |
| 221 | |
| 222 $this->meta = array(); | |
| 223 $this->meta['count'] = count($data); | |
| 224 $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data); | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * Filters data set. Removes elements not listed in $ids list. | |
| 229 * | |
| 230 * @param array $ids List of IDs to remove. | |
| 231 */ | |
| 232 public function filter($ids = array()) | |
| 233 { | |
| 234 $data = $this->get(); | |
| 235 $data = array_intersect($data, $ids); | |
| 236 | |
| 237 $this->meta = array(); | |
| 238 $this->meta['count'] = count($data); | |
| 239 $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data); | |
| 240 } | |
| 241 | |
| 242 /** | |
| 243 * Reverts order of elements in the result | |
| 244 */ | |
| 245 public function revert() | |
| 246 { | |
| 247 $this->order = $this->order == 'ASC' ? 'DESC' : 'ASC'; | |
| 248 | |
| 249 if (empty($this->raw_data)) { | |
| 250 return; | |
| 251 } | |
| 252 | |
| 253 $data = $this->get(); | |
| 254 $data = array_reverse($data); | |
| 255 $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data); | |
| 256 | |
| 257 $this->meta['pos'] = array(); | |
| 258 } | |
| 259 | |
| 260 /** | |
| 261 * Check if the given message ID exists in the object | |
| 262 * | |
| 263 * @param int $msgid Message ID | |
| 264 * @param bool $get_index When enabled element's index will be returned. | |
| 265 * Elements are indexed starting with 0 | |
| 266 * | |
| 267 * @return mixed False if message ID doesn't exist, True if exists or | |
| 268 * index of the element if $get_index=true | |
| 269 */ | |
| 270 public function exists($msgid, $get_index = false) | |
| 271 { | |
| 272 if (empty($this->raw_data)) { | |
| 273 return false; | |
| 274 } | |
| 275 | |
| 276 $msgid = (int) $msgid; | |
| 277 $begin = implode('|', array('^', preg_quote(self::SEPARATOR_ELEMENT, '/'))); | |
| 278 $end = implode('|', array('$', preg_quote(self::SEPARATOR_ELEMENT, '/'))); | |
| 279 | |
| 280 if (preg_match("/($begin)$msgid($end)/", $this->raw_data, $m, | |
| 281 $get_index ? PREG_OFFSET_CAPTURE : null) | |
| 282 ) { | |
| 283 if ($get_index) { | |
| 284 $idx = 0; | |
| 285 if ($m[0][1]) { | |
| 286 $idx = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT, 0, $m[0][1]); | |
| 287 } | |
| 288 // cache position of this element, so we can use it in get_element() | |
| 289 $this->meta['pos'][$idx] = (int)$m[0][1]; | |
| 290 | |
| 291 return $idx; | |
| 292 } | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 return false; | |
| 297 } | |
| 298 | |
| 299 /** | |
| 300 * Return all messages in the result. | |
| 301 * | |
| 302 * @return array List of message IDs | |
| 303 */ | |
| 304 public function get() | |
| 305 { | |
| 306 if (empty($this->raw_data)) { | |
| 307 return array(); | |
| 308 } | |
| 309 | |
| 310 return explode(self::SEPARATOR_ELEMENT, $this->raw_data); | |
| 311 } | |
| 312 | |
| 313 /** | |
| 314 * Return all messages in the result. | |
| 315 * | |
| 316 * @return array List of message IDs | |
| 317 */ | |
| 318 public function get_compressed() | |
| 319 { | |
| 320 if (empty($this->raw_data)) { | |
| 321 return ''; | |
| 322 } | |
| 323 | |
| 324 return rcube_imap_generic::compressMessageSet($this->get()); | |
| 325 } | |
| 326 | |
| 327 /** | |
| 328 * Return result element at specified index | |
| 329 * | |
| 330 * @param int|string $index Element's index or "FIRST" or "LAST" | |
| 331 * | |
| 332 * @return int Element value | |
| 333 */ | |
| 334 public function get_element($index) | |
| 335 { | |
| 336 $count = $this->count(); | |
| 337 | |
| 338 if (!$count) { | |
| 339 return null; | |
| 340 } | |
| 341 | |
| 342 // first element | |
| 343 if ($index === 0 || $index === '0' || $index === 'FIRST') { | |
| 344 $pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT); | |
| 345 if ($pos === false) | |
| 346 $result = (int) $this->raw_data; | |
| 347 else | |
| 348 $result = (int) substr($this->raw_data, 0, $pos); | |
| 349 | |
| 350 return $result; | |
| 351 } | |
| 352 | |
| 353 // last element | |
| 354 if ($index === 'LAST' || $index == $count-1) { | |
| 355 $pos = strrpos($this->raw_data, self::SEPARATOR_ELEMENT); | |
| 356 if ($pos === false) | |
| 357 $result = (int) $this->raw_data; | |
| 358 else | |
| 359 $result = (int) substr($this->raw_data, $pos); | |
| 360 | |
| 361 return $result; | |
| 362 } | |
| 363 | |
| 364 // do we know the position of the element or the neighbour of it? | |
| 365 if (!empty($this->meta['pos'])) { | |
| 366 if (isset($this->meta['pos'][$index])) | |
| 367 $pos = $this->meta['pos'][$index]; | |
| 368 else if (isset($this->meta['pos'][$index-1])) | |
| 369 $pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT, | |
| 370 $this->meta['pos'][$index-1] + 1); | |
| 371 else if (isset($this->meta['pos'][$index+1])) | |
| 372 $pos = strrpos($this->raw_data, self::SEPARATOR_ELEMENT, | |
| 373 $this->meta['pos'][$index+1] - $this->length() - 1); | |
| 374 | |
| 375 if (isset($pos) && preg_match('/([0-9]+)/', $this->raw_data, $m, null, $pos)) { | |
| 376 return (int) $m[1]; | |
| 377 } | |
| 378 } | |
| 379 | |
| 380 // Finally use less effective method | |
| 381 $data = explode(self::SEPARATOR_ELEMENT, $this->raw_data); | |
| 382 | |
| 383 return $data[$index]; | |
| 384 } | |
| 385 | |
| 386 /** | |
| 387 * Returns response parameters, e.g. ESEARCH's MIN/MAX/COUNT/ALL/MODSEQ | |
| 388 * or internal data e.g. MAILBOX, ORDER | |
| 389 * | |
| 390 * @param string $param Parameter name | |
| 391 * | |
| 392 * @return array|string Response parameters or parameter value | |
| 393 */ | |
| 394 public function get_parameters($param=null) | |
| 395 { | |
| 396 $params = $this->params; | |
| 397 $params['MAILBOX'] = $this->mailbox; | |
| 398 $params['ORDER'] = $this->order; | |
| 399 | |
| 400 if ($param !== null) { | |
| 401 return $params[$param]; | |
| 402 } | |
| 403 | |
| 404 return $params; | |
| 405 } | |
| 406 | |
| 407 /** | |
| 408 * Returns length of internal data representation | |
| 409 * | |
| 410 * @return int Data length | |
| 411 */ | |
| 412 protected function length() | |
| 413 { | |
| 414 if (!isset($this->meta['length'])) { | |
| 415 $this->meta['length'] = strlen($this->raw_data); | |
| 416 } | |
| 417 | |
| 418 return $this->meta['length']; | |
| 419 } | |
| 420 } |
