EliteConf
 All Data Structures Namespaces Files Functions Variables Pages
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.1 |
6 | 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 | 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 | 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 */
29 
62 class POP3 {
67  public $POP3_PORT = 110;
68 
73  public $POP3_TIMEOUT = 30;
74 
79  public $CRLF = "\r\n";
80 
85  public $do_debug = 2;
86 
91  public $host;
92 
97  public $port;
98 
103  public $tval;
104 
109  public $username;
110 
115  public $password;
116 
118  // PROPERTIES, PRIVATE AND PROTECTED
120 
121  private $pop_conn;
122  private $connected;
123  private $error; // Error log array
124 
130  public function __construct() {
131  $this->pop_conn = 0;
132  $this->connected = false;
133  $this->error = null;
134  }
135 
145  public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
146  $this->host = $host;
147 
148  // If no port value is passed, retrieve it
149  if ($port == false) {
150  $this->port = $this->POP3_PORT;
151  } else {
152  $this->port = $port;
153  }
154 
155  // If no port value is passed, retrieve it
156  if ($tval == false) {
157  $this->tval = $this->POP3_TIMEOUT;
158  } else {
159  $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  $this->error = null;
168 
169  // Connect
170  $result = $this->Connect($this->host, $this->port, $this->tval);
171 
172  if ($result) {
173  $login_result = $this->Login($this->username, $this->password);
174 
175  if ($login_result) {
176  $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 
197  public function Connect ($host, $port = false, $tval = 30) {
198  // Are we already connected?
199  if ($this->connected) {
200  return true;
201  }
202 
203  /*
204  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  */
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  if ($this->error && $this->do_debug >= 1) {
222  $this->displayErrors();
223  }
224 
225  // Did we connect?
226  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 
234  if ($this->do_debug >= 1) {
235  $this->displayErrors();
236  }
237 
238  return false;
239  }
240 
241  // 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  }
251  }
252 
253  // Get the POP3 server response
254  $pop3_response = $this->getResponse();
255 
256  // 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  }
262 
263  }
264 
272  public function Login ($username = '', $password = '') {
273  if ($this->connected == false) {
274  $this->error = 'Not connected to POP3 server';
275 
276  if ($this->do_debug >= 1) {
277  $this->displayErrors();
278  }
279  }
280 
281  if (empty($username)) {
283  }
284 
285  if (empty($password)) {
287  }
288 
289  $pop_username = "USER $username" . $this->CRLF;
290  $pop_password = "PASS $password" . $this->CRLF;
291 
292  // 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  $pop3_response = $this->getResponse();
300 
301  if ($this->checkResponse($pop3_response)) {
302  return true;
303  } else {
304  return false;
305  }
306  } else {
307  return false;
308  }
309  }
310 
315  public function Disconnect () {
316  $this->sendString('QUIT');
317 
318  fclose($this->pop_conn);
319  }
320 
322  // Private Methods
324 
332  private function getResponse ($size = 128) {
333  $pop3_response = fgets($this->pop_conn, $size);
334 
335  return $pop3_response;
336  }
337 
344  private function sendString ($string) {
345  $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
346 
347  return $bytes_sent;
348  }
349 
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 
364  if ($this->do_debug >= 1) {
365  $this->displayErrors();
366  }
367 
368  return false;
369  } else {
370  return true;
371  }
372 
373  }
374 
379  private function displayErrors () {
380  echo '<pre>';
381 
382  foreach ($this->error as $single_error) {
383  print_r($single_error);
384  }
385 
386  echo '</pre>';
387  }
388 
397  private function catchWarning ($errno, $errstr, $errfile, $errline) {
398  $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 ?>