📄 class-pop3.php
字号:
<?php
/**
* mail_fetch/setup.php
*
* Copyright (c) 1999-2002 The SquirrelMail Project Team
*
* Copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
* Modified by Philippe Mingo 2001 mingo@rotedic.com
* An RFC 1939 compliant wrapper class for the POP3 protocol.
*
* Licensed under the GNU GPL. For full terms see the file COPYING.
*
* pop3 class
*
* $Id: class-pop3.php,v 1.1.1.1 2004/11/02 02:18:24 phppp Exp $
*/
class POP3 {
var $ERROR = ''; // Error string.
var $TIMEOUT = 60; // Default timeout before giving up on a
// network operation.
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
// file descriptor
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 = FALSE; // Set by noop(). See rfc1939.txt
//
var $ALLOWAPOP = FALSE; // Allow or disallow apop()
// This must be set to true
// manually
function POP3 ( $server = '', $timeout = '' ) {
settype($this->BUFFER,"integer");
if( !empty($server) ) {
// 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.
if(empty($this->MAILSERVER))
$this->MAILSERVER = $server;
}
if(!empty($timeout)) {
settype($timeout,"integer");
$this->TIMEOUT = $timeout;
set_time_limit($timeout);
}
return true;
}
function update_timer () {
set_time_limit($this->TIMEOUT);
return true;
}
function connect ($server, $port = 110) {
// Opens a socket to the specified server. Unless overridden,
// port defaults to 110. Returns true on success, false on fail
// If MAILSERVER is set, override $server with it's value
if(!empty($this->MAILSERVER))
$server = $this->MAILSERVER;
if(empty($server)){
$this->ERROR = _("POP3 connect:") . ' ' . _("No server specified");
unset($this->FP);
return false;
}
$fp = fsockopen("$server", $port, $errno, $errstr);
if(!$fp) {
$this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
unset($this->FP);
return false;
}
socket_set_blocking($fp,-1);
$this->update_timer();
$reply = fgets($fp,$this->BUFFER);
$reply = $this->strip_clf($reply);
if($this->DEBUG)
error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
if(!$this->is_ok($reply)) {
$this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
unset($this->FP);
return false;
}
$this->FP = $fp;
$this->BANNER = $this->parse_banner($reply);
// $this->RFC1939 = $this->noop();
// if($this->RFC1939) {
// $this->ERROR = _("POP3: premature NOOP OK, NOT an RFC 1939 Compliant server");
// $this->quit();
// return false;
// } else
return true;
}
function noop () {
if(!isset($this->FP)) {
$this->ERROR = _("POP3 noop:") . ' ' . _("No connection to server");
return false;
} else {
$cmd = "NOOP";
$reply = $this->send_cmd( $cmd );
return( $this->is_ok( $reply ) );
}
}
function user ($user = "") {
// Sends the USER command, returns true or false
if( empty($user) ) {
$this->ERROR = _("POP3 user:") . ' ' . _("no login ID submitted");
return false;
} elseif(!isset($this->FP)) {
$this->ERROR = _("POP3 user:") . ' ' . _("connection not established");
return false;
} else {
$reply = $this->send_cmd("USER $user");
if(!$this->is_ok($reply)) {
$this->ERROR = _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
return false;
} else
return true;
}
}
function pass ($pass = "") {
// Sends the PASS command, returns # of msgs in mailbox,
// returns false (undef) on Auth failure
if(empty($pass)) {
$this->ERROR = _("POP3 pass:") . ' ' . _("No password submitted");
return false;
} elseif(!isset($this->FP)) {
$this->ERROR = _("POP3 pass:") . ' ' . _("connection not established");
return false;
} else {
$reply = $this->send_cmd("PASS $pass");
if(!$this->is_ok($reply)) {
$this->ERROR = _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
$this->quit();
return false;
} else {
// Auth successful.
$count = $this->last("count");
$this->COUNT = $count;
$this->RFC1939 = $this->noop();
if(!$this->RFC1939) {
$this->ERROR = _("POP3 pass:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
$this->quit();
return false;
} else
return $count;
}
}
}
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)
if(!isset($this->FP)) {
$this->ERROR = _("POP3 apop:") . ' ' . _("No connection to server");
return false;
} elseif(!$this->ALLOWAPOP) {
$retVal = $this->login($login,$pass);
return $retVal;
} elseif(empty($login)) {
$this->ERROR = _("POP3 apop:") . ' ' . _("No login ID submitted");
return false;
} elseif(empty($pass)) {
$this->ERROR = _("POP3 apop:") . ' ' . _("No password submitted");
return false;
} else {
$banner = $this->BANNER;
if( (!$banner) or (empty($banner)) ) {
$this->ERROR = _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
$retVal = $this->login($login,$pass);
return $retVal;
} else {
$AuthString = $banner;
$AuthString .= $pass;
$APOPString = md5($AuthString);
$cmd = "APOP $login $APOPString";
$reply = $this->send_cmd($cmd);
if(!$this->is_ok($reply)) {
$this->ERROR = _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
$retVal = $this->login($login,$pass);
return $retVal;
} else {
// Auth successful.
$count = $this->last("count");
$this->COUNT = $count;
$this->RFC1939 = $this->noop();
if(!$this->RFC1939) {
$this->ERROR = _("POP3 apop:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
$this->quit();
return false;
} else
return $count;
}
}
}
}
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 = _("POP3 login:") . ' ' . _("No connection to server");
return false;
} else {
$fp = $this->FP;
if( !$this->user( $login ) ) {
// Preserve the error generated by user()
return false;
} else {
$count = $this->pass($pass);
if( (!$count) || ($count == -1) ) {
// Preserve the error generated by last() and pass()
return false;
} else
return $count;
}
}
}
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.
if(!isset($this->FP)) {
$this->ERROR = _("POP3 top:") . ' ' . _("No connection to server");
return false;
}
$this->update_timer();
$fp = $this->FP;
$buffer = $this->BUFFER;
$cmd = "TOP $msgNum $numLines";
fwrite($fp, "TOP $msgNum $numLines\r\n");
$reply = fgets($fp, $buffer);
$reply = $this->strip_clf($reply);
if($this->DEBUG) {
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
}
if(!$this->is_ok($reply))
{
$this->ERROR = _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
return false;
}
$count = 0;
$MsgArray = array();
$line = fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line))
{
$MsgArray[$count] = $line;
$count++;
$line = fgets($fp,$buffer);
if(empty($line)) { break; }
}
return $MsgArray;
}
function pop_list ($msgNum = "") {
// 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
if(!isset($this->FP))
{
$this->ERROR = _("POP3 pop_list:") . ' ' . _("No connection to server");
return false;
}
$fp = $this->FP;
$Total = $this->COUNT;
if( (!$Total) or ($Total == -1) )
{
return false;
}
if($Total == 0)
{
return array("0","0");
// return -1; // mailbox empty
}
$this->update_timer();
if(!empty($msgNum))
{
$cmd = "LIST $msgNum";
fwrite($fp,"$cmd\r\n");
$reply = fgets($fp,$this->BUFFER);
$reply = $this->strip_clf($reply);
if($this->DEBUG) {
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
}
if(!$this->is_ok($reply))
{
$this->ERROR = _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
return false;
}
list($junk,$num,$size) = explode(" ",$reply);
return $size;
}
$cmd = "LIST";
$reply = $this->send_cmd($cmd);
if(!$this->is_ok($reply))
{
$reply = $this->strip_clf($reply);
$this->ERROR = _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -