00001 <?php 00002 /*~ class.pop3.php 00003 .---------------------------------------------------------------------------. 00004 | Software: PHPMailer - PHP email class | 00005 | Version: 5.0.0 | 00006 | Contact: via sourceforge.net support pages (also www.codeworxtech.com) | 00007 | Info: http://phpmailer.sourceforge.net | 00008 | Support: http://sourceforge.net/projects/phpmailer/ | 00009 | ------------------------------------------------------------------------- | 00010 | Admin: Andy Prevost (project admininistrator) | 00011 | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net | 00012 | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net | 00013 | Founder: Brent R. Matzelle (original founder) | 00014 | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. | 00015 | Copyright (c) 2001-2003, Brent R. Matzelle | 00016 | ------------------------------------------------------------------------- | 00017 | License: Distributed under the Lesser General Public License (LGPL) | 00018 | http://www.gnu.org/copyleft/lesser.html | 00019 | This program is distributed in the hope that it will be useful - WITHOUT | 00020 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 00021 | FITNESS FOR A PARTICULAR PURPOSE. | 00022 | ------------------------------------------------------------------------- | 00023 | We offer a number of paid services (www.codeworxtech.com): | 00024 | - Web Hosting on highly optimized fast and secure servers | 00025 | - Technology Consulting | 00026 | - Oursourcing (highly qualified programmers and graphic designers) | 00027 '---------------------------------------------------------------------------' 00028 */ 00029 00062 class POP3 { 00067 public $POP3_PORT = 110; 00068 00073 public $POP3_TIMEOUT = 30; 00074 00079 public $CRLF = "\r\n"; 00080 00085 public $do_debug = 2; 00086 00091 public $host; 00092 00097 public $port; 00098 00103 public $tval; 00104 00109 public $username; 00110 00115 public $password; 00116 00118 // PROPERTIES, PRIVATE AND PROTECTED 00120 00121 private $pop_conn; 00122 private $connected; 00123 private $error; // Error log array 00124 00130 public function __construct() { 00131 $this->pop_conn = 0; 00132 $this->connected = false; 00133 $this->error = null; 00134 } 00135 00145 public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) { 00146 $this->host = $host; 00147 00148 // If no port value is passed, retrieve it 00149 if ($port == false) { 00150 $this->port = $this->POP3_PORT; 00151 } else { 00152 $this->port = $port; 00153 } 00154 00155 // If no port value is passed, retrieve it 00156 if ($tval == false) { 00157 $this->tval = $this->POP3_TIMEOUT; 00158 } else { 00159 $this->tval = $tval; 00160 } 00161 00162 $this->do_debug = $debug_level; 00163 $this->username = $username; 00164 $this->password = $password; 00165 00166 // Refresh the error log 00167 $this->error = null; 00168 00169 // Connect 00170 $result = $this->Connect($this->host, $this->port, $this->tval); 00171 00172 if ($result) { 00173 $login_result = $this->Login($this->username, $this->password); 00174 00175 if ($login_result) { 00176 $this->Disconnect(); 00177 00178 return true; 00179 } 00180 00181 } 00182 00183 // We need to disconnect regardless if the login succeeded 00184 $this->Disconnect(); 00185 00186 return false; 00187 } 00188 00197 public function Connect ($host, $port = false, $tval = 30) { 00198 // Are we already connected? 00199 if ($this->connected) { 00200 return true; 00201 } 00202 00203 /* 00204 On Windows this will raise a PHP Warning error if the hostname doesn't exist. 00205 Rather than supress it with @fsockopen, let's capture it cleanly instead 00206 */ 00207 00208 set_error_handler(array(&$this, 'catchWarning')); 00209 00210 // Connect to the POP3 server 00211 $this->pop_conn = fsockopen($host, // POP3 Host 00212 $port, // Port # 00213 $errno, // Error Number 00214 $errstr, // Error Message 00215 $tval); // Timeout (seconds) 00216 00217 // Restore the error handler 00218 restore_error_handler(); 00219 00220 // Does the Error Log now contain anything? 00221 if ($this->error && $this->do_debug >= 1) { 00222 $this->displayErrors(); 00223 } 00224 00225 // Did we connect? 00226 if ($this->pop_conn == false) { 00227 // It would appear not... 00228 $this->error = array( 00229 'error' => "Failed to connect to server $host on port $port", 00230 'errno' => $errno, 00231 'errstr' => $errstr 00232 ); 00233 00234 if ($this->do_debug >= 1) { 00235 $this->displayErrors(); 00236 } 00237 00238 return false; 00239 } 00240 00241 // Increase the stream time-out 00242 00243 // Check for PHP 4.3.0 or later 00244 if (version_compare(phpversion(), '5.0.0', 'ge')) { 00245 stream_set_timeout($this->pop_conn, $tval, 0); 00246 } else { 00247 // Does not work on Windows 00248 if (substr(PHP_OS, 0, 3) !== 'WIN') { 00249 socket_set_timeout($this->pop_conn, $tval, 0); 00250 } 00251 } 00252 00253 // Get the POP3 server response 00254 $pop3_response = $this->getResponse(); 00255 00256 // Check for the +OK 00257 if ($this->checkResponse($pop3_response)) { 00258 // The connection is established and the POP3 server is talking 00259 $this->connected = true; 00260 return true; 00261 } 00262 00263 } 00264 00272 public function Login ($username = '', $password = '') { 00273 if ($this->connected == false) { 00274 $this->error = 'Not connected to POP3 server'; 00275 00276 if ($this->do_debug >= 1) { 00277 $this->displayErrors(); 00278 } 00279 } 00280 00281 if (empty($username)) { 00282 $username = $this->username; 00283 } 00284 00285 if (empty($password)) { 00286 $password = $this->password; 00287 } 00288 00289 $pop_username = "USER $username" . $this->CRLF; 00290 $pop_password = "PASS $password" . $this->CRLF; 00291 00292 // Send the Username 00293 $this->sendString($pop_username); 00294 $pop3_response = $this->getResponse(); 00295 00296 if ($this->checkResponse($pop3_response)) { 00297 // Send the Password 00298 $this->sendString($pop_password); 00299 $pop3_response = $this->getResponse(); 00300 00301 if ($this->checkResponse($pop3_response)) { 00302 return true; 00303 } else { 00304 return false; 00305 } 00306 } else { 00307 return false; 00308 } 00309 } 00310 00315 public function Disconnect () { 00316 $this->sendString('QUIT'); 00317 00318 fclose($this->pop_conn); 00319 } 00320 00322 // Private Methods 00324 00332 private function getResponse ($size = 128) { 00333 $pop3_response = fgets($this->pop_conn, $size); 00334 00335 return $pop3_response; 00336 } 00337 00344 private function sendString ($string) { 00345 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string)); 00346 00347 return $bytes_sent; 00348 } 00349 00356 private function checkResponse ($string) { 00357 if (substr($string, 0, 3) !== '+OK') { 00358 $this->error = array( 00359 'error' => "Server reported an error: $string", 00360 'errno' => 0, 00361 'errstr' => '' 00362 ); 00363 00364 if ($this->do_debug >= 1) { 00365 $this->displayErrors(); 00366 } 00367 00368 return false; 00369 } else { 00370 return true; 00371 } 00372 00373 } 00374 00379 private function displayErrors () { 00380 echo '<pre>'; 00381 00382 foreach ($this->error as $single_error) { 00383 print_r($single_error); 00384 } 00385 00386 echo '</pre>'; 00387 } 00388 00397 private function catchWarning ($errno, $errstr, $errfile, $errline) { 00398 $this->error[] = array( 00399 'error' => "Connecting to the POP3 server raised a PHP warning: ", 00400 'errno' => $errno, 00401 'errstr' => $errstr 00402 ); 00403 } 00404 00405 // End of class 00406 } 00407 ?>
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
