alecpl
2010-12-27 814bafcc5a94d69bd0c353ad9939f0e8bc71e8d5
program/include/rcube_imap_generic.php
@@ -109,6 +109,8 @@
    private $prefs;
    private $cmd_tag;
    private $cmd_num = 0;
    private $_debug = false;
    private $_debug_handler = false;
    const ERROR_OK = 0;
    const ERROR_NO = -1;
@@ -142,8 +144,8 @@
        if (!$this->fp)
            return false;
        if (!empty($this->prefs['debug_mode'])) {
            write_log('imap', 'C: '. rtrim($string));
        if ($this->_debug) {
            $this->debug('C: '. rtrim($string));
        }
        $res = fwrite($this->fp, $string . ($endln ? "\r\n" : ''));
@@ -231,8 +233,8 @@
                $this->fp = null;
                break;
            }
            if (!empty($this->prefs['debug_mode'])) {
                write_log('imap', 'S: '. rtrim($buffer));
            if ($this->_debug) {
                $this->debug('S: '. rtrim($buffer));
            }
            $line .= $buffer;
        } while ($buffer[strlen($buffer)-1] != "\n");
@@ -268,8 +270,8 @@
        while ($len < $bytes && !feof($this->fp))
        {
            $d = fread($this->fp, $bytes-$len);
            if (!empty($this->prefs['debug_mode'])) {
                write_log('imap', 'S: '. $d);
            if ($this->_debug) {
                $this->debug('S: '. $d);
            }
            $data .= $d;
            $data_len = strlen($data);
@@ -686,8 +688,8 @@
        $line = trim(fgets($this->fp, 8192));
        if ($this->prefs['debug_mode'] && $line) {
            write_log('imap', 'S: '. $line);
        if ($this->_debug && $line) {
            $this->debug('S: '. $line);
        }
        // Connected to wrong port or connection error?
@@ -2804,37 +2806,46 @@
        list($code, $response) = $this->execute('GETMETADATA', array(
            $this->escape($mailbox), $optlist));
        if ($code == self::ERROR_OK && preg_match('/^\* METADATA /i', $response)) {
            // Parse server response (remove "* METADATA ")
            $response = substr($response, 11);
            $ret_mbox = $this->tokenizeResponse($response, 1);
            $data     = $this->tokenizeResponse($response);
        if ($code == self::ERROR_OK) {
            $result = array();
            $data   = $this->tokenizeResponse($response);
            // The METADATA response can contain multiple entries in a single
            // response or multiple responses for each entry or group of entries
            if (!empty($data) && ($size = count($data))) {
                for ($i=0; $i<$size; $i++) {
                    if (is_array($data[$i])) {
                    if (isset($mbox) && is_array($data[$i])) {
                        $size_sub = count($data[$i]);
                        for ($x=0; $x<$size_sub; $x++) {
                            $data[$data[$i][$x]] = $data[$i][++$x];
                            $result[$mbox][$data[$i][$x]] = $data[$i][++$x];
                        }
                        unset($data[$i]);
                    }
                    else if ($data[$i] == '*' && $data[$i+1] == 'METADATA') {
                        unset($data[$i]);   // "*"
                        unset($data[++$i]); // "METADATA"
                        unset($data[++$i]); // Mailbox
                    else if ($data[$i] == '*') {
                        if ($data[$i+1] == 'METADATA') {
                            $mbox = $data[$i+2];
                            unset($data[$i]);   // "*"
                            unset($data[++$i]); // "METADATA"
                            unset($data[++$i]); // Mailbox
                        }
                        // get rid of other untagged responses
                        else {
                            unset($mbox);
                            unset($data[$i]);
                        }
                    }
                    else {
                        $data[$data[$i]] = $data[++$i];
                    else if (isset($mbox)) {
                        $result[$mbox][$data[$i]] = $data[++$i];
                        unset($data[$i]);
                        unset($data[$i-1]);
                    }
                    else {
                        unset($data[$i]);
                    }
                }
            }
            return $data;
            return $result;
        }
        return NULL;
@@ -2938,43 +2949,58 @@
        list($code, $response) = $this->execute('GETANNOTATION', array(
            $this->escape($mailbox), $entries, $attribs));
        if ($code == self::ERROR_OK && preg_match('/^\* ANNOTATION /i', $response)) {
            // Parse server response (remove "* ANNOTATION ")
            $response = substr($response, 13);
            $ret_mbox = $this->tokenizeResponse($response, 1);
            $data     = $this->tokenizeResponse($response);
            $res      = array();
        if ($code == self::ERROR_OK) {
            $result = array();
            $data   = $this->tokenizeResponse($response);
            // Here we returns only data compatible with METADATA result format
            if (!empty($data) && ($size = count($data))) {
                for ($i=0; $i<$size; $i++) {
                    $entry = $data[$i++];
                    if (is_array($entry)) {
                    $entry = $data[$i];
                    if (isset($mbox) && is_array($entry)) {
                        $attribs = $entry;
                        $entry   = $last_entry;
                    }
                    else
                        $attribs = $data[$i++];
                    else if ($entry == '*') {
                        if ($data[$i+1] == 'ANNOTATION') {
                            $mbox = $data[$i+2];
                            unset($data[$i]);   // "*"
                            unset($data[++$i]); // "ANNOTATION"
                            unset($data[++$i]); // Mailbox
                        }
                        // get rid of other untagged responses
                        else {
                            unset($mbox);
                            unset($data[$i]);
                        }
                        continue;
                    }
                    else if (isset($mbox)) {
                        $attribs = $data[++$i];
                    }
                    else {
                        unset($data[$i]);
                        continue;
                    }
                    if (!empty($attribs)) {
                        for ($x=0, $len=count($attribs); $x<$len;) {
                            $attr  = $attribs[$x++];
                            $value = $attribs[$x++];
                            if ($attr == 'value.priv') {
                                $res['/private' . $entry] = $value;
                                $result[$mbox]['/private' . $entry] = $value;
                            }
                            else if ($attr == 'value.shared') {
                                $res['/shared' . $entry] = $value;
                                $result[$mbox]['/shared' . $entry] = $value;
                            }
                        }
                    }
                    $last_entry = $entry;
                    unset($data[$i-1]);
                    unset($data[$i-2]);
                    unset($data[$i]);
                }
            }
            return $res;
            return $result;
        }
        return NULL;
@@ -3238,4 +3264,35 @@
        return strtr($string, array('\\"'=>'"', '\\\\' => '\\'));
    }
    /**
     * Set the value of the debugging flag.
     *
     * @param   boolean $debug      New value for the debugging flag.
     *
     * @access  public
     * @since   0.5-stable
     */
    function setDebug($debug, $handler = null)
    {
        $this->_debug = $debug;
        $this->_debug_handler = $handler;
    }
    /**
     * Write the given debug text to the current debug output handler.
     *
     * @param   string  $message    Debug mesage text.
     *
     * @access  private
     * @since   0.5-stable
     */
    private function debug($message)
    {
        if ($this->_debug_handler) {
            call_user_func_array($this->_debug_handler, array(&$this, $message));
        } else {
            echo "DEBUG: $message\n";
        }
    }
}