コード例 #1
0
ファイル: HostnameTest.php プロジェクト: heiglandreas/zf2
 /**
  * Test changed with ZF-6676, as IP check is only involved when IP patterns match
  *
  * @see ZF-2861
  * @see ZF-6676
  */
 public function testValidatorMessagesShouldBeTranslated()
 {
     $translations = array('hostnameInvalidLocalName' => 'this is the IP error message');
     $translator = new \Zend\Translator\Translator('ArrayAdapter', $translations);
     $this->_validator->setTranslator($translator);
     $this->_validator->isValid('0.239,512.777');
     $messages = $this->_validator->getMessages();
     $found = false;
     foreach ($messages as $code => $message) {
         if (array_key_exists($code, $translations)) {
             $found = true;
             break;
         }
     }
     $this->assertTrue($found);
     $this->assertEquals($translations[$code], $message);
 }
コード例 #2
0
ファイル: HostnameTest.php プロジェクト: lortnus/zf1
 /**
  * @see ZF-2861
  */
 public function testIpValidatorMessagesShouldBeTranslated()
 {
     require_once 'Zend/Validate/Ip.php';
     $ipValidator = new Zend_Validate_Ip();
     require_once 'Zend/Translate.php';
     $translations = array('notIpAddress' => 'this is the IP error message');
     $translator = new Zend_Translate('array', $translations);
     $this->_validator->setTranslator($translator)->setIpValidator($ipValidator);
     $this->_validator->isValid('0.239,512.777');
     $messages = $ipValidator->getMessages();
     $found = false;
     foreach ($messages as $code => $message) {
         if (array_key_exists($code, $translations)) {
             $found = true;
             break;
         }
     }
     $this->assertTrue($found);
     $this->assertEquals($translations[$code], $message);
 }
コード例 #3
0
ファイル: EmailAddress.php プロジェクト: Orchild/mt
 /**
  * Defined by Zend_Validate_Interface
  *
  * Returns true if and only if $value is a valid email address
  * according to RFC2822
  *
  * @link   http://www.ietf.org/rfc/rfc2822.txt RFC2822
  * @link   http://www.columbia.edu/kermit/ascii.html US-ASCII characters
  * @param  string $value
  * @return boolean
  */
 public function isValid($value)
 {
     $valueString = (string) $value;
     $this->_setValue($valueString);
     // Split email address up
     if (!preg_match('/^(.+)@([^@]+)$/', $valueString, $matches)) {
         $this->_error(self::INVALID);
         return false;
     }
     $this->_localPart = $matches[1];
     $this->_hostname = $matches[2];
     // Match hostname part
     $hostnameResult = $this->hostnameValidator->setTranslator($this->getTranslator())->isValid($this->_hostname);
     if (!$hostnameResult) {
         $this->_error(self::INVALID_HOSTNAME);
         // Get messages and errors from hostnameValidator
         foreach ($this->hostnameValidator->getMessages() as $message) {
             $this->_messages[] = $message;
         }
         foreach ($this->hostnameValidator->getErrors() as $error) {
             $this->_errors[] = $error;
         }
     }
     // MX check on hostname via dns_get_record()
     if ($this->_validateMx) {
         if ($this->validateMxSupported()) {
             $result = dns_get_mx($this->_hostname, $mxHosts);
             if (count($mxHosts) < 1) {
                 $hostnameResult = false;
                 $this->_error(self::INVALID_MX_RECORD);
             }
         } else {
             /**
              * MX checks are not supported by this system
              * @see Zend_Validate_Exception
              */
             require_once 'Zend/Validate/Exception.php';
             throw new Zend_Validate_Exception('Internal error: MX checking not available on this system');
         }
     }
     // First try to match the local part on the common dot-atom format
     $localResult = false;
     // Dot-atom characters are: 1*atext *("." 1*atext)
     // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
     //        "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
     $atext = 'a-zA-Z0-9\\x21\\x23\\x24\\x25\\x26\\x27\\x2a\\x2b\\x2d\\x2f\\x3d\\x3f\\x5e\\x5f\\x60\\x7b\\x7c\\x7d';
     if (preg_match('/^[' . $atext . ']+(\\x2e+[' . $atext . ']+)*$/', $this->_localPart)) {
         $localResult = true;
     } else {
         // Try quoted string format
         // Quoted-string characters are: DQUOTE *([FWS] qtext/quoted-pair) [FWS] DQUOTE
         // qtext: Non white space controls, and the rest of the US-ASCII characters not
         //   including "\" or the quote character
         $noWsCtl = '\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f';
         $qtext = $noWsCtl . '\\x21\\x23-\\x5b\\x5d-\\x7e';
         $ws = '\\x20\\x09';
         if (preg_match('/^\\x22([' . $ws . $qtext . '])*[$ws]?\\x22$/', $this->_localPart)) {
             $localResult = true;
         } else {
             $this->_error(self::DOT_ATOM);
             $this->_error(self::QUOTED_STRING);
             $this->_error(self::INVALID_LOCAL_PART);
         }
     }
     // If both parts valid, return true
     if ($localResult && $hostnameResult) {
         return true;
     } else {
         return false;
     }
 }
