28daed7c090574dd98ef07a9d34734ae96cc58da
4 * mail_fetch/class.POP3.php
6 * Copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
7 * Modified by Philippe Mingo 2001 mingo@rotedic.com
8 * An RFC 1939 compliant wrapper class for the POP3 protocol.
12 * @copyright © 1999-2005 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16 * @subpackage mail_fetch
20 * This is the POP3 class - DOCUMENT ME
22 * @subpackage mail_fetch
25 var $ERROR = ''; // Error string.
27 var $TIMEOUT = 60; // Default timeout before giving up on a
30 var $COUNT = -1; // Mailbox msg count
32 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
33 // Per RFC 1939 the returned line a POP3
34 // server can send is 512 bytes.
36 var $FP = ''; // The connection to the server's
39 var $MAILSERVER = ''; // Set this to hard code the server name
41 var $DEBUG = FALSE; // set to true to echo pop3
42 // commands and responses to error_log
43 // this WILL log passwords!
45 var $BANNER = ''; // Holds the banner returned by the
46 // pop server - used for apop()
48 var $ALLOWAPOP = FALSE; // Allow or disallow apop()
49 // This must be set to true
52 function POP3 ( $server = '', $timeout = '' ) {
53 settype($this->BUFFER
,"integer");
54 if( !empty($server) ) {
55 // Do not allow programs to alter MAILSERVER
56 // if it is already specified. They can get around
57 // this if they -really- want to, so don't count on it.
58 if(empty($this->MAILSERVER
))
59 $this->MAILSERVER
= $server;
61 if(!empty($timeout)) {
62 settype($timeout,"integer");
63 $this->TIMEOUT
= $timeout;
64 if (!ini_get('safe_mode'))
65 set_time_limit($timeout);
70 function update_timer () {
71 if (!ini_get('safe_mode'))
72 set_time_limit($this->TIMEOUT
);
76 function connect ($server, $port = 110) {
77 // Opens a socket to the specified server. Unless overridden,
78 // port defaults to 110. Returns true on success, false on fail
80 // If MAILSERVER is set, override $server with it's value
82 if (!isset($port) ||
!$port) {$port = 110;}
83 if(!empty($this->MAILSERVER
))
84 $server = $this->MAILSERVER
;
87 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
92 $fp = @fsockopen
("$server", $port, $errno, $errstr);
95 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
100 socket_set_blocking($fp,-1);
101 $this->update_timer();
102 $reply = fgets($fp,$this->BUFFER
);
103 $reply = $this->strip_clf($reply);
105 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
106 if(!$this->is_ok($reply)) {
107 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
112 $this->BANNER
= $this->parse_banner($reply);
116 function user ($user = "") {
117 // Sends the USER command, returns true or false
120 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
122 } elseif(!isset($this->FP
)) {
123 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
126 $reply = $this->send_cmd("USER $user");
127 if(!$this->is_ok($reply)) {
128 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
135 function pass ($pass = "") {
136 // Sends the PASS command, returns # of msgs in mailbox,
137 // returns false (undef) on Auth failure
140 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
142 } elseif(!isset($this->FP
)) {
143 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
146 $reply = $this->send_cmd("PASS $pass");
147 if(!$this->is_ok($reply)) {
148 $this->ERROR
= _("POP3 pass:") . ' ' . _("Authentication failed") . " [$reply]";
153 $count = $this->last("count");
154 $this->COUNT
= $count;
160 function apop ($login,$pass) {
161 // Attempts an APOP login. If this fails, it'll
162 // try a standard login. YOUR SERVER MUST SUPPORT
163 // THE USE OF THE APOP COMMAND!
164 // (apop is optional per rfc1939)
166 if(!isset($this->FP
)) {
167 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
169 } elseif(!$this->ALLOWAPOP
) {
170 $retVal = $this->login($login,$pass);
172 } elseif(empty($login)) {
173 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
175 } elseif(empty($pass)) {
176 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
179 $banner = $this->BANNER
;
180 if( (!$banner) or (empty($banner)) ) {
181 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
182 $retVal = $this->login($login,$pass);
185 $AuthString = $banner;
186 $AuthString .= $pass;
187 $APOPString = md5($AuthString);
188 $cmd = "APOP $login $APOPString";
189 $reply = $this->send_cmd($cmd);
190 if(!$this->is_ok($reply)) {
191 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
192 $retVal = $this->login($login,$pass);
196 $count = $this->last("count");
197 $this->COUNT
= $count;
204 function login ($login = "", $pass = "") {
205 // Sends both user and pass. Returns # of msgs in mailbox or
206 // false on failure (or -1, if the error occurs while getting
207 // the number of messages.)
209 if( !isset($this->FP
) ) {
210 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
214 if( !$this->user( $login ) ) {
215 // Preserve the error generated by user()
218 $count = $this->pass($pass);
219 if( (!$count) ||
($count == -1) ) {
220 // Preserve the error generated by last() and pass()
228 function top ($msgNum, $numLines = "0") {
229 // Gets the header and first $numLines of the msg body
230 // returns data in an array with each returned line being
231 // an array element. If $numLines is empty, returns
232 // only the header information, and none of the body.
234 if(!isset($this->FP
)) {
235 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
238 $this->update_timer();
241 $buffer = $this->BUFFER
;
242 $cmd = "TOP $msgNum $numLines";
243 fwrite($fp, "TOP $msgNum $numLines\r\n");
244 $reply = fgets($fp, $buffer);
245 $reply = $this->strip_clf($reply);
247 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
249 if(!$this->is_ok($reply))
251 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
258 $line = fgets($fp,$buffer);
259 while ( !ereg("^\.\r\n",$line))
261 $MsgArray[$count] = $line;
263 $line = fgets($fp,$buffer);
264 if(empty($line)) { break; }
270 function pop_list ($msgNum = "") {
271 // If called with an argument, returns that msgs' size in octets
272 // No argument returns an associative array of undeleted
273 // msg numbers and their sizes in octets
275 if(!isset($this->FP
))
277 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
281 $Total = $this->COUNT
;
282 if( (!$Total) or ($Total == -1) )
288 return array("0","0");
289 // return -1; // mailbox empty
292 $this->update_timer();
296 $cmd = "LIST $msgNum";
297 fwrite($fp,"$cmd\r\n");
298 $reply = fgets($fp,$this->BUFFER
);
299 $reply = $this->strip_clf($reply);
301 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
303 if(!$this->is_ok($reply))
305 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
308 list($junk,$num,$size) = preg_split('/\s+/',$reply);
312 $reply = $this->send_cmd($cmd);
313 if(!$this->is_ok($reply))
315 $reply = $this->strip_clf($reply);
316 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
320 $MsgArray[0] = $Total;
321 for($msgC=1;$msgC <= $Total; $msgC++
)
323 if($msgC > $Total) { break; }
324 $line = fgets($fp,$this->BUFFER
);
325 $line = $this->strip_clf($line);
326 if(ereg("^\.",$line))
328 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
331 list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
332 settype($thisMsg,"integer");
333 if($thisMsg != $msgC)
335 $MsgArray[$msgC] = "deleted";
339 $MsgArray[$msgC] = $msgSize;
345 function get ($msgNum) {
346 // Retrieve the specified msg number. Returns an array
347 // where each line of the msg is an array element.
349 if(!isset($this->FP
))
351 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
355 $this->update_timer();
358 $buffer = $this->BUFFER
;
359 $cmd = "RETR $msgNum";
360 $reply = $this->send_cmd($cmd);
362 if(!$this->is_ok($reply))
364 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
371 $line = fgets($fp,$buffer);
372 while ( !ereg("^\.\r\n",$line))
374 $MsgArray[$count] = $line;
376 $line = fgets($fp,$buffer);
377 if(empty($line)) { break; }
382 function last ( $type = "count" ) {
383 // Returns the highest msg number in the mailbox.
384 // returns -1 on error, 0+ on success, if type != count
385 // results in a popstat() call (2 element array returned)
388 if(!isset($this->FP
))
390 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
394 $reply = $this->send_cmd("STAT");
395 if(!$this->is_ok($reply))
397 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
401 $Vars = preg_split('/\s+/',$reply);
404 settype($count,"integer");
405 settype($size,"integer");
408 return array($count,$size);
414 // Resets the status of the remote server. This includes
415 // resetting the status of ALL msgs to not be deleted.
416 // This method automatically closes the connection to the server.
418 if(!isset($this->FP
))
420 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
423 $reply = $this->send_cmd("RSET");
424 if(!$this->is_ok($reply))
426 // The POP3 RSET command -never- gives a -ERR
427 // response - if it ever does, something truely
430 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
431 @error_log
("POP3 reset: ERROR [$reply]",0);
437 function send_cmd ( $cmd = "" )
439 // Sends a user defined command string to the
440 // POP server and returns the results. Useful for
441 // non-compliant or custom POP servers.
442 // Do NOT includ the \r\n as part of your command
443 // string - it will be appended automatically.
445 // The return value is a standard fgets() call, which
446 // will read up to $this->BUFFER bytes of data, until it
447 // encounters a new line, or EOF, whichever happens first.
449 // This method works best if $cmd responds with only
452 if(!isset($this->FP
))
454 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
460 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
465 $buffer = $this->BUFFER
;
466 $this->update_timer();
467 fwrite($fp,"$cmd\r\n");
468 $reply = fgets($fp,$buffer);
469 $reply = $this->strip_clf($reply);
470 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
475 // Closes the connection to the POP3 server, deleting
476 // any msgs marked as deleted.
478 if(!isset($this->FP
))
480 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
485 fwrite($fp,"$cmd\r\n");
486 $reply = fgets($fp,$this->BUFFER
);
487 $reply = $this->strip_clf($reply);
488 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
494 function popstat () {
495 // Returns an array of 2 elements. The number of undeleted
496 // msgs in the mailbox, and the size of the mbox in octets.
498 $PopArray = $this->last("array");
500 if($PopArray == -1) { return false; }
502 if( (!$PopArray) or (empty($PopArray)) )
509 function uidl ($msgNum = "")
511 // Returns the UIDL of the msg specified. If called with
512 // no arguments, returns an associative array where each
513 // undeleted msg num is a key, and the msg's uidl is the element
514 // Array element 0 will contain the total number of msgs
516 if(!isset($this->FP
)) {
517 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
522 $buffer = $this->BUFFER
;
524 if(!empty($msgNum)) {
525 $cmd = "UIDL $msgNum";
526 $reply = $this->send_cmd($cmd);
527 if(!$this->is_ok($reply))
529 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
532 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
535 $this->update_timer();
537 $UIDLArray = array();
538 $Total = $this->COUNT
;
539 $UIDLArray[0] = $Total;
546 fwrite($fp, "UIDL\r\n");
547 $reply = fgets($fp, $buffer);
548 $reply = $this->strip_clf($reply);
549 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
550 if(!$this->is_ok($reply))
552 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
558 $line = fgets($fp,$buffer);
559 while ( !ereg("^\.\r\n",$line)) {
560 if(ereg("^\.\r\n",$line)) {
563 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
564 $msgUidl = $this->strip_clf($msgUidl);
566 $UIDLArray[$msg] = $msgUidl;
570 $UIDLArray[$count] = 'deleted';
573 $line = fgets($fp,$buffer);
579 function delete ($msgNum = "") {
580 // Flags a specified msg as deleted. The msg will not
581 // be deleted until a quit() method is called.
583 if(!isset($this->FP
))
585 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
590 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
593 $reply = $this->send_cmd("DELE $msgNum");
594 if(!$this->is_ok($reply))
596 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
602 // *********************************************************
604 // The following methods are internal to the class.
606 function is_ok ($cmd = "") {
607 // Return true or false on +OK or -ERR
612 return( ereg ("^\+OK", $cmd ) );
615 function strip_clf ($text = "") {
616 // Strips \r\n from server responses
621 $stripped = str_replace("\r",'',$text);
622 $stripped = str_replace("\n",'',$stripped);
627 function parse_banner ( $server_text ) {
630 $length = strlen($server_text);
631 for($count =0; $count < $length; $count++
)
633 $digit = substr($server_text,$count,1);
635 if( (!$outside) && ($digit != '<') && ($digit != '>') )
649 $banner = $this->strip_clf($banner); // Just in case