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