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 4729 2011-05-04 18:53:11Z 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();
783 if (!empty($this->prefs['ident']) && $this->getCapability('ID')) {
784 $this->id($this->prefs['ident']);
787 $auth_methods = array();
790 // check for supported auth methods
791 if ($auth_method == 'CHECK') {
792 if ($auth_caps = $this->getCapability('AUTH')) {
793 $auth_methods = $auth_caps;
795 // RFC 2595 (LOGINDISABLED) LOGIN disabled when connection is not secure
796 $login_disabled = $this->getCapability('LOGINDISABLED');
797 if (($key = array_search('LOGIN', $auth_methods)) !== false) {
798 if ($login_disabled) {
799 unset($auth_methods[$key]);
802 else if (!$login_disabled) {
803 $auth_methods[] = 'LOGIN';
806 // Use best (for security) supported authentication method
807 foreach (array('DIGEST-MD5', 'CRAM-MD5', 'CRAM_MD5', 'PLAIN', 'LOGIN') as $auth_method) {
808 if (in_array($auth_method, $auth_methods)) {
814 // Prevent from sending credentials in plain text when connection is not secure
815 if ($auth_method == 'LOGIN' && $this->getCapability('LOGINDISABLED')) {
816 $this->setError(self::ERROR_BAD, "Login disabled by IMAP server");
817 $this->closeConnection();
820 // replace AUTH with CRAM-MD5 for backward compat.
821 if ($auth_method == 'AUTH') {
822 $auth_method = 'CRAM-MD5';
826 // pre-login capabilities can be not complete
827 $this->capability_readed = false;
830 switch ($auth_method) {
832 $auth_method = 'CRAM-MD5';
836 $result = $this->authenticate($user, $password, $auth_method);
839 $result = $this->login($user, $password);
842 $this->setError(self::ERROR_BAD, "Configuration error. Unknown auth method: $auth_method");
845 // Connected and authenticated
846 if (is_resource($result)) {
847 if ($this->prefs['force_caps']) {
848 $this->clearCapability();
850 $this->logged = true;
855 $this->closeConnection();
862 return ($this->fp && $this->logged) ? true : false;
865 function closeConnection()
867 if ($this->putLine($this->nextTag() . ' LOGOUT')) {
871 $this->closeSocket();
875 * Executes SELECT command (if mailbox is already not in selected state)
877 * @param string $mailbox Mailbox name
879 * @return boolean True on success, false on error
882 function select($mailbox)
884 if (!strlen($mailbox)) {
888 if ($this->selected == $mailbox) {
892 Temporary commented out because Courier returns \Noselect for INBOX
893 Requires more investigation
895 if (is_array($this->data['LIST']) && is_array($opts = $this->data['LIST'][$mailbox])) {
896 if (in_array('\\Noselect', $opts)) {
901 list($code, $response) = $this->execute('SELECT', array($this->escape($mailbox)));
903 if ($code == self::ERROR_OK) {
904 $response = explode("\r\n", $response);
905 foreach ($response as $line) {
906 if (preg_match('/^\* ([0-9]+) (EXISTS|RECENT)$/i', $line, $m)) {
907 $this->data[strtoupper($m[2])] = (int) $m[1];
909 else if (preg_match('/^\* OK \[(UIDNEXT|UIDVALIDITY|UNSEEN) ([0-9]+)\]/i', $line, $match)) {
910 $this->data[strtoupper($match[1])] = (int) $match[2];
912 else if (preg_match('/^\* OK \[PERMANENTFLAGS \(([^\)]+)\)\]/iU', $line, $match)) {
913 $this->data['PERMANENTFLAGS'] = explode(' ', $match[1]);
917 $this->data['READ-WRITE'] = $this->resultcode != 'READ-ONLY';
919 $this->selected = $mailbox;
927 * Executes STATUS command
929 * @param string $mailbox Mailbox name
930 * @param array $items Additional requested item names. By default
931 * MESSAGES and UNSEEN are requested. Other defined
932 * in RFC3501: UIDNEXT, UIDVALIDITY, RECENT
934 * @return array Status item-value hash
938 function status($mailbox, $items=array())
940 if (!strlen($mailbox)) {
944 if (!in_array('MESSAGES', $items)) {
945 $items[] = 'MESSAGES';
947 if (!in_array('UNSEEN', $items)) {
951 list($code, $response) = $this->execute('STATUS', array($this->escape($mailbox),
952 '(' . implode(' ', (array) $items) . ')'));
954 if ($code == self::ERROR_OK && preg_match('/\* STATUS /i', $response)) {
956 $response = substr($response, 9); // remove prefix "* STATUS "
958 list($mbox, $items) = $this->tokenizeResponse($response, 2);
960 // Fix for #1487859. Some buggy server returns not quoted
961 // folder name with spaces. Let's try to handle this situation
962 if (!is_array($items) && ($pos = strpos($response, '(')) !== false) {
963 $response = substr($response, $pos);
964 $items = $this->tokenizeResponse($response, 1);
965 if (!is_array($items)) {
970 for ($i=0, $len=count($items); $i<$len; $i += 2) {
971 $result[$items[$i]] = (int) $items[$i+1];
974 $this->data['STATUS:'.$mailbox] = $result;
983 * Executes EXPUNGE command
985 * @param string $mailbox Mailbox name
986 * @param string $messages Message UIDs to expunge
988 * @return boolean True on success, False on error
991 function expunge($mailbox, $messages=NULL)
993 if (!$this->select($mailbox)) {
997 if (!$this->data['READ-WRITE']) {
998 $this->setError(self::ERROR_READONLY, "Mailbox is read-only", 'EXPUNGE');
1002 // Clear internal status cache
1003 unset($this->data['STATUS:'.$mailbox]);
1006 $result = $this->execute('UID EXPUNGE', array($messages), self::COMMAND_NORESPONSE);
1008 $result = $this->execute('EXPUNGE', null, self::COMMAND_NORESPONSE);
1010 if ($result == self::ERROR_OK) {
1011 $this->selected = ''; // state has changed, need to reselect
1019 * Executes CLOSE command
1021 * @return boolean True on success, False on error
1027 $result = $this->execute('CLOSE', NULL, self::COMMAND_NORESPONSE);
1029 if ($result == self::ERROR_OK) {
1030 $this->selected = '';
1038 * Executes SUBSCRIBE command
1040 * @param string $mailbox Mailbox name
1042 * @return boolean True on success, False on error
1045 function subscribe($mailbox)
1047 $result = $this->execute('SUBSCRIBE', array($this->escape($mailbox)),
1048 self::COMMAND_NORESPONSE);
1050 return ($result == self::ERROR_OK);
1054 * Executes UNSUBSCRIBE command
1056 * @param string $mailbox Mailbox name
1058 * @return boolean True on success, False on error
1061 function unsubscribe($mailbox)
1063 $result = $this->execute('UNSUBSCRIBE', array($this->escape($mailbox)),
1064 self::COMMAND_NORESPONSE);
1066 return ($result == self::ERROR_OK);
1070 * Executes DELETE command
1072 * @param string $mailbox Mailbox name
1074 * @return boolean True on success, False on error
1077 function deleteFolder($mailbox)
1079 $result = $this->execute('DELETE', array($this->escape($mailbox)),
1080 self::COMMAND_NORESPONSE);
1082 return ($result == self::ERROR_OK);
1086 * Removes all messages in a folder
1088 * @param string $mailbox Mailbox name
1090 * @return boolean True on success, False on error
1093 function clearFolder($mailbox)
1095 $num_in_trash = $this->countMessages($mailbox);
1096 if ($num_in_trash > 0) {
1097 $res = $this->delete($mailbox, '1:*');
1101 if ($this->selected == $mailbox)
1102 $res = $this->close();
1104 $res = $this->expunge($mailbox);
1111 * Returns count of all messages in a folder
1113 * @param string $mailbox Mailbox name
1115 * @return int Number of messages, False on error
1118 function countMessages($mailbox, $refresh = false)
1121 $this->selected = '';
1124 if ($this->selected == $mailbox) {
1125 return $this->data['EXISTS'];
1128 // Check internal cache
1129 $cache = $this->data['STATUS:'.$mailbox];
1130 if (!empty($cache) && isset($cache['MESSAGES'])) {
1131 return (int) $cache['MESSAGES'];
1134 // Try STATUS (should be faster than SELECT)
1135 $counts = $this->status($mailbox);
1136 if (is_array($counts)) {
1137 return (int) $counts['MESSAGES'];
1144 * Returns count of messages with \Recent flag in a folder
1146 * @param string $mailbox Mailbox name
1148 * @return int Number of messages, False on error
1151 function countRecent($mailbox)
1153 if (!strlen($mailbox)) {
1157 $this->select($mailbox);
1159 if ($this->selected == $mailbox) {
1160 return $this->data['RECENT'];
1167 * Returns count of messages without \Seen flag in a specified folder
1169 * @param string $mailbox Mailbox name
1171 * @return int Number of messages, False on error
1174 function countUnseen($mailbox)
1176 // Check internal cache
1177 $cache = $this->data['STATUS:'.$mailbox];
1178 if (!empty($cache) && isset($cache['UNSEEN'])) {
1179 return (int) $cache['UNSEEN'];
1182 // Try STATUS (should be faster than SELECT+SEARCH)
1183 $counts = $this->status($mailbox);
1184 if (is_array($counts)) {
1185 return (int) $counts['UNSEEN'];
1188 // Invoke SEARCH as a fallback
1189 $index = $this->search($mailbox, 'ALL UNSEEN', false, array('COUNT'));
1190 if (is_array($index)) {
1191 return (int) $index['COUNT'];
1198 * Executes ID command (RFC2971)
1200 * @param array $items Client identification information key/value hash
1202 * @return array Server identification information key/value hash
1206 function id($items=array())
1208 if (is_array($items) && !empty($items)) {
1209 foreach ($items as $key => $value) {
1210 $args[] = $this->escape($key, true);
1211 $args[] = $this->escape($value, true);
1215 list($code, $response) = $this->execute('ID', array(
1216 !empty($args) ? '(' . implode(' ', (array) $args) . ')' : $this->escape(null)
1220 if ($code == self::ERROR_OK && preg_match('/\* ID /i', $response)) {
1221 $response = substr($response, 5); // remove prefix "* ID "
1222 $items = $this->tokenizeResponse($response, 1);
1225 for ($i=0, $len=count($items); $i<$len; $i += 2) {
1226 $result[$items[$i]] = $items[$i+1];
1235 function sort($mailbox, $field, $add='', $is_uid=FALSE, $encoding = 'US-ASCII')
1237 $field = strtoupper($field);
1238 if ($field == 'INTERNALDATE') {
1242 $fields = array('ARRIVAL' => 1,'CC' => 1,'DATE' => 1,
1243 'FROM' => 1, 'SIZE' => 1, 'SUBJECT' => 1, 'TO' => 1);
1245 if (!$fields[$field]) {
1249 if (!$this->select($mailbox)) {
1255 $add = $this->compressMessageSet($add);
1257 list($code, $response) = $this->execute($is_uid ? 'UID SORT' : 'SORT',
1258 array("($field)", $encoding, 'ALL' . (!empty($add) ? ' '.$add : '')));
1260 if ($code == self::ERROR_OK) {
1261 // remove prefix and unilateral untagged server responses
1262 $response = substr($response, stripos($response, '* SORT') + 7);
1263 if ($pos = strpos($response, '*')) {
1264 $response = substr($response, 0, $pos);
1266 return preg_split('/[\s\r\n]+/', $response, -1, PREG_SPLIT_NO_EMPTY);
1272 function fetchHeaderIndex($mailbox, $message_set, $index_field='', $skip_deleted=true, $uidfetch=false)
1274 if (is_array($message_set)) {
1275 if (!($message_set = $this->compressMessageSet($message_set)))
1278 list($from_idx, $to_idx) = explode(':', $message_set);
1279 if (empty($message_set) ||
1280 (isset($to_idx) && $to_idx != '*' && (int)$from_idx > (int)$to_idx)) {
1285 $index_field = empty($index_field) ? 'DATE' : strtoupper($index_field);
1287 $fields_a['DATE'] = 1;
1288 $fields_a['INTERNALDATE'] = 4;
1289 $fields_a['ARRIVAL'] = 4;
1290 $fields_a['FROM'] = 1;
1291 $fields_a['REPLY-TO'] = 1;
1292 $fields_a['SENDER'] = 1;
1293 $fields_a['TO'] = 1;
1294 $fields_a['CC'] = 1;
1295 $fields_a['SUBJECT'] = 1;
1296 $fields_a['UID'] = 2;
1297 $fields_a['SIZE'] = 2;
1298 $fields_a['SEEN'] = 3;
1299 $fields_a['RECENT'] = 3;
1300 $fields_a['DELETED'] = 3;
1302 if (!($mode = $fields_a[$index_field])) {
1306 /* Do "SELECT" command */
1307 if (!$this->select($mailbox)) {
1311 // build FETCH command string
1312 $key = $this->nextTag();
1313 $cmd = $uidfetch ? 'UID FETCH' : 'FETCH';
1314 $deleted = $skip_deleted ? ' FLAGS' : '';
1316 if ($mode == 1 && $index_field == 'DATE')
1317 $request = " $cmd $message_set (INTERNALDATE BODY.PEEK[HEADER.FIELDS (DATE)]$deleted)";
1318 else if ($mode == 1)
1319 $request = " $cmd $message_set (BODY.PEEK[HEADER.FIELDS ($index_field)]$deleted)";
1320 else if ($mode == 2) {
1321 if ($index_field == 'SIZE')
1322 $request = " $cmd $message_set (RFC822.SIZE$deleted)";
1324 $request = " $cmd $message_set ($index_field$deleted)";
1325 } else if ($mode == 3)
1326 $request = " $cmd $message_set (FLAGS)";
1328 $request = " $cmd $message_set (INTERNALDATE$deleted)";
1330 $request = $key . $request;
1332 if (!$this->putLine($request)) {
1333 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
1340 $line = rtrim($this->readLine(200));
1341 $line = $this->multLine($line);
1343 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
1347 if ($skip_deleted && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
1348 $flags = explode(' ', strtoupper($matches[1]));
1349 if (in_array('\\DELETED', $flags)) {
1350 $deleted[$id] = $id;
1355 if ($mode == 1 && $index_field == 'DATE') {
1356 if (preg_match('/BODY\[HEADER\.FIELDS \("*DATE"*\)\] (.*)/', $line, $matches)) {
1357 $value = preg_replace(array('/^"*[a-z]+:/i'), '', $matches[1]);
1358 $value = trim($value);
1359 $result[$id] = $this->strToTime($value);
1361 // non-existent/empty Date: header, use INTERNALDATE
1362 if (empty($result[$id])) {
1363 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches))
1364 $result[$id] = $this->strToTime($matches[1]);
1368 } else if ($mode == 1) {
1369 if (preg_match('/BODY\[HEADER\.FIELDS \("?(FROM|REPLY-TO|SENDER|TO|SUBJECT)"?\)\] (.*)/', $line, $matches)) {
1370 $value = preg_replace(array('/^"*[a-z]+:/i', '/\s+$/sm'), array('', ''), $matches[2]);
1371 $result[$id] = trim($value);
1375 } else if ($mode == 2) {
1376 if (preg_match('/(UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) {
1377 $result[$id] = trim($matches[2]);
1381 } else if ($mode == 3) {
1382 if (!$flags && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
1383 $flags = explode(' ', $matches[1]);
1385 $result[$id] = in_array('\\'.$index_field, $flags) ? 1 : 0;
1386 } else if ($mode == 4) {
1387 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
1388 $result[$id] = $this->strToTime($matches[1]);
1394 } while (!$this->startsWith($line, $key, true, true));
1399 static function compressMessageSet($messages, $force=false)
1401 // given a comma delimited list of independent mid's,
1402 // compresses by grouping sequences together
1404 if (!is_array($messages)) {
1405 // if less than 255 bytes long, let's not bother
1406 if (!$force && strlen($messages)<255) {
1410 // see if it's already been compressed
1411 if (strpos($messages, ':') !== false) {
1415 // separate, then sort
1416 $messages = explode(',', $messages);
1422 $start = $prev = $messages[0];
1424 foreach ($messages as $id) {
1425 $incr = $id - $prev;
1426 if ($incr > 1) { // found a gap
1427 if ($start == $prev) {
1428 $result[] = $prev; // push single id
1430 $result[] = $start . ':' . $prev; // push sequence as start_id:end_id
1432 $start = $id; // start of new sequence
1437 // handle the last sequence/id
1438 if ($start == $prev) {
1441 $result[] = $start.':'.$prev;
1444 // return as comma separated string
1445 return implode(',', $result);
1448 static function uncompressMessageSet($messages)
1451 $messages = explode(',', $messages);
1453 foreach ($messages as $part) {
1454 $items = explode(':', $part);
1455 $max = max($items[0], $items[1]);
1457 for ($x=$items[0]; $x<=$max; $x++) {
1466 * Returns message sequence identifier
1468 * @param string $mailbox Mailbox name
1469 * @param int $uid Message unique identifier (UID)
1471 * @return int Message sequence identifier
1474 function UID2ID($mailbox, $uid)
1477 $id_a = $this->search($mailbox, "UID $uid");
1478 if (is_array($id_a) && count($id_a) == 1) {
1479 return (int) $id_a[0];
1486 * Returns message unique identifier (UID)
1488 * @param string $mailbox Mailbox name
1489 * @param int $uid Message sequence identifier
1491 * @return int Message unique identifier
1494 function ID2UID($mailbox, $id)
1496 if (empty($id) || $id < 0) {
1500 if (!$this->select($mailbox)) {
1504 list($code, $response) = $this->execute('FETCH', array($id, '(UID)'));
1506 if ($code == self::ERROR_OK && preg_match("/^\* $id FETCH \(UID (.*)\)/i", $response, $m)) {
1513 function fetchUIDs($mailbox, $message_set=null)
1515 if (is_array($message_set))
1516 $message_set = join(',', $message_set);
1517 else if (empty($message_set))
1518 $message_set = '1:*';
1520 return $this->fetchHeaderIndex($mailbox, $message_set, 'UID', false);
1523 function fetchHeaders($mailbox, $message_set, $uidfetch=false, $bodystr=false, $add='')
1527 if (!$this->select($mailbox)) {
1531 $message_set = $this->compressMessageSet($message_set);
1534 $add = ' '.trim($add);
1536 /* FETCH uid, size, flags and headers */
1537 $key = $this->nextTag();
1538 $request = $key . ($uidfetch ? ' UID' : '') . " FETCH $message_set ";
1539 $request .= "(UID RFC822.SIZE FLAGS INTERNALDATE ";
1541 $request .= "BODYSTRUCTURE ";
1542 $request .= "BODY.PEEK[HEADER.FIELDS (DATE FROM TO SUBJECT CONTENT-TYPE ";
1543 $request .= "LIST-POST DISPOSITION-NOTIFICATION-TO".$add.")])";
1545 if (!$this->putLine($request)) {
1546 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
1550 $line = $this->readLine(4096);
1551 $line = $this->multLine($line);
1556 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
1557 $id = intval($m[1]);
1559 $result[$id] = new rcube_mail_header;
1560 $result[$id]->id = $id;
1561 $result[$id]->subject = '';
1562 $result[$id]->messageID = 'mid:' . $id;
1567 // Sample reply line:
1568 // * 321 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen)
1569 // INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODYSTRUCTURE (...)
1570 // BODY[HEADER.FIELDS ...
1572 if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY/sU', $line, $matches)) {
1575 while (list($name, $value) = $this->tokenizeResponse($str, 2)) {
1576 if ($name == 'UID') {
1577 $result[$id]->uid = intval($value);
1579 else if ($name == 'RFC822.SIZE') {
1580 $result[$id]->size = intval($value);
1582 else if ($name == 'INTERNALDATE') {
1583 $result[$id]->internaldate = $value;
1584 $result[$id]->date = $value;
1585 $result[$id]->timestamp = $this->StrToTime($value);
1587 else if ($name == 'FLAGS') {
1594 while (!preg_match('/ BODYSTRUCTURE (.*) BODY\[HEADER.FIELDS/sU', $line, $m)) {
1595 $line2 = $this->readLine(1024);
1596 $line .= $this->multLine($line2, true);
1598 $result[$id]->body_structure = $m[1];
1601 // the rest of the result
1602 if (preg_match('/ BODY\[HEADER.FIELDS \(.*?\)\]\s*(.*)$/s', $line, $m)) {
1603 $reslines = explode("\n", trim($m[1], '"'));
1604 // re-parse (see below)
1605 foreach ($reslines as $resln) {
1606 if (ord($resln[0])<=32) {
1607 $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($resln);
1609 $lines[++$ln] = trim($resln);
1615 // Start parsing headers. The problem is, some header "lines" take up multiple lines.
1616 // So, we'll read ahead, and if the one we're reading now is a valid header, we'll
1617 // process the previous line. Otherwise, we'll keep adding the strings until we come
1618 // to the next valid header line.
1621 $line = rtrim($this->readLine(300), "\r\n");
1623 // The preg_match below works around communigate imap, which outputs " UID <number>)".
1624 // Without this, the while statement continues on and gets the "FH0 OK completed" message.
1625 // If this loop gets the ending message, then the outer loop does not receive it from radline on line 1249.
1626 // This in causes the if statement on line 1278 to never be true, which causes the headers to end up missing
1627 // If the if statement was changed to pick up the fh0 from this loop, then it causes the outer loop to spin
1628 // An alternative might be:
1629 // if (!preg_match("/:/",$line) && preg_match("/\)$/",$line)) break;
1630 // however, unsure how well this would work with all imap clients.
1631 if (preg_match("/^\s*UID [0-9]+\)$/", $line)) {
1635 // handle FLAGS reply after headers (AOL, Zimbra?)
1636 if (preg_match('/\s+FLAGS \((.*)\)\)$/', $line, $matches)) {
1637 $flags_a = $this->tokenizeResponse($matches[1]);
1641 if (ord($line[0])<=32) {
1642 $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($line);
1644 $lines[++$ln] = trim($line);
1646 // patch from "Maksim Rubis" <siburny@hotmail.com>
1647 } while ($line[0] != ')' && !$this->startsWith($line, $key, true));
1649 if (strncmp($line, $key, strlen($key))) {
1650 // process header, fill rcube_mail_header obj.
1652 if (is_array($headers)) {
1654 while (list($k, $bar) = each($headers)) {
1659 // create array with header field:data
1660 while (list($lines_key, $str) = each($lines)) {
1661 list($field, $string) = explode(':', $str, 2);
1663 $field = strtolower($field);
1664 $string = preg_replace('/\n[\t\s]*/', ' ', trim($string));
1668 $result[$id]->date = $string;
1669 $result[$id]->timestamp = $this->strToTime($string);
1672 $result[$id]->from = $string;
1675 $result[$id]->to = preg_replace('/undisclosed-recipients:[;,]*/', '', $string);
1678 $result[$id]->subject = $string;
1681 $result[$id]->replyto = $string;
1684 $result[$id]->cc = $string;
1687 $result[$id]->bcc = $string;
1689 case 'content-transfer-encoding':
1690 $result[$id]->encoding = $string;
1692 case 'content-type':
1693 $ctype_parts = preg_split('/[; ]/', $string);
1694 $result[$id]->ctype = strtolower(array_shift($ctype_parts));
1695 if (preg_match('/charset\s*=\s*"?([a-z0-9\-\.\_]+)"?/i', $string, $regs)) {
1696 $result[$id]->charset = $regs[1];
1700 $result[$id]->in_reply_to = str_replace(array("\n", '<', '>'), '', $string);
1703 $result[$id]->references = $string;
1705 case 'return-receipt-to':
1706 case 'disposition-notification-to':
1707 case 'x-confirm-reading-to':
1708 $result[$id]->mdn_to = $string;
1711 $result[$id]->messageID = $string;
1714 if (preg_match('/^(\d+)/', $string, $matches)) {
1715 $result[$id]->priority = intval($matches[1]);
1719 if (strlen($field) > 2) {
1720 $result[$id]->others[$field] = $string;
1728 if (!empty($flags_a)) {
1729 foreach ($flags_a as $flag) {
1730 $flag = str_replace('\\', '', $flag);
1731 $result[$id]->flags[] = $flag;
1733 switch (strtoupper($flag)) {
1735 $result[$id]->seen = true;
1738 $result[$id]->deleted = true;
1741 $result[$id]->answered = true;
1744 $result[$id]->forwarded = true;
1747 $result[$id]->mdn_sent = true;
1750 $result[$id]->flagged = true;
1756 } while (!$this->startsWith($line, $key, true));
1761 function fetchHeader($mailbox, $id, $uidfetch=false, $bodystr=false, $add='')
1763 $a = $this->fetchHeaders($mailbox, $id, $uidfetch, $bodystr, $add);
1765 return array_shift($a);
1770 function sortHeaders($a, $field, $flag)
1772 if (empty($field)) {
1776 $field = strtolower($field);
1779 if ($field == 'date' || $field == 'internaldate') {
1780 $field = 'timestamp';
1786 $flag = strtoupper($flag);
1792 // First, we'll create an "index" array.
1793 // Then, we'll use sort() on that array,
1794 // and use that to sort the main array.
1796 // create "index" array
1799 while (list($key, $val) = each($a)) {
1800 if ($field == 'timestamp') {
1801 $data = $this->strToTime($val->date);
1803 $data = $val->timestamp;
1806 $data = $val->$field;
1807 if (is_string($data)) {
1808 $data = str_replace('"', '', $data);
1809 if ($field == 'subject') {
1810 $data = preg_replace('/^(Re: \s*|Fwd:\s*|Fw:\s*)+/i', '', $data);
1812 $data = strtoupper($data);
1815 $index[$key] = $data;
1819 if ($flag == 'ASC') {
1825 // form new array based on index
1828 while (list($key, $val) = each($index)) {
1829 $result[$key] = $a[$key];
1837 function modFlag($mailbox, $messages, $flag, $mod)
1839 if ($mod != '+' && $mod != '-') {
1843 if (!$this->select($mailbox)) {
1847 if (!$this->data['READ-WRITE']) {
1848 $this->setError(self::ERROR_READONLY, "Mailbox is read-only", 'STORE');
1852 // Clear internal status cache
1853 if ($flag == 'SEEN') {
1854 unset($this->data['STATUS:'.$mailbox]['UNSEEN']);
1857 $flag = $this->flags[strtoupper($flag)];
1858 $result = $this->execute('UID STORE', array(
1859 $this->compressMessageSet($messages), $mod . 'FLAGS.SILENT', "($flag)"),
1860 self::COMMAND_NORESPONSE);
1862 return ($result == self::ERROR_OK);
1865 function flag($mailbox, $messages, $flag) {
1866 return $this->modFlag($mailbox, $messages, $flag, '+');
1869 function unflag($mailbox, $messages, $flag) {
1870 return $this->modFlag($mailbox, $messages, $flag, '-');
1873 function delete($mailbox, $messages) {
1874 return $this->modFlag($mailbox, $messages, 'DELETED', '+');
1877 function copy($messages, $from, $to)
1879 if (!$this->select($from)) {
1883 // Clear internal status cache
1884 unset($this->data['STATUS:'.$to]);
1886 $result = $this->execute('UID COPY', array(
1887 $this->compressMessageSet($messages), $this->escape($to)),
1888 self::COMMAND_NORESPONSE);
1890 return ($result == self::ERROR_OK);
1893 function move($messages, $from, $to)
1895 if (!$this->select($from)) {
1899 if (!$this->data['READ-WRITE']) {
1900 $this->setError(self::ERROR_READONLY, "Mailbox is read-only", 'STORE');
1904 $r = $this->copy($messages, $from, $to);
1907 // Clear internal status cache
1908 unset($this->data['STATUS:'.$from]);
1910 return $this->delete($from, $messages);
1915 // Don't be tempted to change $str to pass by reference to speed this up - it will slow it down by about
1916 // 7 times instead :-) See comments on http://uk2.php.net/references and this article:
1917 // http://derickrethans.nl/files/phparch-php-variables-article.pdf
1918 private function parseThread($str, $begin, $end, $root, $parent, $depth, &$depthmap, &$haschildren)
1921 if ($str[$begin] != '(') {
1922 $stop = $begin + strspn($str, '1234567890', $begin, $end - $begin);
1923 $msg = substr($str, $begin, $stop - $begin);
1928 $depthmap[$msg] = $depth;
1929 $haschildren[$msg] = false;
1930 if (!is_null($parent))
1931 $haschildren[$parent] = true;
1932 if ($stop + 1 < $end)
1933 $node[$msg] = $this->parseThread($str, $stop + 1, $end, $root, $msg, $depth + 1, $depthmap, $haschildren);
1935 $node[$msg] = array();
1938 while ($off < $end) {
1943 $p = strpos($str, ')', $off);
1945 error_log("Mismatched brackets parsing IMAP THREAD response:");
1946 error_log(substr($str, ($begin < 10) ? 0 : ($begin - 10), $end - $begin + 20));
1947 error_log(str_repeat(' ', $off - (($begin < 10) ? 0 : ($begin - 10))));
1950 $p1 = strpos($str, '(', $off);
1951 if ($p1 !== false && $p1 < $p) {
1959 $node += $this->parseThread($str, $start + 1, $off - 1, $root, $parent, $depth, $depthmap, $haschildren);
1966 function thread($mailbox, $algorithm='REFERENCES', $criteria='', $encoding='US-ASCII')
1968 $old_sel = $this->selected;
1970 if (!$this->select($mailbox)) {
1974 // return empty result when folder is empty and we're just after SELECT
1975 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
1976 return array(array(), array(), array());
1979 $encoding = $encoding ? trim($encoding) : 'US-ASCII';
1980 $algorithm = $algorithm ? trim($algorithm) : 'REFERENCES';
1981 $criteria = $criteria ? 'ALL '.trim($criteria) : 'ALL';
1984 list($code, $response) = $this->execute('THREAD', array(
1985 $algorithm, $encoding, $criteria));
1987 if ($code == self::ERROR_OK) {
1989 $response = substr($response, stripos($response, '* THREAD') + 9);
1990 // ...unilateral untagged server responses
1991 if ($pos = strpos($response, '*')) {
1992 $response = substr($response, 0, $pos);
1995 $response = str_replace("\r\n", '', $response);
1996 $depthmap = array();
1997 $haschildren = array();
1999 $tree = $this->parseThread($response, 0, strlen($response),
2000 null, null, 0, $depthmap, $haschildren);
2002 return array($tree, $depthmap, $haschildren);
2009 * Executes SEARCH command
2011 * @param string $mailbox Mailbox name
2012 * @param string $criteria Searching criteria
2013 * @param bool $return_uid Enable UID in result instead of sequence ID
2014 * @param array $items Return items (MIN, MAX, COUNT, ALL)
2016 * @return array Message identifiers or item-value hash
2018 function search($mailbox, $criteria, $return_uid=false, $items=array())
2020 $old_sel = $this->selected;
2022 if (!$this->select($mailbox)) {
2026 // return empty result when folder is empty and we're just after SELECT
2027 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
2029 return array_combine($items, array_fill(0, count($items), 0));
2034 $esearch = empty($items) ? false : $this->getCapability('ESEARCH');
2035 $criteria = trim($criteria);
2039 if (!empty($items) && $esearch) {
2040 $params .= 'RETURN (' . implode(' ', $items) . ')';
2042 if (!empty($criteria)) {
2043 $params .= ($params ? ' ' : '') . $criteria;
2049 list($code, $response) = $this->execute($return_uid ? 'UID SEARCH' : 'SEARCH',
2052 if ($code == self::ERROR_OK) {
2054 $response = substr($response, stripos($response,
2055 $esearch ? '* ESEARCH' : '* SEARCH') + ($esearch ? 10 : 9));
2056 // ...and unilateral untagged server responses
2057 if ($pos = strpos($response, '*')) {
2058 $response = rtrim(substr($response, 0, $pos));
2062 // Skip prefix: ... (TAG "A285") UID ...
2063 $this->tokenizeResponse($response, $return_uid ? 2 : 1);
2066 for ($i=0; $i<count($items); $i++) {
2067 // If the SEARCH results in no matches, the server MUST NOT
2068 // include the item result option in the ESEARCH response
2069 if ($ret = $this->tokenizeResponse($response, 2)) {
2070 list ($name, $value) = $ret;
2071 $result[$name] = $value;
2078 $response = preg_split('/[\s\r\n]+/', $response, -1, PREG_SPLIT_NO_EMPTY);
2080 if (!empty($items)) {
2082 if (in_array('COUNT', $items)) {
2083 $result['COUNT'] = count($response);
2085 if (in_array('MIN', $items)) {
2086 $result['MIN'] = !empty($response) ? min($response) : 0;
2088 if (in_array('MAX', $items)) {
2089 $result['MAX'] = !empty($response) ? max($response) : 0;
2091 if (in_array('ALL', $items)) {
2092 $result['ALL'] = $this->compressMessageSet($response, true);
2107 * Returns list of mailboxes
2109 * @param string $ref Reference name
2110 * @param string $mailbox Mailbox name
2111 * @param array $status_opts (see self::_listMailboxes)
2112 * @param array $select_opts (see self::_listMailboxes)
2114 * @return array List of mailboxes or hash of options if $status_opts argument
2118 function listMailboxes($ref, $mailbox, $status_opts=array(), $select_opts=array())
2120 return $this->_listMailboxes($ref, $mailbox, false, $status_opts, $select_opts);
2124 * Returns list of subscribed mailboxes
2126 * @param string $ref Reference name
2127 * @param string $mailbox Mailbox name
2128 * @param array $status_opts (see self::_listMailboxes)
2130 * @return array List of mailboxes or hash of options if $status_opts argument
2134 function listSubscribed($ref, $mailbox, $status_opts=array())
2136 return $this->_listMailboxes($ref, $mailbox, true, $status_opts, NULL);
2140 * IMAP LIST/LSUB command
2142 * @param string $ref Reference name
2143 * @param string $mailbox Mailbox name
2144 * @param bool $subscribed Enables returning subscribed mailboxes only
2145 * @param array $status_opts List of STATUS options (RFC5819: LIST-STATUS)
2146 * Possible: MESSAGES, RECENT, UIDNEXT, UIDVALIDITY, UNSEEN
2147 * @param array $select_opts List of selection options (RFC5258: LIST-EXTENDED)
2148 * Possible: SUBSCRIBED, RECURSIVEMATCH, REMOTE
2150 * @return array List of mailboxes or hash of options if $status_ops argument
2154 private function _listMailboxes($ref, $mailbox, $subscribed=false,
2155 $status_opts=array(), $select_opts=array())
2157 if (!strlen($mailbox)) {
2163 if (!empty($select_opts) && $this->getCapability('LIST-EXTENDED')) {
2164 $select_opts = (array) $select_opts;
2166 $args[] = '(' . implode(' ', $select_opts) . ')';
2169 $args[] = $this->escape($ref);
2170 $args[] = $this->escape($mailbox);
2172 if (!empty($status_opts) && $this->getCapability('LIST-STATUS')) {
2173 $status_opts = (array) $status_opts;
2176 $args[] = 'RETURN (STATUS (' . implode(' ', $status_opts) . '))';
2179 list($code, $response) = $this->execute($subscribed ? 'LSUB' : 'LIST', $args);
2181 if ($code == self::ERROR_OK) {
2183 while ($this->tokenizeResponse($response, 1) == '*') {
2184 $cmd = strtoupper($this->tokenizeResponse($response, 1));
2185 // * LIST (<options>) <delimiter> <mailbox>
2186 if (!$lstatus || $cmd == 'LIST' || $cmd == 'LSUB') {
2187 list($opts, $delim, $mailbox) = $this->tokenizeResponse($response, 3);
2189 // Add to result array
2191 $folders[] = $mailbox;
2194 $folders[$mailbox] = array();
2197 // Add to options array
2198 if (!empty($opts)) {
2199 if (empty($this->data['LIST'][$mailbox]))
2200 $this->data['LIST'][$mailbox] = $opts;
2202 $this->data['LIST'][$mailbox] = array_unique(array_merge(
2203 $this->data['LIST'][$mailbox], $opts));
2206 // * STATUS <mailbox> (<result>)
2207 else if ($cmd == 'STATUS') {
2208 list($mailbox, $status) = $this->tokenizeResponse($response, 2);
2210 for ($i=0, $len=count($status); $i<$len; $i += 2) {
2211 list($name, $value) = $this->tokenizeResponse($status, 2);
2212 $folders[$mailbox][$name] = $value;
2223 function fetchMIMEHeaders($mailbox, $id, $parts, $mime=true)
2225 if (!$this->select($mailbox)) {
2230 $parts = (array) $parts;
2231 $key = $this->nextTag();
2234 $type = $mime ? 'MIME' : 'HEADER';
2237 foreach($parts as $part) {
2238 $peeks[] = "BODY.PEEK[$part.$type]";
2241 $request = "$key FETCH $id (" . implode(' ', $peeks) . ')';
2244 if (!$this->putLine($request)) {
2245 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2250 $line = $this->readLine(1024);
2251 $line = $this->multLine($line);
2253 if (preg_match('/BODY\[([0-9\.]+)\.'.$type.'\]/', $line, $matches)) {
2255 $result[$idx] = preg_replace('/^(\* '.$id.' FETCH \()?\s*BODY\['.$idx.'\.'.$type.'\]\s+/', '', $line);
2256 $result[$idx] = trim($result[$idx], '"');
2257 $result[$idx] = rtrim($result[$idx], "\t\r\n\0\x0B");
2259 } while (!$this->startsWith($line, $key, true));
2264 function fetchPartHeader($mailbox, $id, $is_uid=false, $part=NULL)
2266 $part = empty($part) ? 'HEADER' : $part.'.MIME';
2268 return $this->handlePartBody($mailbox, $id, $is_uid, $part);
2271 function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL)
2273 if (!$this->select($mailbox)) {
2277 switch ($encoding) {
2281 case 'quoted-printable':
2295 $reply_key = '* ' . $id;
2296 $key = $this->nextTag();
2297 $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
2300 if (!$this->putLine($request)) {
2301 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2305 // receive reply line
2307 $line = rtrim($this->readLine(1024));
2308 $a = explode(' ', $line);
2309 } while (!($end = $this->startsWith($line, $key, true)) && $a[2] != 'FETCH');
2311 $len = strlen($line);
2314 // handle empty "* X FETCH ()" response
2315 if ($line[$len-1] == ')' && $line[$len-2] != '(') {
2316 // one line response, get everything between first and last quotes
2317 if (substr($line, -4, 3) == 'NIL') {
2321 $from = strpos($line, '"') + 1;
2322 $to = strrpos($line, '"');
2324 $result = substr($line, $from, $len);
2328 $result = base64_decode($result);
2330 else if ($mode == 2) {
2331 $result = quoted_printable_decode($result);
2333 else if ($mode == 3) {
2334 $result = convert_uudecode($result);
2337 } else if ($line[$len-1] == '}') {
2338 // multi-line request, find sizes of content and receive that many bytes
2339 $from = strpos($line, '{') + 1;
2340 $to = strrpos($line, '}');
2342 $sizeStr = substr($line, $from, $len);
2343 $bytes = (int)$sizeStr;
2346 while ($bytes > 0) {
2347 $line = $this->readLine(4096);
2349 if ($line === NULL) {
2353 $len = strlen($line);
2355 if ($len > $bytes) {
2356 $line = substr($line, 0, $bytes);
2357 $len = strlen($line);
2363 $line = rtrim($line, "\t\r\n\0\x0B");
2364 // create chunks with proper length for base64 decoding
2365 $line = $prev.$line;
2366 $length = strlen($line);
2368 $length = floor($length / 4) * 4;
2369 $prev = substr($line, $length);
2370 $line = substr($line, 0, $length);
2374 $line = base64_decode($line);
2376 } else if ($mode == 2) {
2377 $line = rtrim($line, "\t\r\0\x0B");
2378 $line = quoted_printable_decode($line);
2379 // Remove NULL characters (#1486189)
2380 $line = str_replace("\x00", '', $line);
2382 } else if ($mode == 3) {
2383 $line = rtrim($line, "\t\r\n\0\x0B");
2384 if ($line == 'end' || preg_match('/^begin\s+[0-7]+\s+.+$/', $line))
2386 $line = convert_uudecode($line);
2389 $line = rtrim($line, "\t\r\n\0\x0B") . "\n";
2393 fwrite($file, $line);
2401 // read in anything up until last line
2404 $line = $this->readLine(1024);
2405 } while (!$this->startsWith($line, $key, true));
2407 if ($result !== false) {
2409 fwrite($file, $result);
2410 } else if ($print) {
2420 function createFolder($mailbox)
2422 $result = $this->execute('CREATE', array($this->escape($mailbox)),
2423 self::COMMAND_NORESPONSE);
2425 return ($result == self::ERROR_OK);
2428 function renameFolder($from, $to)
2430 $result = $this->execute('RENAME', array($this->escape($from), $this->escape($to)),
2431 self::COMMAND_NORESPONSE);
2433 return ($result == self::ERROR_OK);
2436 function append($mailbox, &$message)
2442 $message = str_replace("\r", '', $message);
2443 $message = str_replace("\n", "\r\n", $message);
2445 $len = strlen($message);
2450 $key = $this->nextTag();
2451 $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox),
2452 $len, ($this->prefs['literal+'] ? '+' : ''));
2454 if ($this->putLine($request)) {
2455 // Don't wait when LITERAL+ is supported
2456 if (!$this->prefs['literal+']) {
2457 $line = $this->readReply();
2459 if ($line[0] != '+') {
2460 $this->parseResult($line, 'APPEND: ');
2465 if (!$this->putLine($message)) {
2470 $line = $this->readLine();
2471 } while (!$this->startsWith($line, $key, true, true));
2473 // Clear internal status cache
2474 unset($this->data['STATUS:'.$mailbox]);
2476 return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK);
2479 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2485 function appendFromFile($mailbox, $path, $headers=null)
2491 // open message file
2493 if (file_exists(realpath($path))) {
2494 $in_fp = fopen($path, 'r');
2497 $this->setError(self::ERROR_UNKNOWN, "Couldn't open $path for reading");
2501 $body_separator = "\r\n\r\n";
2502 $len = filesize($path);
2509 $headers = preg_replace('/[\r\n]+$/', '', $headers);
2510 $len += strlen($headers) + strlen($body_separator);
2513 // send APPEND command
2514 $key = $this->nextTag();
2515 $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox),
2516 $len, ($this->prefs['literal+'] ? '+' : ''));
2518 if ($this->putLine($request)) {
2519 // Don't wait when LITERAL+ is supported
2520 if (!$this->prefs['literal+']) {
2521 $line = $this->readReply();
2523 if ($line[0] != '+') {
2524 $this->parseResult($line, 'APPEND: ');
2529 // send headers with body separator
2531 $this->putLine($headers . $body_separator, false);
2535 while (!feof($in_fp) && $this->fp) {
2536 $buffer = fgets($in_fp, 4096);
2537 $this->putLine($buffer, false);
2541 if (!$this->putLine('')) { // \r\n
2547 $line = $this->readLine();
2548 } while (!$this->startsWith($line, $key, true, true));
2550 // Clear internal status cache
2551 unset($this->data['STATUS:'.$mailbox]);
2553 return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK);
2556 $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
2562 function fetchStructureString($mailbox, $id, $is_uid=false)
2564 if (!$this->select($mailbox)) {
2568 $key = $this->nextTag();
2570 $command = $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)";
2572 if ($this->putLine($command)) {
2574 $line = $this->readLine(5000);
2575 $line = $this->multLine($line, true);
2576 if (!preg_match("/^$key /", $line))
2578 } while (!$this->startsWith($line, $key, true, true));
2580 $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -1));
2583 $this->setError(self::ERROR_COMMAND, "Unable to send command: $command");
2592 * GETQUOTAROOT "INBOX"
2593 * QUOTAROOT INBOX user/rchijiiwa1
2594 * QUOTA user/rchijiiwa1 (STORAGE 654 9765)
2598 $quota_lines = array();
2599 $key = $this->nextTag();
2600 $command = $key . ' GETQUOTAROOT INBOX';
2602 // get line(s) containing quota info
2603 if ($this->putLine($command)) {
2605 $line = rtrim($this->readLine(5000));
2606 if (preg_match('/^\* QUOTA /', $line)) {
2607 $quota_lines[] = $line;
2609 } while (!$this->startsWith($line, $key, true, true));
2612 $this->setError(self::ERROR_COMMAND, "Unable to send command: $command");
2615 // return false if not found, parse if found
2616 $min_free = PHP_INT_MAX;
2617 foreach ($quota_lines as $key => $quota_line) {
2618 $quota_line = str_replace(array('(', ')'), '', $quota_line);
2619 $parts = explode(' ', $quota_line);
2620 $storage_part = array_search('STORAGE', $parts);
2622 if (!$storage_part) {
2626 $used = intval($parts[$storage_part+1]);
2627 $total = intval($parts[$storage_part+2]);
2628 $free = $total - $used;
2630 // return lowest available space from all quotas
2631 if ($free < $min_free) {
2633 $result['used'] = $used;
2634 $result['total'] = $total;
2635 $result['percent'] = min(100, round(($used/max(1,$total))*100));
2636 $result['free'] = 100 - $result['percent'];
2644 * Send the SETACL command (RFC4314)
2646 * @param string $mailbox Mailbox name
2647 * @param string $user User name
2648 * @param mixed $acl ACL string or array
2650 * @return boolean True on success, False on failure
2655 function setACL($mailbox, $user, $acl)
2657 if (is_array($acl)) {
2658 $acl = implode('', $acl);
2661 $result = $this->execute('SETACL', array(
2662 $this->escape($mailbox), $this->escape($user), strtolower($acl)),
2663 self::COMMAND_NORESPONSE);
2665 return ($result == self::ERROR_OK);
2669 * Send the DELETEACL command (RFC4314)
2671 * @param string $mailbox Mailbox name
2672 * @param string $user User name
2674 * @return boolean True on success, False on failure
2679 function deleteACL($mailbox, $user)
2681 $result = $this->execute('DELETEACL', array(
2682 $this->escape($mailbox), $this->escape($user)),
2683 self::COMMAND_NORESPONSE);
2685 return ($result == self::ERROR_OK);
2689 * Send the GETACL command (RFC4314)
2691 * @param string $mailbox Mailbox name
2693 * @return array User-rights array on success, NULL on error
2697 function getACL($mailbox)
2699 list($code, $response) = $this->execute('GETACL', array($this->escape($mailbox)));
2701 if ($code == self::ERROR_OK && preg_match('/^\* ACL /i', $response)) {
2702 // Parse server response (remove "* ACL ")
2703 $response = substr($response, 6);
2704 $ret = $this->tokenizeResponse($response);
2705 $mbox = array_shift($ret);
2706 $size = count($ret);
2708 // Create user-rights hash array
2709 // @TODO: consider implementing fixACL() method according to RFC4314.2.1.1
2710 // so we could return only standard rights defined in RFC4314,
2711 // excluding 'c' and 'd' defined in RFC2086.
2712 if ($size % 2 == 0) {
2713 for ($i=0; $i<$size; $i++) {
2714 $ret[$ret[$i]] = str_split($ret[++$i]);
2721 $this->setError(self::ERROR_COMMAND, "Incomplete ACL response");
2729 * Send the LISTRIGHTS command (RFC4314)
2731 * @param string $mailbox Mailbox name
2732 * @param string $user User name
2734 * @return array List of user rights
2738 function listRights($mailbox, $user)
2740 list($code, $response) = $this->execute('LISTRIGHTS', array(
2741 $this->escape($mailbox), $this->escape($user)));
2743 if ($code == self::ERROR_OK && preg_match('/^\* LISTRIGHTS /i', $response)) {
2744 // Parse server response (remove "* LISTRIGHTS ")
2745 $response = substr($response, 13);
2747 $ret_mbox = $this->tokenizeResponse($response, 1);
2748 $ret_user = $this->tokenizeResponse($response, 1);
2749 $granted = $this->tokenizeResponse($response, 1);
2750 $optional = trim($response);
2753 'granted' => str_split($granted),
2754 'optional' => explode(' ', $optional),
2762 * Send the MYRIGHTS command (RFC4314)
2764 * @param string $mailbox Mailbox name
2766 * @return array MYRIGHTS response on success, NULL on error
2770 function myRights($mailbox)
2772 list($code, $response) = $this->execute('MYRIGHTS', array($this->escape($mailbox)));
2774 if ($code == self::ERROR_OK && preg_match('/^\* MYRIGHTS /i', $response)) {
2775 // Parse server response (remove "* MYRIGHTS ")
2776 $response = substr($response, 11);
2778 $ret_mbox = $this->tokenizeResponse($response, 1);
2779 $rights = $this->tokenizeResponse($response, 1);
2781 return str_split($rights);
2788 * Send the SETMETADATA command (RFC5464)
2790 * @param string $mailbox Mailbox name
2791 * @param array $entries Entry-value array (use NULL value as NIL)
2793 * @return boolean True on success, False on failure
2797 function setMetadata($mailbox, $entries)
2799 if (!is_array($entries) || empty($entries)) {
2800 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
2804 foreach ($entries as $name => $value) {
2805 if ($value === null) {
2809 $value = sprintf("{%d}\r\n%s", strlen($value), $value);
2811 $entries[$name] = $this->escape($name) . ' ' . $value;
2814 $entries = implode(' ', $entries);
2815 $result = $this->execute('SETMETADATA', array(
2816 $this->escape($mailbox), '(' . $entries . ')'),
2817 self::COMMAND_NORESPONSE);
2819 return ($result == self::ERROR_OK);
2823 * Send the SETMETADATA command with NIL values (RFC5464)
2825 * @param string $mailbox Mailbox name
2826 * @param array $entries Entry names array
2828 * @return boolean True on success, False on failure
2833 function deleteMetadata($mailbox, $entries)
2835 if (!is_array($entries) && !empty($entries)) {
2836 $entries = explode(' ', $entries);
2839 if (empty($entries)) {
2840 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
2844 foreach ($entries as $entry) {
2845 $data[$entry] = NULL;
2848 return $this->setMetadata($mailbox, $data);
2852 * Send the GETMETADATA command (RFC5464)
2854 * @param string $mailbox Mailbox name
2855 * @param array $entries Entries
2856 * @param array $options Command options (with MAXSIZE and DEPTH keys)
2858 * @return array GETMETADATA result on success, NULL on error
2863 function getMetadata($mailbox, $entries, $options=array())
2865 if (!is_array($entries)) {
2866 $entries = array($entries);
2869 // create entries string
2870 foreach ($entries as $idx => $name) {
2871 $entries[$idx] = $this->escape($name);
2875 $entlist = '(' . implode(' ', $entries) . ')';
2877 // create options string
2878 if (is_array($options)) {
2879 $options = array_change_key_case($options, CASE_UPPER);
2882 if (!empty($options['MAXSIZE'])) {
2883 $opts[] = 'MAXSIZE '.intval($options['MAXSIZE']);
2885 if (!empty($options['DEPTH'])) {
2886 $opts[] = 'DEPTH '.intval($options['DEPTH']);
2890 $optlist = '(' . implode(' ', $opts) . ')';
2894 $optlist .= ($optlist ? ' ' : '') . $entlist;
2896 list($code, $response) = $this->execute('GETMETADATA', array(
2897 $this->escape($mailbox), $optlist));
2899 if ($code == self::ERROR_OK) {
2901 $data = $this->tokenizeResponse($response);
2903 // The METADATA response can contain multiple entries in a single
2904 // response or multiple responses for each entry or group of entries
2905 if (!empty($data) && ($size = count($data))) {
2906 for ($i=0; $i<$size; $i++) {
2907 if (isset($mbox) && is_array($data[$i])) {
2908 $size_sub = count($data[$i]);
2909 for ($x=0; $x<$size_sub; $x++) {
2910 $result[$mbox][$data[$i][$x]] = $data[$i][++$x];
2914 else if ($data[$i] == '*') {
2915 if ($data[$i+1] == 'METADATA') {
2916 $mbox = $data[$i+2];
2917 unset($data[$i]); // "*"
2918 unset($data[++$i]); // "METADATA"
2919 unset($data[++$i]); // Mailbox
2921 // get rid of other untagged responses
2927 else if (isset($mbox)) {
2928 $result[$mbox][$data[$i]] = $data[++$i];
2945 * Send the SETANNOTATION command (draft-daboo-imap-annotatemore)
2947 * @param string $mailbox Mailbox name
2948 * @param array $data Data array where each item is an array with
2949 * three elements: entry name, attribute name, value
2951 * @return boolean True on success, False on failure
2955 function setAnnotation($mailbox, $data)
2957 if (!is_array($data) || empty($data)) {
2958 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
2962 foreach ($data as $entry) {
2967 if ($value === null) {
2971 $value = sprintf("{%d}\r\n%s", strlen($value), $value);
2974 // ANNOTATEMORE drafts before version 08 require quoted parameters
2975 $entries[] = sprintf('%s (%s %s)',
2976 $this->escape($name, true), $this->escape($attr, true), $value);
2979 $entries = implode(' ', $entries);
2980 $result = $this->execute('SETANNOTATION', array(
2981 $this->escape($mailbox), $entries), self::COMMAND_NORESPONSE);
2983 return ($result == self::ERROR_OK);
2987 * Send the SETANNOTATION command with NIL values (draft-daboo-imap-annotatemore)
2989 * @param string $mailbox Mailbox name
2990 * @param array $data Data array where each item is an array with
2991 * two elements: entry name and attribute name
2993 * @return boolean True on success, False on failure
2998 function deleteAnnotation($mailbox, $data)
3000 if (!is_array($data) || empty($data)) {
3001 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
3005 return $this->setAnnotation($mailbox, $data);
3009 * Send the GETANNOTATION command (draft-daboo-imap-annotatemore)
3011 * @param string $mailbox Mailbox name
3012 * @param array $entries Entries names
3013 * @param array $attribs Attribs names
3015 * @return array Annotations result on success, NULL on error
3020 function getAnnotation($mailbox, $entries, $attribs)
3022 if (!is_array($entries)) {
3023 $entries = array($entries);
3025 // create entries string
3026 // ANNOTATEMORE drafts before version 08 require quoted parameters
3027 foreach ($entries as $idx => $name) {
3028 $entries[$idx] = $this->escape($name, true);
3030 $entries = '(' . implode(' ', $entries) . ')';
3032 if (!is_array($attribs)) {
3033 $attribs = array($attribs);
3035 // create entries string
3036 foreach ($attribs as $idx => $name) {
3037 $attribs[$idx] = $this->escape($name, true);
3039 $attribs = '(' . implode(' ', $attribs) . ')';
3041 list($code, $response) = $this->execute('GETANNOTATION', array(
3042 $this->escape($mailbox), $entries, $attribs));
3044 if ($code == self::ERROR_OK) {
3046 $data = $this->tokenizeResponse($response);
3048 // Here we returns only data compatible with METADATA result format
3049 if (!empty($data) && ($size = count($data))) {
3050 for ($i=0; $i<$size; $i++) {
3052 if (isset($mbox) && is_array($entry)) {
3054 $entry = $last_entry;
3056 else if ($entry == '*') {
3057 if ($data[$i+1] == 'ANNOTATION') {
3058 $mbox = $data[$i+2];
3059 unset($data[$i]); // "*"
3060 unset($data[++$i]); // "ANNOTATION"
3061 unset($data[++$i]); // Mailbox
3063 // get rid of other untagged responses
3070 else if (isset($mbox)) {
3071 $attribs = $data[++$i];
3078 if (!empty($attribs)) {
3079 for ($x=0, $len=count($attribs); $x<$len;) {
3080 $attr = $attribs[$x++];
3081 $value = $attribs[$x++];
3082 if ($attr == 'value.priv') {
3083 $result[$mbox]['/private' . $entry] = $value;
3085 else if ($attr == 'value.shared') {
3086 $result[$mbox]['/shared' . $entry] = $value;
3090 $last_entry = $entry;
3102 * Creates next command identifier (tag)
3104 * @return string Command identifier
3111 $this->cmd_tag = sprintf('A%04d', $this->cmd_num);
3113 return $this->cmd_tag;
3117 * Sends IMAP command and parses result
3119 * @param string $command IMAP command
3120 * @param array $arguments Command arguments
3121 * @param int $options Execution options
3123 * @return mixed Response code or list of response code and data
3127 function execute($command, $arguments=array(), $options=0)
3129 $tag = $this->nextTag();
3130 $query = $tag . ' ' . $command;
3131 $noresp = ($options & self::COMMAND_NORESPONSE);
3132 $response = $noresp ? null : '';
3134 if (!empty($arguments)) {
3135 $query .= ' ' . implode(' ', $arguments);
3139 if (!$this->putLineC($query)) {
3140 $this->setError(self::ERROR_COMMAND, "Unable to send command: $query");
3141 return $noresp ? self::ERROR_COMMAND : array(self::ERROR_COMMAND, '');
3146 $line = $this->readLine(4096);
3147 if ($response !== null) {
3150 } while (!$this->startsWith($line, $tag . ' ', true, true));
3152 $code = $this->parseResult($line, $command . ': ');
3154 // Remove last line from response
3156 $line_len = min(strlen($response), strlen($line) + 2);
3157 $response = substr($response, 0, -$line_len);
3160 // optional CAPABILITY response
3161 if (($options & self::COMMAND_CAPABILITY) && $code == self::ERROR_OK
3162 && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)
3164 $this->parseCapability($matches[1], true);
3167 // return last line only (without command tag, result and response code)
3168 if ($line && ($options & self::COMMAND_LASTLINE)) {
3169 $response = preg_replace("/^$tag (OK|NO|BAD|BYE|PREAUTH)?\s*(\[[a-z-]+\])?\s*/i", '', trim($line));
3172 return $noresp ? $code : array($code, $response);
3176 * Splits IMAP response into string tokens
3178 * @param string &$str The IMAP's server response
3179 * @param int $num Number of tokens to return
3181 * @return mixed Tokens array or string if $num=1
3185 static function tokenizeResponse(&$str, $num=0)
3189 while (!$num || count($result) < $num) {
3190 // remove spaces from the beginning of the string
3197 if (($epos = strpos($str, "}\r\n", 1)) == false) {
3200 if (!is_numeric(($bytes = substr($str, 1, $epos - 1)))) {
3203 $result[] = substr($str, $epos + 3, $bytes);
3204 // Advance the string
3205 $str = substr($str, $epos + 3 + $bytes);
3210 $len = strlen($str);
3212 for ($pos=1; $pos<$len; $pos++) {
3213 if ($str[$pos] == '"') {
3216 if ($str[$pos] == "\\") {
3217 if ($str[$pos + 1] == '"' || $str[$pos + 1] == "\\") {
3222 if ($str[$pos] != '"') {
3225 // we need to strip slashes for a quoted string
3226 $result[] = stripslashes(substr($str, 1, $pos - 1));
3227 $str = substr($str, $pos + 1);
3230 // Parenthesized list
3232 $str = substr($str, 1);
3233 $result[] = self::tokenizeResponse($str);
3236 $str = substr($str, 1);
3240 // String atom, number, NIL, *, %
3242 // empty or one character
3246 if (strlen($str) < 2) {
3252 // excluded chars: SP, CTL, (, ), {, ", ], %
3253 if (preg_match('/^([\x21\x23\x24\x26\x27\x2A-\x5C\x5E-\x7A\x7C-\x7E]+)/', $str, $m)) {
3254 $result[] = $m[1] == 'NIL' ? NULL : $m[1];
3255 $str = substr($str, strlen($m[1]));
3261 return $num == 1 ? $result[0] : $result;
3264 private function _xor($string, $string2)
3267 $size = strlen($string);
3269 for ($i=0; $i<$size; $i++) {
3270 $result .= chr(ord($string[$i]) ^ ord($string2[$i]));
3277 * Converts datetime string into unix timestamp
3279 * @param string $date Date string
3281 * @return int Unix timestamp
3283 private function strToTime($date)
3285 // support non-standard "GMTXXXX" literal
3286 $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date);
3288 // if date parsing fails, we have a date in non-rfc format.
3289 // remove token from the end and try again
3290 while (($ts = intval(@strtotime($date))) <= 0) {
3291 $d = explode(' ', $date);
3296 $date = implode(' ', $d);
3301 return $ts < 0 ? 0 : $ts;
3304 private function parseCapability($str, $trusted=false)
3306 $str = preg_replace('/^\* CAPABILITY /i', '', $str);
3308 $this->capability = explode(' ', strtoupper($str));
3310 if (!isset($this->prefs['literal+']) && in_array('LITERAL+', $this->capability)) {
3311 $this->prefs['literal+'] = true;
3315 $this->capability_readed = true;
3320 * Escapes a string when it contains special characters (RFC3501)
3322 * @param string $string IMAP string
3323 * @param boolean $force_quotes Forces string quoting
3325 * @return string Escaped string
3326 * @todo String literals, lists
3328 static function escape($string, $force_quotes=false)
3330 if ($string === null) {
3333 else if ($string === '') {
3336 // need quoted-string? find special chars: SP, CTL, (, ), {, %, *, ", \, ]
3337 // plus [ character as a workaround for DBMail's bug (#1487766)
3338 else if ($force_quotes ||
3339 preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5B\x5C\x5D\x7F]+)/', $string)
3341 return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"';
3348 static function unEscape($string)
3350 return strtr($string, array('\\"'=>'"', '\\\\' => '\\'));
3354 * Set the value of the debugging flag.
3356 * @param boolean $debug New value for the debugging flag.
3361 function setDebug($debug, $handler = null)
3363 $this->_debug = $debug;
3364 $this->_debug_handler = $handler;
3368 * Write the given debug text to the current debug output handler.
3370 * @param string $message Debug mesage text.
3375 private function debug($message)
3377 if ($this->_debug_handler) {
3378 call_user_func_array($this->_debug_handler, array(&$this, $message));
3380 echo "DEBUG: $message\n";