| | |
| | | else { |
| | | $this->resultcode = null; |
| | | // parse response for [APPENDUID 1204196876 3456] |
| | | if (preg_match("/^\[APPENDUID [0-9]+ ([0-9,:*]+)\]/i", $str, $m)) { |
| | | if (preg_match("/^\[APPENDUID [0-9]+ ([0-9]+)\]/i", $str, $m)) { |
| | | $this->data['APPENDUID'] = $m[1]; |
| | | } |
| | | // parse response for [COPYUID 1204196876 3456:3457 123:124] |
| | | else if (preg_match("/^\[COPYUID [0-9]+ ([0-9,:]+) ([0-9,:]+)\]/i", $str, $m)) { |
| | | $this->data['COPYUID'] = array($m[1], $m[2]); |
| | | } |
| | | } |
| | | $this->result = $str; |
| | |
| | | } |
| | | else { |
| | | $authc = $user; |
| | | $user = ''; |
| | | } |
| | | $auth_sasl = Auth_SASL::factory('digestmd5'); |
| | | $reply = base64_encode($auth_sasl->getResponse($authc, $pass, |
| | |
| | | } |
| | | else { |
| | | $authc = $user; |
| | | $user = ''; |
| | | } |
| | | |
| | | $reply = base64_encode($user . chr(0) . $authc . chr(0) . $pass); |
| | |
| | | */ |
| | | function copy($messages, $from, $to) |
| | | { |
| | | // Clear last COPYUID data |
| | | unset($this->data['COPYUID']); |
| | | |
| | | if (!$this->select($from)) { |
| | | return false; |
| | | } |
| | |
| | | $result[$id]->encoding = $string; |
| | | break; |
| | | case 'content-type': |
| | | $ctype_parts = preg_split('/[; ]/', $string); |
| | | $ctype_parts = preg_split('/[; ]+/', $string); |
| | | $result[$id]->ctype = strtolower(array_shift($ctype_parts)); |
| | | if (preg_match('/charset\s*=\s*"?([a-z0-9\-\.\_]+)"?/i', $string, $regs)) { |
| | | $result[$id]->charset = $regs[1]; |
| | |
| | | return $this->handlePartBody($mailbox, $id, $is_uid, $part); |
| | | } |
| | | |
| | | function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL) |
| | | function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL, $formatted=true) |
| | | { |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | |
| | | $mode = 0; |
| | | } |
| | | |
| | | // Use BINARY extension when possible (and safe) |
| | | $binary = $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY'); |
| | | $fetch_mode = $binary ? 'BINARY' : 'BODY'; |
| | | |
| | | // format request |
| | | $reply_key = '* ' . $id; |
| | | $key = $this->nextTag(); |
| | | $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])"; |
| | | $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id ($fetch_mode.PEEK[$part])"; |
| | | |
| | | // send request |
| | | if (!$this->putLine($request)) { |
| | | $this->setError(self::ERROR_COMMAND, "Unable to send command: $request"); |
| | | return false; |
| | | } |
| | | |
| | | if ($binary) { |
| | | $mode = -1; |
| | | } |
| | | |
| | | // receive reply line |
| | |
| | | $prev = ''; |
| | | |
| | | while ($bytes > 0) { |
| | | $line = $this->readLine(4096); |
| | | $line = $this->readLine(8192); |
| | | |
| | | if ($line === NULL) { |
| | | break; |
| | | } |
| | | |
| | | $len = strlen($line); |
| | | $len = strlen($line); |
| | | |
| | | if ($len > $bytes) { |
| | | $line = substr($line, 0, $bytes); |
| | |
| | | continue; |
| | | $line = convert_uudecode($line); |
| | | // default |
| | | } else { |
| | | } else if ($formatted) { |
| | | $line = rtrim($line, "\t\r\n\0\x0B") . "\n"; |
| | | } |
| | | |
| | |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $message Message content |
| | | * @param array $flags Message flags |
| | | * @param string $date Message internal date |
| | | * |
| | | * @return string|bool On success APPENDUID response (if available) or True, False on failure |
| | | */ |
| | | function append($mailbox, &$message) |
| | | function append($mailbox, &$message, $flags = array(), $date = null) |
| | | { |
| | | unset($this->data['APPENDUID']); |
| | | |
| | | if (!$mailbox) { |
| | | if ($mailbox === null || $mailbox === '') { |
| | | return false; |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | // build APPEND command |
| | | $key = $this->nextTag(); |
| | | $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox), |
| | | $len, ($this->prefs['literal+'] ? '+' : '')); |
| | | $request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')'; |
| | | if (!empty($date)) { |
| | | $request .= ' ' . $this->escape($date); |
| | | } |
| | | $request .= ' {' . $len . ($this->prefs['literal+'] ? '+' : '') . '}'; |
| | | |
| | | // send APPEND command |
| | | if ($this->putLine($request)) { |
| | | // Don't wait when LITERAL+ is supported |
| | | // Do not wait when LITERAL+ is supported |
| | | if (!$this->prefs['literal+']) { |
| | | $line = $this->readReply(); |
| | | |
| | |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $path Path to the file with message body |
| | | * @param string $headers Message headers |
| | | * @param array $flags Message flags |
| | | * @param string $date Message internal date |
| | | * |
| | | * @return string|bool On success APPENDUID response (if available) or True, False on failure |
| | | */ |
| | | function appendFromFile($mailbox, $path, $headers=null) |
| | | function appendFromFile($mailbox, $path, $headers=null, $flags = array(), $date = null) |
| | | { |
| | | unset($this->data['APPENDUID']); |
| | | |
| | | if (!$mailbox) { |
| | | if ($mailbox === null || $mailbox === '') { |
| | | return false; |
| | | } |
| | | |
| | |
| | | if (file_exists(realpath($path))) { |
| | | $in_fp = fopen($path, 'r'); |
| | | } |
| | | |
| | | if (!$in_fp) { |
| | | $this->setError(self::ERROR_UNKNOWN, "Couldn't open $path for reading"); |
| | | return false; |
| | |
| | | $len += strlen($headers) + strlen($body_separator); |
| | | } |
| | | |
| | | // send APPEND command |
| | | // build APPEND command |
| | | $key = $this->nextTag(); |
| | | $request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox), |
| | | $len, ($this->prefs['literal+'] ? '+' : '')); |
| | | $request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')'; |
| | | if (!empty($date)) { |
| | | $request .= ' ' . $this->escape($date); |
| | | } |
| | | $request .= ' {' . $len . ($this->prefs['literal+'] ? '+' : '') . '}'; |
| | | |
| | | // send APPEND command |
| | | if ($this->putLine($request)) { |
| | | // Don't wait when LITERAL+ is supported |
| | | if (!$this->prefs['literal+']) { |
| | |
| | | */ |
| | | static function getStructurePartData($structure, $part) |
| | | { |
| | | $part_a = self::getStructurePartArray($structure, $part); |
| | | $data = array(); |
| | | $part_a = self::getStructurePartArray($structure, $part); |
| | | $data = array(); |
| | | |
| | | if (empty($part_a)) { |
| | | if (empty($part_a)) { |
| | | return $data; |
| | | } |
| | | |
| | |
| | | |
| | | static function getStructurePartArray($a, $part) |
| | | { |
| | | if (!is_array($a)) { |
| | | if (!is_array($a)) { |
| | | return false; |
| | | } |
| | | |
| | | if (empty($part)) { |
| | | return $a; |
| | | } |
| | | return $a; |
| | | } |
| | | |
| | | $ctype = is_string($a[0]) && is_string($a[1]) ? $a[0] . '/' . $a[1] : ''; |
| | | |
| | |
| | | $a = $a[8]; |
| | | } |
| | | |
| | | if (strpos($part, '.') > 0) { |
| | | $orig_part = $part; |
| | | $pos = strpos($part, '.'); |
| | | $rest = substr($orig_part, $pos+1); |
| | | $part = substr($orig_part, 0, $pos); |
| | | if (strpos($part, '.') > 0) { |
| | | $orig_part = $part; |
| | | $pos = strpos($part, '.'); |
| | | $rest = substr($orig_part, $pos+1); |
| | | $part = substr($orig_part, 0, $pos); |
| | | |
| | | return self::getStructurePartArray($a[$part-1], $rest); |
| | | } |
| | | return self::getStructurePartArray($a[$part-1], $rest); |
| | | } |
| | | else if ($part > 0) { |
| | | if (is_array($a[$part-1])) |
| | | return $a[$part-1]; |
| | | else |
| | | return $a; |
| | | } |
| | | return (is_array($a[$part-1])) ? $a[$part-1] : $a; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * Converts flags array into string for inclusion in IMAP command |
| | | * |
| | | * @param array $flags Flags (see self::flags) |
| | | * |
| | | * @return string Space-separated list of flags |
| | | */ |
| | | private function flagsToStr($flags) |
| | | { |
| | | foreach ((array)$flags as $idx => $flag) { |
| | | if ($flag = $this->flags[strtoupper($flag)]) { |
| | | $flags[$idx] = $flag; |
| | | } |
| | | } |
| | | |
| | | return implode(' ', (array)$flags); |
| | | } |
| | | |
| | | /** |
| | | * Converts datetime string into unix timestamp |
| | | * |
| | | * @param string $date Date string |