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