class.pop3.php

Go to the documentation of this file.
00001 <?php
00002 /*~ class.pop3.php
00003 .---------------------------------------------------------------------------.
00004 |  Software: PHPMailer - PHP email class                                    |
00005 |   Version: 2.1                                                            |
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 |    Author: Andy Prevost (project admininistrator)                         |
00011 |    Author: Brent R. Matzelle (original founder)                           |
00012 | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved.               |
00013 | Copyright (c) 2001-2003, Brent R. Matzelle                                |
00014 | ------------------------------------------------------------------------- |
00015 |   License: Distributed under the Lesser General Public License (LGPL)     |
00016 |            http://www.gnu.org/copyleft/lesser.html                        |
00017 | This program is distributed in the hope that it will be useful - WITHOUT  |
00018 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
00019 | FITNESS FOR A PARTICULAR PURPOSE.                                         |
00020 | ------------------------------------------------------------------------- |
00021 | We offer a number of paid services (www.codeworxtech.com):                |
00022 | - Web Hosting on highly optimized fast and secure servers                 |
00023 | - Technology Consulting                                                   |
00024 | - Oursourcing (highly qualified programmers and graphic designers)        |
00025 '---------------------------------------------------------------------------'
00026 
00048 class POP3 {
00053   public $POP3_PORT = 110;
00054 
00059   public $POP3_TIMEOUT = 30;
00060 
00065   public $CRLF = "\r\n";
00066 
00071   public $do_debug = 2;
00072 
00077   public $host;
00078 
00083   public $port;
00084 
00089   public $tval;
00090 
00095   public $username;
00096 
00101   public $password;
00102 
00106   private $pop_conn;
00107   private $connected;
00108   private $error;     //  Error log array
00116   public function __construct() {
00117     $this->pop_conn  = 0;
00118     $this->connected = false;
00119     $this->error     = null;
00120   }
00121 
00131   public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
00132     $this->host = $host;
00133 
00134     //  If no port value is passed, retrieve it
00135     if ($port == false) {
00136       $this->port = $this->POP3_PORT;
00137     } else {
00138       $this->port = $port;
00139     }
00140 
00141     //  If no port value is passed, retrieve it
00142     if ($tval == false) {
00143       $this->tval = $this->POP3_TIMEOUT;
00144     } else {
00145       $this->tval = $tval;
00146     }
00147 
00148     $this->do_debug = $debug_level;
00149     $this->username = $username;
00150     $this->password = $password;
00151 
00152     //  Refresh the error log
00153     $this->error = null;
00154 
00155     //  Connect
00156     $result = $this->Connect($this->host, $this->port, $this->tval);
00157 
00158     if ($result) {
00159       $login_result = $this->Login($this->username, $this->password);
00160 
00161       if ($login_result) {
00162         $this->Disconnect();
00163 
00164         return true;
00165       }
00166 
00167     }
00168 
00169     //  We need to disconnect regardless if the login succeeded
00170     $this->Disconnect();
00171 
00172     return false;
00173   }
00174 
00183   public function Connect ($host, $port = false, $tval = 30) {
00184     //  Are we already connected?
00185     if ($this->connected) {
00186       return true;
00187     }
00188 
00189     /*
00190     On Windows this will raise a PHP Warning error if the hostname doesn't exist.
00191     Rather than supress it with @fsockopen, let's capture it cleanly instead
00192     */
00193 
00194     set_error_handler(array(&$this, 'catchWarning'));
00195 
00196     //  Connect to the POP3 server
00197     $this->pop_conn = fsockopen($host,    //  POP3 Host
00198                   $port,    //  Port #
00199                   $errno,   //  Error Number
00200                   $errstr,  //  Error Message
00201                   $tval);   //  Timeout (seconds)
00202 
00203     //  Restore the error handler
00204     restore_error_handler();
00205 
00206     //  Does the Error Log now contain anything?
00207     if ($this->error && $this->do_debug >= 1) {
00208       $this->displayErrors();
00209     }
00210 
00211     //  Did we connect?
00212     if ($this->pop_conn == false) {
00213       //  It would appear not...
00214       $this->error = array(
00215         'error' => "Failed to connect to server $host on port $port",
00216         'errno' => $errno,
00217         'errstr' => $errstr
00218       );
00219 
00220       if ($this->do_debug >= 1) {
00221         $this->displayErrors();
00222       }
00223 
00224       return false;
00225     }
00226 
00227     //  Increase the stream time-out
00228 
00229     //  Check for PHP 4.3.0 or later
00230     if (version_compare(phpversion(), '4.3.0', 'ge')) {
00231       stream_set_timeout($this->pop_conn, $tval, 0);
00232     } else {
00233       //  Does not work on Windows
00234       if (substr(PHP_OS, 0, 3) !== 'WIN') {
00235         socket_set_timeout($this->pop_conn, $tval, 0);
00236       }
00237     }
00238 
00239     //  Get the POP3 server response
00240     $pop3_response = $this->getResponse();
00241 
00242     //  Check for the +OK
00243     if ($this->checkResponse($pop3_response)) {
00244     //  The connection is established and the POP3 server is talking
00245     $this->connected = true;
00246       return true;
00247     }
00248 
00249   }
00250 
00258   public function Login ($username = '', $password = '') {
00259     if ($this->connected == false) {
00260       $this->error = 'Not connected to POP3 server';
00261 
00262       if ($this->do_debug >= 1) {
00263         $this->displayErrors();
00264       }
00265     }
00266 
00267     if (empty($username)) {
00268       $username = $this->username;
00269     }
00270 
00271     if (empty($password)) {
00272       $password = $this->password;
00273     }
00274 
00275     $pop_username = "USER $username" . $this->CRLF;
00276     $pop_password = "PASS $password" . $this->CRLF;
00277 
00278     //  Send the Username
00279     $this->sendString($pop_username);
00280     $pop3_response = $this->getResponse();
00281 
00282     if ($this->checkResponse($pop3_response)) {
00283       //  Send the Password
00284       $this->sendString($pop_password);
00285       $pop3_response = $this->getResponse();
00286 
00287       if ($this->checkResponse($pop3_response)) {
00288         return true;
00289       } else {
00290         return false;
00291       }
00292     } else {
00293       return false;
00294     }
00295   }
00296 
00301   public function Disconnect () {
00302     $this->sendString('QUIT');
00303 
00304     fclose($this->pop_conn);
00305   }
00306 
00308   //  Private Methods
00310 
00318   private public function getResponse ($size = 128) {
00319     $pop3_response = fgets($this->pop_conn, $size);
00320 
00321     return $pop3_response;
00322   }
00323 
00330   private function sendString ($string) {
00331     $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
00332 
00333     return $bytes_sent;
00334   }
00335 
00342   private function checkResponse ($string) {
00343     if (substr($string, 0, 3) !== '+OK') {
00344       $this->error = array(
00345         'error' => "Server reported an error: $string",
00346         'errno' => 0,
00347         'errstr' => ''
00348       );
00349 
00350       if ($this->do_debug >= 1) {
00351         $this->displayErrors();
00352       }
00353 
00354       return false;
00355     } else {
00356       return true;
00357     }
00358 
00359   }
00360 
00365   private function displayErrors () {
00366     echo '<pre>';
00367 
00368     foreach ($this->error as $single_error) {
00369       print_r($single_error);
00370     }
00371 
00372     echo '</pre>';
00373   }
00374 
00383   private function catchWarning ($errno, $errstr, $errfile, $errline) {
00384     $this->error[] = array(
00385       'error' => "Connecting to the POP3 server raised a PHP warning: ",
00386       'errno' => $errno,
00387       'errstr' => $errstr
00388     );
00389   }
00390 
00391   //  End of class
00392 }
00393 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6