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