123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <?php
- class POP3
- {
-
- public $Version = '5.2.27';
-
- public $POP3_PORT = 110;
-
- public $POP3_TIMEOUT = 30;
-
- public $CRLF = "\r\n";
-
- public $do_debug = 0;
-
- public $host;
-
- public $port;
-
- public $tval;
-
- public $username;
-
- public $password;
-
- protected $pop_conn;
-
- protected $connected = false;
-
- protected $errors = array();
-
- const CRLF = "\r\n";
-
- public static function popBeforeSmtp(
- $host,
- $port = false,
- $timeout = false,
- $username = '',
- $password = '',
- $debug_level = 0
- ) {
- $pop = new POP3;
- return $pop->authorise($host, $port, $timeout, $username, $password, $debug_level);
- }
-
- public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
- {
- $this->host = $host;
-
- if (false === $port) {
- $this->port = $this->POP3_PORT;
- } else {
- $this->port = (integer)$port;
- }
-
- if (false === $timeout) {
- $this->tval = $this->POP3_TIMEOUT;
- } else {
- $this->tval = (integer)$timeout;
- }
- $this->do_debug = $debug_level;
- $this->username = $username;
- $this->password = $password;
-
- $this->errors = array();
-
- $result = $this->connect($this->host, $this->port, $this->tval);
- if ($result) {
- $login_result = $this->login($this->username, $this->password);
- if ($login_result) {
- $this->disconnect();
- return true;
- }
- }
-
- $this->disconnect();
- return false;
- }
-
- public function connect($host, $port = false, $tval = 30)
- {
-
- if ($this->connected) {
- return true;
- }
-
-
- set_error_handler(array($this, 'catchWarning'));
- if (false === $port) {
- $port = $this->POP3_PORT;
- }
-
- $this->pop_conn = fsockopen(
- $host,
- $port,
- $errno,
- $errstr,
- $tval
- );
-
- restore_error_handler();
-
- if (false === $this->pop_conn) {
-
- $this->setError(array(
- 'error' => "Failed to connect to server $host on port $port",
- 'errno' => $errno,
- 'errstr' => $errstr
- ));
- return false;
- }
-
- stream_set_timeout($this->pop_conn, $tval, 0);
-
- $pop3_response = $this->getResponse();
-
- if ($this->checkResponse($pop3_response)) {
-
- $this->connected = true;
- return true;
- }
- return false;
- }
-
- public function login($username = '', $password = '')
- {
- if (!$this->connected) {
- $this->setError('Not connected to POP3 server');
- }
- if (empty($username)) {
- $username = $this->username;
- }
- if (empty($password)) {
- $password = $this->password;
- }
-
- $this->sendString("USER $username" . self::CRLF);
- $pop3_response = $this->getResponse();
- if ($this->checkResponse($pop3_response)) {
-
- $this->sendString("PASS $password" . self::CRLF);
- $pop3_response = $this->getResponse();
- if ($this->checkResponse($pop3_response)) {
- return true;
- }
- }
- return false;
- }
-
- public function disconnect()
- {
- $this->sendString('QUIT');
-
-
- try {
- @fclose($this->pop_conn);
- } catch (Exception $e) {
-
- };
- }
-
- protected function getResponse($size = 128)
- {
- $response = fgets($this->pop_conn, $size);
- if ($this->do_debug >= 1) {
- echo "Server -> Client: $response";
- }
- return $response;
- }
-
- protected function sendString($string)
- {
- if ($this->pop_conn) {
- if ($this->do_debug >= 2) {
- echo "Client -> Server: $string";
- }
- return fwrite($this->pop_conn, $string, strlen($string));
- }
- return 0;
- }
-
- protected function checkResponse($string)
- {
- if (substr($string, 0, 3) !== '+OK') {
- $this->setError(array(
- 'error' => "Server reported an error: $string",
- 'errno' => 0,
- 'errstr' => ''
- ));
- return false;
- } else {
- return true;
- }
- }
-
- protected function setError($error)
- {
- $this->errors[] = $error;
- if ($this->do_debug >= 1) {
- echo '<pre>';
- foreach ($this->errors as $error) {
- print_r($error);
- }
- echo '</pre>';
- }
- }
-
- public function getErrors()
- {
- return $this->errors;
- }
-
- protected function catchWarning($errno, $errstr, $errfile, $errline)
- {
- $this->setError(array(
- 'error' => "Connecting to the POP3 server raised a PHP warning: ",
- 'errno' => $errno,
- 'errstr' => $errstr,
- 'errfile' => $errfile,
- 'errline' => $errline
- ));
- }
- }
|