Mercurial > hg > rc2
annotate program/lib/Roundcube/rcube_imap.php @ 27:c32b53434b47
more compensation for deprecated empty cases
| author | Charlie Root |
|---|---|
| date | Sun, 18 Jan 2026 14:20:29 -0500 |
| parents | e53add5ddac5 |
| children |
| 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 { | |
| 27 | 628 if (empty($folder)) { |
| 0 | 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, | |
| 27 | 1571 'result' => null, |
| 0 | 1572 )); |
| 1573 | |
| 1574 $folder = $plugin['folder']; | |
| 1575 $search = $plugin['search']; | |
| 1576 $charset = $plugin['charset']; | |
| 1577 $sort_field = $plugin['sort_field']; | |
| 1578 $results = $plugin['result']; | |
| 1579 | |
| 1580 // multi-folder search | |
| 1581 if (!$results && is_array($folder) && count($folder) > 1 && $search != 'ALL') { | |
| 1582 // connect IMAP to have all the required classes and settings loaded | |
| 1583 $this->check_connection(); | |
| 1584 | |
| 1585 // disable threading | |
| 1586 $this->threading = false; | |
| 1587 | |
| 1588 $searcher = new rcube_imap_search($this->options, $this->conn); | |
| 1589 | |
| 1590 // set limit to not exceed the client's request timeout | |
| 1591 $searcher->set_timelimit(60); | |
| 1592 | |
| 1593 // continue existing incomplete search | |
| 1594 if (!empty($this->search_set) && $this->search_set->incomplete && $search == $this->search_string) { | |
| 1595 $searcher->set_results($this->search_set); | |
| 1596 } | |
| 1597 | |
| 1598 // execute the search | |
| 1599 $results = $searcher->exec( | |
| 1600 $folder, | |
| 1601 $search, | |
| 1602 $charset ? $charset : $this->default_charset, | |
| 1603 $sort_field && $this->get_capability('SORT') ? $sort_field : null, | |
| 1604 $this->threading | |
| 1605 ); | |
| 1606 } | |
| 1607 else if (!$results) { | |
| 1608 $folder = is_array($folder) ? $folder[0] : $folder; | |
| 1609 $search = is_array($search) ? $search[$folder] : $search; | |
| 1610 $results = $this->search_index($folder, $search, $charset, $sort_field); | |
| 1611 } | |
| 1612 | |
| 1613 $sorted = $this->threading || $this->search_sorted || $plugin['search_sorted'] ? true : false; | |
| 1614 | |
| 1615 $this->set_search_set(array($search, $results, $charset, $sort_field, $sorted)); | |
| 1616 | |
| 1617 return $results; | |
| 1618 } | |
| 1619 | |
| 1620 /** | |
| 1621 * Direct (real and simple) SEARCH request (without result sorting and caching). | |
| 1622 * | |
| 1623 * @param string $mailbox Mailbox name to search in | |
| 1624 * @param string $str Search string | |
| 1625 * | |
| 1626 * @return rcube_result_index Search result (UIDs) | |
| 1627 */ | |
| 1628 public function search_once($folder = null, $str = 'ALL') | |
| 1629 { | |
| 1630 if (!$this->check_connection()) { | |
| 1631 return new rcube_result_index(); | |
| 1632 } | |
| 1633 | |
| 1634 if (!$str) { | |
| 1635 $str = 'ALL'; | |
| 1636 } | |
| 1637 | |
| 1638 // multi-folder search | |
| 1639 if (is_array($folder) && count($folder) > 1) { | |
| 1640 $searcher = new rcube_imap_search($this->options, $this->conn); | |
| 1641 $index = $searcher->exec($folder, $str, $this->default_charset); | |
| 1642 } | |
| 1643 else { | |
| 1644 $folder = is_array($folder) ? $folder[0] : $folder; | |
| 1645 if (!strlen($folder)) { | |
| 1646 $folder = $this->folder; | |
| 1647 } | |
| 1648 $index = $this->conn->search($folder, $str, true); | |
| 1649 } | |
| 1650 | |
| 1651 return $index; | |
| 1652 } | |
| 1653 | |
| 1654 /** | |
| 1655 * protected search method | |
| 1656 * | |
| 1657 * @param string $folder Folder name | |
| 1658 * @param string $criteria Search criteria | |
| 1659 * @param string $charset Charset | |
| 1660 * @param string $sort_field Sorting field | |
| 1661 * | |
| 1662 * @return rcube_result_index|rcube_result_thread Search results (UIDs) | |
| 1663 * @see rcube_imap::search() | |
| 1664 */ | |
| 1665 protected function search_index($folder, $criteria='ALL', $charset=NULL, $sort_field=NULL) | |
| 1666 { | |
| 1667 if (!$this->check_connection()) { | |
| 1668 if ($this->threading) { | |
| 1669 return new rcube_result_thread(); | |
| 1670 } | |
| 1671 else { | |
| 1672 return new rcube_result_index(); | |
| 1673 } | |
| 1674 } | |
| 1675 | |
| 1676 if ($this->options['skip_deleted'] && !preg_match('/UNDELETED/', $criteria)) { | |
| 1677 $criteria = 'UNDELETED '.$criteria; | |
| 1678 } | |
| 1679 | |
| 1680 // unset CHARSET if criteria string is ASCII, this way | |
| 1681 // SEARCH won't be re-sent after "unsupported charset" response | |
| 1682 if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) { | |
| 1683 $charset = 'US-ASCII'; | |
| 1684 } | |
| 1685 | |
| 1686 if ($this->threading) { | |
| 1687 $threads = $this->conn->thread($folder, $this->threading, $criteria, true, $charset); | |
| 1688 | |
| 1689 // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, | |
| 1690 // but I've seen that Courier doesn't support UTF-8) | |
| 1691 if ($threads->is_error() && $charset && $charset != 'US-ASCII') { | |
| 1692 $threads = $this->conn->thread($folder, $this->threading, | |
| 1693 self::convert_criteria($criteria, $charset), true, 'US-ASCII'); | |
| 1694 } | |
| 1695 | |
| 1696 return $threads; | |
| 1697 } | |
| 1698 | |
| 1699 if ($sort_field && $this->get_capability('SORT')) { | |
| 1700 $charset = $charset ? $charset : $this->default_charset; | |
| 1701 $messages = $this->conn->sort($folder, $sort_field, $criteria, true, $charset); | |
| 1702 | |
| 1703 // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, | |
| 1704 // but I've seen Courier with disabled UTF-8 support) | |
| 1705 if ($messages->is_error() && $charset && $charset != 'US-ASCII') { | |
| 1706 $messages = $this->conn->sort($folder, $sort_field, | |
| 1707 self::convert_criteria($criteria, $charset), true, 'US-ASCII'); | |
| 1708 } | |
| 1709 | |
| 1710 if (!$messages->is_error()) { | |
| 1711 $this->search_sorted = true; | |
| 1712 return $messages; | |
| 1713 } | |
| 1714 } | |
| 1715 | |
| 1716 $messages = $this->conn->search($folder, | |
| 1717 ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true); | |
| 1718 | |
| 1719 // Error, try with US-ASCII (some servers may support only US-ASCII) | |
| 1720 if ($messages->is_error() && $charset && $charset != 'US-ASCII') { | |
| 1721 $messages = $this->conn->search($folder, | |
| 1722 self::convert_criteria($criteria, $charset), true); | |
| 1723 } | |
| 1724 | |
| 1725 $this->search_sorted = false; | |
| 1726 | |
| 1727 return $messages; | |
| 1728 } | |
| 1729 | |
| 1730 /** | |
| 1731 * Converts charset of search criteria string | |
| 1732 * | |
| 1733 * @param string $str Search string | |
| 1734 * @param string $charset Original charset | |
| 1735 * @param string $dest_charset Destination charset (default US-ASCII) | |
| 1736 * | |
| 1737 * @return string Search string | |
| 1738 */ | |
| 1739 public static function convert_criteria($str, $charset, $dest_charset='US-ASCII') | |
| 1740 { | |
| 1741 // convert strings to US_ASCII | |
| 1742 if (preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE)) { | |
| 1743 $last = 0; $res = ''; | |
| 1744 foreach ($matches[1] as $m) { | |
| 1745 $string_offset = $m[1] + strlen($m[0]) + 4; // {}\r\n | |
| 1746 $string = substr($str, $string_offset - 1, $m[0]); | |
| 1747 $string = rcube_charset::convert($string, $charset, $dest_charset); | |
| 1748 | |
| 1749 if ($string === false || !strlen($string)) { | |
| 1750 continue; | |
| 1751 } | |
| 1752 | |
| 1753 $res .= substr($str, $last, $m[1] - $last - 1) . rcube_imap_generic::escape($string); | |
| 1754 $last = $m[0] + $string_offset - 1; | |
| 1755 } | |
| 1756 | |
| 1757 if ($last < strlen($str)) { | |
| 1758 $res .= substr($str, $last, strlen($str)-$last); | |
| 1759 } | |
| 1760 } | |
| 1761 // strings for conversion not found | |
| 1762 else { | |
| 1763 $res = $str; | |
| 1764 } | |
| 1765 | |
| 1766 return $res; | |
| 1767 } | |
| 1768 | |
| 1769 /** | |
| 1770 * Refresh saved search set | |
| 1771 * | |
| 1772 * @return array Current search set | |
| 1773 */ | |
| 1774 public function refresh_search() | |
| 1775 { | |
| 1776 if (!empty($this->search_string)) { | |
| 1777 $this->search( | |
| 1778 is_object($this->search_set) ? $this->search_set->get_parameters('MAILBOX') : '', | |
| 1779 $this->search_string, | |
| 1780 $this->search_charset, | |
| 1781 $this->search_sort_field | |
| 1782 ); | |
| 1783 } | |
| 1784 | |
| 1785 return $this->get_search_set(); | |
| 1786 } | |
| 1787 | |
| 1788 /** | |
| 1789 * Flag certain result subsets as 'incomplete'. | |
| 1790 * For subsequent refresh_search() calls to only refresh the updated parts. | |
| 1791 */ | |
| 1792 protected function set_search_dirty($folder) | |
| 1793 { | |
| 1794 if ($this->search_set && is_a($this->search_set, 'rcube_result_multifolder')) { | |
| 1795 if ($subset = $this->search_set->get_set($folder)) { | |
| 1796 $subset->incomplete = $this->search_set->incomplete = true; | |
| 1797 } | |
| 1798 } | |
| 1799 } | |
| 1800 | |
| 1801 /** | |
| 1802 * Return message headers object of a specific message | |
| 1803 * | |
| 1804 * @param int $id Message UID | |
| 1805 * @param string $folder Folder to read from | |
| 1806 * @param bool $force True to skip cache | |
| 1807 * | |
| 1808 * @return rcube_message_header Message headers | |
| 1809 */ | |
| 1810 public function get_message_headers($uid, $folder = null, $force = false) | |
| 1811 { | |
| 1812 // decode combined UID-folder identifier | |
| 1813 if (preg_match('/^\d+-.+/', $uid)) { | |
| 1814 list($uid, $folder) = explode('-', $uid, 2); | |
| 1815 } | |
| 1816 | |
| 21 | 1817 if (empty($folder)) { |
| 0 | 1818 $folder = $this->folder; |
| 1819 } | |
| 1820 | |
| 1821 // get cached headers | |
| 1822 if (!$force && $uid && ($mcache = $this->get_mcache_engine())) { | |
| 1823 $headers = $mcache->get_message($folder, $uid); | |
| 1824 } | |
| 1825 else if (!$this->check_connection()) { | |
| 1826 $headers = false; | |
| 1827 } | |
| 1828 else { | |
| 1829 $headers = $this->conn->fetchHeader( | |
| 1830 $folder, $uid, true, true, $this->get_fetch_headers()); | |
| 1831 | |
| 1832 if (is_object($headers)) | |
| 1833 $headers->folder = $folder; | |
| 1834 } | |
| 1835 | |
| 1836 return $headers; | |
| 1837 } | |
| 1838 | |
| 1839 /** | |
| 1840 * Fetch message headers and body structure from the IMAP server and build | |
| 1841 * an object structure. | |
| 1842 * | |
| 1843 * @param int $uid Message UID to fetch | |
| 1844 * @param string $folder Folder to read from | |
| 1845 * | |
| 1846 * @return object rcube_message_header Message data | |
| 1847 */ | |
| 1848 public function get_message($uid, $folder = null) | |
| 1849 { | |
| 21 | 1850 if (!empty($folder)) { |
| 0 | 1851 $folder = $this->folder; |
| 1852 } | |
| 1853 | |
| 1854 // decode combined UID-folder identifier | |
| 1855 if (preg_match('/^\d+-.+/', $uid)) { | |
| 1856 list($uid, $folder) = explode('-', $uid, 2); | |
| 1857 } | |
| 1858 | |
| 1859 // Check internal cache | |
| 1860 if (!empty($this->icache['message'])) { | |
| 1861 if (($headers = $this->icache['message']) && $headers->uid == $uid) { | |
| 1862 return $headers; | |
| 1863 } | |
| 1864 } | |
| 1865 | |
| 1866 $headers = $this->get_message_headers($uid, $folder); | |
| 1867 | |
| 1868 // message doesn't exist? | |
| 1869 if (empty($headers)) { | |
| 1870 return null; | |
| 1871 } | |
| 1872 | |
| 1873 // structure might be cached | |
| 1874 if (!empty($headers->structure)) { | |
| 1875 return $headers; | |
| 1876 } | |
| 1877 | |
| 1878 $this->msg_uid = $uid; | |
| 1879 | |
| 1880 if (!$this->check_connection()) { | |
| 1881 return $headers; | |
| 1882 } | |
| 1883 | |
| 1884 if (empty($headers->bodystructure)) { | |
| 1885 $headers->bodystructure = $this->conn->getStructure($folder, $uid, true); | |
| 1886 } | |
| 1887 | |
| 1888 $structure = $headers->bodystructure; | |
| 1889 | |
| 1890 if (empty($structure)) { | |
| 1891 return $headers; | |
| 1892 } | |
| 1893 | |
| 1894 // set message charset from message headers | |
| 1895 if ($headers->charset) { | |
| 1896 $this->struct_charset = $headers->charset; | |
| 1897 } | |
| 1898 else { | |
| 1899 $this->struct_charset = $this->structure_charset($structure); | |
| 1900 } | |
| 1901 | |
| 1902 $headers->ctype = @strtolower($headers->ctype); | |
| 1903 | |
| 1904 // Here we can recognize malformed BODYSTRUCTURE and | |
| 1905 // 1. [@TODO] parse the message in other way to create our own message structure | |
| 1906 // 2. or just show the raw message body. | |
| 1907 // Example of structure for malformed MIME message: | |
| 1908 // ("text" "plain" NIL NIL NIL "7bit" 2154 70 NIL NIL NIL) | |
| 1909 if ($headers->ctype && !is_array($structure[0]) && $headers->ctype != 'text/plain' | |
| 1910 && strtolower($structure[0].'/'.$structure[1]) == 'text/plain' | |
| 1911 ) { | |
| 1912 // A special known case "Content-type: text" (#1488968) | |
| 1913 if ($headers->ctype == 'text') { | |
| 1914 $structure[1] = 'plain'; | |
| 1915 $headers->ctype = 'text/plain'; | |
| 1916 } | |
| 1917 // we can handle single-part messages, by simple fix in structure (#1486898) | |
| 1918 else if (preg_match('/^(text|application)\/(.*)/', $headers->ctype, $m)) { | |
| 1919 $structure[0] = $m[1]; | |
| 1920 $structure[1] = $m[2]; | |
| 1921 } | |
| 1922 else { | |
| 1923 // Try to parse the message using rcube_mime_decode. | |
| 1924 // We need a better solution, it parses message | |
| 1925 // in memory, which wouldn't work for very big messages, | |
| 1926 // (it uses up to 10x more memory than the message size) | |
| 1927 // it's also buggy and not actively developed | |
| 1928 if ($headers->size && rcube_utils::mem_check($headers->size * 10)) { | |
| 1929 $raw_msg = $this->get_raw_body($uid); | |
| 1930 $struct = rcube_mime::parse_message($raw_msg); | |
| 1931 } | |
| 1932 else { | |
| 1933 return $headers; | |
| 1934 } | |
| 1935 } | |
| 1936 } | |
| 1937 | |
| 1938 if (empty($struct)) { | |
| 1939 $struct = $this->structure_part($structure, 0, '', $headers); | |
| 1940 } | |
| 1941 | |
| 1942 // some workarounds on simple messages... | |
| 1943 if (empty($struct->parts)) { | |
| 1944 // ...don't trust given content-type | |
| 1945 if (!empty($headers->ctype)) { | |
| 1946 $struct->mime_id = '1'; | |
| 1947 $struct->mimetype = strtolower($headers->ctype); | |
| 1948 list($struct->ctype_primary, $struct->ctype_secondary) = explode('/', $struct->mimetype); | |
| 1949 } | |
| 1950 | |
| 1951 // ...and charset (there's a case described in #1488968 where invalid content-type | |
| 1952 // results in invalid charset in BODYSTRUCTURE) | |
| 1953 if (!empty($headers->charset) && $headers->charset != $struct->ctype_parameters['charset']) { | |
| 1954 $struct->charset = $headers->charset; | |
| 1955 $struct->ctype_parameters['charset'] = $headers->charset; | |
| 1956 } | |
| 1957 } | |
| 1958 | |
| 1959 $headers->structure = $struct; | |
| 1960 | |
| 1961 return $this->icache['message'] = $headers; | |
| 1962 } | |
| 1963 | |
| 1964 /** | |
| 1965 * Build message part object | |
| 1966 * | |
| 1967 * @param array $part | |
| 1968 * @param int $count | |
| 1969 * @param string $parent | |
| 1970 */ | |
| 1971 protected function structure_part($part, $count = 0, $parent = '', $mime_headers = null) | |
| 1972 { | |
| 1973 $struct = new rcube_message_part; | |
| 1974 $struct->mime_id = empty($parent) ? (string)$count : "$parent.$count"; | |
| 1975 | |
| 1976 // multipart | |
| 1977 if (is_array($part[0])) { | |
| 1978 $struct->ctype_primary = 'multipart'; | |
| 1979 | |
| 1980 /* RFC3501: BODYSTRUCTURE fields of multipart part | |
| 1981 part1 array | |
| 1982 part2 array | |
| 1983 part3 array | |
| 1984 .... | |
| 1985 1. subtype | |
| 1986 2. parameters (optional) | |
| 1987 3. description (optional) | |
| 1988 4. language (optional) | |
| 1989 5. location (optional) | |
| 1990 */ | |
| 1991 | |
| 1992 // find first non-array entry | |
| 1993 for ($i=1; $i<count($part); $i++) { | |
| 1994 if (!is_array($part[$i])) { | |
| 1995 $struct->ctype_secondary = strtolower($part[$i]); | |
| 1996 | |
| 1997 // read content type parameters | |
| 1998 if (is_array($part[$i+1])) { | |
| 1999 $struct->ctype_parameters = array(); | |
| 2000 for ($j=0; $j<count($part[$i+1]); $j+=2) { | |
| 2001 $param = strtolower($part[$i+1][$j]); | |
| 2002 $struct->ctype_parameters[$param] = $part[$i+1][$j+1]; | |
| 2003 } | |
| 2004 } | |
| 2005 | |
| 2006 break; | |
| 2007 } | |
| 2008 } | |
| 2009 | |
| 2010 $struct->mimetype = 'multipart/'.$struct->ctype_secondary; | |
| 2011 | |
| 2012 // build parts list for headers pre-fetching | |
| 2013 for ($i=0; $i<count($part); $i++) { | |
| 2014 if (!is_array($part[$i])) { | |
| 2015 break; | |
| 2016 } | |
| 2017 // fetch message headers if message/rfc822 | |
| 2018 // or named part (could contain Content-Location header) | |
| 2019 if (!is_array($part[$i][0])) { | |
| 2020 $tmp_part_id = $struct->mime_id ? $struct->mime_id.'.'.($i+1) : $i+1; | |
| 2021 if (strtolower($part[$i][0]) == 'message' && strtolower($part[$i][1]) == 'rfc822') { | |
| 2022 $mime_part_headers[] = $tmp_part_id; | |
| 2023 } | |
| 2024 else if (in_array('name', (array)$part[$i][2]) && empty($part[$i][3])) { | |
| 2025 $mime_part_headers[] = $tmp_part_id; | |
| 2026 } | |
| 2027 } | |
| 2028 } | |
| 2029 | |
| 2030 // pre-fetch headers of all parts (in one command for better performance) | |
| 2031 // @TODO: we could do this before _structure_part() call, to fetch | |
| 2032 // headers for parts on all levels | |
| 21 | 2033 if (!empty($mime_part_headers)) { |
| 0 | 2034 $mime_part_headers = $this->conn->fetchMIMEHeaders($this->folder, |
| 2035 $this->msg_uid, $mime_part_headers); | |
| 2036 } | |
| 2037 | |
| 2038 $struct->parts = array(); | |
| 2039 for ($i=0, $count=0; $i<count($part); $i++) { | |
| 2040 if (!is_array($part[$i])) { | |
| 2041 break; | |
| 2042 } | |
| 2043 $tmp_part_id = $struct->mime_id ? $struct->mime_id.'.'.($i+1) : $i+1; | |
| 2044 $struct->parts[] = $this->structure_part($part[$i], ++$count, $struct->mime_id, | |
| 21 | 2045 empty($mime_part_headers[$tmp_part_id]) ? null : $mime_part_headers[$tmp_part_id]) ; |
| 0 | 2046 } |
| 2047 | |
| 2048 return $struct; | |
| 2049 } | |
| 2050 | |
| 2051 /* RFC3501: BODYSTRUCTURE fields of non-multipart part | |
| 2052 0. type | |
| 2053 1. subtype | |
| 2054 2. parameters | |
| 2055 3. id | |
| 2056 4. description | |
| 2057 5. encoding | |
| 2058 6. size | |
| 2059 -- text | |
| 2060 7. lines | |
| 2061 -- message/rfc822 | |
| 2062 7. envelope structure | |
| 2063 8. body structure | |
| 2064 9. lines | |
| 2065 -- | |
| 2066 x. md5 (optional) | |
| 2067 x. disposition (optional) | |
| 2068 x. language (optional) | |
| 2069 x. location (optional) | |
| 2070 */ | |
| 2071 | |
| 2072 // regular part | |
| 2073 $struct->ctype_primary = strtolower($part[0]); | |
| 2074 $struct->ctype_secondary = strtolower($part[1]); | |
| 2075 $struct->mimetype = $struct->ctype_primary.'/'.$struct->ctype_secondary; | |
| 2076 | |
| 2077 // read content type parameters | |
| 2078 if (is_array($part[2])) { | |
| 2079 $struct->ctype_parameters = array(); | |
| 2080 for ($i=0; $i<count($part[2]); $i+=2) { | |
| 2081 $struct->ctype_parameters[strtolower($part[2][$i])] = $part[2][$i+1]; | |
| 2082 } | |
| 2083 | |
| 2084 if (isset($struct->ctype_parameters['charset'])) { | |
| 2085 $struct->charset = $struct->ctype_parameters['charset']; | |
| 2086 } | |
| 2087 } | |
| 2088 | |
| 2089 // #1487700: workaround for lack of charset in malformed structure | |
| 2090 if (empty($struct->charset) && !empty($mime_headers) && $mime_headers->charset) { | |
| 2091 $struct->charset = $mime_headers->charset; | |
| 2092 } | |
| 2093 | |
| 2094 // read content encoding | |
| 2095 if (!empty($part[5])) { | |
| 2096 $struct->encoding = strtolower($part[5]); | |
| 2097 $struct->headers['content-transfer-encoding'] = $struct->encoding; | |
| 2098 } | |
| 2099 | |
| 2100 // get part size | |
| 2101 if (!empty($part[6])) { | |
| 2102 $struct->size = intval($part[6]); | |
| 2103 } | |
| 2104 | |
| 2105 // read part disposition | |
| 2106 $di = 8; | |
| 2107 if ($struct->ctype_primary == 'text') { | |
| 2108 $di += 1; | |
| 2109 } | |
| 2110 else if ($struct->mimetype == 'message/rfc822') { | |
| 2111 $di += 3; | |
| 2112 } | |
| 2113 | |
| 2114 if (is_array($part[$di]) && count($part[$di]) == 2) { | |
| 2115 $struct->disposition = strtolower($part[$di][0]); | |
| 2116 if ($struct->disposition && $struct->disposition !== 'inline' && $struct->disposition !== 'attachment') { | |
| 2117 // RFC2183, Section 2.8 - unrecognized type should be treated as "attachment" | |
| 2118 $struct->disposition = 'attachment'; | |
| 2119 } | |
| 2120 if (is_array($part[$di][1])) { | |
| 2121 for ($n=0; $n<count($part[$di][1]); $n+=2) { | |
| 2122 $struct->d_parameters[strtolower($part[$di][1][$n])] = $part[$di][1][$n+1]; | |
| 2123 } | |
| 2124 } | |
| 2125 } | |
| 2126 | |
| 2127 // get message/rfc822's child-parts | |
| 2128 if (is_array($part[8]) && $di != 8) { | |
| 2129 $struct->parts = array(); | |
| 2130 for ($i=0, $count=0; $i<count($part[8]); $i++) { | |
| 2131 if (!is_array($part[8][$i])) { | |
| 2132 break; | |
| 2133 } | |
| 2134 $struct->parts[] = $this->structure_part($part[8][$i], ++$count, $struct->mime_id); | |
| 2135 } | |
| 2136 } | |
| 2137 | |
| 2138 // get part ID | |
| 2139 if (!empty($part[3])) { | |
| 2140 $struct->content_id = $part[3]; | |
| 2141 $struct->headers['content-id'] = $part[3]; | |
| 2142 | |
| 2143 if (empty($struct->disposition)) { | |
| 2144 $struct->disposition = 'inline'; | |
| 2145 } | |
| 2146 } | |
| 2147 | |
| 2148 // fetch message headers if message/rfc822 or named part (could contain Content-Location header) | |
| 21 | 2149 if ($struct->ctype_primary == 'message' || (!empty($struct->ctype_parameters['name']) && !$struct->content_id)) { |
| 0 | 2150 if (empty($mime_headers)) { |
| 2151 $mime_headers = $this->conn->fetchPartHeader( | |
| 2152 $this->folder, $this->msg_uid, true, $struct->mime_id); | |
| 2153 } | |
| 2154 | |
| 2155 if (is_string($mime_headers)) { | |
| 2156 $struct->headers = rcube_mime::parse_headers($mime_headers) + $struct->headers; | |
| 2157 } | |
| 2158 else if (is_object($mime_headers)) { | |
| 2159 $struct->headers = get_object_vars($mime_headers) + $struct->headers; | |
| 2160 } | |
| 2161 | |
| 2162 // get real content-type of message/rfc822 | |
| 2163 if ($struct->mimetype == 'message/rfc822') { | |
| 2164 // single-part | |
| 2165 if (!is_array($part[8][0])) { | |
| 2166 $struct->real_mimetype = strtolower($part[8][0] . '/' . $part[8][1]); | |
| 2167 } | |
| 2168 // multi-part | |
| 2169 else { | |
| 2170 for ($n=0; $n<count($part[8]); $n++) { | |
| 2171 if (!is_array($part[8][$n])) { | |
| 2172 break; | |
| 2173 } | |
| 2174 } | |
| 2175 $struct->real_mimetype = 'multipart/' . strtolower($part[8][$n]); | |
| 2176 } | |
| 2177 } | |
| 2178 | |
| 2179 if ($struct->ctype_primary == 'message' && empty($struct->parts)) { | |
| 2180 if (is_array($part[8]) && $di != 8) { | |
| 2181 $struct->parts[] = $this->structure_part($part[8], ++$count, $struct->mime_id); | |
| 2182 } | |
| 2183 } | |
| 2184 } | |
| 2185 | |
| 2186 // normalize filename property | |
| 2187 $this->set_part_filename($struct, $mime_headers); | |
| 2188 | |
| 2189 return $struct; | |
| 2190 } | |
| 2191 | |
| 2192 /** | |
| 2193 * Set attachment filename from message part structure | |
| 2194 * | |
| 2195 * @param rcube_message_part $part Part object | |
| 2196 * @param string $headers Part's raw headers | |
| 2197 */ | |
| 2198 protected function set_part_filename(&$part, $headers = null) | |
| 2199 { | |
| 2200 if (!empty($part->d_parameters['filename'])) { | |
| 2201 $filename_mime = $part->d_parameters['filename']; | |
| 2202 } | |
| 2203 else if (!empty($part->d_parameters['filename*'])) { | |
| 2204 $filename_encoded = $part->d_parameters['filename*']; | |
| 2205 } | |
| 2206 else if (!empty($part->ctype_parameters['name*'])) { | |
| 2207 $filename_encoded = $part->ctype_parameters['name*']; | |
| 2208 } | |
| 2209 // RFC2231 value continuations | |
| 2210 // TODO: this should be rewrited to support RFC2231 4.1 combinations | |
| 2211 else if (!empty($part->d_parameters['filename*0'])) { | |
| 2212 $i = 0; | |
| 2213 while (isset($part->d_parameters['filename*'.$i])) { | |
| 2214 $filename_mime .= $part->d_parameters['filename*'.$i]; | |
| 2215 $i++; | |
| 2216 } | |
| 2217 // some servers (eg. dovecot-1.x) have no support for parameter value continuations | |
| 2218 // we must fetch and parse headers "manually" | |
| 2219 if ($i<2) { | |
| 2220 if (!$headers) { | |
| 2221 $headers = $this->conn->fetchPartHeader( | |
| 2222 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2223 } | |
| 2224 $filename_mime = ''; | |
| 2225 $i = 0; | |
| 2226 while (preg_match('/filename\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2227 $filename_mime .= $matches[1]; | |
| 2228 $i++; | |
| 2229 } | |
| 2230 } | |
| 2231 } | |
| 2232 else if (!empty($part->d_parameters['filename*0*'])) { | |
| 2233 $i = 0; | |
| 2234 while (isset($part->d_parameters['filename*'.$i.'*'])) { | |
| 2235 $filename_encoded .= $part->d_parameters['filename*'.$i.'*']; | |
| 2236 $i++; | |
| 2237 } | |
| 2238 if ($i<2) { | |
| 2239 if (!$headers) { | |
| 2240 $headers = $this->conn->fetchPartHeader( | |
| 2241 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2242 } | |
| 2243 $filename_encoded = ''; | |
| 2244 $i = 0; $matches = array(); | |
| 2245 while (preg_match('/filename\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2246 $filename_encoded .= $matches[1]; | |
| 2247 $i++; | |
| 2248 } | |
| 2249 } | |
| 2250 } | |
| 2251 else if (!empty($part->ctype_parameters['name*0'])) { | |
| 2252 $i = 0; | |
| 2253 while (isset($part->ctype_parameters['name*'.$i])) { | |
| 2254 $filename_mime .= $part->ctype_parameters['name*'.$i]; | |
| 2255 $i++; | |
| 2256 } | |
| 2257 if ($i<2) { | |
| 2258 if (!$headers) { | |
| 2259 $headers = $this->conn->fetchPartHeader( | |
| 2260 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2261 } | |
| 2262 $filename_mime = ''; | |
| 2263 $i = 0; $matches = array(); | |
| 2264 while (preg_match('/\s+name\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2265 $filename_mime .= $matches[1]; | |
| 2266 $i++; | |
| 2267 } | |
| 2268 } | |
| 2269 } | |
| 2270 else if (!empty($part->ctype_parameters['name*0*'])) { | |
| 2271 $i = 0; | |
| 2272 while (isset($part->ctype_parameters['name*'.$i.'*'])) { | |
| 2273 $filename_encoded .= $part->ctype_parameters['name*'.$i.'*']; | |
| 2274 $i++; | |
| 2275 } | |
| 2276 if ($i<2) { | |
| 2277 if (!$headers) { | |
| 2278 $headers = $this->conn->fetchPartHeader( | |
| 2279 $this->folder, $this->msg_uid, true, $part->mime_id); | |
| 2280 } | |
| 2281 $filename_encoded = ''; | |
| 2282 $i = 0; $matches = array(); | |
| 2283 while (preg_match('/\s+name\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { | |
| 2284 $filename_encoded .= $matches[1]; | |
| 2285 $i++; | |
| 2286 } | |
| 2287 } | |
| 2288 } | |
| 2289 // read 'name' after rfc2231 parameters as it may contains truncated filename (from Thunderbird) | |
| 2290 else if (!empty($part->ctype_parameters['name'])) { | |
| 2291 $filename_mime = $part->ctype_parameters['name']; | |
| 2292 } | |
| 2293 // Content-Disposition | |
| 2294 else if (!empty($part->headers['content-description'])) { | |
| 2295 $filename_mime = $part->headers['content-description']; | |
| 2296 } | |
| 2297 else { | |
| 2298 return; | |
| 2299 } | |
| 2300 | |
| 2301 // decode filename | |
| 2302 if (!empty($filename_mime)) { | |
| 2303 if (!empty($part->charset)) { | |
| 2304 $charset = $part->charset; | |
| 2305 } | |
| 2306 else if (!empty($this->struct_charset)) { | |
| 2307 $charset = $this->struct_charset; | |
| 2308 } | |
| 2309 else { | |
| 2310 $charset = rcube_charset::detect($filename_mime, $this->default_charset); | |
| 2311 } | |
| 2312 | |
| 2313 $part->filename = rcube_mime::decode_mime_string($filename_mime, $charset); | |
| 2314 } | |
| 2315 else if (!empty($filename_encoded)) { | |
| 2316 // decode filename according to RFC 2231, Section 4 | |
| 2317 if (preg_match("/^([^']*)'[^']*'(.*)$/", $filename_encoded, $fmatches)) { | |
| 2318 $filename_charset = $fmatches[1]; | |
| 2319 $filename_encoded = $fmatches[2]; | |
| 2320 } | |
| 2321 | |
| 2322 $part->filename = rcube_charset::convert(urldecode($filename_encoded), $filename_charset); | |
| 2323 } | |
| 2324 } | |
| 2325 | |
| 2326 /** | |
| 2327 * Get charset name from message structure (first part) | |
| 2328 * | |
| 2329 * @param array $structure Message structure | |
| 2330 * | |
| 2331 * @return string Charset name | |
| 2332 */ | |
| 2333 protected function structure_charset($structure) | |
| 2334 { | |
| 2335 while (is_array($structure)) { | |
| 2336 if (is_array($structure[2]) && $structure[2][0] == 'charset') { | |
| 2337 return $structure[2][1]; | |
| 2338 } | |
| 2339 $structure = $structure[0]; | |
| 2340 } | |
| 2341 } | |
| 2342 | |
| 2343 | |
| 2344 /** | |
| 2345 * Fetch message body of a specific message from the server | |
| 2346 * | |
| 2347 * @param int Message UID | |
| 2348 * @param string Part number | |
| 2349 * @param rcube_message_part Part object created by get_structure() | |
| 2350 * @param mixed True to print part, resource to write part contents in | |
| 2351 * @param resource File pointer to save the message part | |
| 2352 * @param boolean Disables charset conversion | |
| 2353 * @param int Only read this number of bytes | |
| 2354 * @param boolean Enables formatting of text/* parts bodies | |
| 2355 * | |
| 2356 * @return string Message/part body if not printed | |
| 2357 */ | |
| 2358 public function get_message_part($uid, $part = 1, $o_part = null, $print = null, $fp = null, | |
| 2359 $skip_charset_conv = false, $max_bytes = 0, $formatted = true) | |
| 2360 { | |
| 2361 if (!$this->check_connection()) { | |
| 2362 return null; | |
| 2363 } | |
| 2364 | |
| 2365 // get part data if not provided | |
| 2366 if (!is_object($o_part)) { | |
| 2367 $structure = $this->conn->getStructure($this->folder, $uid, true); | |
| 2368 $part_data = rcube_imap_generic::getStructurePartData($structure, $part); | |
| 2369 | |
| 2370 $o_part = new rcube_message_part; | |
| 2371 $o_part->ctype_primary = $part_data['type']; | |
| 2372 $o_part->encoding = $part_data['encoding']; | |
| 2373 $o_part->charset = $part_data['charset']; | |
| 2374 $o_part->size = $part_data['size']; | |
| 2375 } | |
| 2376 | |
| 2377 if ($o_part && $o_part->size) { | |
| 2378 $formatted = $formatted && $o_part->ctype_primary == 'text'; | |
| 2379 $body = $this->conn->handlePartBody($this->folder, $uid, true, | |
| 2380 $part ? $part : 'TEXT', $o_part->encoding, $print, $fp, $formatted, $max_bytes); | |
| 2381 } | |
| 2382 | |
| 2383 if ($fp || $print) { | |
| 2384 return true; | |
| 2385 } | |
| 2386 | |
| 2387 // convert charset (if text or message part) | |
| 2388 if ($body && preg_match('/^(text|message)$/', $o_part->ctype_primary)) { | |
| 2389 // Remove NULL characters if any (#1486189) | |
| 2390 if ($formatted && strpos($body, "\x00") !== false) { | |
| 2391 $body = str_replace("\x00", '', $body); | |
| 2392 } | |
| 2393 | |
| 2394 if (!$skip_charset_conv) { | |
| 2395 if (!$o_part->charset || strtoupper($o_part->charset) == 'US-ASCII') { | |
| 2396 // try to extract charset information from HTML meta tag (#1488125) | |
| 2397 if ($o_part->ctype_secondary == 'html' && preg_match('/<meta[^>]+charset=([a-z0-9-_]+)/i', $body, $m)) { | |
| 2398 $o_part->charset = strtoupper($m[1]); | |
| 2399 } | |
| 2400 else { | |
| 2401 $o_part->charset = $this->default_charset; | |
| 2402 } | |
| 2403 } | |
| 2404 $body = rcube_charset::convert($body, $o_part->charset); | |
| 2405 } | |
| 2406 } | |
| 2407 | |
| 2408 return $body; | |
| 2409 } | |
| 2410 | |
| 2411 /** | |
| 2412 * Returns the whole message source as string (or saves to a file) | |
| 2413 * | |
| 2414 * @param int $uid Message UID | |
| 2415 * @param resource $fp File pointer to save the message | |
| 2416 * @param string $part Optional message part ID | |
| 2417 * | |
| 2418 * @return string Message source string | |
| 2419 */ | |
| 2420 public function get_raw_body($uid, $fp=null, $part = null) | |
| 2421 { | |
| 2422 if (!$this->check_connection()) { | |
| 2423 return null; | |
| 2424 } | |
| 2425 | |
| 2426 return $this->conn->handlePartBody($this->folder, $uid, | |
| 2427 true, $part, null, false, $fp); | |
| 2428 } | |
| 2429 | |
| 2430 /** | |
| 2431 * Returns the message headers as string | |
| 2432 * | |
| 2433 * @param int $uid Message UID | |
| 2434 * @param string $part Optional message part ID | |
| 2435 * | |
| 2436 * @return string Message headers string | |
| 2437 */ | |
| 2438 public function get_raw_headers($uid, $part = null) | |
| 2439 { | |
| 2440 if (!$this->check_connection()) { | |
| 2441 return null; | |
| 2442 } | |
| 2443 | |
| 2444 return $this->conn->fetchPartHeader($this->folder, $uid, true, $part); | |
| 2445 } | |
| 2446 | |
| 2447 /** | |
| 2448 * Sends the whole message source to stdout | |
| 2449 * | |
| 2450 * @param int $uid Message UID | |
| 2451 * @param bool $formatted Enables line-ending formatting | |
| 2452 */ | |
| 2453 public function print_raw_body($uid, $formatted = true) | |
| 2454 { | |
| 2455 if (!$this->check_connection()) { | |
| 2456 return; | |
| 2457 } | |
| 2458 | |
| 2459 $this->conn->handlePartBody($this->folder, $uid, true, null, null, true, null, $formatted); | |
| 2460 } | |
| 2461 | |
| 2462 /** | |
| 2463 * Set message flag to one or several messages | |
| 2464 * | |
| 2465 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2466 * @param string $flag Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT | |
| 2467 * @param string $folder Folder name | |
| 2468 * @param boolean $skip_cache True to skip message cache clean up | |
| 2469 * | |
| 2470 * @return boolean Operation status | |
| 2471 */ | |
| 2472 public function set_flag($uids, $flag, $folder=null, $skip_cache=false) | |
| 2473 { | |
| 2474 if (!strlen($folder)) { | |
| 2475 $folder = $this->folder; | |
| 2476 } | |
| 2477 | |
| 2478 if (!$this->check_connection()) { | |
| 2479 return false; | |
| 2480 } | |
| 2481 | |
| 2482 $flag = strtoupper($flag); | |
| 2483 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2484 | |
| 2485 if (strpos($flag, 'UN') === 0) { | |
| 2486 $result = $this->conn->unflag($folder, $uids, substr($flag, 2)); | |
| 2487 } | |
| 2488 else { | |
| 2489 $result = $this->conn->flag($folder, $uids, $flag); | |
| 2490 } | |
| 2491 | |
| 2492 if ($result && !$skip_cache) { | |
| 2493 // reload message headers if cached | |
| 2494 // update flags instead removing from cache | |
| 2495 if ($mcache = $this->get_mcache_engine()) { | |
| 2496 $status = strpos($flag, 'UN') !== 0; | |
| 2497 $mflag = preg_replace('/^UN/', '', $flag); | |
| 2498 $mcache->change_flag($folder, $all_mode ? null : explode(',', $uids), | |
| 2499 $mflag, $status); | |
| 2500 } | |
| 2501 | |
| 2502 // clear cached counters | |
| 2503 if ($flag == 'SEEN' || $flag == 'UNSEEN') { | |
| 2504 $this->clear_messagecount($folder, array('SEEN', 'UNSEEN')); | |
| 2505 } | |
| 2506 else if ($flag == 'DELETED' || $flag == 'UNDELETED') { | |
| 2507 $this->clear_messagecount($folder, array('ALL', 'THREADS')); | |
| 2508 if ($this->options['skip_deleted']) { | |
| 2509 // remove cached messages | |
| 2510 $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids)); | |
| 2511 } | |
| 2512 } | |
| 2513 | |
| 2514 $this->set_search_dirty($folder); | |
| 2515 } | |
| 2516 | |
| 2517 return $result; | |
| 2518 } | |
| 2519 | |
| 2520 /** | |
| 2521 * Append a mail message (source) to a specific folder | |
| 2522 * | |
| 2523 * @param string $folder Target folder | |
| 2524 * @param string|array $message The message source string or filename | |
| 2525 * or array (of strings and file pointers) | |
| 2526 * @param string $headers Headers string if $message contains only the body | |
| 2527 * @param boolean $is_file True if $message is a filename | |
| 2528 * @param array $flags Message flags | |
| 2529 * @param mixed $date Message internal date | |
| 2530 * @param bool $binary Enables BINARY append | |
| 2531 * | |
| 2532 * @return int|bool Appended message UID or True on success, False on error | |
| 2533 */ | |
| 2534 public function save_message($folder, &$message, $headers='', $is_file=false, $flags = array(), $date = null, $binary = false) | |
| 2535 { | |
| 2536 if (!strlen($folder)) { | |
| 2537 $folder = $this->folder; | |
| 2538 } | |
| 2539 | |
| 2540 if (!$this->check_connection()) { | |
| 2541 return false; | |
| 2542 } | |
| 2543 | |
| 2544 // make sure folder exists | |
| 2545 if (!$this->folder_exists($folder)) { | |
| 2546 return false; | |
| 2547 } | |
| 2548 | |
| 2549 $date = $this->date_format($date); | |
| 2550 | |
| 2551 if ($is_file) { | |
| 2552 $saved = $this->conn->appendFromFile($folder, $message, $headers, $flags, $date, $binary); | |
| 2553 } | |
| 2554 else { | |
| 2555 $saved = $this->conn->append($folder, $message, $flags, $date, $binary); | |
| 2556 } | |
| 2557 | |
| 2558 if ($saved) { | |
| 2559 // increase messagecount of the target folder | |
| 2560 $this->set_messagecount($folder, 'ALL', 1); | |
| 2561 | |
| 2562 $this->plugins->exec_hook('message_saved', array( | |
| 2563 'folder' => $folder, | |
| 2564 'message' => $message, | |
| 2565 'headers' => $headers, | |
| 2566 'is_file' => $is_file, | |
| 2567 'flags' => $flags, | |
| 2568 'date' => $date, | |
| 2569 'binary' => $binary, | |
| 2570 'result' => $saved, | |
| 2571 )); | |
| 2572 } | |
| 2573 | |
| 2574 return $saved; | |
| 2575 } | |
| 2576 | |
| 2577 /** | |
| 2578 * Move a message from one folder to another | |
| 2579 * | |
| 2580 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2581 * @param string $to_mbox Target folder | |
| 2582 * @param string $from_mbox Source folder | |
| 2583 * | |
| 2584 * @return boolean True on success, False on error | |
| 2585 */ | |
| 2586 public function move_message($uids, $to_mbox, $from_mbox='') | |
| 2587 { | |
| 2588 if (!strlen($from_mbox)) { | |
| 2589 $from_mbox = $this->folder; | |
| 2590 } | |
| 2591 | |
| 2592 if ($to_mbox === $from_mbox) { | |
| 2593 return false; | |
| 2594 } | |
| 2595 | |
| 2596 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2597 | |
| 2598 // exit if no message uids are specified | |
| 2599 if (empty($uids)) { | |
| 2600 return false; | |
| 2601 } | |
| 2602 | |
| 2603 if (!$this->check_connection()) { | |
| 2604 return false; | |
| 2605 } | |
| 2606 | |
| 2607 $config = rcube::get_instance()->config; | |
| 2608 $to_trash = $to_mbox == $config->get('trash_mbox'); | |
| 2609 | |
| 2610 // flag messages as read before moving them | |
| 2611 if ($to_trash && $config->get('read_when_deleted')) { | |
| 2612 // don't flush cache (4th argument) | |
| 2613 $this->set_flag($uids, 'SEEN', $from_mbox, true); | |
| 2614 } | |
| 2615 | |
| 2616 // move messages | |
| 2617 $moved = $this->conn->move($uids, $from_mbox, $to_mbox); | |
| 2618 | |
| 2619 // when moving to Trash we make sure the folder exists | |
| 2620 // as it's uncommon scenario we do this when MOVE fails, not before | |
| 2621 if (!$moved && $to_trash && $this->get_response_code() == rcube_storage::TRYCREATE) { | |
| 2622 if ($this->create_folder($to_mbox, true, 'trash')) { | |
| 2623 $moved = $this->conn->move($uids, $from_mbox, $to_mbox); | |
| 2624 } | |
| 2625 } | |
| 2626 | |
| 2627 if ($moved) { | |
| 2628 $this->clear_messagecount($from_mbox); | |
| 2629 $this->clear_messagecount($to_mbox); | |
| 2630 | |
| 2631 $this->set_search_dirty($from_mbox); | |
| 2632 $this->set_search_dirty($to_mbox); | |
| 2633 } | |
| 2634 // moving failed | |
| 2635 else if ($to_trash && $config->get('delete_always', false)) { | |
| 2636 $moved = $this->delete_message($uids, $from_mbox); | |
| 2637 } | |
| 2638 | |
| 2639 if ($moved) { | |
| 2640 // unset threads internal cache | |
| 2641 unset($this->icache['threads']); | |
| 2642 | |
| 2643 // remove message ids from search set | |
| 2644 if ($this->search_set && $from_mbox == $this->folder) { | |
| 2645 // threads are too complicated to just remove messages from set | |
| 2646 if ($this->search_threads || $all_mode) { | |
| 2647 $this->refresh_search(); | |
| 2648 } | |
| 2649 else if (!$this->search_set->incomplete) { | |
| 2650 $this->search_set->filter(explode(',', $uids), $this->folder); | |
| 2651 } | |
| 2652 } | |
| 2653 | |
| 2654 // remove cached messages | |
| 2655 // @TODO: do cache update instead of clearing it | |
| 2656 $this->clear_message_cache($from_mbox, $all_mode ? null : explode(',', $uids)); | |
| 2657 } | |
| 2658 | |
| 2659 return $moved; | |
| 2660 } | |
| 2661 | |
| 2662 /** | |
| 2663 * Copy a message from one folder to another | |
| 2664 * | |
| 2665 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2666 * @param string $to_mbox Target folder | |
| 2667 * @param string $from_mbox Source folder | |
| 2668 * | |
| 2669 * @return boolean True on success, False on error | |
| 2670 */ | |
| 2671 public function copy_message($uids, $to_mbox, $from_mbox='') | |
| 2672 { | |
| 2673 if (!strlen($from_mbox)) { | |
| 2674 $from_mbox = $this->folder; | |
| 2675 } | |
| 2676 | |
| 2677 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2678 | |
| 2679 // exit if no message uids are specified | |
| 2680 if (empty($uids)) { | |
| 2681 return false; | |
| 2682 } | |
| 2683 | |
| 2684 if (!$this->check_connection()) { | |
| 2685 return false; | |
| 2686 } | |
| 2687 | |
| 2688 // copy messages | |
| 2689 $copied = $this->conn->copy($uids, $from_mbox, $to_mbox); | |
| 2690 | |
| 2691 if ($copied) { | |
| 2692 $this->clear_messagecount($to_mbox); | |
| 2693 } | |
| 2694 | |
| 2695 return $copied; | |
| 2696 } | |
| 2697 | |
| 2698 /** | |
| 2699 * Mark messages as deleted and expunge them | |
| 2700 * | |
| 2701 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2702 * @param string $folder Source folder | |
| 2703 * | |
| 2704 * @return boolean True on success, False on error | |
| 2705 */ | |
| 2706 public function delete_message($uids, $folder='') | |
| 2707 { | |
| 2708 if (!strlen($folder)) { | |
| 2709 $folder = $this->folder; | |
| 2710 } | |
| 2711 | |
| 2712 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2713 | |
| 2714 // exit if no message uids are specified | |
| 2715 if (empty($uids)) { | |
| 2716 return false; | |
| 2717 } | |
| 2718 | |
| 2719 if (!$this->check_connection()) { | |
| 2720 return false; | |
| 2721 } | |
| 2722 | |
| 2723 $deleted = $this->conn->flag($folder, $uids, 'DELETED'); | |
| 2724 | |
| 2725 if ($deleted) { | |
| 2726 // send expunge command in order to have the deleted message | |
| 2727 // really deleted from the folder | |
| 2728 $this->expunge_message($uids, $folder, false); | |
| 2729 $this->clear_messagecount($folder); | |
| 2730 | |
| 2731 // unset threads internal cache | |
| 2732 unset($this->icache['threads']); | |
| 2733 | |
| 2734 $this->set_search_dirty($folder); | |
| 2735 | |
| 2736 // remove message ids from search set | |
| 2737 if ($this->search_set && $folder == $this->folder) { | |
| 2738 // threads are too complicated to just remove messages from set | |
| 2739 if ($this->search_threads || $all_mode) { | |
| 2740 $this->refresh_search(); | |
| 2741 } | |
| 2742 else if (!$this->search_set->incomplete) { | |
| 2743 $this->search_set->filter(explode(',', $uids)); | |
| 2744 } | |
| 2745 } | |
| 2746 | |
| 2747 // remove cached messages | |
| 2748 $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids)); | |
| 2749 } | |
| 2750 | |
| 2751 return $deleted; | |
| 2752 } | |
| 2753 | |
| 2754 /** | |
| 2755 * Send IMAP expunge command and clear cache | |
| 2756 * | |
| 2757 * @param mixed $uids Message UIDs as array or comma-separated string, or '*' | |
| 2758 * @param string $folder Folder name | |
| 2759 * @param boolean $clear_cache False if cache should not be cleared | |
| 2760 * | |
| 2761 * @return boolean True on success, False on failure | |
| 2762 */ | |
| 2763 public function expunge_message($uids, $folder = null, $clear_cache = true) | |
| 2764 { | |
| 2765 if ($uids && $this->get_capability('UIDPLUS')) { | |
| 2766 list($uids, $all_mode) = $this->parse_uids($uids); | |
| 2767 } | |
| 2768 else { | |
| 2769 $uids = null; | |
| 2770 } | |
| 2771 | |
| 2772 if (!strlen($folder)) { | |
| 2773 $folder = $this->folder; | |
| 2774 } | |
| 2775 | |
| 2776 if (!$this->check_connection()) { | |
| 2777 return false; | |
| 2778 } | |
| 2779 | |
| 2780 // force folder selection and check if folder is writeable | |
| 2781 // to prevent a situation when CLOSE is executed on closed | |
| 2782 // or EXPUNGE on read-only folder | |
| 2783 $result = $this->conn->select($folder); | |
| 2784 if (!$result) { | |
| 2785 return false; | |
| 2786 } | |
| 2787 | |
| 2788 if (!$this->conn->data['READ-WRITE']) { | |
| 2789 $this->conn->setError(rcube_imap_generic::ERROR_READONLY, "Folder is read-only"); | |
| 2790 return false; | |
| 2791 } | |
| 2792 | |
| 2793 // CLOSE(+SELECT) should be faster than EXPUNGE | |
| 2794 if (empty($uids) || $all_mode) { | |
| 2795 $result = $this->conn->close(); | |
| 2796 } | |
| 2797 else { | |
| 2798 $result = $this->conn->expunge($folder, $uids); | |
| 2799 } | |
| 2800 | |
| 2801 if ($result && $clear_cache) { | |
| 2802 $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids)); | |
| 2803 $this->clear_messagecount($folder); | |
| 2804 } | |
| 2805 | |
| 2806 return $result; | |
| 2807 } | |
| 2808 | |
| 2809 | |
| 2810 /* -------------------------------- | |
| 2811 * folder management | |
| 2812 * --------------------------------*/ | |
| 2813 | |
| 2814 /** | |
| 2815 * Public method for listing subscribed folders. | |
| 2816 * | |
| 2817 * @param string $root Optional root folder | |
| 2818 * @param string $name Optional name pattern | |
| 2819 * @param string $filter Optional filter | |
| 2820 * @param string $rights Optional ACL requirements | |
| 2821 * @param bool $skip_sort Enable to return unsorted list (for better performance) | |
| 2822 * | |
| 2823 * @return array List of folders | |
| 2824 */ | |
| 2825 public function list_folders_subscribed($root='', $name='*', $filter=null, $rights=null, $skip_sort=false) | |
| 2826 { | |
| 2827 $cache_key = $root.':'.$name; | |
| 2828 if (!empty($filter)) { | |
| 2829 $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter)); | |
| 2830 } | |
| 2831 $cache_key .= ':'.$rights; | |
| 2832 $cache_key = 'mailboxes.'.md5($cache_key); | |
| 2833 | |
| 2834 // get cached folder list | |
| 2835 $a_mboxes = $this->get_cache($cache_key); | |
| 2836 if (is_array($a_mboxes)) { | |
| 2837 return $a_mboxes; | |
| 2838 } | |
| 2839 | |
| 2840 // Give plugins a chance to provide a list of folders | |
| 2841 $data = $this->plugins->exec_hook('storage_folders', | |
| 2842 array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB')); | |
| 2843 | |
| 2844 if (isset($data['folders'])) { | |
| 2845 $a_mboxes = $data['folders']; | |
| 2846 } | |
| 2847 else { | |
| 2848 $a_mboxes = $this->list_folders_subscribed_direct($root, $name); | |
| 2849 } | |
| 2850 | |
| 2851 if (!is_array($a_mboxes)) { | |
| 2852 return array(); | |
| 2853 } | |
| 2854 | |
| 2855 // filter folders list according to rights requirements | |
| 2856 if ($rights && $this->get_capability('ACL')) { | |
| 2857 $a_mboxes = $this->filter_rights($a_mboxes, $rights); | |
| 2858 } | |
| 2859 | |
| 2860 // INBOX should always be available | |
| 2861 if (in_array_nocase($root . $name, array('*', '%', 'INBOX', 'INBOX*')) | |
| 2862 && (!$filter || $filter == 'mail') && !in_array('INBOX', $a_mboxes) | |
| 2863 ) { | |
| 2864 array_unshift($a_mboxes, 'INBOX'); | |
| 2865 } | |
| 2866 | |
| 2867 // sort folders (always sort for cache) | |
| 2868 if (!$skip_sort || $this->cache) { | |
| 2869 $a_mboxes = $this->sort_folder_list($a_mboxes); | |
| 2870 } | |
| 2871 | |
| 2872 // write folders list to cache | |
| 2873 $this->update_cache($cache_key, $a_mboxes); | |
| 2874 | |
| 2875 return $a_mboxes; | |
| 2876 } | |
| 2877 | |
| 2878 /** | |
| 2879 * Method for direct folders listing (LSUB) | |
| 2880 * | |
| 2881 * @param string $root Optional root folder | |
| 2882 * @param string $name Optional name pattern | |
| 2883 * | |
| 2884 * @return array List of subscribed folders | |
| 2885 * @see rcube_imap::list_folders_subscribed() | |
| 2886 */ | |
| 2887 public function list_folders_subscribed_direct($root='', $name='*') | |
| 2888 { | |
| 2889 if (!$this->check_connection()) { | |
| 2890 return null; | |
| 2891 } | |
| 2892 | |
| 2893 $config = rcube::get_instance()->config; | |
| 2894 | |
| 2895 // Server supports LIST-EXTENDED, we can use selection options | |
| 2896 // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED | |
| 2897 $list_extended = !$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED'); | |
| 2898 if ($list_extended) { | |
| 2899 // This will also set folder options, LSUB doesn't do that | |
| 2900 $result = $this->conn->listMailboxes($root, $name, | |
| 2901 NULL, array('SUBSCRIBED')); | |
| 2902 } | |
| 2903 else { | |
| 2904 // retrieve list of folders from IMAP server using LSUB | |
| 2905 $result = $this->conn->listSubscribed($root, $name); | |
| 2906 } | |
| 2907 | |
| 2908 if (!is_array($result)) { | |
| 2909 return array(); | |
| 2910 } | |
| 2911 | |
| 2912 // #1486796: some server configurations doesn't return folders in all namespaces | |
| 2913 if ($root == '' && $name == '*' && $config->get('imap_force_ns')) { | |
| 2914 $this->list_folders_update($result, ($list_extended ? 'ext-' : '') . 'subscribed'); | |
| 2915 } | |
| 2916 | |
| 2917 // Remove hidden folders | |
| 2918 if ($config->get('imap_skip_hidden_folders')) { | |
| 2919 $result = array_filter($result, function($v) { return $v[0] != '.'; }); | |
| 2920 } | |
| 2921 | |
| 2922 if ($list_extended) { | |
| 2923 // unsubscribe non-existent folders, remove from the list | |
| 2924 if ($name == '*' && !empty($this->conn->data['LIST'])) { | |
| 2925 foreach ($result as $idx => $folder) { | |
| 2926 if (($opts = $this->conn->data['LIST'][$folder]) | |
| 2927 && in_array_nocase('\\NonExistent', $opts) | |
| 2928 ) { | |
| 2929 $this->conn->unsubscribe($folder); | |
| 2930 unset($result[$idx]); | |
| 2931 } | |
| 2932 } | |
| 2933 } | |
| 2934 } | |
| 2935 else { | |
| 2936 // unsubscribe non-existent folders, remove them from the list | |
| 2937 if (!empty($result) && $name == '*') { | |
| 2938 $existing = $this->list_folders($root, $name); | |
| 2939 $nonexisting = array_diff($result, $existing); | |
| 2940 $result = array_diff($result, $nonexisting); | |
| 2941 | |
| 2942 foreach ($nonexisting as $folder) { | |
| 2943 $this->conn->unsubscribe($folder); | |
| 2944 } | |
| 2945 } | |
| 2946 } | |
| 2947 | |
| 2948 return $result; | |
| 2949 } | |
| 2950 | |
| 2951 /** | |
| 2952 * Get a list of all folders available on the server | |
| 2953 * | |
| 2954 * @param string $root IMAP root dir | |
| 2955 * @param string $name Optional name pattern | |
| 2956 * @param mixed $filter Optional filter | |
| 2957 * @param string $rights Optional ACL requirements | |
| 2958 * @param bool $skip_sort Enable to return unsorted list (for better performance) | |
| 2959 * | |
| 2960 * @return array Indexed array with folder names | |
| 2961 */ | |
| 2962 public function list_folders($root='', $name='*', $filter=null, $rights=null, $skip_sort=false) | |
| 2963 { | |
| 2964 $cache_key = $root.':'.$name; | |
| 2965 if (!empty($filter)) { | |
| 2966 $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter)); | |
| 2967 } | |
| 2968 $cache_key .= ':'.$rights; | |
| 2969 $cache_key = 'mailboxes.list.'.md5($cache_key); | |
| 2970 | |
| 2971 // get cached folder list | |
| 2972 $a_mboxes = $this->get_cache($cache_key); | |
| 2973 if (is_array($a_mboxes)) { | |
| 2974 return $a_mboxes; | |
| 2975 } | |
| 2976 | |
| 2977 // Give plugins a chance to provide a list of folders | |
| 2978 $data = $this->plugins->exec_hook('storage_folders', | |
| 2979 array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LIST')); | |
| 2980 | |
| 2981 if (isset($data['folders'])) { | |
| 2982 $a_mboxes = $data['folders']; | |
| 2983 } | |
| 2984 else { | |
| 2985 // retrieve list of folders from IMAP server | |
| 2986 $a_mboxes = $this->list_folders_direct($root, $name); | |
| 2987 } | |
| 2988 | |
| 2989 if (!is_array($a_mboxes)) { | |
| 2990 $a_mboxes = array(); | |
| 2991 } | |
| 2992 | |
| 2993 // INBOX should always be available | |
| 2994 if (in_array_nocase($root . $name, array('*', '%', 'INBOX', 'INBOX*')) | |
| 2995 && (!$filter || $filter == 'mail') && !in_array('INBOX', $a_mboxes) | |
| 2996 ) { | |
| 2997 array_unshift($a_mboxes, 'INBOX'); | |
| 2998 } | |
| 2999 | |
| 3000 // cache folder attributes | |
| 3001 if ($root == '' && $name == '*' && empty($filter) && !empty($this->conn->data)) { | |
| 3002 $this->update_cache('mailboxes.attributes', $this->conn->data['LIST']); | |
| 3003 } | |
| 3004 | |
| 3005 // filter folders list according to rights requirements | |
| 3006 if ($rights && $this->get_capability('ACL')) { | |
| 3007 $a_mboxes = $this->filter_rights($a_mboxes, $rights); | |
| 3008 } | |
| 3009 | |
| 3010 // filter folders and sort them | |
| 3011 if (!$skip_sort) { | |
| 3012 $a_mboxes = $this->sort_folder_list($a_mboxes); | |
| 3013 } | |
| 3014 | |
| 3015 // write folders list to cache | |
| 3016 $this->update_cache($cache_key, $a_mboxes); | |
| 3017 | |
| 3018 return $a_mboxes; | |
| 3019 } | |
| 3020 | |
| 3021 /** | |
| 3022 * Method for direct folders listing (LIST) | |
| 3023 * | |
| 3024 * @param string $root Optional root folder | |
| 3025 * @param string $name Optional name pattern | |
| 3026 * | |
| 3027 * @return array List of folders | |
| 3028 * @see rcube_imap::list_folders() | |
| 3029 */ | |
| 3030 public function list_folders_direct($root='', $name='*') | |
| 3031 { | |
| 3032 if (!$this->check_connection()) { | |
| 3033 return null; | |
| 3034 } | |
| 3035 | |
| 3036 $result = $this->conn->listMailboxes($root, $name); | |
| 3037 | |
| 3038 if (!is_array($result)) { | |
| 3039 return array(); | |
| 3040 } | |
| 3041 | |
| 3042 $config = rcube::get_instance()->config; | |
| 3043 | |
| 3044 // #1486796: some server configurations doesn't return folders in all namespaces | |
| 3045 if ($root == '' && $name == '*' && $config->get('imap_force_ns')) { | |
| 3046 $this->list_folders_update($result); | |
| 3047 } | |
| 3048 | |
| 3049 // Remove hidden folders | |
| 3050 if ($config->get('imap_skip_hidden_folders')) { | |
| 3051 $result = array_filter($result, function($v) { return $v[0] != '.'; }); | |
| 3052 } | |
| 3053 | |
| 3054 return $result; | |
| 3055 } | |
| 3056 | |
| 3057 /** | |
| 3058 * Fix folders list by adding folders from other namespaces. | |
| 3059 * Needed on some servers eg. Courier IMAP | |
| 3060 * | |
| 3061 * @param array $result Reference to folders list | |
| 3062 * @param string $type Listing type (ext-subscribed, subscribed or all) | |
| 3063 */ | |
| 3064 protected function list_folders_update(&$result, $type = null) | |
| 3065 { | |
| 3066 $namespace = $this->get_namespace(); | |
| 3067 $search = array(); | |
| 3068 | |
| 3069 // build list of namespace prefixes | |
| 3070 foreach ((array)$namespace as $ns) { | |
| 3071 if (is_array($ns)) { | |
| 3072 foreach ($ns as $ns_data) { | |
| 3073 if (strlen($ns_data[0])) { | |
| 3074 $search[] = $ns_data[0]; | |
| 3075 } | |
| 3076 } | |
| 3077 } | |
| 3078 } | |
| 3079 | |
| 3080 if (!empty($search)) { | |
| 3081 // go through all folders detecting namespace usage | |
| 3082 foreach ($result as $folder) { | |
| 3083 foreach ($search as $idx => $prefix) { | |
| 3084 if (strpos($folder, $prefix) === 0) { | |
| 3085 unset($search[$idx]); | |
| 3086 } | |
| 3087 } | |
| 3088 if (empty($search)) { | |
| 3089 break; | |
| 3090 } | |
| 3091 } | |
| 3092 | |
| 3093 // get folders in hidden namespaces and add to the result | |
| 3094 foreach ($search as $prefix) { | |
| 3095 if ($type == 'ext-subscribed') { | |
| 3096 $list = $this->conn->listMailboxes('', $prefix . '*', null, array('SUBSCRIBED')); | |
| 3097 } | |
| 3098 else if ($type == 'subscribed') { | |
| 3099 $list = $this->conn->listSubscribed('', $prefix . '*'); | |
| 3100 } | |
| 3101 else { | |
| 3102 $list = $this->conn->listMailboxes('', $prefix . '*'); | |
| 3103 } | |
| 3104 | |
| 3105 if (!empty($list)) { | |
| 3106 $result = array_merge($result, $list); | |
| 3107 } | |
| 3108 } | |
| 3109 } | |
| 3110 } | |
| 3111 | |
| 3112 /** | |
| 3113 * Filter the given list of folders according to access rights | |
| 3114 * | |
| 3115 * For performance reasons we assume user has full rights | |
| 3116 * on all personal folders. | |
| 3117 */ | |
| 3118 protected function filter_rights($a_folders, $rights) | |
| 3119 { | |
| 3120 $regex = '/('.$rights.')/'; | |
| 3121 | |
| 3122 foreach ($a_folders as $idx => $folder) { | |
| 3123 if ($this->folder_namespace($folder) == 'personal') { | |
| 3124 continue; | |
| 3125 } | |
| 3126 | |
| 3127 $myrights = join('', (array)$this->my_rights($folder)); | |
| 3128 | |
| 3129 if ($myrights !== null && !preg_match($regex, $myrights)) { | |
| 3130 unset($a_folders[$idx]); | |
| 3131 } | |
| 3132 } | |
| 3133 | |
| 3134 return $a_folders; | |
| 3135 } | |
| 3136 | |
| 3137 /** | |
| 3138 * Get mailbox quota information | |
| 3139 * | |
| 3140 * @param string $folder Folder name | |
| 3141 * | |
| 3142 * @return mixed Quota info or False if not supported | |
| 3143 */ | |
| 3144 public function get_quota($folder = null) | |
| 3145 { | |
| 3146 if ($this->get_capability('QUOTA') && $this->check_connection()) { | |
| 3147 return $this->conn->getQuota($folder); | |
| 3148 } | |
| 3149 | |
| 3150 return false; | |
| 3151 } | |
| 3152 | |
| 3153 /** | |
| 3154 * Get folder size (size of all messages in a folder) | |
| 3155 * | |
| 3156 * @param string $folder Folder name | |
| 3157 * | |
| 3158 * @return int Folder size in bytes, False on error | |
| 3159 */ | |
| 3160 public function folder_size($folder) | |
| 3161 { | |
| 3162 if (!strlen($folder)) { | |
| 3163 return false; | |
| 3164 } | |
| 3165 | |
| 3166 if (!$this->check_connection()) { | |
| 3167 return 0; | |
| 3168 } | |
| 3169 | |
| 3170 // On Cyrus we can use special folder annotation, which should be much faster | |
| 3171 if ($this->get_vendor() == 'cyrus') { | |
| 3172 $idx = '/shared/vendor/cmu/cyrus-imapd/size'; | |
| 3173 $result = $this->get_metadata($folder, $idx, array(), true); | |
| 3174 | |
| 3175 if (!empty($result) && is_numeric($result[$folder][$idx])) { | |
| 3176 return $result[$folder][$idx]; | |
| 3177 } | |
| 3178 } | |
| 3179 | |
| 3180 // @TODO: could we try to use QUOTA here? | |
| 3181 $result = $this->conn->fetchHeaderIndex($folder, '1:*', 'SIZE', false); | |
| 3182 | |
| 3183 if (is_array($result)) { | |
| 3184 $result = array_sum($result); | |
| 3185 } | |
| 3186 | |
| 3187 return $result; | |
| 3188 } | |
| 3189 | |
| 3190 /** | |
| 3191 * Subscribe to a specific folder(s) | |
| 3192 * | |
| 3193 * @param array $folders Folder name(s) | |
| 3194 * | |
| 3195 * @return boolean True on success | |
| 3196 */ | |
| 3197 public function subscribe($folders) | |
| 3198 { | |
| 3199 // let this common function do the main work | |
| 3200 return $this->change_subscription($folders, 'subscribe'); | |
| 3201 } | |
| 3202 | |
| 3203 /** | |
| 3204 * Unsubscribe folder(s) | |
| 3205 * | |
| 3206 * @param array $a_mboxes Folder name(s) | |
| 3207 * | |
| 3208 * @return boolean True on success | |
| 3209 */ | |
| 3210 public function unsubscribe($folders) | |
| 3211 { | |
| 3212 // let this common function do the main work | |
| 3213 return $this->change_subscription($folders, 'unsubscribe'); | |
| 3214 } | |
| 3215 | |
| 3216 /** | |
| 3217 * Create a new folder on the server and register it in local cache | |
| 3218 * | |
| 3219 * @param string $folder New folder name | |
| 3220 * @param boolean $subscribe True if the new folder should be subscribed | |
| 3221 * @param string $type Optional folder type (junk, trash, drafts, sent, archive) | |
| 3222 * | |
| 3223 * @return boolean True on success | |
| 3224 */ | |
| 3225 public function create_folder($folder, $subscribe = false, $type = null) | |
| 3226 { | |
| 3227 if (!$this->check_connection()) { | |
| 3228 return false; | |
| 3229 } | |
| 3230 | |
| 3231 $result = $this->conn->createFolder($folder, $type ? array("\\" . ucfirst($type)) : null); | |
| 3232 | |
| 3233 // try to subscribe it | |
| 3234 if ($result) { | |
| 3235 // clear cache | |
| 3236 $this->clear_cache('mailboxes', true); | |
| 3237 | |
| 3238 if ($subscribe) { | |
| 3239 $this->subscribe($folder); | |
| 3240 } | |
| 3241 } | |
| 3242 | |
| 3243 return $result; | |
| 3244 } | |
| 3245 | |
| 3246 /** | |
| 3247 * Set a new name to an existing folder | |
| 3248 * | |
| 3249 * @param string $folder Folder to rename | |
| 3250 * @param string $new_name New folder name | |
| 3251 * | |
| 3252 * @return boolean True on success | |
| 3253 */ | |
| 3254 public function rename_folder($folder, $new_name) | |
| 3255 { | |
| 3256 if (!strlen($new_name)) { | |
| 3257 return false; | |
| 3258 } | |
| 3259 | |
| 3260 if (!$this->check_connection()) { | |
| 3261 return false; | |
| 3262 } | |
| 3263 | |
| 3264 $delm = $this->get_hierarchy_delimiter(); | |
| 3265 | |
| 3266 // get list of subscribed folders | |
| 3267 if ((strpos($folder, '%') === false) && (strpos($folder, '*') === false)) { | |
| 3268 $a_subscribed = $this->list_folders_subscribed('', $folder . $delm . '*'); | |
| 3269 $subscribed = $this->folder_exists($folder, true); | |
| 3270 } | |
| 3271 else { | |
| 3272 $a_subscribed = $this->list_folders_subscribed(); | |
| 3273 $subscribed = in_array($folder, $a_subscribed); | |
| 3274 } | |
| 3275 | |
| 3276 $result = $this->conn->renameFolder($folder, $new_name); | |
| 3277 | |
| 3278 if ($result) { | |
| 3279 // unsubscribe the old folder, subscribe the new one | |
| 3280 if ($subscribed) { | |
| 3281 $this->conn->unsubscribe($folder); | |
| 3282 $this->conn->subscribe($new_name); | |
| 3283 } | |
| 3284 | |
| 3285 // check if folder children are subscribed | |
| 3286 foreach ($a_subscribed as $c_subscribed) { | |
| 3287 if (strpos($c_subscribed, $folder.$delm) === 0) { | |
| 3288 $this->conn->unsubscribe($c_subscribed); | |
| 3289 $this->conn->subscribe(preg_replace('/^'.preg_quote($folder, '/').'/', | |
| 3290 $new_name, $c_subscribed)); | |
| 3291 | |
| 3292 // clear cache | |
| 3293 $this->clear_message_cache($c_subscribed); | |
| 3294 } | |
| 3295 } | |
| 3296 | |
| 3297 // clear cache | |
| 3298 $this->clear_message_cache($folder); | |
| 3299 $this->clear_cache('mailboxes', true); | |
| 3300 } | |
| 3301 | |
| 3302 return $result; | |
| 3303 } | |
| 3304 | |
| 3305 /** | |
| 3306 * Remove folder (with subfolders) from the server | |
| 3307 * | |
| 3308 * @param string $folder Folder name | |
| 3309 * | |
| 3310 * @return boolean True on success, False on failure | |
| 3311 */ | |
| 3312 function delete_folder($folder) | |
| 3313 { | |
| 3314 if (!$this->check_connection()) { | |
| 3315 return false; | |
| 3316 } | |
| 3317 | |
| 3318 $delm = $this->get_hierarchy_delimiter(); | |
| 3319 | |
| 3320 // get list of sub-folders or all folders | |
| 3321 // if folder name contains special characters | |
| 3322 $path = strspn($folder, '%*') > 0 ? ($folder . $delm) : ''; | |
| 3323 $sub_mboxes = $this->list_folders('', $path . '*'); | |
| 3324 | |
| 3325 // According to RFC3501 deleting a \Noselect folder | |
| 3326 // with subfolders may fail. To workaround this we delete | |
| 3327 // subfolders first (in reverse order) (#5466) | |
| 3328 if (!empty($sub_mboxes)) { | |
| 3329 foreach (array_reverse($sub_mboxes) as $mbox) { | |
| 3330 if (strpos($mbox, $folder . $delm) === 0) { | |
| 3331 if ($this->conn->deleteFolder($mbox)) { | |
| 3332 $this->conn->unsubscribe($mbox); | |
| 3333 $this->clear_message_cache($mbox); | |
| 3334 } | |
| 3335 } | |
| 3336 } | |
| 3337 } | |
| 3338 | |
| 3339 // delete the folder | |
| 3340 if ($result = $this->conn->deleteFolder($folder)) { | |
| 3341 // and unsubscribe it | |
| 3342 $this->conn->unsubscribe($folder); | |
| 3343 $this->clear_message_cache($folder); | |
| 3344 } | |
| 3345 | |
| 3346 $this->clear_cache('mailboxes', true); | |
| 3347 | |
| 3348 return $result; | |
| 3349 } | |
| 3350 | |
| 3351 /** | |
| 3352 * Detect special folder associations stored in storage backend | |
| 3353 */ | |
| 3354 public function get_special_folders($forced = false) | |
| 3355 { | |
| 3356 $result = parent::get_special_folders(); | |
| 3357 $rcube = rcube::get_instance(); | |
| 3358 | |
| 3359 // Lock SPECIAL-USE after user preferences change (#4782) | |
| 3360 if ($rcube->config->get('lock_special_folders')) { | |
| 3361 return $result; | |
| 3362 } | |
| 3363 | |
| 3364 if (isset($this->icache['special-use'])) { | |
| 3365 return array_merge($result, $this->icache['special-use']); | |
| 3366 } | |
| 3367 | |
| 3368 if (!$forced || !$this->get_capability('SPECIAL-USE')) { | |
| 3369 return $result; | |
| 3370 } | |
| 3371 | |
| 3372 if (!$this->check_connection()) { | |
| 3373 return $result; | |
| 3374 } | |
| 3375 | |
| 3376 $types = array_map(function($value) { return "\\" . ucfirst($value); }, rcube_storage::$folder_types); | |
| 3377 $special = array(); | |
| 3378 | |
| 3379 // request \Subscribed flag in LIST response as performance improvement for folder_exists() | |
| 3380 $folders = $this->conn->listMailboxes('', '*', array('SUBSCRIBED'), array('SPECIAL-USE')); | |
| 3381 | |
| 3382 if (!empty($folders)) { | |
| 3383 foreach ($folders as $folder) { | |
| 3384 if ($flags = $this->conn->data['LIST'][$folder]) { | |
| 3385 foreach ($types as $type) { | |
| 3386 if (in_array($type, $flags)) { | |
| 3387 $type = strtolower(substr($type, 1)); | |
| 3388 $special[$type] = $folder; | |
| 3389 } | |
| 3390 } | |
| 3391 } | |
| 3392 } | |
| 3393 } | |
| 3394 | |
| 3395 $this->icache['special-use'] = $special; | |
| 3396 unset($this->icache['special-folders']); | |
| 3397 | |
| 3398 return array_merge($result, $special); | |
| 3399 } | |
| 3400 | |
| 3401 /** | |
| 3402 * Set special folder associations stored in storage backend | |
| 3403 */ | |
| 3404 public function set_special_folders($specials) | |
| 3405 { | |
| 3406 if (!$this->get_capability('SPECIAL-USE') || !$this->get_capability('METADATA')) { | |
| 3407 return false; | |
| 3408 } | |
| 3409 | |
| 3410 if (!$this->check_connection()) { | |
| 3411 return false; | |
| 3412 } | |
| 3413 | |
| 3414 $folders = $this->get_special_folders(true); | |
| 3415 $old = (array) $this->icache['special-use']; | |
| 3416 | |
| 3417 foreach ($specials as $type => $folder) { | |
| 3418 if (in_array($type, rcube_storage::$folder_types)) { | |
| 3419 $old_folder = $old[$type]; | |
| 3420 if ($old_folder !== $folder) { | |
| 3421 // unset old-folder metadata | |
| 3422 if ($old_folder !== null) { | |
| 3423 $this->delete_metadata($old_folder, array('/private/specialuse')); | |
| 3424 } | |
| 3425 // set new folder metadata | |
| 3426 if ($folder) { | |
| 3427 $this->set_metadata($folder, array('/private/specialuse' => "\\" . ucfirst($type))); | |
| 3428 } | |
| 3429 } | |
| 3430 } | |
| 3431 } | |
| 3432 | |
| 3433 $this->icache['special-use'] = $specials; | |
| 3434 unset($this->icache['special-folders']); | |
| 3435 | |
| 3436 return true; | |
| 3437 } | |
| 3438 | |
| 3439 /** | |
| 3440 * Checks if folder exists and is subscribed | |
| 3441 * | |
| 3442 * @param string $folder Folder name | |
| 3443 * @param boolean $subscription Enable subscription checking | |
| 3444 * | |
| 3445 * @return boolean TRUE or FALSE | |
| 3446 */ | |
| 3447 public function folder_exists($folder, $subscription = false) | |
| 3448 { | |
| 3449 if ($folder == 'INBOX') { | |
| 3450 return true; | |
| 3451 } | |
| 3452 | |
| 3453 $key = $subscription ? 'subscribed' : 'existing'; | |
| 3454 | |
| 22 | 3455 if (is_array($this->icache[$key]??null) && in_array($folder, $this->icache[$key])) { |
| 0 | 3456 return true; |
| 3457 } | |
| 3458 | |
| 3459 if (!$this->check_connection()) { | |
| 3460 return false; | |
| 3461 } | |
| 3462 | |
| 3463 if ($subscription) { | |
| 3464 // It's possible we already called LIST command, check LIST data | |
| 3465 if (!empty($this->conn->data['LIST']) && !empty($this->conn->data['LIST'][$folder]) | |
| 3466 && in_array_nocase('\\Subscribed', $this->conn->data['LIST'][$folder]) | |
| 3467 ) { | |
| 3468 $a_folders = array($folder); | |
| 3469 } | |
| 3470 else { | |
| 3471 $a_folders = $this->conn->listSubscribed('', $folder); | |
| 3472 } | |
| 3473 } | |
| 3474 else { | |
| 3475 // It's possible we already called LIST command, check LIST data | |
| 3476 if (!empty($this->conn->data['LIST']) && isset($this->conn->data['LIST'][$folder])) { | |
| 3477 $a_folders = array($folder); | |
| 3478 } | |
| 3479 else { | |
| 3480 $a_folders = $this->conn->listMailboxes('', $folder); | |
| 3481 } | |
| 3482 } | |
| 3483 | |
| 3484 if (is_array($a_folders) && in_array($folder, $a_folders)) { | |
| 3485 $this->icache[$key][] = $folder; | |
| 3486 return true; | |
| 3487 } | |
| 3488 | |
| 3489 return false; | |
| 3490 } | |
| 3491 | |
| 3492 /** | |
| 3493 * Returns the namespace where the folder is in | |
| 3494 * | |
| 3495 * @param string $folder Folder name | |
| 3496 * | |
| 3497 * @return string One of 'personal', 'other' or 'shared' | |
| 3498 */ | |
| 3499 public function folder_namespace($folder) | |
| 3500 { | |
| 3501 if ($folder == 'INBOX') { | |
| 3502 return 'personal'; | |
| 3503 } | |
| 3504 | |
| 3505 foreach ($this->namespace as $type => $namespace) { | |
| 3506 if (is_array($namespace)) { | |
| 3507 foreach ($namespace as $ns) { | |
| 3508 if ($len = strlen($ns[0])) { | |
| 3509 if (($len > 1 && $folder == substr($ns[0], 0, -1)) | |
| 3510 || strpos($folder, $ns[0]) === 0 | |
| 3511 ) { | |
| 3512 return $type; | |
| 3513 } | |
| 3514 } | |
| 3515 } | |
| 3516 } | |
| 3517 } | |
| 3518 | |
| 3519 return 'personal'; | |
| 3520 } | |
| 3521 | |
| 3522 /** | |
| 3523 * Modify folder name according to personal namespace prefix. | |
| 3524 * For output it removes prefix of the personal namespace if it's possible. | |
| 3525 * For input it adds the prefix. Use it before creating a folder in root | |
| 3526 * of the folders tree. | |
| 3527 * | |
| 3528 * @param string $folder Folder name | |
| 3529 * @param string $mode Mode name (out/in) | |
| 3530 * | |
| 3531 * @return string Folder name | |
| 3532 */ | |
| 3533 public function mod_folder($folder, $mode = 'out') | |
| 3534 { | |
| 3535 $prefix = $this->namespace['prefix_' . $mode]; // see set_env() | |
| 3536 | |
| 3537 if ($prefix === null || $prefix === '' | |
| 3538 || !($prefix_len = strlen($prefix)) || !strlen($folder) | |
| 3539 ) { | |
| 3540 return $folder; | |
| 3541 } | |
| 3542 | |
| 3543 // remove prefix for output | |
| 3544 if ($mode == 'out') { | |
| 3545 if (substr($folder, 0, $prefix_len) === $prefix) { | |
| 3546 return substr($folder, $prefix_len); | |
| 3547 } | |
| 3548 | |
| 3549 return $folder; | |
| 3550 } | |
| 3551 | |
| 3552 // add prefix for input (e.g. folder creation) | |
| 3553 return $prefix . $folder; | |
| 3554 } | |
| 3555 | |
| 3556 /** | |
| 3557 * Gets folder attributes from LIST response, e.g. \Noselect, \Noinferiors | |
| 3558 * | |
| 3559 * @param string $folder Folder name | |
| 3560 * @param bool $force Set to True if attributes should be refreshed | |
| 3561 * | |
| 3562 * @return array Options list | |
| 3563 */ | |
| 3564 public function folder_attributes($folder, $force=false) | |
| 3565 { | |
| 3566 // get attributes directly from LIST command | |
| 3567 if (!empty($this->conn->data['LIST']) && is_array($this->conn->data['LIST'][$folder])) { | |
| 3568 $opts = $this->conn->data['LIST'][$folder]; | |
| 3569 } | |
| 3570 // get cached folder attributes | |
| 3571 else if (!$force) { | |
| 3572 $opts = $this->get_cache('mailboxes.attributes'); | |
| 3573 $opts = $opts[$folder]; | |
| 3574 } | |
| 3575 | |
| 3576 if (!is_array($opts)) { | |
| 3577 if (!$this->check_connection()) { | |
| 3578 return array(); | |
| 3579 } | |
| 3580 | |
| 3581 $this->conn->listMailboxes('', $folder); | |
| 3582 $opts = $this->conn->data['LIST'][$folder]; | |
| 3583 } | |
| 3584 | |
| 3585 return is_array($opts) ? $opts : array(); | |
| 3586 } | |
| 3587 | |
| 3588 /** | |
| 3589 * Gets connection (and current folder) data: UIDVALIDITY, EXISTS, RECENT, | |
| 3590 * PERMANENTFLAGS, UIDNEXT, UNSEEN | |
| 3591 * | |
| 3592 * @param string $folder Folder name | |
| 3593 * | |
| 3594 * @return array Data | |
| 3595 */ | |
| 3596 public function folder_data($folder) | |
| 3597 { | |
| 3598 if (!strlen($folder)) { | |
| 3599 $folder = $this->folder !== null ? $this->folder : 'INBOX'; | |
| 3600 } | |
| 3601 | |
| 3602 if ($this->conn->selected != $folder) { | |
| 3603 if (!$this->check_connection()) { | |
| 3604 return array(); | |
| 3605 } | |
| 3606 | |
| 3607 if ($this->conn->select($folder)) { | |
| 3608 $this->folder = $folder; | |
| 3609 } | |
| 3610 else { | |
| 3611 return null; | |
| 3612 } | |
| 3613 } | |
| 3614 | |
| 3615 $data = $this->conn->data; | |
| 3616 | |
| 3617 // add (E)SEARCH result for ALL UNDELETED query | |
| 3618 if (!empty($this->icache['undeleted_idx']) | |
| 3619 && $this->icache['undeleted_idx']->get_parameters('MAILBOX') == $folder | |
| 3620 ) { | |
| 3621 $data['UNDELETED'] = $this->icache['undeleted_idx']; | |
| 3622 } | |
| 3623 | |
| 3624 return $data; | |
| 3625 } | |
| 3626 | |
| 3627 /** | |
| 3628 * Returns extended information about the folder | |
| 3629 * | |
| 3630 * @param string $folder Folder name | |
| 3631 * | |
| 3632 * @return array Data | |
| 3633 */ | |
| 3634 public function folder_info($folder) | |
| 3635 { | |
| 3636 if ($this->icache['options'] && $this->icache['options']['name'] == $folder) { | |
| 3637 return $this->icache['options']; | |
| 3638 } | |
| 3639 | |
| 3640 // get cached metadata | |
| 3641 $cache_key = 'mailboxes.folder-info.' . $folder; | |
| 3642 $cached = $this->get_cache($cache_key); | |
| 3643 | |
| 3644 if (is_array($cached)) { | |
| 3645 return $cached; | |
| 3646 } | |
| 3647 | |
| 3648 $acl = $this->get_capability('ACL'); | |
| 3649 $namespace = $this->get_namespace(); | |
| 3650 $options = array(); | |
| 3651 | |
| 3652 // check if the folder is a namespace prefix | |
| 3653 if (!empty($namespace)) { | |
| 3654 $mbox = $folder . $this->delimiter; | |
| 3655 foreach ($namespace as $ns) { | |
| 3656 if (!empty($ns)) { | |
| 3657 foreach ($ns as $item) { | |
| 3658 if ($item[0] === $mbox) { | |
| 3659 $options['is_root'] = true; | |
| 3660 break 2; | |
| 3661 } | |
| 3662 } | |
| 3663 } | |
| 3664 } | |
| 3665 } | |
| 3666 // check if the folder is other user virtual-root | |
| 3667 if (!$options['is_root'] && !empty($namespace) && !empty($namespace['other'])) { | |
| 3668 $parts = explode($this->delimiter, $folder); | |
| 3669 if (count($parts) == 2) { | |
| 3670 $mbox = $parts[0] . $this->delimiter; | |
| 3671 foreach ($namespace['other'] as $item) { | |
| 3672 if ($item[0] === $mbox) { | |
| 3673 $options['is_root'] = true; | |
| 3674 break; | |
| 3675 } | |
| 3676 } | |
| 3677 } | |
| 3678 } | |
| 3679 | |
| 3680 $options['name'] = $folder; | |
| 3681 $options['attributes'] = $this->folder_attributes($folder, true); | |
| 3682 $options['namespace'] = $this->folder_namespace($folder); | |
| 3683 $options['special'] = $this->is_special_folder($folder); | |
| 3684 | |
| 3685 // Set 'noselect' flag | |
| 3686 if (is_array($options['attributes'])) { | |
| 3687 foreach ($options['attributes'] as $attrib) { | |
| 3688 $attrib = strtolower($attrib); | |
| 3689 if ($attrib == '\noselect' || $attrib == '\nonexistent') { | |
| 3690 $options['noselect'] = true; | |
| 3691 } | |
| 3692 } | |
| 3693 } | |
| 3694 else { | |
| 3695 $options['noselect'] = true; | |
| 3696 } | |
| 3697 | |
| 3698 // Get folder rights (MYRIGHTS) | |
| 3699 if ($acl && ($rights = $this->my_rights($folder))) { | |
| 3700 $options['rights'] = $rights; | |
| 3701 } | |
| 3702 | |
| 3703 // Set 'norename' flag | |
| 3704 if (!empty($options['rights'])) { | |
| 3705 $options['norename'] = !in_array('x', $options['rights']) && !in_array('d', $options['rights']); | |
| 3706 | |
| 3707 if (!$options['noselect']) { | |
| 3708 $options['noselect'] = !in_array('r', $options['rights']); | |
| 3709 } | |
| 3710 } | |
| 3711 else { | |
| 3712 $options['norename'] = $options['is_root'] || $options['namespace'] != 'personal'; | |
| 3713 } | |
| 3714 | |
| 3715 // update caches | |
| 3716 $this->icache['options'] = $options; | |
| 3717 $this->update_cache($cache_key, $options); | |
| 3718 | |
| 3719 return $options; | |
| 3720 } | |
| 3721 | |
| 3722 /** | |
| 3723 * Synchronizes messages cache. | |
| 3724 * | |
| 3725 * @param string $folder Folder name | |
| 3726 */ | |
| 3727 public function folder_sync($folder) | |
| 3728 { | |
| 3729 if ($mcache = $this->get_mcache_engine()) { | |
| 3730 $mcache->synchronize($folder); | |
| 3731 } | |
| 3732 } | |
| 3733 | |
| 3734 /** | |
| 3735 * Get message header names for rcube_imap_generic::fetchHeader(s) | |
| 3736 * | |
| 3737 * @return string Space-separated list of header names | |
| 3738 */ | |
| 3739 protected function get_fetch_headers() | |
| 3740 { | |
| 3741 if (!empty($this->options['fetch_headers'])) { | |
| 3742 $headers = explode(' ', $this->options['fetch_headers']); | |
| 3743 } | |
| 3744 else { | |
| 3745 $headers = array(); | |
| 3746 } | |
| 3747 | |
| 19 | 3748 if ($this->messages_caching || !empty($this->options['all_headers'])) { |
| 0 | 3749 $headers = array_merge($headers, $this->all_headers); |
| 3750 } | |
| 3751 | |
| 3752 return $headers; | |
| 3753 } | |
| 3754 | |
| 3755 | |
| 3756 /* ----------------------------------------- | |
| 3757 * ACL and METADATA/ANNOTATEMORE methods | |
| 3758 * ----------------------------------------*/ | |
| 3759 | |
| 3760 /** | |
| 3761 * Changes the ACL on the specified folder (SETACL) | |
| 3762 * | |
| 3763 * @param string $folder Folder name | |
| 3764 * @param string $user User name | |
| 3765 * @param string $acl ACL string | |
| 3766 * | |
| 3767 * @return boolean True on success, False on failure | |
| 3768 * @since 0.5-beta | |
| 3769 */ | |
| 3770 public function set_acl($folder, $user, $acl) | |
| 3771 { | |
| 3772 if (!$this->get_capability('ACL')) { | |
| 3773 return false; | |
| 3774 } | |
| 3775 | |
| 3776 if (!$this->check_connection()) { | |
| 3777 return false; | |
| 3778 } | |
| 3779 | |
| 3780 $this->clear_cache('mailboxes.folder-info.' . $folder); | |
| 3781 | |
| 3782 return $this->conn->setACL($folder, $user, $acl); | |
| 3783 } | |
| 3784 | |
| 3785 /** | |
| 3786 * Removes any <identifier,rights> pair for the | |
| 3787 * specified user from the ACL for the specified | |
| 3788 * folder (DELETEACL) | |
| 3789 * | |
| 3790 * @param string $folder Folder name | |
| 3791 * @param string $user User name | |
| 3792 * | |
| 3793 * @return boolean True on success, False on failure | |
| 3794 * @since 0.5-beta | |
| 3795 */ | |
| 3796 public function delete_acl($folder, $user) | |
| 3797 { | |
| 3798 if (!$this->get_capability('ACL')) { | |
| 3799 return false; | |
| 3800 } | |
| 3801 | |
| 3802 if (!$this->check_connection()) { | |
| 3803 return false; | |
| 3804 } | |
| 3805 | |
| 3806 return $this->conn->deleteACL($folder, $user); | |
| 3807 } | |
| 3808 | |
| 3809 /** | |
| 3810 * Returns the access control list for folder (GETACL) | |
| 3811 * | |
| 3812 * @param string $folder Folder name | |
| 3813 * | |
| 3814 * @return array User-rights array on success, NULL on error | |
| 3815 * @since 0.5-beta | |
| 3816 */ | |
| 3817 public function get_acl($folder) | |
| 3818 { | |
| 3819 if (!$this->get_capability('ACL')) { | |
| 3820 return null; | |
| 3821 } | |
| 3822 | |
| 3823 if (!$this->check_connection()) { | |
| 3824 return null; | |
| 3825 } | |
| 3826 | |
| 3827 return $this->conn->getACL($folder); | |
| 3828 } | |
| 3829 | |
| 3830 /** | |
| 3831 * Returns information about what rights can be granted to the | |
| 3832 * user (identifier) in the ACL for the folder (LISTRIGHTS) | |
| 3833 * | |
| 3834 * @param string $folder Folder name | |
| 3835 * @param string $user User name | |
| 3836 * | |
| 3837 * @return array List of user rights | |
| 3838 * @since 0.5-beta | |
| 3839 */ | |
| 3840 public function list_rights($folder, $user) | |
| 3841 { | |
| 3842 if (!$this->get_capability('ACL')) { | |
| 3843 return null; | |
| 3844 } | |
| 3845 | |
| 3846 if (!$this->check_connection()) { | |
| 3847 return null; | |
| 3848 } | |
| 3849 | |
| 3850 return $this->conn->listRights($folder, $user); | |
| 3851 } | |
| 3852 | |
| 3853 /** | |
| 3854 * Returns the set of rights that the current user has to | |
| 3855 * folder (MYRIGHTS) | |
| 3856 * | |
| 3857 * @param string $folder Folder name | |
| 3858 * | |
| 3859 * @return array MYRIGHTS response on success, NULL on error | |
| 3860 * @since 0.5-beta | |
| 3861 */ | |
| 3862 public function my_rights($folder) | |
| 3863 { | |
| 3864 if (!$this->get_capability('ACL')) { | |
| 3865 return null; | |
| 3866 } | |
| 3867 | |
| 3868 if (!$this->check_connection()) { | |
| 3869 return null; | |
| 3870 } | |
| 3871 | |
| 3872 return $this->conn->myRights($folder); | |
| 3873 } | |
| 3874 | |
| 3875 /** | |
| 3876 * Sets IMAP metadata/annotations (SETMETADATA/SETANNOTATION) | |
| 3877 * | |
| 3878 * @param string $folder Folder name (empty for server metadata) | |
| 3879 * @param array $entries Entry-value array (use NULL value as NIL) | |
| 3880 * | |
| 3881 * @return boolean True on success, False on failure | |
| 3882 * @since 0.5-beta | |
| 3883 */ | |
| 3884 public function set_metadata($folder, $entries) | |
| 3885 { | |
| 3886 if (!$this->check_connection()) { | |
| 3887 return false; | |
| 3888 } | |
| 3889 | |
| 3890 $this->clear_cache('mailboxes.metadata.', true); | |
| 3891 | |
| 3892 if ($this->get_capability('METADATA') || | |
| 3893 (!strlen($folder) && $this->get_capability('METADATA-SERVER')) | |
| 3894 ) { | |
| 3895 return $this->conn->setMetadata($folder, $entries); | |
| 3896 } | |
| 3897 else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { | |
| 3898 foreach ((array)$entries as $entry => $value) { | |
| 3899 list($ent, $attr) = $this->md2annotate($entry); | |
| 3900 $entries[$entry] = array($ent, $attr, $value); | |
| 3901 } | |
| 3902 return $this->conn->setAnnotation($folder, $entries); | |
| 3903 } | |
| 3904 | |
| 3905 return false; | |
| 3906 } | |
| 3907 | |
| 3908 /** | |
| 3909 * Unsets IMAP metadata/annotations (SETMETADATA/SETANNOTATION) | |
| 3910 * | |
| 3911 * @param string $folder Folder name (empty for server metadata) | |
| 3912 * @param array $entries Entry names array | |
| 3913 * | |
| 3914 * @return boolean True on success, False on failure | |
| 3915 * @since 0.5-beta | |
| 3916 */ | |
| 3917 public function delete_metadata($folder, $entries) | |
| 3918 { | |
| 3919 if (!$this->check_connection()) { | |
| 3920 return false; | |
| 3921 } | |
| 3922 | |
| 3923 $this->clear_cache('mailboxes.metadata.', true); | |
| 3924 | |
| 3925 if ($this->get_capability('METADATA') || | |
| 3926 (!strlen($folder) && $this->get_capability('METADATA-SERVER')) | |
| 3927 ) { | |
| 3928 return $this->conn->deleteMetadata($folder, $entries); | |
| 3929 } | |
| 3930 else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { | |
| 3931 foreach ((array)$entries as $idx => $entry) { | |
| 3932 list($ent, $attr) = $this->md2annotate($entry); | |
| 3933 $entries[$idx] = array($ent, $attr, NULL); | |
| 3934 } | |
| 3935 return $this->conn->setAnnotation($folder, $entries); | |
| 3936 } | |
| 3937 | |
| 3938 return false; | |
| 3939 } | |
| 3940 | |
| 3941 /** | |
| 3942 * Returns IMAP metadata/annotations (GETMETADATA/GETANNOTATION) | |
| 3943 * | |
| 3944 * @param string $folder Folder name (empty for server metadata) | |
| 3945 * @param array $entries Entries | |
| 3946 * @param array $options Command options (with MAXSIZE and DEPTH keys) | |
| 3947 * @param bool $force Disables cache use | |
| 3948 * | |
| 3949 * @return array Metadata entry-value hash array on success, NULL on error | |
| 3950 * @since 0.5-beta | |
| 3951 */ | |
| 3952 public function get_metadata($folder, $entries, $options = array(), $force = false) | |
| 3953 { | |
| 3954 $entries = (array) $entries; | |
| 3955 | |
| 3956 if (!$force) { | |
| 3957 // create cache key | |
| 3958 // @TODO: this is the simplest solution, but we do the same with folders list | |
| 3959 // maybe we should store data per-entry and merge on request | |
| 3960 sort($options); | |
| 3961 sort($entries); | |
| 3962 $cache_key = 'mailboxes.metadata.' . $folder; | |
| 3963 $cache_key .= '.' . md5(serialize($options).serialize($entries)); | |
| 3964 | |
| 3965 // get cached data | |
| 3966 $cached_data = $this->get_cache($cache_key); | |
| 3967 | |
| 3968 if (is_array($cached_data)) { | |
| 3969 return $cached_data; | |
| 3970 } | |
| 3971 } | |
| 3972 | |
| 3973 if (!$this->check_connection()) { | |
| 3974 return null; | |
| 3975 } | |
| 3976 | |
| 3977 if ($this->get_capability('METADATA') || | |
| 3978 (!strlen($folder) && $this->get_capability('METADATA-SERVER')) | |
| 3979 ) { | |
| 3980 $res = $this->conn->getMetadata($folder, $entries, $options); | |
| 3981 } | |
| 3982 else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { | |
| 3983 $queries = array(); | |
| 3984 $res = array(); | |
| 3985 | |
| 3986 // Convert entry names | |
| 3987 foreach ($entries as $entry) { | |
| 3988 list($ent, $attr) = $this->md2annotate($entry); | |
| 3989 $queries[$attr][] = $ent; | |
| 3990 } | |
| 3991 | |
| 3992 // @TODO: Honor MAXSIZE and DEPTH options | |
| 3993 foreach ($queries as $attrib => $entry) { | |
| 3994 $result = $this->conn->getAnnotation($folder, $entry, $attrib); | |
| 3995 | |
| 3996 // an error, invalidate any previous getAnnotation() results | |
| 3997 if (!is_array($result)) { | |
| 3998 return null; | |
| 3999 } | |
| 4000 else { | |
| 4001 foreach ($result as $fldr => $data) { | |
| 4002 $res[$fldr] = array_merge((array) $res[$fldr], $data); | |
| 4003 } | |
| 4004 } | |
| 4005 } | |
| 4006 } | |
| 4007 | |
| 4008 if (isset($res)) { | |
| 4009 if (!$force) { | |
| 4010 $this->update_cache($cache_key, $res); | |
| 4011 } | |
| 4012 | |
| 4013 return $res; | |
| 4014 } | |
| 4015 } | |
| 4016 | |
| 4017 /** | |
| 4018 * Converts the METADATA extension entry name into the correct | |
| 4019 * entry-attrib names for older ANNOTATEMORE version. | |
| 4020 * | |
| 4021 * @param string $entry Entry name | |
| 4022 * | |
| 4023 * @return array Entry-attribute list, NULL if not supported (?) | |
| 4024 */ | |
| 4025 protected function md2annotate($entry) | |
| 4026 { | |
| 4027 if (substr($entry, 0, 7) == '/shared') { | |
| 4028 return array(substr($entry, 7), 'value.shared'); | |
| 4029 } | |
| 4030 else if (substr($entry, 0, 8) == '/private') { | |
| 4031 return array(substr($entry, 8), 'value.priv'); | |
| 4032 } | |
| 4033 | |
| 4034 // @TODO: log error | |
| 4035 } | |
| 4036 | |
| 4037 | |
| 4038 /* -------------------------------- | |
| 4039 * internal caching methods | |
| 4040 * --------------------------------*/ | |
| 4041 | |
| 4042 /** | |
| 4043 * Enable or disable indexes caching | |
| 4044 * | |
| 4045 * @param string $type Cache type (@see rcube::get_cache) | |
| 4046 */ | |
| 4047 public function set_caching($type) | |
| 4048 { | |
| 4049 if ($type) { | |
| 4050 $this->caching = $type; | |
| 4051 } | |
| 4052 else { | |
| 4053 if ($this->cache) { | |
| 4054 $this->cache->close(); | |
| 4055 } | |
| 4056 $this->cache = null; | |
| 4057 $this->caching = false; | |
| 4058 } | |
| 4059 } | |
| 4060 | |
| 4061 /** | |
| 4062 * Getter for IMAP cache object | |
| 4063 */ | |
| 4064 protected function get_cache_engine() | |
| 4065 { | |
| 4066 if ($this->caching && !$this->cache) { | |
| 4067 $rcube = rcube::get_instance(); | |
| 4068 $ttl = $rcube->config->get('imap_cache_ttl', '10d'); | |
| 4069 $this->cache = $rcube->get_cache('IMAP', $this->caching, $ttl); | |
| 4070 } | |
| 4071 | |
| 4072 return $this->cache; | |
| 4073 } | |
| 4074 | |
| 4075 /** | |
| 4076 * Returns cached value | |
| 4077 * | |
| 4078 * @param string $key Cache key | |
| 4079 * | |
| 4080 * @return mixed | |
| 4081 */ | |
| 4082 public function get_cache($key) | |
| 4083 { | |
| 4084 if ($cache = $this->get_cache_engine()) { | |
| 4085 return $cache->get($key); | |
| 4086 } | |
| 4087 } | |
| 4088 | |
| 4089 /** | |
| 4090 * Update cache | |
| 4091 * | |
| 4092 * @param string $key Cache key | |
| 4093 * @param mixed $data Data | |
| 4094 */ | |
| 4095 public function update_cache($key, $data) | |
| 4096 { | |
| 4097 if ($cache = $this->get_cache_engine()) { | |
| 4098 $cache->set($key, $data); | |
| 4099 } | |
| 4100 } | |
| 4101 | |
| 4102 /** | |
| 4103 * Clears the cache. | |
| 4104 * | |
| 4105 * @param string $key Cache key name or pattern | |
| 4106 * @param boolean $prefix_mode Enable it to clear all keys starting | |
| 4107 * with prefix specified in $key | |
| 4108 */ | |
| 4109 public function clear_cache($key = null, $prefix_mode = false) | |
| 4110 { | |
| 4111 if ($cache = $this->get_cache_engine()) { | |
| 4112 $cache->remove($key, $prefix_mode); | |
| 4113 } | |
| 4114 } | |
| 4115 | |
| 4116 | |
| 4117 /* -------------------------------- | |
| 4118 * message caching methods | |
| 4119 * --------------------------------*/ | |
| 4120 | |
| 4121 /** | |
| 4122 * Enable or disable messages caching | |
| 4123 * | |
| 4124 * @param boolean $set Flag | |
| 4125 * @param int $mode Cache mode | |
| 4126 */ | |
| 4127 public function set_messages_caching($set, $mode = null) | |
| 4128 { | |
| 4129 if ($set) { | |
| 4130 $this->messages_caching = true; | |
| 4131 | |
| 4132 if ($mode && ($cache = $this->get_mcache_engine())) { | |
| 4133 $cache->set_mode($mode); | |
| 4134 } | |
| 4135 } | |
| 4136 else { | |
| 4137 if ($this->mcache) { | |
| 4138 $this->mcache->close(); | |
| 4139 } | |
| 4140 $this->mcache = null; | |
| 4141 $this->messages_caching = false; | |
| 4142 } | |
| 4143 } | |
| 4144 | |
| 4145 /** | |
| 4146 * Getter for messages cache object | |
| 4147 */ | |
| 4148 protected function get_mcache_engine() | |
| 4149 { | |
| 4150 if ($this->messages_caching && !$this->mcache) { | |
| 4151 $rcube = rcube::get_instance(); | |
| 4152 if (($dbh = $rcube->get_dbh()) && ($userid = $rcube->get_user_id())) { | |
| 4153 $ttl = $rcube->config->get('messages_cache_ttl', '10d'); | |
| 4154 $threshold = $rcube->config->get('messages_cache_threshold', 50); | |
| 4155 $this->mcache = new rcube_imap_cache( | |
| 4156 $dbh, $this, $userid, $this->options['skip_deleted'], $ttl, $threshold); | |
| 4157 } | |
| 4158 } | |
| 4159 | |
| 4160 return $this->mcache; | |
| 4161 } | |
| 4162 | |
| 4163 /** | |
| 4164 * Clears the messages cache. | |
| 4165 * | |
| 4166 * @param string $folder Folder name | |
| 4167 * @param array $uids Optional message UIDs to remove from cache | |
| 4168 */ | |
| 4169 protected function clear_message_cache($folder = null, $uids = null) | |
| 4170 { | |
| 4171 if ($mcache = $this->get_mcache_engine()) { | |
| 4172 $mcache->clear($folder, $uids); | |
| 4173 } | |
| 4174 } | |
| 4175 | |
| 4176 /** | |
| 4177 * Delete outdated cache entries | |
| 4178 */ | |
| 4179 function cache_gc() | |
| 4180 { | |
| 4181 rcube_imap_cache::gc(); | |
| 4182 } | |
| 4183 | |
| 4184 | |
| 4185 /* -------------------------------- | |
| 4186 * protected methods | |
| 4187 * --------------------------------*/ | |
| 4188 | |
| 4189 /** | |
| 4190 * Validate the given input and save to local properties | |
| 4191 * | |
| 4192 * @param string $sort_field Sort column | |
| 4193 * @param string $sort_order Sort order | |
| 4194 */ | |
| 4195 protected function set_sort_order($sort_field, $sort_order) | |
| 4196 { | |
| 4197 if ($sort_field != null) { | |
| 4198 $this->sort_field = asciiwords($sort_field); | |
| 4199 } | |
| 4200 if ($sort_order != null) { | |
| 4201 $this->sort_order = strtoupper($sort_order) == 'DESC' ? 'DESC' : 'ASC'; | |
| 4202 } | |
| 4203 } | |
| 4204 | |
| 4205 /** | |
| 4206 * Sort folders first by default folders and then in alphabethical order | |
| 4207 * | |
| 4208 * @param array $a_folders Folders list | |
| 4209 * @param bool $skip_default Skip default folders handling | |
| 4210 * | |
| 4211 * @return array Sorted list | |
| 4212 */ | |
| 4213 public function sort_folder_list($a_folders, $skip_default = false) | |
| 4214 { | |
| 4215 $specials = array_merge(array('INBOX'), array_values($this->get_special_folders())); | |
| 4216 $folders = array(); | |
| 4217 | |
| 4218 // convert names to UTF-8 | |
| 4219 foreach ($a_folders as $folder) { | |
| 4220 // for better performance skip encoding conversion | |
| 4221 // if the string does not look like UTF7-IMAP | |
| 4222 $folders[$folder] = strpos($folder, '&') === false ? $folder : rcube_charset::convert($folder, 'UTF7-IMAP'); | |
| 4223 } | |
| 4224 | |
| 4225 // sort folders | |
| 4226 // asort($folders, SORT_LOCALE_STRING) is not properly sorting case sensitive names | |
| 4227 uasort($folders, array($this, 'sort_folder_comparator')); | |
| 4228 | |
| 4229 $folders = array_keys($folders); | |
| 4230 | |
| 4231 if ($skip_default) { | |
| 4232 return $folders; | |
| 4233 } | |
| 4234 | |
| 4235 // force the type of folder name variable (#1485527) | |
| 4236 $folders = array_map('strval', $folders); | |
| 4237 $out = array(); | |
| 4238 | |
| 4239 // finally we must put special folders on top and rebuild the list | |
| 4240 // to move their subfolders where they belong... | |
| 4241 $specials = array_unique(array_intersect($specials, $folders)); | |
| 4242 $folders = array_merge($specials, array_diff($folders, $specials)); | |
| 4243 | |
| 4244 $this->sort_folder_specials(null, $folders, $specials, $out); | |
| 4245 | |
| 4246 return $out; | |
| 4247 } | |
| 4248 | |
| 4249 /** | |
| 4250 * Recursive function to put subfolders of special folders in place | |
| 4251 */ | |
| 4252 protected function sort_folder_specials($folder, &$list, &$specials, &$out) | |
| 4253 { | |
| 4254 foreach ($list as $key => $name) { | |
| 4255 if ($folder === null || strpos($name, $folder.$this->delimiter) === 0) { | |
| 4256 $out[] = $name; | |
| 4257 unset($list[$key]); | |
| 4258 | |
| 4259 if (!empty($specials) && ($found = array_search($name, $specials)) !== false) { | |
| 4260 unset($specials[$found]); | |
| 4261 $this->sort_folder_specials($name, $list, $specials, $out); | |
| 4262 } | |
| 4263 } | |
| 4264 } | |
| 4265 | |
| 4266 reset($list); | |
| 4267 } | |
| 4268 | |
| 4269 /** | |
| 4270 * Callback for uasort() that implements correct | |
| 4271 * locale-aware case-sensitive sorting | |
| 4272 */ | |
| 4273 protected function sort_folder_comparator($str1, $str2) | |
| 4274 { | |
| 4275 if ($this->sort_folder_collator === null) { | |
| 4276 $this->sort_folder_collator = false; | |
| 4277 | |
| 4278 // strcoll() does not work with UTF8 locale on Windows, | |
| 4279 // use Collator from the intl extension | |
| 4280 if (stripos(PHP_OS, 'win') === 0 && function_exists('collator_compare')) { | |
| 4281 $locale = $this->options['language'] ?: 'en_US'; | |
| 4282 $this->sort_folder_collator = collator_create($locale) ?: false; | |
| 4283 } | |
| 4284 } | |
| 4285 | |
| 4286 $path1 = explode($this->delimiter, $str1); | |
| 4287 $path2 = explode($this->delimiter, $str2); | |
| 4288 | |
| 4289 foreach ($path1 as $idx => $folder1) { | |
|
17
dd5ed6ef69c9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
16
diff
changeset
|
4290 $folder2 = $path2[$idx]??null; |
| 0 | 4291 |
| 4292 if ($folder1 === $folder2) { | |
| 4293 continue; | |
| 4294 } | |
| 4295 | |
| 4296 if ($this->sort_folder_collator) { | |
| 4297 return collator_compare($this->sort_folder_collator, $folder1, $folder2); | |
| 4298 } | |
| 4299 | |
|
17
dd5ed6ef69c9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
16
diff
changeset
|
4300 # 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
|
4301 return ($folder2 ? strcoll($folder1, $folder2) : 1); |
| 0 | 4302 } |
| 4303 } | |
| 4304 | |
| 4305 /** | |
| 4306 * Find UID of the specified message sequence ID | |
| 4307 * | |
| 4308 * @param int $id Message (sequence) ID | |
| 4309 * @param string $folder Folder name | |
| 4310 * | |
| 4311 * @return int Message UID | |
| 4312 */ | |
| 4313 public function id2uid($id, $folder = null) | |
| 4314 { | |
| 4315 if (!strlen($folder)) { | |
| 4316 $folder = $this->folder; | |
| 4317 } | |
| 4318 | |
| 4319 if (!$this->check_connection()) { | |
| 4320 return null; | |
| 4321 } | |
| 4322 | |
| 4323 return $this->conn->ID2UID($folder, $id); | |
| 4324 } | |
| 4325 | |
| 4326 /** | |
| 4327 * Subscribe/unsubscribe a list of folders and update local cache | |
| 4328 */ | |
| 4329 protected function change_subscription($folders, $mode) | |
| 4330 { | |
| 4331 $updated = 0; | |
| 4332 $folders = (array) $folders; | |
| 4333 | |
| 4334 if (!empty($folders)) { | |
| 4335 if (!$this->check_connection()) { | |
| 4336 return false; | |
| 4337 } | |
| 4338 | |
| 4339 foreach ($folders as $folder) { | |
| 4340 $updated += (int) $this->conn->{$mode}($folder); | |
| 4341 } | |
| 4342 } | |
| 4343 | |
| 4344 // clear cached folders list(s) | |
| 4345 if ($updated) { | |
| 4346 $this->clear_cache('mailboxes', true); | |
| 4347 } | |
| 4348 | |
| 4349 return $updated == count($folders); | |
| 4350 } | |
| 4351 | |
| 4352 /** | |
| 4353 * Increde/decrese messagecount for a specific folder | |
| 4354 */ | |
| 4355 protected function set_messagecount($folder, $mode, $increment) | |
| 4356 { | |
|
16
1866b439e6d3
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
15
diff
changeset
|
4357 if (!is_numeric($increment) || !$this->caching) { |
| 0 | 4358 return false; |
| 4359 } | |
| 4360 | |
| 4361 $mode = strtoupper($mode); | |
| 4362 $a_folder_cache = $this->get_cache('messagecount'); | |
| 4363 | |
| 4364 if (!is_array($a_folder_cache[$folder]) || !isset($a_folder_cache[$folder][$mode])) { | |
| 4365 return false; | |
| 4366 } | |
| 4367 | |
| 4368 // add incremental value to messagecount | |
| 4369 $a_folder_cache[$folder][$mode] += $increment; | |
| 4370 | |
| 4371 // there's something wrong, delete from cache | |
| 4372 if ($a_folder_cache[$folder][$mode] < 0) { | |
| 4373 unset($a_folder_cache[$folder][$mode]); | |
| 4374 } | |
| 4375 | |
| 4376 // write back to cache | |
| 4377 $this->update_cache('messagecount', $a_folder_cache); | |
| 4378 | |
| 4379 return true; | |
| 4380 } | |
| 4381 | |
| 4382 /** | |
| 4383 * Remove messagecount of a specific folder from cache | |
| 4384 */ | |
| 4385 protected function clear_messagecount($folder, $mode = array()) | |
| 4386 { | |
| 4387 $a_folder_cache = $this->get_cache('messagecount'); | |
| 4388 | |
| 27 | 4389 if (isset($a_folder_cache[$folder]) && is_array($a_folder_cache[$folder])) { |
| 0 | 4390 if (!empty($mode)) { |
| 4391 foreach ((array) $mode as $key) { | |
| 4392 unset($a_folder_cache[$folder][$key]); | |
| 4393 } | |
| 4394 } | |
| 4395 else { | |
| 4396 unset($a_folder_cache[$folder]); | |
| 4397 } | |
| 4398 $this->update_cache('messagecount', $a_folder_cache); | |
| 4399 } | |
| 4400 } | |
| 4401 | |
| 4402 /** | |
| 4403 * Converts date string/object into IMAP date/time format | |
| 4404 */ | |
| 4405 protected function date_format($date) | |
| 4406 { | |
| 4407 if (empty($date)) { | |
| 4408 return null; | |
| 4409 } | |
| 4410 | |
| 4411 if (!is_object($date) || !is_a($date, 'DateTime')) { | |
| 4412 try { | |
| 4413 $timestamp = rcube_utils::strtotime($date); | |
| 4414 $date = new DateTime("@".$timestamp); | |
| 4415 } | |
| 4416 catch (Exception $e) { | |
| 4417 return null; | |
| 4418 } | |
| 4419 } | |
| 4420 | |
| 4421 return $date->format('d-M-Y H:i:s O'); | |
| 4422 } | |
| 4423 | |
| 4424 /** | |
| 4425 * This is our own debug handler for the IMAP connection | |
| 4426 */ | |
| 4427 public function debug_handler(&$imap, $message) | |
| 4428 { | |
| 4429 rcube::write_log('imap', $message); | |
| 4430 } | |
| 4431 } |
