5 * @package SquirrelMail
7 * @copyright (c) 1999-2006 The SquirrelMail Project Team
9 * @copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
10 * Modified by Philippe Mingo 2001 mingo@rotedic.com
11 * An RFC 1939 compliant wrapper class for the POP3 protocol.
13 * Licensed under the GNU GPL. For full terms see the file COPYING.
14 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
18 * $Id: class-pop3.php 17436 2011-02-09 17:36:23Z ryan $
22 var $ERROR = ''; // Error string.
24 var $TIMEOUT = 60; // Default timeout before giving up on a
27 var $COUNT = -1; // Mailbox msg count
29 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
30 // Per RFC 1939 the returned line a POP3
31 // server can send is 512 bytes.
33 var $FP = ''; // The connection to the server's
36 var $MAILSERVER = ''; // Set this to hard code the server name
38 var $DEBUG = FALSE; // set to true to echo pop3
39 // commands and responses to error_log
40 // this WILL log passwords!
42 var $BANNER = ''; // Holds the banner returned by the
43 // pop server - used for apop()
45 var $ALLOWAPOP = FALSE; // Allow or disallow apop()
46 // This must be set to true
49 function POP3 ( $server = '', $timeout = '' ) {
50 settype($this->BUFFER,"integer");
51 if( !empty($server) ) {
52 // Do not allow programs to alter MAILSERVER
53 // if it is already specified. They can get around
54 // this if they -really- want to, so don't count on it.
55 if(empty($this->MAILSERVER))
56 $this->MAILSERVER = $server;
58 if(!empty($timeout)) {
59 settype($timeout,"integer");
60 $this->TIMEOUT = $timeout;
61 if (!ini_get('safe_mode'))
62 set_time_limit($timeout);
67 function update_timer () {
68 if (!ini_get('safe_mode'))
69 set_time_limit($this->TIMEOUT);
73 function connect ($server, $port = 110) {
74 // Opens a socket to the specified server. Unless overridden,
75 // port defaults to 110. Returns true on success, false on fail
77 // If MAILSERVER is set, override $server with it's value
79 if (!isset($port) || !$port) {$port = 110;}
80 if(!empty($this->MAILSERVER))
81 $server = $this->MAILSERVER;
84 $this->ERROR = "POP3 connect: " . _("No server specified");
89 $fp = @fsockopen("$server", $port, $errno, $errstr);
92 $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
97 socket_set_blocking($fp,-1);
98 $this->update_timer();
99 $reply = fgets($fp,$this->BUFFER);
100 $reply = $this->strip_clf($reply);
102 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
103 if(!$this->is_ok($reply)) {
104 $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
109 $this->BANNER = $this->parse_banner($reply);
113 function user ($user = "") {
114 // Sends the USER command, returns true or false
117 $this->ERROR = "POP3 user: " . _("no login ID submitted");
119 } elseif(!isset($this->FP)) {
120 $this->ERROR = "POP3 user: " . _("connection not established");
123 $reply = $this->send_cmd("USER $user");
124 if(!$this->is_ok($reply)) {
125 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
132 function pass ($pass = "") {
133 // Sends the PASS command, returns # of msgs in mailbox,
134 // returns false (undef) on Auth failure
137 $this->ERROR = "POP3 pass: " . _("No password submitted");
139 } elseif(!isset($this->FP)) {
140 $this->ERROR = "POP3 pass: " . _("connection not established");
143 $reply = $this->send_cmd("PASS $pass");
144 if(!$this->is_ok($reply)) {
145 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
150 $count = $this->last("count");
151 $this->COUNT = $count;
157 function apop ($login,$pass) {
158 // Attempts an APOP login. If this fails, it'll
159 // try a standard login. YOUR SERVER MUST SUPPORT
160 // THE USE OF THE APOP COMMAND!
161 // (apop is optional per rfc1939)
163 if(!isset($this->FP)) {
164 $this->ERROR = "POP3 apop: " . _("No connection to server");
166 } elseif(!$this->ALLOWAPOP) {
167 $retVal = $this->login($login,$pass);
169 } elseif(empty($login)) {
170 $this->ERROR = "POP3 apop: " . _("No login ID submitted");
172 } elseif(empty($pass)) {
173 $this->ERROR = "POP3 apop: " . _("No password submitted");
176 $banner = $this->BANNER;
177 if( (!$banner) or (empty($banner)) ) {
178 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
179 $retVal = $this->login($login,$pass);
182 $AuthString = $banner;
183 $AuthString .= $pass;
184 $APOPString = md5($AuthString);
185 $cmd = "APOP $login $APOPString";
186 $reply = $this->send_cmd($cmd);
187 if(!$this->is_ok($reply)) {
188 $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
189 $retVal = $this->login($login,$pass);
193 $count = $this->last("count");
194 $this->COUNT = $count;
201 function login ($login = "", $pass = "") {
202 // Sends both user and pass. Returns # of msgs in mailbox or
203 // false on failure (or -1, if the error occurs while getting
204 // the number of messages.)
206 if( !isset($this->FP) ) {
207 $this->ERROR = "POP3 login: " . _("No connection to server");
211 if( !$this->user( $login ) ) {
212 // Preserve the error generated by user()
215 $count = $this->pass($pass);
216 if( (!$count) || ($count == -1) ) {
217 // Preserve the error generated by last() and pass()
225 function top ($msgNum, $numLines = "0") {
226 // Gets the header and first $numLines of the msg body
227 // returns data in an array with each returned line being
228 // an array element. If $numLines is empty, returns
229 // only the header information, and none of the body.
231 if(!isset($this->FP)) {
232 $this->ERROR = "POP3 top: " . _("No connection to server");
235 $this->update_timer();
238 $buffer = $this->BUFFER;
239 $cmd = "TOP $msgNum $numLines";
240 fwrite($fp, "TOP $msgNum $numLines\r\n");
241 $reply = fgets($fp, $buffer);
242 $reply = $this->strip_clf($reply);
244 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
246 if(!$this->is_ok($reply))
248 $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
255 $line = fgets($fp,$buffer);
256 while ( !ereg("^\.\r\n",$line))
258 $MsgArray[$count] = $line;
260 $line = fgets($fp,$buffer);
261 if(empty($line)) { break; }
267 function pop_list ($msgNum = "") {
268 // If called with an argument, returns that msgs' size in octets
269 // No argument returns an associative array of undeleted
270 // msg numbers and their sizes in octets
272 if(!isset($this->FP))
274 $this->ERROR = "POP3 pop_list: " . _("No connection to server");
278 $Total = $this->COUNT;
279 if( (!$Total) or ($Total == -1) )
285 return array("0","0");
286 // return -1; // mailbox empty
289 $this->update_timer();
293 $cmd = "LIST $msgNum";
294 fwrite($fp,"$cmd\r\n");
295 $reply = fgets($fp,$this->BUFFER);
296 $reply = $this->strip_clf($reply);
298 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
300 if(!$this->is_ok($reply))
302 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
305 list($junk,$num,$size) = preg_split('/\s+/',$reply);
309 $reply = $this->send_cmd($cmd);
310 if(!$this->is_ok($reply))
312 $reply = $this->strip_clf($reply);
313 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
317 $MsgArray[0] = $Total;
318 for($msgC=1;$msgC <= $Total; $msgC++)
320 if($msgC > $Total) { break; }
321 $line = fgets($fp,$this->BUFFER);
322 $line = $this->strip_clf($line);
323 if(ereg("^\.",$line))
325 $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
328 list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
329 settype($thisMsg,"integer");
330 if($thisMsg != $msgC)
332 $MsgArray[$msgC] = "deleted";
336 $MsgArray[$msgC] = $msgSize;
342 function get ($msgNum) {
343 // Retrieve the specified msg number. Returns an array
344 // where each line of the msg is an array element.
346 if(!isset($this->FP))
348 $this->ERROR = "POP3 get: " . _("No connection to server");
352 $this->update_timer();
355 $buffer = $this->BUFFER;
356 $cmd = "RETR $msgNum";
357 $reply = $this->send_cmd($cmd);
359 if(!$this->is_ok($reply))
361 $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
368 $line = fgets($fp,$buffer);
369 while ( !ereg("^\.\r\n",$line))
371 if ( $line[0] == '.' ) { $line = substr($line,1); }
372 $MsgArray[$count] = $line;
374 $line = fgets($fp,$buffer);
375 if(empty($line)) { break; }
380 function last ( $type = "count" ) {
381 // Returns the highest msg number in the mailbox.
382 // returns -1 on error, 0+ on success, if type != count
383 // results in a popstat() call (2 element array returned)
386 if(!isset($this->FP))
388 $this->ERROR = "POP3 last: " . _("No connection to server");
392 $reply = $this->send_cmd("STAT");
393 if(!$this->is_ok($reply))
395 $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
399 $Vars = preg_split('/\s+/',$reply);
402 settype($count,"integer");
403 settype($size,"integer");
406 return array($count,$size);
412 // Resets the status of the remote server. This includes
413 // resetting the status of ALL msgs to not be deleted.
414 // This method automatically closes the connection to the server.
416 if(!isset($this->FP))
418 $this->ERROR = "POP3 reset: " . _("No connection to server");
421 $reply = $this->send_cmd("RSET");
422 if(!$this->is_ok($reply))
424 // The POP3 RSET command -never- gives a -ERR
425 // response - if it ever does, something truely
428 $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
429 @error_log("POP3 reset: ERROR [$reply]",0);
435 function send_cmd ( $cmd = "" )
437 // Sends a user defined command string to the
438 // POP server and returns the results. Useful for
439 // non-compliant or custom POP servers.
440 // Do NOT includ the \r\n as part of your command
441 // string - it will be appended automatically.
443 // The return value is a standard fgets() call, which
444 // will read up to $this->BUFFER bytes of data, until it
445 // encounters a new line, or EOF, whichever happens first.
447 // This method works best if $cmd responds with only
450 if(!isset($this->FP))
452 $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
458 $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
463 $buffer = $this->BUFFER;
464 $this->update_timer();
465 fwrite($fp,"$cmd\r\n");
466 $reply = fgets($fp,$buffer);
467 $reply = $this->strip_clf($reply);
468 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
473 // Closes the connection to the POP3 server, deleting
474 // any msgs marked as deleted.
476 if(!isset($this->FP))
478 $this->ERROR = "POP3 quit: " . _("connection does not exist");
483 fwrite($fp,"$cmd\r\n");
484 $reply = fgets($fp,$this->BUFFER);
485 $reply = $this->strip_clf($reply);
486 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
492 function popstat () {
493 // Returns an array of 2 elements. The number of undeleted
494 // msgs in the mailbox, and the size of the mbox in octets.
496 $PopArray = $this->last("array");
498 if($PopArray == -1) { return false; }
500 if( (!$PopArray) or (empty($PopArray)) )
507 function uidl ($msgNum = "")
509 // Returns the UIDL of the msg specified. If called with
510 // no arguments, returns an associative array where each
511 // undeleted msg num is a key, and the msg's uidl is the element
512 // Array element 0 will contain the total number of msgs
514 if(!isset($this->FP)) {
515 $this->ERROR = "POP3 uidl: " . _("No connection to server");
520 $buffer = $this->BUFFER;
522 if(!empty($msgNum)) {
523 $cmd = "UIDL $msgNum";
524 $reply = $this->send_cmd($cmd);
525 if(!$this->is_ok($reply))
527 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
530 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
533 $this->update_timer();
535 $UIDLArray = array();
536 $Total = $this->COUNT;
537 $UIDLArray[0] = $Total;
544 fwrite($fp, "UIDL\r\n");
545 $reply = fgets($fp, $buffer);
546 $reply = $this->strip_clf($reply);
547 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
548 if(!$this->is_ok($reply))
550 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
556 $line = fgets($fp,$buffer);
557 while ( !ereg("^\.\r\n",$line)) {
558 if(ereg("^\.\r\n",$line)) {
561 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
562 $msgUidl = $this->strip_clf($msgUidl);
564 $UIDLArray[$msg] = $msgUidl;
568 $UIDLArray[$count] = 'deleted';
571 $line = fgets($fp,$buffer);
577 function delete ($msgNum = "") {
578 // Flags a specified msg as deleted. The msg will not
579 // be deleted until a quit() method is called.
581 if(!isset($this->FP))
583 $this->ERROR = "POP3 delete: " . _("No connection to server");
588 $this->ERROR = "POP3 delete: " . _("No msg number submitted");
591 $reply = $this->send_cmd("DELE $msgNum");
592 if(!$this->is_ok($reply))
594 $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
600 // *********************************************************
602 // The following methods are internal to the class.
604 function is_ok ($cmd = "") {
605 // Return true or false on +OK or -ERR
610 return( ereg ("^\+OK", $cmd ) );
613 function strip_clf ($text = "") {
614 // Strips \r\n from server responses
619 $stripped = str_replace("\r",'',$text);
620 $stripped = str_replace("\n",'',$stripped);
625 function parse_banner ( $server_text ) {
628 $length = strlen($server_text);
629 for($count =0; $count < $length; $count++)
631 $digit = substr($server_text,$count,1);
633 if( (!$outside) && ($digit != '<') && ($digit != '>') )
647 $banner = $this->strip_clf($banner); // Just in case