Source for file _class_pop3.php
Documentation is available at _class_pop3.php
* An RFC 1939 compliant wrapper class for the POP3 protocol.
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
* @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
* @copyright (c)1999-2002 The SquirrelMail Project Team
* @copyright (c)1999 CDI (cdi@thewebmasters.net) All Rights Reserved
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );
* An RFC 1939 compliant wrapper class for the POP3 protocol.
var $ERROR =
''; // Error string.
var $TIMEOUT =
60; // Default timeout before giving up on a
var $COUNT = -
1; // Mailbox msg count
var $BUFFER =
512; // Socket buffer for socket fgets() calls.
// Per RFC 1939 the returned line a POP3
// server can send is 512 bytes.
var $FP =
''; // The connection to the server's
var $MAILSERVER =
''; // Set this to hard code the server name
var $DEBUG =
false; // set to true to echo pop3
// commands and responses to error_log
// this WILL log passwords!
var $BANNER =
''; // Holds the banner returned by the
// pop server - used for apop()
var $RFC1939 =
true; // Set by noop(). See rfc1939.txt
var $ALLOWAPOP =
false; // Allow or disallow apop()
// This must be set to true
function POP3 ( $server =
'', $timeout =
'' ) {
// Do not allow programs to alter MAILSERVER
// if it is already specified. They can get around
// this if they -really- want to, so don't count on it.
* sets/refreshes script timeout
* Opens a socket to the specified server. Unless overridden,
* @param string server, overriden by MAILSERVER, if not empty
* @param integer port, default 110
* @return true on success, false on fail
function connect ($server, $port =
110)
$this->ERROR =
T_('POP3 connect:') .
' ' .
T_('No server specified');
$fp =
fsockopen($server, $port, $errno, $errstr);
$this->ERROR =
T_('POP3 connect:') .
' ' .
T_('Error') .
" [$errno] [$errstr]";
error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
if(!$this->is_ok($reply)) {
$this->ERROR =
T_('POP3 connect:') .
' ' .
T_('Error') .
" [$reply]";
$this->ERROR =
T_('POP3: premature NOOP OK, NOT an RFC 1939 Compliant server');
$this->ERROR =
T_('POP3 noop:') .
' ' .
T_('No connection to server');
return( $this->is_ok( $reply ) );
function user ($user =
'') {
$this->ERROR =
T_('POP3 user:') .
' ' .
T_('No login ID submitted');
} elseif(!isset
($this->FP)) {
$this->ERROR =
T_('POP3 user:') .
' ' .
T_('connection not established');
if(!$this->is_ok($reply)) {
$this->ERROR =
T_('POP3 user:') .
' ' .
T_('Error') .
" [$reply]";
function pass ($pass =
'') {
// Sends the PASS command, returns # of msgs in mailbox,
// returns false (undef) on Auth failure
$this->ERROR =
T_('POP3 pass:') .
' ' .
T_('No password submitted');
} elseif(!isset
($this->FP)) {
$this->ERROR =
T_('POP3 pass:') .
' ' .
T_('connection not established');
if(!$this->is_ok($reply)) {
$this->ERROR =
T_('POP3 pass:') .
' ' .
T_('authentication failed ') .
"[$reply]";
$count =
$this->last('count');
$this->ERROR =
T_('POP3 pass:') .
' ' .
T_('NOOP failed. Server not RFC 1939 compliant');
function apop ($login,$pass) {
// Attempts an APOP login. If this fails, it'll
// try a standard login. YOUR SERVER MUST SUPPORT
// THE USE OF THE APOP COMMAND!
// (apop is optional per rfc1939)
$this->ERROR =
T_('POP3 apop:') .
' ' .
T_('No connection to server');
$retVal =
$this->login($login,$pass);
} elseif(empty($login)) {
$this->ERROR =
T_('POP3 apop:') .
' ' .
T_('No login ID submitted');
$this->ERROR =
T_('POP3 apop:') .
' ' .
T_('No password submitted');
if( (!$banner) or (empty($banner)) ) {
$this->ERROR =
T_('POP3 apop:') .
' ' .
T_('No server banner') .
' - ' .
T_('abort');
$retVal =
$this->login($login,$pass);
$APOPString =
md5($AuthString);
$cmd =
"APOP $login $APOPString";
if(!$this->is_ok($reply)) {
$this->ERROR =
T_('POP3 apop:') .
' ' .
T_('apop authentication failed') .
' - ' .
T_('abort');
$retVal =
$this->login($login,$pass);
$count =
$this->last('count');
$this->ERROR =
T_('POP3 apop:') .
' ' .
T_('NOOP failed. Server not RFC 1939 compliant');
function login ($login =
'', $pass =
'') {
// Sends both user and pass. Returns # of msgs in mailbox or
// false on failure (or -1, if the error occurs while getting
// the number of messages.)
if( !isset
($this->FP) ) {
$this->ERROR =
T_('POP3 login:') .
' ' .
T_('No connection to server');
if( !$this->user( $login ) ) {
// Preserve the error generated by user()
$count =
$this->pass($pass);
if( (!$count) ||
($count == -
1) ) {
// Preserve the error generated by last() and pass()
function top ($msgNum, $numLines =
'0') {
// Gets the header and first $numLines of the msg body
// returns data in an array with each returned line being
// an array element. If $numLines is empty, returns
// only the header information, and none of the body.
$this->ERROR =
T_('POP3 top:') .
' ' .
T_('No connection to server');
$cmd =
"TOP $msgNum $numLines";
fwrite($fp, "TOP $msgNum $numLines\r\n");
$reply =
fgets($fp, $buffer);
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 top:') .
' ' .
T_('Error') .
" [$reply]";
$line =
fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line))
$MsgArray[$count] =
$line;
$line =
fgets($fp,$buffer);
if(empty($line)) { break; }
// If called with an argument, returns that msgs' size in octets
// No argument returns an associative array of undeleted
// msg numbers and their sizes in octets
$this->ERROR =
T_('POP3 pop_list:') .
' ' .
T_('No connection to server');
if( (!$Total) or ($Total == -
1) )
// return -1; // mailbox empty
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 pop_list:') .
' ' .
T_('Error') .
" [$reply]";
list
($junk,$num,$size) =
explode(' ',$reply);
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 pop_list:') .
' ' .
T_('Error') .
" [$reply]";
for($msgC=
1;$msgC <=
$Total; $msgC++
)
if($msgC >
$Total) { break; }
$this->ERROR =
T_('POP3 pop_list:') .
' ' .
T_('Premature end of list');
list
($thisMsg,$msgSize) =
explode(' ',$line);
$MsgArray[$msgC] =
'deleted';
$MsgArray[$msgC] =
$msgSize;
// Retrieve the specified msg number. Returns an array
// where each line of the msg is an array element.
$this->ERROR =
T_('POP3 get:') .
' ' .
T_('No connection to server');
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 get:') .
' ' .
T_('Error') .
" [$reply]";
$line =
fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line))
$MsgArray[$count] =
$line;
$line =
fgets($fp,$buffer);
if(empty($line)) { break; }
function last ( $type =
'count' ) {
// Returns the highest msg number in the mailbox.
// returns -1 on error, 0+ on success, if type != count
// results in a popstat() call (2 element array returned)
$this->ERROR =
T_('POP3 last:') .
' ' .
T_('No connection to server');
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 last:') .
' ' .
T_('Error') .
" [$reply]";
return array($count,$size);
// Resets the status of the remote server. This includes
// resetting the status of ALL msgs to not be deleted.
// This method automatically closes the connection to the server.
$this->ERROR =
T_('POP3 reset:') .
' ' .
T_('No connection to server');
if(!$this->is_ok($reply))
// The POP3 RSET command -never- gives a -ERR
// response - if it ever does, something truely
$this->ERROR =
T_('POP3 reset:') .
' ' .
T_('Error') .
" [$reply]";
// Sends a user defined command string to the
// POP server and returns the results. Useful for
// non-compliant or custom POP servers.
// Do NOT includ the \r\n as part of your command
// string - it will be appended automatically.
// The return value is a standard fgets() call, which
// will read up to $this->BUFFER bytes of data, until it
// encounters a new line, or EOF, whichever happens first.
// This method works best if $cmd responds with only
$this->ERROR =
T_('POP3 send_cmd:') .
' ' .
T_('No connection to server');
$this->ERROR =
T_('POP3 send_cmd:') .
' ' .
T_('Empty command string');
$reply =
fgets($fp,$buffer);
// Closes the connection to the POP3 server, deleting
// any msgs marked as deleted.
$this->ERROR =
T_('POP3 quit:') .
' ' .
T_('connection does not exist');
// Returns an array of 2 elements. The number of undeleted
// msgs in the mailbox, and the size of the mbox in octets.
$PopArray =
$this->last('array');
if($PopArray == -
1) { return false; }
if( (!$PopArray) or (empty($PopArray)) )
function uidl ($msgNum =
'')
// Returns the UIDL of the msg specified. If called with
// no arguments, returns an associative array where each
// undeleted msg num is a key, and the msg's uidl is the element
// Array element 0 will contain the total number of msgs
$this->ERROR =
T_('POP3 uidl:') .
' ' .
T_('No connection to server');
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 uidl:') .
' ' .
T_('Error') .
" [$reply]";
list
($ok,$num,$myUidl) =
explode(' ',$reply);
$reply =
fgets($fp, $buffer);
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 uidl:') .
' ' .
T_('Error') .
" [$reply]";
$line =
fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line)) {
if(ereg("^\.\r\n",$line)) {
list
($msg,$msgUidl) =
explode(' ',$line);
$UIDLArray[$msg] =
$msgUidl;
$UIDLArray[$count] =
'deleted';
$line =
fgets($fp,$buffer);
function delete ($msgNum =
'') {
// Flags a specified msg as deleted. The msg will not
// be deleted until a quit() method is called.
$this->ERROR =
T_('POP3 delete:') .
' ' .
T_('No connection to server');
$this->ERROR =
T_('POP3 delete:') .
' ' .
T_('No msg number submitted');
$reply =
$this->send_cmd("DELE $msgNum");
if(!$this->is_ok($reply))
$this->ERROR =
T_('POP3 delete:') .
' ' .
T_('Command failed') .
" [$reply]";
// *********************************************************
// The following methods are internal to the class.
function is_ok ($cmd =
'') {
// Return true or false on +OK or -ERR
return( ereg ("^\+OK", $cmd ) );
// Strips \r\n from server responses
$length =
strlen($server_text);
for($count =
0; $count <
$length; $count++
)
$digit =
substr($server_text,$count,1);
if( (!$outside) &&
($digit !=
'<') &&
($digit !=
'>') )
$banner =
$this->strip_clf($banner); // Just in case