pop3.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,227 行 · 第 1/3 页

PHP
1,227
字号
                break;
            case 'APOP':
                $result = $this->_cmdApop( $uid , $pwd );
                // if APOP fails fallback to USER auth
                if( PEAR::isError( $result ) ){
                    //echo "APOP FAILED!!!\n";
                    $result=$this->_authUSER( $uid , $pwd );
                }
                break;
            case 'USER':
                $result = $this->_authUSER( $uid , $pwd );
            break;


            default :
                $result = $this->_raiseError( "$method is not a supported authentication method" );
                break;
        }
        return $result;
    }




     /* Authenticates the user using the USER-PASS method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     *
     * @return mixed    true on success or PEAR_Error on failure
     *
     * @access private
     * @since  1.0
     */
    function _authUSER($user, $pass  )
    {
        if ( PEAR::isError($ret=$this->_cmdUser($user) ) ){
            return $ret;
        }
        if ( PEAR::isError($ret=$this->_cmdPass($pass) ) ){
            return $ret;
        }
        return true;
    }








     /* Authenticates the user using the PLAIN method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     *
     * @return array Returns an array containing the response
     *
     * @access private
     * @since  1.0
     */
    function _authPLAIN($user, $pass  )
    {
        $cmd=sprintf('AUTH PLAIN %s', base64_encode( chr(0) . $user . chr(0) . $pass ) );

        if ( PEAR::isError( $ret = $this->_send($cmd) ) ) {
            return $ret;
        }
        if ( PEAR::isError( $challenge = $this->_recvLn() ) ){
            return $challenge;
        }
        if( PEAR::isError($ret=$this->_checkResponse($challenge) )){
            return $ret;
        }
        
        return true;
    }



     /* Authenticates the user using the PLAIN method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     *
     * @return array Returns an array containing the response
     *
     * @access private
     * @since  1.0
     */
    function _authLOGIN($user, $pass  )
    {
        $this->_send('AUTH LOGIN');

        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        if( PEAR::isError($ret=$this->_checkResponse($challenge) )){
            return $ret;
        }


        if ( PEAR::isError( $ret = $this->_send(sprintf('%s', base64_encode($user))) ) ) {
            return $ret;
        }

        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        if( PEAR::isError($ret=$this->_checkResponse($challenge) )){
            return $ret;
        }

        if ( PEAR::isError( $ret = $this->_send(sprintf('%s', base64_encode($pass))) ) ) {
            return $ret;
        }

        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        return $this->_checkResponse($challenge);
    }





     /* Authenticates the user using the CRAM-MD5 method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     *
     * @return array Returns an array containing the response
     *
     * @access private
     * @since  1.0
     */
    function _authCRAM_MD5($uid, $pwd )
    {
        if ( PEAR::isError( $ret = $this->_send( 'AUTH CRAM-MD5' ) ) ) {
            return $ret;
        }

        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        if( PEAR::isError($ret=$this->_checkResponse($challenge) )){
            return $ret;
        }

        // remove '+ '
        
        $challenge=substr($challenge,2);
        
        $challenge = base64_decode( $challenge );

        $cram = &Auth_SASL::factory('crammd5');
        $auth_str = base64_encode( $cram->getResponse( $uid , $pwd , $challenge ) );


        if ( PEAR::isError($error = $this->_send( $auth_str ) ) ) {
            return $error;
        }
        if ( PEAR::isError( $ret = $this->_recvLn() ) ) {
            return $ret;
        }
        //echo "RET:$ret\n";
        return $this->_checkResponse($ret);
    }



     /* Authenticates the user using the DIGEST-MD5 method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     * @param string The efective user
     *
     * @return array Returns an array containing the response
     *
     * @access private
     * @since  1.0
     */
    function _authDigest_MD5($uid, $pwd)
    {
        if ( PEAR::isError( $ret = $this->_send( 'AUTH DIGEST-MD5' ) ) ) {
            return $ret;
        }

        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        if( PEAR::isError($ret=$this->_checkResponse($challenge) )){
            return $ret;
        }

        // remove '+ '
        $challenge=substr($challenge,2);

        $challenge = base64_decode( $challenge );
        $digest = &Auth_SASL::factory('digestmd5');
        $auth_str = base64_encode($digest->getResponse($uid, $pwd, $challenge, "localhost", "pop3" ));

        if ( PEAR::isError($error = $this->_send( $auth_str ) ) ) {
            return $error;
        }

        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        if( PEAR::isError($ret=$this->_checkResponse($challenge) )){
            return $ret;
        }
         /*
         * We don't use the protocol's third step because POP3 doesn't allow
         * subsequent authentication, so we just silently ignore it.
         */

        if ( PEAR::isError( $challenge = $this->_send("\r\n") ) ) {
            return $challenge ;
        }
        
        if ( PEAR::isError( $challenge = $this->_recvLn() ) ) {
            return $challenge;
        }
        
        return $this->_checkResponse($challenge);
        

    }










    /*
    * Sends the APOP command
    *
    * @param  $user Username to send
    * @param  $pass Password to send
    * @return bool Success/Failure
    */
    function _cmdApop($user, $pass)
    {
        if ($this->_state == NET_POP3_STATE_AUTHORISATION) {

            if (!empty($this->_timestamp)) {
                if(PEAR::isError($data = $this->_sendCmd('APOP ' . $user . ' ' . md5($this->_timestamp . $pass)) ) ){
                    return $data;
                }
                $this->_state = NET_POP3_STATE_TRANSACTION;
                return true;
            }
        }
        return $this->_raiseError('Not In NET_POP3_STATE_AUTHORISATION State1');
    }















    /*
    * Returns the raw headers of the specified message.
    *
    * @param  integer $msg_id Message number
    * @return mixed   Either raw headers or false on error
    */
    function getRawHeaders($msg_id)
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {
            return $this->_cmdTop($msg_id, 0);
        }

        return false;
    }

    /*
    * Returns the  headers of the specified message in an
    * associative array. Array keys are the header names, array
    * values are the header values. In the case of multiple headers
    * having the same names, eg Received:, the array value will be
    * an indexed array of all the header values.
    *
    * @param  integer $msg_id Message number
    * @return mixed   Either array of headers or false on error
    */
    function getParsedHeaders($msg_id)
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {

            $raw_headers = rtrim($this->getRawHeaders($msg_id));

            $raw_headers = preg_replace("/\r\n[ \t]+/", ' ', $raw_headers); // Unfold headers
            $raw_headers = explode("\r\n", $raw_headers);
            foreach ($raw_headers as $value) {
                $name  = substr($value, 0, $pos = strpos($value, ':'));
                $value = ltrim(substr($value, $pos + 1));
                if (isset($headers[$name]) AND is_array($headers[$name])) {
                    $headers[$name][] = $value;
                } elseif (isset($headers[$name])) {
                    $headers[$name] = array($headers[$name], $value);
                } else {
                    $headers[$name] = $value;
                }
            }

            return $headers;
        }

        return false;
    }

    /*
    * Returns the body of the message with given message number.
    *
    * @param  integer $msg_id Message number
    * @return mixed   Either message body or false on error
    */
    function getBody($msg_id)
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {
            $msg = $this->_cmdRetr($msg_id);
            return substr($msg, strpos($msg, "\r\n\r\n")+4);
        }

        return false;
    }

    /*
    * Returns the entire message with given message number.
    *
    * @param  integer $msg_id Message number
    * @return mixed   Either entire message or false on error
    */
    function getMsg($msg_id)
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {
            return $this->_cmdRetr($msg_id);
        }

        return false;
    }

    /*
    * Returns the size of the maildrop
    *
    * @return mixed Either size of maildrop or false on error
    */
    function getSize()
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {
            if (isset($this->_maildrop['size'])) {
                return $this->_maildrop['size'];
            } else {
                list(, $size) = $this->_cmdStat();
                return $size;
            }
        }

        return false;
    }

    /*
    * Returns number of messages in this maildrop
    *
    * @return mixed Either number of messages or false on error
    */
    function numMsg()
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {
            if (isset($this->_maildrop['num_msg'])) {
                return $this->_maildrop['num_msg'];
            } else {
                list($num_msg, ) = $this->_cmdStat();
                return $num_msg;
            }
        }

        return false;
    }

    /*
    * Marks a message for deletion. Only will be deleted if the
    * disconnect() method is called.
    *
    * @param  integer $msg_id Message to delete
    * @return bool Success/Failure
    */
    function deleteMsg($msg_id)
    {
        if ($this->_state == NET_POP3_STATE_TRANSACTION) {
            return $this->_cmdDele($msg_id);
        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?