コード例 #4
0
ファイル: EmailAddress.php プロジェクト: Tony133/zf-web
 /**
  * Defined by Zend_Validate_Interface
  *
  * Returns true if and only if $value is a valid email address
  * according to RFC2822
  *
  * @link   http://www.ietf.org/rfc/rfc2822.txt RFC2822
  * @link   http://www.columbia.edu/kermit/ascii.html US-ASCII characters
  * @param  string $value
  * @return boolean
  */
 public function isValid($value)
 {
     $valueString = (string) $value;
     $matches = array();
     $length = true;
     $this->_setValue($valueString);
     // Split email address up and disallow '..'
     if (strpos($valueString, '..') !== false or !preg_match('/^(.+)@([^@]+)$/', $valueString, $matches)) {
         $this->_error(self::INVALID);
         return false;
     }
     $this->_localPart = $matches[1];
     $this->_hostname = $matches[2];
     if (strlen($this->_localPart) > 64 || strlen($this->_hostname) > 255) {
         $length = false;
         $this->_error(self::LENGTH_EXCEEDED);
     }
     // Match hostname part
     $hostnameResult = $this->hostnameValidator->setTranslator($this->getTranslator())->isValid($this->_hostname);
     if (!$hostnameResult) {
         $this->_error(self::INVALID_HOSTNAME);
         // Get messages and errors from hostnameValidator
         foreach ($this->hostnameValidator->getMessages() as $code => $message) {
             $this->_messages[$code] = $message;
         }
         foreach ($this->hostnameValidator->getErrors() as $error) {
             $this->_errors[] = $error;
         }
     } else {
         if ($this->_validateMx) {
             // MX check on hostname via dns_get_record()
             if ($this->validateMxSupported()) {
                 $result = dns_get_mx($this->_hostname, $mxHosts);
                 if (count($mxHosts) < 1) {
                     // the address may can accept email even if there's no MX record but A or A6 or AAAA record found
                     if ($this->_deepMxCheck && $this->validateDnsSupported()) {
                         if ($this->isReservedIpAddress($this->_hostname)) {
                             $hostnameResult = false;
                             $this->_error(self::INVALID_NETWORK_SEGMENT);
                         } else {
                             if (checkdnsrr($this->_hostname, "A") === false && checkdnsrr($this->_hostname, "AAAA") === false && checkdnsrr($this->_hostname, "A6") === false) {
                                 $hostnameResult = false;
                                 $this->_error(self::INVALID_MX_RECORD);
                             }
                         }
                     } else {
                         $hostnameResult = false;
                         $this->_error(self::INVALID_MX_RECORD);
                     }
                 } else {
                     // must check every MX record for at least one valid address
                     if ($this->_deepMxCheck && $this->validateDnsSupported()) {
                         $hasOneValidAddress = false;
                         foreach ($mxHosts as $key => $hostname) {
                             if (!$this->isReservedIpAddress($hostname) && (checkdnsrr($hostname, "A") !== false || checkdnsrr($hostname, "AAAA") !== false || checkdnsrr($hostname, "A6") !== false)) {
                                 $hasOneValidAddress = true;
                                 break;
                             }
                         }
                         if ($hasOneValidAssress === false) {
                             $hostnameResult = false;
                             $this->_error(self::INVALID_MX_RECORD);
                         }
                     }
                 }
             } else {
                 /**
                  * MX checks are not supported by this system
                  * @see Zend_Validate_Exception
                  */
                 require_once 'Zend/Validate/Exception.php';
                 throw new Zend_Validate_Exception('Internal error: MX checking not available on this system');
             }
         }
     }
     // First try to match the local part on the common dot-atom format
     $localResult = false;
     // Dot-atom characters are: 1*atext *("." 1*atext)
     // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
     //        "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
     $atext = 'a-zA-Z0-9\\x21\\x23\\x24\\x25\\x26\\x27\\x2a\\x2b\\x2d\\x2f\\x3d\\x3f\\x5e\\x5f\\x60\\x7b\\x7c\\x7d\\x7e';
     if (preg_match('/^[' . $atext . ']+(\\x2e+[' . $atext . ']+)*$/', $this->_localPart)) {
         $localResult = true;
     } else {
         // Try quoted string format
         // Quoted-string characters are: DQUOTE *([FWS] qtext/quoted-pair) [FWS] DQUOTE
         // qtext: Non white space controls, and the rest of the US-ASCII characters not
         //   including "\" or the quote character
         $noWsCtl = '\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f';
         $qtext = $noWsCtl . '\\x21\\x23-\\x5b\\x5d-\\x7e';
         $ws = '\\x20\\x09';
         if (preg_match('/^\\x22([' . $ws . $qtext . '])*[$ws]?\\x22$/', $this->_localPart)) {
             $localResult = true;
         } else {
             $this->_error(self::DOT_ATOM);
             $this->_error(self::QUOTED_STRING);
             $this->_error(self::INVALID_LOCAL_PART);
         }
     }
     // If both parts valid, return true
     if ($localResult && $hostnameResult && $length) {
         return true;
     } else {
         return false;
     }
 }