Mercurial > hg > rc2
annotate program/lib/Roundcube/rcube_imap.php @ 24:e53add5ddac5
More cleaning up php8 Warnings/deprecations
| author | Charlie Root |
|---|---|
| date | Fri, 17 Oct 2025 17:31:22 -0400 |
| parents | 303b85d5b561 |
| children | c32b53434b47 |
| rev | line source |
|---|---|
| 0 | 1 <?php |
| 2 | |
| 3 /** | |
| 4 +-----------------------------------------------------------------------+ | |
| 5 | This file is part of the Roundcube Webmail client | | |
| 6 | Copyright (C) 2005-2012, The Roundcube Dev Team | | |
| 7 | Copyright (C) 2011-2012, 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 | IMAP Storage Engine | | |
| 15 +-----------------------------------------------------------------------+ | |
| 16 | Author: Thomas Bruederli <roundcube@gmail.com> | | |
| 17 | Author: Aleksander Machniak <alec@alec.pl> | | |
| 18 +-----------------------------------------------------------------------+ | |
| 19 */ | |
| 20 | |
| 21 /** | |
| 22 * Interface class for accessing an IMAP server | |
| 23 * | |
| 24 * @package Framework | |
| 25 * @subpackage Storage | |
| 26 * @author Thomas Bruederli <roundcube@gmail.com> | |
| 27 * @author Aleksander Machniak <alec@alec.pl> | |
| 28 */ | |
| 29 class rcube_imap extends rcube_storage | |
| 30 { | |
| 31 /** | |
| 32 * Instance of rcube_imap_generic | |
| 33 * | |
| 34 * @var rcube_imap_generic | |
| 35 */ | |
| 36 public $conn; | |
| 37 | |
| 38 /** | |
| 39 * Instance of rcube_imap_cache | |
| 40 * | |
| 41 * @var rcube_imap_cache | |
| 42 */ | |
| 43 protected $mcache; | |
| 44 | |
| 45 /** | |
| 46 * Instance of rcube_cache | |
| 47 * | |
| 48 * @var rcube_cache | |
| 49 */ | |
| 50 protected $cache; | |
| 51 | |
| 52 /** | |
| 53 * Internal (in-memory) cache | |
| 54 * | |
| 55 * @var array | |
| 56 */ | |
| 57 protected $icache = array(); | |
| 58 | |
| 59 protected $plugins; | |
| 21 | 60 protected $parts; |
| 0 | 61 protected $delimiter; |
| 62 protected $namespace; | |
| 63 protected $sort_field = ''; | |
| 64 protected $sort_order = 'DESC'; | |
| 65 protected $struct_charset; | |
| 66 protected $search_set; | |
| 67 protected $search_string = ''; | |
| 68 protected $search_charset = ''; | |
| 69 protected $search_sort_field = ''; | |
| 70 protected $search_threads = false; | |
| 71 protected $search_sorted = false; | |
| 72 protected $options = array('auth_type' => 'check'); | |
| 73 protected $caching = false; | |
| 74 protected $messages_caching = false; | |
| 75 protected $threading = false; | |
|
15
85a746e95663
slowly working through deprecations/warnings from 8.3
Charlie Root
parents:
5
diff
changeset
|
76 protected $connect_done; |
|
85a746e95663
slowly working through deprecations/warnings from 8.3
Charlie Root
parents:
5
diff
changeset
|
77 protected $sort_folder_collator; |
| 19 | 78 protected $msg_uid; |
| 0 | 79 |
| 80 /** | |
| 81 * Object constructor. | |
| 82 */ | |
| 83 public function __construct() | |
| 84 { | |
| 85 $this->conn = new rcube_imap_generic(); | |
| 86 $this->plugins = rcube::get_instance()->plugins; | |
| 87 | |
| 88 // Set namespace and delimiter from session, | |
| 89 // so some methods would work before connection | |
| 90 if (isset($_SESSION['imap_namespace'])) { | |
| 91 $this->namespace = $_SESSION['imap_namespace']; | |
| 92 } | |
| 93 if (isset($_SESSION['imap_delimiter'])) { | |
| 94 $this->delimiter = $_SESSION['imap_delimiter']; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 /** | |
| 99 * Magic getter for backward compat. | |
| 100 * | |
| 101 * @deprecated. | |
| 102 */ | |
| 103 public function __get($name) | |
| 104 { | |
| 105 if (isset($this->{$name})) { | |
| 106 return $this->{$name}; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Connect to an IMAP server | |
| 112 * | |
| 113 * @param string $host Host to connect | |
| 114 * @param string $user Username for IMAP account | |
| 115 * @param string $pass Password for IMAP account | |
| 116 * @param integer $port Port to connect to | |
| 117 * @param string $use_ssl SSL schema (either ssl or tls) or null if plain connection | |
| 118 * | |
| 119 * @return boolean True on success, False on failure | |
| 120 */ | |
| 121 public function connect($host, $user, $pass, $port=143, $use_ssl=null) | |
| 122 { | |
| 123 // check for OpenSSL support in PHP build | |
| 124 if ($use_ssl && extension_loaded('openssl')) { | |
| 125 $this->options['ssl_mode'] = $use_ssl == 'imaps' ? 'ssl' : $use_ssl; | |
| 126 } | |
| 127 else if ($use_ssl) { | |
| 128 rcube::raise_error(array('code' => 403, 'type' => 'imap', | |
| 129 'file' => __FILE__, 'line' => __LINE__, | |
| 130 'message' => "OpenSSL not available"), true, false); | |
| 131 $port = 143; | |
| 132 } | |
| 133 | |
| 134 $this->options['port'] = $port; | |
| 135 | |
| 136 if ($this->options['debug']) { | |
| 137 $this->set_debug(true); | |
| 138 | |
| 139 $this->options['ident'] = array( | |
| 140 'name' => 'Roundcube', | |
| 141 'version' => RCUBE_VERSION, | |
| 142 'php' => PHP_VERSION, | |
| 143 'os' => PHP_OS, | |
| 144 'command' => $_SERVER['REQUEST_URI'], | |
| 145 ); | |
| 146 } | |
| 147 | |
| 148 $attempt = 0; | |
| 149 do { | |
| 150 $data = $this->plugins->exec_hook('storage_connect', | |
| 151 array_merge($this->options, array('host' => $host, 'user' => $user, | |
| 152 'attempt' => ++$attempt))); | |
| 153 | |
| 154 if (!empty($data['pass'])) { | |
| 155 $pass = $data['pass']; | |
| 156 } | |
| 157 | |
| 158 // Handle per-host socket options | |
| 159 rcube_utils::parse_socket_options($data['socket_options'], $data['host']); | |
| 160 | |
| 161 $this->conn->connect($data['host'], $data['user'], $pass, $data); | |
| 162 } while(!$this->conn->connected() && $data['retry']); | |
| 163 | |
| 164 $config = array( | |
| 165 'host' => $data['host'], | |
| 166 'user' => $data['user'], | |
| 167 'password' => $pass, | |
| 168 'port' => $port, | |
| 169 'ssl' => $use_ssl, | |
| 170 ); | |
| 171 | |
| 172 $this->options = array_merge($this->options, $config); | |
| 173 $this->connect_done = true; | |
| 174 | |
| 175 if ($this->conn->connected()) { | |
| 176 // check for session identifier | |
| 177 $session = null; | |
| 178 if (preg_match('/\s+SESSIONID=([^=\s]+)/', $this->conn->result, $m)) { | |
| 179 $session = $m[1]; | |
| 180 } | |
| 181 | |
| 182 // get namespace and delimiter | |
| 183 $this->set_env(); | |
| 184 | |
| 185 // trigger post-connect hook | |
| 186 $this->plugins->exec_hook('storage_connected', array( | |
| 187 'host' => $host, 'user' => $user, 'session' => $session | |
| 188 )); | |
| 189 | |
| 190 return true; | |
| 191 } | |
| 192 // write error log | |
| 193 else if ($this->conn->error) { | |
| 194 if ($pass && $user) { | |
| 195 $message = sprintf("Login failed for %s from %s. %s", | |
| 196 $user, rcube_utils::remote_ip(), $this->conn->error); | |
| 197 | |
| 198 rcube::raise_error(array('code' => 403, 'type' => 'imap', | |
| 199 'file' => __FILE__, 'line' => __LINE__, | |
| 200 'message' => $message), true, false); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 return false; | |
| 205 } | |
| 206 | |
| 207 /** | |
| 208 * Close IMAP connection. | |
| 209 * Usually done on script shutdown | |
| 210 */ | |
| 211 public function close() | |
| 212 { | |
| 213 $this->connect_done = false; | |
| 214 $this->conn->closeConnection(); | |
| 215 | |
| 216 if ($this->mcache) { | |
| 217 $this->mcache->close(); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 /** | |
| 222 * Check connection state, connect if not connected. | |
| 223 * | |
| 224 * @return bool Connection state. | |
| 225 */ | |
| 226 public function check_connection() | |
| 227 { | |
| 228 // Establish connection if it wasn't done yet | |
| 229 if (!$this->connect_done && !empty($this->options['user'])) { | |
| 230 return $this->connect( | |
| 231 $this->options['host'], | |
| 232 $this->options['user'], | |
| 233 $this->options['password'], | |
| 234 $this->options['port'], | |
| 235 $this->options['ssl'] | |
| 236 ); | |
| 237 } | |
| 238 | |
| 239 return $this->is_connected(); | |
| 240 } | |
| 241 | |
| 242 /** | |
| 243 * Checks IMAP connection. | |
| 244 * | |
| 245 * @return boolean TRUE on success, FALSE on failure | |
| 246 */ | |
| 247 public function is_connected() | |
| 248 { | |
| 249 return $this->conn->connected(); | |
| 250 } | |
| 251 | |
| 252 /** | |
| 253 * Returns code of last error | |
| 254 * | |
| 255 * @return int Error code | |
| 256 */ | |
| 257 public function get_error_code() | |
| 258 { | |
| 259 return $this->conn->errornum; | |
| 260 } | |
| 261 | |
| 262 /** | |
| 263 * Returns text of last error | |
| 264 * | |
| 265 * @return string Error string | |
| 266 */ | |
| 267 public function get_error_str() | |
| 268 { | |
| 269 return $this->conn->error; | |
| 270 } | |
| 271 | |
| 272 /** | |
| 273 * Returns code of last command response | |
| 274 * | |
| 275 * @return int Response code | |
| 276 */ | |
| 277 public function get_response_code() | |
| 278 { | |
| 279 switch ($this->conn->resultcode) { | |
| 280 case 'NOPERM': | |
| 281 return self::NOPERM; | |
| 282 case 'READ-ONLY': | |
| 283 return self::READONLY; | |
| 284 case 'TRYCREATE': | |
| 285 return self::TRYCREATE; | |
| 286 case 'INUSE': | |
| 287 return self::INUSE; | |
| 288 case 'OVERQUOTA': | |
| 289 return self::OVERQUOTA; | |
| 290 case 'ALREADYEXISTS': | |
| 291 return self::ALREADYEXISTS; | |
| 292 case 'NONEXISTENT': | |
| 293 return self::NONEXISTENT; | |
| 294 case 'CONTACTADMIN': | |
| 295 return self::CONTACTADMIN; | |
| 296 default: | |
| 297 return self::UNKNOWN; | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 /** | |
| 302 * Activate/deactivate debug mode | |
| 303 * | |
| 304 * @param boolean $dbg True if IMAP conversation should be logged | |
| 305 */ | |
| 306 public function set_debug($dbg = true) | |
| 307 { | |
| 308 $this->options['debug'] = $dbg; | |
| 309 $this->conn->setDebug($dbg, array($this, 'debug_handler')); | |
| 310 } | |
| 311 | |
| 312 /** | |
| 313 * Set internal folder reference. | |
| 314 * All operations will be performed on this folder. | |
| 315 * | |
| 316 * @param string $folder Folder name | |
| 317 */ | |
| 318 public function set_folder($folder) | |
| 319 { | |
| 320 $this->folder = $folder; | |
| 321 } | |
| 322 | |
| 323 /** | |
| 324 * Save a search result for future message listing methods | |
| 325 * | |
| 326 * @param array $set Search set, result from rcube_imap::get_search_set(): | |
| 327 * 0 - searching criteria, string | |
| 328 * 1 - search result, rcube_result_index|rcube_result_thread | |
| 329 * 2 - searching character set, string | |
| 330 * 3 - sorting field, string | |
| 331 * 4 - true if sorted, bool | |
| 332 */ | |
| 333 public function set_search_set($set) | |
| 334 { | |
| 22 | 335 $set = (array) $set; |
| 336 | |
| 337 $this->search_string = $set[0] ?? null; | |
| 338 $this->search_set = $set[1] ?? null; | |
| 339 $this->search_charset = $set[2] ?? null; | |
| 340 $this->search_sort_field = $set[3] ?? null; | |
| 341 $this->search_sorted = $set[4] ?? null; | |
| 342 $this->search_threads = is_a($this->search_set, 'rcube_result_thread'); | |
| 343 | |
| 344 if (is_a($this->search_set, 'rcube_result_multifolder')) { | |
| 345 $this->set_threading(false); | |
| 346 } | |
| 0 | 347 } |
| 348 | |
| 349 /** | |
| 350 * Return the saved search set as hash array | |
| 351 * | |
| 352 * @return array Search set | |
| 353 */ | |
| 354 public function get_search_set() | |
| 355 { | |
| 356 if (empty($this->search_set)) { | |
| 357 return null; | |
| 358 } | |
| 359 | |
| 360 return array( | |
| 361 $this->search_string, | |
| 362 $this->search_set, | |
| 363 $this->search_charset, | |
| 364 $this->search_sort_field, | |
| 365 $this->search_sorted, | |
| 366 ); | |
| 367 } | |
| 368 | |
| 369 /** | |
| 370 * Returns the IMAP server's capability. | |
| 371 * | |
| 372 * @param string $cap Capability name | |
| 373 * | |
| 374 * @return mixed Capability value or TRUE if supported, FALSE if not | |
| 375 */ | |
| 376 public function get_capability($cap) | |
| 377 { | |
| 378 $cap = strtoupper($cap); | |
| 379 $sess_key = "STORAGE_$cap"; | |
| 380 | |
| 381 if (!isset($_SESSION[$sess_key])) { | |
| 382 if (!$this->check_connection()) { | |
| 383 return false; | |
| 384 } | |
| 385 | |
| 386 $_SESSION[$sess_key] = $this->conn->getCapability($cap); | |
| 387 } | |
| 388 | |
| 389 return $_SESSION[$sess_key]; | |
| 390 } | |
| 391 | |
| 392 /** | |
| 393 * Checks the PERMANENTFLAGS capability of the current folder | |
| 394 * and returns true if the given flag is supported by the IMAP server | |
| 395 * | |
| 396 * @param string $flag Permanentflag name | |
| 397 * | |
| 398 * @return boolean True if this flag is supported | |
| 399 */ | |
| 400 public function check_permflag($flag) | |
| 401 { | |
| 402 $flag = strtoupper($flag); | |
| 403 $perm_flags = $this->get_permflags($this->folder); | |
| 404 $imap_flag = $this->conn->flags[$flag]; | |
| 405 | |
| 406 return $imap_flag && !empty($perm_flags) && in_array_nocase($imap_flag, $perm_flags); | |
| 407 } | |
| 408 | |
| 409 /** | |
| 410 * Returns PERMANENTFLAGS of the specified folder | |
| 411 * | |
| 412 * @param string $folder Folder name | |
| 413 * | |
| 414 * @return array Flags | |
| 415 */ | |
| 416 public function get_permflags($folder) | |
| 417 { | |
| 418 if (!strlen($folder)) { | |
| 419 return array(); | |
| 420 } | |
| 421 | |
| 422 if (!$this->check_connection()) { | |
| 423 return array(); | |
| 424 } | |
| 425 | |
| 426 if ($this->conn->select($folder)) { | |
| 427 $permflags = $this->conn->data['PERMANENTFLAGS']; | |
| 428 } | |
| 429 else { | |
| 430 return array(); | |
| 431 } | |
| 432 | |
| 433 if (!is_array($permflags)) { | |
| 434 $permflags = array(); | |
| 435 } | |
| 436 | |
| 437 return $permflags; | |
| 438 } | |
| 439 | |
| 440 /** | |
| 441 * Returns the delimiter that is used by the IMAP server for folder separation | |
| 442 * | |
| 443 * @return string Delimiter string | |
| 444 */ | |
| 445 public function get_hierarchy_delimiter() | |
| 446 { | |
| 447 return $this->delimiter; | |
| 448 } | |
| 449 | |
| 450 /** | |
| 451 * Get namespace | |
| 452 * | |
| 453 * @param string $name Namespace array index: personal, other, shared, prefix | |
| 454 * | |
| 455 * @return array Namespace data | |
| 456 */ | |
| 457 public function get_namespace($name = null) | |
| 458 { | |
| 459 $ns = $this->namespace; | |
| 460 | |
| 461 if ($name) { | |
| 462 // an alias for BC | |
| 463 if ($name == 'prefix') { | |
| 464 $name = 'prefix_in'; | |
| 465 } | |
| 466 | |
| 467 return isset($ns[$name]) ? $ns[$name] : null; | |
| 468 } | |
| 469 | |
| 470 unset($ns['prefix_in'], $ns['prefix_out']); | |
| 471 | |
| 472 return $ns; | |
| 473 } | |
| 474 | |
| 475 /** | |
| 476 * Sets delimiter and namespaces | |
| 477 */ | |
| 478 protected function set_env() | |
| 479 { | |
| 480 if ($this->delimiter !== null && $this->namespace !== null) { | |
| 481 return; | |
| 482 } | |
| 483 | |
| 484 $config = rcube::get_instance()->config; | |
| 485 $imap_personal = $config->get('imap_ns_personal'); | |
| 486 $imap_other = $config->get('imap_ns_other'); | |
| 487 $imap_shared = $config->get('imap_ns_shared'); | |
| 488 $imap_delimiter = $config->get('imap_delimiter'); | |
| 489 | |
| 490 if (!$this->check_connection()) { | |
| 491 return; | |
| 492 } | |
| 493 | |
| 494 $ns = $this->conn->getNamespace(); | |
| 495 | |
| 496 // Set namespaces (NAMESPACE supported) | |
| 497 if (is_array($ns)) { | |
| 498 $this->namespace = $ns; | |
| 499 } | |
| 500 else { | |
| 501 $this->namespace = array( | |
| 502 'personal' => NULL, | |
| 503 'other' => NULL, | |
| 504 'shared' => NULL, | |
| 505 ); | |
| 506 } | |
| 507 | |
| 508 if ($imap_delimiter) { | |
| 509 $this->delimiter = $imap_delimiter; | |
| 510 } | |
| 511 if (empty($this->delimiter)) { | |
| 512 $this->delimiter = $this->namespace['personal'][0][1]; | |
| 513 } | |
| 514 if (empty($this->delimiter)) { | |
| 515 $this->delimiter = $this->conn->getHierarchyDelimiter(); | |
| 516 } | |
| 517 if (empty($this->delimiter)) { | |
| 518 $this->delimiter = '/'; | |
| 519 } | |
| 520 | |
| 521 // Overwrite namespaces | |
| 522 if ($imap_personal !== null) { | |
| 523 $this->namespace['personal'] = NULL; | |
| 524 foreach ((array)$imap_personal as $dir) { | |
| 525 $this->namespace['personal'][] = array($dir, $this->delimiter); | |
| 526 } | |
| 527 } | |
| 528 if ($imap_other !== null) { | |
| 529 $this->namespace['other'] = NULL; | |
| 530 foreach ((array)$imap_other as $dir) { | |
| 531 if ($dir) { | |
| 532 $this->namespace['other'][] = array($dir, $this->delimiter); | |
| 533 } | |
| 534 } | |
| 535 } | |
| 536 if ($imap_shared !== null) { | |
| 537 $this->namespace['shared'] = NULL; | |
| 538 foreach ((array)$imap_shared as $dir) { | |
| 539 if ($dir) { | |
| 540 $this->namespace['shared'][] = array($dir, $this->delimiter); | |
| 541 } | |
| 542 } | |
| 543 } | |
| 544 | |
| 545 // Find personal namespace prefix(es) for self::mod_folder() | |
| 546 if (is_array($this->namespace['personal']) && !empty($this->namespace['personal'])) { | |
| 547 // There can be more than one namespace root, | |
| 548 // - for prefix_out get the first one but only | |
| 549 // if there is only one root | |
| 550 // - for prefix_in get the first one but only | |
| 551 // if there is no non-prefixed namespace root (#5403) | |
| 552 $roots = array(); | |
| 553 foreach ($this->namespace['personal'] as $ns) { | |
| 554 $roots[] = $ns[0]; | |
| 555 } | |
| 556 | |
| 557 if (!in_array('', $roots)) { | |
| 558 $this->namespace['prefix_in'] = $roots[0]; | |
| 559 } | |
| 560 if (count($roots) == 1) { | |
| 561 $this->namespace['prefix_out'] = $roots[0]; | |
| 562 } | |
| 563 } | |
| 564 | |
| 565 $_SESSION['imap_namespace'] = $this->namespace; | |
| 566 $_SESSION['imap_delimiter'] = $this->delimiter; | |
| 567 } | |
| 568 | |
| 569 /** | |
| 570 * Returns IMAP server vendor name | |
| 571 * | |
| 572 * @return string Vendor name | |
| 573 * @since 1.2 | |
| 574 */ | |
| 575 public function get_vendor() | |
| 576 { | |
| 577 if ($_SESSION['imap_vendor'] !== null) { | |
| 578 return $_SESSION['imap_vendor']; | |
| 579 } | |
| 580 | |
| 581 $config = rcube::get_instance()->config; | |
| 582 $imap_vendor = $config->get('imap_vendor'); | |
| 583 | |
| 584 if ($imap_vendor) { | |
| 585 return $imap_vendor; | |
| 586 } | |
| 587 | |
| 588 if (!$this->check_connection()) { | |
| 589 return; | |
| 590 } | |
| 591 | |
| 592 if (($ident = $this->conn->data['ID']) === null) { | |
| 593 $ident = $this->conn->id(array( | |
| 594 'name' => 'Roundcube', | |
| 595 'version' => RCUBE_VERSION, | |
| 596 'php' => PHP_VERSION, | |
| 597 'os' => PHP_OS, | |
| 598 )); | |
| 599 } | |
| 600 | |
| 601 $vendor = (string) (!empty($ident) ? $ident['name'] : ''); | |
| 602 $ident = strtolower($vendor . ' ' . $this->conn->data['GREETING']); | |
| 603 $vendors = array('cyrus', 'dovecot', 'uw-imap', 'gmail', 'hmail'); | |
| 604 | |
| 605 foreach ($vendors as $v) { | |
| 606 if (strpos($ident, $v) !== false) { | |
| 607 $vendor = $v; | |
| 608 break; | |
| 609 } | |
| 610 } | |
| 611 | |
| 612 return $_SESSION['imap_vendor'] = $vendor; | |
| 613 } | |
| 614 | |
| 615 /** | |
| 616 * Get message count for a specific folder | |
| 617 * | |
| 618 * @param string $folder Folder name | |
| 619 * @param string $mode Mode for count [ALL|THREADS|UNSEEN|RECENT|EXISTS] | |
| 620 * @param boolean $force Force reading from server and update cache | |
| 621 * @param boolean $status Enables storing folder status info (max UID/count), | |
| 622 * required for folder_status() | |
| 623 * | |
| 624 * @return int Number of messages | |
| 625 */ | |
| 626 public function count($folder='', $mode='ALL', $force=false, $status=true) | |
| 627 { | |
| 628 if (!strlen($folder)) { | |
| 629 $folder = $this->folder; | |
| 630 } | |
| 631 | |
| 632 return $this->countmessages($folder, $mode, $force, $status); | |
| 633 } | |
| 634 | |
| 635 /** | |
| 636 * Protected method for getting number of messages | |
| 637 * | |
| 638 * @param string $folder Folder name | |
| 639 * @param string $mode Mode for count [ALL|THREADS|UNSEEN|RECENT|EXISTS] | |
| 640 * @param boolean $force Force reading from server and update cache | |
| 641 * @param boolean $status Enables storing folder status info (max UID/count), | |
| 642 * required for folder_status() | |
| 643 * @param boolean $no_search Ignore current search result | |
| 644 * | |
| 645 * @return int Number of messages | |
| 646 * @see rcube_imap::count() | |
| 647 */ | |
| 648 protected function countmessages($folder, $mode = 'ALL', $force = false, $status = true, $no_search = false) | |
| 649 { | |
| 650 $mode = strtoupper($mode); | |
| 651 | |
| 652 // Count search set, assume search set is always up-to-date (don't check $force flag) | |
| 653 // @TODO: this could be handled in more reliable way, e.g. a separate method | |
| 654 // maybe in rcube_imap_search | |
| 655 if (!$no_search && $this->search_string && $folder == $this->folder) { | |
| 656 if ($mode == 'ALL') { | |
| 657 return $this->search_set->count_messages(); | |
| 658 } | |
| 659 else if ($mode == 'THREADS') { | |
| 660 return $this->search_set->count(); | |
| 661 } | |
| 662 } | |
| 663 | |
| 664 // EXISTS is a special alias for ALL, it allows to get the number | |
| 665 // of all messages in a folder also when search is active and with | |
| 666 // any skip_deleted setting | |
| 667 | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
668 if ($this->caching) { |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
669 $a_folder_cache = $this->get_cache('messagecount'); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
670 |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
671 // return cached value |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
672 if (!$force && is_array($a_folder_cache[$folder]) && isset($a_folder_cache[$folder][$mode])) { |
| 0 | 673 return $a_folder_cache[$folder][$mode]; |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
674 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
675 |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
676 if (!is_array($a_folder_cache[$folder])) { |
| 0 | 677 $a_folder_cache[$folder] = array(); |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
678 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
679 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
680 if ($mode == 'THREADS') { |
| 0 | 681 $res = $this->threads($folder); |
| 682 $count = $res->count(); | |
| 683 | |
| 684 if ($status) { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
685 $msg_count = $res->count_messages(); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
686 $this->set_folder_stats($folder, 'cnt', $msg_count); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
687 $this->set_folder_stats($folder, 'maxuid', $msg_count ? $this->id2uid($msg_count, $folder) : 0); |
| 0 | 688 } |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
689 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
690 // Need connection here |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
691 else if (!$this->check_connection()) { |
| 0 | 692 return 0; |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
693 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
694 // RECENT count is fetched a bit different |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
695 else if ($mode == 'RECENT') { |
| 0 | 696 $count = $this->conn->countRecent($folder); |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
697 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
698 // use SEARCH for message counting |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
699 else if ($mode != 'EXISTS' && !empty($this->options['skip_deleted'])) { |
| 0 | 700 $search_str = "ALL UNDELETED"; |
| 701 $keys = array('COUNT'); | |
| 702 | |
| 703 if ($mode == 'UNSEEN') { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
704 $search_str .= " UNSEEN"; |
| 0 | 705 } |
| 706 else { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
707 if ($this->messages_caching) { |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
708 $keys[] = 'ALL'; |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
709 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
710 if ($status) { |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
711 $keys[] = 'MAX'; |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
712 } |
| 0 | 713 } |
| 714 | |
| 715 // @TODO: if $mode == 'ALL' we could try to use cache index here | |
| 716 | |
| 717 // get message count using (E)SEARCH | |
| 718 // not very performant but more precise (using UNDELETED) | |
| 719 $index = $this->conn->search($folder, $search_str, true, $keys); | |
| 720 $count = $index->count(); | |
| 721 | |
| 722 if ($mode == 'ALL') { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
723 // Cache index data, will be used in index_direct() |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
724 $this->icache['undeleted_idx'] = $index; |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
725 |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
726 if ($status) { |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
727 $this->set_folder_stats($folder, 'cnt', $count); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
728 $this->set_folder_stats($folder, 'maxuid', $index->max()); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
729 } |
| 0 | 730 } |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
731 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
732 else { |
| 0 | 733 if ($mode == 'UNSEEN') { |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
734 $count = $this->conn->countUnseen($folder); |
| 0 | 735 } |
| 736 else { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
737 $count = $this->conn->countMessages($folder); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
738 if ($status && $mode == 'ALL') { |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
739 $this->set_folder_stats($folder, 'cnt', $count); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
740 $this->set_folder_stats($folder, 'maxuid', $count ? $this->id2uid($count, $folder) : 0); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
741 } |
| 0 | 742 } |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
743 } |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
744 |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
745 if ($this->caching) { |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
746 $a_folder_cache[$folder][$mode] = (int)$count; |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
747 |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
748 // write back to cache |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
749 $this->update_cache('messagecount', $a_folder_cache); |
|
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
750 } |
| 0 | 751 |
| 752 return (int)$count; | |
| 753 } | |
| 754 | |
| 755 /** | |
| 756 * Public method for listing message flags | |
| 757 * | |
| 758 * @param string $folder Folder name | |
| 759 * @param array $uids Message UIDs | |
| 760 * @param int $mod_seq Optional MODSEQ value (of last flag update) | |
| 761 * | |
| 762 * @return array Indexed array with message flags | |
| 763 */ | |
| 764 public function list_flags($folder, $uids, $mod_seq = null) | |
| 765 { | |
| 766 if (!strlen($folder)) { | |
| 767 $folder = $this->folder; | |
| 768 } | |
| 769 | |
| 770 if (!$this->check_connection()) { | |
| 771 return array(); | |
| 772 } | |
| 773 | |
| 774 // @TODO: when cache was synchronized in this request | |
| 775 // we might already have asked for flag updates, use it. | |
| 776 | |
| 777 $flags = $this->conn->fetch($folder, $uids, true, array('FLAGS'), $mod_seq); | |
| 778 $result = array(); | |
| 779 | |
| 780 if (!empty($flags)) { | |
| 781 foreach ($flags as $message) { | |
| 782 $result[$message->uid] = $message->flags; | |
| 783 } | |
| 784 } | |
| 785 | |
| 786 return $result; | |
| 787 } | |
| 788 | |
| 789 /** | |
| 790 * Public method for listing headers | |
| 791 * | |
| 792 * @param string $folder Folder name | |
| 793 * @param int $page Current page to list | |
| 794 * @param string $sort_field Header field to sort by | |
| 795 * @param string $sort_order Sort order [ASC|DESC] | |
| 796 * @param int $slice Number of slice items to extract from result array | |
| 797 * | |
| 798 * @return array Indexed array with message header objects | |
| 799 */ | |
| 800 public function list_messages($folder='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0) | |
| 801 { | |
| 802 if (!strlen($folder)) { | |
| 803 $folder = $this->folder; | |
| 804 } | |
| 805 | |
| 806 return $this->_list_messages($folder, $page, $sort_field, $sort_order, $slice); | |
| 807 } | |
| 808 | |
| 809 /** | |
| 810 * protected method for listing message headers | |
| 811 * | |
| 812 * @param string $folder Folder name | |
| 813 * @param int $page Current page to list | |
| 814 * @param string $sort_field Header field to sort by | |
| 815 * @param string $sort_order Sort order [ASC|DESC] | |
| 816 * @param int $slice Number of slice items to extract from result array | |
| 817 * | |
| 818 * @return array Indexed array with message header objects | |
| 819 * @see rcube_imap::list_messages | |
| 820 */ | |
| 821 protected function _list_messages($folder='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0) | |
| 822 { | |
| 5 | 823 #rcube::write_log('mail',"_lm |$folder| $sort_field ".$this->threading); |
| 0 | 824 if (!strlen($folder)) { |
| 825 return array(); | |
| 826 } | |
| 827 | |
| 828 $this->set_sort_order($sort_field, $sort_order); | |
| 829 $page = $page ? $page : $this->list_page; | |
| 830 | |
| 831 // use saved message set | |
| 832 if ($this->search_string) { | |
| 833 return $this->list_search_messages($folder, $page, $slice); | |
| 834 } | |
| 835 | |
| 836 if ($this->threading) { | |
| 837 return $this->list_thread_messages($folder, $page, $slice); | |
| 838 } | |
| 839 | |
| 840 // get UIDs of all messages in the folder, sorted | |
| 841 $index = $this->index($folder, $this->sort_field, $this->sort_order); | |
| 842 | |
| 843 if ($index->is_empty()) { | |
| 844 return array(); | |
| 845 } | |
| 846 | |
| 847 $from = ($page-1) * $this->page_size; | |
| 848 $to = $from + $this->page_size; | |
| 849 | |
| 850 $index->slice($from, $to - $from); | |
| 851 | |
| 852 if ($slice) { | |
| 853 $index->slice(-$slice, $slice); | |
| 854 } | |
| 855 | |
| 856 // fetch reqested messages headers | |
| 857 $a_index = $index->get(); | |
| 858 $a_msg_headers = $this->fetch_headers($folder, $a_index); | |
| 859 | |
| 860 return array_values($a_msg_headers); | |
| 861 } | |
| 862 | |
| 863 /** | |
| 864 * protected method for listing message headers using threads | |
| 865 * | |
| 866 * @param string $folder Folder name | |
| 867 * @param int $page Current page to list | |
| 868 * @param int $slice Number of slice items to extract from result array | |
| 869 * | |
| 870 * @return array Indexed array with message header objects | |
| 871 * @see rcube_imap::list_messages | |
| 872 */ | |
| 873 protected function list_thread_messages($folder, $page, $slice=0) | |
| 874 { | |
| 875 // get all threads (not sorted) | |
| 876 if ($mcache = $this->get_mcache_engine()) { | |
| 877 $threads = $mcache->get_thread($folder); | |
| 878 } | |
| 879 else { | |
| 880 $threads = $this->threads($folder); | |
| 881 } | |
| 882 | |
| 883 return $this->fetch_thread_headers($folder, $threads, $page, $slice); | |
| 884 } | |
| 885 | |
| 886 /** | |
| 887 * Method for fetching threads data | |
| 888 * | |
| 889 * @param string $folder Folder name | |
| 890 * | |
| 891 * @return rcube_imap_thread Thread data object | |
| 892 */ | |
| 893 function threads($folder) | |
| 894 { | |
| 895 if ($mcache = $this->get_mcache_engine()) { | |
| 896 // don't store in self's internal cache, cache has it's own internal cache | |
| 897 return $mcache->get_thread($folder); | |
| 898 } | |
| 899 | |
| 900 if (!empty($this->icache['threads'])) { | |
| 901 if ($this->icache['threads']->get_parameters('MAILBOX') == $folder) { | |
| 902 return $this->icache['threads']; | |
| 903 } | |
| 904 } | |
| 905 | |
| 906 // get all threads | |
| 907 $result = $this->threads_direct($folder); | |
| 908 | |
| 909 // add to internal (fast) cache | |
| 910 return $this->icache['threads'] = $result; | |
| 911 } | |
| 912 | |
| 913 /** | |
| 914 * Method for direct fetching of threads data | |
| 915 * | |
| 916 * @param string $folder Folder name | |
| 917 * | |
| 918 * @return rcube_imap_thread Thread data object | |
| 919 */ | |
| 920 function threads_direct($folder) | |
| 921 { | |
| 922 if (!$this->check_connection()) { | |
| 923 return new rcube_result_thread(); | |
| 924 } | |
| 925 | |
| 926 // get all threads | |
| 927 return $this->conn->thread($folder, $this->threading, | |
| 928 $this->options['skip_deleted'] ? 'UNDELETED' : '', true); | |
| 929 } | |
| 930 | |
| 931 /** | |
| 932 * protected method for fetching threaded messages headers | |
| 933 * | |
| 934 * @param string $folder Folder name | |
| 935 * @param rcube_result_thread $threads Threads data object | |
| 936 * @param int $page List page number | |
| 937 * @param int $slice Number of threads to slice | |
| 938 * | |
| 939 * @return array Messages headers | |
| 940 */ | |
| 941 protected function fetch_thread_headers($folder, $threads, $page, $slice=0) | |
| 942 { | |
| 943 // Sort thread structure | |
| 944 $this->sort_threads($threads); | |
| 945 | |
| 946 $from = ($page-1) * $this->page_size; | |
| 947 $to = $from + $this->page_size; | |
| 948 | |
| 949 $threads->slice($from, $to - $from); | |
| 950 | |
| 951 if ($slice) { | |
| 952 $threads->slice(-$slice, $slice); | |
| 953 } | |
| 954 | |
| 955 // Get UIDs of all messages in all threads | |
| 956 $a_index = $threads->get(); | |
| 957 | |
| 958 // fetch reqested headers from server | |
| 959 $a_msg_headers = $this->fetch_headers($folder, $a_index); | |
| 960 | |
| 961 unset($a_index); | |
| 962 | |
| 963 // Set depth, has_children and unread_children fields in headers | |
| 964 $this->set_thread_flags($a_msg_headers, $threads); | |
| 965 | |
| 966 return array_values($a_msg_headers); | |
| 967 } | |
| 968 | |
| 969 /** | |
| 970 * protected method for setting threaded messages flags: | |
| 971 * depth, has_children, unread_children, flagged_children | |
| 972 * | |
| 973 * @param array $headers Reference to headers array indexed by message UID | |
| 974 * @param rcube_result_thread $threads Threads data object | |
| 975 * | |
| 976 * @return array Message headers array indexed by message UID | |
| 977 */ | |
| 978 protected function set_thread_flags(&$headers, $threads) | |
| 979 { | |
| 980 $parents = array(); | |
| 981 | |
| 982 list ($msg_depth, $msg_children) = $threads->get_thread_data(); | |
| 983 | |
| 984 foreach ($headers as $uid => $header) { | |
| 985 $depth = $msg_depth[$uid]; | |
| 986 $parents = array_slice($parents, 0, $depth); | |
| 987 | |
| 988 if (!empty($parents)) { | |
| 989 $headers[$uid]->parent_uid = end($parents); | |
| 990 if (empty($header->flags['SEEN'])) { | |
| 991 $headers[$parents[0]]->unread_children++; | |
| 992 } | |
| 993 if (!empty($header->flags['FLAGGED'])) { | |
| 994 $headers[$parents[0]]->flagged_children++; | |
| 995 } | |
| 996 } | |
| 997 array_push($parents, $uid); | |
| 998 | |
| 999 $headers[$uid]->depth = $depth; | |
| 1000 $headers[$uid]->has_children = $msg_children[$uid]; | |
| 1001 } | |
| 1002 } | |
| 1003 | |
| 1004 /** | |
| 1005 * protected method for listing a set of message headers (search results) | |
| 1006 * | |
| 1007 * @param string $folder Folder name | |
| 1008 * @param int $page Current page to list | |
| 1009 * @param int $slice Number of slice items to extract from result array | |
| 1010 * | |
| 1011 * @return array Indexed array with message header objects | |
| 1012 */ | |
| 1013 protected function list_search_messages($folder, $page, $slice=0) | |
| 1014 { | |
| 1015 if (!strlen($folder) || empty($this->search_set) || $this->search_set->is_empty()) { | |
| 1016 return array(); | |
| 1017 } | |
| 1018 | |
| 1019 // gather messages from a multi-folder search | |
| 1020 if ($this->search_set->multi) { | |
| 1021 $page_size = $this->page_size; | |
| 1022 $sort_field = $this->sort_field; | |
| 1023 $search_set = $this->search_set; | |
| 1024 | |
| 1025 // prepare paging | |
| 1026 $cnt = $search_set->count(); | |
| 1027 $from = ($page-1) * $page_size; | |
| 1028 $to = $from + $page_size; | |
| 1029 $slice_length = min($page_size, $cnt - $from); | |
| 1030 | |
| 1031 // fetch resultset headers, sort and slice them | |
| 1032 if (!empty($sort_field) && $search_set->get_parameters('SORT') != $sort_field) { | |
| 1033 $this->sort_field = null; | |
| 1034 $this->page_size = 1000; // fetch up to 1000 matching messages per folder | |
| 1035 $this->threading = false; | |
| 1036 | |
| 1037 $a_msg_headers = array(); | |
| 1038 foreach ($search_set->sets as $resultset) { | |
| 1039 if (!$resultset->is_empty()) { | |
| 1040 $this->search_set = $resultset; | |
| 1041 $this->search_threads = $resultset instanceof rcube_result_thread; | |
| 1042 | |
| 1043 $a_headers = $this->list_search_messages($resultset->get_parameters('MAILBOX'), 1); | |
| 1044 $a_msg_headers = array_merge($a_msg_headers, $a_headers); | |
| 1045 unset($a_headers); | |
| 1046 } | |
| 1047 } | |
| 1048 | |
| 1049 // sort headers | |
| 1050 if (!empty($a_msg_headers)) { | |
| 1051 $a_msg_headers = rcube_imap_generic::sortHeaders($a_msg_headers, $sort_field, $this->sort_order); | |
| 1052 } | |
| 1053 | |
| 1054 // store (sorted) message index | |
| 1055 $search_set->set_message_index($a_msg_headers, $sort_field, $this->sort_order); | |
| 1056 | |
| 1057 // only return the requested part of the set | |
| 1058 $a_msg_headers = array_slice(array_values($a_msg_headers), $from, $slice_length); | |
| 1059 } | |
| 1060 else { | |
| 1061 if ($this->sort_order != $search_set->get_parameters('ORDER')) { | |
| 1062 $search_set->revert(); | |
| 1063 } | |
| 1064 | |
| 1065 // slice resultset first... | |
| 1066 $fetch = array(); | |
| 1067 foreach (array_slice($search_set->get(), $from, $slice_length) as $msg_id) { | |
| 1068 list($uid, $folder) = explode('-', $msg_id, 2); | |
| 1069 $fetch[$folder][] = $uid; | |
| 1070 } | |
| 1071 | |
| 1072 // ... and fetch the requested set of headers | |
| 1073 $a_msg_headers = array(); | |
| 1074 foreach ($fetch as $folder => $a_index) { | |
| 1075 $a_msg_headers = array_merge($a_msg_headers, array_values($this->fetch_headers($folder, $a_index))); | |
| 1076 } | |
| 1077 } | |
| 1078 | |
| 1079 if ($slice) { | |
| 1080 $a_msg_headers = array_slice($a_msg_headers, -$slice, $slice); | |
| 1081 } | |
| 1082 | |
| 1083 // restore members | |
| 1084 $this->sort_field = $sort_field; | |
| 1085 $this->page_size = $page_size; | |
| 1086 $this->search_set = $search_set; | |
| 1087 | |
| 1088 return $a_msg_headers; | |
| 1089 } | |
| 1090 | |
| 1091 // use saved messages from searching | |
| 1092 if ($this->threading) { | |
| 1093 return $this->list_search_thread_messages($folder, $page, $slice); | |
| 1094 } | |
| 1095 | |
| 1096 // search set is threaded, we need a new one | |
| 1097 if ($this->search_threads) { | |
| 1098 $this->search('', $this->search_string, $this->search_charset, $this->sort_field); | |
| 1099 } | |
| 1100 | |
| 1101 $index = clone $this->search_set; | |
| 1102 $from = ($page-1) * $this->page_size; | |
| 1103 $to = $from + $this->page_size; | |
| 1104 | |
| 1105 // return empty array if no messages found | |
| 1106 if ($index->is_empty()) { | |
| 1107 return array(); | |
| 1108 } | |
| 1109 | |
| 1110 // quickest method (default sorting) | |
| 1111 if (!$this->search_sort_field && !$this->sort_field) { | |
| 1112 $got_index = true; | |
| 1113 } | |
| 1114 // sorted messages, so we can first slice array and then fetch only wanted headers | |
| 1115 else if ($this->search_sorted) { // SORT searching result | |
| 1116 $got_index = true; | |
| 1117 // reset search set if sorting field has been changed | |
| 1118 if ($this->sort_field && $this->search_sort_field != $this->sort_field) { | |
| 1119 $this->search('', $this->search_string, $this->search_charset, $this->sort_field); | |
| 1120 | |
| 1121 $index = clone $this->search_set; | |
| 1122 | |
| 1123 // return empty array if no messages found | |
| 1124 if ($index->is_empty()) { | |
| 1125 return array(); | |
| 1126 } | |
| 1127 } | |
| 1128 } | |
| 1129 | |
| 1130 if ($got_index) { | |
| 1131 if ($this->sort_order != $index->get_parameters('ORDER')) { | |
| 1132 $index->revert(); | |
| 1133 } | |
| 1134 | |
| 1135 // get messages uids for one page | |
| 1136 $index->slice($from, $to-$from); | |
| 1137 | |
| 1138 if ($slice) { | |
| 1139 $index->slice(-$slice, $slice); | |
| 1140 } | |
| 1141 | |
| 1142 // fetch headers | |
| 1143 $a_index = $index->get(); | |
| 1144 $a_msg_headers = $this->fetch_headers($folder, $a_index); | |
| 1145 | |
| 1146 return array_values($a_msg_headers); | |
| 1147 } | |
| 1148 | |
| 1149 // SEARCH result, need sorting | |
| 1150 $cnt = $index->count(); | |
| 1151 | |
| 1152 // 300: experimantal value for best result | |
| 1153 if (($cnt > 300 && $cnt > $this->page_size) || !$this->sort_field) { | |
| 1154 // use memory less expensive (and quick) method for big result set | |
| 1155 $index = clone $this->index('', $this->sort_field, $this->sort_order); | |
| 1156 // get messages uids for one page... | |
| 1157 $index->slice($from, min($cnt-$from, $this->page_size)); | |
| 1158 | |
| 1159 if ($slice) { | |
| 1160 $index->slice(-$slice, $slice); | |
| 1161 } | |
| 1162 | |
| 1163 // ...and fetch headers | |
| 1164 $a_index = $index->get(); | |
| 1165 $a_msg_headers = $this->fetch_headers($folder, $a_index); | |
| 1166 | |
| 1167 return array_values($a_msg_headers); | |
| 1168 } | |
| 1169 else { | |
| 1170 // for small result set we can fetch all messages headers | |
| 1171 $a_index = $index->get(); | |
| 1172 $a_msg_headers = $this->fetch_headers($folder, $a_index, false); | |
| 1173 | |
| 1174 // return empty array if no messages found | |
| 1175 if (!is_array($a_msg_headers) || empty($a_msg_headers)) { | |
| 1176 return array(); | |
| 1177 } | |
| 1178 | |
| 1179 // if not already sorted | |
| 1180 $a_msg_headers = rcube_imap_generic::sortHeaders( | |
| 1181 $a_msg_headers, $this->sort_field, $this->sort_order); | |
| 1182 | |
| 1183 // only return the requested part of the set | |
| 1184 $slice_length = min($this->page_size, $cnt - ($to > $cnt ? $from : $to)); | |
| 1185 $a_msg_headers = array_slice(array_values($a_msg_headers), $from, $slice_length); | |
| 1186 | |
| 1187 if ($slice) { | |
| 1188 $a_msg_headers = array_slice($a_msg_headers, -$slice, $slice); | |
| 1189 } | |
| 1190 | |
| 1191 return $a_msg_headers; | |
| 1192 } | |
| 1193 } | |
| 1194 | |
| 1195 /** | |
| 1196 * protected method for listing a set of threaded message headers (search results) | |
| 1197 * | |
| 1198 * @param string $folder Folder name | |
| 1199 * @param int $page Current page to list | |
| 1200 * @param int $slice Number of slice items to extract from result array | |
| 1201 * | |
| 1202 * @return array Indexed array with message header objects | |
| 1203 * @see rcube_imap::list_search_messages() | |
| 1204 */ | |
| 1205 protected function list_search_thread_messages($folder, $page, $slice=0) | |
| 1206 { | |
| 1207 // update search_set if previous data was fetched with disabled threading | |
| 1208 if (!$this->search_threads) { | |
| 1209 if ($this->search_set->is_empty()) { | |
| 1210 return array(); | |
| 1211 } | |
| 1212 $this->search('', $this->search_string, $this->search_charset, $this->sort_field); | |
| 1213 } | |
| 1214 | |
| 1215 return $this->fetch_thread_headers($folder, clone $this->search_set, $page, $slice); | |
| 1216 } | |
| 1217 | |
| 1218 /** | |
| 1219 * Fetches messages headers (by UID) | |
| 1220 * | |
| 1221 * @param string $folder Folder name | |
| 1222 * @param array $msgs Message UIDs | |
| 1223 * @param bool $sort Enables result sorting by $msgs | |
| 1224 * @param bool $force Disables cache use | |
| 1225 * | |
| 1226 * @return array Messages headers indexed by UID | |
| 1227 */ | |
| 1228 function fetch_headers($folder, $msgs, $sort = true, $force = false) | |
| 1229 { | |
| 1230 if (empty($msgs)) { | |
| 1231 return array(); | |
| 1232 } | |
| 1233 | |
| 1234 if (!$force && ($mcache = $this->get_mcache_engine())) { | |
| 1235 $headers = $mcache->get_messages($folder, $msgs); | |
| 1236 } | |
| 1237 else if (!$this->check_connection()) { | |
| 1238 return array(); | |
| 1239 } | |
| 1240 else { | |
| 1241 // fetch reqested headers from server | |
| 1242 $headers = $this->conn->fetchHeaders( | |
| 1243 $folder, $msgs, true, false, $this->get_fetch_headers()); | |
| 1244 } | |
| 1245 | |
| 1246 if (empty($headers)) { | |
| 1247 return array(); | |
| 1248 } | |
| 1249 | |
| 1250 foreach ($headers as $h) { | |
| 1251 $h->folder = $folder; | |
| 1252 $a_msg_headers[$h->uid] = $h; | |
| 1253 } | |
| 1254 | |
| 1255 if ($sort) { | |
| 1256 // use this class for message sorting | |
| 1257 $sorter = new rcube_message_header_sorter(); | |
| 1258 $sorter->set_index($msgs); | |
| 1259 $sorter->sort_headers($a_msg_headers); | |
| 1260 } | |
| 1261 | |
| 1262 return $a_msg_headers; | |
| 1263 } | |
| 1264 | |
| 1265 /** | |
| 1266 * Returns current status of a folder (compared to the last time use) | |
| 1267 * | |
| 1268 * We compare the maximum UID to determine the number of | |
| 1269 * new messages because the RECENT flag is not reliable. | |
| 1270 * | |
| 1271 * @param string $folder Folder name | |
| 1272 * @param array $diff Difference data | |
| 1273 * | |
| 1274 * @return int Folder status | |
| 1275 */ | |
| 1276 public function folder_status($folder = null, &$diff = array()) | |
| 1277 { | |
| 1278 if (!strlen($folder)) { | |
| 1279 $folder = $this->folder; | |
| 1280 } | |
| 1281 $old = $this->get_folder_stats($folder); | |
| 1282 | |
| 1283 // refresh message count -> will update | |
| 1284 $this->countmessages($folder, 'ALL', true, true, true); | |
| 1285 | |
| 1286 $result = 0; | |
| 1287 | |
| 1288 if (empty($old)) { | |
| 1289 return $result; | |
| 1290 } | |
| 1291 | |
| 1292 $new = $this->get_folder_stats($folder); | |
| 1293 | |
| 1294 // got new messages | |
| 1295 if ($new['maxuid'] > $old['maxuid']) { | |
| 1296 $result += 1; | |
| 1297 // get new message UIDs range, that can be used for example | |
| 1298 // to get the data of these messages | |
| 1299 $diff['new'] = ($old['maxuid'] + 1 < $new['maxuid'] ? ($old['maxuid']+1).':' : '') . $new['maxuid']; | |
| 1300 } | |
| 1301 // some messages has been deleted | |
| 1302 if ($new['cnt'] < $old['cnt']) { | |
| 1303 $result += 2; | |
| 1304 } | |
| 1305 | |
| 1306 // @TODO: optional checking for messages flags changes (?) | |
| 1307 // @TODO: UIDVALIDITY checking | |
| 1308 | |
| 1309 return $result; | |
| 1310 } | |
| 1311 | |
| 1312 /** | |
| 1313 * Stores folder statistic data in session | |
| 1314 * @TODO: move to separate DB table (cache?) | |
| 1315 * | |
| 1316 * @param string $folder Folder name | |
| 1317 * @param string $name Data name | |
| 1318 * @param mixed $data Data value | |
| 1319 */ | |
| 1320 protected function set_folder_stats($folder, $name, $data) | |
| 1321 { | |
| 1322 $_SESSION['folders'][$folder][$name] = $data; | |
| 1323 } | |
| 1324 | |
| 1325 /** | |
| 1326 * Gets folder statistic data | |
| 1327 * | |
| 1328 * @param string $folder Folder name | |
| 1329 * | |
| 1330 * @return array Stats data | |
| 1331 */ | |
| 1332 protected function get_folder_stats($folder) | |
| 1333 { | |
| 24 | 1334 if (!empty($_SESSION['folders'][$folder])) { |
| 0 | 1335 return (array) $_SESSION['folders'][$folder]; |
| 1336 } | |
| 1337 | |
| 1338 return array(); | |
| 1339 } | |
| 1340 | |
| 1341 /** | |
| 1342 * Return sorted list of message UIDs | |
| 1343 * | |
| 1344 * @param string $folder Folder to get index from | |
| 1345 * @param string $sort_field Sort column | |
| 1346 * @param string $sort_order Sort order [ASC, DESC] | |
| 1347 * @param bool $no_threads Get not threaded index | |
| 1348 * @param bool $no_search Get index not limited to search result (optionally) | |
| 1349 * | |
| 1350 * @return rcube_result_index|rcube_result_thread List of messages (UIDs) | |
| 1351 */ | |
| 1352 public function index($folder = '', $sort_field = NULL, $sort_order = NULL, | |
| 1353 $no_threads = false, $no_search = false | |
| 1354 ) { | |
| 1355 if (!$no_threads && $this->threading) { | |
| 1356 return $this->thread_index($folder, $sort_field, $sort_order); | |
| 1357 } | |
| 1358 | |
| 1359 $this->set_sort_order($sort_field, $sort_order); | |
| 1360 | |
| 1361 if (!strlen($folder)) { | |
| 1362 $folder = $this->folder; | |
| 1363 } | |
| 1364 | |
| 1365 // we have a saved search result, get index from there | |
| 1366 if ($this->search_string) { | |
| 1367 if ($this->search_set->is_empty()) { | |
| 1368 return new rcube_result_index($folder, '* SORT'); | |
| 1369 } | |
| 1370 | |
| 1371 if ($this->search_set instanceof rcube_result_multifolder) { | |
| 1372 $index = $this->search_set; | |
| 1373 $index->folder = $folder; | |
| 1374 // TODO: handle changed sorting | |
| 1375 } | |
| 1376 // search result is an index with the same sorting? | |
| 1377 else if (($this->search_set instanceof rcube_result_index) | |
| 1378 && ((!$this->sort_field && !$this->search_sorted) || | |
| 1379 ($this->search_sorted && $this->search_sort_field == $this->sort_field)) | |
| 1380 ) { | |
| 1381 $index = $this->search_set; | |
| 1382 } | |
| 1383 // $no_search is enabled when we are not interested in | |
| 1384 // fetching index for search result, e.g. to sort | |
| 1385 // threaded search result we can use full mailbox index. | |
| 1386 // This makes possible to use index from cache | |
| 1387 else if (!$no_search) { | |
| 1388 if (!$this->sort_field) { | |
| 1389 // No sorting needed, just build index from the search result | |
| 1390 // @TODO: do we need to sort by UID here? | |
| 1391 $search = $this->search_set->get_compressed(); | |
| 1392 $index = new rcube_result_index($folder, '* ESEARCH ALL ' . $search); | |
| 1393 } | |
| 1394 else { | |
| 1395 $index = $this->index_direct($folder, $this->sort_field, $this->sort_order, $this->search_set); | |
| 1396 } | |
| 1397 } | |
| 1398 | |
| 1399 if (isset($index)) { | |
| 1400 if ($this->sort_order != $index->get_parameters('ORDER')) { | |
| 1401 $index->revert(); | |
| 1402 } | |
| 1403 | |
| 1404 return $index; | |
| 1405 } | |
| 1406 } | |
| 1407 | |
| 5 | 1408 #rcube::write_log('mail','cache? '.$this->get_mcache_engine()); |
| 0 | 1409 // check local cache |
| 1410 if ($mcache = $this->get_mcache_engine()) { | |
| 1411 return $mcache->get_index($folder, $this->sort_field, $this->sort_order); | |
| 1412 } | |
| 1413 | |
| 1414 // fetch from IMAP server | |
| 1415 return $this->index_direct($folder, $this->sort_field, $this->sort_order); | |
| 1416 } | |
| 1417 | |
| 1418 /** | |
| 1419 * Return sorted list of message UIDs ignoring current search settings. | |
| 1420 * Doesn't uses cache by default. | |
| 1421 * | |
| 1422 * @param string $folder Folder to get index from | |
| 1423 * @param string $sort_field Sort column | |
| 1424 * @param string $sort_order Sort order [ASC, DESC] | |
| 1425 * @param rcube_result_* $search Optional messages set to limit the result | |
| 1426 * | |
| 1427 * @return rcube_result_index Sorted list of message UIDs | |
| 1428 */ | |
| 1429 public function index_direct($folder, $sort_field = null, $sort_order = null, $search = null) | |
| 1430 { | |
| 1431 if (!empty($search)) { | |
| 1432 $search = $search->get_compressed(); | |
| 1433 } | |
| 1434 | |
| 1435 // use message index sort as default sorting | |
| 1436 if (!$sort_field) { | |
| 1437 // use search result from count() if possible | |
| 1438 if (empty($search) && $this->options['skip_deleted'] | |
| 1439 && !empty($this->icache['undeleted_idx']) | |
| 1440 && $this->icache['undeleted_idx']->get_parameters('ALL') !== null | |
| 1441 && $this->icache['undeleted_idx']->get_parameters('MAILBOX') == $folder | |
| 1442 ) { | |
| 1443 $index = $this->icache['undeleted_idx']; | |
| 1444 } | |
| 1445 else if (!$this->check_connection()) { | |
| 1446 return new rcube_result_index(); | |
| 1447 } | |
| 1448 else { | |
| 1449 $query = $this->options['skip_deleted'] ? 'UNDELETED' : ''; | |
| 1450 if ($search) { | |
| 1451 $query = trim($query . ' UID ' . $search); | |
| 1452 } | |
| 1453 | |
| 1454 $index = $this->conn->search($folder, $query, true); | |
| 1455 } | |
| 1456 } | |
| 1457 else if (!$this->check_connection()) { | |
| 1458 return new rcube_result_index(); | |
| 1459 } | |
| 1460 // fetch complete message index | |
| 1461 else { | |
| 1462 if ($this->get_capability('SORT')) { | |
| 1463 $query = $this->options['skip_deleted'] ? 'UNDELETED' : ''; | |
| 1464 if ($search) { | |
| 1465 $query = trim($query . ' UID ' . $search); | |
| 1466 } | |
| 5 | 1467 #rcube::write_log('mail',"abouttosort $sort_field $query"); |
| 0 | 1468 $index = $this->conn->sort($folder, $sort_field, $query, true); |
| 1469 } | |
| 1470 | |
| 1471 if (empty($index) || $index->is_error()) { | |
| 1472 $index = $this->conn->index($folder, $search ? $search : "1:*", | |
| 1473 $sort_field, $this->options['skip_deleted'], | |
| 1474 $search ? true : false, true); | |
| 1475 } | |
| 1476 } | |
| 1477 | |
| 1478 if ($sort_order != $index->get_parameters('ORDER')) { | |
| 1479 $index->revert(); | |
| 1480 } | |
| 1481 | |
| 1482 return $index; | |
| 1483 } | |
| 1484 | |
| 1485 /** | |
| 1486 * Return index of threaded message UIDs | |
| 1487 * | |
| 1488 * @param string $folder Folder to get index from | |
| 1489 * @param string $sort_field Sort column | |
| 1490 * @param string $sort_order Sort order [ASC, DESC] | |
| 1491 * | |
| 1492 * @return rcube_result_thread Message UIDs | |
| 1493 */ | |
| 1494 public function thread_index($folder='', $sort_field=NULL, $sort_order=NULL) | |
| 1495 { | |
| 1496 if (!strlen($folder)) { | |
| 1497 $folder = $this->folder; | |
| 1498 } | |
| 1499 | |
| 1500 // we have a saved search result, get index from there | |
| 1501 if ($this->search_string && $this->search_threads && $folder == $this->folder) { | |
| 1502 $threads = $this->search_set; | |
| 1503 } | |
| 1504 else { | |
| 1505 // get all threads (default sort order) | |
| 1506 $threads = $this->threads($folder); | |
| 1507 } | |
| 1508 | |
| 1509 $this->set_sort_order($sort_field, $sort_order); | |
| 1510 $this->sort_threads($threads); | |
| 1511 | |
| 1512 return $threads; | |
| 1513 } | |
| 1514 | |
| 1515 /** | |
| 1516 * Sort threaded result, using THREAD=REFS method if available. | |
| 1517 * If not, use any method and re-sort the result in THREAD=REFS way. | |
| 1518 * | |
| 1519 * @param rcube_result_thread $threads Threads result set | |
| 1520 */ | |
| 1521 protected function sort_threads($threads) | |
| 1522 { | |
| 5 | 1523 #rcube::write_log('mail','sort_threads '.count($threads)." ".$this->sort_field); |
| 0 | 1524 if ($threads->is_empty()) { |
| 1525 return; | |
| 1526 } | |
| 1527 // THREAD=ORDEREDSUBJECT: sorting by sent date of root message | |
| 1528 // THREAD=REFERENCES: sorting by sent date of root message | |
| 1529 // THREAD=REFS: sorting by the most recent date in each thread | |
| 1530 | |
| 1531 if ($this->threading != 'REFS' || ($this->sort_field && $this->sort_field != 'date')) { | |
| 1532 $sortby = $this->sort_field ? $this->sort_field : 'date'; | |
| 1533 $index = $this->index($this->folder, $sortby, $this->sort_order, true, true); | |
| 5 | 1534 #rcube::write_log('mail',"sort_threads $sortby".print_r($index,true)); |
| 0 | 1535 if (!$index->is_empty()) { |
| 1536 $threads->sort($index); | |
| 1537 } | |
| 1538 } | |
| 1539 else if ($this->sort_order != $threads->get_parameters('ORDER')) { | |
| 1540 $threads->revert(); | |
| 1541 } | |
| 1542 } | |
| 1543 | |
| 1544 /** | |
| 1545 * Invoke search request to IMAP server | |
| 1546 * | |
| 1547 * @param string $folder Folder name to search in | |
| 1548 * @param string $search Search criteria | |
| 1549 * @param string $charset Search charset | |
| 1550 * @param string $sort_field Header field to sort by | |
| 1551 * | |
| 1552 * @return rcube_result_index Search result object | |
| 1553 * @todo: Search criteria should be provided in non-IMAP format, eg. array | |
| 1554 */ | |
| 1555 public function search($folder = '', $search = 'ALL', $charset = null, $sort_field = null) | |
| 1556 { | |
| 1557 if (!$search) { | |
| 1558 $search = 'ALL'; | |
| 1559 } | |
| 1560 | |
| 1561 if ((is_array($folder) && empty($folder)) || (!is_array($folder) && !strlen($folder))) { | |
| 1562 $folder = $this->folder; | |
| 1563 } | |
| 1564 | |
| 1565 $plugin = $this->plugins->exec_hook('imap_search_before', array( | |
| 1566 'folder' => $folder, | |
| 1567 'search' => $search, | |
| 1568 'charset' => $charset, | |
| 1569 'sort_field' => $sort_field, | |
| 1570 'threading' => $this->threading, | |
| 1571 )); | |
| 1572 | |
| 1573 $folder = $plugin['folder']; | |
| 1574 $search = $plugin['search']; | |
| 1575 $charset = $plugin['charset']; | |
| 1576 $sort_field = $plugin['sort_field']; | |
| 1577 $results = $plugin['result']; | |
| 1578 | |
| 1579 // multi-folder search | |
| 1580 if (!$results && is_array($folder) && count($folder) > 1 && $search != 'ALL') { | |
| 1581 // connect IMAP to have all the required classes and settings loaded | |
| 1582 $this->check_connection(); | |
| 1583 | |
| 1584 // disable threading | |
| 1585 $this->threading = false; | |
| 1586 | |
| 1587 $searcher = new rcube_imap_search($this->options, $this->conn); | |
| 1588 | |
| 1589 // set limit to not exceed the client's request timeout | |
| 1590 $searcher->set_timelimit(60); | |
| 1591 | |
| 1592 // continue existing incomplete search | |
| 1593 if (!empty($this->search_set) && $this->search_set->incomplete && $search == $this->search_string) { | |
| 1594 $searcher->set_results($this->search_set); | |
| 1595 } | |
| 1596 | |
| 1597 // execute the search | |
| 1598 $results = $searcher->exec( | |
| 1599 $folder, | |
| 1600 $search, | |
| 1601 $charset ? $charset : $this->default_charset, | |
| 1602 $sort_field && $this->get_capability('SORT') ? $sort_field : null, | |
| 1603 $this->threading | |
| 1604 ); | |
| 1605 } | |
| 1606 else if (!$results) { | |
| 1607 $folder = is_array($folder) ? $folder[0] : $folder; | |
| 1608 $search = is_array($search) ? $search[$folder] : $search; | |
| 1609 $results = $this->search_index($folder, $search, $charset, $sort_field); | |
| 1610 } | |
| 1611 | |
| 1612 $sorted = $this->threading || $this->search_sorted || $plugin['search_sorted'] ? true : false; | |
| 1613 | |
| 1614 $this->set_search_set(array($search, $results, $charset, $sort_field, $sorted)); | |
| 1615 | |
| 1616 return $results; | |
| 1617 } | |
| 1618 | |
| 1619 /** | |
| 1620 * Direct (real and simple) SEARCH request (without result sorting and caching). | |
| 1621 * | |
| 1622 * @param string $mailbox Mailbox name to search in | |
| 1623 * @param string $str Search string | |
| 1624 * | |
| 1625 * @return rcube_result_index Search result (UIDs) | |
| 1626 */ | |
| 1627 public function search_once($folder = null, $str = 'ALL') | |
| 1628 { | |
| 1629 if (!$this->check_connection()) { | |
| 1630 return new rcube_result_index(); | |
| 1631 } | |
| 1632 | |
| 1633 if (!$str) { | |
| 1634 $str = 'ALL'; | |
| 1635 } | |
| 1636 | |
| 1637 // multi-folder search | |
| 1638 if (is_array($folder) && count($folder) > 1) { | |
| 1639 $searcher = new rcube_imap_search($this->options, $this->conn); | |
| 1640 $index = $searcher->exec($folder, $str, $this->default_charset); | |
| 1641 } | |
| 1642 else { | |
| 1643 $folder = is_array($folder) ? $folder[0] : $folder; | |
| 1644 if (!strlen($folder)) { | |
| 1645 $folder = $this->folder; | |
| 1646 } | |
| 1647 $index = $this->conn->search($folder, $str, true); | |
| 1648 } | |
| 1649 | |
| 1650 return $index; | |
| 1651 } | |
| 1652 | |
| 1653 /** | |
| 1654 * protected search method | |
| 1655 * | |
| 1656 * @param string $folder Folder name | |
| 1657 * @param string $criteria Search criteria | |
| 1658 * @param string $charset Charset | |
| 1659 * @param string $sort_field Sorting field | |
| 1660 * | |
| 1661 * @return rcube_result_index|rcube_result_thread Search results (UIDs) | |
| 1662 * @see rcube_imap::search() | |
| 1663 */ | |
| 1664 protected function search_index($folder, $criteria='ALL', $charset=NULL, $sort_field=NULL) | |
| 1665 { | |
| 1666 if (!$this->check_connection()) { | |
| 1667 if ($this->threading) { | |
| 1668 return new rcube_result_thread(); | |
| 1669 } | |
| 1670 else { | |
| 1671 return new rcube_result_index(); | |
| 1672 } | |
| 1673 } | |
| 1674 | |
| 1675 if ($this->options['skip_deleted'] && !preg_match('/UNDELETED/', $criteria)) { | |
| 1676 $criteria = 'UNDELETED '.$criteria; | |
| 1677 } | |
| 1678 | |
| 1679 // unset CHARSET if criteria string is ASCII, this way | |
| 1680 // SEARCH won't be re-sent after "unsupported charset" response | |
| 1681 if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) { | |
| 1682 $charset = 'US-ASCII'; | |
| 1683 } | |
| 1684 | |
| 1685 if ($this->threading) { | |
| 1686 $threads = $this->conn->thread($folder, $this->threading, $criteria, true, $charset); | |
| 1687 | |
| 1688 // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, | |
| 1689 // but I've seen that Courier doesn't support UTF-8) | |
| 1690 if ($threads->is_error() && $charset && $charset != 'US-ASCII') { | |
| 1691 $threads = $this->conn->thread($folder, $this->threading, | |
| 1692 self::convert_criteria($criteria, $charset), true, 'US-ASCII'); | |
| 1693 } | |
| 1694 | |
| 1695 return $threads; | |
| 1696 } | |
| 1697 | |
| 1698 if ($sort_field && $this->get_capability('SORT')) { | |
| 1699 $charset = $charset ? $charset : $this->default_charset; | |
| 1700 $messages = $this->conn->sort($folder, $sort_field, $criteria, true, $charset); | |
| 1701 | |
| 1702 // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, | |
| 1703 // but I've seen Courier with disabled UTF-8 support) | |
| 1704 if ($messages->is_error() && $charset && $charset != 'US-ASCII') { | |
| 1705 $messages = $this->conn->sort($folder, $sort_field, | |
| 1706 self::convert_criteria($criteria, $charset), true, 'US-ASCII'); | |
| 1707 } | |
| 1708 | |
| 1709 if (!$messages->is_error()) { | |
| 1710 $this->search_sorted = true; | |
| 1711 return $messages; | |
| 1712 } | |
| 1713 } | |
| 1714 | |
| 1715 $messages = $this->conn->search($folder, | |
| 1716 ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true); | |
| 1717 | |
| 1718 // Error, try with US-ASCII (some servers may support only US-ASCII) | |
| 1719 if ($messages->is_error() && $charset && $charset != 'US-ASCII') { | |
| 1720 $messages = $this->conn->search($folder, | |
| 1721 self::convert_criteria($criteria, $charset), true); | |
| 1722 } | |
| 1723 | |
| 1724 $this->search_sorted = false; | |
| 1725 | |
| 1726 return $messages; | |
| 1727 } | |
| 1728 | |
| 1729 /** | |
| 1730 * Converts charset of search criteria string | |
| 1731 * | |
| 1732 * @param string $str Search string | |
| 1733 * @param string $charset Original charset | |
| 1734 * @param string $dest_charset Destination charset (default US-ASCII) | |
| 1735 * | |
| 1736 * @return string Search string | |
| 1737 */ | |
| 1738 public static function convert_criteria($str, $charset, $dest_charset='US-ASCII') | |
| 1739 { | |
| 1740 // convert strings to US_ASCII | |
| 1741 if (preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE)) { | |
| 1742 $last = 0; $res = ''; | |
| 1743 foreach ($matches[1] as $m) { | |
| 1744 $string_offset = $m[1] + strlen($m[0]) + 4; // {}\r\n | |
| 1745 $string = substr($str, $string_offset - 1, $m[0]); | |
| 1746 $string = rcube_charset::convert($string, $charset, $dest_charset); | |
| 1747 | |
| 1748 if ($string === false || !strlen($string)) { | |
| 1749 continue; | |
| 1750 } | |
| 1751 | |
| 1752 $res .= substr($str, $last, $m[1] - $last - 1) . rcube_imap_generic::escape($string); | |
| 1753 $last = $m[0] + $string_offset - 1; | |
| 1754 } | |
| 1755 | |
| 1756 if ($last < strlen($str)) { | |
| 1757 $res .= substr($str, $last, strlen($str)-$last); | |
| 1758 } | |
| 1759 } | |
| 1760 // strings for conversion not found | |
| 1761 else { | |
| 1762 $res = $str; | |
| 1763 } | |
| 1764 | |
| 1765 return $res; | |
| 1766 } | |
| 1767 | |
| 1768 /** | |
| 1769 * Refresh saved search set | |
| 1770 * | |
| 1771 * @return array Current search set | |
| 1772 */ | |
| 1773 public function refresh_search() | |
| 1774 { | |
| 1775 if (!empty($this->search_string)) { | |
| 1776 $this->search( | |
| 1777 is_object($this->search_set) ? $this->search_set->get_parameters('MAILBOX') : '', | |
| 1778 $this->search_string, | |
| 1779 $this->search_charset, | |
| 1780 $this->search_sort_field | |
| 1781 ); | |
| 1782 } | |
| 1783 | |
| 1784 return $this->get_search_set(); | |
| 1785 } | |
| 1786 | |
| 1787 /** | |
| 1788 * Flag certain result subsets as 'incomplete'. | |
| 1789 * For subsequent refresh_search() calls to only refresh the updated parts. | |
| 1790 */ | |
| 1791 protected function set_search_dirty($folder) | |
| 1792 { | |
| 1793 if ($this->search_set && is_a($this->search_set, 'rcube_result_multifolder')) { | |
| 1794 if ($subset = $this->search_set->get_set($folder)) { | |
| 1795 $subset->incomplete = $this->search_set->incomplete = true; | |
| 1796 } | |
| 1797 } | |
| 1798 } | |
| 1799 | |
| 1800 /** | |
| 1801 * Return message headers object of a specific message | |
| 1802 * | |
| 1803 * @param int $id Message UID | |
| 1804 * @param string $folder Folder to read from | |
| 1805 * @param bool $force True to skip cache | |
| 1806 * | |
| 1807 * @return rcube_message_header Message headers | |
| 1808 */ | |
| 1809 public function get_message_headers($uid, $folder = null, $force = false) | |
| 1810 { | |
| 1811 // decode combined UID-folder identifier | |
| 1812 if (preg_match('/^\d+-.+/', $uid)) { | |
| 1813 list($uid, $folder) = explode('-', $uid, 2); | |
| 1814 } | |
| 1815 | |
| 21 | 1816 if (empty($folder)) { |
| 0 | 1817 $folder = $this->folder; |
| 1818 } | |
| 1819 | |
| 1820 // get cached headers | |
| 1821 if (!$force && $uid && ($mcache = $this->get_mcache_engine())) { | |
| 1822 $headers = $mcache->get_message($folder, $uid); | |
| 1823 } | |
| 1824 else if (!$this->check_connection()) { | |
| 1825 $headers = false; | |
| 1826 } | |
| 1827 else { | |
| 1828 $headers = $this->conn->fetchHeader( | |
| 1829 $folder, $uid, true, true, $this->get_fetch_headers()); | |
| 1830 | |
| 1831 if (is_object($headers)) | |
| 1832 $headers->folder = $folder; | |
| 1833 } | |
| 1834 | |
| 1835 return $headers; | |
| 1836 } | |
| 1837 | |
| 1838 /** | |
| 1839 * Fetch message headers and body structure from the IMAP server and build | |
| 1840 * an object structure. | |
| 1841 * | |
| 1842 * @param int $uid Message UID to fetch | |
| 1843 * @param string $folder Folder to read from | |
| 1844 * | |
| 1845 * @return object rcube_message_header Message data | |
| 1846 */ | |
| 1847 public function get_message($uid, $folder = null) | |
| 1848 { | |
| 21 | 1849 if (!empty($folder)) { |
| 0 | 1850 $folder = $this->folder; |
| 1851 } | |
| 1852 | |
| 1853 // decode combined UID-folder identifier | |
| 1854 if (preg_match('/^\d+-.+/', $uid)) { | |
| 1855 list($uid, $folder) = explode('-', $uid, 2); | |
| 1856 } | |
| 1857 | |
| 1858 // Check internal cache | |
| 1859 if (!empty($this->icache['message'])) { | |
| 1860 if (($headers = $this->icache['message']) && $headers->uid == $uid) { | |
| 1861 return $headers; | |
| 1862 } | |
| 1863 } | |
| 1864 | |
| 1865 $headers = $this->get_message_headers($uid, $folder); | |
| 1866 | |
| 1867 // message doesn't exist? | |
| 1868 if (empty($headers)) { | |
| 1869 return null; | |
| 1870 } | |
| 1871 | |
| 1872 // structure might be cached | |
| 1873 if (!empty($headers->structure)) { | |
| 1874 return $headers; | |
| 1875 } | |
| 1876 | |
| 1877 $this->msg_uid = $uid; | |
| 1878 | |
| 1879 if (!$this->check_connection()) { | |
| 1880 return $headers; | |
| 1881 } | |
| 1882 | |
| 1883 if (empty($headers->bodystructure)) { | |
| 1884 $headers->bodystructure = $this->conn->getStructure($folder, $uid, true); | |
| 1885 } | |
| 1886 | |
| 1887 $structure = $headers->bodystructure; | |
| 1888 | |
| 1889 if (empty($structure)) { | |
| 1890 return $headers; | |
| 1891 } | |
| 1892 | |
| 1893 // set message charset from message headers | |
| 1894 if ($headers->charset) { | |
| 1895 $this->struct_charset = $headers->charset; | |
| 1896 } | |
| 1897 else { | |
| 1898 $this->struct_charset = $this->structure_charset($structure); | |
| 1899 } | |
| 1900 | |
| 1901 $headers->ctype = @strtolower($headers->ctype); | |
| 1902 | |
| 1903 // Here we can recognize malformed BODYSTRUCTURE and | |
| 1904 // 1. [@TODO] parse the message in other way to create our own message structure | |
| 1905 // 2. or just show the raw message body. | |
| 1906 // Example of structure for malformed MIME message: | |
| 1907 // ("text" "plain" NIL NIL NIL "7bit" 2154 70 NIL NIL NIL) | |
| 1908 if ($headers->ctype && !is_array($structure[0]) && $headers->ctype != 'text/plain' | |
| 1909 && strtolower($structure[0].'/'.$structure[1]) == 'text/plain' | |
| 1910 ) { | |
| 1911 // A special known case "Content-type: text" (#1488968) | |
| 1912 if ($headers->ctype == 'text') { | |
| 1913 $structure[1] = 'plain'; | |
| 1914 $headers->ctype = 'text/plain'; | |
| 1915 } | |
| 1916 // we can handle single-part messages, by simple fix in structure (#1486898) | |
| 1917 else if (preg_match('/^(text|application)\/(.*)/', $headers->ctype, $m)) { | |
| 1918 $structure[0] = $m[1]; | |
| 1919 $structure[1] = $m[2]; | |
| 1920 } | |
| 1921 else { | |
| 1922 // Try to parse the message using rcube_mime_decode. | |
| 1923 // We need a better solution, it parses message | |
| 1924 // in memory, which wouldn't work for very big messages, | |
| 1925 // (it uses up to 10x more memory than the message size) | |
| 1926 // it's also buggy and not actively developed | |
| 1927 if ($headers->size && rcube_utils::mem_check($headers->size * 10)) { | |
| 1928 $raw_msg = $this->get_raw_body($uid); | |
| 1929 $struct = rcube_mime::parse_message($raw_msg); | |
| 1930 } | |
| 1931 else { | |
| 1932 return $headers; | |
| 1933 } | |
| 1934 } | |
| 1935 } | |
| 1936 | |
| 1937 if (empty($struct)) { | |
| 1938 $struct = $this->structure_part($structure, 0, '', $headers); | |
| 1939 } | |
| 1940 | |
| 1941 // some workarounds on simple messages... | |
| 1942 if (empty($struct->parts)) { | |
| 1943 // ...don't trust given content-type | |
| 1944 if (!empty($headers->ctype)) { | |
| 1945 $struct->mime_id = '1'; | |
| 1946 $struct->mimetype = strtolower($headers->ctype); | |
| 1947 list($struct->ctype_primary, $struct->ctype_secondary) = explode('/', $struct->mimetype); | |
| 1948 } | |
| 1949 | |
| 1950 // ...and charset (there's a case described in #1488968 where invalid content-type | |
| 1951 // results in invalid charset in BODYSTRUCTURE) | |
| 1952 if (!empty($headers->charset) && $headers->charset != $struct->ctype_parameters['charset']) { | |
| 1953 $struct->charset = $headers->charset; | |
| 1954 $struct->ctype_parameters['charset'] = $headers->charset; | |
| 1955 } | |
| 1956 } | |
| 1957 | |
| 1958 $headers->structure = $struct; | |
| 1959 | |
| 1960 return $this->icache['message'] = $headers; | |
| 1961 } | |
| 1962 | |
| 1963 /** | |
| 1964 * Build message part object | |
| 1965 * | |
| 1966 * @param array $part | |
| 1967 * @param int $count | |
| 1968 * @param string $parent | |
| 1969 */ | |
| 1970 protected function structure_part($part, $count = 0, $parent = '', $mime_headers = null) | |
| 1971 { | |
| 1972 $struct = new rcube_message_part; | |
| 1973 $struct->mime_id = empty($parent) ? (string)$count : "$parent.$count"; | |
| 1974 | |
| 1975 // multipart | |
| 1976 if (is_array($part[0])) { | |
| 1977 $struct->ctype_primary = 'multipart'; | |
| 1978 | |
| 1979 /* RFC3501: BODYSTRUCTURE fields of multipart part | |
| 1980 part1 array | |
| 1981 part2 array | |
| 1982 part3 array | |
| 1983 .... | |
| 1984 1. subtype | |
| 1985 2. parameters (optional) | |
| 1986 3. description (optional) | |
| 1987 4. language (optional) | |
| 1988 5. location (optional) | |
| 1989 */ | |
| 1990 | |
| 1991 // find first non-array entry | |
| 1992 for ($i=1; $i<count($part); $i++) { | |
| 1993 if (!is_array($part[$i])) { | |
| 1994 $struct->ctype_secondary = strtolower($part[$i]); | |
| 1995 | |
| 1996 // read content type parameters | |
| 1997 if (is_array($part[$i+1])) { | |
| 1998 $struct->ctype_parameters = array(); | |
| 1999 for ($j=0; $j<count($part[$i+1]); $j+=2) { | |
| 2000 $param = strtolower($part[$i+1][$j]); | |
| 2001 $struct->ctype_parameters[$param] = $part[$i+1][$j+1]; | |
| 2002 } | |
| 2003 } | |
| 2004 | |
| 2005 break; | |
| 2006 } | |
| 2007 } | |
| 2008 | |
| 2009 $struct->mimetype = 'multipart/'.$struct->ctype_secondary; | |
| 2010 | |
| 2011 // build parts list for headers pre-fetching | |
| 2012 for ($i=0; $i<count($part); $i++) { | |
| 2013 if (!is_array($part[$i])) { | |
| 2014 break; | |
| 2015 } | |
| 2016 // fetch message headers if message/rfc822 | |
| 2017 // or named part (could contain Content-Location header) | |
| 2018 if (!is_array($part[$i][0])) { | |
| 2019 $tmp_part_id = $struct->mime_id ? $struct->mime_id.'.'.($i+1) : $i+1; | |
| 2020 if (strtolower($part[$i][0]) == 'message' && strtolower($part[$i][1]) == 'rfc822') { | |
| 2021 $mime_part_headers[] = $tmp_part_id; | |
| 2022 } | |
| 2023 else if (in_array('name', (array)$part[$i][2]) && empty($part[$i][3])) { | |
| 2024 $mime_part_headers[] = $tmp_part_id; | |
| 2025 } | |
| 2026 } | |
| 2027 } | |
| 2028 | |
| 2029 // pre-fetch headers of all parts (in one command for better performance) | |
| 2030 // @TODO: we could do this before _structure_part() call, to fetch | |
| 2031 // headers for parts on all levels | |
| 21 | 2032 if (!empty($mime_part_headers)) { |
| 0 | 2033 $mime_part_headers = $this->conn->fetchMIMEHeaders($this->folder, |
| 2034 $this->msg_uid, $mime_part_headers); | |
| 2035 } | |
| 2036 | |
| 2037 $struct->parts = array(); | |
| 2038 for ($i=0, $count=0; $i<count($part); $i++) { | |
| 2039 if (!is_array($part[$i])) { | |
| 2040 break; | |
| 2041 } | |
| 2042 $tmp_part_id = $struct->mime_id ? $struct->mime_id.'.'.($i+1) : $i+1; | |
| 2043 $struct->parts[] = $this->structure_part($part[$i], ++$count, $struct->mime_id, | |
| 21 | 2044 empty($mime_part_headers[$tmp_part_id]) ? null : $mime_part_headers[$tmp_part_id]) ; |
| 0 | 2045 } |
| 2046 | |
| 2047 return $struct; | |
| 2048 } | |
| 2049 | |
| 2050 /* RFC3501: BODYSTRUCTURE fields of non-multipart part | |
| 2051 0. type | |
| 2052 1. subtype | |
| 2053 2. parameters | |
| 2054 3. id | |
| 2055 4. description | |
| 2056 5. encoding | |
| 2057 6. size | |
| 2058 -- text | |
| 2059 7. lines | |
| 2060 -- message/rfc822 | |
| 2061 7. envelope structure | |
| 2062 8. body structure | |
| 2063 9. lines | |
| 2064 -- | |
| 2065 x. md5 (optional) | |
| 2066 x. disposition (optional) | |
| 2067 x. language (optional) | |
| 2068 x. location (optional) | |
| 2069 */ | |
| 2070 | |
| 2071 // regular part | |
| 2072 $struct->ctype_primary = strtolower($part[0]); | |
| 2073 $struct->ctype_secondary = strtolower($part[1]); | |
| 2074 $struct->mimetype = $struct->ctype_primary.'/'.$struct->ctype_secondary; | |
| 2075 | |
| 2076 // read content type parameters | |
| 2077 if (is_array($part[2])) { | |
| 2078 $struct->ctype_parameters = array(); | |
| 2079 for ($i=0; $i<count($part[2]); $i+=2) { | |
| 2080 $struct->ctype_parameters[strtolower($part[2][$i])] = $part[2][$i+1]; | |
| 2081 } | |
| 2082 | |
| 2083 if (isset($struct->ctype_parameters['charset'])) { | |
| 2084 $struct->charset = $struct->ctype_parameters['charset']; | |
| 2085 } | |
| 2086 } | |
| 2087 | |
| 2088 // #1487700: workaround for lack of charset in malformed structure | |
| 2089 if (empty($struct->charset) && !empty($mime_headers) && $mime_headers->charset) { | |
| 2090 $struct->charset = $mime_headers->charset; | |
| 2091 } | |
| 2092 | |
| 2093 // read content encoding | |
| 2094 if (!empty($part[5])) { | |
| 2095 $struct->encoding = strtolower($part[5]); | |
| 2096 $struct->headers['content-transfer-encoding'] = $struct->encoding; | |
| 2097 } | |
| 2098 | |
| 2099 // get part size | |
| 2100 if (!empty($part[6])) { | |
| 2101 $struct->size = intval($part[6]); | |
| 2102 } | |
| 2103 | |
| 2104 // read part disposition | |
| 2105 $di = 8; | |
| 2106 if ($struct->ctype_primary == 'text') { | |
| 2107 $di += 1; | |
| 2108 } | |
| 2109 else if ($struct->mimetype == 'message/rfc822') { | |
| 2110 $di += 3; | |
| 2111 } | |
| 2112 | |
| 2113 if (is_array($part[$di]) && count($part[$di]) == 2) { | |
| 2114 $struct->disposition = strtolower($part[$di][0]); | |
| 2115 if ($struct->disposition && $struct->disposition !== 'inline' && $struct->disposition !== 'attachment') { | |
| 2116 // RFC2183, Section 2.8 - unrecognized type should be treated as "attachment" | |
| 2117 $struct->disposition = 'attachment'; | |
| 2118 } | |
| 2119 if (is_array($part[$di][1])) { | |
| 2120 for ($n=0; $n<count($part[$di][1]); $n+=2) { | |
| 2121 $struct->d_parameters[strtolower($part[$di][1][$n])] = $part[$di][1][$n+1]; | |
| 2122 } | |
| 2123 } | |
| 2124 } | |
| 2125 | |
| 2126 // get message/rfc822's child-parts | |
| 2127 if (is_array($part[8]) && $di != 8) { | |
| 2128 $struct->parts = array(); | |
| 2129 for ($i=0, $count=0; $i<count($part[8]); $i++) { | |
| 2130 if (!is_array($part[8][$i])) { | |
| 2131 break; | |
| 2132 } | |
| 2133 $struct->parts[] = $this->structure_part($part[8][$i], ++$count, $struct->mime_id); | |
| 2134 } | |
| 2135 } | |
| 2136 | |
| 2137 // get part ID | |
| 2138 if (!empty($part[3])) { | |
| 2139 $struct->content_id = $part[3]; | |
| 2140 $struct->headers['content-id'] = $part[3]; | |
| 2141 | |
| 2142 if (empty($struct->disposition)) { | |
| 2143 $struct->disposition = 'inline'; | |
| 2144 } | |
| 2145 } | |
| 2146 | |
| 2147 // fetch message headers if message/rfc822 or named part (could contain Content-Location header) | |
| 21 | 2148 if ($struct->ctype_primary == 'message' || (!empty($struct->ctype_parameters['name']) && !$struct->content_id)) { |
| 0 | 2149 if (empty($mime_headers)) { |
| 2150 $mime_headers = $this->conn->fetchPartHeader( | |
| 2151 $this->folder, $this->msg_uid, true, $struct->mime_id); | |
| 2152 } | |
| 2153 | |
| 2154 if (is_string($mime_headers)) { | |
| 2155 $struct->headers = rcube_mime::parse_headers($mime_headers) + $struct->headers; | |
| 2156 } | |
| 2157 else if (is_object($mime_headers)) { | |
| 2158 $struct->headers = get_object_vars($mime_headers) + $struct->headers; | |
| 2159 } | |
| 2160 | |
| 2161 // get real content-type of message/rfc822 | |
| 2162 if ($struct->mimetype == 'message/rfc822') { | |
| 2163 // single-part | |
| 2164 if (!is_array($part[8][0])) { | |
| 2165 $struct->real_mimetype = strtolower($part[8][0] . '/' . $part[8][1]); | |
| 2166 } | |
| 2167 // multi-part | |
| 2168 else { | |
| 2169 for ($n=0; $n<count($part[8]); $n++) { | |
| 2170 if (!is_array($part[8][$n])) { | |
| 2171 break; | |
| 2172 } | |
| 2173 } | |
| 2174 $struct->real_mimetype = 'multipart/' . strtolower($part[8][$n]); | |
| 2175 } | |
| 2176 } | |
| 2177 | |
| 2178 if ($struct->ctype_primary == 'message' && empty($struct->parts)) { | |
| 2179 if (is_array($part[8]) && $di != 8) { | |
| 2180 $struct->parts[] = $this->structure_part($part[8], ++$count, $struct->mime_id); | |
| 2181 } | |
| 2182 } | |
| 2183 } | |
| 2184 | |
| 2185 // normalize filename property | |
| 2186 $this->set_part_filename($struct, $mime_headers); | |
| 2187 | |
| 2188 return $struct; | |
| 2189 } | |
| 2190 | |
| 2191 /** | |
| 2192 * Set attachment filename from message part structure | |
| 2193 * | |
| 2194 * @param rcube_message_part $part Part object | |
| 2195 * @param string $headers Part's raw headers | |
| 2196 */ | |
| 2197 protected function set_part_filename(&$part, $headers = null) | |
| 2198 { | |
| 2199 if (!empty($part->d_parameters['filename'])) { | |
| 2200 $filename_mime = $part->d_parameters['filename']; | |
| 2201 } | |
| 2202 else if (!empty($part->d_parameters['filename*'])) { | |
| 2203 $filename_encoded = $part->d_parameters['filename*']; | |
| 2204 } | |
| 2205 else if (!empty($part->ctype_parameters['name*'])) { | |
| 2206 $filename_encoded = $part->ctype_parameters['name*']; | |
| 2207 } | |
| 2208 // RFC2231 value continuations | |
| 2209 // TODO: this should be rewrited to support RFC2231 4.1 combinations | |
| 2210 else if (!empty($part->d_parameters['filename*0'])) { | |
| 2211 $i = 0; | |
| 2212 while (isset($part->d_parameters['filename*'.$i])) { | |
| 2213 $filename_mime .= $part->d_parameters['filename*'.$i]; | |
| 2214 $i++; | |
| 2215 } | |
| 2216 // some servers (eg. dovecot-1.x) have no support for parameter value continuations | |
| 2217 // we must fetch and parse headers "manually" | |
| 2218 if ($i<2) { | |
| 2219 if (!$headers) { | |
| 2220 $headers = $this->conn->fetchPartHeader( | |
| 2221 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2222 } | |
| 2223 $filename_mime = ''; | |
| 2224 $i = 0; | |
| 2225 while (preg_match('/filename\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2226 $filename_mime .= $matches[1]; | |
| 2227 $i++; | |
| 2228 } | |
| 2229 } | |
| 2230 } | |
| 2231 else if (!empty($part->d_parameters['filename*0*'])) { | |
| 2232 $i = 0; | |
| 2233 while (isset($part->d_parameters['filename*'.$i.'*'])) { | |
| 2234 $filename_encoded .= $part->d_parameters['filename*'.$i.'*']; | |
| 2235 $i++; | |
| 2236 } | |
| 2237 if ($i<2) { | |
| 2238 if (!$headers) { | |
| 2239 $headers = $this->conn->fetchPartHeader( | |
| 2240 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2241 } | |
| 2242 $filename_encoded = ''; | |
| 2243 $i = 0; $matches = array(); | |
| 2244 while (preg_match('/filename\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2245 $filename_encoded .= $matches[1]; | |
| 2246 $i++; | |
| 2247 } | |
| 2248 } | |
| 2249 } | |
| 2250 else if (!empty($part->ctype_parameters['name*0'])) { | |
| 2251 $i = 0; | |
| 2252 while (isset($part->ctype_parameters['name*'.$i])) { | |
| 2253 $filename_mime .= $part->ctype_parameters['name*'.$i]; | |
| 2254 $i++; | |
| 2255 } | |
| 2256 if ($i<2) { | |
| 2257 if (!$headers) { | |
| 2258 $headers = $this->conn->fetchPartHeader( | |
| 2259 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2260 } | |
| 2261 $filename_mime = ''; | |
| 2262 $i = 0; $matches = array(); | |
| 2263 while (preg_match('/\s+name\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2264 $filename_mime .= $matches[1]; | |
| 2265 $i++; | |
| 2266 } | |
| 2267 } | |
| 2268 } | |
| 2269 else if (!empty($part->ctype_parameters['name*0*'])) { | |
| 2270 $i = 0; | |
| 2271 while (isset($part->ctype_parameters['name*'.$i.'*'])) { | |
| 2272 $filename_encoded .= $part->ctype_parameters['name*'.$i.'*']; | |
| 2273 $i++; | |
| 2274 } | |
| 2275 if ($i<2) { | |
| 2276 if (!$headers) { | |
| 2277 $headers = $this->conn->fetchPartHeader( | |
| 2278 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2279 } | |
| 2280 $filename_encoded = ''; | |
| 2281 $i = 0; $matches = array(); | |
| 2282 while (preg_match('/\s+name\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2283 $filename_encoded .= $matches[1]; | |
| 2284 $i++; | |
| 2285 } | |
| 2286 } | |
| 2287 } | |
| 2288 // read 'name' after rfc2231 parameters as it may contains truncated filename (from Thunderbird) | |
| 2289 else if (!empty($part->ctype_parameters['name'])) { | |
| 2290 $filename_mime = $part->ctype_parameters['name']; | |
| 2291 } | |
| 2292 // Content-Disposition | |
| 2293 else if (!empty($part->headers['content-description'])) { | |
| 2294 $filename_mime = $part->headers['content-description']; | |
| 2295 } | |
| 2296 else { | |
| 2297 return; | |
| 2298 } | |
| 2299 | |
| 2300 // decode filename | |
| 2301 if (!empty($filename_mime)) { | |
| 2302 if (!empty($part->charset)) { | |
| 2303 $charset = $part->charset; | |
| 2304 } | |
| 2305 else if (!empty($this->struct_charset)) { | |
| 2306 $charset = $this->struct_charset; | |
| 2307 } | |
| 2308 else { | |
| 2309 $charset = rcube_charset::detect($filename_mime, $this->default_charset); | |
| 2310 } | |
| 2311 | |
| 2312 $part->filename = rcube_mime::decode_mime_string($filename_mime, $charset); | |
| 2313 } | |
| 2314 else if (!empty($filename_encoded)) { | |
| 2315 // decode filename according to RFC 2231, Section 4 | |
| 2316 if (preg_match("/^([^']*)'[^']*'(.*)$/", $filename_encoded, $fmatches)) { | |
| 2317 $filename_charset = $fmatches[1]; | |
| 2318 $filename_encoded = $fmatches[2]; | |
| 2319 } | |
| 2320 | |
| 2321 $part->filename = rcube_charset::convert(urldecode($filename_encoded), $filename_charset); | |
| 2322 } | |
| 2323 } | |
| 2324 | |
| 2325 /** | |
| 2326 * Get charset name from message structure (first part) | |
| 2327 * | |
| 2328 * @param array $structure Message structure | |
| 2329 * | |
| 2330 * @return string Charset name | |
| 2331 */ | |
| 2332 protected function structure_charset($structure) | |
| 2333 { | |
| 2334 while (is_array($structure)) { | |
| 2335 if (is_array($structure[2]) && $structure[2][0] == 'charset') { | |
| 2336 return $structure[2][1]; | |
| 2337 } | |
| 2338 $structure = $structure[0]; | |
| 2339 } | |
| 2340 } | |
| 2341 | |
| 2342 | |
| 2343 /** | |
| 2344 * Fetch message body of a specific message from the server | |
| 2345 * | |
| 2346 * @param int Message UID | |
| 2347 * @param string Part number | |
| 2348 * @param rcube_message_part Part object created by get_structure() | |
| 2349 * @param mixed True to print part, resource to write part contents in | |
| 2350 * @param resource File pointer to save the message part | |
| 2351 * @param boolean Disables charset conversion | |
| 2352 * @param int Only read this number of bytes | |
| 2353 * @param boolean Enables formatting of text/* parts bodies | |
| 2354 * | |
| 2355 * @return string Message/part body if not printed | |
| 2356 */ | |
| 2357 public function get_message_part($uid, $part = 1, $o_part = null, $print = null, $fp = null, | |
| 2358 $skip_charset_conv = false, $max_bytes = 0, $formatted = true) | |
| 2359 { | |
| 2360 if (!$this->check_connection()) { | |
| 2361 return null; | |
| 2362 } | |
| 2363 | |
| 2364 // get part data if not provided | |
| 2365 if (!is_object($o_part)) { | |
| 2366 $structure = $this->conn->getStructure($this->folder, $uid, true); | |
| 2367 $part_data = rcube_imap_generic::getStructurePartData($structure, $part); | |
| 2368 | |
| 2369 $o_part = new rcube_message_part; | |
| 2370 $o_part->ctype_primary = $part_data['type']; | |
| 2371 $o_part->encoding = $part_data['encoding']; | |
| 2372 $o_part->charset = $part_data['charset']; | |
| 2373 $o_part->size = $part_data['size']; | |
| 2374 } | |
| 2375 | |
| 2376 if ($o_part && $o_part->size) { | |
| 2377 $formatted = $formatted && $o_part->ctype_primary == 'text'; | |
| 2378 $body = $this->conn->handlePartBody($this->folder, $uid, true, | |
| 2379 $part ? $part : 'TEXT', $o_part->encoding, $print, $fp, $formatted, $max_bytes); | |
| 2380 } | |
| 2381 | |
| 2382 if ($fp || $print) { | |
| 2383 return true; | |
| 2384 } | |
| 2385 | |
| 2386 // convert charset (if text or message part) | |
| 2387 if ($body && preg_match('/^(text|message)$/', $o_part->ctype_primary)) { | |
| 2388 // Remove NULL characters if any (#1486189) | |
| 2389 if ($formatted && strpos($body, "\x00") !== false) { | |
| 2390 $body = str_replace("\x00", '', $body); | |
| 2391 } | |
| 2392 | |
| 2393 if (!$skip_charset_conv) { | |
| 2394 if (!$o_part->charset || strtoupper($o_part->charset) == 'US-ASCII') { | |
| 2395 // try to extract charset information from HTML meta tag (#1488125) | |
| 2396 if ($o_part->ctype_secondary == 'html' && preg_match('/<meta[^>]+charset=([a-z0-9-_]+)/i', $body, $m)) { | |
| 2397 $o_part->charset = strtoupper($m[1]); | |
| 2398 } | |
| 2399 else { | |
| 2400 $o_part->charset = $this->default_charset; | |
| 2401 } | |
| 2402 } | |
| 2403 $body = rcube_charset::convert($body, $o_part->charset); | |
| 2404 } | |
| 2405 } | |
| 2406 | |
| 2407 return $body; | |
| 2408 } | |
| 2409 | |
| 2410 /** | |
| 2411 * Returns the whole message source as string (or saves to a file) | |
| 2412 * | |
| 2413 * @param int $uid Message UID | |
| 2414 * @param resource $fp File pointer to save the message | |
| 2415 * @param string $part Optional message part ID | |
| 2416 * | |
| 2417 * @return string Message source string | |
| 2418 */ | |
| 2419 public function get_raw_body($uid, $fp=null, $part = null) | |
| 2420 { | |
| 2421 if (!$this->check_connection()) { | |
| 2422 return null; | |
| 2423 } | |
| 2424 | |
| 2425 return $this->conn->handlePartBody($this->folder, $uid, | |
| 2426 true, $part, null, false, $fp); | |
| 2427 } | |
| 2428 | |
| 2429 /** | |
| 2430 * Returns the message headers as string | |
| 2431 * | |
| 2432 * @param int $uid Message UID | |
| 2433 * @param string $part Optional message part ID | |
| 2434 * | |
| 2435 * @return string Message headers string | |
| 2436 */ | |
| 2437 public function get_raw_headers($uid, $part = null) | |
| 2438 { | |
| 2439 if (!$this->check_connection()) { | |
| 2440 return null; | |
| 2441 } | |
| 2442 | |
| 2443 return $this->conn->fetchPartHeader($this->folder, $uid, true, $part); | |
| 2444 } | |
| 2445 | |
| 2446 /** | |
| 2447 * Sends the whole message source to stdout | |
| 2448 * | |
| 2449 * @param int $uid Message UID | |
| 2450 * @param bool $formatted Enables line-ending formatting | |
| 2451 */ | |
| 2452 public function print_raw_body($uid, $formatted = true) | |
| 2453 { | |
| 2454 if (!$this->check_connection()) { | |
| 2455 return; | |
| 2456 } | |
| 2457 | |
| 2458 $this->conn->handlePartBody($this->folder, $uid, true, null, null, true, null, $formatted); | |
| 2459 } | |
| 2460 | |
| 2461 /** | |
| 2462 * Set message flag to one or several messages | |
| 2463 * | |
| 2464 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2465 * @param string $flag Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT | |
| 2466 * @param string $folder Folder name | |
| 2467 * @param boolean $skip_cache True to skip message cache clean up | |
| 2468 * | |
| 2469 * @return boolean Operation status | |
| 2470 */ | |
| 2471 public function set_flag($uids, $flag, $folder=null, $skip_cache=false) | |
| 2472 { | |
| 2473 if (!strlen($folder)) { | |
| 2474 $folder = $this->folder; | |
| 2475 } | |
| 2476 | |
| 2477 if (!$this->check_connection()) { | |
| 2478 return false; | |
| 2479 } | |
| 2480 | |
| 2481 $flag = strtoupper($flag); | |
| 2482 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2483 | |
| 2484 if (strpos($flag, 'UN') === 0) { | |
| 2485 $result = $this->conn->unflag($folder, $uids, substr($flag, 2)); | |
| 2486 } | |
| 2487 else { | |
| 2488 $result = $this->conn->flag($folder, $uids, $flag); | |
| 2489 } | |
| 2490 | |
| 2491 if ($result && !$skip_cache) { | |
| 2492 // reload message headers if cached | |
| 2493 // update flags instead removing from cache | |
| 2494 if ($mcache = $this->get_mcache_engine()) { | |
| 2495 $status = strpos($flag, 'UN') !== 0; | |
| 2496 $mflag = preg_replace('/^UN/', '', $flag); | |
| 2497 $mcache->change_flag($folder, $all_mode ? null : explode(',', $uids), | |
| 2498 $mflag, $status); | |
| 2499 } | |
| 2500 | |
| 2501 // clear cached counters | |
| 2502 if ($flag == 'SEEN' || $flag == 'UNSEEN') { | |
| 2503 $this->clear_messagecount($folder, array('SEEN', 'UNSEEN')); | |
| 2504 } | |
| 2505 else if ($flag == 'DELETED' || $flag == 'UNDELETED') { | |
| 2506 $this->clear_messagecount($folder, array('ALL', 'THREADS')); | |
| 2507 if ($this->options['skip_deleted']) { | |
| 2508 // remove cached messages | |
| 2509 $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids)); | |
| 2510 } | |
| 2511 } | |
| 2512 | |
| 2513 $this->set_search_dirty($folder); | |
| 2514 } | |
| 2515 | |
| 2516 return $result; | |
| 2517 } | |
| 2518 | |
| 2519 /** | |
| 2520 * Append a mail message (source) to a specific folder | |
| 2521 * | |
| 2522 * @param string $folder Target folder | |
| 2523 * @param string|array $message The message source string or filename | |
| 2524 * or array (of strings and file pointers) | |
| 2525 * @param string $headers Headers string if $message contains only the body | |
| 2526 * @param boolean $is_file True if $message is a filename | |
| 2527 * @param array $flags Message flags | |
| 2528 * @param mixed $date Message internal date | |
| 2529 * @param bool $binary Enables BINARY append | |
| 2530 * | |
| 2531 * @return int|bool Appended message UID or True on success, False on error | |
| 2532 */ | |
| 2533 public function save_message($folder, &$message, $headers='', $is_file=false, $flags = array(), $date = null, $binary = false) | |
| 2534 { | |
| 2535 if (!strlen($folder)) { | |
| 2536 $folder = $this->folder; | |
| 2537 } | |
| 2538 | |
| 2539 if (!$this->check_connection()) { | |
| 2540 return false; | |
| 2541 } | |
| 2542 | |
| 2543 // make sure folder exists | |
| 2544 if (!$this->folder_exists($folder)) { | |
| 2545 return false; | |
| 2546 } | |
| 2547 | |
| 2548 $date = $this->date_format($date); | |
| 2549 | |
| 2550 if ($is_file) { | |
| 2551 $saved = $this->conn->appendFromFile($folder, $message, $headers, $flags, $date, $binary); | |
| 2552 } | |
| 2553 else { | |
| 2554 $saved = $this->conn->append($folder, $message, $flags, $date, $binary); | |
| 2555 } | |
| 2556 | |
| 2557 if ($saved) { | |
| 2558 // increase messagecount of the target folder | |
| 2559 $this->set_messagecount($folder, 'ALL', 1); | |
| 2560 | |
| 2561 $this->plugins->exec_hook('message_saved', array( | |
| 2562 'folder' => $folder, | |
| 2563 'message' => $message, | |
| 2564 'headers' => $headers, | |
| 2565 'is_file' => $is_file, | |
| 2566 'flags' => $flags, | |
| 2567 'date' => $date, | |
| 2568 'binary' => $binary, | |
| 2569 'result' => $saved, | |
| 2570 )); | |
| 2571 } | |
| 2572 | |
| 2573 return $saved; | |
| 2574 } | |
| 2575 | |
| 2576 /** | |
| 2577 * Move a message from one folder to another | |
| 2578 * | |
| 2579 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2580 * @param string $to_mbox Target folder | |
| 2581 * @param string $from_mbox Source folder | |
| 2582 * | |
| 2583 * @return boolean True on success, False on error | |
| 2584 */ | |
| 2585 public function move_message($uids, $to_mbox, $from_mbox='') | |
| 2586 { | |
| 2587 if (!strlen($from_mbox)) { | |
| 2588 $from_mbox = $this->folder; | |
| 2589 } | |
| 2590 | |
| 2591 if ($to_mbox === $from_mbox) { | |
| 2592 return false; | |
| 2593 } | |
| 2594 | |
| 2595 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2596 | |
| 2597 // exit if no message uids are specified | |
| 2598 if (empty($uids)) { | |
| 2599 return false; | |
| 2600 } | |
| 2601 | |
| 2602 if (!$this->check_connection()) { | |
| 2603 return false; | |
| 2604 } | |
| 2605 | |
| 2606 $config = rcube::get_instance()->config; | |
| 2607 $to_trash = $to_mbox == $config->get('trash_mbox'); | |
| 2608 | |
| 2609 // flag messages as read before moving them | |
| 2610 if ($to_trash && $config->get('read_when_deleted')) { | |
| 2611 // don't flush cache (4th argument) | |
| 2612 $this->set_flag($uids, 'SEEN', $from_mbox, true); | |
| 2613 } | |
| 2614 | |
| 2615 // move messages | |
| 2616 $moved = $this->conn->move($uids, $from_mbox, $to_mbox); | |
| 2617 | |
| 2618 // when moving to Trash we make sure the folder exists | |
| 2619 // as it's uncommon scenario we do this when MOVE fails, not before | |
| 2620 if (!$moved && $to_trash && $this->get_response_code() == rcube_storage::TRYCREATE) { | |
| 2621 if ($this->create_folder($to_mbox, true, 'trash')) { | |
| 2622 $moved = $this->conn->move($uids, $from_mbox, $to_mbox); | |
| 2623 } | |
| 2624 } | |
| 2625 | |
| 2626 if ($moved) { | |
| 2627 $this->clear_messagecount($from_mbox); | |
| 2628 $this->clear_messagecount($to_mbox); | |
| 2629 | |
| 2630 $this->set_search_dirty($from_mbox); | |
| 2631 $this->set_search_dirty($to_mbox); | |
| 2632 } | |
| 2633 // moving failed | |
| 2634 else if ($to_trash && $config->get('delete_always', false)) { | |
| 2635 $moved = $this->delete_message($uids, $from_mbox); | |
| 2636 } | |
| 2637 | |
| 2638 if ($moved) { | |
| 2639 // unset threads internal cache | |
| 2640 unset($this->icache['threads']); | |
| 2641 | |
| 2642 // remove message ids from search set | |
| 2643 if ($this->search_set && $from_mbox == $this->folder) { | |
| 2644 // threads are too complicated to just remove messages from set | |
| 2645 if ($this->search_threads || $all_mode) { | |
| 2646 $this->refresh_search(); | |
| 2647 } | |
| 2648 else if (!$this->search_set->incomplete) { | |
| 2649 $this->search_set->filter(explode(',', $uids), $this->folder); | |
| 2650 } | |
| 2651 } | |
| 2652 | |
| 2653 // remove cached messages | |
| 2654 // @TODO: do cache update instead of clearing it | |
| 2655 $this->clear_message_cache($from_mbox, $all_mode ? null : explode(',', $uids)); | |
| 2656 } | |
| 2657 | |
| 2658 return $moved; | |
| 2659 } | |
| 2660 | |
| 2661 /** | |
| 2662 * Copy a message from one folder to another | |
| 2663 * | |
| 2664 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2665 * @param string $to_mbox Target folder | |
| 2666 * @param string $from_mbox Source folder | |
| 2667 * | |
| 2668 * @return boolean True on success, False on error | |
| 2669 */ | |
| 2670 public function copy_message($uids, $to_mbox, $from_mbox='') | |
| 2671 { | |
| 2672 if (!strlen($from_mbox)) { | |
| 2673 $from_mbox = $this->folder; | |
| 2674 } | |
| 2675 | |
| 2676 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2677 | |
| 2678 // exit if no message uids are specified | |
| 2679 if (empty($uids)) { | |
| 2680 return false; | |
| 2681 } | |
| 2682 | |
| 2683 if (!$this->check_connection()) { | |
| 2684 return false; | |
| 2685 } | |
| 2686 | |
| 2687 // copy messages | |
| 2688 $copied = $this->conn->copy($uids, $from_mbox, $to_mbox); | |
| 2689 | |
| 2690 if ($copied) { | |
| 2691 $this->clear_messagecount($to_mbox); | |
| 2692 } | |
| 2693 | |
| 2694 return $copied; | |
| 2695 } | |
| 2696 | |
| 2697 /** | |
| 2698 * Mark messages as deleted and expunge them | |
| 2699 * | |
| 2700 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2701 * @param string $folder Source folder | |
| 2702 * | |
| 2703 * @return boolean True on success, False on error | |
| 2704 */ | |
| 2705 public function delete_message($uids, $folder='') | |
| 2706 { | |
| 2707 if (!strlen($folder)) { | |
| 2708 $folder = $this->folder; | |
| 2709 } | |
| 2710 | |
| 2711 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2712 | |
| 2713 // exit if no message uids are specified | |
| 2714 if (empty($uids)) { | |
| 2715 return false; | |
| 2716 } | |
| 2717 | |
| 2718 if (!$this->check_connection()) { | |
| 2719 return false; | |
| 2720 } | |
| 2721 | |
| 2722 $deleted = $this->conn->flag($folder, $uids, 'DELETED'); | |
| 2723 | |
| 2724 if ($deleted) { | |
| 2725 // send expunge command in order to have the deleted message | |
| 2726 // really deleted from the folder | |
| 2727 $this->expunge_message($uids, $folder, false); | |
| 2728 $this->clear_messagecount($folder); | |
| 2729 | |
| 2730 // unset threads internal cache | |
| 2731 unset($this->icache['threads']); | |
| 2732 | |
| 2733 $this->set_search_dirty($folder); | |
| 2734 | |
| 2735 // remove message ids from search set | |
| 2736 if ($this->search_set && $folder == $this->folder) { | |
| 2737 // threads are too complicated to just remove messages from set | |
| 2738 if ($this->search_threads || $all_mode) { | |
| 2739 $this->refresh_search(); | |
| 2740 } | |
| 2741 else if (!$this->search_set->incomplete) { | |
| 2742 $this->search_set->filter(explode(',', $uids)); | |
| 2743 } | |
| 2744 } | |
| 2745 | |
| 2746 // remove cached messages | |
| 2747 $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids)); | |
| 2748 } | |
| 2749 | |
| 2750 return $deleted; | |
| 2751 } | |
| 2752 | |
| 2753 /** | |
| 2754 * Send IMAP expunge command and clear cache | |
| 2755 * | |
| 2756 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2757 * @param string $folder Folder name | |
| 2758 * @param boolean $clear_cache False if cache should not be cleared | |
| 2759 * | |
| 2760 * @return boolean True on success, False on failure | |
| 2761 */ | |
| 2762 public function expunge_message($uids, $folder = null, $clear_cache = true) | |
| 2763 { | |
| 2764 if ($uids && $this->get_capability('UIDPLUS')) { | |
| 2765 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2766 } | |
| 2767 else { | |
| 2768 $uids = null; | |
| 2769 } | |
| 2770 | |
| 2771 if (!strlen($folder)) { | |
| 2772 $folder = $this->folder; | |
| 2773 } | |
| 2774 | |
| 2775 if (!$this->check_connection()) { | |
| 2776 return false; | |
| 2777 } | |
| 2778 | |
| 2779 // force folder selection and check if folder is writeable | |
| 2780 // to prevent a situation when CLOSE is executed on closed | |
| 2781 // or EXPUNGE on read-only folder | |
| 2782 $result = $this->conn->select($folder); | |
| 2783 if (!$result) { | |
| 2784 return false; | |
| 2785 } | |
| 2786 | |
| 2787 if (!$this->conn->data['READ-WRITE']) { | |
| 2788 $this->conn->setError(rcube_imap_generic::ERROR_READONLY, "Folder is read-only"); | |
| 2789 return false; | |
| 2790 } | |
| 2791 | |
| 2792 // CLOSE(+SELECT) should be faster than EXPUNGE | |
| 2793 if (empty($uids) || $all_mode) { | |
| 2794 $result = $this->conn->close(); | |
| 2795 } | |
| 2796 else { | |
| 2797 $result = $this->conn->expunge($folder, $uids); | |
| 2798 } | |
| 2799 | |
| 2800 if ($result && $clear_cache) { | |
| 2801 $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids)); | |
| 2802 $this->clear_messagecount($folder); | |
| 2803 } | |
| 2804 | |
| 2805 return $result; | |
| 2806 } | |
| 2807 | |
| 2808 | |
| 2809 /* -------------------------------- | |
| 2810 * folder management | |
| 2811 * --------------------------------*/ | |
| 2812 | |
| 2813 /** | |
| 2814 * Public method for listing subscribed folders. | |
| 2815 * | |
| 2816 * @param string $root Optional root folder | |
| 2817 * @param string $name Optional name pattern | |
| 2818 * @param string $filter Optional filter | |
| 2819 * @param string $rights Optional ACL requirements | |
| 2820 * @param bool $skip_sort Enable to return unsorted list (for better performance) | |
| 2821 * | |
| 2822 * @return array List of folders | |
| 2823 */ | |
| 2824 public function list_folders_subscribed($root='', $name='*', $filter=null, $rights=null, $skip_sort=false) | |
| 2825 { | |
| 2826 $cache_key = $root.':'.$name; | |
| 2827 if (!empty($filter)) { | |
| 2828 $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter)); | |
| 2829 } | |
| 2830 $cache_key .= ':'.$rights; | |
| 2831 $cache_key = 'mailboxes.'.md5($cache_key); | |
| 2832 | |
| 2833 // get cached folder list | |
| 2834 $a_mboxes = $this->get_cache($cache_key); | |
| 2835 if (is_array($a_mboxes)) { | |
| 2836 return $a_mboxes; | |
| 2837 } | |
| 2838 | |
| 2839 // Give plugins a chance to provide a list of folders | |
| 2840 $data = $this->plugins->exec_hook('storage_folders', | |
| 2841 array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB')); | |
| 2842 | |
| 2843 if (isset($data['folders'])) { | |
| 2844 $a_mboxes = $data['folders']; | |
| 2845 } | |
| 2846 else { | |
| 2847 $a_mboxes = $this->list_folders_subscribed_direct($root, $name); | |
| 2848 } | |
| 2849 | |
| 2850 if (!is_array($a_mboxes)) { | |
| 2851 return array(); | |
| 2852 } | |
| 2853 | |
| 2854 // filter folders list according to rights requirements | |
| 2855 if ($rights && $this->get_capability('ACL')) { | |
| 2856 $a_mboxes = $this->filter_rights($a_mboxes, $rights); | |
| 2857 } | |
| 2858 | |
| 2859 // INBOX should always be available | |
| 2860 if (in_array_nocase($root . $name, array('*', '%', 'INBOX', 'INBOX*')) | |
| 2861 && (!$filter || $filter == 'mail') && !in_array('INBOX', $a_mboxes) | |
| 2862 ) { | |
| 2863 array_unshift($a_mboxes, 'INBOX'); | |
| 2864 } | |
| 2865 | |
| 2866 // sort folders (always sort for cache) | |
| 2867 if (!$skip_sort || $this->cache) { | |
| 2868 $a_mboxes = $this->sort_folder_list($a_mboxes); | |
| 2869 } | |
| 2870 | |
| 2871 // write folders list to cache | |
| 2872 $this->update_cache($cache_key, $a_mboxes); | |
| 2873 | |
| 2874 return $a_mboxes; | |
| 2875 } | |
| 2876 | |
| 2877 /** | |
| 2878 * Method for direct folders listing (LSUB) | |
| 2879 * | |
| 2880 * @param string $root Optional root folder | |
| 2881 * @param string $name Optional name pattern | |
| 2882 * | |
| 2883 * @return array List of subscribed folders | |
| 2884 * @see rcube_imap::list_folders_subscribed() | |
| 2885 */ | |
| 2886 public function list_folders_subscribed_direct($root='', $name='*') | |
| 2887 { | |
| 2888 if (!$this->check_connection()) { | |
| 2889 return null; | |
| 2890 } | |
| 2891 | |
| 2892 $config = rcube::get_instance()->config; | |
| 2893 | |
| 2894 // Server supports LIST-EXTENDED, we can use selection options | |
| 2895 // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED | |
| 2896 $list_extended = !$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED'); | |
| 2897 if ($list_extended) { | |
| 2898 // This will also set folder options, LSUB doesn't do that | |
| 2899 $result = $this->conn->listMailboxes($root, $name, | |
| 2900 NULL, array('SUBSCRIBED')); | |
| 2901 } | |
| 2902 else { | |
| 2903 // retrieve list of folders from IMAP server using LSUB | |
| 2904 $result = $this->conn->listSubscribed($root, $name); | |
| 2905 } | |
| 2906 | |
| 2907 if (!is_array($result)) { | |
| 2908 return array(); | |
| 2909 } | |
| 2910 | |
| 2911 // #1486796: some server configurations doesn't return folders in all namespaces | |
| 2912 if ($root == '' && $name == '*' && $config->get('imap_force_ns')) { | |
| 2913 $this->list_folders_update($result, ($list_extended ? 'ext-' : '') . 'subscribed'); | |
| 2914 } | |
| 2915 | |
| 2916 // Remove hidden folders | |
| 2917 if ($config->get('imap_skip_hidden_folders')) { | |
| 2918 $result = array_filter($result, function($v) { return $v[0] != '.'; }); | |
| 2919 } | |
| 2920 | |
| 2921 if ($list_extended) { | |
| 2922 // unsubscribe non-existent folders, remove from the list | |
| 2923 if ($name == '*' && !empty($this->conn->data['LIST'])) { | |
| 2924 foreach ($result as $idx => $folder) { | |
| 2925 if (($opts = $this->conn->data['LIST'][$folder]) | |
| 2926 && in_array_nocase('\\NonExistent', $opts) | |
| 2927 ) { | |
| 2928 $this->conn->unsubscribe($folder); | |
| 2929 unset($result[$idx]); | |
| 2930 } | |
| 2931 } | |
| 2932 } | |
| 2933 } | |
| 2934 else { | |
| 2935 // unsubscribe non-existent folders, remove them from the list | |
| 2936 if (!empty($result) && $name == '*') { | |
| 2937 $existing = $this->list_folders($root, $name); | |
| 2938 $nonexisting = array_diff($result, $existing); | |
| 2939 $result = array_diff($result, $nonexisting); | |
| 2940 | |
| 2941 foreach ($nonexisting as $folder) { | |
| 2942 $this->conn->unsubscribe($folder); | |
| 2943 } | |
| 2944 } | |
| 2945 } | |
| 2946 | |
| 2947 return $result; | |
| 2948 } | |
| 2949 | |
| 2950 /** | |
| 2951 * Get a list of all folders available on the server | |
| 2952 * | |
| 2953 * @param string $root IMAP root dir | |
| 2954 * @param string $name Optional name pattern | |
| 2955 * @param mixed $filter Optional filter | |
| 2956 * @param string $rights Optional ACL requirements | |
| 2957 * @param bool $skip_sort Enable to return unsorted list (for better performance) | |
| 2958 * | |
| 2959 * @return array Indexed array with folder names | |
| 2960 */ | |
| 2961 public function list_folders($root='', $name='*', $filter=null, $rights=null, $skip_sort=false) | |
| 2962 { | |
| 2963 $cache_key = $root.':'.$name; | |
| 2964 if (!empty($filter)) { | |
| 2965 $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter)); | |
| 2966 } | |
| 2967 $cache_key .= ':'.$rights; | |
| 2968 $cache_key = 'mailboxes.list.'.md5($cache_key); | |
| 2969 | |
| 2970 // get cached folder list | |
| 2971 $a_mboxes = $this->get_cache($cache_key); | |
| 2972 if (is_array($a_mboxes)) { | |
| 2973 return $a_mboxes; | |
| 2974 } | |
| 2975 | |
| 2976 // Give plugins a chance to provide a list of folders | |
| 2977 $data = $this->plugins->exec_hook('storage_folders', | |
| 2978 array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LIST')); | |
| 2979 | |
| 2980 if (isset($data['folders'])) { | |
| 2981 $a_mboxes = $data['folders']; | |
| 2982 } | |
| 2983 else { | |
| 2984 // retrieve list of folders from IMAP server | |
| 2985 $a_mboxes = $this->list_folders_direct($root, $name); | |
| 2986 } | |
| 2987 | |
| 2988 if (!is_array($a_mboxes)) { | |
| 2989 $a_mboxes = array(); | |
| 2990 } | |
| 2991 | |
| 2992 // INBOX should always be available | |
| 2993 if (in_array_nocase($root . $name, array('*', '%', 'INBOX', 'INBOX*')) | |
| 2994 && (!$filter || $filter == 'mail') && !in_array('INBOX', $a_mboxes) | |
| 2995 ) { | |
| 2996 array_unshift($a_mboxes, 'INBOX'); | |
| 2997 } | |
| 2998 | |
| 2999 // cache folder attributes | |
| 3000 if ($root == '' && $name == '*' && empty($filter) && !empty($this->conn->data)) { | |
| 3001 $this->update_cache('mailboxes.attributes', $this->conn->data['LIST']); | |
| 3002 } | |
| 3003 | |
| 3004 // filter folders list according to rights requirements | |
| 3005 if ($rights && $this->get_capability('ACL')) { | |
| 3006 $a_mboxes = $this->filter_rights($a_mboxes, $rights); | |
| 3007 } | |
| 3008 | |
| 3009 // filter folders and sort them | |
| 3010 if (!$skip_sort) { | |
| 3011 $a_mboxes = $this->sort_folder_list($a_mboxes); | |
| 3012 } | |
| 3013 | |
| 3014 // write folders list to cache | |
| 3015 $this->update_cache($cache_key, $a_mboxes); | |
| 3016 | |
| 3017 return $a_mboxes; | |
| 3018 } | |
| 3019 | |
| 3020 /** | |
| 3021 * Method for direct folders listing (LIST) | |
| 3022 * | |
| 3023 * @param string $root Optional root folder | |
| 3024 * @param string $name Optional name pattern | |
| 3025 * | |
| 3026 * @return array List of folders | |
| 3027 * @see rcube_imap::list_folders() | |
| 3028 */ | |
| 3029 public function list_folders_direct($root='', $name='*') | |
| 3030 { | |
| 3031 if (!$this->check_connection()) { | |
| 3032 return null; | |
| 3033 } | |
| 3034 | |
| 3035 $result = $this->conn->listMailboxes($root, $name); | |
| 3036 | |
| 3037 if (!is_array($result)) { | |
| 3038 return array(); | |
| 3039 } | |
| 3040 | |
| 3041 $config = rcube::get_instance()->config; | |
| 3042 | |
| 3043 // #1486796: some server configurations doesn't return folders in all namespaces | |
| 3044 if ($root == '' && $name == '*' && $config->get('imap_force_ns')) { | |
| 3045 $this->list_folders_update($result); | |
| 3046 } | |
| 3047 | |
| 3048 // Remove hidden folders | |
| 3049 if ($config->get('imap_skip_hidden_folders')) { | |
| 3050 $result = array_filter($result, function($v) { return $v[0] != '.'; }); | |
| 3051 } | |
| 3052 | |
| 3053 return $result; | |
| 3054 } | |
| 3055 | |
| 3056 /** | |
| 3057 * Fix folders list by adding folders from other namespaces. | |
| 3058 * Needed on some servers eg. Courier IMAP | |
| 3059 * | |
| 3060 * @param array $result Reference to folders list | |
| 3061 * @param string $type Listing type (ext-subscribed, subscribed or all) | |
| 3062 */ | |
| 3063 protected function list_folders_update(&$result, $type = null) | |
| 3064 { | |
| 3065 $namespace = $this->get_namespace(); | |
| 3066 $search = array(); | |
| 3067 | |
| 3068 // build list of namespace prefixes | |
| 3069 foreach ((array)$namespace as $ns) { | |
| 3070 if (is_array($ns)) { | |
| 3071 foreach ($ns as $ns_data) { | |
| 3072 if (strlen($ns_data[0])) { | |
| 3073 $search[] = $ns_data[0]; | |
| 3074 } | |
| 3075 } | |
| 3076 } | |
| 3077 } | |
| 3078 | |
| 3079 if (!empty($search)) { | |
| 3080 // go through all folders detecting namespace usage | |
| 3081 foreach ($result as $folder) { | |
| 3082 foreach ($search as $idx => $prefix) { | |
| 3083 if (strpos($folder, $prefix) === 0) { | |
| 3084 unset($search[$idx]); | |
| 3085 } | |
| 3086 } | |
| 3087 if (empty($search)) { | |
| 3088 break; | |
| 3089 } | |
| 3090 } | |
| 3091 | |
| 3092 // get folders in hidden namespaces and add to the result | |
| 3093 foreach ($search as $prefix) { | |
| 3094 if ($type == 'ext-subscribed') { | |
| 3095 $list = $this->conn->listMailboxes('', $prefix . '*', null, array('SUBSCRIBED')); | |
| 3096 } | |
| 3097 else if ($type == 'subscribed') { | |
| 3098 $list = $this->conn->listSubscribed('', $prefix . '*'); | |
| 3099 } | |
| 3100 else { | |
| 3101 $list = $this->conn->listMailboxes('', $prefix . '*'); | |
| 3102 } | |
| 3103 | |
| 3104 if (!empty($list)) { | |
| 3105 $result = array_merge($result, $list); | |
| 3106 } | |
| 3107 } | |
| 3108 } | |
| 3109 } | |
| 3110 | |
| 3111 /** | |
| 3112 * Filter the given list of folders according to access rights | |
| 3113 * | |
| 3114 * For performance reasons we assume user has full rights | |
| 3115 * on all personal folders. | |
| 3116 */ | |
| 3117 protected function filter_rights($a_folders, $rights) | |
| 3118 { | |
| 3119 $regex = '/('.$rights.')/'; | |
| 3120 | |
| 3121 foreach ($a_folders as $idx => $folder) { | |
| 3122 if ($this->folder_namespace($folder) == 'personal') { | |
| 3123 continue; | |
| 3124 } | |
| 3125 | |
| 3126 $myrights = join('', (array)$this->my_rights($folder)); | |
| 3127 | |
| 3128 if ($myrights !== null && !preg_match($regex, $myrights)) { | |
| 3129 unset($a_folders[$idx]); | |
| 3130 } | |
| 3131 } | |
| 3132 | |
| 3133 return $a_folders; | |
| 3134 } | |
| 3135 | |
| 3136 /** | |
| 3137 * Get mailbox quota information | |
| 3138 * | |
| 3139 * @param string $folder Folder name | |
| 3140 * | |
| 3141 * @return mixed Quota info or False if not supported | |
| 3142 */ | |
| 3143 public function get_quota($folder = null) | |
| 3144 { | |
| 3145 if ($this->get_capability('QUOTA') && $this->check_connection()) { | |
| 3146 return $this->conn->getQuota($folder); | |
| 3147 } | |
| 3148 | |
| 3149 return false; | |
| 3150 } | |
| 3151 | |
| 3152 /** | |
| 3153 * Get folder size (size of all messages in a folder) | |
| 3154 * | |
| 3155 * @param string $folder Folder name | |
| 3156 * | |
| 3157 * @return int Folder size in bytes, False on error | |
| 3158 */ | |
| 3159 public function folder_size($folder) | |
| 3160 { | |
| 3161 if (!strlen($folder)) { | |
| 3162 return false; | |
| 3163 } | |
| 3164 | |
| 3165 if (!$this->check_connection()) { | |
| 3166 return 0; | |
| 3167 } | |
| 3168 | |
| 3169 // On Cyrus we can use special folder annotation, which should be much faster | |
| 3170 if ($this->get_vendor() == 'cyrus') { | |
| 3171 $idx = '/shared/vendor/cmu/cyrus-imapd/size'; | |
| 3172 $result = $this->get_metadata($folder, $idx, array(), true); | |
| 3173 | |
| 3174 if (!empty($result) && is_numeric($result[$folder][$idx])) { | |
| 3175 return $result[$folder][$idx]; | |
| 3176 } | |
| 3177 } | |
| 3178 | |
| 3179 // @TODO: could we try to use QUOTA here? | |
| 3180 $result = $this->conn->fetchHeaderIndex($folder, '1:*', 'SIZE', false); | |
| 3181 | |
| 3182 if (is_array($result)) { | |
| 3183 $result = array_sum($result); | |
| 3184 } | |
| 3185 | |
| 3186 return $result; | |
| 3187 } | |
| 3188 | |
| 3189 /** | |
| 3190 * Subscribe to a specific folder(s) | |
| 3191 * | |
| 3192 * @param array $folders Folder name(s) | |
| 3193 * | |
| 3194 * @return boolean True on success | |
| 3195 */ | |
| 3196 public function subscribe($folders) | |
| 3197 { | |
| 3198 // let this common function do the main work | |
| 3199 return $this->change_subscription($folders, 'subscribe'); | |
| 3200 } | |
| 3201 | |
| 3202 /** | |
| 3203 * Unsubscribe folder(s) | |
| 3204 * | |
| 3205 * @param array $a_mboxes Folder name(s) | |
| 3206 * | |
| 3207 * @return boolean True on success | |
| 3208 */ | |
| 3209 public function unsubscribe($folders) | |
| 3210 { | |
| 3211 // let this common function do the main work | |
| 3212 return $this->change_subscription($folders, 'unsubscribe'); | |
| 3213 } | |
| 3214 | |
| 3215 /** | |
| 3216 * Create a new folder on the server and register it in local cache | |
| 3217 * | |
| 3218 * @param string $folder New folder name | |
| 3219 * @param boolean $subscribe True if the new folder should be subscribed | |
| 3220 * @param string $type Optional folder type (junk, trash, drafts, sent, archive) | |
| 3221 * | |
| 3222 * @return boolean True on success | |
| 3223 */ | |
| 3224 public function create_folder($folder, $subscribe = false, $type = null) | |
| 3225 { | |
| 3226 if (!$this->check_connection()) { | |
| 3227 return false; | |
| 3228 } | |
| 3229 | |
| 3230 $result = $this->conn->createFolder($folder, $type ? array("\\" . ucfirst($type)) : null); | |
| 3231 | |
| 3232 // try to subscribe it | |
| 3233 if ($result) { | |
| 3234 // clear cache | |
| 3235 $this->clear_cache('mailboxes', true); | |
| 3236 | |
| 3237 if ($subscribe) { | |
| 3238 $this->subscribe($folder); | |
| 3239 } | |
| 3240 } | |
| 3241 | |
| 3242 return $result; | |
| 3243 } | |
| 3244 | |
| 3245 /** | |
| 3246 * Set a new name to an existing folder | |
| 3247 * | |
| 3248 * @param string $folder Folder to rename | |
| 3249 * @param string $new_name New folder name | |
| 3250 * | |
| 3251 * @return boolean True on success | |
| 3252 */ | |
| 3253 public function rename_folder($folder, $new_name) | |
| 3254 { | |
| 3255 if (!strlen($new_name)) { | |
| 3256 return false; | |
| 3257 } | |
| 3258 | |
| 3259 if (!$this->check_connection()) { | |
| 3260 return false; | |
| 3261 } | |
| 3262 | |
| 3263 $delm = $this->get_hierarchy_delimiter(); | |
| 3264 | |
| 3265 // get list of subscribed folders | |
| 3266 if ((strpos($folder, '%') === false) && (strpos($folder, '*') === false)) { | |
| 3267 $a_subscribed = $this->list_folders_subscribed('', $folder . $delm . '*'); | |
| 3268 $subscribed = $this->folder_exists($folder, true); | |
| 3269 } | |
| 3270 else { | |
| 3271 $a_subscribed = $this->list_folders_subscribed(); | |
| 3272 $subscribed = in_array($folder, $a_subscribed); | |
| 3273 } | |
| 3274 | |
| 3275 $result = $this->conn->renameFolder($folder, $new_name); | |
| 3276 | |
| 3277 if ($result) { | |
| 3278 // unsubscribe the old folder, subscribe the new one | |
| 3279 if ($subscribed) { | |
| 3280 $this->conn->unsubscribe($folder); | |
| 3281 $this->conn->subscribe($new_name); | |
| 3282 } | |
| 3283 | |
| 3284 // check if folder children are subscribed | |
| 3285 foreach ($a_subscribed as $c_subscribed) { | |
| 3286 if (strpos($c_subscribed, $folder.$delm) === 0) { | |
| 3287 $this->conn->unsubscribe($c_subscribed); | |
| 3288 $this->conn->subscribe(preg_replace('/^'.preg_quote($folder, '/').'/', | |
| 3289 $new_name, $c_subscribed)); | |
| 3290 | |
| 3291 // clear cache | |
| 3292 $this->clear_message_cache($c_subscribed); | |
| 3293 } | |
| 3294 } | |
| 3295 | |
| 3296 // clear cache | |
| 3297 $this->clear_message_cache($folder); | |
| 3298 $this->clear_cache('mailboxes', true); | |
| 3299 } | |
| 3300 | |
| 3301 return $result; | |
| 3302 } | |
| 3303 | |
| 3304 /** | |
| 3305 * Remove folder (with subfolders) from the server | |
| 3306 * | |
| 3307 * @param string $folder Folder name | |
| 3308 * | |
| 3309 * @return boolean True on success, False on failure | |
| 3310 */ | |
| 3311 function delete_folder($folder) | |
| 3312 { | |
| 3313 if (!$this->check_connection()) { | |
| 3314 return false; | |
| 3315 } | |
| 3316 | |
| 3317 $delm = $this->get_hierarchy_delimiter(); | |
| 3318 | |
| 3319 // get list of sub-folders or all folders | |
| 3320 // if folder name contains special characters | |
| 3321 $path = strspn($folder, '%*') > 0 ? ($folder . $delm) : ''; | |
| 3322 $sub_mboxes = $this->list_folders('', $path . '*'); | |
| 3323 | |
| 3324 // According to RFC3501 deleting a \Noselect folder | |
| 3325 // with subfolders may fail. To workaround this we delete | |
| 3326 // subfolders first (in reverse order) (#5466) | |
| 3327 if (!empty($sub_mboxes)) { | |
| 3328 foreach (array_reverse($sub_mboxes) as $mbox) { | |
| 3329 if (strpos($mbox, $folder . $delm) === 0) { | |
| 3330 if ($this->conn->deleteFolder($mbox)) { | |
| 3331 $this->conn->unsubscribe($mbox); | |
| 3332 $this->clear_message_cache($mbox); | |
| 3333 } | |
| 3334 } | |
| 3335 } | |
| 3336 } | |
| 3337 | |
| 3338 // delete the folder | |
| 3339 if ($result = $this->conn->deleteFolder($folder)) { | |
| 3340 // and unsubscribe it | |
| 3341 $this->conn->unsubscribe($folder); | |
| 3342 $this->clear_message_cache($folder); | |
| 3343 } | |
| 3344 | |
| 3345 $this->clear_cache('mailboxes', true); | |
| 3346 | |
| 3347 return $result; | |
| 3348 } | |
| 3349 | |
| 3350 /** | |
| 3351 * Detect special folder associations stored in storage backend | |
| 3352 */ | |
| 3353 public function get_special_folders($forced = false) | |
| 3354 { | |
| 3355 $result = parent::get_special_folders(); | |
| 3356 $rcube = rcube::get_instance(); | |
| 3357 | |
| 3358 // Lock SPECIAL-USE after user preferences change (#4782) | |
| 3359 if ($rcube->config->get('lock_special_folders')) { | |
| 3360 return $result; | |
| 3361 } | |
| 3362 | |
| 3363 if (isset($this->icache['special-use'])) { | |
| 3364 return array_merge($result, $this->icache['special-use']); | |
| 3365 } | |
| 3366 | |
| 3367 if (!$forced || !$this->get_capability('SPECIAL-USE')) { | |
| 3368 return $result; | |
| 3369 } | |
| 3370 | |
| 3371 if (!$this->check_connection()) { | |
| 3372 return $result; | |
| 3373 } | |
| 3374 | |
| 3375 $types = array_map(function($value) { return "\\" . ucfirst($value); }, rcube_storage::$folder_types); | |
| 3376 $special = array(); | |
| 3377 | |
| 3378 // request \Subscribed flag in LIST response as performance improvement for folder_exists() | |
| 3379 $folders = $this->conn->listMailboxes('', '*', array('SUBSCRIBED'), array('SPECIAL-USE')); | |
| 3380 | |
| 3381 if (!empty($folders)) { | |
| 3382 foreach ($folders as $folder) { | |
| 3383 if ($flags = $this->conn->data['LIST'][$folder]) { | |
| 3384 foreach ($types as $type) { | |
| 3385 if (in_array($type, $flags)) { | |
| 3386 $type = strtolower(substr($type, 1)); | |
| 3387 $special[$type] = $folder; | |
| 3388 } | |
| 3389 } | |
| 3390 } | |
| 3391 } | |
| 3392 } | |
| 3393 | |
| 3394 $this->icache['special-use'] = $special; | |
| 3395 unset($this->icache['special-folders']); | |
| 3396 | |
| 3397 return array_merge($result, $special); | |
| 3398 } | |
| 3399 | |
| 3400 /** | |
| 3401 * Set special folder associations stored in storage backend | |
| 3402 */ | |
| 3403 public function set_special_folders($specials) | |
| 3404 { | |
| 3405 if (!$this->get_capability('SPECIAL-USE') || !$this->get_capability('METADATA')) { | |
| 3406 return false; | |
| 3407 } | |
| 3408 | |
| 3409 if (!$this->check_connection()) { | |
| 3410 return false; | |
| 3411 } | |
| 3412 | |
| 3413 $folders = $this->get_special_folders(true); | |
| 3414 $old = (array) $this->icache['special-use']; | |
| 3415 | |
| 3416 foreach ($specials as $type => $folder) { | |
| 3417 if (in_array($type, rcube_storage::$folder_types)) { | |
| 3418 $old_folder = $old[$type]; | |
| 3419 if ($old_folder !== $folder) { | |
| 3420 // unset old-folder metadata | |
| 3421 if ($old_folder !== null) { | |
| 3422 $this->delete_metadata($old_folder, array('/private/specialuse')); | |
| 3423 } | |
| 3424 // set new folder metadata | |
| 3425 if ($folder) { | |
| 3426 $this->set_metadata($folder, array('/private/specialuse' => "\\" . ucfirst($type))); | |
| 3427 } | |
| 3428 } | |
| 3429 } | |
| 3430 } | |
| 3431 | |
| 3432 $this->icache['special-use'] = $specials; | |
| 3433 unset($this->icache['special-folders']); | |
| 3434 | |
| 3435 return true; | |
| 3436 } | |
| 3437 | |
| 3438 /** | |
| 3439 * Checks if folder exists and is subscribed | |
| 3440 * | |
| 3441 * @param string $folder Folder name | |
| 3442 * @param boolean $subscription Enable subscription checking | |
| 3443 * | |
| 3444 * @return boolean TRUE or FALSE | |
| 3445 */ | |
| 3446 public function folder_exists($folder, $subscription = false) | |
| 3447 { | |
| 3448 if ($folder == 'INBOX') { | |
| 3449 return true; | |
| 3450 } | |
| 3451 | |
| 3452 $key = $subscription ? 'subscribed' : 'existing'; | |
| 3453 | |
| 22 | 3454 if (is_array($this->icache[$key]??null) && in_array($folder, $this->icache[$key])) { |
| 0 | 3455 return true; |
| 3456 } | |
| 3457 | |
| 3458 if (!$this->check_connection()) { | |
| 3459 return false; | |
| 3460 } | |
| 3461 | |
| 3462 if ($subscription) { | |
| 3463 // It's possible we already called LIST command, check LIST data | |
| 3464 if (!empty($this->conn->data['LIST']) && !empty($this->conn->data['LIST'][$folder]) | |
| 3465 && in_array_nocase('\\Subscribed', $this->conn->data['LIST'][$folder]) | |
| 3466 ) { | |
| 3467 $a_folders = array($folder); | |
| 3468 } | |
| 3469 else { | |
| 3470 $a_folders = $this->conn->listSubscribed('', $folder); | |
| 3471 } | |
| 3472 } | |
| 3473 else { | |
| 3474 // It's possible we already called LIST command, check LIST data | |
| 3475 if (!empty($this->conn->data['LIST']) && isset($this->conn->data['LIST'][$folder])) { | |
| 3476 $a_folders = array($folder); | |
| 3477 } | |
| 3478 else { | |
| 3479 $a_folders = $this->conn->listMailboxes('', $folder); | |
| 3480 } | |
| 3481 } | |
| 3482 | |
| 3483 if (is_array($a_folders) && in_array($folder, $a_folders)) { | |
| 3484 $this->icache[$key][] = $folder; | |
| 3485 return true; | |
| 3486 } | |
| 3487 | |
| 3488 return false; | |
| 3489 } | |
| 3490 | |
| 3491 /** | |
| 3492 * Returns the namespace where the folder is in | |
| 3493 * | |
| 3494 * @param string $folder Folder name | |
| 3495 * | |
| 3496 * @return string One of 'personal', 'other' or 'shared' | |
| 3497 */ | |
| 3498 public function folder_namespace($folder) | |
| 3499 { | |
| 3500 if ($folder == 'INBOX') { | |
| 3501 return 'personal'; | |
| 3502 } | |
| 3503 | |
| 3504 foreach ($this->namespace as $type => $namespace) { | |
| 3505 if (is_array($namespace)) { | |
| 3506 foreach ($namespace as $ns) { | |
| 3507 if ($len = strlen($ns[0])) { | |
| 3508 if (($len > 1 && $folder == substr($ns[0], 0, -1)) | |
| 3509 || strpos($folder, $ns[0]) === 0 | |
| 3510 ) { | |
| 3511 return $type; | |
| 3512 } | |
| 3513 } | |
| 3514 } | |
| 3515 } | |
| 3516 } | |
| 3517 | |
| 3518 return 'personal'; | |
| 3519 } | |
| 3520 | |
| 3521 /** | |
| 3522 * Modify folder name according to personal namespace prefix. | |
| 3523 * For output it removes prefix of the personal namespace if it's possible. | |
| 3524 * For input it adds the prefix. Use it before creating a folder in root | |
| 3525 * of the folders tree. | |
| 3526 * | |
| 3527 * @param string $folder Folder name | |
| 3528 * @param string $mode Mode name (out/in) | |
| 3529 * | |
| 3530 * @return string Folder name | |
| 3531 */ | |
| 3532 public function mod_folder($folder, $mode = 'out') | |
| 3533 { | |
| 3534 $prefix = $this->namespace['prefix_' . $mode]; // see set_env() | |
| 3535 | |
| 3536 if ($prefix === null || $prefix === '' | |
| 3537 || !($prefix_len = strlen($prefix)) || !strlen($folder) | |
| 3538 ) { | |
| 3539 return $folder; | |
| 3540 } | |
| 3541 | |
| 3542 // remove prefix for output | |
| 3543 if ($mode == 'out') { | |
| 3544 if (substr($folder, 0, $prefix_len) === $prefix) { | |
| 3545 return substr($folder, $prefix_len); | |
| 3546 } | |
| 3547 | |
| 3548 return $folder; | |
| 3549 } | |
| 3550 | |
| 3551 // add prefix for input (e.g. folder creation) | |
| 3552 return $prefix . $folder; | |
| 3553 } | |
| 3554 | |
| 3555 /** | |
| 3556 * Gets folder attributes from LIST response, e.g. \Noselect, \Noinferiors | |
| 3557 * | |
| 3558 * @param string $folder Folder name | |
| 3559 * @param bool $force Set to True if attributes should be refreshed | |
| 3560 * | |
| 3561 * @return array Options list | |
| 3562 */ | |
| 3563 public function folder_attributes($folder, $force=false) | |
| 3564 { | |
| 3565 // get attributes directly from LIST command | |
| 3566 if (!empty($this->conn->data['LIST']) && is_array($this->conn->data['LIST'][$folder])) { | |
| 3567 $opts = $this->conn->data['LIST'][$folder]; | |
| 3568 } | |
| 3569 // get cached folder attributes | |
| 3570 else if (!$force) { | |
| 3571 $opts = $this->get_cache('mailboxes.attributes'); | |
| 3572 $opts = $opts[$folder]; | |
| 3573 } | |
| 3574 | |
| 3575 if (!is_array($opts)) { | |
| 3576 if (!$this->check_connection()) { | |
| 3577 return array(); | |
| 3578 } | |
| 3579 | |
| 3580 $this->conn->listMailboxes('', $folder); | |
| 3581 $opts = $this->conn->data['LIST'][$folder]; | |
| 3582 } | |
| 3583 | |
| 3584 return is_array($opts) ? $opts : array(); | |
| 3585 } | |
| 3586 | |
| 3587 /** | |
| 3588 * Gets connection (and current folder) data: UIDVALIDITY, EXISTS, RECENT, | |
| 3589 * PERMANENTFLAGS, UIDNEXT, UNSEEN | |
| 3590 * | |
| 3591 * @param string $folder Folder name | |
| 3592 * | |
| 3593 * @return array Data | |
| 3594 */ | |
| 3595 public function folder_data($folder) | |
| 3596 { | |
| 3597 if (!strlen($folder)) { | |
| 3598 $folder = $this->folder !== null ? $this->folder : 'INBOX'; | |
| 3599 } | |
| 3600 | |
| 3601 if ($this->conn->selected != $folder) { | |
| 3602 if (!$this->check_connection()) { | |
| 3603 return array(); | |
| 3604 } | |
| 3605 | |
| 3606 if ($this->conn->select($folder)) { | |
| 3607 $this->folder = $folder; | |
| 3608 } | |
| 3609 else { | |
| 3610 return null; | |
| 3611 } | |
| 3612 } | |
| 3613 | |
| 3614 $data = $this->conn->data; | |
| 3615 | |
| 3616 // add (E)SEARCH result for ALL UNDELETED query | |
| 3617 if (!empty($this->icache['undeleted_idx']) | |
| 3618 && $this->icache['undeleted_idx']->get_parameters('MAILBOX') == $folder | |
| 3619 ) { | |
| 3620 $data['UNDELETED'] = $this->icache['undeleted_idx']; | |
| 3621 } | |
| 3622 | |
| 3623 return $data; | |
| 3624 } | |
| 3625 | |
| 3626 /** | |
| 3627 * Returns extended information about the folder | |
| 3628 * | |
| 3629 * @param string $folder Folder name | |
| 3630 * | |
| 3631 * @return array Data | |
| 3632 */ | |
| 3633 public function folder_info($folder) | |
| 3634 { | |
| 3635 if ($this->icache['options'] && $this->icache['options']['name'] == $folder) { | |
| 3636 return $this->icache['options']; | |
| 3637 } | |
| 3638 | |
| 3639 // get cached metadata | |
| 3640 $cache_key = 'mailboxes.folder-info.' . $folder; | |
| 3641 $cached = $this->get_cache($cache_key); | |
| 3642 | |
| 3643 if (is_array($cached)) { | |
| 3644 return $cached; | |
| 3645 } | |
| 3646 | |
| 3647 $acl = $this->get_capability('ACL'); | |
| 3648 $namespace = $this->get_namespace(); | |
| 3649 $options = array(); | |
| 3650 | |
| 3651 // check if the folder is a namespace prefix | |
| 3652 if (!empty($namespace)) { | |
| 3653 $mbox = $folder . $this->delimiter; | |
| 3654 foreach ($namespace as $ns) { | |
| 3655 if (!empty($ns)) { | |
| 3656 foreach ($ns as $item) { | |
| 3657 if ($item[0] === $mbox) { | |
| 3658 $options['is_root'] = true; | |
| 3659 break 2; | |
| 3660 } | |
| 3661 } | |
| 3662 } | |
| 3663 } | |
| 3664 } | |
| 3665 // check if the folder is other user virtual-root | |
| 3666 if (!$options['is_root'] && !empty($namespace) && !empty($namespace['other'])) { | |
| 3667 $parts = explode($this->delimiter, $folder); | |
| 3668 if (count($parts) == 2) { | |
| 3669 $mbox = $parts[0] . $this->delimiter; | |
| 3670 foreach ($namespace['other'] as $item) { | |
| 3671 if ($item[0] === $mbox) { | |
| 3672 $options['is_root'] = true; | |
| 3673 break; | |
| 3674 } | |
| 3675 } | |
| 3676 } | |
| 3677 } | |
| 3678 | |
| 3679 $options['name'] = $folder; | |
| 3680 $options['attributes'] = $this->folder_attributes($folder, true); | |
| 3681 $options['namespace'] = $this->folder_namespace($folder); | |
| 3682 $options['special'] = $this->is_special_folder($folder); | |
| 3683 | |
| 3684 // Set 'noselect' flag | |
| 3685 if (is_array($options['attributes'])) { | |
| 3686 foreach ($options['attributes'] as $attrib) { | |
| 3687 $attrib = strtolower($attrib); | |
| 3688 if ($attrib == '\noselect' || $attrib == '\nonexistent') { | |
| 3689 $options['noselect'] = true; | |
| 3690 } | |
| 3691 } | |
| 3692 } | |
| 3693 else { | |
| 3694 $options['noselect'] = true; | |
| 3695 } | |
| 3696 | |
| 3697 // Get folder rights (MYRIGHTS) | |
| 3698 if ($acl && ($rights = $this->my_rights($folder))) { | |
| 3699 $options['rights'] = $rights; | |
| 3700 } | |
| 3701 | |
| 3702 // Set 'norename' flag | |
| 3703 if (!empty($options['rights'])) { | |
| 3704 $options['norename'] = !in_array('x', $options['rights']) && !in_array('d', $options['rights']); | |
| 3705 | |
| 3706 if (!$options['noselect']) { | |
| 3707 $options['noselect'] = !in_array('r', $options['rights']); | |
| 3708 } | |
| 3709 } | |
| 3710 else { | |
| 3711 $options['norename'] = $options['is_root'] || $options['namespace'] != 'personal'; | |
| 3712 } | |
| 3713 | |
| 3714 // update caches | |
| 3715 $this->icache['options'] = $options; | |
| 3716 $this->update_cache($cache_key, $options); | |
| 3717 | |
| 3718 return $options; | |
| 3719 } | |
| 3720 | |
| 3721 /** | |
| 3722 * Synchronizes messages cache. | |
| 3723 * | |
| 3724 * @param string $folder Folder name | |
| 3725 */ | |
| 3726 public function folder_sync($folder) | |
| 3727 { | |
| 3728 if ($mcache = $this->get_mcache_engine()) { | |
| 3729 $mcache->synchronize($folder); | |
| 3730 } | |
| 3731 } | |
| 3732 | |
| 3733 /** | |
| 3734 * Get message header names for rcube_imap_generic::fetchHeader(s) | |
| 3735 * | |
| 3736 * @return string Space-separated list of header names | |
| 3737 */ | |
| 3738 protected function get_fetch_headers() | |
| 3739 { | |
| 3740 if (!empty($this->options['fetch_headers'])) { | |
| 3741 $headers = explode(' ', $this->options['fetch_headers']); | |
| 3742 } | |
| 3743 else { | |
| 3744 $headers = array(); | |
| 3745 } | |
| 3746 | |
| 19 | 3747 if ($this->messages_caching || !empty($this->options['all_headers'])) { |
| 0 | 3748 $headers = array_merge($headers, $this->all_headers); |
| 3749 } | |
| 3750 | |
| 3751 return $headers; | |
| 3752 } | |
| 3753 | |
| 3754 | |
| 3755 /* ----------------------------------------- | |
| 3756 * ACL and METADATA/ANNOTATEMORE methods | |
| 3757 * ----------------------------------------*/ | |
| 3758 | |
| 3759 /** | |
| 3760 * Changes the ACL on the specified folder (SETACL) | |
| 3761 * | |
| 3762 * @param string $folder Folder name | |
| 3763 * @param string $user User name | |
| 3764 * @param string $acl ACL string | |
| 3765 * | |
| 3766 * @return boolean True on success, False on failure | |
| 3767 * @since 0.5-beta | |
| 3768 */ | |
| 3769 public function set_acl($folder, $user, $acl) | |
| 3770 { | |
| 3771 if (!$this->get_capability('ACL')) { | |
| 3772 return false; | |
| 3773 } | |
| 3774 | |
| 3775 if (!$this->check_connection()) { | |
| 3776 return false; | |
| 3777 } | |
| 3778 | |
| 3779 $this->clear_cache('mailboxes.folder-info.' . $folder); | |
| 3780 | |
| 3781 return $this->conn->setACL($folder, $user, $acl); | |
| 3782 } | |
| 3783 | |
| 3784 /** | |
| 3785 * Removes any <identifier,rights> pair for the | |
| 3786 * specified user from the ACL for the specified | |
| 3787 * folder (DELETEACL) | |
| 3788 * | |
| 3789 * @param string $folder Folder name | |
| 3790 * @param string $user User name | |
| 3791 * | |
| 3792 * @return boolean True on success, False on failure | |
| 3793 * @since 0.5-beta | |
| 3794 */ | |
| 3795 public function delete_acl($folder, $user) | |
| 3796 { | |
| 3797 if (!$this->get_capability('ACL')) { | |
| 3798 return false; | |
| 3799 } | |
| 3800 | |
| 3801 if (!$this->check_connection()) { | |
| 3802 return false; | |
| 3803 } | |
| 3804 | |
| 3805 return $this->conn->deleteACL($folder, $user); | |
| 3806 } | |
| 3807 | |
| 3808 /** | |
| 3809 * Returns the access control list for folder (GETACL) | |
| 3810 * | |
| 3811 * @param string $folder Folder name | |
| 3812 * | |
| 3813 * @return array User-rights array on success, NULL on error | |
| 3814 * @since 0.5-beta | |
| 3815 */ | |
| 3816 public function get_acl($folder) | |
| 3817 { | |
| 3818 if (!$this->get_capability('ACL')) { | |
| 3819 return null; | |
| 3820 } | |
| 3821 | |
| 3822 if (!$this->check_connection()) { | |
| 3823 return null; | |
| 3824 } | |
| 3825 | |
| 3826 return $this->conn->getACL($folder); | |
| 3827 } | |
| 3828 | |
| 3829 /** | |
| 3830 * Returns information about what rights can be granted to the | |
| 3831 * user (identifier) in the ACL for the folder (LISTRIGHTS) | |
| 3832 * | |
| 3833 * @param string $folder Folder name | |
| 3834 * @param string $user User name | |
| 3835 * | |
| 3836 * @return array List of user rights | |
| 3837 * @since 0.5-beta | |
| 3838 */ | |
| 3839 public function list_rights($folder, $user) | |
| 3840 { | |
| 3841 if (!$this->get_capability('ACL')) { | |
| 3842 return null; | |
| 3843 } | |
| 3844 | |
| 3845 if (!$this->check_connection()) { | |
| 3846 return null; | |
| 3847 } | |
| 3848 | |
| 3849 return $this->conn->listRights($folder, $user); | |
| 3850 } | |
| 3851 | |
| 3852 /** | |
| 3853 * Returns the set of rights that the current user has to | |
| 3854 * folder (MYRIGHTS) | |
| 3855 * | |
| 3856 * @param string $folder Folder name | |
| 3857 * | |
| 3858 * @return array MYRIGHTS response on success, NULL on error | |
| 3859 * @since 0.5-beta | |
| 3860 */ | |
| 3861 public function my_rights($folder) | |
| 3862 { | |
| 3863 if (!$this->get_capability('ACL')) { | |
| 3864 return null; | |
| 3865 } | |
| 3866 | |
| 3867 if (!$this->check_connection()) { | |
| 3868 return null; | |
| 3869 } | |
| 3870 | |
| 3871 return $this->conn->myRights($folder); | |
| 3872 } | |
| 3873 | |
| 3874 /** | |
| 3875 * Sets IMAP metadata/annotations (SETMETADATA/SETANNOTATION) | |
| 3876 * | |
| 3877 * @param string $folder Folder name (empty for server metadata) | |
| 3878 * @param array $entries Entry-value array (use NULL value as NIL) | |
| 3879 * | |
| 3880 * @return boolean True on success, False on failure | |
| 3881 * @since 0.5-beta | |
| 3882 */ | |
| 3883 public function set_metadata($folder, $entries) | |
| 3884 { | |
| 3885 if (!$this->check_connection()) { | |
| 3886 return false; | |
| 3887 } | |
| 3888 | |
| 3889 $this->clear_cache('mailboxes.metadata.', true); | |
| 3890 | |
| 3891 if ($this->get_capability('METADATA') || | |
| 3892 (!strlen($folder) && $this->get_capability('METADATA-SERVER')) | |
| 3893 ) { | |
| 3894 return $this->conn->setMetadata($folder, $entries); | |
| 3895 } | |
| 3896 else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { | |
| 3897 foreach ((array)$entries as $entry => $value) { | |
| 3898 list($ent, $attr) = $this->md2annotate($entry); | |
| 3899 $entries[$entry] = array($ent, $attr, $value); | |
| 3900 } | |
| 3901 return $this->conn->setAnnotation($folder, $entries); | |
| 3902 } | |
| 3903 | |
| 3904 return false; | |
| 3905 } | |
| 3906 | |
| 3907 /** | |
| 3908 * Unsets IMAP metadata/annotations (SETMETADATA/SETANNOTATION) | |
| 3909 * | |
| 3910 * @param string $folder Folder name (empty for server metadata) | |
| 3911 * @param array $entries Entry names array | |
| 3912 * | |
| 3913 * @return boolean True on success, False on failure | |
| 3914 * @since 0.5-beta | |
| 3915 */ | |
| 3916 public function delete_metadata($folder, $entries) | |
| 3917 { | |
| 3918 if (!$this->check_connection()) { | |
| 3919 return false; | |
| 3920 } | |
| 3921 | |
| 3922 $this->clear_cache('mailboxes.metadata.', true); | |
| 3923 | |
| 3924 if ($this->get_capability('METADATA') || | |
| 3925 (!strlen($folder) && $this->get_capability('METADATA-SERVER')) | |
| 3926 ) { | |
| 3927 return $this->conn->deleteMetadata($folder, $entries); | |
| 3928 } | |
| 3929 else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { | |
| 3930 foreach ((array)$entries as $idx => $entry) { | |
| 3931 list($ent, $attr) = $this->md2annotate($entry); | |
| 3932 $entries[$idx] = array($ent, $attr, NULL); | |
| 3933 } | |
| 3934 return $this->conn->setAnnotation($folder, $entries); | |
| 3935 } | |
| 3936 | |
| 3937 return false; | |
| 3938 } | |
| 3939 | |
| 3940 /** | |
| 3941 * Returns IMAP metadata/annotations (GETMETADATA/GETANNOTATION) | |
| 3942 * | |
| 3943 * @param string $folder Folder name (empty for server metadata) | |
| 3944 * @param array $entries Entries | |
| 3945 * @param array $options Command options (with MAXSIZE and DEPTH keys) | |
| 3946 * @param bool $force Disables cache use | |
| 3947 * | |
| 3948 * @return array Metadata entry-value hash array on success, NULL on error | |
| 3949 * @since 0.5-beta | |
| 3950 */ | |
| 3951 public function get_metadata($folder, $entries, $options = array(), $force = false) | |
| 3952 { | |
| 3953 $entries = (array) $entries; | |
| 3954 | |
| 3955 if (!$force) { | |
| 3956 // create cache key | |
| 3957 // @TODO: this is the simplest solution, but we do the same with folders list | |
| 3958 // maybe we should store data per-entry and merge on request | |
| 3959 sort($options); | |
| 3960 sort($entries); | |
| 3961 $cache_key = 'mailboxes.metadata.' . $folder; | |
| 3962 $cache_key .= '.' . md5(serialize($options).serialize($entries)); | |
| 3963 | |
| 3964 // get cached data | |
| 3965 $cached_data = $this->get_cache($cache_key); | |
| 3966 | |
| 3967 if (is_array($cached_data)) { | |
| 3968 return $cached_data; | |
| 3969 } | |
| 3970 } | |
| 3971 | |
| 3972 if (!$this->check_connection()) { | |
| 3973 return null; | |
| 3974 } | |
| 3975 | |
| 3976 if ($this->get_capability('METADATA') || | |
| 3977 (!strlen($folder) && $this->get_capability('METADATA-SERVER')) | |
| 3978 ) { | |
| 3979 $res = $this->conn->getMetadata($folder, $entries, $options); | |
| 3980 } | |
| 3981 else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { | |
| 3982 $queries = array(); | |
| 3983 $res = array(); | |
| 3984 | |
| 3985 // Convert entry names | |
| 3986 foreach ($entries as $entry) { | |
| 3987 list($ent, $attr) = $this->md2annotate($entry); | |
| 3988 $queries[$attr][] = $ent; | |
| 3989 } | |
| 3990 | |
| 3991 // @TODO: Honor MAXSIZE and DEPTH options | |
| 3992 foreach ($queries as $attrib => $entry) { | |
| 3993 $result = $this->conn->getAnnotation($folder, $entry, $attrib); | |
| 3994 | |
| 3995 // an error, invalidate any previous getAnnotation() results | |
| 3996 if (!is_array($result)) { | |
| 3997 return null; | |
| 3998 } | |
| 3999 else { | |
| 4000 foreach ($result as $fldr => $data) { | |
| 4001 $res[$fldr] = array_merge((array) $res[$fldr], $data); | |
| 4002 } | |
| 4003 } | |
| 4004 } | |
| 4005 } | |
| 4006 | |
| 4007 if (isset($res)) { | |
| 4008 if (!$force) { | |
| 4009 $this->update_cache($cache_key, $res); | |
| 4010 } | |
| 4011 | |
| 4012 return $res; | |
| 4013 } | |
| 4014 } | |
| 4015 | |
| 4016 /** | |
| 4017 * Converts the METADATA extension entry name into the correct | |
| 4018 * entry-attrib names for older ANNOTATEMORE version. | |
| 4019 * | |
| 4020 * @param string $entry Entry name | |
| 4021 * | |
| 4022 * @return array Entry-attribute list, NULL if not supported (?) | |
| 4023 */ | |
| 4024 protected function md2annotate($entry) | |
| 4025 { | |
| 4026 if (substr($entry, 0, 7) == '/shared') { | |
| 4027 return array(substr($entry, 7), 'value.shared'); | |
| 4028 } | |
| 4029 else if (substr($entry, 0, 8) == '/private') { | |
| 4030 return array(substr($entry, 8), 'value.priv'); | |
| 4031 } | |
| 4032 | |
| 4033 // @TODO: log error | |
| 4034 } | |
| 4035 | |
| 4036 | |
| 4037 /* -------------------------------- | |
| 4038 * internal caching methods | |
| 4039 * --------------------------------*/ | |
| 4040 | |
| 4041 /** | |
| 4042 * Enable or disable indexes caching | |
| 4043 * | |
| 4044 * @param string $type Cache type (@see rcube::get_cache) | |
| 4045 */ | |
| 4046 public function set_caching($type) | |
| 4047 { | |
| 4048 if ($type) { | |
| 4049 $this->caching = $type; | |
| 4050 } | |
| 4051 else { | |
| 4052 if ($this->cache) { | |
| 4053 $this->cache->close(); | |
| 4054 } | |
| 4055 $this->cache = null; | |
| 4056 $this->caching = false; | |
| 4057 } | |
| 4058 } | |
| 4059 | |
| 4060 /** | |
| 4061 * Getter for IMAP cache object | |
| 4062 */ | |
| 4063 protected function get_cache_engine() | |
| 4064 { | |
| 4065 if ($this->caching && !$this->cache) { | |
| 4066 $rcube = rcube::get_instance(); | |
| 4067 $ttl = $rcube->config->get('imap_cache_ttl', '10d'); | |
| 4068 $this->cache = $rcube->get_cache('IMAP', $this->caching, $ttl); | |
| 4069 } | |
| 4070 | |
| 4071 return $this->cache; | |
| 4072 } | |
| 4073 | |
| 4074 /** | |
| 4075 * Returns cached value | |
| 4076 * | |
| 4077 * @param string $key Cache key | |
| 4078 * | |
| 4079 * @return mixed | |
| 4080 */ | |
| 4081 public function get_cache($key) | |
| 4082 { | |
| 4083 if ($cache = $this->get_cache_engine()) { | |
| 4084 return $cache->get($key); | |
| 4085 } | |
| 4086 } | |
| 4087 | |
| 4088 /** | |
| 4089 * Update cache | |
| 4090 * | |
| 4091 * @param string $key Cache key | |
| 4092 * @param mixed $data Data | |
| 4093 */ | |
| 4094 public function update_cache($key, $data) | |
| 4095 { | |
| 4096 if ($cache = $this->get_cache_engine()) { | |
| 4097 $cache->set($key, $data); | |
| 4098 } | |
| 4099 } | |
| 4100 | |
| 4101 /** | |
| 4102 * Clears the cache. | |
| 4103 * | |
| 4104 * @param string $key Cache key name or pattern | |
| 4105 * @param boolean $prefix_mode Enable it to clear all keys starting | |
| 4106 * with prefix specified in $key | |
| 4107 */ | |
| 4108 public function clear_cache($key = null, $prefix_mode = false) | |
| 4109 { | |
| 4110 if ($cache = $this->get_cache_engine()) { | |
| 4111 $cache->remove($key, $prefix_mode); | |
| 4112 } | |
| 4113 } | |
| 4114 | |
| 4115 | |
| 4116 /* -------------------------------- | |
| 4117 * message caching methods | |
| 4118 * --------------------------------*/ | |
| 4119 | |
| 4120 /** | |
| 4121 * Enable or disable messages caching | |
| 4122 * | |
| 4123 * @param boolean $set Flag | |
| 4124 * @param int $mode Cache mode | |
| 4125 */ | |
| 4126 public function set_messages_caching($set, $mode = null) | |
| 4127 { | |
| 4128 if ($set) { | |
| 4129 $this->messages_caching = true; | |
| 4130 | |
| 4131 if ($mode && ($cache = $this->get_mcache_engine())) { | |
| 4132 $cache->set_mode($mode); | |
| 4133 } | |
| 4134 } | |
| 4135 else { | |
| 4136 if ($this->mcache) { | |
| 4137 $this->mcache->close(); | |
| 4138 } | |
| 4139 $this->mcache = null; | |
| 4140 $this->messages_caching = false; | |
| 4141 } | |
| 4142 } | |
| 4143 | |
| 4144 /** | |
| 4145 * Getter for messages cache object | |
| 4146 */ | |
| 4147 protected function get_mcache_engine() | |
| 4148 { | |
| 4149 if ($this->messages_caching && !$this->mcache) { | |
| 4150 $rcube = rcube::get_instance(); | |
| 4151 if (($dbh = $rcube->get_dbh()) && ($userid = $rcube->get_user_id())) { | |
| 4152 $ttl = $rcube->config->get('messages_cache_ttl', '10d'); | |
| 4153 $threshold = $rcube->config->get('messages_cache_threshold', 50); | |
| 4154 $this->mcache = new rcube_imap_cache( | |
| 4155 $dbh, $this, $userid, $this->options['skip_deleted'], $ttl, $threshold); | |
| 4156 } | |
| 4157 } | |
| 4158 | |
| 4159 return $this->mcache; | |
| 4160 } | |
| 4161 | |
| 4162 /** | |
| 4163 * Clears the messages cache. | |
| 4164 * | |
| 4165 * @param string $folder Folder name | |
| 4166 * @param array $uids Optional message UIDs to remove from cache | |
| 4167 */ | |
| 4168 protected function clear_message_cache($folder = null, $uids = null) | |
| 4169 { | |
| 4170 if ($mcache = $this->get_mcache_engine()) { | |
| 4171 $mcache->clear($folder, $uids); | |
| 4172 } | |
| 4173 } | |
| 4174 | |
| 4175 /** | |
| 4176 * Delete outdated cache entries | |
| 4177 */ | |
| 4178 function cache_gc() | |
| 4179 { | |
| 4180 rcube_imap_cache::gc(); | |
| 4181 } | |
| 4182 | |
| 4183 | |
| 4184 /* -------------------------------- | |
| 4185 * protected methods | |
| 4186 * --------------------------------*/ | |
| 4187 | |
| 4188 /** | |
| 4189 * Validate the given input and save to local properties | |
| 4190 * | |
| 4191 * @param string $sort_field Sort column | |
| 4192 * @param string $sort_order Sort order | |
| 4193 */ | |
| 4194 protected function set_sort_order($sort_field, $sort_order) | |
| 4195 { | |
| 4196 if ($sort_field != null) { | |
| 4197 $this->sort_field = asciiwords($sort_field); | |
| 4198 } | |
| 4199 if ($sort_order != null) { | |
| 4200 $this->sort_order = strtoupper($sort_order) == 'DESC' ? 'DESC' : 'ASC'; | |
| 4201 } | |
| 4202 } | |
| 4203 | |
| 4204 /** | |
| 4205 * Sort folders first by default folders and then in alphabethical order | |
| 4206 * | |
| 4207 * @param array $a_folders Folders list | |
| 4208 * @param bool $skip_default Skip default folders handling | |
| 4209 * | |
| 4210 * @return array Sorted list | |
| 4211 */ | |
| 4212 public function sort_folder_list($a_folders, $skip_default = false) | |
| 4213 { | |
| 4214 $specials = array_merge(array('INBOX'), array_values($this->get_special_folders())); | |
| 4215 $folders = array(); | |
| 4216 | |
| 4217 // convert names to UTF-8 | |
| 4218 foreach ($a_folders as $folder) { | |
| 4219 // for better performance skip encoding conversion | |
| 4220 // if the string does not look like UTF7-IMAP | |
| 4221 $folders[$folder] = strpos($folder, '&') === false ? $folder : rcube_charset::convert($folder, 'UTF7-IMAP'); | |
| 4222 } | |
| 4223 | |
| 4224 // sort folders | |
| 4225 // asort($folders, SORT_LOCALE_STRING) is not properly sorting case sensitive names | |
| 4226 uasort($folders, array($this, 'sort_folder_comparator')); | |
| 4227 | |
| 4228 $folders = array_keys($folders); | |
| 4229 | |
| 4230 if ($skip_default) { | |
| 4231 return $folders; | |
| 4232 } | |
| 4233 | |
| 4234 // force the type of folder name variable (#1485527) | |
| 4235 $folders = array_map('strval', $folders); | |
| 4236 $out = array(); | |
| 4237 | |
| 4238 // finally we must put special folders on top and rebuild the list | |
| 4239 // to move their subfolders where they belong... | |
| 4240 $specials = array_unique(array_intersect($specials, $folders)); | |
| 4241 $folders = array_merge($specials, array_diff($folders, $specials)); | |
| 4242 | |
| 4243 $this->sort_folder_specials(null, $folders, $specials, $out); | |
| 4244 | |
| 4245 return $out; | |
| 4246 } | |
| 4247 | |
| 4248 /** | |
| 4249 * Recursive function to put subfolders of special folders in place | |
| 4250 */ | |
| 4251 protected function sort_folder_specials($folder, &$list, &$specials, &$out) | |
| 4252 { | |
| 4253 foreach ($list as $key => $name) { | |
| 4254 if ($folder === null || strpos($name, $folder.$this->delimiter) === 0) { | |
| 4255 $out[] = $name; | |
| 4256 unset($list[$key]); | |
| 4257 | |
| 4258 if (!empty($specials) && ($found = array_search($name, $specials)) !== false) { | |
| 4259 unset($specials[$found]); | |
| 4260 $this->sort_folder_specials($name, $list, $specials, $out); | |
| 4261 } | |
| 4262 } | |
| 4263 } | |
| 4264 | |
| 4265 reset($list); | |
| 4266 } | |
| 4267 | |
| 4268 /** | |
| 4269 * Callback for uasort() that implements correct | |
| 4270 * locale-aware case-sensitive sorting | |
| 4271 */ | |
| 4272 protected function sort_folder_comparator($str1, $str2) | |
| 4273 { | |
| 4274 if ($this->sort_folder_collator === null) { | |
| 4275 $this->sort_folder_collator = false; | |
| 4276 | |
| 4277 // strcoll() does not work with UTF8 locale on Windows, | |
| 4278 // use Collator from the intl extension | |
| 4279 if (stripos(PHP_OS, 'win') === 0 && function_exists('collator_compare')) { | |
| 4280 $locale = $this->options['language'] ?: 'en_US'; | |
| 4281 $this->sort_folder_collator = collator_create($locale) ?: false; | |
| 4282 } | |
| 4283 } | |
| 4284 | |
| 4285 $path1 = explode($this->delimiter, $str1); | |
| 4286 $path2 = explode($this->delimiter, $str2); | |
| 4287 | |
| 4288 foreach ($path1 as $idx => $folder1) { | |
|
17
dd5ed6ef69c9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
16
diff
changeset
|
4289 $folder2 = $path2[$idx]??null; |
| 0 | 4290 |
| 4291 if ($folder1 === $folder2) { | |
| 4292 continue; | |
| 4293 } | |
| 4294 | |
| 4295 if ($this->sort_folder_collator) { | |
| 4296 return collator_compare($this->sort_folder_collator, $folder1, $folder2); | |
| 4297 } | |
| 4298 | |
|
17
dd5ed6ef69c9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
16
diff
changeset
|
4299 # HST: I believe null arg used to be arbitrarily 'less' than anything |
|
dd5ed6ef69c9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
16
diff
changeset
|
4300 return ($folder2 ? strcoll($folder1, $folder2) : 1); |
| 0 | 4301 } |
| 4302 } | |
| 4303 | |
| 4304 /** | |
| 4305 * Find UID of the specified message sequence ID | |
| 4306 * | |
| 4307 * @param int $id Message (sequence) ID | |
| 4308 * @param string $folder Folder name | |
| 4309 * | |
| 4310 * @return int Message UID | |
| 4311 */ | |
| 4312 public function id2uid($id, $folder = null) | |
| 4313 { | |
| 4314 if (!strlen($folder)) { | |
| 4315 $folder = $this->folder; | |
| 4316 } | |
| 4317 | |
| 4318 if (!$this->check_connection()) { | |
| 4319 return null; | |
| 4320 } | |
| 4321 | |
| 4322 return $this->conn->ID2UID($folder, $id); | |
| 4323 } | |
| 4324 | |
| 4325 /** | |
| 4326 * Subscribe/unsubscribe a list of folders and update local cache | |
| 4327 */ | |
| 4328 protected function change_subscription($folders, $mode) | |
| 4329 { | |
| 4330 $updated = 0; | |
| 4331 $folders = (array) $folders; | |
| 4332 | |
| 4333 if (!empty($folders)) { | |
| 4334 if (!$this->check_connection()) { | |
| 4335 return false; | |
| 4336 } | |
| 4337 | |
| 4338 foreach ($folders as $folder) { | |
| 4339 $updated += (int) $this->conn->{$mode}($folder); | |
| 4340 } | |
| 4341 } | |
| 4342 | |
| 4343 // clear cached folders list(s) | |
| 4344 if ($updated) { | |
| 4345 $this->clear_cache('mailboxes', true); | |
| 4346 } | |
| 4347 | |
| 4348 return $updated == count($folders); | |
| 4349 } | |
| 4350 | |
| 4351 /** | |
| 4352 * Increde/decrese messagecount for a specific folder | |
| 4353 */ | |
| 4354 protected function set_messagecount($folder, $mode, $increment) | |
| 4355 { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
4356 if (!is_numeric($increment) || !$this->caching) { |
| 0 | 4357 return false; |
| 4358 } | |
| 4359 | |
| 4360 $mode = strtoupper($mode); | |
| 4361 $a_folder_cache = $this->get_cache('messagecount'); | |
| 4362 | |
| 4363 if (!is_array($a_folder_cache[$folder]) || !isset($a_folder_cache[$folder][$mode])) { | |
| 4364 return false; | |
| 4365 } | |
| 4366 | |
| 4367 // add incremental value to messagecount | |
| 4368 $a_folder_cache[$folder][$mode] += $increment; | |
| 4369 | |
| 4370 // there's something wrong, delete from cache | |
| 4371 if ($a_folder_cache[$folder][$mode] < 0) { | |
| 4372 unset($a_folder_cache[$folder][$mode]); | |
| 4373 } | |
| 4374 | |
| 4375 // write back to cache | |
| 4376 $this->update_cache('messagecount', $a_folder_cache); | |
| 4377 | |
| 4378 return true; | |
| 4379 } | |
| 4380 | |
| 4381 /** | |
| 4382 * Remove messagecount of a specific folder from cache | |
| 4383 */ | |
| 4384 protected function clear_messagecount($folder, $mode = array()) | |
| 4385 { | |
| 4386 $a_folder_cache = $this->get_cache('messagecount'); | |
| 4387 | |
| 4388 if (is_array($a_folder_cache[$folder])) { | |
| 4389 if (!empty($mode)) { | |
| 4390 foreach ((array) $mode as $key) { | |
| 4391 unset($a_folder_cache[$folder][$key]); | |
| 4392 } | |
| 4393 } | |
| 4394 else { | |
| 4395 unset($a_folder_cache[$folder]); | |
| 4396 } | |
| 4397 $this->update_cache('messagecount', $a_folder_cache); | |
| 4398 } | |
| 4399 } | |
| 4400 | |
| 4401 /** | |
| 4402 * Converts date string/object into IMAP date/time format | |
| 4403 */ | |
| 4404 protected function date_format($date) | |
| 4405 { | |
| 4406 if (empty($date)) { | |
| 4407 return null; | |
| 4408 } | |
| 4409 | |
| 4410 if (!is_object($date) || !is_a($date, 'DateTime')) { | |
| 4411 try { | |
| 4412 $timestamp = rcube_utils::strtotime($date); | |
| 4413 $date = new DateTime("@".$timestamp); | |
| 4414 } | |
| 4415 catch (Exception $e) { | |
| 4416 return null; | |
| 4417 } | |
| 4418 } | |
| 4419 | |
| 4420 return $date->format('d-M-Y H:i:s O'); | |
| 4421 } | |
| 4422 | |
| 4423 /** | |
| 4424 * This is our own debug handler for the IMAP connection | |
| 4425 */ | |
| 4426 public function debug_handler(&$imap, $message) | |
| 4427 { | |
| 4428 rcube::write_log('imap', $message); | |
| 4429 } | |
| 4430 } |
