- Add debug handler support in rcube_imap_generic

release-0.6
alecpl 14 years ago
parent 4d268b1fae
commit 7f1da4818e

@ -503,7 +503,7 @@ class rcmail
'auth_method' => $this->config->get('imap_auth_type', 'check'), 'auth_method' => $this->config->get('imap_auth_type', 'check'),
'auth_cid' => $this->config->get('imap_auth_cid'), 'auth_cid' => $this->config->get('imap_auth_cid'),
'auth_pw' => $this->config->get('imap_auth_pw'), 'auth_pw' => $this->config->get('imap_auth_pw'),
'debug_mode' => (bool) $this->config->get('imap_debug', 0), 'debug' => (bool) $this->config->get('imap_debug', 0),
'force_caps' => (bool) $this->config->get('imap_force_caps'), 'force_caps' => (bool) $this->config->get('imap_force_caps'),
'timeout' => (int) $this->config->get('imap_timeout', 0), 'timeout' => (int) $this->config->get('imap_timeout', 0),
); );

@ -148,6 +148,9 @@ class rcube_imap
$this->options['port'] = $port; $this->options['port'] = $port;
if ($this->options['debug'])
$this->conn->setDebug(true, array($this, 'debug_handler'));
$attempt = 0; $attempt = 0;
do { do {
$data = rcmail::get_instance()->plugins->exec_hook('imap_connect', $data = rcmail::get_instance()->plugins->exec_hook('imap_connect',
@ -223,7 +226,7 @@ class rcube_imap
*/ */
function get_error_code() function get_error_code()
{ {
return ($this->conn) ? $this->conn->errornum : 0; return $this->conn->errornum;
} }
@ -234,7 +237,7 @@ class rcube_imap
*/ */
function get_error_str() function get_error_str()
{ {
return ($this->conn) ? $this->conn->error : null; return $this->conn->error;
} }
@ -245,9 +248,6 @@ class rcube_imap
*/ */
function get_response_code() function get_response_code()
{ {
if (!$this->conn)
return self::UNKNOWN;
switch ($this->conn->resultcode) { switch ($this->conn->resultcode) {
case 'NOPERM': case 'NOPERM':
return self::NOPERM; return self::NOPERM;
@ -278,7 +278,7 @@ class rcube_imap
*/ */
function get_response_str() function get_response_str()
{ {
return ($this->conn) ? $this->conn->result : null; return $this->conn->result;
} }
@ -546,7 +546,7 @@ class rcube_imap
$imap_shared = $config->get('imap_ns_shared'); $imap_shared = $config->get('imap_ns_shared');
$imap_delimiter = $config->get('imap_delimiter'); $imap_delimiter = $config->get('imap_delimiter');
if (!$this->conn) if (!$this->conn->connected())
return; return;
$ns = $this->conn->getNamespace(); $ns = $this->conn->getNamespace();
@ -4740,6 +4740,16 @@ class rcube_imap
return $result; return $result;
} }
/**
* This is our own debug handler for the IMAP connection
* @access public
*/
public function debug_handler(&$imap, $message)
{
write_log('imap', $message);
}
} // end class rcube_imap } // end class rcube_imap

@ -109,6 +109,8 @@ class rcube_imap_generic
private $prefs; private $prefs;
private $cmd_tag; private $cmd_tag;
private $cmd_num = 0; private $cmd_num = 0;
private $_debug = false;
private $_debug_handler = false;
const ERROR_OK = 0; const ERROR_OK = 0;
const ERROR_NO = -1; const ERROR_NO = -1;
@ -142,8 +144,8 @@ class rcube_imap_generic
if (!$this->fp) if (!$this->fp)
return false; return false;
if (!empty($this->prefs['debug_mode'])) { if ($this->_debug) {
write_log('imap', 'C: '. rtrim($string)); $this->debug('C: '. rtrim($string));
} }
$res = fwrite($this->fp, $string . ($endln ? "\r\n" : '')); $res = fwrite($this->fp, $string . ($endln ? "\r\n" : ''));
@ -231,8 +233,8 @@ class rcube_imap_generic
$this->fp = null; $this->fp = null;
break; break;
} }
if (!empty($this->prefs['debug_mode'])) { if ($this->_debug) {
write_log('imap', 'S: '. rtrim($buffer)); $this->debug('S: '. rtrim($buffer));
} }
$line .= $buffer; $line .= $buffer;
} while ($buffer[strlen($buffer)-1] != "\n"); } while ($buffer[strlen($buffer)-1] != "\n");
@ -268,8 +270,8 @@ class rcube_imap_generic
while ($len < $bytes && !feof($this->fp)) while ($len < $bytes && !feof($this->fp))
{ {
$d = fread($this->fp, $bytes-$len); $d = fread($this->fp, $bytes-$len);
if (!empty($this->prefs['debug_mode'])) { if ($this->_debug) {
write_log('imap', 'S: '. $d); $this->debug('S: '. $d);
} }
$data .= $d; $data .= $d;
$data_len = strlen($data); $data_len = strlen($data);
@ -686,8 +688,8 @@ class rcube_imap_generic
$line = trim(fgets($this->fp, 8192)); $line = trim(fgets($this->fp, 8192));
if ($this->prefs['debug_mode'] && $line) { if ($this->_debug && $line) {
write_log('imap', 'S: '. $line); $this->debug('S: '. $line);
} }
// Connected to wrong port or connection error? // Connected to wrong port or connection error?
@ -3238,4 +3240,35 @@ class rcube_imap_generic
return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); 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";
}
}
} }

Loading…
Cancel
Save