Project

General

Profile

« Previous | Next » 

Revision 2053

Added by darkviper almost 11 years ago

! update PHPMailer to version Version 5.2.7

View differences:

class.pop3.php
1 1
<?php
2
/*~ class.pop3.php
3
.---------------------------------------------------------------------------.
4
|  Software: PHPMailer - PHP email class                                    |
5
|   Version: 5.2.1                                                          |
6
|      Site: https://code.google.com/a/apache-extras.org/p/phpmailer/       |
7
| ------------------------------------------------------------------------- |
8
|     Admin: Jim Jagielski (project admininistrator)                        |
9
|   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
10
|          : Marcus Bointon (coolbru) coolbru@users.sourceforge.net         |
11
|          : Jim Jagielski (jimjag) jimjag@gmail.com                        |
12
|   Founder: Brent R. Matzelle (original founder)                           |
13
| Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved.              |
14
| Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved.               |
15
| Copyright (c) 2001-2003, Brent R. Matzelle                                |
16
| ------------------------------------------------------------------------- |
17
|   License: Distributed under the Lesser General Public License (LGPL)     |
18
|            http://www.gnu.org/copyleft/lesser.html                        |
19
| This program is distributed in the hope that it will be useful - WITHOUT  |
20
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
21
| FITNESS FOR A PARTICULAR PURPOSE.                                         |
22
'---------------------------------------------------------------------------'
23
*/
24

  
25 2
/**
26
 * PHPMailer - PHP POP Before SMTP Authentication Class
27
 * NOTE: Designed for use with PHP version 5 and up
3
 * PHPMailer POP-Before-SMTP Authentication Class.
4
 * PHP Version 5.0.0
5
 * Version 5.2.7
28 6
 * @package PHPMailer
29
 * @author Andy Prevost
30
 * @author Marcus Bointon
31
 * @author Jim Jagielski
7
 * @link https://github.com/PHPMailer/PHPMailer/
8
 * @author Marcus Bointon (coolbru) <phpmailer@synchromedia.co.uk>
9
 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
10
 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
11
 * @author Brent R. Matzelle (original founder)
12
 * @copyright 2013 Marcus Bointon
32 13
 * @copyright 2010 - 2012 Jim Jagielski
33 14
 * @copyright 2004 - 2009 Andy Prevost
34
 * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
35
 * @version $Id: class.pop3.php 450 2010-06-23 16:46:33Z coolbru $
15
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
16
 * @note This program is distributed in the hope that it will be useful - WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE.
36 19
 */
37 20

  
38 21
/**
39
 * POP Before SMTP Authentication Class
40
 * Version 5.2.1
41
 *
42
 * Author: Richard Davey (rich@corephp.co.uk)
43
 * Modifications: Andy Prevost
44
 * License: LGPL, see PHPMailer License
45
 *
46
 * Specifically for PHPMailer to allow POP before SMTP authentication.
47
 * Does not yet work with APOP - if you have an APOP account, contact Richard Davey
48
 * and we can test changes to this script.
49
 *
50
 * This class is based on the structure of the SMTP class originally authored by Chris Ryan
51
 *
52
 * This class is rfc 1939 compliant and implements all the commands
53
 * required for POP3 connection, authentication and disconnection.
54
 *
22
 * PHPMailer POP-Before-SMTP Authentication Class.
23
 * Specifically for PHPMailer to use for RFC1939 POP-before-SMTP authentication.
24
 * Does not support APOP.
55 25
 * @package PHPMailer
56
 * @author Richard Davey
26
 * @author Richard Davey (original author) <rich@corephp.co.uk>
27
 * @author Marcus Bointon (coolbru) <phpmailer@synchromedia.co.uk>
28
 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
29
 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
57 30
 */
