imapprotocol.php

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

PHP
2,423
字号
        }

        $this->_getNextToken( $args , $plus );

        $this->_getNextToken( $args , $space );

        $this->_getNextToken( $args , $challenge );

        $challenge = base64_decode( $challenge );

        $digest = &Auth_SASL::factory('digestmd5');

        $auth_str = base64_encode($digest->getResponse($uid, $pwd, $challenge,"localhost", "imap"));

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

        if ( PEAR::isError( $args = $this->_recvLn() )) {
            return $args;
        }
        /*
         * We don't use the protocol's third step because IMAP doesn't allow
         * subsequent authentication, so we just silently ignore it.
         */
        if ( PEAR::isError( $error = $this->_send( "\r\n" ) ) ) {
            return $error;
        }
    }








     /* Authenticates the user using the CRAM-MD5 method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     * @param string The cmdID.
     *
     * @return array Returns an array containing the response
     *
     * @access private
     * @since  1.0
     */
    function _authCRAM_MD5($uid, $pwd, $cmdid)
    {



        if ( PEAR::isError($error = $this->_putCMD( $cmdid ,"AUTHENTICATE" , "CRAM-MD5") ) ) {
            return $error;
        }

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

        $this->_getNextToken( $args , $plus );

        $this->_getNextToken( $args , $space );

        $this->_getNextToken( $args , $challenge );

        $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."\r\n" ) ) ) {
            return $error;
        }

    }









     /* Authenticates the user using the LOGIN method.
     *
     * @param string The userid to authenticate as.
     * @param string The password to authenticate with.
     * @param string The cmdID.
     *
     * @return array Returns an array containing the response
     *
     * @access private
     * @since  1.0
     */
    function _authLOGIN($uid, $pwd, $cmdid)
    {

        if (PEAR::isError($error = $this->_putCMD($cmdid,"AUTHENTICATE", "LOGIN"))) {
            return $error;
        }

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

        $this->_getNextToken( $args , $plus );

        $this->_getNextToken( $args , $space );

        $this->_getNextToken( $args , $challenge );

        $challenge = base64_decode( $challenge );

        $auth_str = base64_encode( "$uid" );

        if ( PEAR::isError( $error = $this->_send( $auth_str."\r\n" ) ) ) {
            return $error;
        }

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

        $auth_str = base64_encode( "$pwd" );

        if ( PEAR::isError($error = $this->_send( $auth_str."\r\n" ) ) ) {
            return $error;
        }

    }








    /**
     * Returns the name of the best authentication method that the server
     * has advertised.
     *
     * @param string if !=null,authenticate with this method ($userMethod).
     *
     * @return mixed    Returns a string containing the name of the best
     *                  supported authentication method or a PEAR_Error object
     *                  if a failure condition is encountered.
     * @access private
     * @since  1.0
     */
    function _getBestAuthMethod($userMethod = null)
    {
       $this->cmdCapability();

        if($userMethod != null ){

            $methods = array();

            $methods[] = $userMethod;

        }else{
            $methods = $this->supportedAuthMethods;
        }

        if( ($methods != null) && ($this->_serverAuthMethods != null)){
            foreach ( $methods as $method ) {
                if ( in_array( $method , $this->_serverAuthMethods ) ) {
                    return $method;
                }
            }
            $serverMethods=implode(',' ,$this->_serverAuthMethods);
            $myMethods=implode(',' ,$this->supportedAuthMethods);
            return new PEAR_Error("$method NOT supported authentication method!. This IMAP server " .
                "supports these methods: $serverMethods, but I support $myMethods");
        }else{
            return new PEAR_Error("This IMAP server don't support any Auth methods");
        }
    }









    /**
     * Attempt to disconnect from the iMAP server.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdLogout()
    {
        if( !$this->_connected ){
            return new PEAR_Error( 'not connected!' );
        }

        $cmdid = $this->_getCmdId();
        if ( PEAR::isError( $error = $this->_putCMD( $cmdid , 'LOGOUT' ) ) ) {
            return $error;
        }
        if ( PEAR::isError($args = $this->_getRawResponse() ) ) {
            return $args;
        }
        if (PEAR::isError( $this->_socket->disconnect() ) ) {
            return new PEAR_Error('socket disconnect failed');
        }

        return $args;
        // not for now
        //return $this->_genericImapResponseParser($args,$cmdid);

    }





    /**
     * Send the NOOP command.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdNoop()
    {
        return $this->_genericCommand('NOOP');
    }









    /**
     * Send the CHECK command.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdCheck()
    {
        return $this->_genericCommand('CHECK');
    }










    /**
     * Send the  Select Mailbox Command
     *
     * @param string The mailbox to select.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdSelect($mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        if( !PEAR::isError( $ret= $this->_genericCommand('SELECT', $mailbox_name) ) ){
            $this->currentMailbox  = $mailbox;
        }
        return $ret;
    }










    /**
     * Send the  EXAMINE  Mailbox Command
     *
     * @param string The mailbox to examine.
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdExamine($mailbox)
    {

        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        $ret=$this->_genericCommand('EXAMINE', $mailbox_name);
        $parsed='';
        if(isset( $ret["PARSED"] ) ){
            for($i=0;$i<count($ret["PARSED"]); $i++){ $command=$ret["PARSED"][$i]["EXT"];
                    $parsed[key($command)]=$command[key($command)];
            }
        }
        return array("PARSED"=>$parsed,"RESPONSE"=>$ret["RESPONSE"]);
    }







    /**
     * Send the  CREATE Mailbox Command
     *
     * @param string The mailbox to create.
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdCreate($mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        return $this->_genericCommand('CREATE', $mailbox_name);
    }







    /**
     * Send the  RENAME Mailbox Command
     *
     * @param string The old mailbox name.
     * @param string The new (renamed) mailbox name.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdRename($mailbox, $new_mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        $new_mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($new_mailbox) );
        return $this->_genericCommand('RENAME', "$mailbox_name $new_mailbox_name" );
    }








    /**
     * Send the  DELETE Mailbox Command
     *
     * @param string The mailbox name to delete.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdDelete($mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        return $this->_genericCommand('DELETE', $mailbox_name);
    }







    /**
     * Send the  SUSCRIBE  Mailbox Command
     *
     * @param string The mailbox name to suscribe.
     *
     * @return array Returns an array containing the response
     *
     * @access public
     * @since  1.0
     */
    function cmdSubscribe($mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        return $this->_genericCommand('SUBSCRIBE', $mailbox_name );
    }








    /**
     * Send the  UNSUSCRIBE  Mailbox Command
     *
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or true on success.
     * @access public
     * @since  1.0
     */
    function cmdUnsubscribe($mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        return $this->_genericCommand('UNSUBSCRIBE', $mailbox_name );
    }








    /**
     * Send the  FETCH Command
     *
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or true on success.
     * @access public
     * @since  1.0
     */
    function cmdFetch($msgset, $fetchparam)
    {
        return $this->_genericCommand('FETCH' , "$msgset $fetchparam" );
    }







    /**
     * Send the  CAPABILITY Command
     *
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or true on success.
     * @access public
     * @since  1.0
     */
    function cmdCapability()
    {
        $ret = $this->_genericCommand( 'CAPABILITY' );

        if(isset( $ret["PARSED"] ) ){
            $ret["PARSED"]=$ret["PARSED"][0]["EXT"]["CAPABILITY"];
            //fill the $this->_serverAuthMethods and $this->_serverSupportedCapabilities arrays
            foreach( $ret["PARSED"]["CAPABILITIES"] as $auth_method ){
                if( strtoupper( substr( $auth_method , 0 ,5 ) ) == "AUTH=" )
                    $this->_serverAuthMethods[] = substr( $auth_method , 5 );
            }
            // Keep the capabilities response to use ir later
            $this->_serverSupportedCapabilities = $ret["PARSED"]["CAPABILITIES"];
        }

        return $ret;
    }



⌨️ 快捷键说明

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