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);
109 function user ($user = "") {
110 // Sends the USER command, returns true or false
113 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
115 } elseif(!isset($this->FP
)) {
116 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
119 $reply = $this->send_cmd("USER $user");
120 if(!$this->is_ok($reply)) {
121 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
128 function pass ($pass = "") {
129 // Sends the PASS command, returns # of msgs in mailbox,
130 // returns false (undef) on Auth failure
133 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
135 } elseif(!isset($this->FP
)) {
136 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
139 $reply = $this->send_cmd("PASS $pass");
140 if(!$this->is_ok($reply)) {
141 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
146 $count = $this->last("count");
147 $this->COUNT
= $count;
153 function apop ($login,$pass) {
154 // Attempts an APOP login. If this fails, it'll
155 // try a standard login. YOUR SERVER MUST SUPPORT
156 // THE USE OF THE APOP COMMAND!
157 // (apop is optional per rfc1939)
159 if(!isset($this->FP
)) {
160 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
162 } elseif(!$this->ALLOWAPOP
) {
163 $retVal = $this->login($login,$pass);
165 } elseif(empty($login)) {
166 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
168 } elseif(empty($pass)) {
169 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
172 $banner = $this->BANNER
;
173 if( (!$banner) or (empty($banner)) ) {
174 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
175 $retVal = $this->login($login,$pass);
178 $AuthString = $banner;
179 $AuthString .= $pass;
180 $APOPString = md5($AuthString);
181 $cmd = "APOP $login $APOPString";
182 $reply = $this->send_cmd($cmd);
183 if(!$this->is_ok($reply)) {
184 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
185 $retVal = $this->login($login,$pass);
189 $count = $this->last("count");
190 $this->COUNT
= $count;
197 function login ($login = "", $pass = "") {
198 // Sends both user and pass. Returns # of msgs in mailbox or
199 // false on failure (or -1, if the error occurs while getting
200 // the number of messages.)
202 if( !isset($this->FP
) ) {
203 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
207 if( !$this->user( $login ) ) {
208 // Preserve the error generated by user()
211 $count = $this->pass($pass);
212 if( (!$count) ||
($count == -1) ) {
213 // Preserve the error generated by last() and pass()
221 function top ($msgNum, $numLines = "0") {
222 // Gets the header and first $numLines of the msg body
223 // returns data in an array with each returned line being
224 // an array element. If $numLines is empty, returns
225 // only the header information, and none of the body.
227 if(!isset($this->FP
)) {
228 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
231 $this->update_timer();
234 $buffer = $this->BUFFER
;
235 $cmd = "TOP $msgNum $numLines";
236 fwrite($fp, "TOP $msgNum $numLines\r\n");
237 $reply = fgets($fp, $buffer);
238 $reply = $this->strip_clf($reply);
240 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
242 if(!$this->is_ok($reply))
244 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
251 $line = fgets($fp,$buffer);
252 while ( !ereg("^\.\r\n",$line))
254 $MsgArray[$count] = $line;
256 $line = fgets($fp,$buffer);
257 if(empty($line)) { break; }
263 function pop_list ($msgNum = "") {
264 // If called with an argument, returns that msgs' size in octets
265 // No argument returns an associative array of undeleted
266 // msg numbers and their sizes in octets
268 if(!isset($this->FP
))
270 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
274 $Total = $this->COUNT
;
275 if( (!$Total) or ($Total == -1) )
281 return array("0","0");
282 // return -1; // mailbox empty
285 $this->update_timer();
289 $cmd = "LIST $msgNum";
290 fwrite($fp,"$cmd\r\n");
291 $reply = fgets($fp,$this->BUFFER
);
292 $reply = $this->strip_clf($reply);
294 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
296 if(!$this->is_ok($reply))
298 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
301 list($junk,$num,$size) = explode(" ",$reply);
305 $reply = $this->send_cmd($cmd);
306 if(!$this->is_ok($reply))
308 $reply = $this->strip_clf($reply);
309 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
313 $MsgArray[0] = $Total;
314 for($msgC=1;$msgC <= $Total; $msgC++
)
316 if($msgC > $Total) { break; }
317 $line = fgets($fp,$this->BUFFER
);
318 $line = $this->strip_clf($line);
319 if(ereg("^\.",$line))
321 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
324 list($thisMsg,$msgSize) = explode(" ",$line);
325 settype($thisMsg,"integer");
326 if($thisMsg != $msgC)
328 $MsgArray[$msgC] = "deleted";
332 $MsgArray[$msgC] = $msgSize;
338 function get ($msgNum) {
339 // Retrieve the specified msg number. Returns an array
340 // where each line of the msg is an array element.
342 if(!isset($this->FP
))
344 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
348 $this->update_timer();
351 $buffer = $this->BUFFER
;
352 $cmd = "RETR $msgNum";
353 $reply = $this->send_cmd($cmd);
355 if(!$this->is_ok($reply))
357 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
364 $line = fgets($fp,$buffer);
365 while ( !ereg("^\.\r\n",$line))
367 $MsgArray[$count] = $line;
369 $line = fgets($fp,$buffer);
370 if(empty($line)) { break; }
375 function last ( $type = "count" ) {
376 // Returns the highest msg number in the mailbox.
377 // returns -1 on error, 0+ on success, if type != count
378 // results in a popstat() call (2 element array returned)
381 if(!isset($this->FP
))
383 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
387 $reply = $this->send_cmd("STAT");
388 if(!$this->is_ok($reply))
390 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
394 $Vars = explode(" ",$reply);
397 settype($count,"integer");
398 settype($size,"integer");
401 return array($count,$size);
407 // Resets the status of the remote server. This includes
408 // resetting the status of ALL msgs to not be deleted.
409 // This method automatically closes the connection to the server.
411 if(!isset($this->FP
))
413 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
416 $reply = $this->send_cmd("RSET");
417 if(!$this->is_ok($reply))
419 // The POP3 RSET command -never- gives a -ERR
420 // response - if it ever does, something truely
423 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
424 @error_log
("POP3 reset: ERROR [$reply]",0);
430 function send_cmd ( $cmd = "" )
432 // Sends a user defined command string to the
433 // POP server and returns the results. Useful for
434 // non-compliant or custom POP servers.
435 // Do NOT includ the \r\n as part of your command
436 // string - it will be appended automatically.
438 // The return value is a standard fgets() call, which
439 // will read up to $this->BUFFER bytes of data, until it
440 // encounters a new line, or EOF, whichever happens first.
442 // This method works best if $cmd responds with only
445 if(!isset($this->FP
))
447 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
453 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
458 $buffer = $this->BUFFER
;
459 $this->update_timer();
460 fwrite($fp,"$cmd\r\n");
461 $reply = fgets($fp,$buffer);
462 $reply = $this->strip_clf($reply);
463 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
468 // Closes the connection to the POP3 server, deleting
469 // any msgs marked as deleted.
471 if(!isset($this->FP
))
473 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
478 fwrite($fp,"$cmd\r\n");
479 $reply = fgets($fp,$this->BUFFER
);
480 $reply = $this->strip_clf($reply);
481 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
487 function popstat () {
488 // Returns an array of 2 elements. The number of undeleted
489 // msgs in the mailbox, and the size of the mbox in octets.
491 $PopArray = $this->last("array");
493 if($PopArray == -1) { return false; }
495 if( (!$PopArray) or (empty($PopArray)) )
502 function uidl ($msgNum = "")
504 // Returns the UIDL of the msg specified. If called with
505 // no arguments, returns an associative array where each
506 // undeleted msg num is a key, and the msg's uidl is the element
507 // Array element 0 will contain the total number of msgs
509 if(!isset($this->FP
)) {
510 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
515 $buffer = $this->BUFFER
;
517 if(!empty($msgNum)) {
518 $cmd = "UIDL $msgNum";
519 $reply = $this->send_cmd($cmd);
520 if(!$this->is_ok($reply))
522 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
525 list ($ok,$num,$myUidl) = explode(" ",$reply);
528 $this->update_timer();
530 $UIDLArray = array();
531 $Total = $this->COUNT
;
532 $UIDLArray[0] = $Total;
539 fwrite($fp, "UIDL\r\n");
540 $reply = fgets($fp, $buffer);
541 $reply = $this->strip_clf($reply);
542 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
543 if(!$this->is_ok($reply))
545 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
551 $line = fgets($fp,$buffer);
552 while ( !ereg("^\.\r\n",$line)) {
553 if(ereg("^\.\r\n",$line)) {
556 list ($msg,$msgUidl) = explode(" ",$line);
557 $msgUidl = $this->strip_clf($msgUidl);
559 $UIDLArray[$msg] = $msgUidl;
563 $UIDLArray[$count] = 'deleted';
566 $line = fgets($fp,$buffer);
572 function delete ($msgNum = "") {
573 // Flags a specified msg as deleted. The msg will not
574 // be deleted until a quit() method is called.
576 if(!isset($this->FP
))
578 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
583 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
586 $reply = $this->send_cmd("DELE $msgNum");
587 if(!$this->is_ok($reply))
589 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
595 // *********************************************************
597 // The following methods are internal to the class.
599 function is_ok ($cmd = "") {
600 // Return true or false on +OK or -ERR
605 return( ereg ("^\+OK", $cmd ) );
608 function strip_clf ($text = "") {
609 // Strips \r\n from server responses
614 $stripped = str_replace("\r",'',$text);
615 $stripped = str_replace("\n",'',$stripped);
620 function parse_banner ( $server_text ) {
623 $length = strlen($server_text);
624 for($count =0; $count < $length; $count++
)
626 $digit = substr($server_text,$count,1);
628 if( (!$outside) && ($digit != '<') && ($digit != '>') )
642 $banner = $this->strip_clf($banner); // Just in case