⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class_rfc822.php

📁 系统介绍 Engineer系统是降低服务器负载
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?phpclass Mail_RFC822{    /**     * The address being parsed by the RFC822 object.     * @var string $address     */    var $address = '';    /**     * The default domain to use for unqualified addresses.     * @var string $default_domain     */    var $default_domain = 'localhost';    /**     * Should we return a nested array showing groups, or flatten everything?     * @var boolean $nestGroups     */    var $nestGroups = true;    /**     * Whether or not to validate atoms for non-ascii characters.     * @var boolean $validate     */    var $validate = true;    /**     * The array of raw addresses built up as we parse.     * @var array $addresses     */    var $addresses = array();    /**     * The final array of parsed address information that we build up.     * @var array $structure     */    var $structure = array();    /**     * The current error message, if any.     * @var string $error     */    var $error = null;    /**     * An internal counter/pointer.     * @var integer $index     */    var $index = null;    /**     * The number of groups that have been found in the address list.     * @var integer $num_groups     * @access public     */    var $num_groups = 0;    /**     * A variable so that we can tell whether or not we're inside a     * Mail_RFC822 object.     * @var boolean $mailRFC822     */    var $mailRFC822 = true;        /**    * A limit after which processing stops    * @var int $limit    */    var $limit = null;    /**     * Sets up the object. The address must either be set here or when     * calling parseAddressList(). One or the other.     *     * @access public     * @param string  $address         The address(es) to validate.     * @param string  $default_domain  Default domain/host etc. If not supplied, will be set to localhost.     * @param boolean $nest_groups     Whether to return the structure with groups nested for easier viewing.     * @param boolean $validate        Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.     *      * @return object Mail_RFC822 A new Mail_RFC822 object.     */    function Mail_RFC822($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)    {        if (isset($address))        $this->address        = $address;        if (isset($default_domain)) $this->default_domain = $default_domain;        if (isset($nest_groups))    $this->nestGroups     = $nest_groups;        if (isset($validate))       $this->validate       = $validate;        if (isset($limit))          $this->limit          = $limit;    }    /**     * Starts the whole process. The address must either be set here     * or when creating the object. One or the other.     *     * @access public     * @param string  $address         The address(es) to validate.     * @param string  $default_domain  Default domain/host etc.     * @param boolean $nest_groups     Whether to return the structure with groups nested for easier viewing.     * @param boolean $validate        Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.     *      * @return array A structured array of addresses.     */    function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)    {        if (!isset($this->mailRFC822)) {            $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);            return $obj->parseAddressList();        }        if (isset($address))        $this->address        = $address;        if (isset($default_domain)) $this->default_domain = $default_domain;        if (isset($nest_groups))    $this->nestGroups     = $nest_groups;        if (isset($validate))       $this->validate       = $validate;        if (isset($limit))          $this->limit          = $limit;        $this->structure  = array();        $this->addresses  = array();        $this->error      = null;        $this->index      = null;        while ($this->address = $this->_splitAddresses($this->address)) {            continue;        }                if ($this->address === false || isset($this->error)) {            return false;        }        // Reset timer since large amounts of addresses can take a long time to        // get here        @set_time_limit(30);        // Loop through all the addresses        for ($i = 0; $i < count($this->addresses); $i++){            if (($return = $this->_validateAddress($this->addresses[$i])) === false                || isset($this->error)) {                return false;            }                        if (!$this->nestGroups) {                $this->structure = array_merge($this->structure, $return);            } else {                $this->structure[] = $return;            }        }        return $this->structure;    }    /**     * Splits an address into seperate addresses.     *      * @access private     * @param string $address The addresses to split.     * @return boolean Success or failure.     */    function _splitAddresses($address)    {        if (!empty($this->limit) AND count($this->addresses) == $this->limit) {            return '';        }        if ($this->_isGroup($address) && !isset($this->error)) {            $split_char = ';';            $is_group   = true;        } elseif (!isset($this->error)) {            $split_char = ',';            $is_group   = false;        } elseif (isset($this->error)) {            return false;        }        // Split the string based on the above ten or so lines.        $parts  = explode($split_char, $address);        $string = $this->_splitCheck($parts, $split_char);        // If a group...        if ($is_group) {            // If $string does not contain a colon outside of            // brackets/quotes etc then something's fubar.            // First check there's a colon at all:            if (strpos($string, ':') === false) {                $this->error = 'Invalid address: ' . $string;                return false;            }            // Now check it's outside of brackets/quotes:            if (!$this->_splitCheck(explode(':', $string), ':'))                return false;            // We must have a group at this point, so increase the counter:            $this->num_groups++;        }        // $string now contains the first full address/group.        // Add to the addresses array.        $this->addresses[] = array(                                   'address' => trim($string),                                   'group'   => $is_group                                   );        // Remove the now stored address from the initial line, the +1        // is to account for the explode character.        $address = trim(substr($address, strlen($string) + 1));        // If the next char is a comma and this was a group, then        // there are more addresses, otherwise, if there are any more        // chars, then there is another address.        if ($is_group && substr($address, 0, 1) == ','){            $address = trim(substr($address, 1));            return $address;        } elseif (strlen($address) > 0) {            return $address;        } else {            return '';        }        // If you got here then something's off        return false;    }    /**     * Checks for a group at the start of the string.     *      * @access private     * @param string $address The address to check.     * @return boolean Whether or not there is a group at the start of the string.     */    function _isGroup($address)    {        // First comma not in quotes, angles or escaped:        $parts  = explode(',', $address);        $string = $this->_splitCheck($parts, ',');        // Now we have the first address, we can reliably check for a        // group by searching for a colon that's not escaped or in        // quotes or angle brackets.        if (count($parts = explode(':', $string)) > 1) {            $string2 = $this->_splitCheck($parts, ':');            return ($string2 !== $string);        } else {            return false;        }    }    /**     * A common function that will check an exploded string.     *      * @access private     * @param array $parts The exloded string.     * @param string $char  The char that was exploded on.     * @return mixed False if the string contains unclosed quotes/brackets, or the string on success.     */    function _splitCheck($parts, $char)    {        $string = $parts[0];        for ($i = 0; $i < count($parts); $i++) {            if ($this->_hasUnclosedQuotes($string)                || $this->_hasUnclosedBrackets($string, '<>')                || $this->_hasUnclosedBrackets($string, '[]')                || $this->_hasUnclosedBrackets($string, '()')                || substr($string, -1) == '\\') {                if (isset($parts[$i + 1])) {                    $string = $string . $char . $parts[$i + 1];                } else {                    $this->error = 'Invalid address spec. Unclosed bracket or quotes';                    return false;                }            } else {                $this->index = $i;                break;            }        }        return $string;    }    /**     * Checks if a string has an unclosed quotes or not.     *      * @access private     * @param string $string The string to check.     * @return boolean True if there are unclosed quotes inside the string, false otherwise.     */    function _hasUnclosedQuotes($string)    {        $string     = explode('"', $string);        $string_cnt = count($string);        for ($i = 0; $i < (count($string) - 1); $i++)            if (substr($string[$i], -1) == '\\')                $string_cnt--;        return ($string_cnt % 2 === 0);    }    /**     * Checks if a string has an unclosed brackets or not. IMPORTANT:     * This function handles both angle brackets and square brackets;     *      * @access private     * @param string $string The string to check.     * @param string $chars  The characters to check for.     * @return boolean True if there are unclosed brackets inside the string, false otherwise.     */    function _hasUnclosedBrackets($string, $chars)    {        $num_angle_start = substr_count($string, $chars[0]);        $num_angle_end   = substr_count($string, $chars[1]);        $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);        $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);        if ($num_angle_start < $num_angle_end) {            $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';            return false;        } else {            return ($num_angle_start > $num_angle_end);        }    }    /**     * Sub function that is used only by hasUnclosedBrackets().     *      * @access private     * @param string $string The string to check.     * @param integer &$num    The number of occurences.     * @param string $char   The character to count.     * @return integer The number of occurences of $char in $string, adjusted for backslashes.     */    function _hasUnclosedBracketsSub($string, &$num, $char)    {        $parts = explode($char, $string);        for ($i = 0; $i < count($parts); $i++){            if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))                $num--;            if (isset($parts[$i + 1]))                $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];        }                return $num;    }    /**     * Function to begin checking the address.     *     * @access private     * @param string $address The address to validate.     * @return mixed False on failure, or a structured array of address information on success.     */    function _validateAddress($address)    {        $is_group = false;        if ($address['group']) {            $is_group = true;            // Get the group part of the name            $parts     = explode(':', $address['address']);            $groupname = $this->_splitCheck($parts, ':');            $structure = array();            // And validate the group part of the name.            if (!$this->_validatePhrase($groupname)){                $this->error = 'Group name did not validate.';                return false;            } else {                // Don't include groups if we are not nesting                // them. This avoids returning invalid addresses.                if ($this->nestGroups) {                    $structure = new stdClass;                    $structure->groupname = $groupname;                }            }            $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));        }        // If a group then split on comma and put into an array.        // Otherwise, Just put the whole address in an array.        if ($is_group) {            while (strlen($address['address']) > 0) {                $parts       = explode(',', $address['address']);                $addresses[] = $this->_splitCheck($parts, ',');                $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));            }        } else {            $addresses[] = $address['address'];        }        // Check that $addresses is set, if address like this:        // Groupname:;        // Then errors were appearing.        if (!isset($addresses)){            $this->error = 'Empty group.';            return false;        }        for ($i = 0; $i < count($addresses); $i++) {            $addresses[$i] = trim($addresses[$i]);        }        // Validate each mailbox.        // Format could be one of: name         //                         geezer        // ... or any other format valid by RFC 822.        array_walk($addresses, array($this, 'validateMailbox'));        // Nested format        if ($this->nestGroups) {            if ($is_group) {                $structure->addresses = $addresses;            } else {                $structure = $addresses[0];            }

⌨️ 快捷键说明

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