pop3.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,227 行 · 第 1/3 页
PHP
1,227 行
return false;
}
/*
* Combination of LIST/UIDL commands, returns an array
* of data
*
* @param integer $msg_id Optional message number
* @return mixed Array of data or false on error
*/
function getListing($msg_id = null)
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if (!isset($msg_id)){
$list=array();
if ($list = $this->_cmdList()) {
if ($uidl = $this->_cmdUidl()) {
foreach ($uidl as $i => $value) {
$list[$i]['uidl'] = $value['uidl'];
}
}
return $list;
}else{
return array();
}
} else {
if ($list = $this->_cmdList($msg_id) AND $uidl = $this->_cmdUidl($msg_id)) {
return array_merge($list, $uidl);
}
}
}
return false;
}
/*
* Sends the USER command
*
* @param string $user Username to send
* @return bool Success/Failure
*/
function _cmdUser($user)
{
if ($this->_state == NET_POP3_STATE_AUTHORISATION) {
return $this->_sendCmd('USER ' . $user);
}
return $this->_raiseError('Not In NET_POP3_STATE_AUTHORISATION State');
}
/*
* Sends the PASS command
*
* @param string $pass Password to send
* @return bool Success/Failure
*/
function _cmdPass($pass)
{
if ($this->_state == NET_POP3_STATE_AUTHORISATION) {
return $this->_sendCmd('PASS ' . $pass);
}
return $this->_raiseError('Not In NET_POP3_STATE_AUTHORISATION State');
}
/*
* Sends the STAT command
*
* @return mixed Indexed array of number of messages and
* maildrop size, or false on error.
*/
function _cmdStat()
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if(!PEAR::isError($data = $this->_sendCmd('STAT'))){
sscanf($data, '+OK %d %d', $msg_num, $size);
$this->_maildrop['num_msg'] = $msg_num;
$this->_maildrop['size'] = $size;
return array($msg_num, $size);
}
}
return false;
}
/*
* Sends the LIST command
*
* @param integer $msg_id Optional message number
* @return mixed Indexed array of msg_id/msg size or
* false on error
*/
function _cmdList($msg_id = null)
{
$return=array();
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if (!isset($msg_id)) {
if(!PEAR::isError($data = $this->_sendCmd('LIST') )){
$data = $this->_getMultiline();
$data = explode("\r\n", $data);
foreach ($data as $line) {
if($line !=''){
sscanf($line, '%s %s', $msg_id, $size);
$return[] = array('msg_id' => $msg_id, 'size' => $size);
}
}
return $return;
}
} else {
if(!PEAR::isError($data = $this->_sendCmd('LIST ' . $msg_id))){
if($data!=''){
sscanf($data, '+OK %d %d', $msg_id, $size);
return array('msg_id' => $msg_id, 'size' => $size);
}
return array();
}
}
}
return false;
}
/*
* Sends the RETR command
*
* @param integer $msg_id The message number to retrieve
* @return mixed The message or false on error
*/
function _cmdRetr($msg_id)
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if(!PEAR::isError($data = $this->_sendCmd('RETR ' . $msg_id) )){
$data = $this->_getMultiline();
return $data;
}
}
return false;
}
/*
* Sends the DELE command
*
* @param integer $msg_id Message number to mark as deleted
* @return bool Success/Failure
*/
function _cmdDele($msg_id)
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
return $this->_sendCmd('DELE ' . $msg_id);
}
return false;
}
/*
* Sends the NOOP command
*
* @return bool Success/Failure
*/
function _cmdNoop()
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if(!PEAR::isError($data = $this->_sendCmd('NOOP'))){
return true;
}
}
return false;
}
/*
* Sends the RSET command
*
* @return bool Success/Failure
*/
function _cmdRset()
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if(!PEAR::isError($data = $this->_sendCmd('RSET'))){
return true;
}
}
return false;
}
/*
* Sends the QUIT command
*
* @return bool Success/Failure
*/
function _cmdQuit()
{
$data = $this->_sendCmd('QUIT');
$this->_state = NET_POP3_STATE_DISCONNECTED;
$this->_socket->disconnect();
return (bool)$data;
}
/*
* Sends the TOP command
*
* @param integer $msg_id Message number
* @param integer $num_lines Number of lines to retrieve
* @return mixed Message data or false on error
*/
function _cmdTop($msg_id, $num_lines)
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if(!PEAR::isError($data = $this->_sendCmd('TOP ' . $msg_id . ' ' . $num_lines))){
return $this->_getMultiline();
}
}
return false;
}
/*
* Sends the UIDL command
*
* @param integer $msg_id Message number
* @return mixed indexed array of msg_id/uidl or false on error
*/
function _cmdUidl($msg_id = null)
{
if ($this->_state == NET_POP3_STATE_TRANSACTION) {
if (!isset($msg_id)) {
if(!PEAR::isError($data = $this->_sendCmd('UIDL') )){
$data = $this->_getMultiline();
$data = explode("\r\n", $data);
foreach ($data as $line) {
sscanf($line, '%d %s', $msg_id, $uidl);
$return[] = array('msg_id' => $msg_id, 'uidl' => $uidl);
}
return $return;
}
} else {
$data = $this->_sendCmd('UIDL ' . $msg_id);
sscanf($data, '+OK %d %s', $msg_id, $uidl);
return array('msg_id' => $msg_id, 'uidl' => $uidl);
}
}
return false;
}
/*
* Sends a command, checks the reponse, and
* if good returns the reponse, other wise
* returns false.
*
* @param string $cmd Command to send (\r\n will be appended)
* @return mixed First line of response if successful, otherwise false
*/
function _sendCmd($cmd)
{
if (PEAR::isError($result = $this->_send($cmd) )){
return $result ;
}
if (PEAR::isError($data = $this->_recvLn() )){
return $data;
}
if ( strtoupper(substr($data, 0, 3)) == '+OK') {
return $data;
}
return $this->_raiseError($data);
}
/*
* Reads a multiline reponse and returns the data
*
* @return string The reponse.
*/
function _getMultiline()
{
$data = '';
while(!PEAR::isError($tmp = $this->_recvLn() ) ) {
if($tmp == '.'){
return substr($data, 0, -2);
}
if (substr($tmp, 0, 2) == '..') {
$tmp = substr($tmp, 1);
}
$data .= $tmp . "\r\n";
}
return substr($data, 0, -2);
}
/**
* Sets the bebug state
*
* @param bool $debug
* @access public
* @return void
*/
function setDebug($debug=true)
{
$this->_debug=$debug;
}
/**
* Send the given string of data to the server.
*
* @param string $data The string of data to send.
*
* @return mixed True on success or a PEAR_Error object on failure.
*
* @access private
* @since 1.0
*/
function _send($data)
{
if ($this->_debug) {
echo "C: $data\n";
}
if (PEAR::isError($error = $this->_socket->writeLine($data))) {
return $this->_raiseError('Failed to write to socket: ' . $error->getMessage());
}
return true;
}
/**
* Receive the given string of data from the server.
*
* @return mixed a line of response on success or a PEAR_Error object on failure.
*
* @access private
* @since 1.0
*/
function _recvLn()
{
if (PEAR::isError( $lastline = $this->_socket->readLine( 8192 ) ) ) {
return $this->_raiseError('Failed to write to socket: ' . $this->lastline->getMessage() );
}
if($this->_debug){
// S: means this data was sent by the POP3 Server
echo "S:$lastline\n" ;
}
return $lastline;
}
/**
* Checks de server Response
*
* @param string $response the response
* @return mixed true on success or a PEAR_Error object on failure.
*
* @access private
* @since 1.3.3
*/
function _checkResponse($response)
{
if (@substr(strtoupper($response), 0, 3) == '+OK') {
return true;
}else{
if (@substr(strtoupper($response), 0, 4) == '-ERR') {
return $this->_raiseError($response);
}else{
if (@substr(strtoupper($response), 0, 2) == '+ ') {
return true;
}
}
}
return $this->_raiseError("Unknown Response ($response)");
}
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?