Aleksander Machniak
2013-03-15 ea98ec0939532d6539689524414b9eeb1c6cd0fc
program/lib/Roundcube/rcube_imap_generic.php
@@ -2,8 +2,6 @@
/**
 +-----------------------------------------------------------------------+
 | program/include/rcube_imap_generic.php                                |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
 | Copyright (C) 2011-2012, Kolab Systems AG                             |
@@ -19,13 +17,11 @@
 |   functionality built-in.                                             |
 |                                                                       |
 |   Based on Iloha IMAP Library. See http://ilohamail.org/ for details  |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 | Author: Ryo Chijiiwa <Ryo@IlohaMail.org>                              |
 +-----------------------------------------------------------------------+
*/
/**
 * PHP based wrapper class to connect to an IMAP server
@@ -757,12 +753,16 @@
        $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']);
        if (!$this->fp) {
            if (!$errstr) {
                $errstr = "Unknown reason (fsockopen() function disabled?)";
            }
            $this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s", $host, $this->prefs['port'], $errstr));
            return false;
        }
        if ($this->prefs['timeout'] > 0)
        if ($this->prefs['timeout'] > 0) {
            stream_set_timeout($this->fp, $this->prefs['timeout']);
        }
        $line = trim(fgets($this->fp, 8192));
@@ -906,7 +906,7 @@
     */
    function closeConnection()
    {
        if ($this->putLine($this->nextTag() . ' LOGOUT')) {
        if ($this->logged && $this->putLine($this->nextTag() . ' LOGOUT')) {
            $this->readReply();
        }
@@ -1065,8 +1065,8 @@
    /**
     * Executes EXPUNGE command
     *
     * @param string $mailbox  Mailbox name
     * @param string $messages Message UIDs to expunge
     * @param string       $mailbox  Mailbox name
     * @param string|array $messages Message UIDs to expunge
     *
     * @return boolean True on success, False on error
     */
@@ -1084,10 +1084,13 @@
        // Clear internal status cache
        unset($this->data['STATUS:'.$mailbox]);
        if ($messages)
            $result = $this->execute('UID EXPUNGE', array($messages), self::COMMAND_NORESPONSE);
        else
        if (!empty($messages) && $messages != '*' && $this->hasCapability('UIDPLUS')) {
            $messages = self::compressMessageSet($messages);
            $result   = $this->execute('UID EXPUNGE', array($messages), self::COMMAND_NORESPONSE);
        }
        else {
            $result = $this->execute('EXPUNGE', null, self::COMMAND_NORESPONSE);
        }
        if ($result == self::ERROR_OK) {
            $this->selected = null; // state has changed, need to reselect
@@ -1310,6 +1313,11 @@
                // * LIST (<options>) <delimiter> <mailbox>
                if ($cmd == 'LIST' || $cmd == 'LSUB') {
                    list($opts, $delim, $mailbox) = $this->tokenizeResponse($line, 3);
                    // Remove redundant separator at the end of folder name, UW-IMAP bug? (#1488879)
                    if ($delim) {
                        $mailbox = rtrim($mailbox, $delim);
                    }
                    // Add to result array
                    if (!$lstatus) {
@@ -1975,7 +1983,6 @@
    /**
     * Moves message(s) from one folder to another.
     * Original message(s) will be marked as deleted.
     *
     * @param string|array  $messages  Message UID(s)
     * @param string        $from      Mailbox name
@@ -1994,15 +2001,41 @@
            return false;
        }
        $r = $this->copy($messages, $from, $to);
        // use MOVE command (RFC 6851)
        if ($this->hasCapability('MOVE')) {
            // Clear last COPYUID data
            unset($this->data['COPYUID']);
        if ($r) {
            // Clear internal status cache
            unset($this->data['STATUS:'.$to]);
            unset($this->data['STATUS:'.$from]);
            $result = $this->execute('UID MOVE', array(
                $this->compressMessageSet($messages), $this->escape($to)),
                self::COMMAND_NORESPONSE);
            return ($result == self::ERROR_OK);
        }
        // use COPY + STORE +FLAGS.SILENT \Deleted + EXPUNGE
        $result = $this->copy($messages, $from, $to);
        if ($result) {
            // Clear internal status cache
            unset($this->data['STATUS:'.$from]);
            return $this->flag($from, $messages, 'DELETED');
            $result = $this->flag($from, $messages, 'DELETED');
            if ($messages == '*') {
                // CLOSE+SELECT should be faster than EXPUNGE
                $this->close();
            }
            else {
                $this->expunge($from, $messages);
            }
        }
        return $r;
        return $result;
    }
    /**
@@ -2548,10 +2581,11 @@
     * @param string $message Message content
     * @param array  $flags   Message flags
     * @param string $date    Message internal date
     * @param bool   $binary  Enable BINARY append (RFC3516)
     *
     * @return string|bool On success APPENDUID response (if available) or True, False on failure
     */
    function append($mailbox, &$message, $flags = array(), $date = null)
    function append($mailbox, &$message, $flags = array(), $date = null, $binary = false)
    {
        unset($this->data['APPENDUID']);
@@ -2559,8 +2593,13 @@
            return false;
        }
        $message = str_replace("\r", '', $message);
        $message = str_replace("\n", "\r\n", $message);
        $binary       = $binary && $this->getCapability('BINARY');
        $literal_plus = !$binary && $this->prefs['literal+'];
        if (!$binary) {
            $message = str_replace("\r", '', $message);
            $message = str_replace("\n", "\r\n", $message);
        }
        $len = strlen($message);
        if (!$len) {
@@ -2573,12 +2612,12 @@
        if (!empty($date)) {
            $request .= ' ' . $this->escape($date);
        }
        $request .= ' {' . $len . ($this->prefs['literal+'] ? '+' : '') . '}';
        $request .= ' ' . ($binary ? '~' : '') . '{' . $len . ($literal_plus ? '+' : '') . '}';
        // send APPEND command
        if ($this->putLine($request)) {
            // Do not wait when LITERAL+ is supported
            if (!$this->prefs['literal+']) {
            if (!$literal_plus) {
                $line = $this->readReply();
                if ($line[0] != '+') {
@@ -2620,10 +2659,11 @@
     * @param string $headers Message headers
     * @param array  $flags   Message flags
     * @param string $date    Message internal date
     * @param bool   $binary  Enable BINARY append (RFC3516)
     *
     * @return string|bool On success APPENDUID response (if available) or True, False on failure
     */
    function appendFromFile($mailbox, $path, $headers=null, $flags = array(), $date = null)
    function appendFromFile($mailbox, $path, $headers=null, $flags = array(), $date = null, $binary = false)
    {
        unset($this->data['APPENDUID']);
@@ -2654,18 +2694,21 @@
            $len += strlen($headers) + strlen($body_separator);
        }
        $binary       = $binary && $this->getCapability('BINARY');
        $literal_plus = !$binary && $this->prefs['literal+'];
        // build APPEND command
        $key = $this->nextTag();
        $request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')';
        if (!empty($date)) {
            $request .= ' ' . $this->escape($date);
        }
        $request .= ' {' . $len . ($this->prefs['literal+'] ? '+' : '') . '}';
        $request .= ' ' . ($binary ? '~' : '') . '{' . $len . ($literal_plus ? '+' : '') . '}';
        // send APPEND command
        if ($this->putLine($request)) {
            // Don't wait when LITERAL+ is supported
            if (!$this->prefs['literal+']) {
            if (!$literal_plus) {
                $line = $this->readReply();
                if ($line[0] != '+') {
@@ -3487,7 +3530,7 @@
            // 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($messages, ':') !== false) {