root@9: pop_conn = 0; root@9: $this->connected = false; root@9: $this->error = null; root@9: } root@9: root@9: /** root@9: * Combination of public events - connect, login, disconnect root@9: * @access public root@9: * @param string $host root@9: * @param integer $port root@9: * @param integer $tval root@9: * @param string $username root@9: * @param string $password root@9: */ root@9: public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) { root@9: $this->host = $host; root@9: root@9: // If no port value is passed, retrieve it root@9: if ($port == false) { root@9: $this->port = $this->POP3_PORT; root@9: } else { root@9: $this->port = $port; root@9: } root@9: root@9: // If no port value is passed, retrieve it root@9: if ($tval == false) { root@9: $this->tval = $this->POP3_TIMEOUT; root@9: } else { root@9: $this->tval = $tval; root@9: } root@9: root@9: $this->do_debug = $debug_level; root@9: $this->username = $username; root@9: $this->password = $password; root@9: root@9: // Refresh the error log root@9: $this->error = null; root@9: root@9: // Connect root@9: $result = $this->Connect($this->host, $this->port, $this->tval); root@9: root@9: if ($result) { root@9: $login_result = $this->Login($this->username, $this->password); root@9: root@9: if ($login_result) { root@9: $this->Disconnect(); root@9: root@9: return true; root@9: } root@9: root@9: } root@9: root@9: // We need to disconnect regardless if the login succeeded root@9: $this->Disconnect(); root@9: root@9: return false; root@9: } root@9: root@9: /** root@9: * Connect to the POP3 server root@9: * @access public root@9: * @param string $host root@9: * @param integer $port root@9: * @param integer $tval root@9: * @return boolean root@9: */ root@9: public function Connect ($host, $port = false, $tval = 30) { root@9: // Are we already connected? root@9: if ($this->connected) { root@9: return true; root@9: } root@9: root@9: /* root@9: On Windows this will raise a PHP Warning error if the hostname doesn't exist. root@9: Rather than supress it with @fsockopen, let's capture it cleanly instead root@9: */ root@9: root@9: set_error_handler(array(&$this, 'catchWarning')); root@9: root@9: // Connect to the POP3 server root@9: $this->pop_conn = fsockopen($host, // POP3 Host root@9: $port, // Port # root@9: $errno, // Error Number root@9: $errstr, // Error Message root@9: $tval); // Timeout (seconds) root@9: root@9: // Restore the error handler root@9: restore_error_handler(); root@9: root@9: // Does the Error Log now contain anything? root@9: if ($this->error && $this->do_debug >= 1) { root@9: $this->displayErrors(); root@9: } root@9: root@9: // Did we connect? root@9: if ($this->pop_conn == false) { root@9: // It would appear not... root@9: $this->error = array( root@9: 'error' => "Failed to connect to server $host on port $port", root@9: 'errno' => $errno, root@9: 'errstr' => $errstr root@9: ); root@9: root@9: if ($this->do_debug >= 1) { root@9: $this->displayErrors(); root@9: } root@9: root@9: return false; root@9: } root@9: root@9: // Increase the stream time-out root@9: root@9: // Check for PHP 4.3.0 or later root@9: if (version_compare(phpversion(), '5.0.0', 'ge')) { root@9: stream_set_timeout($this->pop_conn, $tval, 0); root@9: } else { root@9: // Does not work on Windows root@9: if (substr(PHP_OS, 0, 3) !== 'WIN') { root@9: socket_set_timeout($this->pop_conn, $tval, 0); root@9: } root@9: } root@9: root@9: // Get the POP3 server response root@9: $pop3_response = $this->getResponse(); root@9: root@9: // Check for the +OK root@9: if ($this->checkResponse($pop3_response)) { root@9: // The connection is established and the POP3 server is talking root@9: $this->connected = true; root@9: return true; root@9: } root@9: root@9: } root@9: root@9: /** root@9: * Login to the POP3 server (does not support APOP yet) root@9: * @access public root@9: * @param string $username root@9: * @param string $password root@9: * @return boolean root@9: */ root@9: public function Login ($username = '', $password = '') { root@9: if ($this->connected == false) { root@9: $this->error = 'Not connected to POP3 server'; root@9: root@9: if ($this->do_debug >= 1) { root@9: $this->displayErrors(); root@9: } root@9: } root@9: root@9: if (empty($username)) { root@9: $username = $this->username; root@9: } root@9: root@9: if (empty($password)) { root@9: $password = $this->password; root@9: } root@9: root@9: $pop_username = "USER $username" . $this->CRLF; root@9: $pop_password = "PASS $password" . $this->CRLF; root@9: root@9: // Send the Username root@9: $this->sendString($pop_username); root@9: $pop3_response = $this->getResponse(); root@9: root@9: if ($this->checkResponse($pop3_response)) { root@9: // Send the Password root@9: $this->sendString($pop_password); root@9: $pop3_response = $this->getResponse(); root@9: root@9: if ($this->checkResponse($pop3_response)) { root@9: return true; root@9: } else { root@9: return false; root@9: } root@9: } else { root@9: return false; root@9: } root@9: } root@9: root@9: /** root@9: * Disconnect from the POP3 server root@9: * @access public root@9: */ root@9: public function Disconnect () { root@9: $this->sendString('QUIT'); root@9: root@9: fclose($this->pop_conn); root@9: } root@9: root@9: ///////////////////////////////////////////////// root@9: // Private Methods root@9: ///////////////////////////////////////////////// root@9: root@9: /** root@9: * Get the socket response back. root@9: * $size is the maximum number of bytes to retrieve root@9: * @access private root@9: * @param integer $size root@9: * @return string root@9: */ root@9: private function getResponse ($size = 128) { root@9: $pop3_response = fgets($this->pop_conn, $size); root@9: root@9: return $pop3_response; root@9: } root@9: root@9: /** root@9: * Send a string down the open socket connection to the POP3 server root@9: * @access private root@9: * @param string $string root@9: * @return integer root@9: */ root@9: private function sendString ($string) { root@9: $bytes_sent = fwrite($this->pop_conn, $string, strlen($string)); root@9: root@9: return $bytes_sent; root@9: } root@9: root@9: /** root@9: * Checks the POP3 server response for +OK or -ERR root@9: * @access private root@9: * @param string $string root@9: * @return boolean root@9: */ root@9: private function checkResponse ($string) { root@9: if (substr($string, 0, 3) !== '+OK') { root@9: $this->error = array( root@9: 'error' => "Server reported an error: $string", root@9: 'errno' => 0, root@9: 'errstr' => '' root@9: ); root@9: root@9: if ($this->do_debug >= 1) { root@9: $this->displayErrors(); root@9: } root@9: root@9: return false; root@9: } else { root@9: return true; root@9: } root@9: root@9: } root@9: root@9: /** root@9: * If debug is enabled, display the error message array root@9: * @access private root@9: */ root@9: private function displayErrors () { root@9: echo '
';
root@9: 
root@9:     foreach ($this->error as $single_error) {
root@9:       print_r($single_error);
root@9:     }
root@9: 
root@9:     echo '
'; root@9: } root@9: root@9: /** root@9: * Takes over from PHP for the socket warning handler root@9: * @access private root@9: * @param integer $errno root@9: * @param string $errstr root@9: * @param string $errfile root@9: * @param integer $errline root@9: */ root@9: private function catchWarning ($errno, $errstr, $errfile, $errline) { root@9: $this->error[] = array( root@9: 'error' => "Connecting to the POP3 server raised a PHP warning: ", root@9: 'errno' => $errno, root@9: 'errstr' => $errstr root@9: ); root@9: } root@9: root@9: // End of class root@9: } root@9: ?>