6 * Copyright (c) 1999-2002 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.
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
47 function POP3 ( $server = '', $timeout = '' ) {
48 settype($this->BUFFER
,"integer");
49 if( !empty($server) ) {
50 // Do not allow programs to alter MAILSERVER
51 // if it is already specified. They can get around
52 // this if they -really- want to, so don't count on it.
53 if(empty($this->MAILSERVER
))
54 $this->MAILSERVER
= $server;
56 if(!empty($timeout)) {
57 settype($timeout,"integer");
58 $this->TIMEOUT
= $timeout;
59 set_time_limit($timeout);
64 function update_timer () {
65 set_time_limit($this->TIMEOUT
);
69 function connect ($server, $port = 110) {
70 // Opens a socket to the specified server. Unless overridden,
71 // port defaults to 110. Returns true on success, false on fail
73 // If MAILSERVER is set, override $server with it's value
75 if(!empty($this->MAILSERVER
))
76 $server = $this->MAILSERVER
;
79 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
84 $fp = fsockopen("$server", $port, $errno, $errstr);
87 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
92 socket_set_blocking($fp,-1);
93 $this->update_timer();
94 $reply = fgets($fp,$this->BUFFER
);
95 $reply = $this->strip_clf($reply);
97 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
98 if(!$this->is_ok($reply)) {
99 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
104 $this->BANNER
= $this->parse_banner($reply);
110 if(!isset($this->FP
)) {
111 $this->ERROR
= _("POP3 noop:") . ' ' . _("No connection to server");
115 $reply = $this->send_cmd( $cmd );
116 return( $this->is_ok( $reply ) );
120 function user ($user = "") {
121 // Sends the USER command, returns true or false
124 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
126 } elseif(!isset($this->FP
)) {
127 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
130 $reply = $this->send_cmd("USER $user");
131 if(!$this->is_ok($reply)) {
132 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
139 function pass ($pass = "") {
140 // Sends the PASS command, returns # of msgs in mailbox,
141 // returns false (undef) on Auth failure
144 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
146 } elseif(!isset($this->FP
)) {
147 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
150 $reply = $this->send_cmd("PASS $pass");
151 if(!$this->is_ok($reply)) {
152 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
157 $count = $this->last("count");
158 $this->COUNT
= $count;
164 function apop ($login,$pass) {
165 // Attempts an APOP login. If this fails, it'll
166 // try a standard login. YOUR SERVER MUST SUPPORT
167 // THE USE OF THE APOP COMMAND!
168 // (apop is optional per rfc1939)
170 if(!isset($this->FP
)) {
171 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
173 } elseif(!$this->ALLOWAPOP
) {
174 $retVal = $this->login($login,$pass);
176 } elseif(empty($login)) {
177 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
179 } elseif(empty($pass)) {
180 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
183 $banner = $this->BANNER
;
184 if( (!$banner) or (empty($banner)) ) {
185 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
186 $retVal = $this->login($login,$pass);
189 $AuthString = $banner;
190 $AuthString .= $pass;
191 $APOPString = md5($AuthString);
192 $cmd = "APOP $login $APOPString";
193 $reply = $this->send_cmd($cmd);
194 if(!$this->is_ok($reply)) {
195 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
196 $retVal = $this->login($login,$pass);
200 $count = $this->last("count");
201 $this->COUNT
= $count;
208 function login ($login = "", $pass = "") {
209 // Sends both user and pass. Returns # of msgs in mailbox or
210 // false on failure (or -1, if the error occurs while getting
211 // the number of messages.)
213 if( !isset($this->FP
) ) {
214 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
218 if( !$this->user( $login ) ) {
219 // Preserve the error generated by user()
222 $count = $this->pass($pass);
223 if( (!$count) ||
($count == -1) ) {
224 // Preserve the error generated by last() and pass()
232 function top ($msgNum, $numLines = "0") {
233 // Gets the header and first $numLines of the msg body
234 // returns data in an array with each returned line being
235 // an array element. If $numLines is empty, returns
236 // only the header information, and none of the body.
238 if(!isset($this->FP
)) {
239 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
242 $this->update_timer();
245 $buffer = $this->BUFFER
;
246 $cmd = "TOP $msgNum $numLines";
247 fwrite($fp, "TOP $msgNum $numLines\r\n");
248 $reply = fgets($fp, $buffer);
249 $reply = $this->strip_clf($reply);
251 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
253 if(!$this->is_ok($reply))
255 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
262 $line = fgets($fp,$buffer);
263 while ( !ereg("^\.\r\n",$line))
265 $MsgArray[$count] = $line;
267 $line = fgets($fp,$buffer);
268 if(empty($line)) { break; }
274 function pop_list ($msgNum = "") {
275 // If called with an argument, returns that msgs' size in octets
276 // No argument returns an associative array of undeleted
277 // msg numbers and their sizes in octets
279 if(!isset($this->FP
))
281 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
285 $Total = $this->COUNT
;
286 if( (!$Total) or ($Total == -1) )
292 return array("0","0");
293 // return -1; // mailbox empty
296 $this->update_timer();
300 $cmd = "LIST $msgNum";
301 fwrite($fp,"$cmd\r\n");
302 $reply = fgets($fp,$this->BUFFER
);
303 $reply = $this->strip_clf($reply);
305 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
307 if(!$this->is_ok($reply))
309 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
312 list($junk,$num,$size) = explode(" ",$reply);
316 $reply = $this->send_cmd($cmd);
317 if(!$this->is_ok($reply))
319 $reply = $this->strip_clf($reply);
320 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
324 $MsgArray[0] = $Total;
325 for($msgC=1;$msgC <= $Total; $msgC++
)
327 if($msgC > $Total) { break; }
328 $line = fgets($fp,$this->BUFFER
);
329 $line = $this->strip_clf($line);
330 if(ereg("^\.",$line))
332 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
335 list($thisMsg,$msgSize) = explode(" ",$line);
336 settype($thisMsg,"integer");
337 if($thisMsg != $msgC)
339 $MsgArray[$msgC] = "deleted";
343 $MsgArray[$msgC] = $msgSize;
349 function get ($msgNum) {
350 // Retrieve the specified msg number. Returns an array
351 // where each line of the msg is an array element.
353 if(!isset($this->FP
))
355 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
359 $this->update_timer();
362 $buffer = $this->BUFFER
;
363 $cmd = "RETR $msgNum";
364 $reply = $this->send_cmd($cmd);
366 if(!$this->is_ok($reply))
368 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
375 $line = fgets($fp,$buffer);
376 while ( !ereg("^\.\r\n",$line))
378 $MsgArray[$count] = $line;
380 $line = fgets($fp,$buffer);
381 if(empty($line)) { break; }
386 function last ( $type = "count" ) {
387 // Returns the highest msg number in the mailbox.
388 // returns -1 on error, 0+ on success, if type != count
389 // results in a popstat() call (2 element array returned)
392 if(!isset($this->FP
))
394 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
398 $reply = $this->send_cmd("STAT");
399 if(!$this->is_ok($reply))
401 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
405 $Vars = explode(" ",$reply);
408 settype($count,"integer");
409 settype($size,"integer");
412 return array($count,$size);
418 // Resets the status of the remote server. This includes
419 // resetting the status of ALL msgs to not be deleted.
420 // This method automatically closes the connection to the server.
422 if(!isset($this->FP
))
424 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
427 $reply = $this->send_cmd("RSET");
428 if(!$this->is_ok($reply))
430 // The POP3 RSET command -never- gives a -ERR
431 // response - if it ever does, something truely
434 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
435 @error_log
("POP3 reset: ERROR [$reply]",0);
441 function send_cmd ( $cmd = "" )
443 // Sends a user defined command string to the
444 // POP server and returns the results. Useful for
445 // non-compliant or custom POP servers.
446 // Do NOT includ the \r\n as part of your command
447 // string - it will be appended automatically.
449 // The return value is a standard fgets() call, which
450 // will read up to $this->BUFFER bytes of data, until it
451 // encounters a new line, or EOF, whichever happens first.
453 // This method works best if $cmd responds with only
456 if(!isset($this->FP
))
458 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
464 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
469 $buffer = $this->BUFFER
;
470 $this->update_timer();
471 fwrite($fp,"$cmd\r\n");
472 $reply = fgets($fp,$buffer);
473 $reply = $this->strip_clf($reply);
474 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
479 // Closes the connection to the POP3 server, deleting
480 // any msgs marked as deleted.
482 if(!isset($this->FP
))
484 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
489 fwrite($fp,"$cmd\r\n");
490 $reply = fgets($fp,$this->BUFFER
);
491 $reply = $this->strip_clf($reply);
492 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
498 function popstat () {
499 // Returns an array of 2 elements. The number of undeleted
500 // msgs in the mailbox, and the size of the mbox in octets.
502 $PopArray = $this->last("array");
504 if($PopArray == -1) { return false; }
506 if( (!$PopArray) or (empty($PopArray)) )
513 function uidl ($msgNum = "")
515 // Returns the UIDL of the msg specified. If called with
516 // no arguments, returns an associative array where each
517 // undeleted msg num is a key, and the msg's uidl is the element
518 // Array element 0 will contain the total number of msgs
520 if(!isset($this->FP
)) {
521 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
526 $buffer = $this->BUFFER
;
528 if(!empty($msgNum)) {
529 $cmd = "UIDL $msgNum";
530 $reply = $this->send_cmd($cmd);
531 if(!$this->is_ok($reply))
533 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
536 list ($ok,$num,$myUidl) = explode(" ",$reply);
539 $this->update_timer();
541 $UIDLArray = array();
542 $Total = $this->COUNT
;
543 $UIDLArray[0] = $Total;
550 fwrite($fp, "UIDL\r\n");
551 $reply = fgets($fp, $buffer);
552 $reply = $this->strip_clf($reply);
553 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
554 if(!$this->is_ok($reply))
556 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
562 $line = fgets($fp,$buffer);
563 while ( !ereg("^\.\r\n",$line)) {
564 if(ereg("^\.\r\n",$line)) {
567 list ($msg,$msgUidl) = explode(" ",$line);
568 $msgUidl = $this->strip_clf($msgUidl);
570 $UIDLArray[$msg] = $msgUidl;
574 $UIDLArray[$count] = 'deleted';
577 $line = fgets($fp,$buffer);
583 function delete ($msgNum = "") {
584 // Flags a specified msg as deleted. The msg will not
585 // be deleted until a quit() method is called.
587 if(!isset($this->FP
))
589 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
594 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
597 $reply = $this->send_cmd("DELE $msgNum");
598 if(!$this->is_ok($reply))
600 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
606 // *********************************************************
608 // The following methods are internal to the class.
610 function is_ok ($cmd = "") {
611 // Return true or false on +OK or -ERR
616 return( ereg ("^\+OK", $cmd ) );
619 function strip_clf ($text = "") {
620 // Strips \r\n from server responses
625 $stripped = str_replace("\r",'',$text);
626 $stripped = str_replace("\n",'',$stripped);
631 function parse_banner ( $server_text ) {
634 $length = strlen($server_text);
635 for($count =0; $count < $length; $count++
)
637 $digit = substr($server_text,$count,1);
639 if( (!$outside) && ($digit != '<') && ($digit != '>') )
653 $banner = $this->strip_clf($banner); // Just in case