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 $RFC1939 = TRUE; // Set by noop(). See rfc1939.txt
46 var $ALLOWAPOP = FASLE
; // Allow or disallow apop()
47 // This must be set to true
50 function POP3 ( $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 set_time_limit($timeout);
67 function update_timer () {
68 set_time_limit($this->TIMEOUT
);
72 function connect ($server, $port = 110) {
73 // Opens a socket to the specified server. Unless overridden,
74 // port defaults to 110. Returns true on success, false on fail
76 // If MAILSERVER is set, override $server with it's value
78 if(!empty($this->MAILSERVER
))
79 $server = $this->MAILSERVER
;
82 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
87 $fp = fsockopen("$server", $port, $errno, $errstr);
90 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
95 socket_set_blocking($fp,-1);
96 $this->update_timer();
97 $reply = fgets($fp,$this->BUFFER
);
98 $reply = $this->strip_clf($reply);
100 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
101 if(!$this->is_ok($reply)) {
102 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
107 $this->BANNER
= $this->parse_banner($reply);
108 $this->RFC1939
= $this->noop();
110 $this->ERROR
= _("POP3: premature NOOP OK, NOT an RFC 1939 Compliant server");
119 if(!isset($this->FP
)) {
120 $this->ERROR
= _("POP3 noop:") . ' ' . _("No connection to server");
124 $reply = $this->send_cmd( $cmd );
125 return( $this->is_ok( $reply ) );
129 function user ($user = "") {
130 // Sends the USER command, returns true or false
133 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
135 } elseif(!isset($this->FP
)) {
136 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
139 $reply = $this->send_cmd("USER $user");
140 if(!$this->is_ok($reply)) {
141 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
148 function pass ($pass = "") {
149 // Sends the PASS command, returns # of msgs in mailbox,
150 // returns false (undef) on Auth failure
153 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
155 } elseif(!isset($this->FP
)) {
156 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
159 $reply = $this->send_cmd("PASS $pass");
160 if(!$this->is_ok($reply)) {
161 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
166 $count = $this->last("count");
167 $this->COUNT
= $count;
168 $this->RFC1939
= $this->noop();
169 if(!$this->RFC1939
) {
170 $this->ERROR
= _("POP3 pass:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
179 function apop ($login,$pass) {
180 // Attempts an APOP login. If this fails, it'll
181 // try a standard login. YOUR SERVER MUST SUPPORT
182 // THE USE OF THE APOP COMMAND!
183 // (apop is optional per rfc1939)
185 if(!isset($this->FP
)) {
186 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
188 } elseif(!$this->ALLOWAPOP
) {
189 $retVal = $this->login($login,$pass);
191 } elseif(empty($login)) {
192 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
194 } elseif(empty($pass)) {
195 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
198 $banner = $this->BANNER
;
199 if( (!$banner) or (empty($banner)) ) {
200 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
201 $retVal = $this->login($login,$pass);
204 $AuthString = $banner;
205 $AuthString .= $pass;
206 $APOPString = md5($AuthString);
207 $cmd = "APOP $login $APOPString";
208 $reply = $this->send_cmd($cmd);
209 if(!$this->is_ok($reply)) {
210 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
211 $retVal = $this->login($login,$pass);
215 $count = $this->last("count");
216 $this->COUNT
= $count;
217 $this->RFC1939
= $this->noop();
218 if(!$this->RFC1939
) {
219 $this->ERROR
= _("POP3 apop:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
229 function login ($login = "", $pass = "") {
230 // Sends both user and pass. Returns # of msgs in mailbox or
231 // false on failure (or -1, if the error occurs while getting
232 // the number of messages.)
234 if( !isset($this->FP
) ) {
235 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
239 if( !$this->user( $login ) ) {
240 // Preserve the error generated by user()
243 $count = $this->pass($pass);
244 if( (!$count) ||
($count == -1) ) {
245 // Preserve the error generated by last() and pass()
253 function top ($msgNum, $numLines = "0") {
254 // Gets the header and first $numLines of the msg body
255 // returns data in an array with each returned line being
256 // an array element. If $numLines is empty, returns
257 // only the header information, and none of the body.
259 if(!isset($this->FP
)) {
260 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
263 $this->update_timer();
266 $buffer = $this->BUFFER
;
267 $cmd = "TOP $msgNum $numLines";
268 fwrite($fp, "TOP $msgNum $numLines\r\n");
269 $reply = fgets($fp, $buffer);
270 $reply = $this->strip_clf($reply);
272 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
274 if(!$this->is_ok($reply))
276 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
283 $line = fgets($fp,$buffer);
284 while ( !ereg("^\.\r\n",$line))
286 $MsgArray[$count] = $line;
288 $line = fgets($fp,$buffer);
289 if(empty($line)) { break; }
295 function pop_list ($msgNum = "") {
296 // If called with an argument, returns that msgs' size in octets
297 // No argument returns an associative array of undeleted
298 // msg numbers and their sizes in octets
300 if(!isset($this->FP
))
302 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
306 $Total = $this->COUNT
;
307 if( (!$Total) or ($Total == -1) )
313 return array("0","0");
314 // return -1; // mailbox empty
317 $this->update_timer();
321 $cmd = "LIST $msgNum";
322 fwrite($fp,"$cmd\r\n");
323 $reply = fgets($fp,$this->BUFFER
);
324 $reply = $this->strip_clf($reply);
326 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
328 if(!$this->is_ok($reply))
330 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
333 list($junk,$num,$size) = explode(" ",$reply);
337 $reply = $this->send_cmd($cmd);
338 if(!$this->is_ok($reply))
340 $reply = $this->strip_clf($reply);
341 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
345 $MsgArray[0] = $Total;
346 for($msgC=1;$msgC <= $Total; $msgC++
)
348 if($msgC > $Total) { break; }
349 $line = fgets($fp,$this->BUFFER
);
350 $line = $this->strip_clf($line);
351 if(ereg("^\.",$line))
353 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
356 list($thisMsg,$msgSize) = explode(" ",$line);
357 settype($thisMsg,"integer");
358 if($thisMsg != $msgC)
360 $MsgArray[$msgC] = "deleted";
364 $MsgArray[$msgC] = $msgSize;
370 function get ($msgNum) {
371 // Retrieve the specified msg number. Returns an array
372 // where each line of the msg is an array element.
374 if(!isset($this->FP
))
376 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
380 $this->update_timer();
383 $buffer = $this->BUFFER
;
384 $cmd = "RETR $msgNum";
385 $reply = $this->send_cmd($cmd);
387 if(!$this->is_ok($reply))
389 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
396 $line = fgets($fp,$buffer);
397 while ( !ereg("^\.\r\n",$line))
399 $MsgArray[$count] = $line;
401 $line = fgets($fp,$buffer);
402 if(empty($line)) { break; }
407 function last ( $type = "count" ) {
408 // Returns the highest msg number in the mailbox.
409 // returns -1 on error, 0+ on success, if type != count
410 // results in a popstat() call (2 element array returned)
413 if(!isset($this->FP
))
415 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
419 $reply = $this->send_cmd("STAT");
420 if(!$this->is_ok($reply))
422 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
426 $Vars = explode(" ",$reply);
429 settype($count,"integer");
430 settype($size,"integer");
433 return array($count,$size);
439 // Resets the status of the remote server. This includes
440 // resetting the status of ALL msgs to not be deleted.
441 // This method automatically closes the connection to the server.
443 if(!isset($this->FP
))
445 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
448 $reply = $this->send_cmd("RSET");
449 if(!$this->is_ok($reply))
451 // The POP3 RSET command -never- gives a -ERR
452 // response - if it ever does, something truely
455 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
456 @error_log
("POP3 reset: ERROR [$reply]",0);
462 function send_cmd ( $cmd = "" )
464 // Sends a user defined command string to the
465 // POP server and returns the results. Useful for
466 // non-compliant or custom POP servers.
467 // Do NOT includ the \r\n as part of your command
468 // string - it will be appended automatically.
470 // The return value is a standard fgets() call, which
471 // will read up to $this->BUFFER bytes of data, until it
472 // encounters a new line, or EOF, whichever happens first.
474 // This method works best if $cmd responds with only
477 if(!isset($this->FP
))
479 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
485 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
490 $buffer = $this->BUFFER
;
491 $this->update_timer();
492 fwrite($fp,"$cmd\r\n");
493 $reply = fgets($fp,$buffer);
494 $reply = $this->strip_clf($reply);
495 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
500 // Closes the connection to the POP3 server, deleting
501 // any msgs marked as deleted.
503 if(!isset($this->FP
))
505 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
510 fwrite($fp,"$cmd\r\n");
511 $reply = fgets($fp,$this->BUFFER
);
512 $reply = $this->strip_clf($reply);
513 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
519 function popstat () {
520 // Returns an array of 2 elements. The number of undeleted
521 // msgs in the mailbox, and the size of the mbox in octets.
523 $PopArray = $this->last("array");
525 if($PopArray == -1) { return false; }
527 if( (!$PopArray) or (empty($PopArray)) )
534 function uidl ($msgNum = "")
536 // Returns the UIDL of the msg specified. If called with
537 // no arguments, returns an associative array where each
538 // undeleted msg num is a key, and the msg's uidl is the element
539 // Array element 0 will contain the total number of msgs
541 if(!isset($this->FP
)) {
542 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
547 $buffer = $this->BUFFER
;
549 if(!empty($msgNum)) {
550 $cmd = "UIDL $msgNum";
551 $reply = $this->send_cmd($cmd);
552 if(!$this->is_ok($reply))
554 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
557 list ($ok,$num,$myUidl) = explode(" ",$reply);
560 $this->update_timer();
562 $UIDLArray = array();
563 $Total = $this->COUNT
;
564 $UIDLArray[0] = $Total;
571 fwrite($fp, "UIDL\r\n");
572 $reply = fgets($fp, $buffer);
573 $reply = $this->strip_clf($reply);
574 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
575 if(!$this->is_ok($reply))
577 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
583 $line = fgets($fp,$buffer);
584 while ( !ereg("^\.\r\n",$line)) {
585 if(ereg("^\.\r\n",$line)) {
588 list ($msg,$msgUidl) = explode(" ",$line);
589 $msgUidl = $this->strip_clf($msgUidl);
591 $UIDLArray[$msg] = $msgUidl;
595 $UIDLArray[$count] = 'deleted';
598 $line = fgets($fp,$buffer);
604 function delete ($msgNum = "") {
605 // Flags a specified msg as deleted. The msg will not
606 // be deleted until a quit() method is called.
608 if(!isset($this->FP
))
610 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
615 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
618 $reply = $this->send_cmd("DELE $msgNum");
619 if(!$this->is_ok($reply))
621 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
627 // *********************************************************
629 // The following methods are internal to the class.
631 function is_ok ($cmd = "") {
632 // Return true or false on +OK or -ERR
637 return( ereg ("^\+OK", $cmd ) );
640 function strip_clf ($text = "") {
641 // Strips \r\n from server responses
646 $stripped = str_replace("\r",'',$text);
647 $stripped = str_replace("\n",'',$stripped);
652 function parse_banner ( $server_text ) {
655 $length = strlen($server_text);
656 for($count =0; $count < $length; $count++
)
658 $digit = substr($server_text,$count,1);
660 if( (!$outside) && ($digit != '<') && ($digit != '>') )
674 $banner = $this->strip_clf($banner); // Just in case