Commit 8dab87be by Carsten Brandt

fixed EmailValidator IDN and full pattern support

also made everything conistent with client validation
parent 64862f96
......@@ -117,16 +117,16 @@ yii.validation = (function ($) {
var valid = true;
if (options.enableIDN) {
var regexp = /^(.*)@(.*)$/,
var regexp = /^(.*<?)(.*)@(.*)(>?)$/,
matches = regexp.exec(value);
if (matches === null) {
valid = false;
} 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);
}
},
......
......@@ -93,15 +93,15 @@ class EmailValidator extends Validator
public function validateValue($value)
{
// 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;
}
if (($atPosition = strpos($value, '@')) === false) {
if (!preg_match('/^(.*<?)(.*)@(.*)(>?)$/', $value, $matches)) {
return false;
}
$domain = rtrim(substr($value, $atPosition + 1), '>');
$domain = $matches[3];
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);
if ($valid) {
......
......@@ -24,6 +24,55 @@ class EmailValidatorTest extends TestCase
$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>'));
$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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment