0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-20 10:02:14 +02:00

Refactor of error handling and debug output

Better property defaults, no need for constructor
Better default port handling
This commit is contained in:
Synchro 2014-06-05 03:11:24 +02:00
parent a71a0d89d7
commit e4008971fb

View File

@ -113,31 +113,20 @@ class POP3
* @type bool
* @access private
*/
private $connected;
private $connected = false;
/**
* Error container.
* @type array
* @access private
*/
private $error;
private $errors = array();
/**
* Line break constant
*/
const CRLF = "\r\n";
/**
* Constructor.
* @access public
*/
public function __construct()
{
$this->pop_conn = 0;
$this->connected = false;
$this->error = null;
}
/**
* Simple static wrapper for all-in-one POP before SMTP
* @param $host
@ -191,8 +180,8 @@ class POP3
$this->do_debug = $debug_level;
$this->username = $username;
$this->password = $password;
// Refresh the error log
$this->error = null;
// Reset the error log
$this->errors = array();
// connect
$result = $this->connect($this->host, $this->port, $this->tval);
if ($result) {
@ -211,7 +200,7 @@ class POP3
* Connect to a POP3 server.
* @access public
* @param string $host
* @param bool|int $port
* @param bool|integer $port
* @param integer $tval
* @return boolean
*/
@ -226,6 +215,10 @@ class POP3
//Rather than suppress it with @fsockopen, capture it cleanly instead
set_error_handler(array($this, 'catchWarning'));
if ($port === false) {
$port = $this->POP3_PORT;
}
// connect to the POP3 server
$this->pop_conn = fsockopen(
$host, // POP3 Host
@ -236,34 +229,20 @@ class POP3
); // Timeout (seconds)
// Restore the error handler
restore_error_handler();
// Does the Error Log now contain anything?
if ($this->error && $this->do_debug >= 1) {
$this->displayErrors();
}
// Did we connect?
if ($this->pop_conn == false) {
if ($this->pop_conn === false) {
// It would appear not...
$this->error = array(
$this->setError(array(
'error' => "Failed to connect to server $host on port $port",
'errno' => $errno,
'errstr' => $errstr
);
if ($this->do_debug >= 1) {
$this->displayErrors();
}
));
return false;
}
// Increase the stream time-out
// Check for PHP 4.3.0 or later
if (version_compare(phpversion(), '5.0.0', 'ge')) {
stream_set_timeout($this->pop_conn, $tval, 0);
} else {
// Does not work on Windows
if (substr(PHP_OS, 0, 3) !== 'WIN') {
socket_set_timeout($this->pop_conn, $tval, 0);
}
}
stream_set_timeout($this->pop_conn, $tval, 0);
// Get the POP3 server response
$pop3_response = $this->getResponse();
@ -286,12 +265,8 @@ class POP3
*/
public function login($username = '', $password = '')
{
if ($this->connected == false) {
$this->error = 'Not connected to POP3 server';
if ($this->do_debug >= 1) {
$this->displayErrors();
}
if (!$this->connected) {
$this->setError('Not connected to POP3 server');
}
if (empty($username)) {
$username = $this->username;
@ -369,14 +344,11 @@ class POP3
private function checkResponse($string)
{
if (substr($string, 0, 3) !== '+OK') {
$this->error = array(
$this->setError(array(
'error' => "Server reported an error: $string",
'errno' => 0,
'errstr' => ''
);
if ($this->do_debug >= 1) {
$this->displayErrors();
}
));
return false;
} else {
return true;
@ -384,16 +356,19 @@ class POP3
}
/**
* Display errors if debug is enabled.
* @access private
* Add an error to the internal error store.
* Also display debug output if it's enabled.
* @param $error
*/
private function displayErrors()
{
echo '<pre>';
foreach ($this->error as $single_error) {
print_r($single_error);
private function setError($error) {
$this->errors[] = $error;
if ($this->do_debug >= 1) {
echo '<pre>';
foreach ($this->errors as $error) {
print_r($error);
}
echo '</pre>';
}
echo '</pre>';
}
/**
@ -406,12 +381,12 @@ class POP3
*/
private function catchWarning($errno, $errstr, $errfile, $errline)
{
$this->error[] = array(
$this->setError(array(
'error' => "Connecting to the POP3 server raised a PHP warning: ",
'errno' => $errno,
'errstr' => $errstr,
'errfile' => $errfile,
'errline' => $errline
);
));
}
}