Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/7281' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 10, 2015
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected function validateLocalPart()
// 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)) {
if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->idnToAscii($this->localPart))) {
$result = true;
} else {
// Try quoted string format (RFC 5321 Chapter 4.1.2)
Expand Down Expand Up @@ -373,7 +373,7 @@ protected function validateMXRecords()
{
$mxHosts = array();
$weight = array();
$result = getmxrr($this->hostname, $mxHosts, $weight);
$result = getmxrr($this->idnToAscii($this->hostname), $mxHosts, $weight);
if (!empty($mxHosts) && !empty($weight)) {
$this->mxRecord = array_combine($mxHosts, $weight);
} else {
Expand Down Expand Up @@ -488,10 +488,10 @@ public function isValid($value)
}

$length = true;
$this->setValue($value);
$this->setValue($this->idnToUtf8($value));

// Split email address up and disallow '..'
if (!$this->splitEmailParts($value)) {
if (!$this->splitEmailParts($this->getValue())) {
$this->error(self::INVALID_FORMAT);
return false;
}
Expand All @@ -517,4 +517,30 @@ public function isValid($value)

return false;
}

/**
* Safely convert UTF-8 encoded domain name to ASCII
* @param string $email the UTF-8 encoded email
* @return string
*/
protected function idnToAscii($email)
{
if (extension_loaded('intl')) {
return idn_to_ascii($email);
}
return $email;
}

/**
* Safely convert ASCII encoded domain name to UTF-8
* @param string $email the ASCII encoded email
* @return string
*/
protected function idnToUtf8($email)
{
if (extension_loaded('intl')) {
return idn_to_utf8($email);
}
return $email;
}
}
11 changes: 11 additions & 0 deletions test/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,11 @@ public function testUseMxCheckBasicValid()
'bob@thelongestdomainnameintheworldandthensomeandthensomemoreandmore.com'
);

if (extension_loaded('intl')) {
$emailAddresses[] = 'иван@письмо.рф';
$emailAddresses[] = 'xn--@-7sbfxdyelgv5j.xn--p1ai';
}

foreach ($emailAddresses as $input) {
$this->assertTrue($validator->isValid($input), "$input failed to pass validation:\n"
. implode("\n", $validator->getMessages()));
Expand Down Expand Up @@ -728,6 +733,12 @@ public function testUseMxRecordsBasicInvalid()
'bob @ domain.com',
'Abc..123@example.com'
);

if (!extension_loaded('intl')) {
$emailAddresses[] = 'иван@письмо.рф';
$emailAddresses[] = 'xn--@-7sbfxdyelgv5j.xn--p1ai';
}

foreach ($emailAddresses as $input) {
$this->assertFalse($validator->isValid($input), implode("\n", $this->validator->getMessages()) . $input);
}
Expand Down

0 comments on commit 43b8fe9

Please sign in to comment.