mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-01 13:08:58 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0dbf5d7845 | |||
| 0878374ab8 | |||
| 0c26e9dece | |||
| ca90a3291e | |||
| d04083e28b | |||
| db476db25e | |||
| d37d5a361e |
@@ -0,0 +1,3 @@
|
|||||||
|
# Thanks!
|
||||||
|
|
||||||
|
github: [egulias]
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
|
.phpunit.result.cache
|
||||||
.idea
|
.idea
|
||||||
|
.vscode
|
||||||
composer.lock
|
composer.lock
|
||||||
report/
|
report/
|
||||||
vendor/
|
vendor/
|
||||||
|
|||||||
+23
-1
@@ -1,5 +1,4 @@
|
|||||||
language: php
|
language: php
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- php: 5.5.9
|
- php: 5.5.9
|
||||||
@@ -28,6 +27,29 @@ matrix:
|
|||||||
env:
|
env:
|
||||||
- psalm=yes
|
- psalm=yes
|
||||||
dist: bionic
|
dist: bionic
|
||||||
|
#ppc64le support code
|
||||||
|
- php: 5.6
|
||||||
|
arch: ppc64le
|
||||||
|
env:
|
||||||
|
dist: xenial
|
||||||
|
- php: 7.0
|
||||||
|
arch: ppc64le
|
||||||
|
dist: xenial
|
||||||
|
- php: 7.1
|
||||||
|
arch: ppc64le
|
||||||
|
env:
|
||||||
|
- psalm=yes
|
||||||
|
dist: bionic
|
||||||
|
- php: 7.2
|
||||||
|
arch: ppc64le
|
||||||
|
env:
|
||||||
|
- psalm=yes
|
||||||
|
dist: bionic
|
||||||
|
- php: 7.4
|
||||||
|
arch: ppc64le
|
||||||
|
env:
|
||||||
|
- psalm=yes
|
||||||
|
dist: bionic
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- if [ "$deps" = "low" ]; then composer update --prefer-lowest; else composer install; fi
|
- if [ "$deps" = "low" ]; then composer update --prefer-lowest; else composer install; fi
|
||||||
|
|||||||
@@ -42,16 +42,14 @@ $validator->isValid("example@example.com", new RFCValidation()); //true
|
|||||||
|
|
||||||
### Available validations ###
|
### Available validations ###
|
||||||
|
|
||||||
1. [RFCValidation](/src/Validation/RFCValidation.php)
|
1. [RFCValidation](/src/Validation/RFCValidation.php): Standard RFC-like email validation.
|
||||||
2. [NoRFCWarningsValidation](/src/Validation/NoRFCWarningsValidation.php)
|
2. [NoRFCWarningsValidation](/src/Validation/NoRFCWarningsValidation.php): RFC-like validation that will fail when warnings* are found.
|
||||||
3. [DNSCheckValidation](/src/Validation/DNSCheckValidation.php)
|
3. [DNSCheckValidation](/src/Validation/DNSCheckValidation.php): Will check if there are DNS records that signal that the server accepts emails. This does not entails that the email exists.
|
||||||
4. [SpoofCheckValidation](/src/Validation/SpoofCheckValidation.php)
|
4. [SpoofCheckValidation](/src/Validation/SpoofCheckValidation.php): Will check for multi-utf-8 chars that can signal an erroneous email name.
|
||||||
5. [MultipleValidationWithAnd](/src/Validation/MultipleValidationWithAnd.php)
|
5. [MultipleValidationWithAnd](/src/Validation/MultipleValidationWithAnd.php): It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
|
||||||
6. [Your own validation](#how-to-extend)
|
6. [Your own validation](#how-to-extend): You can extend the library behaviour by implementing your own validations.
|
||||||
|
|
||||||
`MultipleValidationWithAnd`
|
*warnings: Warnings are deviations from the RFC that in a broader interpretation are acceptded.
|
||||||
|
|
||||||
It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
@@ -66,7 +64,8 @@ $multipleValidations = new MultipleValidationWithAnd([
|
|||||||
new RFCValidation(),
|
new RFCValidation(),
|
||||||
new DNSCheckValidation()
|
new DNSCheckValidation()
|
||||||
]);
|
]);
|
||||||
$validator->isValid("example@example.com", $multipleValidations); //true
|
//ietf.org has MX records signaling a server with email capabilites
|
||||||
|
$validator->isValid("example@ietf.org", $multipleValidations); //true
|
||||||
```
|
```
|
||||||
|
|
||||||
### How to extend ###
|
### How to extend ###
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ class EmailLexer extends AbstractLexer
|
|||||||
const S_DOT = 46;
|
const S_DOT = 46;
|
||||||
const S_DQUOTE = 34;
|
const S_DQUOTE = 34;
|
||||||
const S_SQUOTE = 39;
|
const S_SQUOTE = 39;
|
||||||
|
const S_BACKTICK = 96;
|
||||||
const S_OPENPARENTHESIS = 49;
|
const S_OPENPARENTHESIS = 49;
|
||||||
const S_CLOSEPARENTHESIS = 261;
|
const S_CLOSEPARENTHESIS = 261;
|
||||||
const S_OPENBRACKET = 262;
|
const S_OPENBRACKET = 262;
|
||||||
@@ -60,6 +61,7 @@ class EmailLexer extends AbstractLexer
|
|||||||
',' => self::S_COMMA,
|
',' => self::S_COMMA,
|
||||||
'.' => self::S_DOT,
|
'.' => self::S_DOT,
|
||||||
"'" => self::S_SQUOTE,
|
"'" => self::S_SQUOTE,
|
||||||
|
"`" => self::S_BACKTICK,
|
||||||
'"' => self::S_DQUOTE,
|
'"' => self::S_DQUOTE,
|
||||||
'-' => self::S_HYPHEN,
|
'-' => self::S_HYPHEN,
|
||||||
'::' => self::S_DOUBLECOLON,
|
'::' => self::S_DOUBLECOLON,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ use Egulias\EmailValidator\Warning\TLD;
|
|||||||
class DomainPart extends Parser
|
class DomainPart extends Parser
|
||||||
{
|
{
|
||||||
const DOMAIN_MAX_LENGTH = 254;
|
const DOMAIN_MAX_LENGTH = 254;
|
||||||
|
const LABEL_MAX_LENGTH = 63;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@@ -160,6 +161,7 @@ class DomainPart extends Parser
|
|||||||
protected function doParseDomainPart()
|
protected function doParseDomainPart()
|
||||||
{
|
{
|
||||||
$domain = '';
|
$domain = '';
|
||||||
|
$label = '';
|
||||||
$openedParenthesis = 0;
|
$openedParenthesis = 0;
|
||||||
do {
|
do {
|
||||||
$prev = $this->lexer->getPrevious();
|
$prev = $this->lexer->getPrevious();
|
||||||
@@ -190,7 +192,12 @@ class DomainPart extends Parser
|
|||||||
$this->parseDomainLiteral();
|
$this->parseDomainLiteral();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->checkLabelLength($prev);
|
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
|
||||||
|
$this->checkLabelLength($label);
|
||||||
|
$label = '';
|
||||||
|
} else {
|
||||||
|
$label .= $this->lexer->token['value'];
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->isFWS()) {
|
if ($this->isFWS()) {
|
||||||
$this->parseFWS();
|
$this->parseFWS();
|
||||||
@@ -203,6 +210,8 @@ class DomainPart extends Parser
|
|||||||
}
|
}
|
||||||
} while (null !== $this->lexer->token['type']);
|
} while (null !== $this->lexer->token['type']);
|
||||||
|
|
||||||
|
$this->checkLabelLength($label);
|
||||||
|
|
||||||
return $domain;
|
return $domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,6 +345,7 @@ class DomainPart extends Parser
|
|||||||
$invalidDomainTokens = array(
|
$invalidDomainTokens = array(
|
||||||
EmailLexer::S_DQUOTE => true,
|
EmailLexer::S_DQUOTE => true,
|
||||||
EmailLexer::S_SQUOTE => true,
|
EmailLexer::S_SQUOTE => true,
|
||||||
|
EmailLexer::S_BACKTICK => true,
|
||||||
EmailLexer::S_SEMICOLON => true,
|
EmailLexer::S_SEMICOLON => true,
|
||||||
EmailLexer::S_GREATERTHAN => true,
|
EmailLexer::S_GREATERTHAN => true,
|
||||||
EmailLexer::S_LOWERTHAN => true,
|
EmailLexer::S_LOWERTHAN => true,
|
||||||
@@ -385,16 +395,31 @@ class DomainPart extends Parser
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkLabelLength(array $prev)
|
/**
|
||||||
|
* @param string $label
|
||||||
|
*/
|
||||||
|
protected function checkLabelLength($label)
|
||||||
{
|
{
|
||||||
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
|
if ($this->isLabelTooLong($label)) {
|
||||||
$prev['type'] === EmailLexer::GENERIC &&
|
|
||||||
strlen($prev['value']) > 63
|
|
||||||
) {
|
|
||||||
$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
|
$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $label
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isLabelTooLong($label)
|
||||||
|
{
|
||||||
|
if (preg_match('/[^\x00-\x7F]/', $label)) {
|
||||||
|
idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
|
||||||
|
|
||||||
|
return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
|
||||||
|
}
|
||||||
|
|
||||||
|
return strlen($label) > self::LABEL_MAX_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
protected function parseDomainComments()
|
protected function parseDomainComments()
|
||||||
{
|
{
|
||||||
$this->isUnclosedComment();
|
$this->isUnclosedComment();
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ abstract class Warning
|
|||||||
*/
|
*/
|
||||||
public function code()
|
public function code()
|
||||||
{
|
{
|
||||||
return self::CODE;
|
return static::CODE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -139,6 +139,7 @@ class EmailLexerTests extends TestCase
|
|||||||
array(".", EmailLexer::S_DOT),
|
array(".", EmailLexer::S_DOT),
|
||||||
array("\"", EmailLexer::S_DQUOTE),
|
array("\"", EmailLexer::S_DQUOTE),
|
||||||
array("'", EmailLexer::S_SQUOTE),
|
array("'", EmailLexer::S_SQUOTE),
|
||||||
|
array("`", EmailLexer::S_BACKTICK),
|
||||||
array("-", EmailLexer::S_HYPHEN),
|
array("-", EmailLexer::S_HYPHEN),
|
||||||
array("\\", EmailLexer::S_BACKSLASH),
|
array("\\", EmailLexer::S_BACKSLASH),
|
||||||
array("/", EmailLexer::S_SLASH),
|
array("/", EmailLexer::S_SLASH),
|
||||||
|
|||||||
@@ -26,11 +26,22 @@ class NoRFCWarningsValidationTest extends TestCase
|
|||||||
$this->assertInstanceOf(RFCWarnings::class, $validation->getError());
|
$this->assertInstanceOf(RFCWarnings::class, $validation->getError());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmailWithoutWarningsIsValid()
|
/**
|
||||||
|
* @dataProvider getValidEmailsWithoutWarnings
|
||||||
|
*/
|
||||||
|
public function testEmailWithoutWarningsIsValid($email)
|
||||||
{
|
{
|
||||||
$validation = new NoRFCWarningsValidation();
|
$validation = new NoRFCWarningsValidation();
|
||||||
|
|
||||||
$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
|
$this->assertTrue($validation->isValid($email, new EmailLexer()));
|
||||||
$this->assertNull($validation->getError());
|
$this->assertNull($validation->getError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getValidEmailsWithoutWarnings()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['example@example.com',],
|
||||||
|
[sprintf('example@%s.com', str_repeat('ъ', 40)),],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ use Egulias\EmailValidator\Warning\CFWSWithFWS;
|
|||||||
use Egulias\EmailValidator\Warning\Comment;
|
use Egulias\EmailValidator\Warning\Comment;
|
||||||
use Egulias\EmailValidator\Warning\DomainLiteral;
|
use Egulias\EmailValidator\Warning\DomainLiteral;
|
||||||
use Egulias\EmailValidator\Warning\DomainTooLong;
|
use Egulias\EmailValidator\Warning\DomainTooLong;
|
||||||
|
use Egulias\EmailValidator\Warning\EmailTooLong;
|
||||||
use Egulias\EmailValidator\Warning\IPV6BadChar;
|
use Egulias\EmailValidator\Warning\IPV6BadChar;
|
||||||
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
|
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
|
||||||
use Egulias\EmailValidator\Warning\IPV6ColonStart;
|
use Egulias\EmailValidator\Warning\IPV6ColonStart;
|
||||||
@@ -169,6 +170,7 @@ class RFCValidationTest extends TestCase
|
|||||||
['test@ '],
|
['test@ '],
|
||||||
['test@example.com []'],
|
['test@example.com []'],
|
||||||
["test@example.com'"],
|
["test@example.com'"],
|
||||||
|
["test@exam`ple.com"],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,12 +227,12 @@ class RFCValidationTest extends TestCase
|
|||||||
$this->assertTrue($this->validator->isValid($email, $this->lexer));
|
$this->assertTrue($this->validator->isValid($email, $this->lexer));
|
||||||
$warnings = $this->validator->getWarnings();
|
$warnings = $this->validator->getWarnings();
|
||||||
$this->assertCount(
|
$this->assertCount(
|
||||||
count($warnings), $expectedWarnings,
|
count($expectedWarnings), $warnings,
|
||||||
"Expected: " . implode(",", $expectedWarnings) . " and got " . implode(",", $warnings)
|
"Expected: " . implode(",", $expectedWarnings) . " and got " . implode(",", $warnings)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($warnings as $warning) {
|
foreach ($warnings as $warning) {
|
||||||
$this->assertArrayHasKey($warning->code(), $expectedWarnings);
|
$this->assertContains($warning->code(), $expectedWarnings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,17 +279,29 @@ class RFCValidationTest extends TestCase
|
|||||||
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
|
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
[DomainTooLong::CODE, LabelTooLong::CODE,],
|
[DomainTooLong::CODE, LabelTooLong::CODE, EmailTooLong::CODE],
|
||||||
'example2@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
|
'example2@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
|
||||||
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
|
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
|
||||||
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'
|
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
[DomainTooLong::CODE, LabelTooLong::CODE,],
|
[DomainTooLong::CODE, LabelTooLong::CODE, EmailTooLong::CODE],
|
||||||
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
|
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
|
||||||
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
|
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
|
||||||
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpar'
|
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpar'
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
[LabelTooLong::CODE,],
|
||||||
|
sprintf('example@%s.com', str_repeat('ъ', 60)),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[LabelTooLong::CODE,],
|
||||||
|
sprintf('example@%s.com', str_repeat('a4t', 22)),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[LabelTooLong::CODE,],
|
||||||
|
sprintf('example@%s', str_repeat('a4t', 22)),
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user