mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-02 05:27:52 +00:00
Compare commits
56 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5631c3fe2e | |||
| c383717824 | |||
| aabed98198 | |||
| 3b0fd01aa3 | |||
| 4efe697504 | |||
| 7a4c5714f5 | |||
| 0a26375a7a | |||
| 773fe5f4a3 | |||
| 4a209951ee | |||
| a8b0bab5aa | |||
| cc0b91f311 | |||
| 8092ecaeff | |||
| f19defb373 | |||
| 9a518fb1bd | |||
| 9b184605e8 | |||
| 7a4842c60f | |||
| 687ab6a66e | |||
| 116d4054fa | |||
| af9417f765 | |||
| ab0bcfb424 | |||
| 833b3e7924 | |||
| fec1609458 | |||
| add559b99c | |||
| 19e390d863 | |||
| 5320c26b38 | |||
| 3b6ab3bcd5 | |||
| 7a64ea18af | |||
| 9103f4f99b | |||
| 3f623e9006 | |||
| 833eb65135 | |||
| e61481fefb | |||
| 518f80a0ff | |||
| 2ec7babda6 | |||
| 05367768d6 | |||
| 5c3a79217c | |||
| 749b423775 | |||
| 49494c3189 | |||
| 8b62522238 | |||
| 82fb2d6ce1 | |||
| 2933b7bbd1 | |||
| f5c48f62d3 | |||
| 43650e8566 | |||
| 3d94cc6487 | |||
| 65c8c3d7bb | |||
| 57f8ae443b | |||
| 18df863858 | |||
| 953cbdbbcf | |||
| 41cb75ec00 | |||
| 8927160d8d | |||
| 887605ad1b | |||
| 43e1825cd8 | |||
| 2f44d7b1e1 | |||
| 355cb7e604 | |||
| d6fcd2d7ca | |||
| 9be8eb420a | |||
| 20066b1c1e |
+19
-3
@@ -1,3 +1,5 @@
|
|||||||
|
sudo: false
|
||||||
|
|
||||||
language: php
|
language: php
|
||||||
|
|
||||||
php:
|
php:
|
||||||
@@ -5,11 +7,25 @@ php:
|
|||||||
- 5.4
|
- 5.4
|
||||||
- 5.5
|
- 5.5
|
||||||
- 5.6
|
- 5.6
|
||||||
|
- 7.0
|
||||||
- hhvm
|
- hhvm
|
||||||
|
|
||||||
before_script:
|
env:
|
||||||
- wget http://getcomposer.org/composer.phar
|
global:
|
||||||
- php composer.phar install --dev --no-interaction
|
- deps=no
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
fast_finish: true
|
||||||
|
include:
|
||||||
|
- php: 5.3
|
||||||
|
env: deps=low
|
||||||
|
- php: 5.6
|
||||||
|
env: deps=high
|
||||||
|
|
||||||
|
install:
|
||||||
|
- if [ "$deps" = "no" ]; then composer install; fi
|
||||||
|
- if [ "$deps" = "low" ]; then composer update --prefer-lowest; fi
|
||||||
|
- if [ "$deps" = "high" ]; then composer update; fi
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- mkdir -p build/logs
|
- mkdir -p build/logs
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
EmailValidator [](https://travis-ci.org/egulias/EmailValidator) [](https://coveralls.io/r/egulias/EmailValidator?branch=master) [](https://insight.sensiolabs.com/projects/b18d473e-bd5a-4d88-a7b2-7aeaee0ebd7b)[](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=master)
|
#EmailValidator
|
||||||
|
[](https://travis-ci.org/egulias/EmailValidator) [](https://coveralls.io/r/egulias/EmailValidator?branch=master) [](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=master) [](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6)
|
||||||
=============================
|
=============================
|
||||||
|
With the help of
|
||||||
|
|
||||||
|

|
||||||
##Installation##
|
##Installation##
|
||||||
Install via composer. Add to your current compooser.json ```require``` key: ```"egulias/email-validator":"1.0.x-dev" ```
|
|
||||||
|
Run the command below to install via Composer
|
||||||
|
|
||||||
|
```shell
|
||||||
|
composer require egulias/email-validator
|
||||||
|
```
|
||||||
|
|
||||||
##Usage##
|
##Usage##
|
||||||
|
|
||||||
@@ -24,7 +32,7 @@ More advanced example (returns detailed diagnostic error codes):
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use egulias\EmailValidator\EmailValidator;
|
use Egulias\EmailValidator\EmailValidator;
|
||||||
|
|
||||||
$validator = new EmailValidator;
|
$validator = new EmailValidator;
|
||||||
$email = 'dominic@sayers.cc';
|
$email = 'dominic@sayers.cc';
|
||||||
@@ -46,6 +54,6 @@ As this is a port from another library and work, here are other people related t
|
|||||||
* Josepf Bielawski [@stloyd](http://github.com/stloyd): For its first re-work of Dominic's lib
|
* Josepf Bielawski [@stloyd](http://github.com/stloyd): For its first re-work of Dominic's lib
|
||||||
* Dominic Sayers [@dominicsayers](http://github.com/dominicsayers): The original isemail function
|
* Dominic Sayers [@dominicsayers](http://github.com/dominicsayers): The original isemail function
|
||||||
|
|
||||||
##Licence##
|
##License##
|
||||||
Released under the MIT Licence attached with this code.
|
Released under the MIT License attached with this code.
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -10,15 +10,16 @@
|
|||||||
],
|
],
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "1.0.x-dev"
|
"dev-master": "1.3.x-dev"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">= 5.3.3",
|
"php": ">= 5.3.3",
|
||||||
"doctrine/lexer": "~1.0"
|
"doctrine/lexer": "~1.0,>=1.0.1"
|
||||||
},
|
},
|
||||||
"require-dev" : {
|
"require-dev" : {
|
||||||
"satooshi/php-coveralls": "dev-master"
|
"satooshi/php-coveralls": "dev-master",
|
||||||
|
"phpunit/phpunit": "~4.4"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {
|
"psr-0": {
|
||||||
|
|||||||
Generated
+902
-110
File diff suppressed because it is too large
Load Diff
@@ -31,10 +31,13 @@ class EmailLexer extends AbstractLexer
|
|||||||
const S_SEMICOLON = 275;
|
const S_SEMICOLON = 275;
|
||||||
const S_OPENQBRACKET = 276;
|
const S_OPENQBRACKET = 276;
|
||||||
const S_CLOSEQBRACKET = 277;
|
const S_CLOSEQBRACKET = 277;
|
||||||
|
const S_SLASH = 278;
|
||||||
const S_EMPTY = null;
|
const S_EMPTY = null;
|
||||||
const GENERIC = 300;
|
const GENERIC = 300;
|
||||||
const CRLF = 301;
|
const CRLF = 301;
|
||||||
const INVALID = 302;
|
const INVALID = 302;
|
||||||
|
const ASCII_INVALID_FROM = 127;
|
||||||
|
const ASCII_INVALID_TO = 199;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
|
* US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
|
||||||
@@ -52,6 +55,7 @@ class EmailLexer extends AbstractLexer
|
|||||||
';' => self::S_SEMICOLON,
|
';' => self::S_SEMICOLON,
|
||||||
'@' => self::S_AT,
|
'@' => self::S_AT,
|
||||||
'\\' => self::S_BACKSLASH,
|
'\\' => self::S_BACKSLASH,
|
||||||
|
'/' => self::S_SLASH,
|
||||||
',' => self::S_COMMA,
|
',' => self::S_COMMA,
|
||||||
'.' => self::S_DOT,
|
'.' => self::S_DOT,
|
||||||
'"' => self::S_DQUOTE,
|
'"' => self::S_DQUOTE,
|
||||||
@@ -67,14 +71,29 @@ class EmailLexer extends AbstractLexer
|
|||||||
'>' => self::S_GREATERTHAN,
|
'>' => self::S_GREATERTHAN,
|
||||||
'{' => self::S_OPENQBRACKET,
|
'{' => self::S_OPENQBRACKET,
|
||||||
'}' => self::S_CLOSEQBRACKET,
|
'}' => self::S_CLOSEQBRACKET,
|
||||||
'' => self::S_EMPTY
|
'' => self::S_EMPTY,
|
||||||
|
'\0' => self::C_NUL,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
protected $hasInvalidTokens = false;
|
||||||
|
|
||||||
protected $previous;
|
protected $previous;
|
||||||
|
|
||||||
|
public function reset()
|
||||||
|
{
|
||||||
|
$this->hasInvalidTokens = false;
|
||||||
|
parent::reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasInvalidTokens()
|
||||||
|
{
|
||||||
|
return $this->hasInvalidTokens;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $type
|
* @param $type
|
||||||
* @throws \UnexpectedValueException
|
* @throws \UnexpectedValueException
|
||||||
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function find($type)
|
public function find($type)
|
||||||
{
|
{
|
||||||
@@ -100,7 +119,7 @@ class EmailLexer extends AbstractLexer
|
|||||||
/**
|
/**
|
||||||
* moveNext
|
* moveNext
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function moveNext()
|
public function moveNext()
|
||||||
{
|
{
|
||||||
@@ -112,17 +131,17 @@ class EmailLexer extends AbstractLexer
|
|||||||
/**
|
/**
|
||||||
* Lexical catchable patterns.
|
* Lexical catchable patterns.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
protected function getCatchablePatterns()
|
protected function getCatchablePatterns()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'[a-zA-Z_]+[46]?',
|
'[a-zA-Z_]+[46]?', //ASCII and domain literal
|
||||||
|
'[^\x00-\x7F]', //UTF-8
|
||||||
'[0-9]+',
|
'[0-9]+',
|
||||||
'\r\n',
|
'\r\n',
|
||||||
'::',
|
'::',
|
||||||
'\s+',
|
'\s+?',
|
||||||
'[\x10-\x1F]+',
|
|
||||||
'.',
|
'.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -130,11 +149,11 @@ class EmailLexer extends AbstractLexer
|
|||||||
/**
|
/**
|
||||||
* Lexical non-catchable patterns.
|
* Lexical non-catchable patterns.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
protected function getNonCatchablePatterns()
|
protected function getNonCatchablePatterns()
|
||||||
{
|
{
|
||||||
return array('[\x7f-\xff]+');
|
return array('[\xA0-\xff]+');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,14 +165,59 @@ class EmailLexer extends AbstractLexer
|
|||||||
*/
|
*/
|
||||||
protected function getType(&$value)
|
protected function getType(&$value)
|
||||||
{
|
{
|
||||||
if (isset($this->charValue[$value])) {
|
if ($this->isNullType($value)) {
|
||||||
|
return self::C_NUL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->isValid($value)) {
|
||||||
return $this->charValue[$value];
|
return $this->charValue[$value];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match('/[\x10-\x1F]+/', $value)) {
|
if ($this->isUTF8Invalid($value)) {
|
||||||
|
$this->hasInvalidTokens = true;
|
||||||
return self::INVALID;
|
return self::INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::GENERIC;
|
return self::GENERIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function isValid($value)
|
||||||
|
{
|
||||||
|
if (isset($this->charValue[$value])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isNullType($value)
|
||||||
|
{
|
||||||
|
if ($value === "\0") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isUTF8Invalid($value)
|
||||||
|
{
|
||||||
|
if (preg_match('/\p{Cc}+/u', $value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getModifiers()
|
||||||
|
{
|
||||||
|
return 'iu';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ class EmailParser
|
|||||||
$this->domainPartParser = new DomainPart($this->lexer);
|
$this->domainPartParser = new DomainPart($this->lexer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $str
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function parse($str)
|
public function parse($str)
|
||||||
{
|
{
|
||||||
$this->lexer->setInput($str);
|
$this->lexer->setInput($str);
|
||||||
@@ -36,11 +40,16 @@ class EmailParser
|
|||||||
throw new \InvalidArgumentException('ERR_NOLOCALPART');
|
throw new \InvalidArgumentException('ERR_NOLOCALPART');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$this->localPartParser->parse($str);
|
$this->localPartParser->parse($str);
|
||||||
$this->domainPartParser->parse($str);
|
$this->domainPartParser->parse($str);
|
||||||
|
|
||||||
$this->setParts($str);
|
$this->setParts($str);
|
||||||
|
|
||||||
|
if ($this->lexer->hasInvalidTokens()) {
|
||||||
|
throw new \InvalidArgumentException('ERR_INVALID_ATEXT');
|
||||||
|
}
|
||||||
|
|
||||||
return array('local' => $this->localPart, 'domain' => $this->domainPart);
|
return array('local' => $this->localPart, 'domain' => $this->domainPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +87,10 @@ class EmailParser
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $localPart
|
||||||
|
* @param string $parsedDomainPart
|
||||||
|
*/
|
||||||
protected function addLongEmailWarning($localPart, $parsedDomainPart)
|
protected function addLongEmailWarning($localPart, $parsedDomainPart)
|
||||||
{
|
{
|
||||||
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
|
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace Egulias\EmailValidator;
|
namespace Egulias\EmailValidator;
|
||||||
|
|
||||||
use Egulias\EmailValidator\Parser\LocalPart;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EmailValidator
|
* EmailValidator
|
||||||
*
|
*
|
||||||
@@ -97,7 +95,7 @@ class EmailValidator
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($strict) ? (!$this->hasWarnings() && $dns) : true;
|
return !$strict || (!$this->hasWarnings() && $dns);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,16 +144,9 @@ class EmailValidator
|
|||||||
|
|
||||||
protected function checkDNS()
|
protected function checkDNS()
|
||||||
{
|
{
|
||||||
$checked = false;
|
$checked = true;
|
||||||
if (!function_exists('dns_get_record') && (
|
|
||||||
in_array(self::DNSWARN_NO_RECORD, $this->warnings) &&
|
|
||||||
in_array(self::DNSWARN_NO_MX_RECORD, $this->warnings)
|
|
||||||
)) {
|
|
||||||
return $checked;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'MX');
|
$result = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'MX');
|
||||||
$checked = true;
|
|
||||||
|
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
$this->warnings[] = self::DNSWARN_NO_RECORD;
|
$this->warnings[] = self::DNSWARN_NO_RECORD;
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ class DomainPart extends Parser
|
|||||||
if ($this->lexer->token['type'] === EmailLexer::S_EMPTY) {
|
if ($this->lexer->token['type'] === EmailLexer::S_EMPTY) {
|
||||||
throw new \InvalidArgumentException('ERR_NODOMAIN');
|
throw new \InvalidArgumentException('ERR_NODOMAIN');
|
||||||
}
|
}
|
||||||
|
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
|
||||||
|
throw new \InvalidArgumentException('ERR_DOMAINHYPHENEND');
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
||||||
$this->warnings[] = EmailValidator::DEPREC_COMMENT;
|
$this->warnings[] = EmailValidator::DEPREC_COMMENT;
|
||||||
@@ -101,8 +104,13 @@ class DomainPart extends Parser
|
|||||||
{
|
{
|
||||||
$domain = '';
|
$domain = '';
|
||||||
do {
|
do {
|
||||||
|
|
||||||
$prev = $this->lexer->getPrevious();
|
$prev = $this->lexer->getPrevious();
|
||||||
|
|
||||||
|
if ($this->lexer->token['type'] === EmailLexer::S_SLASH) {
|
||||||
|
throw new \InvalidArgumentException('ERR_DOMAIN_CHAR_NOT_ALLOWED');
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
||||||
$this->parseComments();
|
$this->parseComments();
|
||||||
$this->lexer->moveNext();
|
$this->lexer->moveNext();
|
||||||
@@ -235,6 +243,17 @@ class DomainPart extends Parser
|
|||||||
|
|
||||||
protected function checkDomainPartExceptions($prev)
|
protected function checkDomainPartExceptions($prev)
|
||||||
{
|
{
|
||||||
|
$invalidDomainTokens = array(
|
||||||
|
EmailLexer::S_DQUOTE => true,
|
||||||
|
EmailLexer::S_SEMICOLON => true,
|
||||||
|
EmailLexer::S_GREATERTHAN => true,
|
||||||
|
EmailLexer::S_LOWERTHAN => true,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isset($invalidDomainTokens[$this->lexer->token['type']])) {
|
||||||
|
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->lexer->token['type'] === EmailLexer::S_COMMA) {
|
if ($this->lexer->token['type'] === EmailLexer::S_COMMA) {
|
||||||
throw new \InvalidArgumentException('ERR_COMMA_IN_DOMAIN');
|
throw new \InvalidArgumentException('ERR_COMMA_IN_DOMAIN');
|
||||||
}
|
}
|
||||||
@@ -295,5 +314,4 @@ class DomainPart extends Parser
|
|||||||
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
|
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Parser;
|
|||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
use Egulias\EmailValidator\EmailValidator;
|
use Egulias\EmailValidator\EmailValidator;
|
||||||
|
use \InvalidArgumentException;
|
||||||
|
|
||||||
class LocalPart extends Parser
|
class LocalPart extends Parser
|
||||||
{
|
{
|
||||||
@@ -21,8 +21,7 @@ class LocalPart extends Parser
|
|||||||
|
|
||||||
$closingQuote = $this->checkDQUOTE($closingQuote);
|
$closingQuote = $this->checkDQUOTE($closingQuote);
|
||||||
if ($closingQuote && $parseDQuote) {
|
if ($closingQuote && $parseDQuote) {
|
||||||
$this->parseDoubleQuote();
|
$parseDQuote = $this->parseDoubleQuote();
|
||||||
$parseDQuote = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
||||||
@@ -56,23 +55,51 @@ class LocalPart extends Parser
|
|||||||
|
|
||||||
protected function parseDoubleQuote()
|
protected function parseDoubleQuote()
|
||||||
{
|
{
|
||||||
$special = array (
|
$parseAgain = true;
|
||||||
|
$special = array(
|
||||||
EmailLexer::S_CR => true,
|
EmailLexer::S_CR => true,
|
||||||
EmailLexer::S_HTAB => true,
|
EmailLexer::S_HTAB => true,
|
||||||
EmailLexer::S_LF => true
|
EmailLexer::S_LF => true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$invalid = array(
|
||||||
|
EmailLexer::C_NUL => true,
|
||||||
|
EmailLexer::S_HTAB => true,
|
||||||
|
EmailLexer::S_CR => true,
|
||||||
|
EmailLexer::S_LF => true
|
||||||
|
);
|
||||||
$setSpecialsWarning = true;
|
$setSpecialsWarning = true;
|
||||||
|
|
||||||
$this->lexer->moveNext();
|
$this->lexer->moveNext();
|
||||||
|
|
||||||
while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) {
|
while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) {
|
||||||
|
$parseAgain = false;
|
||||||
if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
|
if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
|
||||||
$this->warnings[] = EmailValidator::CFWS_FWS;
|
$this->warnings[] = EmailValidator::CFWS_FWS;
|
||||||
$setSpecialsWarning = false;
|
$setSpecialsWarning = false;
|
||||||
}
|
}
|
||||||
$this->lexer->moveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$this->lexer->moveNext();
|
||||||
|
|
||||||
|
if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) {
|
||||||
|
throw new InvalidArgumentException("ERR_EXPECTED_ATEXT");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$prev = $this->lexer->getPrevious();
|
||||||
|
|
||||||
|
if ($prev['type'] === EmailLexer::S_BACKSLASH) {
|
||||||
|
if (!$this->checkDQUOTE(false)) {
|
||||||
|
throw new \InvalidArgumentException("ERR_UNCLOSED_DQUOTE");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) {
|
||||||
|
throw new \InvalidArgumentException("ERR_EXPECED_AT");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parseAgain;
|
||||||
|
}
|
||||||
|
|
||||||
protected function isInvalidToken($token, $closingQuote)
|
protected function isInvalidToken($token, $closingQuote)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -124,8 +124,7 @@ abstract class Parser
|
|||||||
|
|
||||||
if ($previous['type'] === EmailLexer::S_BACKSLASH
|
if ($previous['type'] === EmailLexer::S_BACKSLASH
|
||||||
&&
|
&&
|
||||||
($this->lexer->token['type'] === EmailLexer::S_SP ||
|
$this->lexer->token['type'] !== EmailLexer::GENERIC
|
||||||
$this->lexer->token['type'] === EmailLexer::S_HTAB)
|
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -164,6 +163,7 @@ abstract class Parser
|
|||||||
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) {
|
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) {
|
||||||
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
|
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->warnings[] = EmailValidator::RFC5321_QUOTEDSTRING;
|
$this->warnings[] = EmailValidator::RFC5321_QUOTEDSTRING;
|
||||||
try {
|
try {
|
||||||
$this->lexer->find(EmailLexer::S_DQUOTE);
|
$this->lexer->find(EmailLexer::S_DQUOTE);
|
||||||
@@ -187,4 +187,4 @@ abstract class Parser
|
|||||||
throw new \InvalidArgumentException("ERR_FWS_CRLF_END");
|
throw new \InvalidArgumentException("ERR_FWS_CRLF_END");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,73 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($token, $lexer->token['type']);
|
$this->assertEquals($token, $lexer->token['type']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLexerParsesMultipleSpaces()
|
||||||
|
{
|
||||||
|
$lexer = new EmailLexer();
|
||||||
|
$lexer->setInput(' ');
|
||||||
|
$lexer->moveNext();
|
||||||
|
$lexer->moveNext();
|
||||||
|
$this->assertEquals(EmailLexer::S_SP, $lexer->token['type']);
|
||||||
|
$lexer->moveNext();
|
||||||
|
$this->assertEquals(EmailLexer::S_SP, $lexer->token['type']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider invalidUTF8CharsProvider
|
||||||
|
*/
|
||||||
|
public function testLexerParsesInvalidUTF8($char)
|
||||||
|
{
|
||||||
|
$lexer = new EmailLexer();
|
||||||
|
$lexer->setInput($char);
|
||||||
|
$lexer->moveNext();
|
||||||
|
$lexer->moveNext();
|
||||||
|
|
||||||
|
$this->assertEquals(EmailLexer::INVALID, $lexer->token['type']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function invalidUTF8CharsProvider()
|
||||||
|
{
|
||||||
|
$chars = array();
|
||||||
|
for ($i = 0; $i < 0x100; ++$i) {
|
||||||
|
$c = $this->utf8Chr($i);
|
||||||
|
if (preg_match('/(?=\p{Cc})(?=[^\t\n\n\r])/u', $c) && !preg_match('/\x{0000}/u', $c)) {
|
||||||
|
$chars[] = array($c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $chars;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function utf8Chr($code_point)
|
||||||
|
{
|
||||||
|
|
||||||
|
if ($code_point < 0 || 0x10FFFF < $code_point || (0xD800 <= $code_point && $code_point <= 0xDFFF)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($code_point < 0x80) {
|
||||||
|
$hex[0] = $code_point;
|
||||||
|
$ret = chr($hex[0]);
|
||||||
|
} elseif ($code_point < 0x800) {
|
||||||
|
$hex[0] = 0x1C0 | $code_point >> 6;
|
||||||
|
$hex[1] = 0x80 | $code_point & 0x3F;
|
||||||
|
$ret = chr($hex[0]).chr($hex[1]);
|
||||||
|
} elseif ($code_point < 0x10000) {
|
||||||
|
$hex[0] = 0xE0 | $code_point >> 12;
|
||||||
|
$hex[1] = 0x80 | $code_point >> 6 & 0x3F;
|
||||||
|
$hex[2] = 0x80 | $code_point & 0x3F;
|
||||||
|
$ret = chr($hex[0]).chr($hex[1]).chr($hex[2]);
|
||||||
|
} else {
|
||||||
|
$hex[0] = 0xF0 | $code_point >> 18;
|
||||||
|
$hex[1] = 0x80 | $code_point >> 12 & 0x3F;
|
||||||
|
$hex[2] = 0x80 | $code_point >> 6 & 0x3F;
|
||||||
|
$hex[3] = 0x80 | $code_point & 0x3F;
|
||||||
|
$ret = chr($hex[0]).chr($hex[1]).chr($hex[2]).chr($hex[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
public function testLexerForTab()
|
public function testLexerForTab()
|
||||||
{
|
{
|
||||||
$lexer = new EmailLexer();
|
$lexer = new EmailLexer();
|
||||||
@@ -36,6 +103,17 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(EmailLexer::S_HTAB, $lexer->token['type']);
|
$this->assertEquals(EmailLexer::S_HTAB, $lexer->token['type']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLexerForUTF8()
|
||||||
|
{
|
||||||
|
$lexer = new EmailLexer();
|
||||||
|
$lexer->setInput("áÇ@bar.com");
|
||||||
|
$lexer->moveNext();
|
||||||
|
$lexer->moveNext();
|
||||||
|
$this->assertEquals(EmailLexer::GENERIC, $lexer->token['type']);
|
||||||
|
$lexer->moveNext();
|
||||||
|
$this->assertEquals(EmailLexer::GENERIC, $lexer->token['type']);
|
||||||
|
}
|
||||||
|
|
||||||
public function testLexerSearchToken()
|
public function testLexerSearchToken()
|
||||||
{
|
{
|
||||||
$lexer = new EmailLexer();
|
$lexer = new EmailLexer();
|
||||||
@@ -61,6 +139,7 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
|
|||||||
array("\"", EmailLexer::S_DQUOTE),
|
array("\"", EmailLexer::S_DQUOTE),
|
||||||
array("-", EmailLexer::S_HYPHEN),
|
array("-", EmailLexer::S_HYPHEN),
|
||||||
array("\\", EmailLexer::S_BACKSLASH),
|
array("\\", EmailLexer::S_BACKSLASH),
|
||||||
|
array("/", EmailLexer::S_SLASH),
|
||||||
array("(", EmailLexer::S_OPENPARENTHESIS),
|
array("(", EmailLexer::S_OPENPARENTHESIS),
|
||||||
array(")", EmailLexer::S_CLOSEPARENTHESIS),
|
array(")", EmailLexer::S_CLOSEPARENTHESIS),
|
||||||
array('<', EmailLexer::S_LOWERTHAN),
|
array('<', EmailLexer::S_LOWERTHAN),
|
||||||
@@ -74,7 +153,9 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
|
|||||||
array('{', EmailLexer::S_OPENQBRACKET),
|
array('{', EmailLexer::S_OPENQBRACKET),
|
||||||
array('}', EmailLexer::S_CLOSEQBRACKET),
|
array('}', EmailLexer::S_CLOSEQBRACKET),
|
||||||
array('', EmailLexer::S_EMPTY),
|
array('', EmailLexer::S_EMPTY),
|
||||||
array(chr(31), EmailLexer::INVALID)
|
array(chr(31), EmailLexer::INVALID),
|
||||||
|
array(chr(226), EmailLexer::GENERIC),
|
||||||
|
array(chr(0), EmailLexer::C_NUL)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,9 +26,18 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue($this->validator->isValid($email));
|
$this->assertTrue($this->validator->isValid($email));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInvalidUTF8Email()
|
||||||
|
{
|
||||||
|
$validator = new EmailValidator;
|
||||||
|
$email = "\x80\x81\x82@\x83\x84\x85.\x86\x87\x88";
|
||||||
|
|
||||||
|
$this->assertFalse($validator->isValid($email));
|
||||||
|
}
|
||||||
|
|
||||||
public function getValidEmails()
|
public function getValidEmails()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
array('â@iana.org'),
|
||||||
array('fabien@symfony.com'),
|
array('fabien@symfony.com'),
|
||||||
array('example@example.co.uk'),
|
array('example@example.co.uk'),
|
||||||
array('fabien_potencier@example.fr'),
|
array('fabien_potencier@example.fr'),
|
||||||
@@ -43,6 +52,17 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('"user,name"@example.com'),
|
array('"user,name"@example.com'),
|
||||||
array('"user name"@example.com'),
|
array('"user name"@example.com'),
|
||||||
array('"user@name"@example.com'),
|
array('"user@name"@example.com'),
|
||||||
|
array('"\a"@iana.org'),
|
||||||
|
array('"test\ test"@iana.org'),
|
||||||
|
array('""@iana.org'),
|
||||||
|
array('"\""@iana.org'),
|
||||||
|
array('müller@möller.de'),
|
||||||
|
array('test@email*'),
|
||||||
|
array('test@email!'),
|
||||||
|
array('test@email&'),
|
||||||
|
array('test@email^'),
|
||||||
|
array('test@email%'),
|
||||||
|
array('test@email$'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,9 +77,12 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function getInvalidEmails()
|
public function getInvalidEmails()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
array('test@example.com test'),
|
||||||
|
array('user name@example.com'),
|
||||||
|
array('user name@example.com'),
|
||||||
array('example.@example.co.uk'),
|
array('example.@example.co.uk'),
|
||||||
array('example@example@example.co.uk'),
|
array('example@example@example.co.uk'),
|
||||||
array('(fabien_potencier@example.fr)'),
|
array('(test_exampel@example.fr)'),
|
||||||
array('example(example)example@example.co.uk'),
|
array('example(example)example@example.co.uk'),
|
||||||
array('.example@localhost'),
|
array('.example@localhost'),
|
||||||
array('ex\ample@localhost'),
|
array('ex\ample@localhost'),
|
||||||
@@ -72,6 +95,34 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('username@example,com'),
|
array('username@example,com'),
|
||||||
array('usern,ame@example.com'),
|
array('usern,ame@example.com'),
|
||||||
array('user[na]me@example.com'),
|
array('user[na]me@example.com'),
|
||||||
|
array('"""@iana.org'),
|
||||||
|
array('"\"@iana.org'),
|
||||||
|
array('"test"test@iana.org'),
|
||||||
|
array('"test""test"@iana.org'),
|
||||||
|
array('"test"."test"@iana.org'),
|
||||||
|
array('"test".test@iana.org'),
|
||||||
|
array('"test"' . chr(0) . '@iana.org'),
|
||||||
|
array('"test\"@iana.org'),
|
||||||
|
array(chr(226) . '@iana.org'),
|
||||||
|
array('test@' . chr(226) . '.org'),
|
||||||
|
array('\r\ntest@iana.org'),
|
||||||
|
array('\r\n test@iana.org'),
|
||||||
|
array('\r\n \r\ntest@iana.org'),
|
||||||
|
array('\r\n \r\ntest@iana.org'),
|
||||||
|
array('\r\n \r\n test@iana.org'),
|
||||||
|
array('test@iana.org \r\n'),
|
||||||
|
array('test@iana.org \r\n '),
|
||||||
|
array('test@iana.org \r\n \r\n'),
|
||||||
|
array('test@iana.org \r\n\r\n'),
|
||||||
|
array('test@iana.org \r\n\r\n '),
|
||||||
|
array('test@iana/icann.org'),
|
||||||
|
array('test@foo;bar.com'),
|
||||||
|
array('test;123@foobar.com'),
|
||||||
|
array('test@example..com'),
|
||||||
|
array('email.email@email."'),
|
||||||
|
array('test@email>'),
|
||||||
|
array('test@email<'),
|
||||||
|
array('test@email{'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,16 +175,45 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($warnings, $this->validator->getWarnings());
|
$this->assertEquals($warnings, $this->validator->getWarnings());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getInvalidEmailsWithWarnings
|
||||||
|
*/
|
||||||
|
public function testInvalidEmailsWithDnsCheckAndStrictMode($warnings, $email)
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->validator->isValid($email, true, true));
|
||||||
|
|
||||||
|
$this->assertEquals($warnings, $this->validator->getWarnings());
|
||||||
|
}
|
||||||
|
|
||||||
public function getInvalidEmailsWithWarnings()
|
public function getInvalidEmailsWithWarnings()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(array( EmailValidator::DEPREC_CFWS_NEAR_AT,), 'example @example.co.uk'),
|
array(
|
||||||
array(array( EmailValidator::DEPREC_CFWS_NEAR_AT,), 'example@ example.co.uk'),
|
array(
|
||||||
array(array( EmailValidator::CFWS_COMMENT,), 'example@example(examplecomment).co.uk'),
|
EmailValidator::DEPREC_CFWS_NEAR_AT,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD
|
||||||
|
),
|
||||||
|
'example @example.co.uk'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
EmailValidator::DEPREC_CFWS_NEAR_AT,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD
|
||||||
|
),
|
||||||
|
'example@ example.co.uk'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
EmailValidator::CFWS_COMMENT,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD
|
||||||
|
),
|
||||||
|
'example@example(examplecomment).co.uk'
|
||||||
|
),
|
||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
EmailValidator::CFWS_COMMENT,
|
EmailValidator::CFWS_COMMENT,
|
||||||
EmailValidator::DEPREC_CFWS_NEAR_AT,
|
EmailValidator::DEPREC_CFWS_NEAR_AT,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD,
|
||||||
),
|
),
|
||||||
'example(examplecomment)@example.co.uk'
|
'example(examplecomment)@example.co.uk'
|
||||||
),
|
),
|
||||||
@@ -141,6 +221,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(
|
array(
|
||||||
EmailValidator::RFC5321_QUOTEDSTRING,
|
EmailValidator::RFC5321_QUOTEDSTRING,
|
||||||
EmailValidator::CFWS_FWS,
|
EmailValidator::CFWS_FWS,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD,
|
||||||
),
|
),
|
||||||
"\"\t\"@example.co.uk"
|
"\"\t\"@example.co.uk"
|
||||||
),
|
),
|
||||||
@@ -148,6 +229,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(
|
array(
|
||||||
EmailValidator::RFC5321_QUOTEDSTRING,
|
EmailValidator::RFC5321_QUOTEDSTRING,
|
||||||
EmailValidator::CFWS_FWS,
|
EmailValidator::CFWS_FWS,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD
|
||||||
),
|
),
|
||||||
"\"\r\"@example.co.uk"
|
"\"\r\"@example.co.uk"
|
||||||
),
|
),
|
||||||
@@ -239,12 +321,14 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
EmailValidator::RFC5321_QUOTEDSTRING,
|
EmailValidator::RFC5321_QUOTEDSTRING,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD
|
||||||
),
|
),
|
||||||
'"example"@example.co.uk'
|
'"example"@example.co.uk'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
EmailValidator::RFC5322_LOCAL_TOOLONG,
|
EmailValidator::RFC5322_LOCAL_TOOLONG,
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD
|
||||||
),
|
),
|
||||||
'too_long_localpart_too_long_localpart_too_long_localpart_too_long_localpart@example.co.uk'
|
'too_long_localpart_too_long_localpart_too_long_localpart_too_long_localpart@example.co.uk'
|
||||||
),
|
),
|
||||||
@@ -275,6 +359,17 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
|
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
|
||||||
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpar'
|
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpar'
|
||||||
),
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
EmailValidator::DNSWARN_NO_RECORD,
|
||||||
|
),
|
||||||
|
'test@test'
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInvalidEmailsWithStrict()
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->validator->isValid('"test"@test', false, true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user