4 class.POP3.php3 v1.0 99/03/24 CDI cdi@thewebmasters.net
5 Copyright (c) 1999 - CDI (cdi@thewebmasters.net) All Rights Reserved
6 Modified by Philippe Mingo 2001 mingo@rotedic.com
7 An RFC 1939 compliant wrapper class for the POP3 protocol.
11 var $ERROR = ""; // Error string.
13 var $TIMEOUT = 60; // Default timeout before giving up on a
16 var $COUNT = -1; // Mailbox msg count
18 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
19 // Per RFC 1939 the returned line a POP3
20 // server can send is 512 bytes.
22 var $FP = ""; // The connection to the server's
25 var $MAILSERVER = ""; // Set this to hard code the server name
27 var $DEBUG = false; // set to true to echo pop3
28 // commands and responses to error_log
29 // this WILL log passwords!
31 var $BANNER = ""; // Holds the banner returned by the
32 // pop server - used for apop()
34 var $RFC1939 = true; // Set by noop(). See rfc1939.txt
37 var $ALLOWAPOP = false; // Allow or disallow apop()
38 // This must be set to true
41 function POP3 ( $server = "", $timeout = "" ) {
42 settype($this->BUFFER
,"integer");
43 if( !empty($server) ) {
44 // Do not allow programs to alter MAILSERVER
45 // if it is already specified. They can get around
46 // this if they -really- want to, so don't count on it.
47 if(empty($this->MAILSERVER
))
48 $this->MAILSERVER
= $server;
50 if(!empty($timeout)) {
51 settype($timeout,"integer");
52 $this->TIMEOUT
= $timeout;
53 set_time_limit($timeout);
58 function update_timer () {
59 set_time_limit($this->TIMEOUT
);
63 function connect ($server, $port = 110) {
64 // Opens a socket to the specified server. Unless overridden,
65 // port defaults to 110. Returns true on success, false on fail
67 // If MAILSERVER is set, override $server with it's value
69 if(!empty($this->MAILSERVER
))
70 $server = $this->MAILSERVER
;
73 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
78 $fp = fsockopen("$server", $port, $errno, $errstr);
81 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
86 socket_set_blocking($fp,-1);
87 $this->update_timer();
88 $reply = fgets($fp,$this->BUFFER
);
89 $reply = $this->strip_clf($reply);
91 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
92 if(!$this->is_ok($reply)) {
93 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
98 $this->BANNER
= $this->parse_banner($reply);
99 $this->RFC1939
= $this->noop();
101 $this->ERROR
= _("POP3: premature NOOP OK, NOT an RFC 1939 Compliant server");
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;
159 $this->RFC1939
= $this->noop();
160 if(!$this->RFC1939
) {
161 $this->ERROR
= _("POP3 pass:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
170 function apop ($login,$pass) {
171 // Attempts an APOP login. If this fails, it'll
172 // try a standard login. YOUR SERVER MUST SUPPORT
173 // THE USE OF THE APOP COMMAND!
174 // (apop is optional per rfc1939)
176 if(!isset($this->FP
)) {
177 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
179 } elseif(!$this->ALLOWAPOP
) {
180 $retVal = $this->login($login,$pass);
182 } elseif(empty($login)) {
183 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
185 } elseif(empty($pass)) {
186 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
189 $banner = $this->BANNER
;
190 if( (!$banner) or (empty($banner)) ) {
191 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
192 $retVal = $this->login($login,$pass);
195 $AuthString = $banner;
196 $AuthString .= $pass;
197 $APOPString = md5($AuthString);
198 $cmd = "APOP $login $APOPString";
199 $reply = $this->send_cmd($cmd);
200 if(!$this->is_ok($reply)) {
201 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
202 $retVal = $this->login($login,$pass);
206 $count = $this->last("count");
207 $this->COUNT
= $count;
208 $this->RFC1939
= $this->noop();
209 if(!$this->RFC1939
) {
210 $this->ERROR
= _("POP3 apop:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
220 function login ($login = "", $pass = "") {
221 // Sends both user and pass. Returns # of msgs in mailbox or
222 // false on failure (or -1, if the error occurs while getting
223 // the number of messages.)
225 if( !isset($this->FP
) ) {
226 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
230 if( !$this->user( $login ) ) {
231 // Preserve the error generated by user()
234 $count = $this->pass($pass);
235 if( (!$count) ||
($count == -1) ) {
236 // Preserve the error generated by last() and pass()
244 function top ($msgNum, $numLines = "0") {
245 // Gets the header and first $numLines of the msg body
246 // returns data in an array with each returned line being
247 // an array element. If $numLines is empty, returns
248 // only the header information, and none of the body.
250 if(!isset($this->FP
)) {
251 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
254 $this->update_timer();
257 $buffer = $this->BUFFER
;
258 $cmd = "TOP $msgNum $numLines";
259 fwrite($fp, "TOP $msgNum $numLines\r\n");
260 $reply = fgets($fp, $buffer);
261 $reply = $this->strip_clf($reply);
263 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
265 if(!$this->is_ok($reply))
267 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
274 $line = fgets($fp,$buffer);
275 while ( !ereg("^\.\r\n",$line))
277 $MsgArray[$count] = $line;
279 $line = fgets($fp,$buffer);
280 if(empty($line)) { break; }
286 function pop_list ($msgNum = "") {
287 // If called with an argument, returns that msgs' size in octets
288 // No argument returns an associative array of undeleted
289 // msg numbers and their sizes in octets
291 if(!isset($this->FP
))
293 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
297 $Total = $this->COUNT
;
298 if( (!$Total) or ($Total == -1) )
304 return array("0","0");
305 // return -1; // mailbox empty
308 $this->update_timer();
312 $cmd = "LIST $msgNum";
313 fwrite($fp,"$cmd\r\n");
314 $reply = fgets($fp,$this->BUFFER
);
315 $reply = $this->strip_clf($reply);
317 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
319 if(!$this->is_ok($reply))
321 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
324 list($junk,$num,$size) = explode(" ",$reply);
328 $reply = $this->send_cmd($cmd);
329 if(!$this->is_ok($reply))
331 $reply = $this->strip_clf($reply);
332 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
336 $MsgArray[0] = $Total;
337 for($msgC=1;$msgC <= $Total; $msgC++
)
339 if($msgC > $Total) { break; }
340 $line = fgets($fp,$this->BUFFER
);
341 $line = $this->strip_clf($line);
342 if(ereg("^\.",$line))
344 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
347 list($thisMsg,$msgSize) = explode(" ",$line);
348 settype($thisMsg,"integer");
349 if($thisMsg != $msgC)
351 $MsgArray[$msgC] = "deleted";
355 $MsgArray[$msgC] = $msgSize;
361 function get ($msgNum) {
362 // Retrieve the specified msg number. Returns an array
363 // where each line of the msg is an array element.
365 if(!isset($this->FP
))
367 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
371 $this->update_timer();
374 $buffer = $this->BUFFER
;
375 $cmd = "RETR $msgNum";
376 $reply = $this->send_cmd($cmd);
378 if(!$this->is_ok($reply))
380 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
387 $line = fgets($fp,$buffer);
388 while ( !ereg("^\.\r\n",$line))
390 $MsgArray[$count] = $line;
392 $line = fgets($fp,$buffer);
393 if(empty($line)) { break; }
398 function last ( $type = "count" ) {
399 // Returns the highest msg number in the mailbox.
400 // returns -1 on error, 0+ on success, if type != count
401 // results in a popstat() call (2 element array returned)
404 if(!isset($this->FP
))
406 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
410 $reply = $this->send_cmd("STAT");
411 if(!$this->is_ok($reply))
413 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
417 $Vars = explode(" ",$reply);
420 settype($count,"integer");
421 settype($size,"integer");
424 return array($count,$size);
430 // Resets the status of the remote server. This includes
431 // resetting the status of ALL msgs to not be deleted.
432 // This method automatically closes the connection to the server.
434 if(!isset($this->FP
))
436 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
439 $reply = $this->send_cmd("RSET");
440 if(!$this->is_ok($reply))
442 // The POP3 RSET command -never- gives a -ERR
443 // response - if it ever does, something truely
446 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
447 @error_log
("POP3 reset: ERROR [$reply]",0);
453 function send_cmd ( $cmd = "" )
455 // Sends a user defined command string to the
456 // POP server and returns the results. Useful for
457 // non-compliant or custom POP servers.
458 // Do NOT includ the \r\n as part of your command
459 // string - it will be appended automatically.
461 // The return value is a standard fgets() call, which
462 // will read up to $this->BUFFER bytes of data, until it
463 // encounters a new line, or EOF, whichever happens first.
465 // This method works best if $cmd responds with only
468 if(!isset($this->FP
))
470 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
476 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
481 $buffer = $this->BUFFER
;
482 $this->update_timer();
483 fwrite($fp,"$cmd\r\n");
484 $reply = fgets($fp,$buffer);
485 $reply = $this->strip_clf($reply);
486 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
491 // Closes the connection to the POP3 server, deleting
492 // any msgs marked as deleted.
494 if(!isset($this->FP
))
496 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
501 fwrite($fp,"$cmd\r\n");
502 $reply = fgets($fp,$this->BUFFER
);
503 $reply = $this->strip_clf($reply);
504 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
510 function popstat () {
511 // Returns an array of 2 elements. The number of undeleted
512 // msgs in the mailbox, and the size of the mbox in octets.
514 $PopArray = $this->last("array");
516 if($PopArray == -1) { return false; }
518 if( (!$PopArray) or (empty($PopArray)) )
525 function uidl ($msgNum = "")
527 // Returns the UIDL of the msg specified. If called with
528 // no arguments, returns an associative array where each
529 // undeleted msg num is a key, and the msg's uidl is the element
530 // Array element 0 will contain the total number of msgs
532 if(!isset($this->FP
)) {
533 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
538 $buffer = $this->BUFFER
;
540 if(!empty($msgNum)) {
541 $cmd = "UIDL $msgNum";
542 $reply = $this->send_cmd($cmd);
543 if(!$this->is_ok($reply))
545 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
548 list ($ok,$num,$myUidl) = explode(" ",$reply);
551 $this->update_timer();
553 $UIDLArray = array();
554 $Total = $this->COUNT
;
555 $UIDLArray[0] = $Total;
562 fwrite($fp, "UIDL\r\n");
563 $reply = fgets($fp, $buffer);
564 $reply = $this->strip_clf($reply);
565 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
566 if(!$this->is_ok($reply))
568 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
574 $line = fgets($fp,$buffer);
575 while ( !ereg("^\.\r\n",$line)) {
576 if(ereg("^\.\r\n",$line)) {
579 list ($msg,$msgUidl) = explode(" ",$line);
580 $msgUidl = $this->strip_clf($msgUidl);
582 $UIDLArray[$msg] = $msgUidl;
586 $UIDLArray[$count] = "deleted";
589 $line = fgets($fp,$buffer);
595 function delete ($msgNum = "") {
596 // Flags a specified msg as deleted. The msg will not
597 // be deleted until a quit() method is called.
599 if(!isset($this->FP
))
601 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
606 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
609 $reply = $this->send_cmd("DELE $msgNum");
610 if(!$this->is_ok($reply))
612 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
618 // *********************************************************
620 // The following methods are internal to the class.
622 function is_ok ($cmd = "") {
623 // Return true or false on +OK or -ERR
628 return( ereg ("^\+OK", $cmd ) );
631 function strip_clf ($text = "") {
632 // Strips \r\n from server responses
637 $stripped = str_replace("\r",'',$text);
638 $stripped = str_replace("\n",'',$stripped);
643 function parse_banner ( $server_text ) {
646 $length = strlen($server_text);
647 for($count =0; $count < $length; $count++
)
649 $digit = substr($server_text,$count,1);
651 if( (!$outside) && ($digit != '<') && ($digit != '>') )
665 $banner = $this->strip_clf($banner); // Just in case