6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
8 * Copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
9 * Modified by Philippe Mingo 2001 mingo@rotedic.com
10 * An RFC 1939 compliant wrapper class for the POP3 protocol.
12 * Licensed under the GNU GPL. For full terms see the file COPYING.
18 * @subpackage mail_fetch
22 * This is the pop3 class - DOCUMENT ME
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 set_time_limit($timeout);
70 function update_timer () {
71 set_time_limit($this->TIMEOUT
);
75 function connect ($server, $port = 110) {
76 // Opens a socket to the specified server. Unless overridden,
77 // port defaults to 110. Returns true on success, false on fail
79 // If MAILSERVER is set, override $server with it's value
81 if (!isset($port) ||
!$port) {$port = 110;}
82 if(!empty($this->MAILSERVER
))
83 $server = $this->MAILSERVER
;
86 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
91 $fp = fsockopen("$server", $port, $errno, $errstr);
94 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
99 socket_set_blocking($fp,-1);
100 $this->update_timer();
101 $reply = fgets($fp,$this->BUFFER
);
102 $reply = $this->strip_clf($reply);
104 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
105 if(!$this->is_ok($reply)) {
106 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
111 $this->BANNER
= $this->parse_banner($reply);
115 function user ($user = "") {
116 // Sends the USER command, returns true or false
119 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
121 } elseif(!isset($this->FP
)) {
122 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
125 $reply = $this->send_cmd("USER $user");
126 if(!$this->is_ok($reply)) {
127 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
134 function pass ($pass = "") {
135 // Sends the PASS command, returns # of msgs in mailbox,
136 // returns false (undef) on Auth failure
139 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
141 } elseif(!isset($this->FP
)) {
142 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
145 $reply = $this->send_cmd("PASS $pass");
146 if(!$this->is_ok($reply)) {
147 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
152 $count = $this->last("count");
153 $this->COUNT
= $count;
159 function apop ($login,$pass) {
160 // Attempts an APOP login. If this fails, it'll
161 // try a standard login. YOUR SERVER MUST SUPPORT
162 // THE USE OF THE APOP COMMAND!
163 // (apop is optional per rfc1939)
165 if(!isset($this->FP
)) {
166 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
168 } elseif(!$this->ALLOWAPOP
) {
169 $retVal = $this->login($login,$pass);
171 } elseif(empty($login)) {
172 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
174 } elseif(empty($pass)) {
175 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
178 $banner = $this->BANNER
;
179 if( (!$banner) or (empty($banner)) ) {
180 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
181 $retVal = $this->login($login,$pass);
184 $AuthString = $banner;
185 $AuthString .= $pass;
186 $APOPString = md5($AuthString);
187 $cmd = "APOP $login $APOPString";
188 $reply = $this->send_cmd($cmd);
189 if(!$this->is_ok($reply)) {
190 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
191 $retVal = $this->login($login,$pass);
195 $count = $this->last("count");
196 $this->COUNT
= $count;
203 function login ($login = "", $pass = "") {
204 // Sends both user and pass. Returns # of msgs in mailbox or
205 // false on failure (or -1, if the error occurs while getting
206 // the number of messages.)
208 if( !isset($this->FP
) ) {
209 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
213 if( !$this->user( $login ) ) {
214 // Preserve the error generated by user()
217 $count = $this->pass($pass);
218 if( (!$count) ||
($count == -1) ) {
219 // Preserve the error generated by last() and pass()
227 function top ($msgNum, $numLines = "0") {
228 // Gets the header and first $numLines of the msg body
229 // returns data in an array with each returned line being
230 // an array element. If $numLines is empty, returns
231 // only the header information, and none of the body.
233 if(!isset($this->FP
)) {
234 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
237 $this->update_timer();
240 $buffer = $this->BUFFER
;
241 $cmd = "TOP $msgNum $numLines";
242 fwrite($fp, "TOP $msgNum $numLines\r\n");
243 $reply = fgets($fp, $buffer);
244 $reply = $this->strip_clf($reply);
246 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
248 if(!$this->is_ok($reply))
250 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
257 $line = fgets($fp,$buffer);
258 while ( !ereg("^\.\r\n",$line))
260 $MsgArray[$count] = $line;
262 $line = fgets($fp,$buffer);
263 if(empty($line)) { break; }
269 function pop_list ($msgNum = "") {
270 // If called with an argument, returns that msgs' size in octets
271 // No argument returns an associative array of undeleted
272 // msg numbers and their sizes in octets
274 if(!isset($this->FP
))
276 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
280 $Total = $this->COUNT
;
281 if( (!$Total) or ($Total == -1) )
287 return array("0","0");
288 // return -1; // mailbox empty
291 $this->update_timer();
295 $cmd = "LIST $msgNum";
296 fwrite($fp,"$cmd\r\n");
297 $reply = fgets($fp,$this->BUFFER
);
298 $reply = $this->strip_clf($reply);
300 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
302 if(!$this->is_ok($reply))
304 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
307 list($junk,$num,$size) = explode(" ",$reply);
311 $reply = $this->send_cmd($cmd);
312 if(!$this->is_ok($reply))
314 $reply = $this->strip_clf($reply);
315 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
319 $MsgArray[0] = $Total;
320 for($msgC=1;$msgC <= $Total; $msgC++
)
322 if($msgC > $Total) { break; }
323 $line = fgets($fp,$this->BUFFER
);
324 $line = $this->strip_clf($line);
325 if(ereg("^\.",$line))
327 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
330 list($thisMsg,$msgSize) = explode(" ",$line);
331 settype($thisMsg,"integer");
332 if($thisMsg != $msgC)
334 $MsgArray[$msgC] = "deleted";
338 $MsgArray[$msgC] = $msgSize;
344 function get ($msgNum) {
345 // Retrieve the specified msg number. Returns an array
346 // where each line of the msg is an array element.
348 if(!isset($this->FP
))
350 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
354 $this->update_timer();
357 $buffer = $this->BUFFER
;
358 $cmd = "RETR $msgNum";
359 $reply = $this->send_cmd($cmd);
361 if(!$this->is_ok($reply))
363 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
370 $line = fgets($fp,$buffer);
371 while ( !ereg("^\.\r\n",$line))
373 $MsgArray[$count] = $line;
375 $line = fgets($fp,$buffer);
376 if(empty($line)) { break; }
381 function last ( $type = "count" ) {
382 // Returns the highest msg number in the mailbox.
383 // returns -1 on error, 0+ on success, if type != count
384 // results in a popstat() call (2 element array returned)
387 if(!isset($this->FP
))
389 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
393 $reply = $this->send_cmd("STAT");
394 if(!$this->is_ok($reply))
396 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
400 $Vars = explode(" ",$reply);
403 settype($count,"integer");
404 settype($size,"integer");
407 return array($count,$size);
413 // Resets the status of the remote server. This includes
414 // resetting the status of ALL msgs to not be deleted.
415 // This method automatically closes the connection to the server.
417 if(!isset($this->FP
))
419 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
422 $reply = $this->send_cmd("RSET");
423 if(!$this->is_ok($reply))
425 // The POP3 RSET command -never- gives a -ERR
426 // response - if it ever does, something truely
429 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
430 @error_log
("POP3 reset: ERROR [$reply]",0);
436 function send_cmd ( $cmd = "" )
438 // Sends a user defined command string to the
439 // POP server and returns the results. Useful for
440 // non-compliant or custom POP servers.
441 // Do NOT includ the \r\n as part of your command
442 // string - it will be appended automatically.
444 // The return value is a standard fgets() call, which
445 // will read up to $this->BUFFER bytes of data, until it
446 // encounters a new line, or EOF, whichever happens first.
448 // This method works best if $cmd responds with only
451 if(!isset($this->FP
))
453 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
459 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
464 $buffer = $this->BUFFER
;
465 $this->update_timer();
466 fwrite($fp,"$cmd\r\n");
467 $reply = fgets($fp,$buffer);
468 $reply = $this->strip_clf($reply);
469 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
474 // Closes the connection to the POP3 server, deleting
475 // any msgs marked as deleted.
477 if(!isset($this->FP
))
479 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
484 fwrite($fp,"$cmd\r\n");
485 $reply = fgets($fp,$this->BUFFER
);
486 $reply = $this->strip_clf($reply);
487 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
493 function popstat () {
494 // Returns an array of 2 elements. The number of undeleted
495 // msgs in the mailbox, and the size of the mbox in octets.
497 $PopArray = $this->last("array");
499 if($PopArray == -1) { return false; }
501 if( (!$PopArray) or (empty($PopArray)) )
508 function uidl ($msgNum = "")
510 // Returns the UIDL of the msg specified. If called with
511 // no arguments, returns an associative array where each
512 // undeleted msg num is a key, and the msg's uidl is the element
513 // Array element 0 will contain the total number of msgs
515 if(!isset($this->FP
)) {
516 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
521 $buffer = $this->BUFFER
;
523 if(!empty($msgNum)) {
524 $cmd = "UIDL $msgNum";
525 $reply = $this->send_cmd($cmd);
526 if(!$this->is_ok($reply))
528 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
531 list ($ok,$num,$myUidl) = explode(" ",$reply);
534 $this->update_timer();
536 $UIDLArray = array();
537 $Total = $this->COUNT
;
538 $UIDLArray[0] = $Total;
545 fwrite($fp, "UIDL\r\n");
546 $reply = fgets($fp, $buffer);
547 $reply = $this->strip_clf($reply);
548 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
549 if(!$this->is_ok($reply))
551 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
557 $line = fgets($fp,$buffer);
558 while ( !ereg("^\.\r\n",$line)) {
559 if(ereg("^\.\r\n",$line)) {
562 list ($msg,$msgUidl) = explode(" ",$line);
563 $msgUidl = $this->strip_clf($msgUidl);
565 $UIDLArray[$msg] = $msgUidl;
569 $UIDLArray[$count] = 'deleted';
572 $line = fgets($fp,$buffer);
578 function delete ($msgNum = "") {
579 // Flags a specified msg as deleted. The msg will not
580 // be deleted until a quit() method is called.
582 if(!isset($this->FP
))
584 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
589 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
592 $reply = $this->send_cmd("DELE $msgNum");
593 if(!$this->is_ok($reply))
595 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
601 // *********************************************************
603 // The following methods are internal to the class.
605 function is_ok ($cmd = "") {
606 // Return true or false on +OK or -ERR
611 return( ereg ("^\+OK", $cmd ) );
614 function strip_clf ($text = "") {
615 // Strips \r\n from server responses
620 $stripped = str_replace("\r",'',$text);
621 $stripped = str_replace("\n",'',$stripped);
626 function parse_banner ( $server_text ) {
629 $length = strlen($server_text);
630 for($count =0; $count < $length; $count++
)
632 $digit = substr($server_text,$count,1);
634 if( (!$outside) && ($digit != '<') && ($digit != '>') )
648 $banner = $this->strip_clf($banner); // Just in case