imapprotocol.php

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

PHP
2,423
字号







    /**
     * Send the  STATUS Mailbox Command
     *
     * @param string $mailbox the mailbox name
     * @param string $request the request status it could be:
     *              MESSAGES | RECENT | UIDNEXT
     *              UIDVALIDITY | UNSEEN
     * @return array Returns a Parsed Response
     *
     * @access public
     * @since  1.0
     */
    function cmdStatus($mailbox, $request)
    {

        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );

        if( $request!="MESSAGES" && $request!="RECENT" && $request!="UIDNEXT" &&
            $request!="UIDVALIDITY" && $request!="UNSEEN" ){
            // TODO:  fix this error!
            $this->_prot_error("request '$request' is invalid! see RFC2060!!!!" , __LINE__ , __FILE__, false );
        }
        $ret = $this->_genericCommand('STATUS', "$mailbox_name ($request)" );
        if(isset( $ret["PARSED"] ) ){
            $ret['PARSED']=$ret["PARSED"][count($ret['PARSED'])-1]["EXT"];
        }
        return $ret;
    }






    /**
     * Send the  LIST  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 cmdList($mailbox_base, $mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        $mailbox_base=sprintf("\"%s\"",$this->utf_7_encode($mailbox_base) );
        return $this->_genericCommand('LIST', "$mailbox_base $mailbox_name" );
    }






    /**
     * Send the  LSUB  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 cmdLsub($mailbox_base, $mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        $mailbox_base=sprintf("\"%s\"",$this->utf_7_encode($mailbox_base) );
        return $this->_genericCommand('LSUB', "$mailbox_base $mailbox_name" );
    }






    /**
     * Send the  APPEND  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 cmdAppend($mailbox, $msg , $flags_list = '' ,$time = '')
    {
        if(!$this->_connected){
            return new PEAR_Error('not connected!');
        }


        $cmdid=$this->_getCmdId();
        $msg_size=strlen($msg);

        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        // TODO:
        // Falta el codigo para que flags list y time hagan algo!!
        if( $this->hasCapability( "LITERAL+" ) == true ){
            $param=sprintf("%s %s%s{%s+}\r\n%s",$mailbox_name,$flags_list,$time,$msg_size,$msg);
            if (PEAR::isError($error = $this->_putCMD($cmdid , 'APPEND' , $param ) ) ) {
                return $error;
            }
        }else{
            $param=sprintf("%s %s%s{%s}\r\n",$mailbox_name,$flags_list,$time,$msg_size);
            if (PEAR::isError($error = $this->_putCMD($cmdid , 'APPEND' , $param ) ) ) {
            return $error;
            }
            if (PEAR::isError($error = $this->_recvLn() ) ) {
            return $error;
            }

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


        $args=$this->_getRawResponse($cmdid);
        $ret = $this->_genericImapResponseParser($args,$cmdid);
        return $ret;
    }



    /**
     * Send the CLOSE 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 cmdClose()
    {
        return $this->_genericCommand('CLOSE');
    }






    /**
     * Send the EXPUNGE 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 cmdExpunge()
    {
        $ret=$this->_genericCommand('EXPUNGE');

        if(isset( $ret["PARSED"] ) ){
           $parsed=$ret["PARSED"];
            unset($ret["PARSED"]);
            foreach($parsed as $command){
                if( strtoupper($command["COMMAND"]) == 'EXPUNGE' ){
                        $ret["PARSED"][$command["COMMAND"]][]=$command["NRO"];
                }else{
                        $ret["PARSED"][$command["COMMAND"]]=$command["NRO"];
                }
            }
        }
        return $ret;
    }







    /**
     * Send the SEARCH 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 cmdSearch($search_cmd)
    {
        /*        if($_charset != '' )
                    $_charset = "[$_charset] ";
                $param=sprintf("%s%s",$charset,$search_cmd);
        */
        $ret = $this->_genericCommand('SEARCH', $search_cmd );
        if(isset( $ret["PARSED"] ) ){
            $ret["PARSED"]=$ret["PARSED"][0]["EXT"];
        }
        return $ret;
    }






    /**
     * Send the STORE command.
     *
     * @param string $message_set  the sessage_set
     * @param string $dataitem: the way we store the flags
     *          FLAGS: replace the flags whith $value
     *          FLAGS.SILENT: replace the flags whith $value but don't return untagged responses
     *
     *          +FLAGS: Add the flags whith $value
     *          +FLAGS.SILENT: Add the flags whith $value but don't return untagged responses
     *
     *          -FLAGS: Remove the flags whith $value
     *          -FLAGS.SILENT: Remove the flags whith $value but don't return untagged responses
     *
     * @param string $value
     * @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 cmdStore($message_set, $dataitem, $value)
    {
        /* As said in RFC2060...
        C: A003 STORE 2:4 +FLAGS (\Deleted)
        S: * 2 FETCH FLAGS (\Deleted \Seen)
        S: * 3 FETCH FLAGS (\Deleted)
        S: * 4 FETCH FLAGS (\Deleted \Flagged \Seen)
        S: A003 OK STORE completed
        */
        if( $dataitem!="FLAGS" && $dataitem!="FLAGS.SILENT" && $dataitem!="+FLAGS" &&
            $dataitem!="+FLAGS.SILENT" && $dataitem!="-FLAGS" && $dataitem!="-FLAGS.SILENT" ){
            $this->_prot_error("dataitem '$dataitem' is invalid! see RFC2060!!!!" , __LINE__ , __FILE__ );
        }
        $param=sprintf("%s %s (%s)",$message_set,$dataitem,$value);
        return $this->_genericCommand('STORE', $param );
    }







    /**
     * Send the COPY 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 cmdCopy($message_set, $mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        return $this->_genericCommand('COPY', sprintf("%s %s",$message_set,$mailbox_name) );
    }













    function cmdUidFetch($msgset, $fetchparam)
    {
        return $this->_genericCommand('UID FETCH', sprintf("%s %s",$msgset,$fetchparam) );
    }








    function cmdUidCopy($message_set, $mailbox)
    {
        $mailbox_name=sprintf("\"%s\"",$this->utf_7_encode($mailbox) );
        return $this->_genericCommand('UID COPY', sprintf("%s %s",$message_set,$mailbox_name) );
    }








     /**
     * Send the UID STORE command.
     *
     * @param string $message_set  the sessage_set
     * @param string $dataitem: the way we store the flags
     *          FLAGS: replace the flags whith $value
     *          FLAGS.SILENT: replace the flags whith $value but don't return untagged responses
     *
     *          +FLAGS: Add the flags whith $value
     *          +FLAGS.SILENT: Add the flags whith $value but don't return untagged responses
     *
     *          -FLAGS: Remove the flags whith $value
     *          -FLAGS.SILENT: Remove the flags whith $value but don't return untagged responses
     *
     * @param string $value
     * @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 cmdUidStore($message_set, $dataitem, $value)
    {
        /* As said in RFC2060...
        C: A003 STORE 2:4 +FLAGS (\Deleted)
        S: * 2 FETCH FLAGS (\Deleted \Seen)
        S: * 3 FETCH FLAGS (\Deleted)
        S: * 4 FETCH FLAGS (\Deleted \Flagged \Seen)
        S: A003 OK STORE completed
        */
        if( $dataitem!="FLAGS" && $dataitem!="FLAGS.SILENT" && $dataitem!="+FLAGS" &&
            $dataitem!="+FLAGS.SILENT" && $dataitem!="-FLAGS" && $dataitem!="-FLAGS.SILENT" ){
            $this->_prot_error("dataitem '$dataitem' is invalid! see RFC2060!!!!" , __LINE__ , __FILE__ );
        }
        return $this->_genericCommand('UID STORE', sprintf("%s %s (%s)",$message_set,$dataitem,$value) );
    }










    /**
     * Send the SEARCH 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 cmdUidSearch($search_cmd)
    {
        $ret=$this->_genericCommand('UID SEARCH', sprintf("%s",$search_cmd) );
        if(isset( $ret["PARSED"] ) ){
            $ret["PARSED"]=$ret["PARSED"][0]["EXT"];
        }
        return $ret;
    }











    /**
     * Send the X 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 cmdX($atom, $parameters)
    {
        return $this->_genericCommand("X$atom", $parameters );
    }








/********************************************************************
***
***             HERE ENDS the RFC2060 IMAPS FUNCTIONS
***             AND BEGIN THE EXTENSIONS FUNCTIONS
***
********************************************************************/







