CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

class.pop3.php
Go to the documentation of this file.
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
59 class POP3 {
64 public $POP3_PORT = 110;
65
70 public $POP3_TIMEOUT = 30;
71
76 public $CRLF = "\r\n";
77
82 public $do_debug = 2;
83
88 public $host;
89
94 public $port;
95
100 public $tval;
101
106 public $username;
107
112 public $password;
113
118 public $Version = '5.2.1';
119
121 // PROPERTIES, PRIVATE AND PROTECTED
123
124 private $pop_conn;
125 private $connected;
126 private $error; // Error log array
127
133 public function __construct() {
134 $this->pop_conn = 0;
135 $this->connected = false;
136 $this->error = null;
137 }
138
148 public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
149 $this->host = $host;
150
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;
156 }
157
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;
163 }
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
184 }
185
186 // We need to disconnect regardless if the login succeeded
187 $this->Disconnect();
188
189 return false;
190 }
191
200 public function Connect ($host, $port = false, $tval = 30) {
201 // Are we already connected?
202 if ($this->connected) {
203 return true;
204 }
205
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 */
210
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();
226 }
227
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 );
236
237 if ($this->do_debug >= 1) {
238 $this->displayErrors();
239 }
240
241 return false;
242 }
243
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 }
254 }
255
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;
264 }
265
266 }
267
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 }
282 }
283
284 if (empty($username)) {
285 $username = $this->username;
286 }
287
288 if (empty($password)) {
289 $password = $this->password;
290 }
291
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;
311 }
312 }
313
318 public function Disconnect () {
319 $this->sendString('QUIT');
320
321 fclose($this->pop_conn);
322 }
323
325 // Private Methods
327
335 private function getResponse ($size = 128) {
336 $pop3_response = fgets($this->pop_conn, $size);
337
338 return $pop3_response;
339 }
340
347 private function sendString ($string) {
348 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
349
350 return $bytes_sent;
351 }
352
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
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
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 }
410 ?>