Aleksander Machniak
2012-10-24 1e7aa75d18e34b0733d49a3af689be0f3d73b83c
program/include/rcube_imap_generic.php
@@ -530,6 +530,7 @@
                }
                else {
                    $authc = $user;
                    $user  = '';
                }
                $auth_sasl = Auth_SASL::factory('digestmd5');
                $reply = base64_encode($auth_sasl->getResponse($authc, $pass,
@@ -568,6 +569,7 @@
            }
            else {
                $authc = $user;
                $user  = '';
            }
            $reply = base64_encode($user . chr(0) . $authc . chr(0) . $pass);
@@ -2178,7 +2180,7 @@
                            $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];
@@ -2400,15 +2402,22 @@
            $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
@@ -2455,13 +2464,13 @@
            $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);
@@ -2531,10 +2540,12 @@
     *
     * @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']);
@@ -2550,12 +2561,17 @@
            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();
@@ -2596,10 +2612,12 @@
     * @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']);
@@ -2630,11 +2648,15 @@
            $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+']) {
@@ -3537,6 +3559,24 @@
    }
    /**
     * 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