web/lib/Zend/Validate/EmailAddress.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Validate
    16  * @package    Zend_Validate
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: EmailAddress.php 25057 2012-11-02 20:35:40Z rob $
    19  * @version    $Id$
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Validate_Abstract
    23  * @see Zend_Validate_Abstract
    24  */
    24  */
    30 require_once 'Zend/Validate/Hostname.php';
    30 require_once 'Zend/Validate/Hostname.php';
    31 
    31 
    32 /**
    32 /**
    33  * @category   Zend
    33  * @category   Zend
    34  * @package    Zend_Validate
    34  * @package    Zend_Validate
    35  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    35  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  */
    37  */
    38 class Zend_Validate_EmailAddress extends Zend_Validate_Abstract
    38 class Zend_Validate_EmailAddress extends Zend_Validate_Abstract
    39 {
    39 {
    40     const INVALID            = 'emailAddressInvalid';
    40     const INVALID            = 'emailAddressInvalid';
   130      * 'hostname' => A hostname validator, see Zend_Validate_Hostname
   130      * 'hostname' => A hostname validator, see Zend_Validate_Hostname
   131      * 'allow'    => Options for the hostname validator, see Zend_Validate_Hostname::ALLOW_*
   131      * 'allow'    => Options for the hostname validator, see Zend_Validate_Hostname::ALLOW_*
   132      * 'mx'       => If MX check should be enabled, boolean
   132      * 'mx'       => If MX check should be enabled, boolean
   133      * 'deep'     => If a deep MX check should be done, boolean
   133      * 'deep'     => If a deep MX check should be done, boolean
   134      *
   134      *
   135      * @param array|Zend_Config $options OPTIONAL
   135      * @param array|string|Zend_Config $options OPTIONAL
   136      * @return void
       
   137      */
   136      */
   138     public function __construct($options = array())
   137     public function __construct($options = array())
   139     {
   138     {
   140         if ($options instanceof Zend_Config) {
   139         if ($options instanceof Zend_Config) {
   141             $options = $options->toArray();
   140             $options = $options->toArray();
   169 
   168 
   170     /**
   169     /**
   171      * Set options for the email validator
   170      * Set options for the email validator
   172      *
   171      *
   173      * @param array $options
   172      * @param array $options
   174      * @return Zend_Validate_EmailAddress fluid interface
   173      * @return Zend_Validate_EmailAddress Provides a fluent inteface
   175      */
   174      */
   176     public function setOptions(array $options = array())
   175     public function setOptions(array $options = array())
   177     {
   176     {
   178         if (array_key_exists('messages', $options)) {
   177         if (array_key_exists('messages', $options)) {
   179             $this->setMessages($options['messages']);
   178             $this->setMessages($options['messages']);
   240     }
   239     }
   241 
   240 
   242     /**
   241     /**
   243      * @param Zend_Validate_Hostname $hostnameValidator OPTIONAL
   242      * @param Zend_Validate_Hostname $hostnameValidator OPTIONAL
   244      * @param int                    $allow             OPTIONAL
   243      * @param int                    $allow             OPTIONAL
   245      * @return void
   244      * @return $this
   246      */
   245      */
   247     public function setHostnameValidator(Zend_Validate_Hostname $hostnameValidator = null, $allow = Zend_Validate_Hostname::ALLOW_DNS)
   246     public function setHostnameValidator(Zend_Validate_Hostname $hostnameValidator = null, $allow = Zend_Validate_Hostname::ALLOW_DNS)
   248     {
   247     {
   249         if (!$hostnameValidator) {
   248         if (!$hostnameValidator) {
   250             $hostnameValidator = new Zend_Validate_Hostname($allow);
   249             $hostnameValidator = new Zend_Validate_Hostname($allow);
   281      * Set whether we check for a valid MX record via DNS
   280      * Set whether we check for a valid MX record via DNS
   282      *
   281      *
   283      * This only applies when DNS hostnames are validated
   282      * This only applies when DNS hostnames are validated
   284      *
   283      *
   285      * @param boolean $mx Set allowed to true to validate for MX records, and false to not validate them
   284      * @param boolean $mx Set allowed to true to validate for MX records, and false to not validate them
   286      * @return Zend_Validate_EmailAddress Fluid Interface
   285      * @throws Zend_Validate_Exception
       
   286      * @return Zend_Validate_EmailAddress Provides a fluent inteface
   287      */
   287      */
   288     public function setValidateMx($mx)
   288     public function setValidateMx($mx)
   289     {
   289     {
   290         if ((bool) $mx && !$this->validateMxSupported()) {
   290         if ((bool) $mx && !$this->validateMxSupported()) {
   291             require_once 'Zend/Validate/Exception.php';
   291             require_once 'Zend/Validate/Exception.php';
   308 
   308 
   309     /**
   309     /**
   310      * Set whether we check MX record should be a deep validation
   310      * Set whether we check MX record should be a deep validation
   311      *
   311      *
   312      * @param boolean $deep Set deep to true to perform a deep validation process for MX records
   312      * @param boolean $deep Set deep to true to perform a deep validation process for MX records
   313      * @return Zend_Validate_EmailAddress Fluid Interface
   313      * @return Zend_Validate_EmailAddress Provides a fluent inteface
   314      */
   314      */
   315     public function setDeepMxCheck($deep)
   315     public function setDeepMxCheck($deep)
   316     {
   316     {
   317         $this->_options['deep'] = (bool) $deep;
   317         $this->_options['deep'] = (bool) $deep;
   318         return $this;
   318         return $this;
   331     /**
   331     /**
   332      * Sets if the domain should also be checked
   332      * Sets if the domain should also be checked
   333      * or only the local part of the email address
   333      * or only the local part of the email address
   334      *
   334      *
   335      * @param boolean $domain
   335      * @param boolean $domain
   336      * @return Zend_Validate_EmailAddress Fluid Interface
   336      * @return Zend_Validate_EmailAddress Provides a fluent inteface
   337      */
   337      */
   338     public function setDomainCheck($domain = true)
   338     public function setDomainCheck($domain = true)
   339     {
   339     {
   340         $this->_options['domain'] = (boolean) $domain;
   340         $this->_options['domain'] = (boolean) $domain;
   341         return $this;
   341         return $this;
   422         //        "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
   422         //        "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
   423         $atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
   423         $atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
   424         if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->_localPart)) {
   424         if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->_localPart)) {
   425             $result = true;
   425             $result = true;
   426         } else {
   426         } else {
   427             // Try quoted string format
   427             // Try quoted string format (RFC 5321 Chapter 4.1.2)
   428 
   428 
   429             // Quoted-string characters are: DQUOTE *([FWS] qtext/quoted-pair) [FWS] DQUOTE
   429             // Quoted-string characters are: DQUOTE *(qtext/quoted-pair) DQUOTE
   430             // qtext: Non white space controls, and the rest of the US-ASCII characters not
   430             $qtext      = '\x20-\x21\x23-\x5b\x5d-\x7e'; // %d32-33 / %d35-91 / %d93-126
   431             //   including "\" or the quote character
   431             $quotedPair = '\x20-\x7e'; // %d92 %d32-126
   432             $noWsCtl = '\x01-\x08\x0b\x0c\x0e-\x1f\x7f';
   432             if (preg_match('/^"(['. $qtext .']|\x5c[' . $quotedPair . '])*"$/', $this->localPart)) {
   433             $qtext   = $noWsCtl . '\x21\x23-\x5b\x5d-\x7e';
       
   434             $ws      = '\x20\x09';
       
   435             if (preg_match('/^\x22([' . $ws . $qtext . '])*[$ws]?\x22$/', $this->_localPart)) {
       
   436                 $result = true;
   433                 $result = true;
   437             } else {
   434             } else {
   438                 $this->_error(self::DOT_ATOM);
   435                 $this->_error(self::DOT_ATOM);
   439                 $this->_error(self::QUOTED_STRING);
   436                 $this->_error(self::QUOTED_STRING);
   440                 $this->_error(self::INVALID_LOCAL_PART);
   437                 $this->_error(self::INVALID_LOCAL_PART);
   450      * @return boolean
   447      * @return boolean
   451      */
   448      */
   452     private function _validateMXRecords()
   449     private function _validateMXRecords()
   453     {
   450     {
   454         $mxHosts = array();
   451         $mxHosts = array();
   455         $result = getmxrr($this->_hostname, $mxHosts);
   452         $hostname = $this->_hostname;
       
   453 
       
   454         //decode IDN domain name if possible
       
   455         if (function_exists('idn_to_ascii')) {
       
   456             $hostname = idn_to_ascii($this->_hostname);
       
   457         }
       
   458 
       
   459         $result = getmxrr($hostname, $mxHosts);
   456         if (!$result) {
   460         if (!$result) {
   457             $this->_error(self::INVALID_MX_RECORD);
   461             $this->_error(self::INVALID_MX_RECORD);
   458         } else if ($this->_options['deep'] && function_exists('checkdnsrr')) {
   462         } else if ($this->_options['deep'] && function_exists('checkdnsrr')) {
   459             $validAddress = false;
   463             $validAddress = false;
   460             $reserved     = true;
   464             $reserved     = true;