Browse Source

fixed EmailValidator IDN and full pattern support

also made everything conistent with client validation
tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
8dab87be3d
  1. 6
      framework/yii/assets/yii.validation.js
  2. 8
      framework/yii/validators/EmailValidator.php
  3. 49
      tests/unit/framework/validators/EmailValidatorTest.php

6
framework/yii/assets/yii.validation.js

@ -117,16 +117,16 @@ yii.validation = (function ($) {
var valid = true; var valid = true;
if (options.enableIDN) { if (options.enableIDN) {
var regexp = /^(.*)@(.*)$/, var regexp = /^(.*<?)(.*)@(.*)(>?)$/,
matches = regexp.exec(value); matches = regexp.exec(value);
if (matches === null) { if (matches === null) {
valid = false; valid = false;
} else { } else {
value = punycode.toASCII(matches[1]) + '@' + punycode.toASCII(matches[2]); value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4];
} }
} }
if (!valid || !(value.match(options.pattern) && (!options.allowName || value.match(options.fullPattern)))) { if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) {
addMessage(messages, options.message, value); addMessage(messages, options.message, value);
} }
}, },

8
framework/yii/validators/EmailValidator.php

@ -93,15 +93,15 @@ class EmailValidator extends Validator
public function validateValue($value) public function validateValue($value)
{ {
// make sure string length is limited to avoid DOS attacks // make sure string length is limited to avoid DOS attacks
if (!is_string($value) || strlen($value) >= 255) { if (!is_string($value) || strlen($value) >= 320) {
return false; return false;
} }
if (($atPosition = strpos($value, '@')) === false) { if (!preg_match('/^(.*<?)(.*)@(.*)(>?)$/', $value, $matches)) {
return false; return false;
} }
$domain = rtrim(substr($value, $atPosition + 1), '>'); $domain = $matches[3];
if ($this->enableIDN) { if ($this->enableIDN) {
$value = idn_to_ascii(ltrim(substr($value, 0, $atPosition), '<')) . '@' . idn_to_ascii($domain); $value = $matches[1] . idn_to_ascii($matches[2]) . '@' . idn_to_ascii($domain) . $matches[4];
} }
$valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value); $valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value);
if ($valid) { if ($valid) {

49
tests/unit/framework/validators/EmailValidatorTest.php

@ -24,6 +24,55 @@ class EmailValidatorTest extends TestCase
$this->assertTrue($validator->validateValue('sam@rmcreative.ru')); $this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
$this->assertTrue($validator->validateValue('5011@gmail.com')); $this->assertTrue($validator->validateValue('5011@gmail.com'));
$this->assertFalse($validator->validateValue('rmcreative.ru')); $this->assertFalse($validator->validateValue('rmcreative.ru'));
$this->assertFalse($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
$this->assertFalse($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
$this->assertFalse($validator->validateValue('<mail@cebe.cc>'));
$this->assertFalse($validator->validateValue('info@örtliches.de'));
$this->assertFalse($validator->validateValue('sam@рмкреатиф.ru'));
$validator->allowName = true;
$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
$this->assertTrue($validator->validateValue('5011@gmail.com'));
$this->assertFalse($validator->validateValue('rmcreative.ru'));
$this->assertTrue($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
$this->assertTrue($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
$this->assertTrue($validator->validateValue('<mail@cebe.cc>'));
$this->assertFalse($validator->validateValue('info@örtliches.de'));
$this->assertFalse($validator->validateValue('sam@рмкреатиф.ru'));
$this->assertFalse($validator->validateValue('Informtation info@oertliches.de'));
}
public function testValidateIdnValue()
{
if (!extension_loaded("intl")) {
$this->markTestSkipped("intl not installed. Skipping.");
}
$validator = new EmailValidator();
$validator->enableIDN = true;
$this->assertTrue($validator->validateValue('info@örtliches.de'));
$this->assertTrue($validator->validateValue('sam@рмкреатиф.ru'));
$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
$this->assertTrue($validator->validateValue('5011@gmail.com'));
$this->assertFalse($validator->validateValue('rmcreative.ru'));
$this->assertFalse($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
$this->assertFalse($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
$this->assertFalse($validator->validateValue('<mail@cebe.cc>'));
$validator->allowName = true;
$this->assertTrue($validator->validateValue('info@örtliches.de'));
$this->assertTrue($validator->validateValue('Informtation <info@örtliches.de>'));
$this->assertFalse($validator->validateValue('Informtation info@örtliches.de'));
$this->assertTrue($validator->validateValue('sam@рмкреатиф.ru'));
$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
$this->assertTrue($validator->validateValue('5011@gmail.com'));
$this->assertFalse($validator->validateValue('rmcreative.ru'));
$this->assertTrue($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
$this->assertTrue($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
$this->assertTrue($validator->validateValue('<mail@cebe.cc>'));
} }
public function testValidateValueMx() public function testValidateValueMx()

Loading…
Cancel
Save