Project

General

Profile

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