5 * Copyright (c) 1999-2011 CDI (cdi@thewebmasters.net) All Rights Reserved
6 * Modified by Philippe Mingo 2001-2009 mingo@rotedic.com
7 * An RFC 1939 compliant wrapper class for the POP3 protocol.
9 * Licensed under the GNU GPL. For full terms see the file COPYING.
13 * @copyright 1999-2011 The SquirrelMail Project Team
14 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16 * @subpackage mail_fetch
20 var $ERROR = ''; // Error string.
22 var $TIMEOUT = 60; // Default timeout before giving up on a
25 var $COUNT = -1; // Mailbox msg count
27 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
28 // Per RFC 1939 the returned line a POP3
29 // server can send is 512 bytes.
31 var $FP = ''; // The connection to the server's
34 var $MAILSERVER = ''; // Set this to hard code the server name
36 var $DEBUG = FALSE; // set to true to echo pop3
37 // commands and responses to error_log
38 // this WILL log passwords!
40 var $BANNER = ''; // Holds the banner returned by the
41 // pop server - used for apop()
43 var $ALLOWAPOP = FALSE; // Allow or disallow apop()
44 // This must be set to true
50 function __construct ( $server = '', $timeout = '' ) {
51 settype($this->BUFFER,"integer");
52 if( !empty($server) ) {
53 // Do not allow programs to alter MAILSERVER
54 // if it is already specified. They can get around
55 // this if they -really- want to, so don't count on it.
56 if(empty($this->MAILSERVER))
57 $this->MAILSERVER = $server;
59 if(!empty($timeout)) {
60 settype($timeout,"integer");
61 $this->TIMEOUT = $timeout;
62 if (!ini_get('safe_mode'))
63 set_time_limit($timeout);
71 public function POP3( $server = '', $timeout = '' ) {
72 self::__construct( $server, $timeout );
75 function update_timer () {
76 if (!ini_get('safe_mode'))
77 set_time_limit($this->TIMEOUT);
81 function connect ($server, $port = 110) {
82 // Opens a socket to the specified server. Unless overridden,
83 // port defaults to 110. Returns true on success, false on fail
85 // If MAILSERVER is set, override $server with its value.
87 if (!isset($port) || !$port) {$port = 110;}
88 if(!empty($this->MAILSERVER))
89 $server = $this->MAILSERVER;
92 $this->ERROR = "POP3 connect: " . _("No server specified");
97 $fp = @fsockopen("$server", $port, $errno, $errstr);
100 $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
105 socket_set_blocking($fp,-1);
106 $this->update_timer();
107 $reply = fgets($fp,$this->BUFFER);
108 $reply = $this->strip_clf($reply);
110 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
111 if(!$this->is_ok($reply)) {
112 $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
117 $this->BANNER = $this->parse_banner($reply);
121 function user ($user = "") {
122 // Sends the USER command, returns true or false
125 $this->ERROR = "POP3 user: " . _("no login ID submitted");
127 } elseif(!isset($this->FP)) {
128 $this->ERROR = "POP3 user: " . _("connection not established");
131 $reply = $this->send_cmd("USER $user");
132 if(!$this->is_ok($reply)) {
133 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
140 function pass ($pass = "") {
141 // Sends the PASS command, returns # of msgs in mailbox,
142 // returns false (undef) on Auth failure
145 $this->ERROR = "POP3 pass: " . _("No password submitted");
147 } elseif(!isset($this->FP)) {
148 $this->ERROR = "POP3 pass: " . _("connection not established");
151 $reply = $this->send_cmd("PASS $pass");
152 if(!$this->is_ok($reply)) {
153 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
158 $count = $this->last("count");
159 $this->COUNT = $count;
165 function apop ($login,$pass) {
166 // Attempts an APOP login. If this fails, it'll
167 // try a standard login. YOUR SERVER MUST SUPPORT
168 // THE USE OF THE APOP COMMAND!
169 // (apop is optional per rfc1939)
171 if(!isset($this->FP)) {
172 $this->ERROR = "POP3 apop: " . _("No connection to server");
174 } elseif(!$this->ALLOWAPOP) {
175 $retVal = $this->login($login,$pass);
177 } elseif(empty($login)) {
178 $this->ERROR = "POP3 apop: " . _("No login ID submitted");
180 } elseif(empty($pass)) {
181 $this->ERROR = "POP3 apop: " . _("No password submitted");
184 $banner = $this->BANNER;
185 if( (!$banner) or (empty($banner)) ) {
186 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
187 $retVal = $this->login($login,$pass);
190 $AuthString = $banner;
191 $AuthString .= $pass;
192 $APOPString = md5($AuthString);
193 $cmd = "APOP $login $APOPString";
194 $reply = $this->send_cmd($cmd);
195 if(!$this->is_ok($reply)) {
196 $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
197 $retVal = $this->login($login,$pass);
201 $count = $this->last("count");
202 $this->COUNT = $count;
209 function login ($login = "", $pass = "") {
210 // Sends both user and pass. Returns # of msgs in mailbox or
211 // false on failure (or -1, if the error occurs while getting
212 // the number of messages.)
214 if( !isset($this->FP) ) {
215 $this->ERROR = "POP3 login: " . _("No connection to server");
219 if( !$this->user( $login ) ) {
220 // Preserve the error generated by user()
223 $count = $this->pass($pass);
224 if( (!$count) || ($count == -1) ) {
225 // Preserve the error generated by last() and pass()
233 function top ($msgNum, $numLines = "0") {
234 // Gets the header and first $numLines of the msg body
235 // returns data in an array with each returned line being
236 // an array element. If $numLines is empty, returns
237 // only the header information, and none of the body.
239 if(!isset($this->FP)) {
240 $this->ERROR = "POP3 top: " . _("No connection to server");
243 $this->update_timer();
246 $buffer = $this->BUFFER;
247 $cmd = "TOP $msgNum $numLines";
248 fwrite($fp, "TOP $msgNum $numLines\r\n");
249 $reply = fgets($fp, $buffer);
250 $reply = $this->strip_clf($reply);
252 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
254 if(!$this->is_ok($reply))
256 $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
263 $line = fgets($fp,$buffer);
264 while ( !preg_match('/^\.\r\n/',$line))
266 $MsgArray[$count] = $line;
268 $line = fgets($fp,$buffer);
269 if(empty($line)) { break; }
275 function pop_list ($msgNum = "") {
276 // If called with an argument, returns that msgs' size in octets
277 // No argument returns an associative array of undeleted
278 // msg numbers and their sizes in octets
280 if(!isset($this->FP))
282 $this->ERROR = "POP3 pop_list: " . _("No connection to server");
286 $Total = $this->COUNT;
287 if( (!$Total) or ($Total == -1) )
293 return array("0","0");
294 // return -1; // mailbox empty
297 $this->update_timer();
301 $cmd = "LIST $msgNum";
302 fwrite($fp,"$cmd\r\n");
303 $reply = fgets($fp,$this->BUFFER);
304 $reply = $this->strip_clf($reply);
306 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
308 if(!$this->is_ok($reply))
310 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
313 list($junk,$num,$size) = preg_split('/\s+/',$reply);
317 $reply = $this->send_cmd($cmd);
318 if(!$this->is_ok($reply))
320 $reply = $this->strip_clf($reply);
321 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
325 $MsgArray[0] = $Total;
326 for($msgC=1;$msgC <= $Total; $msgC++)
328 if($msgC > $Total) { break; }
329 $line = fgets($fp,$this->BUFFER);
330 $line = $this->strip_clf($line);
331 if(strpos($line, '.') === 0)
333 $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
336 list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
337 settype($thisMsg,"integer");
338 if($thisMsg != $msgC)
340 $MsgArray[$msgC] = "deleted";
344 $MsgArray[$msgC] = $msgSize;
350 function get ($msgNum) {
351 // Retrieve the specified msg number. Returns an array
352 // where each line of the msg is an array element.
354 if(!isset($this->FP))
356 $this->ERROR = "POP3 get: " . _("No connection to server");
360 $this->update_timer();
363 $buffer = $this->BUFFER;
364 $cmd = "RETR $msgNum";
365 $reply = $this->send_cmd($cmd);
367 if(!$this->is_ok($reply))
369 $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
376 $line = fgets($fp,$buffer);
377 while ( !preg_match('/^\.\r\n/',$line))
379 if ( $line{0} == '.' ) { $line = substr($line,1); }
380 $MsgArray[$count] = $line;
382 $line = fgets($fp,$buffer);
383 if(empty($line)) { break; }
388 function last ( $type = "count" ) {
389 // Returns the highest msg number in the mailbox.
390 // returns -1 on error, 0+ on success, if type != count
391 // results in a popstat() call (2 element array returned)
394 if(!isset($this->FP))
396 $this->ERROR = "POP3 last: " . _("No connection to server");
400 $reply = $this->send_cmd("STAT");
401 if(!$this->is_ok($reply))
403 $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
407 $Vars = preg_split('/\s+/',$reply);
410 settype($count,"integer");
411 settype($size,"integer");
414 return array($count,$size);
420 // Resets the status of the remote server. This includes
421 // resetting the status of ALL msgs to not be deleted.
422 // This method automatically closes the connection to the server.
424 if(!isset($this->FP))
426 $this->ERROR = "POP3 reset: " . _("No connection to server");
429 $reply = $this->send_cmd("RSET");
430 if(!$this->is_ok($reply))
432 // The POP3 RSET command -never- gives a -ERR
433 // response - if it ever does, something truely
436 $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
437 @error_log("POP3 reset: ERROR [$reply]",0);
443 function send_cmd ( $cmd = "" )
445 // Sends a user defined command string to the
446 // POP server and returns the results. Useful for
447 // non-compliant or custom POP servers.
448 // Do NOT includ the \r\n as part of your command
449 // string - it will be appended automatically.
451 // The return value is a standard fgets() call, which
452 // will read up to $this->BUFFER bytes of data, until it
453 // encounters a new line, or EOF, whichever happens first.
455 // This method works best if $cmd responds with only
458 if(!isset($this->FP))
460 $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
466 $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
471 $buffer = $this->BUFFER;
472 $this->update_timer();
473 fwrite($fp,"$cmd\r\n");
474 $reply = fgets($fp,$buffer);
475 $reply = $this->strip_clf($reply);
476 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
481 // Closes the connection to the POP3 server, deleting
482 // any msgs marked as deleted.
484 if(!isset($this->FP))
486 $this->ERROR = "POP3 quit: " . _("connection does not exist");
491 fwrite($fp,"$cmd\r\n");
492 $reply = fgets($fp,$this->BUFFER);
493 $reply = $this->strip_clf($reply);
494 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
500 function popstat () {
501 // Returns an array of 2 elements. The number of undeleted
502 // msgs in the mailbox, and the size of the mbox in octets.
504 $PopArray = $this->last("array");
506 if($PopArray == -1) { return false; }
508 if( (!$PopArray) or (empty($PopArray)) )
515 function uidl ($msgNum = "")
517 // Returns the UIDL of the msg specified. If called with
518 // no arguments, returns an associative array where each
519 // undeleted msg num is a key, and the msg's uidl is the element
520 // Array element 0 will contain the total number of msgs
522 if(!isset($this->FP)) {
523 $this->ERROR = "POP3 uidl: " . _("No connection to server");
528 $buffer = $this->BUFFER;
530 if(!empty($msgNum)) {
531 $cmd = "UIDL $msgNum";
532 $reply = $this->send_cmd($cmd);
533 if(!$this->is_ok($reply))
535 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
538 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
541 $this->update_timer();
543 $UIDLArray = array();
544 $Total = $this->COUNT;
545 $UIDLArray[0] = $Total;
552 fwrite($fp, "UIDL\r\n");
553 $reply = fgets($fp, $buffer);
554 $reply = $this->strip_clf($reply);
555 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
556 if(!$this->is_ok($reply))
558 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
564 $line = fgets($fp,$buffer);
565 while ( !preg_match('/^\.\r\n/',$line)) {
566 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
567 $msgUidl = $this->strip_clf($msgUidl);
569 $UIDLArray[$msg] = $msgUidl;
573 $UIDLArray[$count] = 'deleted';
576 $line = fgets($fp,$buffer);
582 function delete ($msgNum = "") {
583 // Flags a specified msg as deleted. The msg will not
584 // be deleted until a quit() method is called.
586 if(!isset($this->FP))
588 $this->ERROR = "POP3 delete: " . _("No connection to server");
593 $this->ERROR = "POP3 delete: " . _("No msg number submitted");
596 $reply = $this->send_cmd("DELE $msgNum");
597 if(!$this->is_ok($reply))
599 $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
605 // *********************************************************
607 // The following methods are internal to the class.
609 function is_ok ($cmd = "") {
610 // Return true or false on +OK or -ERR
615 return( stripos($cmd, '+OK') !== false );
618 function strip_clf ($text = "") {
619 // Strips \r\n from server responses
624 $stripped = str_replace(array("\r","\n"),'',$text);
629 function parse_banner ( $server_text ) {
632 $length = strlen($server_text);
633 for($count =0; $count < $length; $count++)
635 $digit = substr($server_text,$count,1);
637 if( (!$outside) && ($digit != '<') && ($digit != '>') )
651 $banner = $this->strip_clf($banner); // Just in case
657 // For php4 compatibility
658 if (!function_exists("stripos")) {
659 function stripos($haystack, $needle){
660 return strpos($haystack, stristr( $haystack, $needle ));