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.
17 * $Id: class-pop3.php 8082 2008-06-14 16:36:13Z westi $
23 * @package SquirrelMail
26 var $ERROR = ''; // Error string.
28 var $TIMEOUT = 60; // Default timeout before giving up on a
31 var $COUNT = -1; // Mailbox msg count
33 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
34 // Per RFC 1939 the returned line a POP3
35 // server can send is 512 bytes.
37 var $FP = ''; // The connection to the server's
40 var $MAILSERVER = ''; // Set this to hard code the server name
42 var $DEBUG = FALSE; // set to true to echo pop3
43 // commands and responses to error_log
44 // this WILL log passwords!
46 var $BANNER = ''; // Holds the banner returned by the
47 // pop server - used for apop()
49 var $ALLOWAPOP = FALSE; // Allow or disallow apop()
50 // This must be set to true
53 function POP3 ( $server = '', $timeout = '' ) {
54 settype($this->BUFFER,"integer");
55 if( !empty($server) ) {
56 // Do not allow programs to alter MAILSERVER
57 // if it is already specified. They can get around
58 // this if they -really- want to, so don't count on it.
59 if(empty($this->MAILSERVER))
60 $this->MAILSERVER = $server;
62 if(!empty($timeout)) {
63 settype($timeout,"integer");
64 $this->TIMEOUT = $timeout;
65 if (!ini_get('safe_mode'))
66 set_time_limit($timeout);
71 function update_timer () {
72 if (!ini_get('safe_mode'))
73 set_time_limit($this->TIMEOUT);
77 function connect ($server, $port = 110) {
78 // Opens a socket to the specified server. Unless overridden,
79 // port defaults to 110. Returns true on success, false on fail
81 // If MAILSERVER is set, override $server with it's value
83 if (!isset($port) || !$port) {$port = 110;}
84 if(!empty($this->MAILSERVER))
85 $server = $this->MAILSERVER;
88 $this->ERROR = "POP3 connect: " . _("No server specified");
93 $fp = @fsockopen("$server", $port, $errno, $errstr);
96 $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
101 socket_set_blocking($fp,-1);
102 $this->update_timer();
103 $reply = fgets($fp,$this->BUFFER);
104 $reply = $this->strip_clf($reply);
106 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
107 if(!$this->is_ok($reply)) {
108 $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
113 $this->BANNER = $this->parse_banner($reply);
117 function user ($user = "") {
118 // Sends the USER command, returns true or false
121 $this->ERROR = "POP3 user: " . _("no login ID submitted");
123 } elseif(!isset($this->FP)) {
124 $this->ERROR = "POP3 user: " . _("connection not established");
127 $reply = $this->send_cmd("USER $user");
128 if(!$this->is_ok($reply)) {
129 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
136 function pass ($pass = "") {
137 // Sends the PASS command, returns # of msgs in mailbox,
138 // returns false (undef) on Auth failure
141 $this->ERROR = "POP3 pass: " . _("No password submitted");
143 } elseif(!isset($this->FP)) {
144 $this->ERROR = "POP3 pass: " . _("connection not established");
147 $reply = $this->send_cmd("PASS $pass");
148 if(!$this->is_ok($reply)) {
149 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
154 $count = $this->last("count");
155 $this->COUNT = $count;
161 function apop ($login,$pass) {
162 // Attempts an APOP login. If this fails, it'll
163 // try a standard login. YOUR SERVER MUST SUPPORT
164 // THE USE OF THE APOP COMMAND!
165 // (apop is optional per rfc1939)
167 if(!isset($this->FP)) {
168 $this->ERROR = "POP3 apop: " . _("No connection to server");
170 } elseif(!$this->ALLOWAPOP) {
171 $retVal = $this->login($login,$pass);
173 } elseif(empty($login)) {
174 $this->ERROR = "POP3 apop: " . _("No login ID submitted");
176 } elseif(empty($pass)) {
177 $this->ERROR = "POP3 apop: " . _("No password submitted");
180 $banner = $this->BANNER;
181 if( (!$banner) or (empty($banner)) ) {
182 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
183 $retVal = $this->login($login,$pass);
186 $AuthString = $banner;
187 $AuthString .= $pass;
188 $APOPString = md5($AuthString);
189 $cmd = "APOP $login $APOPString";
190 $reply = $this->send_cmd($cmd);
191 if(!$this->is_ok($reply)) {
192 $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
193 $retVal = $this->login($login,$pass);
197 $count = $this->last("count");
198 $this->COUNT = $count;
205 function login ($login = "", $pass = "") {
206 // Sends both user and pass. Returns # of msgs in mailbox or
207 // false on failure (or -1, if the error occurs while getting
208 // the number of messages.)
210 if( !isset($this->FP) ) {
211 $this->ERROR = "POP3 login: " . _("No connection to server");
215 if( !$this->user( $login ) ) {
216 // Preserve the error generated by user()
219 $count = $this->pass($pass);
220 if( (!$count) || ($count == -1) ) {
221 // Preserve the error generated by last() and pass()
229 function top ($msgNum, $numLines = "0") {
230 // Gets the header and first $numLines of the msg body
231 // returns data in an array with each returned line being
232 // an array element. If $numLines is empty, returns
233 // only the header information, and none of the body.
235 if(!isset($this->FP)) {
236 $this->ERROR = "POP3 top: " . _("No connection to server");
239 $this->update_timer();
242 $buffer = $this->BUFFER;
243 $cmd = "TOP $msgNum $numLines";
244 fwrite($fp, "TOP $msgNum $numLines\r\n");
245 $reply = fgets($fp, $buffer);
246 $reply = $this->strip_clf($reply);
248 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
250 if(!$this->is_ok($reply))
252 $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
259 $line = fgets($fp,$buffer);
260 while ( !ereg("^\.\r\n",$line))
262 $MsgArray[$count] = $line;
264 $line = fgets($fp,$buffer);
265 if(empty($line)) { break; }
271 function pop_list ($msgNum = "") {
272 // If called with an argument, returns that msgs' size in octets
273 // No argument returns an associative array of undeleted
274 // msg numbers and their sizes in octets
276 if(!isset($this->FP))
278 $this->ERROR = "POP3 pop_list: " . _("No connection to server");
282 $Total = $this->COUNT;
283 if( (!$Total) or ($Total == -1) )
289 return array("0","0");
290 // return -1; // mailbox empty
293 $this->update_timer();
297 $cmd = "LIST $msgNum";
298 fwrite($fp,"$cmd\r\n");
299 $reply = fgets($fp,$this->BUFFER);
300 $reply = $this->strip_clf($reply);
302 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
304 if(!$this->is_ok($reply))
306 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
309 list($junk,$num,$size) = preg_split('/\s+/',$reply);
313 $reply = $this->send_cmd($cmd);
314 if(!$this->is_ok($reply))
316 $reply = $this->strip_clf($reply);
317 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
321 $MsgArray[0] = $Total;
322 for($msgC=1;$msgC <= $Total; $msgC++)
324 if($msgC > $Total) { break; }
325 $line = fgets($fp,$this->BUFFER);
326 $line = $this->strip_clf($line);
327 if(ereg("^\.",$line))
329 $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
332 list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
333 settype($thisMsg,"integer");
334 if($thisMsg != $msgC)
336 $MsgArray[$msgC] = "deleted";
340 $MsgArray[$msgC] = $msgSize;
346 function get ($msgNum) {
347 // Retrieve the specified msg number. Returns an array
348 // where each line of the msg is an array element.
350 if(!isset($this->FP))
352 $this->ERROR = "POP3 get: " . _("No connection to server");
356 $this->update_timer();
359 $buffer = $this->BUFFER;
360 $cmd = "RETR $msgNum";
361 $reply = $this->send_cmd($cmd);
363 if(!$this->is_ok($reply))
365 $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
372 $line = fgets($fp,$buffer);
373 while ( !ereg("^\.\r\n",$line))
375 if ( $line{0} == '.' ) { $line = substr($line,1); }
376 $MsgArray[$count] = $line;
378 $line = fgets($fp,$buffer);
379 if(empty($line)) { break; }
384 function last ( $type = "count" ) {
385 // Returns the highest msg number in the mailbox.
386 // returns -1 on error, 0+ on success, if type != count
387 // results in a popstat() call (2 element array returned)
390 if(!isset($this->FP))
392 $this->ERROR = "POP3 last: " . _("No connection to server");
396 $reply = $this->send_cmd("STAT");
397 if(!$this->is_ok($reply))
399 $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
403 $Vars = preg_split('/\s+/',$reply);
406 settype($count,"integer");
407 settype($size,"integer");
410 return array($count,$size);
416 // Resets the status of the remote server. This includes
417 // resetting the status of ALL msgs to not be deleted.
418 // This method automatically closes the connection to the server.
420 if(!isset($this->FP))
422 $this->ERROR = "POP3 reset: " . _("No connection to server");
425 $reply = $this->send_cmd("RSET");
426 if(!$this->is_ok($reply))
428 // The POP3 RSET command -never- gives a -ERR
429 // response - if it ever does, something truely
432 $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
433 @error_log("POP3 reset: ERROR [$reply]",0);
439 function send_cmd ( $cmd = "" )
441 // Sends a user defined command string to the
442 // POP server and returns the results. Useful for
443 // non-compliant or custom POP servers.
444 // Do NOT includ the \r\n as part of your command
445 // string - it will be appended automatically.
447 // The return value is a standard fgets() call, which
448 // will read up to $this->BUFFER bytes of data, until it
449 // encounters a new line, or EOF, whichever happens first.
451 // This method works best if $cmd responds with only
454 if(!isset($this->FP))
456 $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
462 $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
467 $buffer = $this->BUFFER;
468 $this->update_timer();
469 fwrite($fp,"$cmd\r\n");
470 $reply = fgets($fp,$buffer);
471 $reply = $this->strip_clf($reply);
472 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
477 // Closes the connection to the POP3 server, deleting
478 // any msgs marked as deleted.
480 if(!isset($this->FP))
482 $this->ERROR = "POP3 quit: " . _("connection does not exist");
487 fwrite($fp,"$cmd\r\n");
488 $reply = fgets($fp,$this->BUFFER);
489 $reply = $this->strip_clf($reply);
490 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
496 function popstat () {
497 // Returns an array of 2 elements. The number of undeleted
498 // msgs in the mailbox, and the size of the mbox in octets.
500 $PopArray = $this->last("array");
502 if($PopArray == -1) { return false; }
504 if( (!$PopArray) or (empty($PopArray)) )
511 function uidl ($msgNum = "")
513 // Returns the UIDL of the msg specified. If called with
514 // no arguments, returns an associative array where each
515 // undeleted msg num is a key, and the msg's uidl is the element
516 // Array element 0 will contain the total number of msgs
518 if(!isset($this->FP)) {
519 $this->ERROR = "POP3 uidl: " . _("No connection to server");
524 $buffer = $this->BUFFER;
526 if(!empty($msgNum)) {
527 $cmd = "UIDL $msgNum";
528 $reply = $this->send_cmd($cmd);
529 if(!$this->is_ok($reply))
531 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
534 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
537 $this->update_timer();
539 $UIDLArray = array();
540 $Total = $this->COUNT;
541 $UIDLArray[0] = $Total;
548 fwrite($fp, "UIDL\r\n");
549 $reply = fgets($fp, $buffer);
550 $reply = $this->strip_clf($reply);
551 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
552 if(!$this->is_ok($reply))
554 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
560 $line = fgets($fp,$buffer);
561 while ( !ereg("^\.\r\n",$line)) {
562 if(ereg("^\.\r\n",$line)) {
565 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
566 $msgUidl = $this->strip_clf($msgUidl);
568 $UIDLArray[$msg] = $msgUidl;
572 $UIDLArray[$count] = 'deleted';
575 $line = fgets($fp,$buffer);
581 function delete ($msgNum = "") {
582 // Flags a specified msg as deleted. The msg will not
583 // be deleted until a quit() method is called.
585 if(!isset($this->FP))
587 $this->ERROR = "POP3 delete: " . _("No connection to server");
592 $this->ERROR = "POP3 delete: " . _("No msg number submitted");
595 $reply = $this->send_cmd("DELE $msgNum");
596 if(!$this->is_ok($reply))
598 $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
604 // *********************************************************
606 // The following methods are internal to the class.
608 function is_ok ($cmd = "") {
609 // Return true or false on +OK or -ERR
614 return( ereg ("^\+OK", $cmd ) );
617 function strip_clf ($text = "") {
618 // Strips \r\n from server responses
623 $stripped = str_replace("\r",'',$text);
624 $stripped = str_replace("\n",'',$stripped);
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