/********************************************************************
***             RFC2087 IMAP4 QUOTA extension BEGINS HERE
********************************************************************/


    /**
     * Send the GETQUOTA command.
     *
     * @param string $mailbox_name the mailbox name to query for quota data
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or quota data on success
     * @access public
     * @since  1.0
     */

    function cmdGetQuota($mailbox_name)
    {


        //Check if the IMAP server has QUOTA support
        if( ! $this->hasQuotaSupport() ){
            return new PEAR_Error("This IMAP server does not support QUOTA's! ");
        }
        $mailbox_name=sprintf("%s",$this->utf_7_encode($mailbox_name) );
        $ret = $this->_genericCommand('GETQUOTA', $mailbox_name );
        if(isset( $ret["PARSED"] ) ){
        // remove the array index because the quota response returns only 1 line of output
            $ret['PARSED']=$ret["PARSED"][0];
        }
        return $ret;
    }


    /**
     * Send the GETQUOTAROOT command.
     *
     * @param string $mailbox_name the mailbox name to query for quota data
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or quota data on success
     * @access public
     * @since  1.0
     */

    function cmdGetQuotaRoot($mailbox_name)
    {
        //Check if the IMAP server has QUOTA support
        if( ! $this->hasQuotaSupport() ){
            return new PEAR_Error("This IMAP server does not support QUOTA's! ");
        }
        $mailbox_name=sprintf("%s",$this->utf_7_encode($mailbox_name) );
        $ret = $this->_genericCommand('GETQUOTAROOT', $mailbox_name );

        if(isset( $ret["PARSED"] ) ){
        // remove the array index because the quota response returns only 1 line of output
            $ret['PARSED']=$ret["PARSED"][0];
        }
        return $ret;
    }




    /**
     * Send the SETQUOTA command.
     *
     * @param string $mailbox_name the mailbox name to query for quota data
     * @param string $storageQuota sets the max number of bytes this mailbox can handle
     * @param string $messagesQuota sets the max number of messages this mailbox can handle
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or quota data on success
     * @access public
     * @since  1.0
     */
// TODO:  implement the quota by number of emails!!
    function cmdSetQuota($mailbox_name, $storageQuota = null ,$messagesQuota = null )

⌨️ 快捷键说明

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