imap.php.svn-base

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

SVN-BASE
1,840
字号
<?php//// +----------------------------------------------------------------------+// | PHP Version 4                                                        |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 The PHP Group                                |// +----------------------------------------------------------------------+// | This source file is subject to version 2.02 of the PHP license,      |// | that is bundled with this package in the file LICENSE, and is        |// | available at through the world-wide-web at                           |// | http://www.php.net/license/2_02.txt.                                 |// | If you did not receive a copy of the PHP license and are unable to   |// | obtain it through the world-wide-web, please send a note to          |// | license@php.net so we can mail you a copy immediately.               |// +----------------------------------------------------------------------+// | Author: Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>       |// +----------------------------------------------------------------------+require_once 'Net/IMAPProtocol.php';/** * Provides an implementation of the IMAP protocol using PEAR's * Net_Socket:: class. * * @package Net_IMAP * @author  Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> */class Net_IMAP extends Net_IMAPProtocol {    /**     * Constructor     *     * Instantiates a new Net_SMTP object, overriding any defaults     * with parameters that are passed in.     *     * @param string The server to connect to.     * @param int The port to connect to.     * @param string The value to give when sending EHLO or HELO.     */    function Net_IMAP($host = 'localhost', $port = 143)    {        $this->Net_IMAPProtocol();        $ret = $this->connect( $host , $port );    }     /**     * Attempt to connect to the IMAP server located at $host $port     * @param string $host The IMAP server     * @param string $port The IMAP port     *     *          It is only useful in a very few circunstances     *          because the contructor already makes this job     * @return true on success or PEAR_Error     *     * @access public     * @since  1.0     */    function connect($host, $port)    {        $ret=$this->cmdConnect($host,$port);        if($ret === true ){            return $ret;        }        if(empty($ret)){            return new PEAR_Error("Unexpected response on connection");        }        if(PEAR::isError($ret) ){            return $ret;        }        if(isset(    $ret["RESPONSE"]["CODE"] ) ){            if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){                return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);            }        }        return $ret;    }    /**     * Attempt to authenticate to the IMAP server.     * @param string $user The userid to authenticate as.     * @param string $pass The password to authenticate with.     * @param string $useauthenticate true: authenticate using     *        the IMAP AUTHENTICATE command. false: authenticate using     *        the IMAP AUTHENTICATE command. 'string': authenticate using     *        the IMAP AUTHENTICATE command but using the authMethod in 'string'     * @param boolean $selectMailbox automaticaly select inbox on login (false does not)     *     * @return true on success or PEAR_Error     *     * @access public     * @since  1.0     */    function login($user, $pass, $useauthenticate = true, $selectMailbox=true)    {        if ( $useauthenticate ){            //$useauthenticate is a string if the user hardcodes an AUTHMethod            // (the user calls $imap->login("user","password","CRAM-MD5"); for example!            $method = is_string( $useauthenticate ) ? $useauthenticate : null;            //Try the selected Auth method            if ( PEAR::isError( $ret = $this->cmdAuthenticate( $user , $pass , $method  ) ) ) {                // Verify the methods that we have in common with the server                if(is_array($this->_serverAuthMethods)){                    $commonMethods=array_intersect ($this->supportedAuthMethods, $this->_serverAuthMethods );                }else{                    $this->_serverAuthMethods=null;                }                if($this->_serverAuthMethods == null  || count($commonMethods) == 0 || $this->supportedAuthMethods == null ){                    // The server does not have any auth method, so I try LOGIN                    if ( PEAR::isError( $ret = $this->cmdLogin( $user, $pass ) ) ) {                        return $ret;                    }                }else{                    return $ret;                }            }            if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){                return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);            }        }else{            //The user request "PLAIN"  auth, we use the login command            if ( PEAR::isError( $ret = $this->cmdLogin( $user, $pass ) ) ) {                return $ret;            }            if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){                return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);            }        }        if($selectMailbox){            //Select INBOX            if ( PEAR::isError( $ret=$this->cmdSelect( $this->getCurrentMailbox() ) ) ) {                return $ret;            }        }        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        return true;    }    /*    * Disconnect function. Sends the QUIT command    * and closes the socket.    *    * @return bool Success/Failure    */    function disconnect($expungeOnExit = false)    {        if($expungeOnExit){            $ret=$this->cmdExpunge();            if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){                $ret=$this->cmdLogout();                return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);            }        }        $ret=$this->cmdLogout();        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        return true;    }     /*    * Changes  the default/current mailbox th $mailbox    *    *    * @return bool Success/Pear_Error Failure    */    function selectMailbox($mailbox)    {        $ret=$this->cmdSelect($mailbox);        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        return true;    }     /*    * Checks  the mailbox $mailbox    *    *    * @return bool Success/Pear_Error Failure    */    function examineMailbox($mailbox)    {        $ret=$this->cmdExamine($mailbox);        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        //$ret_aux["EXISTS"]=$ret["PARSED"]["EXISTS"];        //$ret_aux["RECENT"]=$ret["PARSED"]["RECENT"];        return $ret;    }    /*    * Returns the raw headers of the specified message.    *    * @param  $msg_id Message number    * @return mixed   Either raw headers or false on error    */    function getRawHeaders($msg_id)    {        $ret=$this->cmdFetch($msg_id, "BODY[HEADER]");        if(strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        $ret=$ret["PARSED"][0]["EXT"]["BODY[HEADER]"]["CONTENT"];        return $ret;    }    /*    * 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  $msg_id Message number    * @return mixed   Either array of headers or false on error    */    function getParsedHeaders($msg_id)    {            $ret=$this->getRawHeaders($msg_id);            $raw_headers = rtrim($ret);            $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;    }    /*    * Returns an array containing the message ID, the size and the UID    * of each message selected.    * message selection can be a valid IMAP command, a number or an array of    * messages    *    * @param  $msg_id Message number    * @return mixed   Either array of message data or PearError on error    */    function getMessagesList($msg_id = null)    {        if( $msg_id != null){            if(is_array($msg_id)){                $message_set=$this->_getSearchListFromArray($msg_id);            }else{                $message_set=$msg_id;            }        }else{            $message_set="1:*";        }        $ret=$this->cmdFetch($message_set,"(RFC822.SIZE UID)");        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        foreach($ret["PARSED"] as $msg){            $ret_aux[]=array("msg_id"=>$msg["NRO"],"size" => $msg["EXT"]["RFC822.SIZE"],"uidl"=> $msg["EXT"]["UID"]);        }        return $ret_aux;    }    function getSummary($msg_id = null)    {       if( $msg_id != null){            if(is_array($msg_id)){                $message_set=$this->_getSearchListFromArray($msg_id);            }else{                $message_set=$msg_id;            }        }else{            $message_set="1:*";        }        $ret=$this->cmdFetch($message_set,"(RFC822.SIZE UID FLAGS ENVELOPE INTERNALDATE)");        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        if(isset( $ret["PARSED"] ) ){            for($i=0; $i<count($ret["PARSED"]) ; $i++){                $a=$ret["PARSED"][$i]['EXT']['ENVELOPE'];                $a['MSG_NUM']=$ret["PARSED"][$i]['NRO'];                $a['UID']=$ret["PARSED"][$i]['EXT']['UID'];                $a['FLAGS']=$ret["PARSED"][$i]['EXT']['FLAGS'];                $a['INTERNALDATE']=$ret["PARSED"][$i]['EXT']['INTERNALDATE'];                $a['SIZE']=$ret["PARSED"][$i]['EXT']['RFC822.SIZE'];                $env[]=$a;                $a=null;            }            return $env;        }        //return $ret;    }    /*    * Returns the body of the message with given message number.    *    * @param  $msg_id Message number    * @return mixed   Either message body or false on error    */    function getBody($msg_id)    {        $ret=$this->cmdFetch($msg_id,"BODY[TEXT]");        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        $ret=$ret["PARSED"][0]["EXT"]["BODY[TEXT]"]["CONTENT"];        //$ret=$resp["PARSED"][0]["EXT"]["RFC822"]["CONTENT"];        return $ret;    }    /*    * Returns the entire message with given message number.    *    * @param  $msg_id Message number    * @return mixed   Either entire message or false on error    */    function getMessages($msg_id = null, $indexIsMessageNumber=true)    {        //$resp=$this->cmdFetch($msg_id,"(BODY[TEXT] BODY[HEADER])");        if( $msg_id != null){            if(is_array($msg_id)){                $message_set=$this->_getSearchListFromArray($msg_id);            }else{                $message_set=$msg_id;            }        }else{            $message_set="1:*";        }        $ret=$this->cmdFetch($message_set,"RFC822");        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        if(isset($ret["PARSED"])){            foreach($ret["PARSED"] as $msg){                if(isset($msg["EXT"]["RFC822"]["CONTENT"])){                    if($indexIsMessageNumber){                        $ret_aux[$msg["NRO"]]=$msg["EXT"]["RFC822"]["CONTENT"];                    }else{                        $ret_aux[]=$msg["EXT"]["RFC822"]["CONTENT"];                    }        }            }            return $ret_aux;       }       return array();    }    /*    * Returns number of messages in this mailbox    *    * @param  string $mailbox  the mailbox    * @return mixed Either number of messages or Pear_Error on error    */    function getNumberOfMessages($mailbox = '')    {        if ( $mailbox == '' || $mailbox == null ){            $mailbox=$this->getCurrentMailbox();        }        $ret=$this->cmdStatus( $mailbox , "MESSAGES" );        if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){            return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);        }        if( isset($ret["PARSED"]["STATUS"]["ATTRIBUTES"]["MESSAGES"] ) ){

⌨️ 快捷键说明

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