4 +-----------------------------------------------------------------------+
5 | program/include/rcube_imap_generic.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Provide alternative IMAP library that doesn't rely on the standard |
13 | C-Client based version. This allows to function regardless |
14 | of whether or not the PHP build it's running on has IMAP |
15 | functionality built-in. |
17 | Based on Iloha IMAP Library. See http://ilohamail.org/ for details |
19 +-----------------------------------------------------------------------+
20 | Author: Aleksander Machniak <alec@alec.pl> |
21 | Author: Ryo Chijiiwa <Ryo@IlohaMail.org> |
22 +-----------------------------------------------------------------------+
24 $Id: rcube_imap_generic.php 4643 2011-04-11 12:24:00Z alec $
30 * Struct representing an e-mail message header
33 * @author Aleksander Machniak <alec@alec.pl>
35 class rcube_mail_header
53 public $body_structure;
58 public $mdn_sent = false;
59 public $is_draft = false;
61 public $deleted = false;
62 public $recent = false;
63 public $answered = false;
64 public $forwarded = false;
66 public $flagged = false;
67 public $has_children = false;
69 public $unread_children = 0;
70 public $others = array();
73 // For backward compatibility with cached messages (#1486602)
74 class iilBasicHeader extends rcube_mail_header
79 * PHP based wrapper class to connect to an IMAP server
82 * @author Aleksander Machniak <alec@alec.pl>
84 class rcube_imap_generic
90 public $data = array();
91 public $flags = array(
93 'DELETED' => '\\Deleted',
94 'RECENT' => '\\Recent',
95 'ANSWERED' => '\\Answered',
97 'FLAGGED' => '\\Flagged',
98 'FORWARDED' => '$Forwarded',
99 'MDNSENT' => '$MDNSent',
106 private $logged = false;
107 private $capability = array();
108 private $capability_readed = false;
111 private $cmd_num = 0;
112 private $_debug = false;
113 private $_debug_handler = false;
117 const ERROR_BAD = -2;
118 const ERROR_BYE = -3;
119 const ERROR_UNKNOWN = -4;
120 const ERROR_COMMAND = -5;
121 const ERROR_READONLY = -6;
123 const COMMAND_NORESPONSE = 1;
124 const COMMAND_CAPABILITY = 2;
125 const COMMAND_LASTLINE = 4;
130 function __construct()
135 * Send simple (one line) command to the connection stream
137 * @param string $string Command string
138 * @param bool $endln True if CRLF need to be added at the end of command
140 * @param int Number of bytes sent, False on error
142 function putLine($string, $endln=true)
148 $this->debug('C: '. rtrim($string));
151 $res = fwrite($this->fp, $string . ($endln ? "\r\n" : ''));
153 if ($res === false) {
162 * Send command to the connection stream with Command Continuation
163 * Requests (RFC3501 7.5) and LITERAL+ (RFC2088) support
165 * @param string $string Command string
166 * @param bool $endln True if CRLF need to be added at the end of command
168 * @param int Number of bytes sent, False on error
170 function putLineC($string, $endln=true)
179 if ($parts = preg_split('/(\{[0-9]+\}\r\n)/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE)) {
180 for ($i=0, $cnt=count($parts); $i<$cnt; $i++) {
181 if (preg_match('/^\{[0-9]+\}\r\n$/', $parts[$i+1])) {
183 if ($this->prefs['literal+'])
184 $parts[$i+1] = preg_replace('/([0-9]+)/', '\\1+', $parts[$i+1]);
186 $bytes = $this->putLine($parts[$i].$parts[$i+1], false);
187 if ($bytes === false)
191 // don't wait if server supports LITERAL+ capability
192 if (!$this->prefs['literal+']) {
193 $line = $this->readLine(1000);
194 // handle error in command
201 $bytes = $this->putLine($parts[$i], false);
202 if ($bytes === false)
212 function readLine($size=1024)
222 return $line ? $line : NULL;
225 $buffer = fgets($this->fp, $size);
227 if ($buffer === false) {
228 $this->closeSocket();
232 $this->debug('S: '. rtrim($buffer));
235 } while (substr($buffer, -1) != "\n");
240 function multLine($line, $escape=false)
242 $line = rtrim($line);
243 if (preg_match('/\{[0-9]+\}$/', $line)) {
246 preg_match_all('/(.*)\{([0-9]+)\}$/', $line, $a);
248 while (strlen($out) < $bytes) {
249 $line = $this->readBytes($bytes);
255 $line = $a[1][0] . ($escape ? $this->escape($out) : $out);
261 function readBytes($bytes)
265 while ($len < $bytes && !$this->eof())
267 $d = fread($this->fp, $bytes-$len);
269 $this->debug('S: '. $d);
272 $data_len = strlen($data);
273 if ($len == $data_len) {
274 break; // nothing was read -> exit to avoid apache lockups
282 function readReply(&$untagged=null)
285 $line = trim($this->readLine(1024));
286 // store untagged response lines
289 } while ($line[0] == '*');
292 $untagged = join("\n", $untagged);
297 function parseResult($string, $err_prefix='')
299 if (preg_match('/^[a-z0-9*]+ (OK|NO|BAD|BYE)(.*)$/i', trim($string), $matches)) {
300 $res = strtoupper($matches[1]);
301 $str = trim($matches[2]);
304 $this->errornum = self::ERROR_OK;
305 } else if ($res == 'NO') {
306 $this->errornum = self::ERROR_NO;
307 } else if ($res == 'BAD') {
308 $this->errornum = self::ERROR_BAD;
309 } else if ($res == 'BYE') {
310 $this->closeSocket();
311 $this->errornum = self::ERROR_BYE;
316 // get response string and code (RFC5530)
317 if (preg_match("/^\[([a-z-]+)\]/i", $str, $m)) {
318 $this->resultcode = strtoupper($m[1]);
319 $str = trim(substr($str, strlen($m[1]) + 2));
322 $this->resultcode = null;
324 $this->result = $str;
326 if ($this->errornum != self::ERROR_OK) {
327 $this->error = $err_prefix ? $err_prefix.$str : $str;
331 return $this->errornum;
333 return self::ERROR_UNKNOWN;
336 private function eof()
338 if (!is_resource($this->fp)) {
342 // If a connection opened by fsockopen() wasn't closed
343 // by the server, feof() will hang.
344 $start = microtime(true);
346 if (feof($this->fp) ||
347 ($this->prefs['timeout'] && (microtime(true) - $start > $this->prefs['timeout']))
349 $this->closeSocket();
356 private function closeSocket()
362 function setError($code, $msg='')
364 $this->errornum = $code;
368 // check if $string starts with $match (or * BYE/BAD)
369 function startsWith($string, $match, $error=false, $nonempty=false)
371 $len = strlen($match);
378 if (strncmp($string, $match, $len) == 0) {
381 if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) {
382 if (strtoupper($m[1]) == 'BYE') {
383 $this->closeSocket();
387 if ($nonempty && !strlen($string)) {
393 private function hasCapability($name)
395 if (empty($this->capability) || $name == '') {
399 if (in_array($name, $this->capability)) {
402 else if (strpos($name, '=')) {
407 foreach ($this->capability as $cap) {
408 $entry = explode('=', $cap);
409 if ($entry[0] == $name) {
410 $result[] = $entry[1];
414 return !empty($result) ? $result : false;
418 * Capabilities checker
420 * @param string $name Capability name
422 * @return mixed Capability values array for key=value pairs, true/false for others
424 function getCapability($name)
426 $result = $this->hasCapability($name);
428 if (!empty($result)) {
431 else if ($this->capability_readed) {
435 // get capabilities (only once) because initial
436 // optional CAPABILITY response may differ
437 $result = $this->execute('CAPABILITY');
439 if ($result[0] == self::ERROR_OK) {
440 $this->parseCapability($result[1]);
443 $this->capability_readed = true;
445 return $this->hasCapability($name);
448 function clearCapability()
450 $this->capability = array();
451 $this->capability_readed = false;
455 * DIGEST-MD5/CRAM-MD5/PLAIN Authentication
457 * @param string $user
458 * @param string $pass
459 * @param string $type Authentication type (PLAIN/CRAM-MD5/DIGEST-MD5)
461 * @return resource Connection resourse on success, error code on error
463 function authenticate($user, $pass, $type='PLAIN')
465 if ($type == 'CRAM-MD5' || $type == 'DIGEST-MD5') {
466 if ($type == 'DIGEST-MD5' && !class_exists('Auth_SASL')) {
467 $this->setError(self::ERROR_BYE,
468 "The Auth_SASL package is required for DIGEST-MD5 authentication");
469 return self::ERROR_BAD;
472 $this->putLine($this->nextTag() . " AUTHENTICATE $type");
473 $line = trim($this->readReply());
475 if ($line[0] == '+') {
476 $challenge = substr($line, 2);
479 return $this->parseResult($line);
482 if ($type == 'CRAM-MD5') {
487 // initialize ipad, opad
488 for ($i=0; $i<64; $i++) {
493 // pad $pass so it's 64 bytes
494 $padLen = 64 - strlen($pass);
495 for ($i=0; $i<$padLen; $i++) {
500 $hash = md5($this->_xor($pass, $opad) . pack("H*",
501 md5($this->_xor($pass, $ipad) . base64_decode($challenge))));
502 $reply = base64_encode($user . ' ' . $hash);
505 $this->putLine($reply);
508 // RFC2831: DIGEST-MD5
509 // proxy authorization
510 if (!empty($this->prefs['auth_cid'])) {
511 $authc = $this->prefs['auth_cid'];
512 $pass = $this->prefs['auth_pw'];
517 $auth_sasl = Auth_SASL::factory('digestmd5');
518 $reply = base64_encode($auth_sasl->getResponse($authc, $pass,
519 base64_decode($challenge), $this->host, 'imap', $user));
522 $this->putLine($reply);
523 $line = trim($this->readReply());
525 if ($line[0] == '+') {
526 $challenge = substr($line, 2);
529 return $this->parseResult($line);
533 $challenge = base64_decode($challenge);
534 if (strpos($challenge, 'rspauth=') === false) {
535 $this->setError(self::ERROR_BAD,
536 "Unexpected response from server to DIGEST-MD5 response");
537 return self::ERROR_BAD;
543 $line = $this->readReply();
544 $result = $this->parseResult($line);
547 // proxy authorization
548 if (!empty($this->prefs['auth_cid'])) {
549 $authc = $this->prefs['auth_cid'];
550 $pass = $this->prefs['auth_pw'];
556 $reply = base64_encode($user . chr(0) . $authc . chr(0) . $pass);
558 // RFC 4959 (SASL-IR): save one round trip
559 if ($this->getCapability('SASL-IR')) {
560 list($result, $line) = $this->execute("AUTHENTICATE PLAIN", array($reply),
561 self::COMMAND_LASTLINE | self::COMMAND_CAPABILITY);
564 $this->putLine($this->nextTag() . " AUTHENTICATE PLAIN");
565 $line = trim($this->readReply());
567 if ($line[0] != '+') {
568 return $this->parseResult($line);
571 // send result, get reply and process it
572 $this->putLine($reply);
573 $line = $this->readReply();
574 $result = $this->parseResult($line);
578 if ($result == self::ERROR_OK) {
579 // optional CAPABILITY response
580 if ($line && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) {
581 $this->parseCapability($matches[1], true);
586 $this->setError($result, "AUTHENTICATE $type: $line");
593 * LOGIN Authentication
595 * @param string $user
596 * @param string $pass
598 * @return resource Connection resourse on success, error code on error
600 function login($user, $password)
602 list($code, $response) = $this->execute('LOGIN', array(
603 $this->escape($user), $this->escape($password)), self::COMMAND_CAPABILITY);
605 // re-set capabilities list if untagged CAPABILITY response provided
606 if (preg_match('/\* CAPABILITY (.+)/i', $response, $matches)) {
607 $this->parseCapability($matches[1], true);
610 if ($code == self::ERROR_OK) {
620 * @return string The delimiter
622 function getHierarchyDelimiter()
624 if ($this->prefs['delimiter']) {
625 return $this->prefs['delimiter'];
628 // try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8)
629 list($code, $response) = $this->execute('LIST',
630 array($this->escape(''), $this->escape('')));
632 if ($code == self::ERROR_OK) {
633 $args = $this->tokenizeResponse($response, 4);
634 $delimiter = $args[3];
636 if (strlen($delimiter) > 0) {
637 return ($this->prefs['delimiter'] = $delimiter);
645 * NAMESPACE handler (RFC 2342)
647 * @return array Namespace data hash (personal, other, shared)
649 function getNamespace()
651 if (array_key_exists('namespace', $this->prefs)) {
652 return $this->prefs['namespace'];
655 if (!$this->getCapability('NAMESPACE')) {
656 return self::ERROR_BAD;
659 list($code, $response) = $this->execute('NAMESPACE');
661 if ($code == self::ERROR_OK && preg_match('/^\* NAMESPACE /', $response)) {
662 $data = $this->tokenizeResponse(substr($response, 11));
665 if (!is_array($data)) {
669 $this->prefs['namespace'] = array(
670 'personal' => $data[0],
672 'shared' => $data[2],
675 return $this->prefs['namespace'];
678 function connect($host, $user, $password, $options=null)
681 if (is_array($options)) {
682 $this->prefs = $options;
685 if (!empty($this->prefs['auth_method'])) {
686 $auth_method = strtoupper($this->prefs['auth_method']);
688 $auth_method = 'CHECK';
693 // initialize connection
695 $this->errornum = self::ERROR_OK;
696 $this->selected = '';
699 $this->logged = false;
703 $this->setError(self::ERROR_BAD, "Empty host");
707 $this->setError(self::ERROR_NO, "Empty user");
710 if (empty($password)) {
711 $this->setError(self::ERROR_NO, "Empty password");
715 if (!$this->prefs['port']) {
716 $this->prefs['port'] = 143;
719 if ($this->prefs['ssl_mode'] && $this->prefs['ssl_mode'] != 'tls') {
720 $host = $this->prefs['ssl_mode'] . '://' . $host;
723 if ($this->prefs['timeout'] <= 0) {
724 $this->prefs['timeout'] = ini_get('default_socket_timeout');
728 $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']);
731 $this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s", $host, $this->prefs['port'], $errstr));
735 if ($this->prefs['timeout'] > 0)
736 stream_set_timeout($this->fp, $this->prefs['timeout']);
738 $line = trim(fgets($this->fp, 8192));
740 if ($this->_debug && $line) {
741 $this->debug('S: '. $line);
744 // Connected to wrong port or connection error?
745 if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) {
747 $error = sprintf("Wrong startup greeting (%s:%d): %s", $host, $this->prefs['port'], $line);
749 $error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']);
751 $this->setError(self::ERROR_BAD, $error);
752 $this->closeConnection();
756 // RFC3501 [7.1] optional CAPABILITY response
757 if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) {
758 $this->parseCapability($matches[1], true);
762 if ($this->prefs['ssl_mode'] == 'tls' && $this->getCapability('STARTTLS')) {
763 if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
764 $res = $this->execute('STARTTLS');
766 if ($res[0] != self::ERROR_OK) {
767 $this->closeConnection();
771 if (!stream_socket_enable_crypto($this->fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
772 $this->setError(self::ERROR_BAD, "Unable to negotiate TLS");
773 $this->closeConnection();
777 // Now we're secure, capabilities need to be reread
778 $this->clearCapability();
781 // Use best (for security) supported authentication method
782 foreach (array('DIGEST-MD5', 'CRAM-MD5', 'CRAM_MD5', 'PLAIN', 'LOGIN') as $auth_method) {
783 if (in_array($auth_method, $auth_methods)) {
790 if (!empty($this->prefs['ident']) && $this->getCapability('ID')) {
791 $this->id($this->prefs['ident']);
794 $auth_methods = array();
797 // check for supported auth methods
798 if ($auth_method == 'CHECK') {
799 if ($auth_caps = $this->getCapability('AUTH')) {
800 $auth_methods = $auth_caps;
802 // RFC 2595 (LOGINDISABLED) LOGIN disabled when connection is not secure
803 $login_disabled = $this->getCapability('LOGINDISABLED');
804 if (($key = array_search('LOGIN', $auth_methods)) !== false) {
805 if ($login_disabled) {
806 unset($auth_methods[$key]);
809 else if (!$login_disabled) {
810 $auth_methods[] = 'LOGIN';
813 // Use best (for security) supported authentication method
\r
814 foreach (array('DIGEST-MD5', 'CRAM-MD5', 'CRAM_MD5', 'PLAIN', 'LOGIN') as $auth_method) {
\r
815 if (in_array($auth_method, $auth_methods)) {
\r
821 // Prevent from sending credentials in plain text when connection is not secure
822 if ($auth_method == 'LOGIN' && $this->getCapability('LOGINDISABLED')) {
823 $this->setError(self::ERROR_BAD, "Login disabled by IMAP server");
824 $this->closeConnection();
827 // replace AUTH with CRAM-MD5 for backward compat.
828 if ($auth_method == 'AUTH') {
829 $auth_method = 'CRAM-MD5';
833 // pre-login capabilities can be not complete
834 $this->capability_readed = false;
837 switch ($auth_method) {
839 $auth_method = 'CRAM-MD5';
843 $result = $this->authenticate($user, $password, $auth_method);
846 $result = $this->login($user, $password);
849 $this->setError(self::ERROR_BAD, "Configuration error. Unknown auth method: $auth_method");
852 // Connected and authenticated
853 if (is_resource($result)) {
854 if ($this->prefs['force_caps']) {
855 $this->clearCapability();
857 $this->logged = true;
862 $this->closeConnection();
869 return ($this->fp && $this->logged) ? true : false;
872 function closeConnection()
874 if ($this->putLine($this->nextTag() . ' LOGOUT')) {
878 $this->closeSocket();
882 * Executes SELECT command (if mailbox is already not in selected state)
884 * @param string $mailbox Mailbox name
886 * @return boolean True on success, false on error
889 function select($mailbox)
891 if (!strlen($mailbox)) {
895 if ($this->selected == $mailbox) {
899 Temporary commented out because Courier returns \Noselect for INBOX
900 Requires more investigation
902 if (is_array($this->data['LIST']) && is_array($opts = $this->data['LIST'][$mailbox])) {
903 if (in_array('\\Noselect', $opts)) {
908 list($code, $response) = $this->execute('SELECT', array($this->escape($mailbox)));
910 if ($code == self::ERROR_OK) {
911 $response = explode("\r\n", $response);
912 foreach ($response as $line) {
913 if (preg_match('/^\* ([0-9]+) (EXISTS|RECENT)$/i', $line, $m)) {
914 $this->data[strtoupper($m[2])] = (int) $m[1];
916 else if (preg_match('/^\* OK \[(UIDNEXT|UIDVALIDITY|UNSEEN) ([0-9]+)\]/i', $line, $match)) {
917 $this->data[strtoupper($match[1])] = (int) $match[2];
919 else if (preg_match('/^\* OK \[PERMANENTFLAGS \(([^\)]+)\)\]/iU', $line, $match)) {
920 $this->data['PERMANENTFLAGS'] = explode(' ', $match[1]);
924 $this->data['READ-WRITE'] = $this->resultcode != 'READ-ONLY';
926 $this->selected = $mailbox;
934 * Executes STATUS command
936 * @param string $mailbox Mailbox name
937 * @param array $items Additional requested item names. By default
938 * MESSAGES and UNSEEN are requested. Other defined
939 * in RFC3501: UIDNEXT, UIDVALIDITY, RECENT
941 * @return array Status item-value hash
945 function status($mailbox, $items=array())
947 if (!strlen($mailbox)) {
951 if (!in_array('MESSAGES', $items)) {
952 $items[] = 'MESSAGES';
954 if (!in_array('UNSEEN', $items)) {
958 list($code, $response) = $this->execute('STATUS', array($this->escape($mailbox),
959 '(' . implode(' ', (array) $items) . ')'));
961 if ($code == self::ERROR_OK && preg_match('/\* STATUS /i', $response)) {
963 $response = substr($response, 9); // remove prefix "* STATUS "
965 list($mbox, $items) = $this->tokenizeResponse($response, 2);
967 // Fix for #1487859. Some buggy server returns not quoted
968 // folder name with spaces. Let's try to handle this situation
969 if (!is_array($items) && ($pos = strpos($response, '(')) !== false) {
970 $response = substr($response, $pos);
971 $items = $this->tokenizeResponse($response, 1);
972 if (!is_array($items)) {
977 for ($i=0, $len=count($items); $i<$len; $i += 2) {
978 $result[$items[$i]] = (int) $items[$i+1];
981 $this->data['STATUS:'.$mailbox] = $result;
990 * Executes EXPUNGE command
992 * @param string $mailbox Mailbox name
993 * @param string $messages Message UIDs to expunge
995 * @return boolean True on success, False on error
998 function expunge($mailbox, $messages=NULL)
1000 if (!$this->select($mailbox)) {
1004 if (!$this->data['READ-WRITE']) {
1005 $this->setError(self::ERROR_READONLY, "Mailbox is read-only", 'EXPUNGE');
1009 // Clear internal status cache
1010 unset($this->data['STATUS:'.$mailbox]);
1013 $result = $this->execute('UID EXPUNGE', array($messages), self::COMMAND_NORESPONSE);
1015 $result = $this->execute('EXPUNGE', null, self::COMMAND_NORESPONSE);
1017 if ($result == self::ERROR_OK) {
1018 $this->selected = ''; // state has changed, need to reselect
1026 * Executes CLOSE command
1028 * @return boolean True on success, False on error
1034 $result = $this->execute('CLOSE', NULL, self::COMMAND_NORESPONSE);
1036 if ($result == self::ERROR_OK) {
1037 $this->selected = '';
1045 * Executes SUBSCRIBE command
1047 * @param string $mailbox Mailbox name
1049 * @return boolean True on success, False on error
1052 function subscribe($mailbox)
1054 $result = $this->execute('SUBSCRIBE', array($this->escape($mailbox)),
1055 self::COMMAND_NORESPONSE);
1057 return ($result == self::ERROR_OK);
1061 * Executes UNSUBSCRIBE command
1063 * @param string $mailbox Mailbox name
1065 * @return boolean True on success, False on error
1068 function unsubscribe($mailbox)
1070 $result = $this->execute('UNSUBSCRIBE', array($this->escape($mailbox)),
1071 self::COMMAND_NORESPONSE);
1073 return ($result == self::ERROR_OK);
1077 * Executes DELETE command
1079 * @param string $mailbox Mailbox name
1081 * @return boolean True on success, False on error
1084 function deleteFolder($mailbox)
1086 $result = $this->execute('DELETE', array($this->escape($mailbox)),
1087 self::COMMAND_NORESPONSE);
1089 return ($result == self::ERROR_OK);
1093 * Removes all messages in a folder
1095 * @param string $mailbox Mailbox name
1097 * @return boolean True on success, False on error
1100 function clearFolder($mailbox)
1102 $num_in_trash = $this->countMessages($mailbox);
1103 if ($num_in_trash > 0) {
1104 $res = $this->delete($mailbox, '1:*');
1108 if ($this->selected == $mailbox)
1109 $res = $this->close();
1111 $res = $this->expunge($mailbox);
1118 * Returns count of all messages in a folder
1120 * @param string $mailbox Mailbox name
1122 * @return int Number of messages, False on error
1125 function countMessages($mailbox, $refresh = false)
1128 $this->selected = '';
1131 if ($this->selected == $mailbox) {
1132 return $this->data['EXISTS'];
1135 // Check internal cache
1136 $cache = $this->data['STATUS:'.$mailbox];
1137 if (!empty($cache) && isset($cache['MESSAGES'])) {
1138 return (int) $cache['MESSAGES'];
1141 // Try STATUS (should be faster than SELECT)
1142 $counts = $this->status($mailbox);
1143 if (is_array($counts)) {
1144 return (int) $counts['MESSAGES'];
1151 * Returns count of messages with \Recent flag in a folder
1153 * @param string $mailbox Mailbox name
1155 * @return int Number of messages, False on error
1158 function countRecent($mailbox)
1160 if (!strlen($mailbox)) {
1164 $this->select($mailbox);
1166 if ($this->selected == $mailbox) {
1167 return $this->data['RECENT'];
1174 * Returns count of messages without \Seen flag in a specified folder
1176 * @param string $mailbox Mailbox name
1178 * @return int Number of messages, False on error
1181 function countUnseen($mailbox)
1183 // Check internal cache
1184 $cache = $this->data['STATUS:'.$mailbox];
1185 if (!empty($cache) && isset($cache['UNSEEN'])) {
1186 return (int) $cache['UNSEEN'];
1189 // Try STATUS (should be faster than SELECT+SEARCH)
1190 $counts = $this->status($mailbox);
1191 if (is_array($counts)) {
1192 return (int) $counts['UNSEEN'];
1195 // Invoke SEARCH as a fallback
1196 $index = $this->search($mailbox, 'ALL UNSEEN', false, array('COUNT'));
1197 if (is_array($index)) {
1198 return (int) $index['COUNT'];
1205 * Executes ID command (RFC2971)
1207 * @param array $items Client identification information key/value hash
1209 * @return array Server identification information key/value hash
1213 function id($items=array())
1215 if (is_array($items) && !empty($items)) {
1216 foreach ($items as $key => $value) {
1217 $args[] = $this->escape($key);
1218 $args[] = $this->escape($value);
1222 list($code, $response) = $this->execute('ID', array(
1223 !empty($args) ? '(' . implode(' ', (array) $args) . ')' : $this->escape(null)
1227 if ($code == self::ERROR_OK && preg_match('/\* ID /i', $response)) {
1228 $response = substr($response, 5); // remove prefix "* ID "
1229 $items = $this->tokenizeResponse($response);
1232 for ($i=0, $len=count($items); $i<$len; $i += 2) {
1233 $result[$items[$i]] = $items[$i+1];
1242 function sort($mailbox, $field, $add='', $is_uid=FALSE, $encoding = 'US-ASCII')
1244 $field = strtoupper($field);
1245 if ($field == 'INTERNALDATE') {
1249 $fields = array('ARRIVAL' => 1,'CC' => 1,'DATE' => 1,
1250 'FROM' => 1, 'SIZE' => 1, 'SUBJECT' => 1, 'TO' => 1);
1252 if (!$fields[$field]) {
1256 if (!$this->select($mailbox)) {
1262 $add = $this->compressMessageSet($add);
1264 list($code, $response) = $this->execute($is_uid ? 'UID SORT' : 'SORT',
1265 array("($field)", $encoding, 'ALL' . (!empty($add) ? ' '.$add : '')));
1267 if ($code == self::ERROR_OK) {
1268 // remove prefix and unilateral untagged server responses
1269 $response = substr($response, stripos($response, '* SORT') + 7);
1270 if ($pos = strpos($response, '*')) {
1271 $response = substr($response, 0, $pos);
1273 return preg_split('/[\s\r\n]+/', $response, -1, PREG_SPLIT_NO_EMPTY);
1279 function fetchHeaderIndex($mailbox, $message_set, $index_field='', $skip_deleted=true, $uidfetch=false)
1281 if (is_array($message_set)) {
1282 if (!($message_set = $this->compressMessageSet($message_set)))
1285 list($from_idx, $to_idx) = explode(':', $message_set);
1286 if (empty($message_set) ||
1287 (isset($to_idx) && $to_idx != '*' && (int)$from_idx > (int)$to_idx)) {
1292 $index_field = empty($index_field) ? 'DATE' : strtoupper($index_field);
1294 $fields_a['DATE'] = 1;
1295 $fields_a['INTERNALDATE'] = 4;
1296 $fields_a['ARRIVAL'] = 4;
1297 $fields_a['FROM'] = 1;
1298 $fields_a['REPLY-TO'] = 1;
1299 $fields_a['SENDER'] = 1;
1300 $fields_a['TO'] = 1;
1301 $fields_a['CC'] = 1;
1302 $fields_a['SUBJECT'] = 1;
1303 $fields_a['UID'] = 2;
1304 $fields_a['SIZE'] = 2;
1305 $fields_a['SEEN'] = 3;
1306 $fields_a['RECENT'] = 3;
1307 $fields_a['DELETED'] = 3;
1309 if (!($mode = $fields_a[$index_field])) {
1313 /* Do "SELECT" command */
1314 if (!$this->select($mailbox)) {
1318 // build FETCH command string
1319 $key = $this->nextTag();
1320 $cmd = $uidfetch ? 'UID FETCH' : 'FETCH';
1321 $deleted = $skip_deleted ? ' FLAGS' : '';
1323 if ($mode == 1 && $index_field == 'DATE')
1324 $request = " $cmd $message_set (INTERNALDATE BODY.PEEK[HEADER.FIELDS (DATE)]$deleted)";
1325 else if ($mode == 1)
1326 $request = " $cmd $message_set (BODY.PEEK[HEADER.FIELDS ($index_field)]$deleted)";
1327 else if ($mode == 2) {
1328 if ($index_field == 'SIZE')
1329 $request = " $cmd $message_set (RFC822.SIZE$deleted)";
1331 $request = " $cmd $message_set ($index_field$deleted)";
1332 } else if ($mode == 3)
1333 $request = " $cmd $message_set (FLAGS)";
1335 $request = " $cmd $message_set (INTERNALDATE$deleted)";
1337 $request = $key . $request;
1339 if (!$this->putLine($request)) {
1340 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
1347 $line = rtrim($this->readLine(200));
1348 $line = $this->multLine($line);
1350 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
1354 if ($skip_deleted && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
1355 $flags = explode(' ', strtoupper($matches[1]));
1356 if (in_array('\\DELETED', $flags)) {
1357 $deleted[$id] = $id;
1362 if ($mode == 1 && $index_field == 'DATE') {
1363 if (preg_match('/BODY\[HEADER\.FIELDS \("*DATE"*\)\] (.*)/', $line, $matches)) {
1364 $value = preg_replace(array('/^"*[a-z]+:/i'), '', $matches[1]);
1365 $value = trim($value);
1366 $result[$id] = $this->strToTime($value);
1368 // non-existent/empty Date: header, use INTERNALDATE
1369 if (empty($result[$id])) {
1370 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches))
1371 $result[$id] = $this->strToTime($matches[1]);
1375 } else if ($mode == 1) {
1376 if (preg_match('/BODY\[HEADER\.FIELDS \("?(FROM|REPLY-TO|SENDER|TO|SUBJECT)"?\)\] (.*)/', $line, $matches)) {
1377 $value = preg_replace(array('/^"*[a-z]+:/i', '/\s+$/sm'), array('', ''), $matches[2]);
1378 $result[$id] = trim($value);
1382 } else if ($mode == 2) {
1383 if (preg_match('/(UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) {
1384 $result[$id] = trim($matches[2]);
1388 } else if ($mode == 3) {
1389 if (!$flags && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
1390 $flags = explode(' ', $matches[1]);
1392 $result[$id] = in_array('\\'.$index_field, $flags) ? 1 : 0;
1393 } else if ($mode == 4) {
1394 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
1395 $result[$id] = $this->strToTime($matches[1]);
1401 } while (!$this->startsWith($line, $key, true, true));
1406 static function compressMessageSet($messages, $force=false)
1408 // given a comma delimited list of independent mid's,
1409 // compresses by grouping sequences together
1411 if (!is_array($messages)) {
1412 // if less than 255 bytes long, let's not bother
1413 if (!$force && strlen($messages)<255) {
1417 // see if it's already been compressed
1418 if (strpos($messages, ':') !== false) {
1422 // separate, then sort
1423 $messages = explode(',', $messages);
1429 $start = $prev = $messages[0];
1431 foreach ($messages as $id) {
1432 $incr = $id - $prev;
1433 if ($incr > 1) { // found a gap
1434 if ($start == $prev) {
1435 $result[] = $prev; // push single id
1437 $result[] = $start . ':' . $prev; // push sequence as start_id:end_id
1439 $start = $id; // start of new sequence
1444 // handle the last sequence/id
1445 if ($start == $prev) {
1448 $result[] = $start.':'.$prev;
1451 // return as comma separated string
1452 return implode(',', $result);
1455 static function uncompressMessageSet($messages)
1458 $messages = explode(',', $messages);
1460 foreach ($messages as $part) {
1461 $items = explode(':', $part);
1462 $max = max($items[0], $items[1]);
1464 for ($x=$items[0]; $x<=$max; $x++) {
1473 * Returns message sequence identifier
1475 * @param string $mailbox Mailbox name
1476 * @param int $uid Message unique identifier (UID)
1478 * @return int Message sequence identifier
1481 function UID2ID($mailbox, $uid)
1484 $id_a = $this->search($mailbox, "UID $uid");
1485 if (is_array($id_a) && count($id_a) == 1) {
1486 return (int) $id_a[0];
1493 * Returns message unique identifier (UID)
1495 * @param string $mailbox Mailbox name
1496 * @param int $uid Message sequence identifier
1498 * @return int Message unique identifier
1501 function ID2UID($mailbox, $id)
1503 if (empty($id) || $id < 0) {
1507 if (!$this->select($mailbox)) {
1511 list($code, $response) = $this->execute('FETCH', array($id, '(UID)'));
1513 if ($code == self::ERROR_OK && preg_match("/^\* $id FETCH \(UID (.*)\)/i", $response, $m)) {
1520 function fetchUIDs($mailbox, $message_set=null)
1522 if (is_array($message_set))
1523 $message_set = join(',', $message_set);
1524 else if (empty($message_set))
1525 $message_set = '1:*';
1527 return $this->fetchHeaderIndex($mailbox, $message_set, 'UID', false);
1530 function fetchHeaders($mailbox, $message_set, $uidfetch=false, $bodystr=false, $add='')
1534 if (!$this->select($mailbox)) {
1538 $message_set = $this->compressMessageSet($message_set);
1541 $add = ' '.trim($add);
1543 /* FETCH uid, size, flags and headers */
1544 $key = $this->nextTag();
1545 $request = $key . ($uidfetch ? ' UID' : '') . " FETCH $message_set ";
1546 $request .= "(UID RFC822.SIZE FLAGS INTERNALDATE ";
1548 $request .= "BODYSTRUCTURE ";
1549 $request .= "BODY.PEEK[HEADER.FIELDS (DATE FROM TO SUBJECT CONTENT-TYPE ";
1550 $request .= "LIST-POST DISPOSITION-NOTIFICATION-TO".$add.")])";
1552 if (!$this->putLine($request)) {
1553 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
1557 $line = $this->readLine(4096);
1558 $line = $this->multLine($line);
1563 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
1564 $id = intval($m[1]);
1566 $result[$id] = new rcube_mail_header;
1567 $result[$id]->id = $id;
1568 $result[$id]->subject = '';
1569 $result[$id]->messageID = 'mid:' . $id;
1574 // Sample reply line:
1575 // * 321 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen)
1576 // INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODYSTRUCTURE (...)
1577 // BODY[HEADER.FIELDS ...
1579 if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY/sU', $line, $matches)) {
1582 while (list($name, $value) = $this->tokenizeResponse($str, 2)) {
1583 if ($name == 'UID') {
1584 $result[$id]->uid = intval($value);
1586 else if ($name == 'RFC822.SIZE') {
1587 $result[$id]->size = intval($value);
1589 else if ($name == 'INTERNALDATE') {
1590 $result[$id]->internaldate = $value;
1591 $result[$id]->date = $value;
1592 $result[$id]->timestamp = $this->StrToTime($value);
1594 else if ($name == 'FLAGS') {
1601 while (!preg_match('/ BODYSTRUCTURE (.*) BODY\[HEADER.FIELDS/sU', $line, $m)) {
1602 $line2 = $this->readLine(1024);
1603 $line .= $this->multLine($line2, true);
1605 $result[$id]->body_structure = $m[1];
1608 // the rest of the result
1609 if (preg_match('/ BODY\[HEADER.FIELDS \(.*?\)\]\s*(.*)$/s', $line, $m)) {
1610 $reslines = explode("\n", trim($m[1], '"'));
1611 // re-parse (see below)
1612 foreach ($reslines as $resln) {
1613 if (ord($resln[0])<=32) {
1614 $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($resln);
1616 $lines[++$ln] = trim($resln);
1622 // Start parsing headers. The problem is, some header "lines" take up multiple lines.
1623 // So, we'll read ahead, and if the one we're reading now is a valid header, we'll
1624 // process the previous line. Otherwise, we'll keep adding the strings until we come
1625 // to the next valid header line.
1628 $line = rtrim($this->readLine(300), "\r\n");
1630 // The preg_match below works around communigate imap, which outputs " UID <number>)".
1631 // Without this, the while statement continues on and gets the "FH0 OK completed" message.
1632 // If this loop gets the ending message, then the outer loop does not receive it from radline on line 1249.
1633 // This in causes the if statement on line 1278 to never be true, which causes the headers to end up missing
1634 // If the if statement was changed to pick up the fh0 from this loop, then it causes the outer loop to spin
1635 // An alternative might be:
1636 // if (!preg_match("/:/",$line) && preg_match("/\)$/",$line)) break;
1637 // however, unsure how well this would work with all imap clients.
1638 if (preg_match("/^\s*UID [0-9]+\)$/", $line)) {
1642 // handle FLAGS reply after headers (AOL, Zimbra?)
1643 if (preg_match('/\s+FLAGS \((.*)\)\)$/', $line, $matches)) {
1644 $flags_a = $this->tokenizeResponse($matches[1]);
1648 if (ord($line[0])<=32) {
1649 $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($line);
1651 $lines[++$ln] = trim($line);
1653 // patch from "Maksim Rubis" <siburny@hotmail.com>
1654 } while ($line[0] != ')' && !$this->startsWith($line, $key, true));
1656 if (strncmp($line, $key, strlen($key))) {
1657 // process header, fill rcube_mail_header obj.
1659 if (is_array($headers)) {
1661 while (list($k, $bar) = each($headers)) {
1666 // create array with header field:data
1667 while (list($lines_key, $str) = each($lines)) {
1668 list($field, $string) = explode(':', $str, 2);
1670 $field = strtolower($field);
1671 $string = preg_replace('/\n[\t\s]*/', ' ', trim($string));
1675 $result[$id]->date = $string;
1676 $result[$id]->timestamp = $this->strToTime($string);
1679 $result[$id]->from = $string;
1682 $result[$id]->to = preg_replace('/undisclosed-recipients:[;,]*/', '', $string);
1685 $result[$id]->subject = $string;
1688 $result[$id]->replyto = $string;
1691 $result[$id]->cc = $string;
1694 $result[$id]->bcc = $string;
1696 case 'content-transfer-encoding':
1697 $result[$id]->encoding = $string;
1699 case 'content-type':
1700 $ctype_parts = preg_split('/[; ]/', $string);
1701 $result[$id]->ctype = strtolower(array_shift($ctype_parts));
1702 if (preg_match('/charset\s*=\s*"?([a-z0-9\-\.\_]+)"?/i', $string, $regs)) {
1703 $result[$id]->charset = $regs[1];
1707 $result[$id]->in_reply_to = str_replace(array("\n", '<', '>'), '', $string);
1710 $result[$id]->references = $string;
1712 case 'return-receipt-to':
1713 case 'disposition-notification-to':
1714 case 'x-confirm-reading-to':
1715 $result[$id]->mdn_to = $string;
1718 $result[$id]->messageID = $string;
1721 if (preg_match('/^(\d+)/', $string, $matches)) {
1722 $result[$id]->priority = intval($matches[1]);
1726 if (strlen($field) > 2) {
1727 $result[$id]->others[$field] = $string;
1735 if (!empty($flags_a)) {
1736 foreach ($flags_a as $flag) {
1737 $flag = str_replace('\\', '', $flag);
1738 $result[$id]->flags[] = $flag;
1740 switch (strtoupper($flag)) {
1742 $result[$id]->seen = true;
1745 $result[$id]->deleted = true;
1748 $result[$id]->answered = true;
1751 $result[$id]->forwarded = true;
1754 $result[$id]->mdn_sent = true;
1757 $result[$id]->flagged = true;
1763 } while (!$this->startsWith($line, $key, true));
1768 function fetchHeader($mailbox, $id, $uidfetch=false, $bodystr=false, $add='')
1770 $a = $this->fetchHeaders($mailbox, $id, $uidfetch, $bodystr, $add);
1772 return array_shift($a);
1777 function sortHeaders($a, $field, $flag)
1779 if (empty($field)) {
1783 $field = strtolower($field);
1786 if ($field == 'date' || $field == 'internaldate') {
1787 $field = 'timestamp';
1793 $flag = strtoupper($flag);
1799 // First, we'll create an "index" array.
1800 // Then, we'll use sort() on that array,
1801 // and use that to sort the main array.
1803 // create "index" array
1806 while (list($key, $val) = each($a)) {
1807 if ($field == 'timestamp') {
1808 $data = $this->strToTime($val->date);
1810 $data = $val->timestamp;
1813 $data = $val->$field;
1814 if (is_string($data)) {
1815 $data = str_replace('"', '', $data);
1816 if ($field == 'subject') {
1817 $data = preg_replace('/^(Re: \s*|Fwd:\s*|Fw:\s*)+/i', '', $data);
1819 $data = strtoupper($data);
1822 $index[$key] = $data;
1826 if ($flag == 'ASC') {
1832 // form new array based on index
1835 while (list($key, $val) = each($index)) {
1836 $result[$key] = $a[$key];
1844 function modFlag($mailbox, $messages, $flag, $mod)
1846 if ($mod != '+' && $mod != '-') {
1850 if (!$this->select($mailbox)) {
1854 if (!$this->data['READ-WRITE']) {
1855 $this->setError(self::ERROR_READONLY, "Mailbox is read-only", 'STORE');
1859 // Clear internal status cache
1860 if ($flag == 'SEEN') {
1861 unset($this->data['STATUS:'.$mailbox]['UNSEEN']);
1864 $flag = $this->flags[strtoupper($flag)];
1865 $result = $this->execute('UID STORE', array(
1866 $this->compressMessageSet($messages), $mod . 'FLAGS.SILENT', "($flag)"),
1867 self::COMMAND_NORESPONSE);
1869 return ($result == self::ERROR_OK);
1872 function flag($mailbox, $messages, $flag) {
1873 return $this->modFlag($mailbox, $messages, $flag, '+');
1876 function unflag($mailbox, $messages, $flag) {
1877 return $this->modFlag($mailbox, $messages, $flag, '-');
1880 function delete($mailbox, $messages) {
1881 return $this->modFlag($mailbox, $messages, 'DELETED', '+');
1884 function copy($messages, $from, $to)
1886 if (!$this->select($from)) {
1890 // Clear internal status cache
1891 unset($this->data['STATUS:'.$to]);
1893 $result = $this->execute('UID COPY', array(
1894 $this->compressMessageSet($messages), $this->escape($to)),
1895 self::COMMAND_NORESPONSE);
1897 return ($result == self::ERROR_OK);
1900 function move($messages, $from, $to)
1902 if (!$this->select($from)) {
1906 if (!$this->data['READ-WRITE']) {
1907 $this->setError(self::ERROR_READONLY, "Mailbox is read-only", 'STORE');
1911 $r = $this->copy($messages, $from, $to);
1914 // Clear internal status cache
1915 unset($this->data['STATUS:'.$from]);
1917 return $this->delete($from, $messages);
1922 // Don't be tempted to change $str to pass by reference to speed this up - it will slow it down by about
1923 // 7 times instead :-) See comments on http://uk2.php.net/references and this article:
1924 // http://derickrethans.nl/files/phparch-php-variables-article.pdf
1925 private function parseThread($str, $begin, $end, $root, $parent, $depth, &$depthmap, &$haschildren)
1928 if ($str[$begin] != '(') {
1929 $stop = $begin + strspn($str, '1234567890', $begin, $end - $begin);
1930 $msg = substr($str, $begin, $stop - $begin);
1935 $depthmap[$msg] = $depth;
1936 $haschildren[$msg] = false;
1937 if (!is_null($parent))
1938 $haschildren[$parent] = true;
1939 if ($stop + 1 < $end)
1940 $node[$msg] = $this->parseThread($str, $stop + 1, $end, $root, $msg, $depth + 1, $depthmap, $haschildren);
1942 $node[$msg] = array();
1945 while ($off < $end) {
1950 $p = strpos($str, ')', $off);
1952 error_log("Mismatched brackets parsing IMAP THREAD response:");
1953 error_log(substr($str, ($begin < 10) ? 0 : ($begin - 10), $end - $begin + 20));
1954 error_log(str_repeat(' ', $off - (($begin < 10) ? 0 : ($begin - 10))));
1957 $p1 = strpos($str, '(', $off);
1958 if ($p1 !== false && $p1 < $p) {
1966 $node += $this->parseThread($str, $start + 1, $off - 1, $root, $parent, $depth, $depthmap, $haschildren);
1973 function thread($mailbox, $algorithm='REFERENCES', $criteria='', $encoding='US-ASCII')
1975 $old_sel = $this->selected;
1977 if (!$this->select($mailbox)) {
1981 // return empty result when folder is empty and we're just after SELECT
1982 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
1983 return array(array(), array(), array());
1986 $encoding = $encoding ? trim($encoding) : 'US-ASCII';
1987 $algorithm = $algorithm ? trim($algorithm) : 'REFERENCES';
1988 $criteria = $criteria ? 'ALL '.trim($criteria) : 'ALL';
1991 list($code, $response) = $this->execute('THREAD', array(
1992 $algorithm, $encoding, $criteria));
1994 if ($code == self::ERROR_OK) {
1996 $response = substr($response, stripos($response, '* THREAD') + 9);
1997 // ...unilateral untagged server responses
1998 if ($pos = strpos($response, '*')) {
1999 $response = substr($response, 0, $pos);
2002 $response = str_replace("\r\n", '', $response);
2003 $depthmap = array();
2004 $haschildren = array();
2006 $tree = $this->parseThread($response, 0, strlen($response),
2007 null, null, 0, $depthmap, $haschildren);
2009 return array($tree, $depthmap, $haschildren);
2016 * Executes SEARCH command
2018 * @param string $mailbox Mailbox name
2019 * @param string $criteria Searching criteria
2020 * @param bool $return_uid Enable UID in result instead of sequence ID
2021 * @param array $items Return items (MIN, MAX, COUNT, ALL)
2023 * @return array Message identifiers or item-value hash
2025 function search($mailbox, $criteria, $return_uid=false, $items=array())
2027 $old_sel = $this->selected;
2029 if (!$this->select($mailbox)) {
2033 // return empty result when folder is empty and we're just after SELECT
2034 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
2036 return array_combine($items, array_fill(0, count($items), 0));
2041 $esearch = empty($items) ? false : $this->getCapability('ESEARCH');
2042 $criteria = trim($criteria);
2046 if (!empty($items) && $esearch) {
2047 $params .= 'RETURN (' . implode(' ', $items) . ')';
2049 if (!empty($criteria)) {
2050 $params .= ($params ? ' ' : '') . $criteria;
2056 list($code, $response) = $this->execute($return_uid ? 'UID SEARCH' : 'SEARCH',
2059 if ($code == self::ERROR_OK) {
2061 $response = substr($response, stripos($response,
2062 $esearch ? '* ESEARCH' : '* SEARCH') + ($esearch ? 10 : 9));
2063 // ...and unilateral untagged server responses
2064 if ($pos = strpos($response, '*')) {
2065 $response = rtrim(substr($response, 0, $pos));
2069 // Skip prefix: ... (TAG "A285") UID ...
2070 $this->tokenizeResponse($response, $return_uid ? 2 : 1);
2073 for ($i=0; $i<count($items); $i++) {
2074 // If the SEARCH results in no matches, the server MUST NOT
2075 // include the item result option in the ESEARCH response
2076 if ($ret = $this->tokenizeResponse($response, 2)) {
2077 list ($name, $value) = $ret;
2078 $result[$name] = $value;
2085 $response = preg_split('/[\s\r\n]+/', $response, -1, PREG_SPLIT_NO_EMPTY);
2087 if (!empty($items)) {
2089 if (in_array('COUNT', $items)) {
2090 $result['COUNT'] = count($response);
2092 if (in_array('MIN', $items)) {
2093 $result['MIN'] = !empty($response) ? min($response) : 0;
2095 if (in_array('MAX', $items)) {
2096 $result['MAX'] = !empty($response) ? max($response) : 0;
2098 if (in_array('ALL', $items)) {
2099 $result['ALL'] = $this->compressMessageSet($response, true);
2114 * Returns list of mailboxes
2116 * @param string $ref Reference name
2117 * @param string $mailbox Mailbox name
2118 * @param array $status_opts (see self::_listMailboxes)
2119 * @param array $select_opts (see self::_listMailboxes)
2121 * @return array List of mailboxes or hash of options if $status_opts argument
2125 function listMailboxes($ref, $mailbox, $status_opts=array(), $select_opts=array())
2127 return $this->_listMailboxes($ref, $mailbox, false, $status_opts, $select_opts);
2131 * Returns list of subscribed mailboxes
2133 * @param string $ref Reference name
2134 * @param string $mailbox Mailbox name
2135 * @param array $status_opts (see self::_listMailboxes)
2137 * @return array List of mailboxes or hash of options if $status_opts argument
2141 function listSubscribed($ref, $mailbox, $status_opts=array())
2143 return $this->_listMailboxes($ref, $mailbox, true, $status_opts, NULL);
2147 * IMAP LIST/LSUB command
2149 * @param string $ref Reference name
2150 * @param string $mailbox Mailbox name
2151 * @param bool $subscribed Enables returning subscribed mailboxes only
2152 * @param array $status_opts List of STATUS options (RFC5819: LIST-STATUS)
2153 * Possible: MESSAGES, RECENT, UIDNEXT, UIDVALIDITY, UNSEEN
2154 * @param array $select_opts List of selection options (RFC5258: LIST-EXTENDED)
2155 * Possible: SUBSCRIBED, RECURSIVEMATCH, REMOTE
2157 * @return array List of mailboxes or hash of options if $status_ops argument
2161 private function _listMailboxes($ref, $mailbox, $subscribed=false,
2162 $status_opts=array(), $select_opts=array())
2164 if (!strlen($mailbox)) {
2170 if (!empty($select_opts) && $this->getCapability('LIST-EXTENDED')) {
2171 $select_opts = (array) $select_opts;
2173 $args[] = '(' . implode(' ', $select_opts) . ')';
2176 $args[] = $this->escape($ref);
2177 $args[] = $this->escape($mailbox);
2179 if (!empty($status_opts) && $this->getCapability('LIST-STATUS')) {
2180 $status_opts = (array) $status_opts;
2183 $args[] = 'RETURN (STATUS (' . implode(' ', $status_opts) . '))';
2186 list($code, $response) = $this->execute($subscribed ? 'LSUB' : 'LIST', $args);
2188 if ($code == self::ERROR_OK) {
2190 while ($this->tokenizeResponse($response, 1) == '*') {
2191 $cmd = strtoupper($this->tokenizeResponse($response, 1));
2192 // * LIST (<options>) <delimiter> <mailbox>
2193 if (!$lstatus || $cmd == 'LIST' || $cmd == 'LSUB') {
2194 list($opts, $delim, $mailbox) = $this->tokenizeResponse($response, 3);
2196 // Add to result array
2198 $folders[] = $mailbox;
2201 $folders[$mailbox] = array();
2204 // Add to options array
2205 if (!empty($opts)) {
2206 if (empty($this->data['LIST'][$mailbox]))
2207 $this->data['LIST'][$mailbox] = $opts;
2209 $this->data['LIST'][$mailbox] = array_unique(array_merge(
2210 $this->data['LIST'][$mailbox], $opts));
2213 // * STATUS <mailbox> (<result>)
2214 else if ($cmd == 'STATUS') {
2215 list($mailbox, $status) = $this->tokenizeResponse($response, 2);
2217 for ($i=0, $len=count($status); $i<$len; $i += 2) {
2218 list($name, $value) = $this->tokenizeResponse($status, 2);
2219 $folders[$mailbox][$name] = $value;
2230 function fetchMIMEHeaders($mailbox, $id, $parts, $mime=true)
2232 if (!$this->select($mailbox)) {
2237 $parts = (array) $parts;
2238 $key = $this->nextTag();
2241 $type = $mime ? 'MIME' : 'HEADER';
2244 foreach($parts as $part) {
2245 $peeks[] = "BODY.PEEK[$part.$type]";
2248 $request = "$key FETCH $id (" . implode(' ', $peeks) . ')';
2251 if (!$this->putLine($request)) {
2252 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2257 $line = $this->readLine(1024);
2258 $line = $this->multLine($line);
2260 if (preg_match('/BODY\[([0-9\.]+)\.'.$type.'\]/', $line, $matches)) {
2262 $result[$idx] = preg_replace('/^(\* '.$id.' FETCH \()?\s*BODY\['.$idx.'\.'.$type.'\]\s+/', '', $line);
2263 $result[$idx] = trim($result[$idx], '"');
2264 $result[$idx] = rtrim($result[$idx], "\t\r\n\0\x0B");
2266 } while (!$this->startsWith($line, $key, true));
2271 function fetchPartHeader($mailbox, $id, $is_uid=false, $part=NULL)
2273 $part = empty($part) ? 'HEADER' : $part.'.MIME';
2275 return $this->handlePartBody($mailbox, $id, $is_uid, $part);
2278 function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL)
2280 if (!$this->select($mailbox)) {
2284 switch ($encoding) {
2288 case 'quoted-printable':
2302 $reply_key = '* ' . $id;
2303 $key = $this->nextTag();
2304 $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
2307 if (!$this->putLine($request)) {
2308 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2312 // receive reply line
2314 $line = rtrim($this->readLine(1024));
2315 $a = explode(' ', $line);
2316 } while (!($end = $this->startsWith($line, $key, true)) && $a[2] != 'FETCH');
2318 $len = strlen($line);
2321 // handle empty "* X FETCH ()" response
2322 if ($line[$len-1] == ')' && $line[$len-2] != '(') {
2323 // one line response, get everything between first and last quotes
2324 if (substr($line, -4, 3) == 'NIL') {
2328 $from = strpos($line, '"') + 1;
2329 $to = strrpos($line, '"');
2331 $result = substr($line, $from, $len);
2335 $result = base64_decode($result);
2337 else if ($mode == 2) {
2338 $result = quoted_printable_decode($result);
2340 else if ($mode == 3) {
2341 $result = convert_uudecode($result);
2344 } else if ($line[$len-1] == '}') {
2345 // multi-line request, find sizes of content and receive that many bytes
2346 $from = strpos($line, '{') + 1;
2347 $to = strrpos($line, '}');
2349 $sizeStr = substr($line, $from, $len);
2350 $bytes = (int)$sizeStr;
2353 while ($bytes > 0) {
2354 $line = $this->readLine(4096);
2356 if ($line === NULL) {
2360 $len = strlen($line);
2362 if ($len > $bytes) {
2363 $line = substr($line, 0, $bytes);
2364 $len = strlen($line);
2370 $line = rtrim($line, "\t\r\n\0\x0B");
2371 // create chunks with proper length for base64 decoding
2372 $line = $prev.$line;
2373 $length = strlen($line);
2375 $length = floor($length / 4) * 4;
2376 $prev = substr($line, $length);
2377 $line = substr($line, 0, $length);
2381 $line = base64_decode($line);
2383 } else if ($mode == 2) {
2384 $line = rtrim($line, "\t\r\0\x0B");
2385 $line = quoted_printable_decode($line);
2386 // Remove NULL characters (#1486189)
2387 $line = str_replace("\x00", '', $line);
2389 } else if ($mode == 3) {
2390 $line = rtrim($line, "\t\r\n\0\x0B");
2391 if ($line == 'end' || preg_match('/^begin\s+[0-7]+\s+.+$/', $line))
2393 $line = convert_uudecode($line);
2396 $line = rtrim($line, "\t\r\n\0\x0B") . "\n";
2400 fwrite($file, $line);
2408 // read in anything up until last line
2411 $line = $this->readLine(1024);
2412 } while (!$this->startsWith($line, $key, true));
2414 if ($result !== false) {
2416 fwrite($file, $result);
2417 } else if ($print) {
2427 function createFolder($mailbox)
2429 $result = $this->execute('CREATE', array($this->escape($mailbox)),
2430 self::COMMAND_NORESPONSE);
2432 return ($result == self::ERROR_OK);
2435 function renameFolder($from, $to)
2437 $result = $this->execute('RENAME', array($this->escape($from), $this->escape($to)),
2438 self::COMMAND_NORESPONSE);
2440 return ($result == self::ERROR_OK);
2443 function append($mailbox, &$message)
2449 $message = str_replace("\r", '', $message);
2450 $message = str_replace("\n", "\r\n", $message);
2452 $len = strlen($message);
2457 $key = $this->nextTag();
2458 $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox),
2459 $len, ($this->prefs['literal+'] ? '+' : ''));
2461 if ($this->putLine($request)) {
2462 // Don't wait when LITERAL+ is supported
2463 if (!$this->prefs['literal+']) {
2464 $line = $this->readReply();
2466 if ($line[0] != '+') {
2467 $this->parseResult($line, 'APPEND: ');
2472 if (!$this->putLine($message)) {
2477 $line = $this->readLine();
2478 } while (!$this->startsWith($line, $key, true, true));
2480 // Clear internal status cache
2481 unset($this->data['STATUS:'.$mailbox]);
2483 return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK);
2486 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2492 function appendFromFile($mailbox, $path, $headers=null)
2498 // open message file
2500 if (file_exists(realpath($path))) {
2501 $in_fp = fopen($path, 'r');
2504 $this->setError(self::ERROR_UNKNOWN, "Couldn't open $path for reading");
2508 $body_separator = "\r\n\r\n";
2509 $len = filesize($path);
2516 $headers = preg_replace('/[\r\n]+$/', '', $headers);
2517 $len += strlen($headers) + strlen($body_separator);
2520 // send APPEND command
2521 $key = $this->nextTag();
2522 $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox),
2523 $len, ($this->prefs['literal+'] ? '+' : ''));
2525 if ($this->putLine($request)) {
2526 // Don't wait when LITERAL+ is supported
2527 if (!$this->prefs['literal+']) {
2528 $line = $this->readReply();
2530 if ($line[0] != '+') {
2531 $this->parseResult($line, 'APPEND: ');
2536 // send headers with body separator
2538 $this->putLine($headers . $body_separator, false);
2542 while (!feof($in_fp) && $this->fp) {
2543 $buffer = fgets($in_fp, 4096);
2544 $this->putLine($buffer, false);
2548 if (!$this->putLine('')) { // \r\n
2554 $line = $this->readLine();
2555 } while (!$this->startsWith($line, $key, true, true));
2557 // Clear internal status cache
2558 unset($this->data['STATUS:'.$mailbox]);
2560 return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK);
2563 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2569 function fetchStructureString($mailbox, $id, $is_uid=false)
2571 if (!$this->select($mailbox)) {
2575 $key = $this->nextTag();
2577 $command = $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)";
2579 if ($this->putLine($command)) {
2581 $line = $this->readLine(5000);
2582 $line = $this->multLine($line, true);
2583 if (!preg_match("/^$key /", $line))
2585 } while (!$this->startsWith($line, $key, true, true));
2587 $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -1));
2590 $this->setError(self::ERROR_COMMAND, "Unable to send command: $command");
2599 * GETQUOTAROOT "INBOX"
2600 * QUOTAROOT INBOX user/rchijiiwa1
2601 * QUOTA user/rchijiiwa1 (STORAGE 654 9765)
2605 $quota_lines = array();
2606 $key = $this->nextTag();
2607 $command = $key . ' GETQUOTAROOT INBOX';
2609 // get line(s) containing quota info
2610 if ($this->putLine($command)) {
2612 $line = rtrim($this->readLine(5000));
2613 if (preg_match('/^\* QUOTA /', $line)) {
2614 $quota_lines[] = $line;
2616 } while (!$this->startsWith($line, $key, true, true));
2619 $this->setError(self::ERROR_COMMAND, "Unable to send command: $command");
2622 // return false if not found, parse if found
2623 $min_free = PHP_INT_MAX;
2624 foreach ($quota_lines as $key => $quota_line) {
2625 $quota_line = str_replace(array('(', ')'), '', $quota_line);
2626 $parts = explode(' ', $quota_line);
2627 $storage_part = array_search('STORAGE', $parts);
2629 if (!$storage_part) {
2633 $used = intval($parts[$storage_part+1]);
2634 $total = intval($parts[$storage_part+2]);
2635 $free = $total - $used;
2637 // return lowest available space from all quotas
2638 if ($free < $min_free) {
2640 $result['used'] = $used;
2641 $result['total'] = $total;
2642 $result['percent'] = min(100, round(($used/max(1,$total))*100));
2643 $result['free'] = 100 - $result['percent'];
2651 * Send the SETACL command (RFC4314)
2653 * @param string $mailbox Mailbox name
2654 * @param string $user User name
2655 * @param mixed $acl ACL string or array
2657 * @return boolean True on success, False on failure
2662 function setACL($mailbox, $user, $acl)
2664 if (is_array($acl)) {
2665 $acl = implode('', $acl);
2668 $result = $this->execute('SETACL', array(
2669 $this->escape($mailbox), $this->escape($user), strtolower($acl)),
2670 self::COMMAND_NORESPONSE);
2672 return ($result == self::ERROR_OK);
2676 * Send the DELETEACL command (RFC4314)
2678 * @param string $mailbox Mailbox name
2679 * @param string $user User name
2681 * @return boolean True on success, False on failure
2686 function deleteACL($mailbox, $user)
2688 $result = $this->execute('DELETEACL', array(
2689 $this->escape($mailbox), $this->escape($user)),
2690 self::COMMAND_NORESPONSE);
2692 return ($result == self::ERROR_OK);
2696 * Send the GETACL command (RFC4314)
2698 * @param string $mailbox Mailbox name
2700 * @return array User-rights array on success, NULL on error
2704 function getACL($mailbox)
2706 list($code, $response) = $this->execute('GETACL', array($this->escape($mailbox)));
2708 if ($code == self::ERROR_OK && preg_match('/^\* ACL /i', $response)) {
2709 // Parse server response (remove "* ACL ")
2710 $response = substr($response, 6);
2711 $ret = $this->tokenizeResponse($response);
2712 $mbox = array_shift($ret);
2713 $size = count($ret);
2715 // Create user-rights hash array
2716 // @TODO: consider implementing fixACL() method according to RFC4314.2.1.1
2717 // so we could return only standard rights defined in RFC4314,
2718 // excluding 'c' and 'd' defined in RFC2086.
2719 if ($size % 2 == 0) {
2720 for ($i=0; $i<$size; $i++) {
2721 $ret[$ret[$i]] = str_split($ret[++$i]);
2728 $this->setError(self::ERROR_COMMAND, "Incomplete ACL response");
2736 * Send the LISTRIGHTS command (RFC4314)
2738 * @param string $mailbox Mailbox name
2739 * @param string $user User name
2741 * @return array List of user rights
2745 function listRights($mailbox, $user)
2747 list($code, $response) = $this->execute('LISTRIGHTS', array(
2748 $this->escape($mailbox), $this->escape($user)));
2750 if ($code == self::ERROR_OK && preg_match('/^\* LISTRIGHTS /i', $response)) {
2751 // Parse server response (remove "* LISTRIGHTS ")
2752 $response = substr($response, 13);
2754 $ret_mbox = $this->tokenizeResponse($response, 1);
2755 $ret_user = $this->tokenizeResponse($response, 1);
2756 $granted = $this->tokenizeResponse($response, 1);
2757 $optional = trim($response);
2760 'granted' => str_split($granted),
2761 'optional' => explode(' ', $optional),
2769 * Send the MYRIGHTS command (RFC4314)
2771 * @param string $mailbox Mailbox name
2773 * @return array MYRIGHTS response on success, NULL on error
2777 function myRights($mailbox)
2779 list($code, $response) = $this->execute('MYRIGHTS', array($this->escape($mailbox)));
2781 if ($code == self::ERROR_OK && preg_match('/^\* MYRIGHTS /i', $response)) {
2782 // Parse server response (remove "* MYRIGHTS ")
2783 $response = substr($response, 11);
2785 $ret_mbox = $this->tokenizeResponse($response, 1);
2786 $rights = $this->tokenizeResponse($response, 1);
2788 return str_split($rights);
2795 * Send the SETMETADATA command (RFC5464)
2797 * @param string $mailbox Mailbox name
2798 * @param array $entries Entry-value array (use NULL value as NIL)
2800 * @return boolean True on success, False on failure
2804 function setMetadata($mailbox, $entries)
2806 if (!is_array($entries) || empty($entries)) {
2807 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
2811 foreach ($entries as $name => $value) {
2812 if ($value === null) {
2816 $value = sprintf("{%d}\r\n%s", strlen($value), $value);
2818 $entries[$name] = $this->escape($name) . ' ' . $value;
2821 $entries = implode(' ', $entries);
2822 $result = $this->execute('SETMETADATA', array(
2823 $this->escape($mailbox), '(' . $entries . ')'),
2824 self::COMMAND_NORESPONSE);
2826 return ($result == self::ERROR_OK);
2830 * Send the SETMETADATA command with NIL values (RFC5464)
2832 * @param string $mailbox Mailbox name
2833 * @param array $entries Entry names array
2835 * @return boolean True on success, False on failure
2840 function deleteMetadata($mailbox, $entries)
2842 if (!is_array($entries) && !empty($entries)) {
2843 $entries = explode(' ', $entries);
2846 if (empty($entries)) {
2847 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
2851 foreach ($entries as $entry) {
2852 $data[$entry] = NULL;
2855 return $this->setMetadata($mailbox, $data);
2859 * Send the GETMETADATA command (RFC5464)
2861 * @param string $mailbox Mailbox name
2862 * @param array $entries Entries
2863 * @param array $options Command options (with MAXSIZE and DEPTH keys)
2865 * @return array GETMETADATA result on success, NULL on error
2870 function getMetadata($mailbox, $entries, $options=array())
2872 if (!is_array($entries)) {
2873 $entries = array($entries);
2876 // create entries string
2877 foreach ($entries as $idx => $name) {
2878 $entries[$idx] = $this->escape($name);
2882 $entlist = '(' . implode(' ', $entries) . ')';
2884 // create options string
2885 if (is_array($options)) {
2886 $options = array_change_key_case($options, CASE_UPPER);
2889 if (!empty($options['MAXSIZE'])) {
2890 $opts[] = 'MAXSIZE '.intval($options['MAXSIZE']);
2892 if (!empty($options['DEPTH'])) {
2893 $opts[] = 'DEPTH '.intval($options['DEPTH']);
2897 $optlist = '(' . implode(' ', $opts) . ')';
2901 $optlist .= ($optlist ? ' ' : '') . $entlist;
2903 list($code, $response) = $this->execute('GETMETADATA', array(
2904 $this->escape($mailbox), $optlist));
2906 if ($code == self::ERROR_OK) {
2908 $data = $this->tokenizeResponse($response);
2910 // The METADATA response can contain multiple entries in a single
2911 // response or multiple responses for each entry or group of entries
2912 if (!empty($data) && ($size = count($data))) {
2913 for ($i=0; $i<$size; $i++) {
2914 if (isset($mbox) && is_array($data[$i])) {
2915 $size_sub = count($data[$i]);
2916 for ($x=0; $x<$size_sub; $x++) {
2917 $result[$mbox][$data[$i][$x]] = $data[$i][++$x];
2921 else if ($data[$i] == '*') {
2922 if ($data[$i+1] == 'METADATA') {
2923 $mbox = $data[$i+2];
2924 unset($data[$i]); // "*"
2925 unset($data[++$i]); // "METADATA"
2926 unset($data[++$i]); // Mailbox
2928 // get rid of other untagged responses
2934 else if (isset($mbox)) {
2935 $result[$mbox][$data[$i]] = $data[++$i];
2952 * Send the SETANNOTATION command (draft-daboo-imap-annotatemore)
2954 * @param string $mailbox Mailbox name
2955 * @param array $data Data array where each item is an array with
2956 * three elements: entry name, attribute name, value
2958 * @return boolean True on success, False on failure
2962 function setAnnotation($mailbox, $data)
2964 if (!is_array($data) || empty($data)) {
2965 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
2969 foreach ($data as $entry) {
2974 if ($value === null) {
2978 $value = sprintf("{%d}\r\n%s", strlen($value), $value);
2981 // ANNOTATEMORE drafts before version 08 require quoted parameters
2982 $entries[] = sprintf('%s (%s %s)',
2983 $this->escape($name, true), $this->escape($attr, true), $value);
2986 $entries = implode(' ', $entries);
2987 $result = $this->execute('SETANNOTATION', array(
2988 $this->escape($mailbox), $entries), self::COMMAND_NORESPONSE);
2990 return ($result == self::ERROR_OK);
2994 * Send the SETANNOTATION command with NIL values (draft-daboo-imap-annotatemore)
2996 * @param string $mailbox Mailbox name
2997 * @param array $data Data array where each item is an array with
2998 * two elements: entry name and attribute name
3000 * @return boolean True on success, False on failure
3005 function deleteAnnotation($mailbox, $data)
3007 if (!is_array($data) || empty($data)) {
3008 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
3012 return $this->setAnnotation($mailbox, $data);
3016 * Send the GETANNOTATION command (draft-daboo-imap-annotatemore)
3018 * @param string $mailbox Mailbox name
3019 * @param array $entries Entries names
3020 * @param array $attribs Attribs names
3022 * @return array Annotations result on success, NULL on error
3027 function getAnnotation($mailbox, $entries, $attribs)
3029 if (!is_array($entries)) {
3030 $entries = array($entries);
3032 // create entries string
3033 // ANNOTATEMORE drafts before version 08 require quoted parameters
3034 foreach ($entries as $idx => $name) {
3035 $entries[$idx] = $this->escape($name, true);
3037 $entries = '(' . implode(' ', $entries) . ')';
3039 if (!is_array($attribs)) {
3040 $attribs = array($attribs);
3042 // create entries string
3043 foreach ($attribs as $idx => $name) {
3044 $attribs[$idx] = $this->escape($name, true);
3046 $attribs = '(' . implode(' ', $attribs) . ')';
3048 list($code, $response) = $this->execute('GETANNOTATION', array(
3049 $this->escape($mailbox), $entries, $attribs));
3051 if ($code == self::ERROR_OK) {
3053 $data = $this->tokenizeResponse($response);
3055 // Here we returns only data compatible with METADATA result format
3056 if (!empty($data) && ($size = count($data))) {
3057 for ($i=0; $i<$size; $i++) {
3059 if (isset($mbox) && is_array($entry)) {
3061 $entry = $last_entry;
3063 else if ($entry == '*') {
3064 if ($data[$i+1] == 'ANNOTATION') {
3065 $mbox = $data[$i+2];
3066 unset($data[$i]); // "*"
3067 unset($data[++$i]); // "ANNOTATION"
3068 unset($data[++$i]); // Mailbox
3070 // get rid of other untagged responses
3077 else if (isset($mbox)) {
3078 $attribs = $data[++$i];
3085 if (!empty($attribs)) {
3086 for ($x=0, $len=count($attribs); $x<$len;) {
3087 $attr = $attribs[$x++];
3088 $value = $attribs[$x++];
3089 if ($attr == 'value.priv') {
3090 $result[$mbox]['/private' . $entry] = $value;
3092 else if ($attr == 'value.shared') {
3093 $result[$mbox]['/shared' . $entry] = $value;
3097 $last_entry = $entry;
3109 * Creates next command identifier (tag)
3111 * @return string Command identifier
3118 $this->cmd_tag = sprintf('A%04d', $this->cmd_num);
3120 return $this->cmd_tag;
3124 * Sends IMAP command and parses result
3126 * @param string $command IMAP command
3127 * @param array $arguments Command arguments
3128 * @param int $options Execution options
3130 * @return mixed Response code or list of response code and data
3134 function execute($command, $arguments=array(), $options=0)
3136 $tag = $this->nextTag();
3137 $query = $tag . ' ' . $command;
3138 $noresp = ($options & self::COMMAND_NORESPONSE);
3139 $response = $noresp ? null : '';
3141 if (!empty($arguments)) {
3142 $query .= ' ' . implode(' ', $arguments);
3146 if (!$this->putLineC($query)) {
3147 $this->setError(self::ERROR_COMMAND, "Unable to send command: $query");
3148 return $noresp ? self::ERROR_COMMAND : array(self::ERROR_COMMAND, '');
3153 $line = $this->readLine(4096);
3154 if ($response !== null) {
3157 } while (!$this->startsWith($line, $tag . ' ', true, true));
3159 $code = $this->parseResult($line, $command . ': ');
3161 // Remove last line from response
3163 $line_len = min(strlen($response), strlen($line) + 2);
3164 $response = substr($response, 0, -$line_len);
3167 // optional CAPABILITY response
3168 if (($options & self::COMMAND_CAPABILITY) && $code == self::ERROR_OK
3169 && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)
3171 $this->parseCapability($matches[1], true);
3174 // return last line only (without command tag, result and response code)
3175 if ($line && ($options & self::COMMAND_LASTLINE)) {
3176 $response = preg_replace("/^$tag (OK|NO|BAD|BYE|PREAUTH)?\s*(\[[a-z-]+\])?\s*/i", '', trim($line));
3179 return $noresp ? $code : array($code, $response);
3183 * Splits IMAP response into string tokens
3185 * @param string &$str The IMAP's server response
3186 * @param int $num Number of tokens to return
3188 * @return mixed Tokens array or string if $num=1
3192 static function tokenizeResponse(&$str, $num=0)
3196 while (!$num || count($result) < $num) {
3197 // remove spaces from the beginning of the string
3204 if (($epos = strpos($str, "}\r\n", 1)) == false) {
3207 if (!is_numeric(($bytes = substr($str, 1, $epos - 1)))) {
3210 $result[] = substr($str, $epos + 3, $bytes);
3211 // Advance the string
3212 $str = substr($str, $epos + 3 + $bytes);
3217 $len = strlen($str);
3219 for ($pos=1; $pos<$len; $pos++) {
3220 if ($str[$pos] == '"') {
3223 if ($str[$pos] == "\\") {
3224 if ($str[$pos + 1] == '"' || $str[$pos + 1] == "\\") {
3229 if ($str[$pos] != '"') {
3232 // we need to strip slashes for a quoted string
3233 $result[] = stripslashes(substr($str, 1, $pos - 1));
3234 $str = substr($str, $pos + 1);
3237 // Parenthesized list
3239 $str = substr($str, 1);
3240 $result[] = self::tokenizeResponse($str);
3243 $str = substr($str, 1);
3247 // String atom, number, NIL, *, %
3249 // empty or one character
3253 if (strlen($str) < 2) {
3259 // excluded chars: SP, CTL, (, ), {, ", ], %
3260 if (preg_match('/^([\x21\x23\x24\x26\x27\x2A-\x5C\x5E-\x7A\x7C-\x7E]+)/', $str, $m)) {
3261 $result[] = $m[1] == 'NIL' ? NULL : $m[1];
3262 $str = substr($str, strlen($m[1]));
3268 return $num == 1 ? $result[0] : $result;
3271 private function _xor($string, $string2)
3274 $size = strlen($string);
3276 for ($i=0; $i<$size; $i++) {
3277 $result .= chr(ord($string[$i]) ^ ord($string2[$i]));
3284 * Converts datetime string into unix timestamp
3286 * @param string $date Date string
3288 * @return int Unix timestamp
3290 private function strToTime($date)
3292 // support non-standard "GMTXXXX" literal
3293 $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date);
3295 // if date parsing fails, we have a date in non-rfc format.
3296 // remove token from the end and try again
3297 while (($ts = intval(@strtotime($date))) <= 0) {
3298 $d = explode(' ', $date);
3303 $date = implode(' ', $d);
3308 return $ts < 0 ? 0 : $ts;
3311 private function parseCapability($str, $trusted=false)
3313 $str = preg_replace('/^\* CAPABILITY /i', '', $str);
3315 $this->capability = explode(' ', strtoupper($str));
3317 if (!isset($this->prefs['literal+']) && in_array('LITERAL+', $this->capability)) {
3318 $this->prefs['literal+'] = true;
3322 $this->capability_readed = true;
3327 * Escapes a string when it contains special characters (RFC3501)
3329 * @param string $string IMAP string
3330 * @param boolean $force_quotes Forces string quoting
3332 * @return string Escaped string
3333 * @todo String literals, lists
3335 static function escape($string, $force_quotes=false)
3337 if ($string === null) {
3340 else if ($string === '') {
3343 // need quoted-string? find special chars: SP, CTL, (, ), {, %, *, ", \, ]
3344 // plus [ character as a workaround for DBMail's bug (#1487766)
3345 else if ($force_quotes ||
3346 preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5B\x5C\x5D\x7F]+)/', $string)
3348 return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"';
3355 static function unEscape($string)
3357 return strtr($string, array('\\"'=>'"', '\\\\' => '\\'));
3361 * Set the value of the debugging flag.
3363 * @param boolean $debug New value for the debugging flag.
3368 function setDebug($debug, $handler = null)
3370 $this->_debug = $debug;
3371 $this->_debug_handler = $handler;
3375 * Write the given debug text to the current debug output handler.
3377 * @param string $message Debug mesage text.
3382 private function debug($message)
3384 if ($this->_debug_handler) {
3385 call_user_func_array($this->_debug_handler, array(&$this, $message));
3387 echo "DEBUG: $message\n";