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.
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 (!isset($port) ||
!$port) {$port = 110;}
76 if(!empty($this->MAILSERVER
))
77 $server = $this->MAILSERVER
;
80 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
85 $fp = fsockopen("$server", $port, $errno, $errstr);
88 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
93 socket_set_blocking($fp,-1);
94 $this->update_timer();
95 $reply = fgets($fp,$this->BUFFER
);
96 $reply = $this->strip_clf($reply);
98 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
99 if(!$this->is_ok($reply)) {
100 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
105 $this->BANNER
= $this->parse_banner($reply);
111 if(!isset($this->FP
)) {
112 $this->ERROR
= _("POP3 noop:") . ' ' . _("No connection to server");
116 $reply = $this->send_cmd( $cmd );
117 return( $this->is_ok( $reply ) );
121 function user ($user = "") {
122 // Sends the USER command, returns true or false
125 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
127 } elseif(!isset($this->FP
)) {
128 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
131 $reply = $this->send_cmd("USER $user");
132 if(!$this->is_ok($reply)) {
133 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
140 function pass ($pass = "") {
141 // Sends the PASS command, returns # of msgs in mailbox,
142 // returns false (undef) on Auth failure
145 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
147 } elseif(!isset($this->FP
)) {
148 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
151 $reply = $this->send_cmd("PASS $pass");
152 if(!$this->is_ok($reply)) {
153 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
158 $count = $this->last("count");
159 $this->COUNT
= $count;
165 function apop ($login,$pass) {
166 // Attempts an APOP login. If this fails, it'll
167 // try a standard login. YOUR SERVER MUST SUPPORT
168 // THE USE OF THE APOP COMMAND!
169 // (apop is optional per rfc1939)
171 if(!isset($this->FP
)) {
172 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
174 } elseif(!$this->ALLOWAPOP
) {
175 $retVal = $this->login($login,$pass);
177 } elseif(empty($login)) {
178 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
180 } elseif(empty($pass)) {
181 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
184 $banner = $this->BANNER
;
185 if( (!$banner) or (empty($banner)) ) {
186 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
187 $retVal = $this->login($login,$pass);
190 $AuthString = $banner;
191 $AuthString .= $pass;
192 $APOPString = md5($AuthString);
193 $cmd = "APOP $login $APOPString";
194 $reply = $this->send_cmd($cmd);
195 if(!$this->is_ok($reply)) {
196 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
197 $retVal = $this->login($login,$pass);
201 $count = $this->last("count");
202 $this->COUNT
= $count;
209 function login ($login = "", $pass = "") {
210 // Sends both user and pass. Returns # of msgs in mailbox or
211 // false on failure (or -1, if the error occurs while getting
212 // the number of messages.)
214 if( !isset($this->FP
) ) {
215 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
219 if( !$this->user( $login ) ) {
220 // Preserve the error generated by user()
223 $count = $this->pass($pass);
224 if( (!$count) ||
($count == -1) ) {
225 // Preserve the error generated by last() and pass()
233 function top ($msgNum, $numLines = "0") {
234 // Gets the header and first $numLines of the msg body
235 // returns data in an array with each returned line being
236 // an array element. If $numLines is empty, returns
237 // only the header information, and none of the body.
239 if(!isset($this->FP
)) {
240 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
243 $this->update_timer();
246 $buffer = $this->BUFFER
;
247 $cmd = "TOP $msgNum $numLines";
248 fwrite($fp, "TOP $msgNum $numLines\r\n");
249 $reply = fgets($fp, $buffer);
250 $reply = $this->strip_clf($reply);
252 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
254 if(!$this->is_ok($reply))
256 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
263 $line = fgets($fp,$buffer);
264 while ( !ereg("^\.\r\n",$line))
266 $MsgArray[$count] = $line;
268 $line = fgets($fp,$buffer);
269 if(empty($line)) { break; }
275 function pop_list ($msgNum = "") {
276 // If called with an argument, returns that msgs' size in octets
277 // No argument returns an associative array of undeleted
278 // msg numbers and their sizes in octets
280 if(!isset($this->FP
))
282 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
286 $Total = $this->COUNT
;
287 if( (!$Total) or ($Total == -1) )
293 return array("0","0");
294 // return -1; // mailbox empty
297 $this->update_timer();
301 $cmd = "LIST $msgNum";
302 fwrite($fp,"$cmd\r\n");
303 $reply = fgets($fp,$this->BUFFER
);
304 $reply = $this->strip_clf($reply);
306 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
308 if(!$this->is_ok($reply))
310 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
313 list($junk,$num,$size) = explode(" ",$reply);
317 $reply = $this->send_cmd($cmd);
318 if(!$this->is_ok($reply))
320 $reply = $this->strip_clf($reply);
321 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
325 $MsgArray[0] = $Total;
326 for($msgC=1;$msgC <= $Total; $msgC++
)
328 if($msgC > $Total) { break; }
329 $line = fgets($fp,$this->BUFFER
);
330 $line = $this->strip_clf($line);
331 if(ereg("^\.",$line))
333 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
336 list($thisMsg,$msgSize) = explode(" ",$line);
337 settype($thisMsg,"integer");
338 if($thisMsg != $msgC)
340 $MsgArray[$msgC] = "deleted";
344 $MsgArray[$msgC] = $msgSize;
350 function get ($msgNum) {
351 // Retrieve the specified msg number. Returns an array
352 // where each line of the msg is an array element.
354 if(!isset($this->FP
))
356 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
360 $this->update_timer();
363 $buffer = $this->BUFFER
;
364 $cmd = "RETR $msgNum";
365 $reply = $this->send_cmd($cmd);
367 if(!$this->is_ok($reply))
369 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
376 $line = fgets($fp,$buffer);
377 while ( !ereg("^\.\r\n",$line))
379 $MsgArray[$count] = $line;
381 $line = fgets($fp,$buffer);
382 if(empty($line)) { break; }
387 function last ( $type = "count" ) {
388 // Returns the highest msg number in the mailbox.
389 // returns -1 on error, 0+ on success, if type != count
390 // results in a popstat() call (2 element array returned)
393 if(!isset($this->FP
))
395 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
399 $reply = $this->send_cmd("STAT");
400 if(!$this->is_ok($reply))
402 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
406 $Vars = explode(" ",$reply);
409 settype($count,"integer");
410 settype($size,"integer");
413 return array($count,$size);
419 // Resets the status of the remote server. This includes
420 // resetting the status of ALL msgs to not be deleted.
421 // This method automatically closes the connection to the server.
423 if(!isset($this->FP
))
425 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
428 $reply = $this->send_cmd("RSET");
429 if(!$this->is_ok($reply))
431 // The POP3 RSET command -never- gives a -ERR
432 // response - if it ever does, something truely
435 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
436 @error_log
("POP3 reset: ERROR [$reply]",0);
442 function send_cmd ( $cmd = "" )
444 // Sends a user defined command string to the
445 // POP server and returns the results. Useful for
446 // non-compliant or custom POP servers.
447 // Do NOT includ the \r\n as part of your command
448 // string - it will be appended automatically.
450 // The return value is a standard fgets() call, which
451 // will read up to $this->BUFFER bytes of data, until it
452 // encounters a new line, or EOF, whichever happens first.
454 // This method works best if $cmd responds with only
457 if(!isset($this->FP
))
459 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
465 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
470 $buffer = $this->BUFFER
;
471 $this->update_timer();
472 fwrite($fp,"$cmd\r\n");
473 $reply = fgets($fp,$buffer);
474 $reply = $this->strip_clf($reply);
475 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
480 // Closes the connection to the POP3 server, deleting
481 // any msgs marked as deleted.
483 if(!isset($this->FP
))
485 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
490 fwrite($fp,"$cmd\r\n");
491 $reply = fgets($fp,$this->BUFFER
);
492 $reply = $this->strip_clf($reply);
493 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
499 function popstat () {
500 // Returns an array of 2 elements. The number of undeleted
501 // msgs in the mailbox, and the size of the mbox in octets.
503 $PopArray = $this->last("array");
505 if($PopArray == -1) { return false; }
507 if( (!$PopArray) or (empty($PopArray)) )
514 function uidl ($msgNum = "")
516 // Returns the UIDL of the msg specified. If called with
517 // no arguments, returns an associative array where each
518 // undeleted msg num is a key, and the msg's uidl is the element
519 // Array element 0 will contain the total number of msgs
521 if(!isset($this->FP
)) {
522 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
527 $buffer = $this->BUFFER
;
529 if(!empty($msgNum)) {
530 $cmd = "UIDL $msgNum";
531 $reply = $this->send_cmd($cmd);
532 if(!$this->is_ok($reply))
534 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
537 list ($ok,$num,$myUidl) = explode(" ",$reply);
540 $this->update_timer();
542 $UIDLArray = array();
543 $Total = $this->COUNT
;
544 $UIDLArray[0] = $Total;
551 fwrite($fp, "UIDL\r\n");
552 $reply = fgets($fp, $buffer);
553 $reply = $this->strip_clf($reply);
554 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
555 if(!$this->is_ok($reply))
557 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
563 $line = fgets($fp,$buffer);
564 while ( !ereg("^\.\r\n",$line)) {
565 if(ereg("^\.\r\n",$line)) {
568 list ($msg,$msgUidl) = explode(" ",$line);
569 $msgUidl = $this->strip_clf($msgUidl);
571 $UIDLArray[$msg] = $msgUidl;
575 $UIDLArray[$count] = 'deleted';
578 $line = fgets($fp,$buffer);
584 function delete ($msgNum = "") {
585 // Flags a specified msg as deleted. The msg will not
586 // be deleted until a quit() method is called.
588 if(!isset($this->FP
))
590 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
595 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
598 $reply = $this->send_cmd("DELE $msgNum");
599 if(!$this->is_ok($reply))
601 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
607 // *********************************************************
609 // The following methods are internal to the class.
611 function is_ok ($cmd = "") {
612 // Return true or false on +OK or -ERR
617 return( ereg ("^\+OK", $cmd ) );
620 function strip_clf ($text = "") {
621 // Strips \r\n from server responses
626 $stripped = str_replace("\r",'',$text);
627 $stripped = str_replace("\n",'',$stripped);
632 function parse_banner ( $server_text ) {
635 $length = strlen($server_text);
636 for($count =0; $count < $length; $count++
)
638 $digit = substr($server_text,$count,1);
640 if( (!$outside) && ($digit != '<') && ($digit != '>') )
654 $banner = $this->strip_clf($banner); // Just in case