Skip to content

Commit

Permalink
Refactor of error handling and debug output
Browse files Browse the repository at this point in the history
Better property defaults, no need for constructor
Better default port handling
  • Loading branch information
Synchro committed Jun 5, 2014
1 parent a71a0d8 commit e400897
Showing 1 changed file with 31 additions and 56 deletions.
87 changes: 31 additions & 56 deletions class.pop3.php
Expand Up @@ -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
Expand Down Expand Up @@ -191,8 +180,8 @@ public function authorise($host, $port = false, $tval = false, $username = '', $
$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) {
Expand All @@ -211,7 +200,7 @@ public function authorise($host, $port = false, $tval = false, $username = '', $
* Connect to a POP3 server.
* @access public
* @param string $host
* @param bool|int $port
* @param bool|integer $port
* @param integer $tval
* @return boolean
*/
Expand All @@ -226,6 +215,10 @@ public function connect($host, $port = false, $tval = 30)
//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
Expand All @@ -236,34 +229,20 @@ public function connect($host, $port = false, $tval = 30)
); // 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();
Expand All @@ -286,12 +265,8 @@ public function connect($host, $port = false, $tval = 30)
*/
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;
Expand Down Expand Up @@ -369,31 +344,31 @@ private function sendString($string)
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;
}
}

/**
* 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>';
}

/**
Expand All @@ -406,12 +381,12 @@ private function displayErrors()
*/
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
);
));
}
}

0 comments on commit e400897

Please sign in to comment.