5
* Copyright (c) 1999-2011 CDI (cdi@thewebmasters.net) All Rights Reserved
6
* Modified by Philippe Mingo 2001-2009 mingo@rotedic.com
7
* An RFC 1939 compliant wrapper class for the POP3 protocol.
9
* Licensed under the GNU GPL. For full terms see the file COPYING.
13
* @copyright 1999-2011 The SquirrelMail Project Team
14
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
16
* @subpackage mail_fetch
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
if (!ini_get('safe_mode'))
60
set_time_limit($timeout);
65
function update_timer () {
66
if (!ini_get('safe_mode'))
67
set_time_limit($this->TIMEOUT);
71
function connect ($server, $port = 110) {
72
// Opens a socket to the specified server. Unless overridden,
73
// port defaults to 110. Returns true on success, false on fail
75
// If MAILSERVER is set, override $server with it's value
77
if (!isset($port) || !$port) {$port = 110;}
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);
111
function user ($user = "") {
112
// Sends the USER command, returns true or false
115
$this->ERROR = "POP3 user: " . _("no login ID submitted");
117
} elseif(!isset($this->FP)) {
118
$this->ERROR = "POP3 user: " . _("connection not established");
121
$reply = $this->send_cmd("USER $user");
122
if(!$this->is_ok($reply)) {
123
$this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
130
function pass ($pass = "") {
131
// Sends the PASS command, returns # of msgs in mailbox,
132
// returns false (undef) on Auth failure
135
$this->ERROR = "POP3 pass: " . _("No password submitted");
137
} elseif(!isset($this->FP)) {
138
$this->ERROR = "POP3 pass: " . _("connection not established");
141
$reply = $this->send_cmd("PASS $pass");
142
if(!$this->is_ok($reply)) {
143
$this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
148
$count = $this->last("count");
149
$this->COUNT = $count;
155
function apop ($login,$pass) {
156
// Attempts an APOP login. If this fails, it'll
157
// try a standard login. YOUR SERVER MUST SUPPORT
158
// THE USE OF THE APOP COMMAND!
159
// (apop is optional per rfc1939)
161
if(!isset($this->FP)) {
162
$this->ERROR = "POP3 apop: " . _("No connection to server");
164
} elseif(!$this->ALLOWAPOP) {
165
$retVal = $this->login($login,$pass);
167
} elseif(empty($login)) {
168
$this->ERROR = "POP3 apop: " . _("No login ID submitted");
170
} elseif(empty($pass)) {
171
$this->ERROR = "POP3 apop: " . _("No password submitted");
174
$banner = $this->BANNER;
175
if( (!$banner) or (empty($banner)) ) {
176
$this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
177
$retVal = $this->login($login,$pass);
180
$AuthString = $banner;
181
$AuthString .= $pass;
182
$APOPString = md5($AuthString);
183
$cmd = "APOP $login $APOPString";
184
$reply = $this->send_cmd($cmd);
185
if(!$this->is_ok($reply)) {
186
$this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
187
$retVal = $this->login($login,$pass);
191
$count = $this->last("count");
192
$this->COUNT = $count;
199
function login ($login = "", $pass = "") {
200
// Sends both user and pass. Returns # of msgs in mailbox or
201
// false on failure (or -1, if the error occurs while getting
202
// the number of messages.)
204
if( !isset($this->FP) ) {
205
$this->ERROR = "POP3 login: " . _("No connection to server");
209
if( !$this->user( $login ) ) {
210
// Preserve the error generated by user()
213
$count = $this->pass($pass);
214
if( (!$count) || ($count == -1) ) {
215
// Preserve the error generated by last() and pass()
223
function top ($msgNum, $numLines = "0") {
224
// Gets the header and first $numLines of the msg body
225
// returns data in an array with each returned line being
226
// an array element. If $numLines is empty, returns
227
// only the header information, and none of the body.
229
if(!isset($this->FP)) {
230
$this->ERROR = "POP3 top: " . _("No connection to server");
233
$this->update_timer();
236
$buffer = $this->BUFFER;
237
$cmd = "TOP $msgNum $numLines";
238
fwrite($fp, "TOP $msgNum $numLines\r\n");
239
$reply = fgets($fp, $buffer);
240
$reply = $this->strip_clf($reply);
242
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
244
if(!$this->is_ok($reply))
246
$this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
253
$line = fgets($fp,$buffer);
254
while ( !preg_match('/^\.\r\n/',$line))
256
$MsgArray[$count] = $line;
258
$line = fgets($fp,$buffer);
259
if(empty($line)) { break; }
265
function pop_list ($msgNum = "") {
266
// If called with an argument, returns that msgs' size in octets
267
// No argument returns an associative array of undeleted
268
// msg numbers and their sizes in octets
270
if(!isset($this->FP))
272
$this->ERROR = "POP3 pop_list: " . _("No connection to server");
276
$Total = $this->COUNT;
277
if( (!$Total) or ($Total == -1) )
283
return array("0","0");
284
// return -1; // mailbox empty
287
$this->update_timer();
291
$cmd = "LIST $msgNum";
292
fwrite($fp,"$cmd\r\n");
293
$reply = fgets($fp,$this->BUFFER);
294
$reply = $this->strip_clf($reply);
296
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
298
if(!$this->is_ok($reply))
300
$this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
303
list($junk,$num,$size) = preg_split('/\s+/',$reply);
307
$reply = $this->send_cmd($cmd);
308
if(!$this->is_ok($reply))
310
$reply = $this->strip_clf($reply);
311
$this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
315
$MsgArray[0] = $Total;
316
for($msgC=1;$msgC <= $Total; $msgC++)
318
if($msgC > $Total) { break; }
319
$line = fgets($fp,$this->BUFFER);
320
$line = $this->strip_clf($line);
321
if(strpos($line, '.') === 0)
323
$this->ERROR = "POP3 pop_list: " . _("Premature end of list");
326
list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
327
settype($thisMsg,"integer");
328
if($thisMsg != $msgC)
330
$MsgArray[$msgC] = "deleted";
334
$MsgArray[$msgC] = $msgSize;
340
function get ($msgNum) {
341
// Retrieve the specified msg number. Returns an array
342
// where each line of the msg is an array element.
344
if(!isset($this->FP))
346
$this->ERROR = "POP3 get: " . _("No connection to server");
350
$this->update_timer();
353
$buffer = $this->BUFFER;
354
$cmd = "RETR $msgNum";
355
$reply = $this->send_cmd($cmd);
357
if(!$this->is_ok($reply))
359
$this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
366
$line = fgets($fp,$buffer);
367
while ( !preg_match('/^\.\r\n/',$line))
369
if ( $line{0} == '.' ) { $line = substr($line,1); }
370
$MsgArray[$count] = $line;
372
$line = fgets($fp,$buffer);
373
if(empty($line)) { break; }
378
function last ( $type = "count" ) {
379
// Returns the highest msg number in the mailbox.
380
// returns -1 on error, 0+ on success, if type != count
381
// results in a popstat() call (2 element array returned)
384
if(!isset($this->FP))
386
$this->ERROR = "POP3 last: " . _("No connection to server");
390
$reply = $this->send_cmd("STAT");
391
if(!$this->is_ok($reply))
393
$this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
397
$Vars = preg_split('/\s+/',$reply);
400
settype($count,"integer");
401
settype($size,"integer");
404
return array($count,$size);
410
// Resets the status of the remote server. This includes
411
// resetting the status of ALL msgs to not be deleted.
412
// This method automatically closes the connection to the server.
414
if(!isset($this->FP))
416
$this->ERROR = "POP3 reset: " . _("No connection to server");
419
$reply = $this->send_cmd("RSET");
420
if(!$this->is_ok($reply))
422
// The POP3 RSET command -never- gives a -ERR
423
// response - if it ever does, something truely
426
$this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
427
@error_log("POP3 reset: ERROR [$reply]",0);
433
function send_cmd ( $cmd = "" )
435
// Sends a user defined command string to the
436
// POP server and returns the results. Useful for
437
// non-compliant or custom POP servers.
438
// Do NOT includ the \r\n as part of your command
439
// string - it will be appended automatically.
441
// The return value is a standard fgets() call, which
442
// will read up to $this->BUFFER bytes of data, until it
443
// encounters a new line, or EOF, whichever happens first.
445
// This method works best if $cmd responds with only
448
if(!isset($this->FP))
450
$this->ERROR = "POP3 send_cmd: " . _("No connection to server");
456
$this->ERROR = "POP3 send_cmd: " . _("Empty command string");
461
$buffer = $this->BUFFER;
462
$this->update_timer();
463
fwrite($fp,"$cmd\r\n");
464
$reply = fgets($fp,$buffer);
465
$reply = $this->strip_clf($reply);
466
if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
471
// Closes the connection to the POP3 server, deleting
472
// any msgs marked as deleted.
474
if(!isset($this->FP))
476
$this->ERROR = "POP3 quit: " . _("connection does not exist");
481
fwrite($fp,"$cmd\r\n");
482
$reply = fgets($fp,$this->BUFFER);
483
$reply = $this->strip_clf($reply);
484
if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
490
function popstat () {
491
// Returns an array of 2 elements. The number of undeleted
492
// msgs in the mailbox, and the size of the mbox in octets.
494
$PopArray = $this->last("array");
496
if($PopArray == -1) { return false; }
498
if( (!$PopArray) or (empty($PopArray)) )
505
function uidl ($msgNum = "")
507
// Returns the UIDL of the msg specified. If called with
508
// no arguments, returns an associative array where each
509
// undeleted msg num is a key, and the msg's uidl is the element
510
// Array element 0 will contain the total number of msgs
512
if(!isset($this->FP)) {
513
$this->ERROR = "POP3 uidl: " . _("No connection to server");
518
$buffer = $this->BUFFER;
520
if(!empty($msgNum)) {
521
$cmd = "UIDL $msgNum";
522
$reply = $this->send_cmd($cmd);
523
if(!$this->is_ok($reply))
525
$this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
528
list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
531
$this->update_timer();
533
$UIDLArray = array();
534
$Total = $this->COUNT;
535
$UIDLArray[0] = $Total;
542
fwrite($fp, "UIDL\r\n");
543
$reply = fgets($fp, $buffer);
544
$reply = $this->strip_clf($reply);
545
if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
546
if(!$this->is_ok($reply))
548
$this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
554
$line = fgets($fp,$buffer);
555
while ( !preg_match('/^\.\r\n/',$line)) {
556
list ($msg,$msgUidl) = preg_split('/\s+/',$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( stripos($cmd, '+OK') !== false );
608
function strip_clf ($text = "") {
609
// Strips \r\n from server responses
614
$stripped = str_replace(array("\r","\n"),'',$text);
619
function parse_banner ( $server_text ) {
622
$length = strlen($server_text);
623
for($count =0; $count < $length; $count++)
625
$digit = substr($server_text,$count,1);
627
if( (!$outside) && ($digit != '<') && ($digit != '>') )
641
$banner = $this->strip_clf($banner); // Just in case
647
// For php4 compatibility
648
if (!function_exists("stripos")) {
649
function stripos($haystack, $needle){
650
return strpos($haystack, stristr( $haystack, $needle ));