| | |
| | | // send result |
| | | $this->putLine($reply); |
| | | $line = $this->readLine(1024); |
| | | |
| | | |
| | | if ($line[0] == '+') { |
| | | $challenge = substr($line, 2); |
| | | } |
| | |
| | | if ($result == self::ERROR_OK) { |
| | | // optional CAPABILITY response |
| | | if ($line && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { |
| | | $this->parseCapability($matches[1]); |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | return $this->fp; |
| | | } |
| | |
| | | |
| | | // re-set capabilities list if untagged CAPABILITY response provided |
| | | if (preg_match('/\* CAPABILITY (.+)/i', $response, $matches)) { |
| | | $this->parseCapability($matches[1]); |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | |
| | | if ($code == self::ERROR_OK) { |
| | |
| | | if (array_key_exists('namespace', $this->prefs)) { |
| | | return $this->prefs['namespace']; |
| | | } |
| | | |
| | | |
| | | if (!$this->getCapability('NAMESPACE')) { |
| | | return self::ERROR_BAD; |
| | | } |
| | |
| | | } else { |
| | | $auth_method = 'CHECK'; |
| | | } |
| | | |
| | | $message = "INITIAL: $auth_method\n"; |
| | | |
| | | $result = false; |
| | | |
| | |
| | | // Connected to wrong port or connection error? |
| | | if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) { |
| | | if ($line) |
| | | $this->error = sprintf("Wrong startup greeting (%s:%d): %s", $host, $this->prefs['port'], $line); |
| | | $error = sprintf("Wrong startup greeting (%s:%d): %s", $host, $this->prefs['port'], $line); |
| | | else |
| | | $this->error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']); |
| | | $this->errornum = self::ERROR_BAD; |
| | | $error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']); |
| | | |
| | | $this->set_error(self::ERROR_BAD, $error); |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | |
| | | // RFC3501 [7.1] optional CAPABILITY response |
| | | if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { |
| | | $this->parseCapability($matches[1]); |
| | | $this->capability_readed = true; |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | |
| | | $this->message .= $line; |
| | | $this->message = $line; |
| | | |
| | | // TLS connection |
| | | if ($this->prefs['ssl_mode'] == 'tls' && $this->getCapability('STARTTLS')) { |
| | |
| | | $res = $this->execute('STARTTLS'); |
| | | |
| | | if ($res[0] != self::ERROR_OK) { |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | |
| | | if (!stream_socket_enable_crypto($this->fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { |
| | | $this->set_error(self::ERROR_BAD, "Unable to negotiate TLS"); |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | else { |
| | | // Prevent from sending credentials in plain text when connection is not secure |
| | | if ($auth_method == 'LOGIN' && $this->getCapability('LOGINDISABLED')) { |
| | | $this->set_error(self::ERROR_BAD, "Login disabled by IMAP server"); |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | // replace AUTH with CRAM-MD5 for backward compat. |
| | | $auth_methods[] = $auth_method == 'AUTH' ? 'CRAM-MD5' : $auth_method; |
| | | } |
| | | |
| | | // pre-login capabilities can be not complete |
| | | $this->capability_readed = false; |
| | | |
| | | // Authenticate |
| | | foreach ($auth_methods as $method) { |
| | |
| | | // Connected and authenticated |
| | | if (is_resource($result)) { |
| | | if ($this->prefs['force_caps']) { |
| | | // forget current capabilities |
| | | $this->clearCapability(); |
| | | } else { |
| | | // pre-login capabilities can be not complete |
| | | $this->capability_readed = false; |
| | | } |
| | | $this->getRootDir(); |
| | | $this->logged = true; |
| | |
| | | } |
| | | |
| | | // Close connection |
| | | @fclose($this->fp); |
| | | $this->fp = false; |
| | | $this->close(); |
| | | |
| | | return false; |
| | | return false; |
| | | } |
| | | |
| | | function connected() |
| | |
| | | |
| | | function close() |
| | | { |
| | | if ($this->logged && $this->putLine($this->next_tag() . ' LOGOUT')) { |
| | | if (!feof($this->fp)) |
| | | fgets($this->fp, 1024); |
| | | } |
| | | if ($this->putLine($this->next_tag() . ' LOGOUT')) { |
| | | $this->readReply(); |
| | | } |
| | | |
| | | @fclose($this->fp); |
| | | $this->fp = false; |
| | | } |
| | |
| | | * Executes STATUS comand |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param array $items Requested item names |
| | | * @param array $items Additional requested item names. By default |
| | | * MESSAGES and UNSEEN are requested. Other defined |
| | | * in RFC3501: UIDNEXT, UIDVALIDITY, RECENT |
| | | * |
| | | * @return array Status item-value hash |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function status($mailbox, $items) |
| | | function status($mailbox, $items=array()) |
| | | { |
| | | if (empty($mailbox) || empty($items)) { |
| | | if (empty($mailbox)) { |
| | | return false; |
| | | } |
| | | |
| | | if (!in_array('MESSAGES', $items)) { |
| | | $items[] = 'MESSAGES'; |
| | | } |
| | | if (!in_array('UNSEEN', $items)) { |
| | | $items[] = 'UNSEEN'; |
| | | } |
| | | |
| | | list($code, $response) = $this->execute('STATUS', array($this->escape($mailbox), |
| | | '(' . implode(' ', (array) $items) . ')')); |
| | |
| | | for ($i=0, $len=count($items); $i<$len; $i += 2) { |
| | | $result[$items[$i]] = (int) $items[$i+1]; |
| | | } |
| | | |
| | | $this->data['STATUS:'.$mailbox] = $result; |
| | | |
| | | return $result; |
| | | } |
| | |
| | | $this->selected = ''; |
| | | } |
| | | |
| | | $this->select($mailbox); |
| | | if ($this->selected == $mailbox) { |
| | | return $this->data['EXISTS']; |
| | | } |
| | | |
| | | // Check internal cache |
| | | $cache = $this->data['STATUS:'.$mailbox]; |
| | | if (!empty($cache) && isset($cache['MESSAGES'])) { |
| | | return (int) $cache['MESSAGES']; |
| | | } |
| | | |
| | | // Try STATUS (should be faster than SELECT) |
| | | $counts = $this->status($mailbox); |
| | | if (is_array($counts)) { |
| | | return (int) $counts['MESSAGES']; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | |
| | | */ |
| | | function countUnseen($mailbox) |
| | | { |
| | | // Try STATUS, should be faster |
| | | $counts = $this->status($mailbox, array('UNSEEN')); |
| | | // Check internal cache |
| | | $cache = $this->data['STATUS:'.$mailbox]; |
| | | if (!empty($cache) && isset($cache['UNSEEN'])) { |
| | | return (int) $cache['UNSEEN']; |
| | | } |
| | | |
| | | // Try STATUS (should be faster than SELECT+SEARCH) |
| | | $counts = $this->status($mailbox); |
| | | if (is_array($counts)) { |
| | | return (int) $counts['UNSEEN']; |
| | | } |
| | |
| | | } |
| | | |
| | | // message IDs |
| | | if (is_array($add)) |
| | | $add = $this->compressMessageSet(join(',', $add)); |
| | | if (!empty($add)) |
| | | $add = $this->compressMessageSet($add); |
| | | |
| | | list($code, $response) = $this->execute($is_uid ? 'UID SORT' : 'SORT', |
| | | array("($field)", $encoding, 'ALL' . (!empty($add) ? ' '.$add : ''))); |
| | |
| | | function fetchHeaderIndex($mailbox, $message_set, $index_field='', $skip_deleted=true, $uidfetch=false) |
| | | { |
| | | if (is_array($message_set)) { |
| | | if (!($message_set = $this->compressMessageSet(join(',', $message_set)))) |
| | | if (!($message_set = $this->compressMessageSet($message_set))) |
| | | return false; |
| | | } else { |
| | | list($from_idx, $to_idx) = explode(':', $message_set); |
| | |
| | | return $result; |
| | | } |
| | | |
| | | private function compressMessageSet($message_set, $force=false) |
| | | static function compressMessageSet($messages, $force=false) |
| | | { |
| | | // given a comma delimited list of independent mid's, |
| | | // compresses by grouping sequences together |
| | | |
| | | // if less than 255 bytes long, let's not bother |
| | | if (!$force && strlen($message_set)<255) { |
| | | return $message_set; |
| | | } |
| | | if (!is_array($messages)) { |
| | | // if less than 255 bytes long, let's not bother |
| | | if (!$force && strlen($messages)<255) { |
| | | return $messages; |
| | | } |
| | | |
| | | // see if it's already been compressed |
| | | if (strpos($message_set, ':') !== false) { |
| | | return $message_set; |
| | | } |
| | | // see if it's already been compressed |
| | | if (strpos($messages, ':') !== false) { |
| | | return $messages; |
| | | } |
| | | |
| | | // separate, then sort |
| | | $ids = explode(',', $message_set); |
| | | sort($ids); |
| | | // separate, then sort |
| | | $messages = explode(',', $messages); |
| | | } |
| | | |
| | | sort($messages); |
| | | |
| | | $result = array(); |
| | | $start = $prev = $ids[0]; |
| | | $start = $prev = $messages[0]; |
| | | |
| | | foreach ($ids as $id) { |
| | | foreach ($messages as $id) { |
| | | $incr = $id - $prev; |
| | | if ($incr > 1) { //found a gap |
| | | if ($start == $prev) { |
| | |
| | | } |
| | | |
| | | // handle the last sequence/id |
| | | if ($start==$prev) { |
| | | if ($start == $prev) { |
| | | $result[] = $prev; |
| | | } else { |
| | | $result[] = $start.':'.$prev; |
| | |
| | | return implode(',', $result); |
| | | } |
| | | |
| | | function UID2ID($folder, $uid) |
| | | static function uncompressMessageSet($messages) |
| | | { |
| | | if ($uid > 0) { |
| | | $id_a = $this->search($folder, "UID $uid"); |
| | | if (is_array($id_a) && count($id_a) == 1) { |
| | | return $id_a[0]; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | $result = array(); |
| | | $messages = explode(',', $messages); |
| | | |
| | | function ID2UID($folder, $id) |
| | | { |
| | | if (empty($id)) { |
| | | return -1; |
| | | } |
| | | foreach ($messages as $part) { |
| | | $items = explode(':', $part); |
| | | $max = max($items[0], $items[1]); |
| | | |
| | | if (!$this->select($folder)) { |
| | | return -1; |
| | | for ($x=$items[0]; $x<=$max; $x++) { |
| | | $result[] = $x; |
| | | } |
| | | } |
| | | |
| | | $result = -1; |
| | | return $result; |
| | | } |
| | | |
| | | /** |
| | | * Returns message sequence identifier |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param int $uid Message unique identifier (UID) |
| | | * |
| | | * @return int Message sequence identifier |
| | | * @access public |
| | | */ |
| | | function UID2ID($mailbox, $uid) |
| | | { |
| | | if ($uid > 0) { |
| | | $id_a = $this->search($mailbox, "UID $uid"); |
| | | if (is_array($id_a) && count($id_a) == 1) { |
| | | return (int) $id_a[0]; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * Returns message unique identifier (UID) |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param int $uid Message sequence identifier |
| | | * |
| | | * @return int Message unique identifier |
| | | * @access public |
| | | */ |
| | | function ID2UID($mailbox, $id) |
| | | { |
| | | if (empty($id) || $id < 0) { |
| | | return null; |
| | | } |
| | | |
| | | if (!$this->select($mailbox)) { |
| | | return null; |
| | | } |
| | | |
| | | list($code, $response) = $this->execute('FETCH', array($id, '(UID)')); |
| | | |
| | | if ($code == self::ERROR_OK && preg_match("/^\* $id FETCH \(UID (.*)\)/i", $response, $m)) { |
| | | $result = $m[1]; |
| | | return (int) $m[1]; |
| | | } |
| | | |
| | | return $result; |
| | | return null; |
| | | } |
| | | |
| | | function fetchUIDs($mailbox, $message_set=null) |
| | |
| | | return false; |
| | | } |
| | | |
| | | if (is_array($message_set)) |
| | | $message_set = join(',', $message_set); |
| | | |
| | | $message_set = $this->compressMessageSet($message_set); |
| | | |
| | | if ($add) |
| | |
| | | return false; |
| | | } |
| | | do { |
| | | $line = $this->readLine(1024); |
| | | $line = $this->readLine(4096); |
| | | $line = $this->multLine($line); |
| | | |
| | | if (!$line) |
| | |
| | | if ($field == 'date' || $field == 'internaldate') { |
| | | $field = 'timestamp'; |
| | | } |
| | | |
| | | if (empty($flag)) { |
| | | $flag = 'ASC'; |
| | | } else { |
| | | $flag = strtoupper($flag); |
| | | } |
| | | |
| | | $stripArr = ($field=='subject') ? array('Re: ','Fwd: ','Fw: ','"') : array('"'); |
| | | |
| | | $c = count($a); |
| | | if ($c > 0) { |
| | | |
| | | // Strategy: |
| | | // First, we'll create an "index" array. |
| | | // Then, we'll use sort() on that array, |
| | |
| | | } else { |
| | | $data = $val->$field; |
| | | if (is_string($data)) { |
| | | $data = strtoupper(str_replace($stripArr, '', $data)); |
| | | $data = str_replace('"', '', $data); |
| | | if ($field == 'subject') { |
| | | $data = preg_replace('/^(Re: \s*|Fwd:\s*|Fw:\s*)+/i', '', $data); |
| | | } |
| | | $data = strtoupper($data); |
| | | } |
| | | } |
| | | $index[$key]=$data; |
| | | $index[$key] = $data; |
| | | } |
| | | |
| | | // sort index |
| | | $i = 0; |
| | | if ($flag == 'ASC') { |
| | | asort($index); |
| | | } else { |
| | |
| | | $result = array(); |
| | | reset($index); |
| | | while (list($key, $val) = each($index)) { |
| | | $result[$key]=$a[$key]; |
| | | $i++; |
| | | $result[$key] = $a[$key]; |
| | | } |
| | | } |
| | | |
| | |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | | } |
| | | |
| | | // Clear internal status cache |
| | | unset($this->data['STATUS:'.$mailbox]); |
| | | |
| | | $result = $this->execute($messages ? 'UID EXPUNGE' : 'EXPUNGE', |
| | | array($messages), self::COMMAND_NORESPONSE); |
| | |
| | | function modFlag($mailbox, $messages, $flag, $mod) |
| | | { |
| | | if ($mod != '+' && $mod != '-') { |
| | | return -1; |
| | | $mod = '+'; |
| | | } |
| | | |
| | | $flag = $this->flags[strtoupper($flag)]; |
| | | |
| | | if (!$this->select($mailbox)) { |
| | | return -1; |
| | | return false; |
| | | } |
| | | |
| | | $c = 0; |
| | | $key = $this->next_tag(); |
| | | $command = "$key UID STORE $messages {$mod}FLAGS ($flag)"; |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return -1; |
| | | // Clear internal status cache |
| | | if ($flag == 'SEEN') { |
| | | unset($this->data['STATUS:'.$mailbox]['UNSEEN']); |
| | | } |
| | | |
| | | do { |
| | | $line = $this->readLine(); |
| | | if ($line[0] == '*') { |
| | | $c++; |
| | | } |
| | | } while (!$this->startsWith($line, $key, true, true)); |
| | | $flag = $this->flags[strtoupper($flag)]; |
| | | $result = $this->execute('UID STORE', array( |
| | | $this->compressMessageSet($messages), $mod . 'FLAGS.SILENT', "($flag)"), |
| | | self::COMMAND_NORESPONSE); |
| | | |
| | | if ($this->parseResult($line, 'STORE: ') == self::ERROR_OK) { |
| | | return $c; |
| | | } |
| | | |
| | | return -1; |
| | | return ($result == self::ERROR_OK); |
| | | } |
| | | |
| | | function flag($mailbox, $messages, $flag) { |
| | |
| | | function copy($messages, $from, $to) |
| | | { |
| | | if (empty($from) || empty($to)) { |
| | | return -1; |
| | | return false; |
| | | } |
| | | |
| | | if (!$this->select($from)) { |
| | | return -1; |
| | | return false; |
| | | } |
| | | |
| | | $result = $this->execute('UID COPY', array($messages, $this->escape($to)), |
| | | // Clear internal status cache |
| | | unset($this->data['STATUS:'.$to]); |
| | | |
| | | $result = $this->execute('UID COPY', array( |
| | | $this->compressMessageSet($messages), $this->escape($to)), |
| | | self::COMMAND_NORESPONSE); |
| | | |
| | | return $result; |
| | | return ($result == self::ERROR_OK); |
| | | } |
| | | |
| | | function move($messages, $from, $to) |
| | | { |
| | | if (!$from || !$to) { |
| | | return false; |
| | | } |
| | | |
| | | $r = $this->copy($messages, $from, $to); |
| | | |
| | | if ($r) { |
| | | // Clear internal status cache |
| | | unset($this->data['STATUS:'.$from]); |
| | | |
| | | return $this->delete($from, $messages); |
| | | } |
| | | return $r; |
| | | } |
| | | |
| | | // Don't be tempted to change $str to pass by reference to speed this up - it will slow it down by about |
| | |
| | | return $node; |
| | | } |
| | | |
| | | function thread($folder, $algorithm='REFERENCES', $criteria='', $encoding='US-ASCII') |
| | | function thread($mailbox, $algorithm='REFERENCES', $criteria='', $encoding='US-ASCII') |
| | | { |
| | | $old_sel = $this->selected; |
| | | |
| | | if (!$this->select($folder)) { |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | | } |
| | | |
| | | // return empty result when folder is empty and we're just after SELECT |
| | | if ($old_sel != $folder && !$this->data['EXISTS']) { |
| | | if ($old_sel != $mailbox && !$this->data['EXISTS']) { |
| | | return array(array(), array(), array()); |
| | | } |
| | | |
| | |
| | | if (in_array('MAX', $items)) |
| | | $result['MAX'] = !empty($response) ? max($response) : 0; |
| | | if (in_array('ALL', $items)) |
| | | $result['ALL'] = $this->compressMessageSet(implode(',', $response), true); |
| | | $result['ALL'] = $this->compressMessageSet($response, true); |
| | | |
| | | return $result; |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | function move($messages, $from, $to) |
| | | { |
| | | if (!$from || !$to) { |
| | | return -1; |
| | | } |
| | | |
| | | $r = $this->copy($messages, $from, $to); |
| | | |
| | | if ($r==0) { |
| | | return $this->delete($from, $messages); |
| | | } |
| | | return $r; |
| | | } |
| | | |
| | | /** |
| | | * Returns list of mailboxes |
| | | * |
| | | * @param string $ref Reference name |
| | | * @param string $mailbox Mailbox name |
| | | * @param array $status_opts (see self::_listMailboxes) |
| | | * @param array $select_opts (see self::_listMailboxes) |
| | | * |
| | | * @return array List of mailboxes or hash of options if $status_opts argument |
| | | * is non-empty. |
| | | * @access public |
| | | */ |
| | | function listMailboxes($ref, $mailbox, $status_opts=array()) |
| | | function listMailboxes($ref, $mailbox, $status_opts=array(), $select_opts=array()) |
| | | { |
| | | return $this->_listMailboxes($ref, $mailbox, false, $status_opts); |
| | | return $this->_listMailboxes($ref, $mailbox, false, $status_opts, $select_opts); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param string $mailbox Mailbox name |
| | | * @param array $status_opts (see self::_listMailboxes) |
| | | * |
| | | * @return array List of mailboxes or hash of options if $status_ops argument |
| | | * @return array List of mailboxes or hash of options if $status_opts argument |
| | | * is non-empty. |
| | | * @access public |
| | | */ |
| | | function listSubscribed($ref, $mailbox, $status_opts=array()) |
| | | { |
| | | return $this->_listMailboxes($ref, $mailbox, true, $status_opts); |
| | | return $this->_listMailboxes($ref, $mailbox, true, $status_opts, NULL); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param bool $subscribed Enables returning subscribed mailboxes only |
| | | * @param array $status_opts List of STATUS options (RFC5819: LIST-STATUS) |
| | | * Possible: MESSAGES, RECENT, UIDNEXT, UIDVALIDITY, UNSEEN |
| | | * @param array $select_opts List of selection options (RFC5258: LIST-EXTENDED) |
| | | * Possible: SUBSCRIBED, RECURSIVEMATCH, REMOTE |
| | | * |
| | | * @return array List of mailboxes or hash of options if $status_ops argument |
| | | * is non-empty. |
| | | * @access private |
| | | */ |
| | | private function _listMailboxes($ref, $mailbox, $subscribed=false, $status_opts=array()) |
| | | private function _listMailboxes($ref, $mailbox, $subscribed=false, |
| | | $status_opts=array(), $select_opts=array()) |
| | | { |
| | | if (empty($mailbox)) { |
| | | $mailbox = '*'; |
| | |
| | | $ref = $this->prefs['rootdir']; |
| | | } |
| | | |
| | | $args = array($this->escape($ref), $this->escape($mailbox)); |
| | | $args = array(); |
| | | |
| | | if (!empty($select_opts) && $this->getCapability('LIST-EXTENDED')) { |
| | | $select_opts = (array) $select_opts; |
| | | |
| | | $args[] = '(' . implode(' ', $select_opts) . ')'; |
| | | } |
| | | |
| | | $args[] = $this->escape($ref); |
| | | $args[] = $this->escape($mailbox); |
| | | |
| | | if (!empty($status_opts) && $this->getCapability('LIST-STATUS')) { |
| | | $status_opts = array($status_opts); |
| | | $status_opts = (array) $status_opts; |
| | | $lstatus = true; |
| | | |
| | | $args[] = 'RETURN (STATUS (' . implode(' ', $status_opts) . '))'; |
| | |
| | | $cmd = strtoupper($this->tokenizeResponse($response, 1)); |
| | | // * LIST (<options>) <delimiter> <mailbox> |
| | | if (!$lstatus || $cmd == 'LIST' || $cmd == 'LSUB') { |
| | | list($opts, $delim, $folder) = $this->tokenizeResponse($response, 3); |
| | | list($opts, $delim, $mailbox) = $this->tokenizeResponse($response, 3); |
| | | |
| | | // Add to result array |
| | | if (!$lstatus) { |
| | | $folders[] = $folder; |
| | | $folders[] = $mailbox; |
| | | } |
| | | else { |
| | | $folders[$folder] = array(); |
| | | $folders[$mailbox] = array(); |
| | | } |
| | | |
| | | if ($cmd == 'LIST') { |
| | | $this->data['LIST'][$folder] = $opts; |
| | | // Add to options array |
| | | if (!empty($opts)) { |
| | | if (empty($this->data['LIST'][$mailbox])) |
| | | $this->data['LIST'][$mailbox] = $opts; |
| | | else |
| | | $this->data['LIST'][$mailbox] = array_unique(array_merge( |
| | | $this->data['LIST'][$mailbox], $opts)); |
| | | } |
| | | } |
| | | // * STATUS <mailbox> (<result>) |
| | | else if ($cmd == 'STATUS') { |
| | | list($folder, $status) = $this->tokenizeResponse($response, 2); |
| | | list($mailbox, $status) = $this->tokenizeResponse($response, 2); |
| | | |
| | | for ($i=0, $len=count($status); $i<$len; $i += 2) { |
| | | list($name, $value) = $this->tokenizeResponse($status, 2); |
| | | $folders[$folder][$name] = $value; |
| | | $folders[$mailbox][$name] = $value; |
| | | } |
| | | } |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | function createFolder($folder) |
| | | function createFolder($mailbox) |
| | | { |
| | | $result = $this->execute('CREATE', array($this->escape($folder)), |
| | | $result = $this->execute('CREATE', array($this->escape($mailbox)), |
| | | self::COMMAND_NORESPONSE); |
| | | |
| | | return ($result == self::ERROR_OK); |
| | |
| | | return ($result == self::ERROR_OK); |
| | | } |
| | | |
| | | function deleteFolder($folder) |
| | | function deleteFolder($mailbox) |
| | | { |
| | | $result = $this->execute('DELETE', array($this->escape($folder)), |
| | | $result = $this->execute('DELETE', array($this->escape($mailbox)), |
| | | self::COMMAND_NORESPONSE); |
| | | |
| | | return ($result == self::ERROR_OK); |
| | | } |
| | | |
| | | function clearFolder($folder) |
| | | function clearFolder($mailbox) |
| | | { |
| | | $num_in_trash = $this->countMessages($folder); |
| | | $num_in_trash = $this->countMessages($mailbox); |
| | | if ($num_in_trash > 0) { |
| | | $this->delete($folder, '1:*'); |
| | | $this->delete($mailbox, '1:*'); |
| | | } |
| | | return ($this->expunge($folder) >= 0); |
| | | return ($this->expunge($mailbox) >= 0); |
| | | } |
| | | |
| | | function subscribe($folder) |
| | | function subscribe($mailbox) |
| | | { |
| | | $result = $this->execute('SUBSCRIBE', array($this->escape($folder)), |
| | | $result = $this->execute('SUBSCRIBE', array($this->escape($mailbox)), |
| | | self::COMMAND_NORESPONSE); |
| | | |
| | | return ($result == self::ERROR_OK); |
| | | } |
| | | |
| | | function unsubscribe($folder) |
| | | function unsubscribe($mailbox) |
| | | { |
| | | $result = $this->execute('UNSUBSCRIBE', array($this->escape($folder)), |
| | | $result = $this->execute('UNSUBSCRIBE', array($this->escape($mailbox)), |
| | | self::COMMAND_NORESPONSE); |
| | | |
| | | return ($result == self::ERROR_OK); |
| | | } |
| | | |
| | | function append($folder, &$message) |
| | | function append($mailbox, &$message) |
| | | { |
| | | if (!$folder) { |
| | | if (!$mailbox) { |
| | | return false; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | $key = $this->next_tag(); |
| | | $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($folder), |
| | | $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox), |
| | | $len, ($this->prefs['literal+'] ? '+' : '')); |
| | | |
| | | if ($this->putLine($request)) { |
| | |
| | | $line = $this->readLine(); |
| | | } while (!$this->startsWith($line, $key, true, true)); |
| | | |
| | | // Clear internal status cache |
| | | unset($this->data['STATUS:'.$mailbox]); |
| | | |
| | | return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK); |
| | | } |
| | | else { |
| | |
| | | return false; |
| | | } |
| | | |
| | | function appendFromFile($folder, $path, $headers=null) |
| | | function appendFromFile($mailbox, $path, $headers=null) |
| | | { |
| | | if (!$folder) { |
| | | if (!$mailbox) { |
| | | return false; |
| | | } |
| | | |
| | |
| | | |
| | | // send APPEND command |
| | | $key = $this->next_tag(); |
| | | $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($folder), |
| | | $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox), |
| | | $len, ($this->prefs['literal+'] ? '+' : '')); |
| | | |
| | | if ($this->putLine($request)) { |
| | |
| | | $line = $this->readLine(); |
| | | } while (!$this->startsWith($line, $key, true, true)); |
| | | |
| | | // Clear internal status cache |
| | | unset($this->data['STATUS:'.$mailbox]); |
| | | |
| | | return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK); |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | function fetchStructureString($folder, $id, $is_uid=false) |
| | | function fetchStructureString($mailbox, $id, $is_uid=false) |
| | | { |
| | | if (!$this->select($folder)) { |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | | } |
| | | |
| | |
| | | if (($options & self::COMMAND_CAPABILITY) && $code == self::ERROR_OK |
| | | && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches) |
| | | ) { |
| | | $this->parseCapability($matches[1]); |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | |
| | | return $noresp ? $code : array($code, $response); |
| | |
| | | return $string; |
| | | } |
| | | |
| | | private function parseCapability($str) |
| | | private function parseCapability($str, $trusted=false) |
| | | { |
| | | $str = preg_replace('/^\* CAPABILITY /i', '', $str); |
| | | |
| | |
| | | if (!isset($this->prefs['literal+']) && in_array('LITERAL+', $this->capability)) { |
| | | $this->prefs['literal+'] = true; |
| | | } |
| | | |
| | | if ($trusted) { |
| | | $this->capability_readed = true; |
| | | } |
| | | } |
| | | |
| | | /** |