| 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 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
class POP3 |
|---|
| 47 |
{ |
|---|
| 48 |
|
|---|
| 49 |
* Default POP3 port |
|---|
| 50 |
* @var int |
|---|
| 51 |
*/ |
|---|
| 52 |
var $POP3_PORT = 110; |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
* Default Timeout |
|---|
| 56 |
* @var int |
|---|
| 57 |
*/ |
|---|
| 58 |
var $POP3_TIMEOUT = 30; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
* POP3 Carriage Return + Line Feed |
|---|
| 62 |
* @var string |
|---|
| 63 |
*/ |
|---|
| 64 |
var $CRLF = "\r\n"; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Displaying Debug warnings? (0 = now, 1+ = yes) |
|---|
| 68 |
* @var int |
|---|
| 69 |
*/ |
|---|
| 70 |
var $do_debug = 2; |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
* POP3 Mail Server |
|---|
| 74 |
* @var string |
|---|
| 75 |
*/ |
|---|
| 76 |
var $host; |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
* POP3 Port |
|---|
| 80 |
* @var int |
|---|
| 81 |
*/ |
|---|
| 82 |
var $port; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* POP3 Timeout Value |
|---|
| 86 |
* @var int |
|---|
| 87 |
*/ |
|---|
| 88 |
var $tval; |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* POP3 Username |
|---|
| 92 |
* @var string |
|---|
| 93 |
*/ |
|---|
| 94 |
var $username; |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
* POP3 Password |
|---|
| 98 |
* @var string |
|---|
| 99 |
*/ |
|---|
| 100 |
var $password; |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
* @access private |
|---|
| 104 |
*/ |
|---|
| 105 |
var $pop_conn; |
|---|
| 106 |
var $connected; |
|---|
| 107 |
var $error; |
|---|
| 108 |
/**#@-*/ |
|---|
| 109 |
|
|---|
| 110 |
/** |
|---|
| 111 |
* Constructor, sets the initial values |
|---|
| 112 |
* |
|---|
| 113 |
* @return POP3 |
|---|
| 114 |
*/ |
|---|
| 115 |
function POP3 () |
|---|
| 116 |
{ |
|---|
| 117 |
$this->pop_conn = 0; |
|---|
| 118 |
$this->connected = false; |
|---|
| 119 |
$this->error = null; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
* Combination of public events - connect, login, disconnect |
|---|
| 124 |
* |
|---|
| 125 |
* @param string $host |
|---|
| 126 |
* @param integer $port |
|---|
| 127 |
* @param integer $tval |
|---|
| 128 |
* @param string $username |
|---|
| 129 |
* @param string $password |
|---|
| 130 |
*/ |
|---|
| 131 |
function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) |
|---|
| 132 |
{ |
|---|
| 133 |
$this->host = $host; |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
if ($port == false) |
|---|
| 137 |
{ |
|---|
| 138 |
$this->port = $this->POP3_PORT; |
|---|
| 139 |
} |
|---|
| 140 |
else |
|---|
| 141 |
{ |
|---|
| 142 |
$this->port = $port; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
if ($tval == false) |
|---|
| 147 |
{ |
|---|
| 148 |
$this->tval = $this->POP3_TIMEOUT; |
|---|
| 149 |
} |
|---|
| 150 |
else |
|---|
| 151 |
{ |
|---|
| 152 |
$this->tval = $tval; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
$this->do_debug = $debug_level; |
|---|
| 156 |
$this->username = $username; |
|---|
| 157 |
$this->password = $password; |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
$this->error = null; |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
$result = $this->Connect($this->host, $this->port, $this->tval); |
|---|
| 164 |
|
|---|
| 165 |
if ($result) |
|---|
| 166 |
{ |
|---|
| 167 |
$login_result = $this->Login($this->username, $this->password); |
|---|
| 168 |
|
|---|
| 169 |
if ($login_result) |
|---|
| 170 |
{ |
|---|
| 171 |
$this->Disconnect(); |
|---|
| 172 |
|
|---|
| 173 |
return true; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
$this->Disconnect(); |
|---|
| 180 |
|
|---|
| 181 |
return false; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
* Connect to the POP3 server |
|---|
| 186 |
* |
|---|
| 187 |
* @param string $host |
|---|
| 188 |
* @param integer $port |
|---|
| 189 |
* @param integer $tval |
|---|
| 190 |
* @return boolean |
|---|
| 191 |
*/ |
|---|
| 192 |
function Connect ($host, $port = false, $tval = 30) |
|---|
| 193 |
{ |
|---|
| 194 |
|
|---|
| 195 |
if ($this->connected) |
|---|
| 196 |
{ |
|---|
| 197 |
return true; |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
On Windows this will raise a PHP Warning error if the hostname doesn't exist. |
|---|
| 202 |
Rather than supress it with @fsockopen, let's capture it cleanly instead |
|---|
| 203 |
*/ |
|---|
| 204 |
|
|---|
| 205 |
set_error_handler(array(&$this, 'catchWarning')); |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
$this->pop_conn = fsockopen($host, |
|---|
| 209 |
$port, |
|---|
| 210 |
$errno, |
|---|
| 211 |
$errstr, |
|---|
| 212 |
$tval); |
|---|
| 213 |
|
|---|
| 214 |
// Restore the error handler |
|---|
| 215 |
restore_error_handler(); |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
if ($this->error && $this->do_debug >= 1) |
|---|
| 219 |
{ |
|---|
| 220 |
$this->displayErrors(); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
if ($this->pop_conn == false) |
|---|
| 225 |
{ |
|---|
| 226 |
|
|---|
| 227 |
$this->error = array( |
|---|
| 228 |
'error' => "Failed to connect to server $host on port $port", |
|---|
| 229 |
'errno' => $errno, |
|---|
| 230 |
'errstr' => $errstr |
|---|
| 231 |
); |
|---|
| 232 |
|
|---|
| 233 |
if ($this->do_debug >= 1) |
|---|
| 234 |
{ |
|---|
| 235 |
$this->displayErrors(); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
return false; |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
// Check for PHP 4.3.0 or later |
|---|
| 244 |
if (version_compare(phpversion(), '4.3.0', 'ge')) |
|---|
| 245 |
{ |
|---|
| 246 |
stream_set_timeout($this->pop_conn, $tval, 0); |
|---|
| 247 |
} |
|---|
| 248 |
else |
|---|
| 249 |
{ |
|---|
| 250 |
|
|---|
| 251 |
if (substr(PHP_OS, 0, 3) !== 'WIN') |
|---|
| 252 |
{ |
|---|
| 253 |
socket_set_timeout($this->pop_conn, $tval, 0); |
|---|
| 254 |
} |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
$pop3_response = $this->getResponse(); |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
if ($this->checkResponse($pop3_response)) |
|---|
| 262 |
{ |
|---|
| 263 |
|
|---|
| 264 |
$this->connected = true; |
|---|
| 265 |
return true; |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
* Login to the POP3 server (does not support APOP yet) |
|---|
| 272 |
* |
|---|
| 273 |
* @param string $username |
|---|
| 274 |
* @param string $password |
|---|
| 275 |
* @return boolean |
|---|
| 276 |
*/ |
|---|
| 277 |
function Login ($username = '', $password = '') |
|---|
| 278 |
{ |
|---|
| 279 |
if ($this->connected == false) |
|---|
| 280 |
{ |
|---|
| 281 |
$this->error = 'Not connected to POP3 server'; |
|---|
| 282 |
|
|---|
| 283 |
if ($this->do_debug >= 1) |
|---|
| 284 |
{ |
|---|
| 285 |
$this->displayErrors(); |
|---|
| 286 |
} |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
if (empty($username)) |
|---|
| 290 |
{ |
|---|
| 291 |
$username = $this->username; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
if (empty($password)) |
|---|
| 295 |
{ |
|---|
| 296 |
$password = $this->password; |
|---|
| 297 |
} |
|---|
| 298 |
|
|---|
| 299 |
$pop_username = "USER $username" . $this->CRLF; |
|---|
| 300 |
$pop_password = "PASS $password" . $this->CRLF; |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
$this->sendString($pop_username); |
|---|
| 304 |
$pop3_response = $this->getResponse(); |
|---|
| 305 |
|
|---|
| 306 |
if ($this->checkResponse($pop3_response)) |
|---|
| 307 |
{ |
|---|
| 308 |
|
|---|
| 309 |
$this->sendString($pop_password); |
|---|
| 310 |
$pop3_response = $this->getResponse(); |
|---|
| 311 |
|
|---|
| 312 |
if ($this->checkResponse($pop3_response)) |
|---|
| 313 |
{ |
|---|
| 314 |
return true; |
|---|
| 315 |
} |
|---|
| 316 |
else |
|---|
| 317 |
{ |
|---|
| 318 |
return false; |
|---|
| 319 |
} |
|---|
| 320 |
} |
|---|
| 321 |
else |
|---|
| 322 |
{ |
|---|
| 323 |
return false; |
|---|
| 324 |
} |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
* Disconnect from the POP3 server |
|---|
| 329 |
*/ |
|---|
| 330 |
function Disconnect () |
|---|
| 331 |
{ |
|---|
| 332 |
$this->sendString('QUIT'); |
|---|
| 333 |
|
|---|
| 334 |
fclose($this->pop_conn); |
|---|
| 335 |
} |
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
--------------- |
|---|
| 339 |
Private Methods |
|---|
| 340 |
--------------- |
|---|
| 341 |
*/ |
|---|
| 342 |
|
|---|
| 343 |
/** |
|---|
| 344 |
* Get the socket response back. |
|---|
| 345 |
* $size is the maximum number of bytes to retrieve |
|---|
| 346 |
* |
|---|
| 347 |
* @param integer $size |
|---|
| 348 |
* @return string |
|---|
| 349 |
*/ |
|---|
| 350 |
function getResponse ($size = 128) |
|---|
| 351 |
{ |
|---|
| 352 |
$pop3_response = fgets($this->pop_conn, $size); |
|---|
| 353 |
|
|---|
| 354 |
return $pop3_response; |
|---|
| 355 |
} |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
* Send a string down the open socket connection to the POP3 server |
|---|
| 359 |
* |
|---|
| 360 |
* @param string $string |
|---|
| 361 |
* @return integer |
|---|
| 362 |
*/ |
|---|
| 363 |
function sendString ($string) |
|---|
| 364 |
{ |
|---|
| 365 |
$bytes_sent = fwrite($this->pop_conn, $string, strlen($string)); |
|---|
| 366 |
|
|---|
| 367 |
return $bytes_sent; |
|---|
| 368 |
|
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
* Checks the POP3 server response for +OK or -ERR |
|---|
| 373 |
* |
|---|
| 374 |
* @param string $string |
|---|
| 375 |
* @return boolean |
|---|
| 376 |
*/ |
|---|
| 377 |
function checkResponse ($string) |
|---|
| 378 |
{ |
|---|
| 379 |
if (substr($string, 0, 3) !== '+OK') |
|---|
| 380 |
{ |
|---|
| 381 |
$this->error = array( |
|---|
| 382 |
'error' => "Server reported an error: $string", |
|---|
| 383 |
'errno' => 0, |
|---|
| 384 |
'errstr' => '' |
|---|
| 385 |
); |
|---|
| 386 |
|
|---|
| 387 |
if ($this->do_debug >= 1) |
|---|
| 388 |
{ |
|---|
| 389 |
$this->displayErrors(); |
|---|
| 390 |
} |
|---|
| 391 |
|
|---|
| 392 |
return false; |
|---|
| 393 |
} |
|---|
| 394 |
else |
|---|
| 395 |
{ |
|---|
| 396 |
return true; |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
* If debug is enabled, display the error message array |
|---|
| 403 |
* |
|---|
| 404 |
*/ |
|---|
| 405 |
function displayErrors () |
|---|
| 406 |
{ |
|---|
| 407 |
echo '<pre>'; |
|---|
| 408 |
|
|---|
| 409 |
foreach ($this->error as $single_error) |
|---|
| 410 |
{ |
|---|
| 411 |
print_r($single_error); |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
echo '</pre>'; |
|---|
| 415 |
} |
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
* Takes over from PHP for the socket warning handler |
|---|
| 419 |
* |
|---|
| 420 |
* @param integer $errno |
|---|
| 421 |
* @param string $errstr |
|---|
| 422 |
* @param string $errfile |
|---|
| 423 |
* @param integer $errline |
|---|
| 424 |
*/ |
|---|
| 425 |
function catchWarning ($errno, $errstr, $errfile, $errline) |
|---|
| 426 |
{ |
|---|
| 427 |
$this->error[] = array( |
|---|
| 428 |
'error' => "Connecting to the POP3 server raised a PHP warning: ", |
|---|
| 429 |
'errno' => $errno, |
|---|
| 430 |
'errstr' => $errstr |
|---|
| 431 |
); |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
} |
|---|
| 436 |
?> |
|---|
| 437 |
|
|---|