| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: |
|
| 19: |
|
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: |
|
| 30: | class POP3
|
| 31: | {
|
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: |
|
| 37: | public $Version = '5.2.28';
|
| 38: |
|
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: |
|
| 44: | public $POP3_PORT = 110;
|
| 45: |
|
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: |
|
| 51: | public $POP3_TIMEOUT = 30;
|
| 52: |
|
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: |
|
| 59: | public $CRLF = "\r\n";
|
| 60: |
|
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: |
|
| 67: | public $do_debug = 0;
|
| 68: |
|
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: |
|
| 74: | public $host;
|
| 75: |
|
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: |
|
| 81: | public $port;
|
| 82: |
|
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: |
|
| 88: | public $tval;
|
| 89: |
|
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: |
|
| 95: | public $username;
|
| 96: |
|
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: |
|
| 102: | public $password;
|
| 103: |
|
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: |
|
| 109: | protected $pop_conn;
|
| 110: |
|
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: |
|
| 116: | protected $connected = false;
|
| 117: |
|
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: |
|
| 123: | protected $errors = array();
|
| 124: |
|
| 125: | |
| 126: | |
| 127: |
|
| 128: | const CRLF = "\r\n";
|
| 129: |
|
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: |
|
| 140: | public static function popBeforeSmtp(
|
| 141: | $host,
|
| 142: | $port = false,
|
| 143: | $timeout = false,
|
| 144: | $username = '',
|
| 145: | $password = '',
|
| 146: | $debug_level = 0
|
| 147: | ) {
|
| 148: | $pop = new POP3;
|
| 149: | return $pop->authorise($host, $port, $timeout, $username, $password, $debug_level);
|
| 150: | }
|
| 151: |
|
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: |
|
| 165: | public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
|
| 166: | {
|
| 167: | $this->host = $host;
|
| 168: |
|
| 169: | if (false === $port) {
|
| 170: | $this->port = $this->POP3_PORT;
|
| 171: | } else {
|
| 172: | $this->port = (integer)$port;
|
| 173: | }
|
| 174: |
|
| 175: | if (false === $timeout) {
|
| 176: | $this->tval = $this->POP3_TIMEOUT;
|
| 177: | } else {
|
| 178: | $this->tval = (integer)$timeout;
|
| 179: | }
|
| 180: | $this->do_debug = $debug_level;
|
| 181: | $this->username = $username;
|
| 182: | $this->password = $password;
|
| 183: |
|
| 184: | $this->errors = array();
|
| 185: |
|
| 186: | $result = $this->connect($this->host, $this->port, $this->tval);
|
| 187: | if ($result) {
|
| 188: | $login_result = $this->login($this->username, $this->password);
|
| 189: | if ($login_result) {
|
| 190: | $this->disconnect();
|
| 191: | return true;
|
| 192: | }
|
| 193: | }
|
| 194: |
|
| 195: | $this->disconnect();
|
| 196: | return false;
|
| 197: | }
|
| 198: |
|
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: |
|
| 207: | public function connect($host, $port = false, $tval = 30)
|
| 208: | {
|
| 209: |
|
| 210: | if ($this->connected) {
|
| 211: | return true;
|
| 212: | }
|
| 213: |
|
| 214: |
|
| 215: |
|
| 216: | set_error_handler(array($this, 'catchWarning'));
|
| 217: |
|
| 218: | if (false === $port) {
|
| 219: | $port = $this->POP3_PORT;
|
| 220: | }
|
| 221: |
|
| 222: |
|
| 223: | $this->pop_conn = fsockopen(
|
| 224: | $host,
|
| 225: | $port,
|
| 226: | $errno,
|
| 227: | $errstr,
|
| 228: | $tval
|
| 229: | );
|
| 230: |
|
| 231: | restore_error_handler();
|
| 232: |
|
| 233: |
|
| 234: | if (false === $this->pop_conn) {
|
| 235: |
|
| 236: | $this->setError(array(
|
| 237: | 'error' => "Failed to connect to server $host on port $port",
|
| 238: | 'errno' => $errno,
|
| 239: | 'errstr' => $errstr
|
| 240: | ));
|
| 241: | return false;
|
| 242: | }
|
| 243: |
|
| 244: |
|
| 245: | stream_set_timeout($this->pop_conn, $tval, 0);
|
| 246: |
|
| 247: |
|
| 248: | $pop3_response = $this->getResponse();
|
| 249: |
|
| 250: | if ($this->checkResponse($pop3_response)) {
|
| 251: |
|
| 252: | $this->connected = true;
|
| 253: | return true;
|
| 254: | }
|
| 255: | return false;
|
| 256: | }
|
| 257: |
|
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: |
|
| 266: | public function login($username = '', $password = '')
|
| 267: | {
|
| 268: | if (!$this->connected) {
|
| 269: | $this->setError('Not connected to POP3 server');
|
| 270: | }
|
| 271: | if (empty($username)) {
|
| 272: | $username = $this->username;
|
| 273: | }
|
| 274: | if (empty($password)) {
|
| 275: | $password = $this->password;
|
| 276: | }
|
| 277: |
|
| 278: |
|
| 279: | $this->sendString("USER $username" . self::CRLF);
|
| 280: | $pop3_response = $this->getResponse();
|
| 281: | if ($this->checkResponse($pop3_response)) {
|
| 282: |
|
| 283: | $this->sendString("PASS $password" . self::CRLF);
|
| 284: | $pop3_response = $this->getResponse();
|
| 285: | if ($this->checkResponse($pop3_response)) {
|
| 286: | return true;
|
| 287: | }
|
| 288: | }
|
| 289: | return false;
|
| 290: | }
|
| 291: |
|
| 292: | |
| 293: | |
| 294: | |
| 295: |
|
| 296: | public function disconnect()
|
| 297: | {
|
| 298: | $this->sendString('QUIT');
|
| 299: |
|
| 300: |
|
| 301: | try {
|
| 302: | @fclose($this->pop_conn);
|
| 303: | } catch (Exception $e) {
|
| 304: |
|
| 305: | };
|
| 306: | }
|
| 307: |
|
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: |
|
| 315: | protected function getResponse($size = 128)
|
| 316: | {
|
| 317: | $response = fgets($this->pop_conn, $size);
|
| 318: | if ($this->do_debug >= 1) {
|
| 319: | echo "Server -> Client: $response";
|
| 320: | }
|
| 321: | return $response;
|
| 322: | }
|
| 323: |
|
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: |
|
| 330: | protected function sendString($string)
|
| 331: | {
|
| 332: | if ($this->pop_conn) {
|
| 333: | if ($this->do_debug >= 2) {
|
| 334: | echo "Client -> Server: $string";
|
| 335: | }
|
| 336: | return fwrite($this->pop_conn, $string, strlen($string));
|
| 337: | }
|
| 338: | return 0;
|
| 339: | }
|
| 340: |
|
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: |
|
| 348: | protected function checkResponse($string)
|
| 349: | {
|
| 350: | if (substr($string, 0, 3) !== '+OK') {
|
| 351: | $this->setError(array(
|
| 352: | 'error' => "Server reported an error: $string",
|
| 353: | 'errno' => 0,
|
| 354: | 'errstr' => ''
|
| 355: | ));
|
| 356: | return false;
|
| 357: | } else {
|
| 358: | return true;
|
| 359: | }
|
| 360: | }
|
| 361: |
|
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: |
|
| 368: | protected function setError($error)
|
| 369: | {
|
| 370: | $this->errors[] = $error;
|
| 371: | if ($this->do_debug >= 1) {
|
| 372: | echo '<pre>';
|
| 373: | foreach ($this->errors as $error) {
|
| 374: | print_r($error);
|
| 375: | }
|
| 376: | echo '</pre>';
|
| 377: | }
|
| 378: | }
|
| 379: |
|
| 380: | |
| 381: | |
| 382: | |
| 383: |
|
| 384: | public function getErrors()
|
| 385: | {
|
| 386: | return $this->errors;
|
| 387: | }
|
| 388: |
|
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: |
|
| 397: | protected function catchWarning($errno, $errstr, $errfile, $errline)
|
| 398: | {
|
| 399: | $this->setError(array(
|
| 400: | 'error' => "Connecting to the POP3 server raised a PHP warning: ",
|
| 401: | 'errno' => $errno,
|
| 402: | 'errstr' => $errstr,
|
| 403: | 'errfile' => $errfile,
|
| 404: | 'errline' => $errline
|
| 405: | ));
|
| 406: | }
|
| 407: | }
|
| 408: | |