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