81ba34d8c496f636679a355acbbefbf0c143094d
4 * mail_fetch/class.POP3.php
6 * Copyright (c) 1999-2005 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.
18 * @subpackage mail_fetch
22 * This is the POP3 class - DOCUMENT ME
23 * @package squirrelmail
26 var $ERROR = ''; // Error string.
28 var $TIMEOUT = 60; // Default timeout before giving up on a
31 var $COUNT = -1; // Mailbox msg count
33 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
34 // Per RFC 1939 the returned line a POP3
35 // server can send is 512 bytes.
37 var $FP = ''; // The connection to the server's
40 var $MAILSERVER = ''; // Set this to hard code the server name
42 var $DEBUG = FALSE; // set to true to echo pop3
43 // commands and responses to error_log
44 // this WILL log passwords!
46 var $BANNER = ''; // Holds the banner returned by the
47 // pop server - used for apop()
49 var $ALLOWAPOP = FALSE; // Allow or disallow apop()
50 // This must be set to true
53 function POP3 ( $server = '', $timeout = '' ) {
54 settype($this->BUFFER
,"integer");
55 if( !empty($server) ) {
56 // Do not allow programs to alter MAILSERVER
57 // if it is already specified. They can get around
58 // this if they -really- want to, so don't count on it.
59 if(empty($this->MAILSERVER
))
60 $this->MAILSERVER
= $server;
62 if(!empty($timeout)) {
63 settype($timeout,"integer");
64 $this->TIMEOUT
= $timeout;
65 if (!ini_get('safe_mode'))
66 set_time_limit($timeout);
71 function update_timer () {
72 if (!ini_get('safe_mode'))
73 set_time_limit($this->TIMEOUT
);
77 function connect ($server, $port = 110) {
78 // Opens a socket to the specified server. Unless overridden,
79 // port defaults to 110. Returns true on success, false on fail
81 // If MAILSERVER is set, override $server with it's value
83 if (!isset($port) ||
!$port) {$port = 110;}
84 if(!empty($this->MAILSERVER
))
85 $server = $this->MAILSERVER
;
88 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
93 $fp = @fsockopen
("$server", $port, $errno, $errstr);
96 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
101 socket_set_blocking($fp,-1);
102 $this->update_timer();
103 $reply = fgets($fp,$this->BUFFER
);
104 $reply = $this->strip_clf($reply);
106 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
107 if(!$this->is_ok($reply)) {
108 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
113 $this->BANNER
= $this->parse_banner($reply);
117 function user ($user = "") {
118 // Sends the USER command, returns true or false
121 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
123 } elseif(!isset($this->FP
)) {
124 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
127 $reply = $this->send_cmd("USER $user");
128 if(!$this->is_ok($reply)) {
129 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
136 function pass ($pass = "") {
137 // Sends the PASS command, returns # of msgs in mailbox,
138 // returns false (undef) on Auth failure
141 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
143 } elseif(!isset($this->FP
)) {
144 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
147 $reply = $this->send_cmd("PASS $pass");
148 if(!$this->is_ok($reply)) {
149 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
154 $count = $this->last("count");
155 $this->COUNT
= $count;
161 function apop ($login,$pass) {
162 // Attempts an APOP login. If this fails, it'll
163 // try a standard login. YOUR SERVER MUST SUPPORT
164 // THE USE OF THE APOP COMMAND!
165 // (apop is optional per rfc1939)
167 if(!isset($this->FP
)) {
168 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
170 } elseif(!$this->ALLOWAPOP
) {
171 $retVal = $this->login($login,$pass);
173 } elseif(empty($login)) {
174 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
176 } elseif(empty($pass)) {
177 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
180 $banner = $this->BANNER
;
181 if( (!$banner) or (empty($banner)) ) {
182 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
183 $retVal = $this->login($login,$pass);
186 $AuthString = $banner;
187 $AuthString .= $pass;
188 $APOPString = md5($AuthString);
189 $cmd = "APOP $login $APOPString";
190 $reply = $this->send_cmd($cmd);
191 if(!$this->is_ok($reply)) {
192 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
193 $retVal = $this->login($login,$pass);
197 $count = $this->last("count");
198 $this->COUNT
= $count;
205 function login ($login = "", $pass = "") {
206 // Sends both user and pass. Returns # of msgs in mailbox or
207 // false on failure (or -1, if the error occurs while getting
208 // the number of messages.)
210 if( !isset($this->FP
) ) {
211 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
215 if( !$this->user( $login ) ) {
216 // Preserve the error generated by user()
219 $count = $this->pass($pass);
220 if( (!$count) ||
($count == -1) ) {
221 // Preserve the error generated by last() and pass()
229 function top ($msgNum, $numLines = "0") {
230 // Gets the header and first $numLines of the msg body
231 // returns data in an array with each returned line being
232 // an array element. If $numLines is empty, returns
233 // only the header information, and none of the body.
235 if(!isset($this->FP
)) {
236 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
239 $this->update_timer();
242 $buffer = $this->BUFFER
;
243 $cmd = "TOP $msgNum $numLines";
244 fwrite($fp, "TOP $msgNum $numLines\r\n");
245 $reply = fgets($fp, $buffer);
246 $reply = $this->strip_clf($reply);
248 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
250 if(!$this->is_ok($reply))
252 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
259 $line = fgets($fp,$buffer);
260 while ( !ereg("^\.\r\n",$line))
262 $MsgArray[$count] = $line;
264 $line = fgets($fp,$buffer);
265 if(empty($line)) { break; }
271 function pop_list ($msgNum = "") {
272 // If called with an argument, returns that msgs' size in octets
273 // No argument returns an associative array of undeleted
274 // msg numbers and their sizes in octets
276 if(!isset($this->FP
))
278 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
282 $Total = $this->COUNT
;
283 if( (!$Total) or ($Total == -1) )
289 return array("0","0");
290 // return -1; // mailbox empty
293 $this->update_timer();
297 $cmd = "LIST $msgNum";
298 fwrite($fp,"$cmd\r\n");
299 $reply = fgets($fp,$this->BUFFER
);
300 $reply = $this->strip_clf($reply);
302 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
304 if(!$this->is_ok($reply))
306 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
309 list($junk,$num,$size) = preg_split('/\s+/',$reply);
313 $reply = $this->send_cmd($cmd);
314 if(!$this->is_ok($reply))
316 $reply = $this->strip_clf($reply);
317 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
321 $MsgArray[0] = $Total;
322 for($msgC=1;$msgC <= $Total; $msgC++
)
324 if($msgC > $Total) { break; }
325 $line = fgets($fp,$this->BUFFER
);
326 $line = $this->strip_clf($line);
327 if(ereg("^\.",$line))
329 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
332 list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
333 settype($thisMsg,"integer");
334 if($thisMsg != $msgC)
336 $MsgArray[$msgC] = "deleted";
340 $MsgArray[$msgC] = $msgSize;
346 function get ($msgNum) {
347 // Retrieve the specified msg number. Returns an array
348 // where each line of the msg is an array element.
350 if(!isset($this->FP
))
352 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
356 $this->update_timer();
359 $buffer = $this->BUFFER
;
360 $cmd = "RETR $msgNum";
361 $reply = $this->send_cmd($cmd);
363 if(!$this->is_ok($reply))
365 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
372 $line = fgets($fp,$buffer);
373 while ( !ereg("^\.\r\n",$line))
375 $MsgArray[$count] = $line;
377 $line = fgets($fp,$buffer);
378 if(empty($line)) { break; }
383 function last ( $type = "count" ) {
384 // Returns the highest msg number in the mailbox.
385 // returns -1 on error, 0+ on success, if type != count
386 // results in a popstat() call (2 element array returned)
389 if(!isset($this->FP
))
391 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
395 $reply = $this->send_cmd("STAT");
396 if(!$this->is_ok($reply))
398 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
402 $Vars = preg_split('/\s+/',$reply);
405 settype($count,"integer");
406 settype($size,"integer");
409 return array($count,$size);
415 // Resets the status of the remote server. This includes
416 // resetting the status of ALL msgs to not be deleted.
417 // This method automatically closes the connection to the server.
419 if(!isset($this->FP
))
421 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
424 $reply = $this->send_cmd("RSET");
425 if(!$this->is_ok($reply))
427 // The POP3 RSET command -never- gives a -ERR
428 // response - if it ever does, something truely
431 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
432 @error_log
("POP3 reset: ERROR [$reply]",0);
438 function send_cmd ( $cmd = "" )
440 // Sends a user defined command string to the
441 // POP server and returns the results. Useful for
442 // non-compliant or custom POP servers.
443 // Do NOT includ the \r\n as part of your command
444 // string - it will be appended automatically.
446 // The return value is a standard fgets() call, which
447 // will read up to $this->BUFFER bytes of data, until it
448 // encounters a new line, or EOF, whichever happens first.
450 // This method works best if $cmd responds with only
453 if(!isset($this->FP
))
455 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
461 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
466 $buffer = $this->BUFFER
;
467 $this->update_timer();
468 fwrite($fp,"$cmd\r\n");
469 $reply = fgets($fp,$buffer);
470 $reply = $this->strip_clf($reply);
471 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
476 // Closes the connection to the POP3 server, deleting
477 // any msgs marked as deleted.
479 if(!isset($this->FP
))
481 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
486 fwrite($fp,"$cmd\r\n");
487 $reply = fgets($fp,$this->BUFFER
);
488 $reply = $this->strip_clf($reply);
489 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
495 function popstat () {
496 // Returns an array of 2 elements. The number of undeleted
497 // msgs in the mailbox, and the size of the mbox in octets.
499 $PopArray = $this->last("array");
501 if($PopArray == -1) { return false; }
503 if( (!$PopArray) or (empty($PopArray)) )
510 function uidl ($msgNum = "")
512 // Returns the UIDL of the msg specified. If called with
513 // no arguments, returns an associative array where each
514 // undeleted msg num is a key, and the msg's uidl is the element
515 // Array element 0 will contain the total number of msgs
517 if(!isset($this->FP
)) {
518 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
523 $buffer = $this->BUFFER
;
525 if(!empty($msgNum)) {
526 $cmd = "UIDL $msgNum";
527 $reply = $this->send_cmd($cmd);
528 if(!$this->is_ok($reply))
530 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
533 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
536 $this->update_timer();
538 $UIDLArray = array();
539 $Total = $this->COUNT
;
540 $UIDLArray[0] = $Total;
547 fwrite($fp, "UIDL\r\n");
548 $reply = fgets($fp, $buffer);
549 $reply = $this->strip_clf($reply);
550 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
551 if(!$this->is_ok($reply))
553 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
559 $line = fgets($fp,$buffer);
560 while ( !ereg("^\.\r\n",$line)) {
561 if(ereg("^\.\r\n",$line)) {
564 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
565 $msgUidl = $this->strip_clf($msgUidl);
567 $UIDLArray[$msg] = $msgUidl;
571 $UIDLArray[$count] = 'deleted';
574 $line = fgets($fp,$buffer);
580 function delete ($msgNum = "") {
581 // Flags a specified msg as deleted. The msg will not
582 // be deleted until a quit() method is called.
584 if(!isset($this->FP
))
586 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
591 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
594 $reply = $this->send_cmd("DELE $msgNum");
595 if(!$this->is_ok($reply))
597 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
603 // *********************************************************
605 // The following methods are internal to the class.
607 function is_ok ($cmd = "") {
608 // Return true or false on +OK or -ERR
613 return( ereg ("^\+OK", $cmd ) );
616 function strip_clf ($text = "") {
617 // Strips \r\n from server responses
622 $stripped = str_replace("\r",'',$text);
623 $stripped = str_replace("\n",'',$stripped);
628 function parse_banner ( $server_text ) {
631 $length = strlen($server_text);
632 for($count =0; $count < $length; $count++
)
634 $digit = substr($server_text,$count,1);
636 if( (!$outside) && ($digit != '<') && ($digit != '>') )
650 $banner = $this->strip_clf($banner); // Just in case