Aleksander Machniak
2012-08-08 2bbc3da52aee81e920e46778d68278bd31f7bb6b
program/include/rcube_imap_generic.php
@@ -5,8 +5,8 @@
 | program/include/rcube_imap_generic.php                                |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2010, The Roundcube Dev Team                       |
 | Copyright (C) 2011, Kolab Systems AG                                  |
 | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
 | Copyright (C) 2011-2012, Kolab Systems AG                             |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
@@ -24,47 +24,8 @@
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 | Author: Ryo Chijiiwa <Ryo@IlohaMail.org>                              |
 +-----------------------------------------------------------------------+
 $Id$
*/
/**
 * Struct representing an e-mail message header
 *
 * @package Mail
 * @author  Aleksander Machniak <alec@alec.pl>
 */
class rcube_mail_header
{
    public $id;
    public $uid;
    public $subject;
    public $from;
    public $to;
    public $cc;
    public $replyto;
    public $in_reply_to;
    public $date;
    public $messageID;
    public $size;
    public $encoding;
    public $charset;
    public $ctype;
    public $timestamp;
    public $bodystructure;
    public $internaldate;
    public $references;
    public $priority;
    public $mdn_to;
    public $others = array();
    public $flags = array();
}
// For backward compatibility with cached messages (#1486602)
class iilBasicHeader extends rcube_mail_header
{
}
/**
 * PHP based wrapper class to connect to an IMAP server
@@ -352,8 +313,12 @@
                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;
@@ -1352,12 +1317,16 @@
                        $folders[$mailbox] = array();
                    }
                    // Add to options array
                    if (empty($this->data['LIST'][$mailbox]))
                        $this->data['LIST'][$mailbox] = $opts;
                    else if (!empty($opts))
                        $this->data['LIST'][$mailbox] = array_unique(array_merge(
                            $this->data['LIST'][$mailbox], $opts));
                    // store LSUB options only if not empty, this way
                    // we can detect a situation when LIST doesn't return specified folder
                    if (!empty($opts) || $cmd == 'LIST') {
                        // Add to options array
                        if (empty($this->data['LIST'][$mailbox]))
                            $this->data['LIST'][$mailbox] = $opts;
                        else if (!empty($opts))
                            $this->data['LIST'][$mailbox] = array_unique(array_merge(
                                $this->data['LIST'][$mailbox], $opts));
                    }
                }
                // * STATUS <mailbox> (<result>)
                else if ($cmd == 'STATUS') {
@@ -1453,7 +1422,7 @@
        // Invoke SEARCH as a fallback
        $index = $this->search($mailbox, 'ALL UNSEEN', false, array('COUNT'));
        if (!$index->isError()) {
        if (!$index->is_error()) {
            return $index->count();
        }
@@ -1507,14 +1476,31 @@
     */
    function enable($extension)
    {
        if (empty($extension))
        if (empty($extension)) {
            return false;
        }
        if (!$this->hasCapability('ENABLE'))
        if (!$this->hasCapability('ENABLE')) {
            return false;
        }
        if (!is_array($extension))
        if (!is_array($extension)) {
            $extension = array($extension);
        }
        if (!empty($this->extensions_enabled)) {
            // check if all extensions are already enabled
            $diff = array_diff($extension, $this->extensions_enabled);
            if (empty($diff)) {
                return $extension;
            }
            // Make sure the mailbox isn't selected, before enabling extension(s)
            if ($this->selected !== null) {
                $this->close();
            }
        }
        list($code, $response) = $this->execute('ENABLE', $extension);
@@ -1522,7 +1508,9 @@
            $response = substr($response, 10); // remove prefix "* ENABLED "
            $result   = (array) $this->tokenizeResponse($response);
            return $result;
            $this->extensions_enabled = array_unique(array_merge((array)$this->extensions_enabled, $result));
            return $this->extensions_enabled;
        }
        return false;
