[ Index ] |
|
Code source de e107 0.7.8 |
1 <?php 2 /* 3 POP Before SMTP Authentication Class 4 Version 1.0 5 6 Author: Richard Davey (rich@corephp.co.uk) 7 License: LGPL, see PHPMailer License 8 9 Specifically for PHPMailer to allow POP before SMTP authentication. 10 Does not yet work with APOP - if you have an APOP account, contact me 11 and we can test changes to this script. 12 13 This class is based on the structure of the SMTP class by Chris Ryan 14 15 This class is rfc 1939 compliant and implements all the commands 16 required for POP3 connection, authentication and disconnection. 17 18 @package PHPMailer 19 @author Richard Davey 20 */ 21 22 class POP3 23 { 24 // Default POP3 port 25 var $POP3_PORT = 110; 26 27 // Default Timeout 28 var $POP3_TIMEOUT = 30; 29 30 // Carriage Return + Line Feed 31 var $CRLF = "\r\n"; 32 33 // Displaying Debug warnings? (0 = now, 1+ = yes) 34 var $do_debug = 2; 35 36 // Socket connection resource handle 37 var $pop_conn; 38 39 // Boolean state of connection 40 var $connected; 41 42 // Error log array 43 var $error; 44 45 // POP3 Server Connection Required values 46 47 // Mail Server 48 var $host; 49 50 // Port 51 var $port; 52 53 // Timeout Value 54 var $tval; 55 56 // POP3 Username 57 var $username; 58 59 // POP3 Password 60 var $password; 61 62 /** 63 * Our constructor, sets the initial values 64 * 65 * @return POP3 66 */ 67 function POP3 () 68 { 69 $this->pop_conn = 0; 70 $this->connected = false; 71 $this->error = null; 72 } 73 74 /** 75 * Combination of public events - connect, login, disconnect 76 * 77 * @param string $host 78 * @param integer $port 79 * @param integer $tval 80 * @param string $username 81 * @param string $password 82 */ 83 function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) 84 { 85 $this->host = $host; 86 87 // If no port value is passed, retrieve it 88 if ($port == false) 89 { 90 $this->port = $this->POP3_PORT; 91 } 92 else 93 { 94 $this->port = $port; 95 } 96 97 // If no port value is passed, retrieve it 98 if ($tval == false) 99 { 100 $this->tval = $this->POP3_TIMEOUT; 101 } 102 else 103 { 104 $this->tval = $tval; 105 } 106 107 $this->do_debug = $debug_level; 108 $this->username = $username; 109 $this->password = $password; 110 111 // Refresh the error log 112 $this->error = null; 113 114 // Connect 115 $result = $this->Connect($this->host, $this->port, $this->tval); 116 117 if ($result) 118 { 119 $login_result = $this->Login($this->username, $this->password); 120 121 if ($login_result) 122 { 123 $this->Disconnect(); 124 125 return true; 126 } 127 128 } 129 130 // We need to disconnect regardless if the login succeeded 131 $this->Disconnect(); 132 133 return false; 134 } 135 136 /** 137 * Connect to the POP3 server 138 * 139 * @param string $host 140 * @param integer $port 141 * @param integer $tval 142 * @return boolean 143 */ 144 function Connect ($host, $port = false, $tval = 30) 145 { 146 // Are we already connected? 147 if ($this->connected) 148 { 149 return true; 150 } 151 152 /* 153 On Windows this will raise a PHP Warning error if the hostname doesn't exist. 154 Rather than supress it with @fsockopen, let's capture it cleanly instead 155 */ 156 157 set_error_handler(array(&$this, 'catchWarning')); 158 159 // Connect to the POP3 server 160 $this->pop_conn = fsockopen($host, // POP3 Host 161 $port, // Port # 162 $errno, // Error Number 163 $errstr, // Error Message 164 $tval); // Timeout (seconds) 165 166 // Restore the error handler 167 restore_error_handler(); 168 169 // Does the Error Log now contain anything? 170 if ($this->error && $this->do_debug >= 1) 171 { 172 $this->displayErrors(); 173 } 174 175 // Did we connect? 176 if ($this->pop_conn == false) 177 { 178 // It would appear not... 179 $this->error = array( 180 'error' => "Failed to connect to server $host on port $port", 181 'errno' => $errno, 182 'errstr' => $errstr 183 ); 184 185 if ($this->do_debug >= 1) 186 { 187 $this->displayErrors(); 188 } 189 190 return false; 191 } 192 193 // Increase the stream time-out 194 195 // Check for PHP 4.3.0 or later 196 if (version_compare(phpversion(), '4.3.0', 'ge')) 197 { 198 stream_set_timeout($this->pop_conn, $tval, 0); 199 } 200 else 201 { 202 // Does not work on Windows 203 if (substr(PHP_OS, 0, 3) !== 'WIN') 204 { 205 socket_set_timeout($this->pop_conn, $tval, 0); 206 } 207 } 208 209 // Get the POP3 server response 210 $pop3_response = $this->getResponse(); 211 212 // Check for the +OK 213 if ($this->checkResponse($pop3_response)) 214 { 215 // The connection is established and the POP3 server is talking 216 $this->connected = true; 217 return true; 218 } 219 220 } 221 222 /** 223 * Login to the POP3 server (does not support APOP yet) 224 * 225 * @param string $username 226 * @param string $password 227 * @return boolean 228 */ 229 function Login ($username = '', $password = '') 230 { 231 if ($this->connected == false) 232 { 233 $this->error = 'Not connected to POP3 server'; 234 235 if ($this->do_debug >= 1) 236 { 237 $this->displayErrors(); 238 } 239 } 240 241 if (empty($username)) 242 { 243 $username = $this->username; 244 } 245 246 if (empty($password)) 247 { 248 $password = $this->password; 249 } 250 251 $pop_username = "USER $username" . $this->CRLF; 252 $pop_password = "PASS $password" . $this->CRLF; 253 254 // Send the Username 255 $this->sendString($pop_username); 256 $pop3_response = $this->getResponse(); 257 258 if ($this->checkResponse($pop3_response)) 259 { 260 // Send the Password 261 $this->sendString($pop_password); 262 $pop3_response = $this->getResponse(); 263 264 if ($this->checkResponse($pop3_response)) 265 { 266 return true; 267 } 268 else 269 { 270 return false; 271 } 272 } 273 else 274 { 275 return false; 276 } 277 } 278 279 /** 280 * Disconnect from the POP3 server 281 * 282 */ 283 function Disconnect () 284 { 285 $this->sendString('QUIT'); 286 287 fclose($this->pop_conn); 288 } 289 290 /* 291 --------------- 292 Private Methods 293 --------------- 294 */ 295 296 /** 297 * Get the socket response back. 298 * $size is the maximum number of bytes to retrieve 299 * 300 * @param integer $size 301 * @return string 302 */ 303 function getResponse ($size = 128) 304 { 305 $pop3_response = fgets($this->pop_conn, $size); 306 307 return $pop3_response; 308 } 309 310 /** 311 * Send a string down the open socket connection to the POP3 server 312 * 313 * @param string $string 314 * @return integer 315 */ 316 function sendString ($string) 317 { 318 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string)); 319 320 return $bytes_sent; 321 322 } 323 324 /** 325 * Checks the POP3 server response for +OK or -ERR 326 * 327 * @param string $string 328 * @return boolean 329 */ 330 function checkResponse ($string) 331 { 332 if (substr($string, 0, 3) !== '+OK') 333 { 334 $this->error = array( 335 'error' => "Server reported an error: $string", 336 'errno' => 0, 337 'errstr' => '' 338 ); 339 340 if ($this->do_debug >= 1) 341 { 342 $this->displayErrors(); 343 } 344 345 return false; 346 } 347 else 348 { 349 return true; 350 } 351 352 } 353 354 /** 355 * If debug is enabled, display the error message array 356 * 357 */ 358 function displayErrors () 359 { 360 echo '<pre>'; 361 362 foreach ($this->error as $single_error) 363 { 364 print_r($single_error); 365 } 366 367 echo '</pre>'; 368 } 369 370 /** 371 * Takes over from PHP for the socket warning handler 372 * 373 * @param integer $errno 374 * @param string $errstr 375 * @param string $errfile 376 * @param integer $errline 377 */ 378 function catchWarning ($errno, $errstr, $errfile, $errline) 379 { 380 $this->error[] = array( 381 'error' => "Connecting to the POP3 server raised a PHP warning: ", 382 'errno' => $errno, 383 'errstr' => $errstr 384 ); 385 } 386 387 // End of class 388 } 389 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Apr 1 01:23:32 2007 | par Balluche grâce à PHPXref 0.7 |