58 31

  
59
class POP3 {
60
  /**
61
   * Default POP3 port
62
   * @var int
63
   */
64
  public $POP3_PORT = 110;
32
class POP3
33
{
34
    /**
35
     * The POP3 PHPMailer Version number.
36
     * @type string
37
     * @access public
38
     */
39
    public $Version = '5.2.7';
65 40

  
66
  /**
67
   * Default Timeout
68
   * @var int
69
   */
70
  public $POP3_TIMEOUT = 30;
41
    /**
42
     * Default POP3 port number.
43
     * @type int
44
     * @access public
45
     */
46
    public $POP3_PORT = 110;
71 47

  
72
  /**
73
   * POP3 Carriage Return + Line Feed
74
   * @var string
75
   */
76
  public $CRLF = "\r\n";
48
    /**
49
     * Default timeout in seconds.
50
     * @type int
51
     * @access public
52
     */
53
    public $POP3_TIMEOUT = 30;
77 54

  
78
  /**
79
   * Displaying Debug warnings? (0 = now, 1+ = yes)
80
   * @var int
81
   */
82
  public $do_debug = 2;
55
    /**
56
     * POP3 Carriage Return + Line Feed.
57
     * @type string
58
     * @access public
59
     * @deprecated Use the constant instead
60
     */
61
    public $CRLF = "\r\n";
83 62

  
84
  /**
85
   * POP3 Mail Server
86
   * @var string
87
   */
88
  public $host;
63
    /**
64
     * Debug display level.
65
     * Options: 0 = no, 1+ = yes
66
     * @type int
67
     * @access public
68
     */
69
    public $do_debug = 0;
89 70

  
90
  /**
91
   * POP3 Port
92
   * @var int
93
   */
94
  public $port;
71
    /**
72
     * POP3 mail server hostname.
73
     * @type string
74
     * @access public
75
     */
76
    public $host;
95 77

  
96
  /**
97
   * POP3 Timeout Value
98
   * @var int
99
   */
100
  public $tval;
78
    /**
79
     * POP3 port number.
80
     * @type int
81
     * @access public
82
     */
83
    public $port;
101 84

  
102
  /**
103
   * POP3 Username
104
   * @var string
105
   */
106
  public $username;
85
    /**
86
     * POP3 Timeout Value in seconds.
87
     * @type int
88
     * @access public
89
     */
90
    public $tval;
107 91

  
108
  /**
109
   * POP3 Password
110
   * @var string
111
   */
112
  public $password;
92
    /**
93
     * POP3 username
94
     * @type string
95
     * @access public
96
     */
97
    public $username;
113 98

  
114
  /**
115
   * Sets the POP3 PHPMailer Version number
116
   * @var string
117
   */
118
  public $Version         = '5.2.1';
99
    /**
100
     * POP3 password.
101
     * @type string
102
     * @access public
103
     */
104
    public $password;
119 105

  
120
  /////////////////////////////////////////////////
121
  // PROPERTIES, PRIVATE AND PROTECTED
122
  /////////////////////////////////////////////////
106
    /**
107
     * Resource handle for the POP3 connection socket.
108
     * @type resource
109
     * @access private
110
     */
111
    private $pop_conn;
123 112

  
124
  private $pop_conn;
125
  private $connected;
126
  private $error;     //  Error log array
113
    /**
114
     * Are we connected?
115
     * @type bool
116
     * @access private
117
     */
118
    private $connected;
127 119

  
128
  /**
129
   * Constructor, sets the initial values
130
   * @access public
131
   * @return POP3
132
   */
133
  public function __construct() {
134
    $this->pop_conn  = 0;
135
    $this->connected = false;
136
    $this->error     = null;
137
  }
120
    /**
121
     * Error container.
122
     * @type array
123
     * @access private
124
     */
125
    private $error;
138 126

  
139
  /**
140
   * Combination of public events - connect, login, disconnect
141
   * @access public
142
   * @param string $host
143
   * @param integer $port
144
   * @param integer $tval
145
   * @param string $username
146
   * @param string $password
147
   */
148
  public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
149
    $this->host = $host;
127
    /**
128
     * Line break constant
129
     */
130
    const CRLF = "\r\n";
150 131

  
151
    //  If no port value is passed, retrieve it
152
    if ($port == false) {
153
      $this->port = $this->POP3_PORT;
154
    } else {
155
      $this->port = $port;
132
    /**
133
     * Constructor.
134
     * @access public
135
     */
136
    public function __construct()
137
    {
138
        $this->pop_conn = 0;
139
        $this->connected = false;
140
        $this->error = null;
156 141
    }
157 142

  
158
    //  If no port value is passed, retrieve it
159
    if ($tval == false) {
160
      $this->tval = $this->POP3_TIMEOUT;
161
    } else {
162
      $this->tval = $tval;
143
    /**
144
     * Simple static wrapper for all-in-one POP before SMTP
145
     * @param $host
146
     * @param bool $port
147
     * @param bool $tval
148
     * @param string $username
149
     * @param string $password
150
     * @param int $debug_level
151
     * @return bool
152
     */
153
    public static function popBeforeSmtp(
154
        $host,
155
        $port = false,
156
        $tval = false,
157
        $username = '',
158
        $password = '',
159
        $debug_level = 0
160
    ) {
161
        $pop = new POP3;
162
        return $pop->authorise($host, $port, $tval, $username, $password, $debug_level);
163 163
    }
164 164

  
165
    $this->do_debug = $debug_level;
166
    $this->username = $username;
167
    $this->password = $password;
168

  
169
    //  Refresh the error log
170
    $this->error = null;
171

  
172
    //  Connect
173
    $result = $this->Connect($this->host, $this->port, $this->tval);
174

  
175
    if ($result) {
176
      $login_result = $this->Login($this->username, $this->password);
177

  
178
      if ($login_result) {
179
        $this->Disconnect();
180

  
181
        return true;
182
      }
183

  
165
    /**
166
     * Authenticate with a POP3 server.
167
     * A connect, login, disconnect sequence
168
     * appropriate for POP-before SMTP authorisation.
169
     * @access public
170
     * @param string $host
171
     * @param bool|int $port
172
     * @param bool|int $tval
173
     * @param string $username
174
     * @param string $password
175
     * @param int $debug_level
176
     * @return bool
177
     */
178
    public function authorise($host, $port = false, $tval = false, $username = '', $password = '', $debug_level = 0)
179
    {
180
        $this->host = $host;
181
        // If no port value provided, use default
182
        if ($port === false) {
183
            $this->port = $this->POP3_PORT;
184
        } else {
185
            $this->port = $port;
186
        }
187
        // If no timeout value provided, use default
188
        if ($tval === false) {
189
            $this->tval = $this->POP3_TIMEOUT;
190
        } else {
191
            $this->tval = $tval;
192
        }
193
        $this->do_debug = $debug_level;
194
        $this->username = $username;
195
        $this->password = $password;
196
        //  Refresh the error log
197
        $this->error = null;
198
        //  connect
199
        $result = $this->connect($this->host, $this->port, $this->tval);
200
        if ($result) {
201
            $login_result = $this->login($this->username, $this->password);
202
            if ($login_result) {
203
                $this->disconnect();
204
                return true;
205
            }
206
        }
207
        // We need to disconnect regardless of whether the login succeeded
208
        $this->disconnect();
209
        return false;
184 210
    }
185 211

  
186
    //  We need to disconnect regardless if the login succeeded
187
    $this->Disconnect();
212
    /**
213
     * Connect to a POP3 server.
214
     * @access public
215
     * @param string $host
216
     * @param bool|int $port
217
     * @param integer $tval
218
     * @return boolean
219
     */
220
    public function connect($host, $port = false, $tval = 30)
221
    {
222
        //  Are we already connected?
223
        if ($this->connected) {
224
            return true;
225
        }
188 226

  
189
    return false;
190
  }
227
        //On Windows this will raise a PHP Warning error if the hostname doesn't exist.
228
        //Rather than suppress it with @fsockopen, capture it cleanly instead
229
        set_error_handler(array($this, 'catchWarning'));
191 230

  
192
  /**
193
   * Connect to the POP3 server
194
   * @access public
195
   * @param string $host
196
   * @param integer $port
197
   * @param integer $tval
198
   * @return boolean
199
   */
200
  public function Connect ($host, $port = false, $tval = 30) {
201
    //  Are we already connected?
202
    if ($this->connected) {
203
      return true;
204
    }
231
        //  connect to the POP3 server
232
        $this->pop_conn = fsockopen(
233
            $host, //  POP3 Host
234
            $port, //  Port #
235
            $errno, //  Error Number
236
            $errstr, //  Error Message
237
            $tval
238
        ); //  Timeout (seconds)
239
        //  Restore the error handler
240
        restore_error_handler();
241
        //  Does the Error Log now contain anything?
242
        if ($this->error && $this->do_debug >= 1) {
243
            $this->displayErrors();
244
        }
245
        //  Did we connect?
246
        if ($this->pop_conn == false) {
247
            //  It would appear not...
248
            $this->error = array(
249
                'error' => "Failed to connect to server $host on port $port",
250
                'errno' => $errno,
251
                'errstr' => $errstr
252
            );
253
            if ($this->do_debug >= 1) {
254
                $this->displayErrors();
255
            }
256
            return false;
257
        }
205 258

  
206
    /*
207
    On Windows this will raise a PHP Warning error if the hostname doesn't exist.
208
    Rather than supress it with @fsockopen, let's capture it cleanly instead
209
    */
259
        //  Increase the stream time-out
260
        //  Check for PHP 4.3.0 or later
261
        if (version_compare(phpversion(), '5.0.0', 'ge')) {
262
            stream_set_timeout($this->pop_conn, $tval, 0);
263
        } else {
264
            //  Does not work on Windows
265
            if (substr(PHP_OS, 0, 3) !== 'WIN') {
266
                socket_set_timeout($this->pop_conn, $tval, 0);
267
            }
268
        }
210 269

  
211
    set_error_handler(array(&$this, 'catchWarning'));
212

  
213
    //  Connect to the POP3 server
214
    $this->pop_conn = fsockopen($host,    //  POP3 Host
215
                  $port,    //  Port #
216
                  $errno,   //  Error Number
217
                  $errstr,  //  Error Message
218
                  $tval);   //  Timeout (seconds)
219

  
220
    //  Restore the error handler
221
    restore_error_handler();
222

  
223
    //  Does the Error Log now contain anything?
224
    if ($this->error && $this->do_debug >= 1) {
225
      $this->displayErrors();
270
        //  Get the POP3 server response
271
        $pop3_response = $this->getResponse();
272
        //  Check for the +OK
273
        if ($this->checkResponse($pop3_response)) {
274
            //  The connection is established and the POP3 server is talking
275
            $this->connected = true;
276
            return true;
277
        }
278
        return false;
226 279
    }
227 280

  
228
    //  Did we connect?
229
    if ($this->pop_conn == false) {
230
      //  It would appear not...
231
      $this->error = array(
232
        'error' => "Failed to connect to server $host on port $port",
233
        'errno' => $errno,
234
        'errstr' => $errstr
235
      );
281
    /**
282
     * Log in to the POP3 server.
283
     * Does not support APOP (RFC 2828, 4949).
284
     * @access public
285
     * @param string $username
286
     * @param string $password
287
     * @return boolean
288
     */
289
    public function login($username = '', $password = '')
290
    {
291
        if ($this->connected == false) {
292
            $this->error = 'Not connected to POP3 server';
236 293

  
237
      if ($this->do_debug >= 1) {
238
        $this->displayErrors();
239
      }
294
            if ($this->do_debug >= 1) {
295
                $this->displayErrors();
296
            }
297
        }
298
        if (empty($username)) {
299
            $username = $this->username;
300
        }
301
        if (empty($password)) {
302
            $password = $this->password;
303
        }
240 304

  
241
      return false;
305
        // Send the Username
306
        $this->sendString("USER $username" . self::CRLF);
307
        $pop3_response = $this->getResponse();
308
        if ($this->checkResponse($pop3_response)) {
309
            // Send the Password
310
            $this->sendString("PASS $password" . self::CRLF);
311
            $pop3_response = $this->getResponse();
312
            if ($this->checkResponse($pop3_response)) {
313
                return true;
314
            }
315
        }
316
        return false;
242 317
    }
243 318

  
244
    //  Increase the stream time-out
245

  
246
    //  Check for PHP 4.3.0 or later
247
    if (version_compare(phpversion(), '5.0.0', 'ge')) {
248
      stream_set_timeout($this->pop_conn, $tval, 0);
249
    } else {
250
      //  Does not work on Windows
251
      if (substr(PHP_OS, 0, 3) !== 'WIN') {
252
        socket_set_timeout($this->pop_conn, $tval, 0);
253
      }
319
    /**
320
     * Disconnect from the POP3 server.
321
     * @access public
322
     */
323
    public function disconnect()
324
    {
325
        $this->sendString('QUIT');
326
        //The QUIT command may cause the daemon to exit, which will kill our connection
327
        //So ignore errors here
328
        @fclose($this->pop_conn);
254 329
    }
255 330

  
256
    //  Get the POP3 server response
257
    $pop3_response = $this->getResponse();
258

  
259
    //  Check for the +OK
260
    if ($this->checkResponse($pop3_response)) {
261
    //  The connection is established and the POP3 server is talking
262
    $this->connected = true;
263
      return true;
331
    /**
332
     * Get a response from the POP3 server.
333
     * $size is the maximum number of bytes to retrieve
334
     * @param integer $size
335
     * @return string
336
     * @access private
337
     */
338
    private function getResponse($size = 128)
339
    {
340
        $r = fgets($this->pop_conn, $size);
341
        if ($this->do_debug >= 1) {
342
            echo "Server -> Client: $r";
343
        }
344
        return $r;
264 345
    }
265 346

  
266
  }
267

  
268
  /**
269
   * Login to the POP3 server (does not support APOP yet)
270
   * @access public
271
   * @param string $username
272
   * @param string $password
273
   * @return boolean
274
   */
275
  public function Login ($username = '', $password = '') {
276
    if ($this->connected == false) {
277
      $this->error = 'Not connected to POP3 server';
278

  
279
      if ($this->do_debug >= 1) {
280
        $this->displayErrors();
281
      }
347
    /**
348
     * Send raw data to the POP3 server.
349
     * @param string $string
350
     * @return integer
351
     * @access private
352
     */
353
    private function sendString($string)
354
    {
355
        if ($this->pop_conn) {
356
            if ($this->do_debug >= 2) { //Show client messages when debug >= 2
357
                echo "Client -> Server: $string";
358
            }
359
            return fwrite($this->pop_conn, $string, strlen($string));
360
        }
361
        return 0;
282 362
    }
283 363

  
284
    if (empty($username)) {
285
      $username = $this->username;
364
    /**
365
     * Checks the POP3 server response.
366
     * Looks for for +OK or -ERR.
367
     * @param string $string
368
     * @return boolean
369
     * @access private
370
     */
371
    private function checkResponse($string)
372
    {
373
        if (substr($string, 0, 3) !== '+OK') {
374
            $this->error = array(
375
                'error' => "Server reported an error: $string",
376
                'errno' => 0,
377
                'errstr' => ''
378
            );
379
            if ($this->do_debug >= 1) {
380
                $this->displayErrors();
381
            }
382
            return false;
383
        } else {
384
            return true;
385
        }
286 386
    }
287 387

  
288
    if (empty($password)) {
289
      $password = $this->password;
388
    /**
389
     * Display errors if debug is enabled.
390
     * @access private
391
     */
392
    private function displayErrors()
393
    {
394
        echo '<pre>';
395
        foreach ($this->error as $single_error) {
396
            print_r($single_error);
397
        }
398
        echo '</pre>';
290 399
    }
291 400

  
292
    $pop_username = "USER $username" . $this->CRLF;
293
    $pop_password = "PASS $password" . $this->CRLF;
294

  
295
    //  Send the Username
296
    $this->sendString($pop_username);
297
    $pop3_response = $this->getResponse();
298

  
299
    if ($this->checkResponse($pop3_response)) {
300
      //  Send the Password
301
      $this->sendString($pop_password);
302
      $pop3_response = $this->getResponse();
303

  
304
      if ($this->checkResponse($pop3_response)) {
305
        return true;
306
      } else {
307
        return false;
308
      }
309
    } else {
310
      return false;
401
    /**
402
     * POP3 connection error handler.
403
     * @param integer $errno
404
     * @param string $errstr
405
     * @param string $errfile
406
     * @param integer $errline
407
     * @access private
408
     */
409
    private function catchWarning($errno, $errstr, $errfile, $errline)
410
    {
411
        $this->error[] = array(
412
            'error' => "Connecting to the POP3 server raised a PHP warning: ",
413
            'errno' => $errno,
414
            'errstr' => $errstr,
415
            'errfile' => $errfile,
416
            'errline' => $errline
417
        );
311 418
    }
312
  }
313

  
314
  /**
315
   * Disconnect from the POP3 server
316
   * @access public
317
   */
318
  public function Disconnect () {
319
    $this->sendString('QUIT');
320

  
321
    fclose($this->pop_conn);
322
  }
323

  
324
  /////////////////////////////////////////////////
325
  //  Private Methods
326
  /////////////////////////////////////////////////
327

  
328
  /**
329
   * Get the socket response back.
330
   * $size is the maximum number of bytes to retrieve
331
   * @access private
332
   * @param integer $size
333
   * @return string
334
   */
335
  private function getResponse ($size = 128) {
336
    $pop3_response = fgets($this->pop_conn, $size);
337

  
338
    return $pop3_response;
339
  }
340

  
341
  /**
342
   * Send a string down the open socket connection to the POP3 server
343
   * @access private
344
   * @param string $string
345
   * @return integer
346
   */
347
  private function sendString ($string) {
348
    $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
349

  
350
    return $bytes_sent;
351
  }
352

  
353
  /**
354
   * Checks the POP3 server response for +OK or -ERR
355
   * @access private
356
   * @param string $string
357
   * @return boolean
358
   */
359
  private function checkResponse ($string) {
360
    if (substr($string, 0, 3) !== '+OK') {
361
      $this->error = array(
362
        'error' => "Server reported an error: $string",
363
        'errno' => 0,
364
        'errstr' => ''
365
      );
366

  
367
      if ($this->do_debug >= 1) {
368
        $this->displayErrors();
369
      }
370

  
371
      return false;
372
    } else {
373
      return true;
374
    }
375

  
376
  }
377

  
378
  /**
379
   * If debug is enabled, display the error message array
380
   * @access private
381
   */
382
  private function displayErrors () {
383
    echo '<pre>';
384

  
385
    foreach ($this->error as $single_error) {
386
      print_r($single_error);
387
    }
388

  
389
    echo '</pre>';
390
  }
391

  
392
  /**
393
   * Takes over from PHP for the socket warning handler
394
   * @access private
395
   * @param integer $errno
396
   * @param string $errstr
397
   * @param string $errfile
398
   * @param integer $errline
399
   */
400
  private function catchWarning ($errno, $errstr, $errfile, $errline) {
401
    $this->error[] = array(
402
      'error' => "Connecting to the POP3 server raised a PHP warning: ",
403
      'errno' => $errno,
404
      'errstr' => $errstr
405
    );
406
  }
407

  
408
  //  End of class
409 419
}
410
?>

Also available in: Unified diff