@@ -1541,8 +1529,6 @@
     */
    function sort($mailbox, $field, $add='', $return_uid=false, $encoding = 'US-ASCII')
    {
        require_once dirname(__FILE__) . '/rcube_result_index.php';
        $field = strtoupper($field);
        if ($field == 'INTERNALDATE') {
            $field = 'ARRIVAL';
@@ -1557,6 +1543,11 @@
        if (!$this->select($mailbox)) {
            return new rcube_result_index($mailbox);
        }
        // RFC 5957: SORT=DISPLAY
        if (($field == 'FROM' || $field == 'TO') && $this->getCapability('SORT=DISPLAY')) {
            $field = 'DISPLAY' . $field;
        }
        // message IDs
@@ -1586,8 +1577,6 @@
     */
    function thread($mailbox, $algorithm='REFERENCES', $criteria='', $return_uid=false, $encoding='US-ASCII')
    {
        require_once dirname(__FILE__) . '/rcube_result_thread.php';
        $old_sel = $this->selected;
        if (!$this->select($mailbox)) {
@@ -1626,8 +1615,6 @@
     */
    function search($mailbox, $criteria, $return_uid=false, $items=array())
    {
        require_once dirname(__FILE__) . '/rcube_result_index.php';
        $old_sel = $this->selected;
        if (!$this->select($mailbox)) {
@@ -1641,7 +1628,7 @@
        // If ESEARCH is supported always use ALL
        // but not when items are specified or using simple id2uid search
        if (empty($items) && ((int) $criteria != $criteria)) {
        if (empty($items) && preg_match('/[^0-9]/', $criteria)) {
            $items = array('ALL');
        }
@@ -1687,8 +1674,6 @@
    function index($mailbox, $message_set, $index_field='', $skip_deleted=true,
        $uidfetch=false, $return_uid=false)
    {
        require_once dirname(__FILE__) . '/rcube_result_index.php';
        $msg_index = $this->fetchHeaderIndex($mailbox, $message_set,
            $index_field, $skip_deleted, $uidfetch, $return_uid);
@@ -1969,6 +1954,9 @@
     */
    function copy($messages, $from, $to)
    {
        // Clear last COPYUID data
        unset($this->data['COPYUID']);
        if (!$this->select($from)) {
            return false;
        }
@@ -2025,7 +2013,7 @@
     * @param string $mod_seq     Modification sequence for CHANGEDSINCE (RFC4551) query
     * @param bool   $vanished    Enables VANISHED parameter (RFC5162) for CHANGEDSINCE query
     *
     * @return array List of rcube_mail_header elements, False on error
     * @return array List of rcube_message_header elements, False on error
     * @since 0.6
     */
    function fetch($mailbox, $message_set, $is_uid = false, $query_items = array(),
@@ -2065,14 +2053,15 @@
            if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
                $id = intval($m[1]);
                $result[$id]            = new rcube_mail_header;
                $result[$id]            = new rcube_message_header;
                $result[$id]->id        = $id;
                $result[$id]->subject   = '';
                $result[$id]->messageID = 'mid:' . $id;
                $lines = array();
                $line  = substr($line, strlen($m[0]) + 2);
                $ln    = 0;
                $headers = null;
                $lines   = array();
                $line    = substr($line, strlen($m[0]) + 2);
                $ln      = 0;
                // get complete entry
                while (preg_match('/\{([0-9]+)\}\r\n$/', $line, $m)) {
@@ -3213,10 +3202,10 @@
     */
    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;
        }
@@ -3249,13 +3238,13 @@
    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] : '';
@@ -3263,20 +3252,17 @@
            $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;
        }
    }
    /**
@@ -3607,13 +3593,16 @@
        if ($string === null) {
            return 'NIL';
        }
        if ($string === '') {
            return '""';
        }
        // atom-string (only safe characters)
        if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) {
        if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x25\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) {
            return $string;
        }
        // quoted-string
        if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) {
            return '"' . addcslashes($string, '\\"') . '"';