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