mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-31 20:50:01 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ac0be80b3 | |||
| c38e9600fb | |||
| 777864f79e | |||
| ecbcdcef05 | |||
| b28de87213 | |||
| 6d6a87e5d9 | |||
| e3c1f426e5 |
+1
-1
@@ -3,7 +3,7 @@ imports:
|
||||
|
||||
build:
|
||||
environment:
|
||||
php: '7.4'
|
||||
php: '7.3'
|
||||
tests:
|
||||
override:
|
||||
-
|
||||
|
||||
@@ -2,6 +2,10 @@ language: php
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- php: 7.3
|
||||
env:
|
||||
- psalm=yes
|
||||
dist: bionic
|
||||
- php: 7.4
|
||||
dist: bionic
|
||||
env:
|
||||
@@ -11,6 +15,11 @@ matrix:
|
||||
- psalm=yes
|
||||
dist: bionic
|
||||
#ppc64le support code
|
||||
- php: 7.3
|
||||
arch: ppc64le
|
||||
env:
|
||||
- psalm=yes
|
||||
dist: bionic
|
||||
- php: 7.4
|
||||
arch: ppc64le
|
||||
env:
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
* Access to local part and domain part from EmailParser
|
||||
* Validations outside of the scope of the RFC will be considered "extra" validations, thus opening the door for adding new; will live in their own folder "extra" (as requested in #248, #195, #183).
|
||||
|
||||
## Breaking changes
|
||||
## Breacking changes
|
||||
|
||||
* PHP version upgraded to match Symfony's (as of 12/2020).
|
||||
* DNSCheckValidation now fails for missing MX records. While the RFC argues that the existence of only A records to be valid, starting in v3 they will be considered invalid.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2013-2022 Eduardo Gulias Davis
|
||||
Copyright (c) 2013-2021 Eduardo Gulias Davis
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.2",
|
||||
"doctrine/lexer": "^1.2|^2",
|
||||
"doctrine/lexer": "^1.2",
|
||||
"symfony/polyfill-intl-idn": "^1.15"
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
Generated
+241
-340
File diff suppressed because it is too large
Load Diff
+16
-5
@@ -1,8 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<files psalm-version="5.4.0@62db5d4f6a7ae0a20f7cc5a4952d730272fc0863">
|
||||
<file src="src/Parser/DomainPart.php">
|
||||
<RedundantConditionGivenDocblockType occurrences="1">
|
||||
<code>null !== $this->lexer->token['type']</code>
|
||||
</RedundantConditionGivenDocblockType>
|
||||
<files psalm-version="3.8.3@389af1bfc739bfdff3f9e3dc7bd6499aee51a831">
|
||||
<file src="src/EmailLexer.php">
|
||||
<DocblockTypeContradiction occurrences="1">
|
||||
<code>self::$nullToken</code>
|
||||
</DocblockTypeContradiction>
|
||||
</file>
|
||||
<file src="src/Parser/Parser.php">
|
||||
<MissingReturnType occurrences="1">
|
||||
<code>parse</code>
|
||||
</MissingReturnType>
|
||||
</file>
|
||||
<file src="src/Validation/SpoofCheckValidation.php">
|
||||
<UndefinedClass occurrences="2">
|
||||
<code>Spoofchecker</code>
|
||||
<code>Spoofchecker</code>
|
||||
</UndefinedClass>
|
||||
</file>
|
||||
</files>
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
totallyTyped="false"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config ./vendor/vimeo/psalm/config.xsd"
|
||||
errorBaseline="psalm.baseline.xml"
|
||||
errorBaseline="./psalm.baseline.xml"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="src" />
|
||||
@@ -13,11 +14,5 @@
|
||||
</projectFiles>
|
||||
|
||||
<issueHandlers>
|
||||
<DeprecatedMethod>
|
||||
<errorLevel type="suppress">
|
||||
<!-- This issue needs to be resolved before upgrading to Lexer 3 -->
|
||||
<referencedMethod name="Doctrine\Common\Lexer\Token::offsetGet"/>
|
||||
</errorLevel>
|
||||
</DeprecatedMethod>
|
||||
</issueHandlers>
|
||||
</psalm>
|
||||
|
||||
+59
-67
@@ -3,62 +3,58 @@
|
||||
namespace Egulias\EmailValidator;
|
||||
|
||||
use Doctrine\Common\Lexer\AbstractLexer;
|
||||
use Doctrine\Common\Lexer\Token;
|
||||
|
||||
/**
|
||||
* @extends AbstractLexer<int, string>
|
||||
*/
|
||||
class EmailLexer extends AbstractLexer
|
||||
{
|
||||
//ASCII values
|
||||
public const S_EMPTY = null;
|
||||
public const C_NUL = 0;
|
||||
public const S_HTAB = 9;
|
||||
public const S_LF = 10;
|
||||
public const S_CR = 13;
|
||||
public const S_SP = 32;
|
||||
public const EXCLAMATION = 33;
|
||||
public const S_DQUOTE = 34;
|
||||
public const NUMBER_SIGN = 35;
|
||||
public const DOLLAR = 36;
|
||||
public const PERCENTAGE = 37;
|
||||
public const AMPERSAND = 38;
|
||||
public const S_SQUOTE = 39;
|
||||
public const S_OPENPARENTHESIS = 40;
|
||||
public const S_CLOSEPARENTHESIS = 41;
|
||||
public const ASTERISK = 42;
|
||||
public const S_PLUS = 43;
|
||||
public const S_COMMA = 44;
|
||||
public const S_HYPHEN = 45;
|
||||
public const S_DOT = 46;
|
||||
public const S_SLASH = 47;
|
||||
public const S_COLON = 58;
|
||||
public const S_SEMICOLON = 59;
|
||||
public const S_LOWERTHAN = 60;
|
||||
public const S_EQUAL = 61;
|
||||
public const S_GREATERTHAN = 62;
|
||||
public const QUESTIONMARK = 63;
|
||||
public const S_AT = 64;
|
||||
public const S_OPENBRACKET = 91;
|
||||
public const S_BACKSLASH = 92;
|
||||
public const S_CLOSEBRACKET = 93;
|
||||
public const CARET = 94;
|
||||
public const S_UNDERSCORE = 95;
|
||||
public const S_BACKTICK = 96;
|
||||
public const S_OPENCURLYBRACES = 123;
|
||||
public const S_PIPE = 124;
|
||||
public const S_CLOSECURLYBRACES = 125;
|
||||
public const S_TILDE = 126;
|
||||
public const C_DEL = 127;
|
||||
public const INVERT_QUESTIONMARK= 168;
|
||||
public const INVERT_EXCLAMATION = 173;
|
||||
public const GENERIC = 300;
|
||||
public const S_IPV6TAG = 301;
|
||||
public const INVALID = 302;
|
||||
public const CRLF = 1310;
|
||||
public const S_DOUBLECOLON = 5858;
|
||||
public const ASCII_INVALID_FROM = 127;
|
||||
public const ASCII_INVALID_TO = 199;
|
||||
const S_EMPTY = null;
|
||||
const C_NUL = 0;
|
||||
const S_HTAB = 9;
|
||||
const S_LF = 10;
|
||||
const S_CR = 13;
|
||||
const S_SP = 32;
|
||||
const EXCLAMATION = 33;
|
||||
const S_DQUOTE = 34;
|
||||
const NUMBER_SIGN = 35;
|
||||
const DOLLAR = 36;
|
||||
const PERCENTAGE = 37;
|
||||
const AMPERSAND = 38;
|
||||
const S_SQUOTE = 39;
|
||||
const S_OPENPARENTHESIS = 40;
|
||||
const S_CLOSEPARENTHESIS = 41;
|
||||
const ASTERISK = 42;
|
||||
const S_PLUS = 43;
|
||||
const S_COMMA = 44;
|
||||
const S_HYPHEN = 45;
|
||||
const S_DOT = 46;
|
||||
const S_SLASH = 47;
|
||||
const S_COLON = 58;
|
||||
const S_SEMICOLON = 59;
|
||||
const S_LOWERTHAN = 60;
|
||||
const S_EQUAL = 61;
|
||||
const S_GREATERTHAN = 62;
|
||||
const QUESTIONMARK = 63;
|
||||
const S_AT = 64;
|
||||
const S_OPENBRACKET = 91;
|
||||
const S_BACKSLASH = 92;
|
||||
const S_CLOSEBRACKET = 93;
|
||||
const CARET = 94;
|
||||
const S_UNDERSCORE = 95;
|
||||
const S_BACKTICK = 96;
|
||||
const S_OPENCURLYBRACES = 123;
|
||||
const S_PIPE = 124;
|
||||
const S_CLOSECURLYBRACES = 125;
|
||||
const S_TILDE = 126;
|
||||
const C_DEL = 127;
|
||||
const INVERT_QUESTIONMARK= 168;
|
||||
const INVERT_EXCLAMATION = 173;
|
||||
const GENERIC = 300;
|
||||
const S_IPV6TAG = 301;
|
||||
const INVALID = 302;
|
||||
const CRLF = 1310;
|
||||
const S_DOUBLECOLON = 5858;
|
||||
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)
|
||||
@@ -111,11 +107,11 @@ class EmailLexer extends AbstractLexer
|
||||
'¡' => self::INVERT_EXCLAMATION,
|
||||
];
|
||||
|
||||
public const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
|
||||
const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
|
||||
|
||||
public const VALID_UTF8_REGEX = '/\p{Cc}+/u';
|
||||
const VALID_UTF8_REGEX = '/\p{Cc}+/u';
|
||||
|
||||
public const CATCHABLE_PATTERNS = [
|
||||
const CATCHABLE_PATTERNS = [
|
||||
'[a-zA-Z]+[46]?', //ASCII and domain literal
|
||||
'[^\x00-\x7F]', //UTF-8
|
||||
'[0-9]+',
|
||||
@@ -125,11 +121,11 @@ class EmailLexer extends AbstractLexer
|
||||
'.',
|
||||
];
|
||||
|
||||
public const NON_CATCHABLE_PATTERNS = [
|
||||
const NON_CATCHABLE_PATTERNS = [
|
||||
'[\xA0-\xff]+',
|
||||
];
|
||||
|
||||
public const MODIFIERS = 'iu';
|
||||
const MODIFIERS = 'iu';
|
||||
|
||||
/** @var bool */
|
||||
protected $hasInvalidTokens = false;
|
||||
@@ -144,20 +140,18 @@ class EmailLexer extends AbstractLexer
|
||||
/**
|
||||
* The last matched/seen token.
|
||||
*
|
||||
* @var array|Token
|
||||
* @var array
|
||||
*
|
||||
* @psalm-suppress NonInvariantDocblockPropertyType
|
||||
* @psalm-var array{value:string, type:null|int, position:int}|Token<int, string>
|
||||
* @psalm-var array{value:string, type:null|int, position:int}
|
||||
* @psalm-suppress NonInvariantDocblockPropertyType
|
||||
*/
|
||||
public $token;
|
||||
|
||||
/**
|
||||
* The next token in the input.
|
||||
*
|
||||
* @var array|Token|null
|
||||
*
|
||||
* @psalm-suppress NonInvariantDocblockPropertyType
|
||||
* @psalm-var array{position: int, type: int|null|string, value: int|string}|Token<int, string>|null
|
||||
* @var array|null
|
||||
*/
|
||||
public $lookahead;
|
||||
|
||||
@@ -216,9 +210,7 @@ class EmailLexer extends AbstractLexer
|
||||
$this->accumulator .= $this->token['value'];
|
||||
}
|
||||
|
||||
$this->previous = $this->token instanceof Token
|
||||
? ['value' => $this->token->value, 'type' => $this->token->type, 'position' => $this->token->position]
|
||||
: $this->token;
|
||||
$this->previous = $this->token;
|
||||
|
||||
if($this->lookahead === null) {
|
||||
$this->lookahead = self::$nullToken;
|
||||
@@ -245,7 +237,7 @@ class EmailLexer extends AbstractLexer
|
||||
$encoded = $value;
|
||||
|
||||
if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') {
|
||||
$encoded = mb_convert_encoding($value, 'UTF-8', 'Windows-1252');
|
||||
$encoded = utf8_encode($value);
|
||||
}
|
||||
|
||||
if ($this->isValid($encoded)) {
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Egulias\EmailValidator;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Result\Result;
|
||||
use Egulias\EmailValidator\Parser\LocalPart;
|
||||
use Egulias\EmailValidator\Parser\DomainPart;
|
||||
@@ -12,7 +13,7 @@ use Egulias\EmailValidator\Result\Reason\NoLocalPart;
|
||||
|
||||
class EmailParser extends Parser
|
||||
{
|
||||
public const EMAIL_MAX_LENGTH = 254;
|
||||
const EMAIL_MAX_LENGTH = 254;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Egulias\EmailValidator;
|
||||
|
||||
use Egulias\EmailValidator\Parser;
|
||||
use Egulias\EmailValidator\Result\Result;
|
||||
use Egulias\EmailValidator\Parser\IDLeftPart;
|
||||
use Egulias\EmailValidator\Parser\IDRightPart;
|
||||
@@ -13,7 +14,7 @@ use Egulias\EmailValidator\Result\Reason\NoLocalPart;
|
||||
class MessageIDParser extends Parser
|
||||
{
|
||||
|
||||
public const EMAILID_MAX_LENGTH = 254;
|
||||
const EMAILID_MAX_LENGTH = 254;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -88,4 +89,4 @@ class MessageIDParser extends Parser
|
||||
$this->warnings[EmailTooLong::CODE] = new EmailTooLong();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,8 +59,7 @@ class Comment extends PartParser
|
||||
|
||||
if($this->openedParenthesis >= 1) {
|
||||
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
|
||||
}
|
||||
if ($this->openedParenthesis < 0) {
|
||||
} else if ($this->openedParenthesis < 0) {
|
||||
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
|
||||
}
|
||||
|
||||
@@ -101,4 +100,4 @@ class Comment extends PartParser
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,4 +15,4 @@ interface CommentStrategy
|
||||
public function endOfLoopValidations(EmailLexer $lexer) : Result;
|
||||
|
||||
public function getWarnings() : array;
|
||||
}
|
||||
}
|
||||
@@ -34,4 +34,4 @@ class DomainComment implements CommentStrategy
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,4 +34,4 @@ class LocalComment implements CommentStrategy
|
||||
{
|
||||
return $this->warnings;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,9 @@ use Egulias\EmailValidator\Warning\DomainLiteral as WarningDomainLiteral;
|
||||
|
||||
class DomainLiteral extends PartParser
|
||||
{
|
||||
public const IPV4_REGEX = '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
|
||||
const IPV4_REGEX = '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
|
||||
|
||||
public const OBSOLETE_WARNINGS = [
|
||||
const OBSOLETE_WARNINGS = [
|
||||
EmailLexer::INVALID,
|
||||
EmailLexer::C_DEL,
|
||||
EmailLexer::S_LF,
|
||||
@@ -50,7 +50,7 @@ class DomainLiteral extends PartParser
|
||||
}
|
||||
|
||||
if ($this->lexer->isNextTokenAny(
|
||||
array(EmailLexer::S_HTAB, EmailLexer::S_SP, EmailLexer::CRLF)
|
||||
array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF)
|
||||
)) {
|
||||
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
|
||||
$this->parseFWS();
|
||||
@@ -208,4 +208,4 @@ class DomainLiteral extends PartParser
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Egulias\EmailValidator\Parser;
|
||||
|
||||
use Doctrine\Common\Lexer\Token;
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Warning\TLD;
|
||||
use Egulias\EmailValidator\Result\Result;
|
||||
@@ -25,8 +24,8 @@ use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser;
|
||||
|
||||
class DomainPart extends PartParser
|
||||
{
|
||||
public const DOMAIN_MAX_LENGTH = 253;
|
||||
public const LABEL_MAX_LENGTH = 63;
|
||||
const DOMAIN_MAX_LENGTH = 253;
|
||||
const LABEL_MAX_LENGTH = 63;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -213,10 +212,7 @@ class DomainPart extends PartParser
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-param array|Token<int, string> $token
|
||||
*/
|
||||
private function checkNotAllowedChars($token) : Result
|
||||
private function checkNotAllowedChars(array $token) : Result
|
||||
{
|
||||
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
|
||||
if (isset($notAllowed[$token['type']])) {
|
||||
@@ -296,7 +292,7 @@ class DomainPart extends PartParser
|
||||
private function isLabelTooLong(string $label) : bool
|
||||
{
|
||||
if (preg_match('/[^\x00-\x7F]/', $label)) {
|
||||
idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
|
||||
idn_to_ascii(utf8_decode($label), IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
|
||||
return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
|
||||
}
|
||||
return strlen($label) > self::LABEL_MAX_LENGTH;
|
||||
@@ -313,4 +309,4 @@ class DomainPart extends PartParser
|
||||
{
|
||||
return $this->domainPart;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
namespace Egulias\EmailValidator\Parser;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Parser\Parser;
|
||||
use Egulias\EmailValidator\Result\ValidEmail;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Warning\CFWSWithFWS;
|
||||
@@ -84,4 +85,4 @@ class DoubleQuote extends PartParser
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ use Egulias\EmailValidator\Result\ValidEmail;
|
||||
|
||||
class FoldingWhiteSpace extends PartParser
|
||||
{
|
||||
public const FWS_TYPES = [
|
||||
const FWS_TYPES = [
|
||||
EmailLexer::S_SP,
|
||||
EmailLexer::S_HTAB,
|
||||
EmailLexer::S_CR,
|
||||
@@ -83,4 +83,4 @@ class FoldingWhiteSpace extends PartParser
|
||||
|
||||
return in_array($this->lexer->token['type'], self::FWS_TYPES);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Egulias\EmailValidator\Parser;
|
||||
|
||||
use Egulias\EmailValidator\Result\Result;
|
||||
use Egulias\EmailValidator\Parser\LocalPart;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\CommentsInIDRight;
|
||||
|
||||
@@ -12,4 +13,4 @@ class IDLeftPart extends LocalPart
|
||||
{
|
||||
return new InvalidEmail(new CommentsInIDRight(), $this->lexer->token['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,4 @@ class IDRightPart extends DomainPart
|
||||
}
|
||||
return new ValidEmail();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ use Egulias\EmailValidator\Parser\CommentStrategy\LocalComment;
|
||||
|
||||
class LocalPart extends PartParser
|
||||
{
|
||||
public const INVALID_TOKENS = [
|
||||
const INVALID_TOKENS = [
|
||||
EmailLexer::S_COMMA => EmailLexer::S_COMMA,
|
||||
EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET,
|
||||
EmailLexer::S_OPENBRACKET => EmailLexer::S_OPENBRACKET,
|
||||
@@ -162,4 +162,4 @@ class LocalPart extends PartParser
|
||||
|
||||
return new ValidEmail();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,4 +60,4 @@ abstract class PartParser
|
||||
&&
|
||||
$this->lexer->token['type'] !== EmailLexer::GENERIC;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use Egulias\EmailValidator\Result\Reason\Reason;
|
||||
|
||||
class InvalidEmail implements Result
|
||||
{
|
||||
private $token;
|
||||
private $token;
|
||||
/**
|
||||
* @var Reason
|
||||
*/
|
||||
@@ -43,4 +43,4 @@ class InvalidEmail implements Result
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class AtextAfterCFWS implements Reason
|
||||
{
|
||||
return 'ATEXT found after CFWS';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ namespace Egulias\EmailValidator\Result\Reason;
|
||||
|
||||
class CRLFAtTheEnd implements Reason
|
||||
{
|
||||
public const CODE = 149;
|
||||
public const REASON = "CRLF at the end";
|
||||
const CODE = 149;
|
||||
const REASON = "CRLF at the end";
|
||||
|
||||
public function code() : int
|
||||
{
|
||||
|
||||
@@ -13,4 +13,4 @@ class CharNotAllowed implements Reason
|
||||
{
|
||||
return "Character not allowed";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class CommaInDomain implements Reason
|
||||
{
|
||||
return "Comma ',' is not allowed in domain part";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class CommentsInIDRight implements Reason
|
||||
{
|
||||
return 'Comments are not allowed in IDRight for message-id';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,4 @@ abstract class DetailedReason implements Reason
|
||||
{
|
||||
$this->detailedDescription = $details;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class DomainAcceptsNoMail implements Reason
|
||||
{
|
||||
return 'Domain accepts no mail (Null MX, RFC7505)';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,4 +23,4 @@ class ExceptionFound implements Reason
|
||||
{
|
||||
return $this->exception->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class ExpectingDomainLiteralClose implements Reason
|
||||
{
|
||||
return "Closing bracket ']' for domain literal not found";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class LocalOrReservedDomain implements Reason
|
||||
{
|
||||
return 'Local, mDNS or reserved domain (RFC2606, RFC6762)';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class NoDNSRecord implements Reason
|
||||
{
|
||||
return 'No MX or A DSN record was found for this email';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ interface Reason
|
||||
* Short description of the result, human readable.
|
||||
*/
|
||||
public function description() : string;
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,4 @@ class UnOpenedComment implements Reason
|
||||
{
|
||||
return 'Missing opening comment parentheses - https://tools.ietf.org/html/rfc5322#section-3.2.2';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,4 +23,4 @@ class UnusualElements implements Reason
|
||||
{
|
||||
return 'Unusual element found, wourld render invalid in majority of cases. Element found: ' . $this->element;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,4 @@ interface Result
|
||||
* Code for user land to act upon.
|
||||
*/
|
||||
public function code() : int;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Egulias\EmailValidator\Result;
|
||||
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\SpoofEmail as ReasonSpoofEmail;
|
||||
|
||||
class SpoofEmail extends InvalidEmail
|
||||
@@ -10,4 +11,4 @@ class SpoofEmail extends InvalidEmail
|
||||
$this->reason = new ReasonSpoofEmail();
|
||||
parent::__construct($this->reason, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,4 @@ class ValidEmail implements Result
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class DNSCheckValidation implements EmailValidation
|
||||
* Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2),
|
||||
* mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G)
|
||||
*/
|
||||
public const RESERVED_DNS_TOP_LEVEL_NAMES = [
|
||||
const RESERVED_DNS_TOP_LEVEL_NAMES = [
|
||||
// Reserved Top Level DNS Names
|
||||
'test',
|
||||
'example',
|
||||
@@ -55,22 +55,11 @@ class DNSCheckValidation implements EmailValidation
|
||||
*/
|
||||
private $mxRecords = [];
|
||||
|
||||
/**
|
||||
* @var DNSGetRecordWrapper
|
||||
*/
|
||||
private $dnsGetRecord;
|
||||
|
||||
public function __construct(?DNSGetRecordWrapper $dnsGetRecord = null)
|
||||
public function __construct()
|
||||
{
|
||||
if (!function_exists('idn_to_ascii')) {
|
||||
throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
|
||||
}
|
||||
|
||||
if ($dnsGetRecord == null) {
|
||||
$dnsGetRecord = new DNSGetRecordWrapper();
|
||||
}
|
||||
|
||||
$this->dnsGetRecord = $dnsGetRecord;
|
||||
}
|
||||
|
||||
public function isValid(string $email, EmailLexer $emailLexer) : bool
|
||||
@@ -132,17 +121,27 @@ class DNSCheckValidation implements EmailValidation
|
||||
*/
|
||||
private function validateDnsRecords($host) : bool
|
||||
{
|
||||
$dnsRecordsResult = $this->dnsGetRecord->getRecords($host, static::DNS_RECORD_TYPES_TO_CHECK);
|
||||
// A workaround to fix https://bugs.php.net/bug.php?id=73149
|
||||
/** @psalm-suppress InvalidArgument */
|
||||
set_error_handler(
|
||||
static function (int $errorLevel, string $errorMessage): ?bool {
|
||||
throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
|
||||
}
|
||||
);
|
||||
|
||||
if ($dnsRecordsResult->withError()) {
|
||||
try {
|
||||
// Get all MX, A and AAAA DNS records for host
|
||||
$dnsRecords = dns_get_record($host, static::DNS_RECORD_TYPES_TO_CHECK);
|
||||
} catch (\RuntimeException $exception) {
|
||||
$this->error = new InvalidEmail(new UnableToGetDNSRecord(), '');
|
||||
|
||||
return false;
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
$dnsRecords = $dnsRecordsResult->getRecords();
|
||||
|
||||
// No MX, A or AAAA DNS records
|
||||
if ($dnsRecords === []) {
|
||||
if ($dnsRecords === [] || $dnsRecords === false) {
|
||||
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
|
||||
return false;
|
||||
}
|
||||
@@ -169,11 +168,6 @@ class DNSCheckValidation implements EmailValidation
|
||||
*/
|
||||
private function validateMxRecord($dnsRecord) : bool
|
||||
{
|
||||
if (!isset($dnsRecord['type'])) {
|
||||
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($dnsRecord['type'] !== 'MX') {
|
||||
return true;
|
||||
}
|
||||
@@ -188,4 +182,4 @@ class DNSCheckValidation implements EmailValidation
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
namespace Egulias\EmailValidator\Validation;
|
||||
|
||||
class DNSGetRecordWrapper
|
||||
{
|
||||
/**
|
||||
* @param string $host
|
||||
* @param int $type
|
||||
*/
|
||||
public function getRecords(string $host, int $type) : DNSRecords
|
||||
{
|
||||
// A workaround to fix https://bugs.php.net/bug.php?id=73149
|
||||
/** @psalm-suppress InvalidArgument */
|
||||
set_error_handler(
|
||||
static function (int $errorLevel, string $errorMessage): ?bool {
|
||||
throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
|
||||
}
|
||||
);
|
||||
try {
|
||||
// Get all MX, A and AAAA DNS records for host
|
||||
return new DNSRecords(dns_get_record($host, $type));
|
||||
} catch (\RuntimeException $exception) {
|
||||
return new DNSRecords([], true);
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Validation;
|
||||
|
||||
class DNSRecords
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array $records
|
||||
*/
|
||||
private $records = [];
|
||||
|
||||
/**
|
||||
* @var bool $error
|
||||
*/
|
||||
private $error = false;
|
||||
|
||||
public function __construct(array $records, bool $error = false)
|
||||
{
|
||||
$this->records = $records;
|
||||
$this->error = $error;
|
||||
}
|
||||
|
||||
public function getRecords() : array
|
||||
{
|
||||
return $this->records;
|
||||
}
|
||||
|
||||
public function withError() : bool
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -9,7 +9,7 @@ class EmptyValidationList extends \InvalidArgumentException
|
||||
/**
|
||||
* @param int $code
|
||||
*/
|
||||
public function __construct($code = 0, ?Exception $previous = null)
|
||||
public function __construct($code = 0, Exception $previous = null)
|
||||
{
|
||||
parent::__construct("Empty validation list is not allowed", $code, $previous);
|
||||
}
|
||||
|
||||
@@ -13,13 +13,13 @@ class MultipleValidationWithAnd implements EmailValidation
|
||||
* If one of validations fails, the remaining validations will be skipped.
|
||||
* This means MultipleErrors will only contain a single error, the first found.
|
||||
*/
|
||||
public const STOP_ON_ERROR = 0;
|
||||
const STOP_ON_ERROR = 0;
|
||||
|
||||
/**
|
||||
* All of validations will be invoked even if one of them got failure.
|
||||
* So MultipleErrors will contain all causes.
|
||||
*/
|
||||
public const ALLOW_ALL_ERRORS = 1;
|
||||
const ALLOW_ALL_ERRORS = 1;
|
||||
|
||||
/**
|
||||
* @var EmailValidation[]
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class AddressLiteral extends Warning
|
||||
{
|
||||
public const CODE = 12;
|
||||
const CODE = 12;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class CFWSNearAt extends Warning
|
||||
{
|
||||
public const CODE = 49;
|
||||
const CODE = 49;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class CFWSWithFWS extends Warning
|
||||
{
|
||||
public const CODE = 18;
|
||||
const CODE = 18;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class Comment extends Warning
|
||||
{
|
||||
public const CODE = 17;
|
||||
const CODE = 17;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class DeprecatedComment extends Warning
|
||||
{
|
||||
public const CODE = 37;
|
||||
const CODE = 37;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class DomainLiteral extends Warning
|
||||
{
|
||||
public const CODE = 70;
|
||||
const CODE = 70;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ use Egulias\EmailValidator\EmailParser;
|
||||
|
||||
class EmailTooLong extends Warning
|
||||
{
|
||||
public const CODE = 66;
|
||||
const CODE = 66;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6BadChar extends Warning
|
||||
{
|
||||
public const CODE = 74;
|
||||
const CODE = 74;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6ColonEnd extends Warning
|
||||
{
|
||||
public const CODE = 77;
|
||||
const CODE = 77;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6ColonStart extends Warning
|
||||
{
|
||||
public const CODE = 76;
|
||||
const CODE = 76;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6Deprecated extends Warning
|
||||
{
|
||||
public const CODE = 13;
|
||||
const CODE = 13;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6DoubleColon extends Warning
|
||||
{
|
||||
public const CODE = 73;
|
||||
const CODE = 73;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6GroupCount extends Warning
|
||||
{
|
||||
public const CODE = 72;
|
||||
const CODE = 72;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class IPV6MaxGroups extends Warning
|
||||
{
|
||||
public const CODE = 75;
|
||||
const CODE = 75;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,8 +4,8 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class LocalTooLong extends Warning
|
||||
{
|
||||
public const CODE = 64;
|
||||
public const LOCAL_PART_LENGTH = 64;
|
||||
const CODE = 64;
|
||||
const LOCAL_PART_LENGTH = 64;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class NoDNSMXRecord extends Warning
|
||||
{
|
||||
public const CODE = 6;
|
||||
const CODE = 6;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class ObsoleteDTEXT extends Warning
|
||||
{
|
||||
public const CODE = 71;
|
||||
const CODE = 71;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class QuotedPart extends Warning
|
||||
{
|
||||
public const CODE = 36;
|
||||
const CODE = 36;
|
||||
|
||||
/**
|
||||
* @param scalar $prevToken
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class QuotedString extends Warning
|
||||
{
|
||||
public const CODE = 11;
|
||||
const CODE = 11;
|
||||
|
||||
/**
|
||||
* @param scalar $prevToken
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
class TLD extends Warning
|
||||
{
|
||||
public const CODE = 9;
|
||||
const CODE = 9;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
|
||||
|
||||
abstract class Warning
|
||||
{
|
||||
public const CODE = 0;
|
||||
const CODE = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
|
||||
@@ -9,8 +9,6 @@ use Egulias\EmailValidator\Result\Reason\LocalOrReservedDomain;
|
||||
use Egulias\EmailValidator\Result\Reason\NoDNSRecord;
|
||||
use Egulias\EmailValidator\Result\Reason\UnableToGetDNSRecord;
|
||||
use Egulias\EmailValidator\Validation\DNSCheckValidation;
|
||||
use Egulias\EmailValidator\Validation\DNSGetRecordWrapper;
|
||||
use Egulias\EmailValidator\Validation\DNSRecords;
|
||||
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -118,35 +116,13 @@ class DNSCheckValidationTest extends TestCase
|
||||
error_reporting(\E_ALL);
|
||||
|
||||
// UnableToGetDNSRecord raises on network errors (e.g. timeout) that we can‘t emulate in tests (for sure),
|
||||
// but we can simulate with the wrapper helper
|
||||
|
||||
$wrapper = new class extends DNSGetRecordWrapper {
|
||||
public function getRecords(string $host, int $type) : DNSRecords
|
||||
{
|
||||
return new DNSRecords([], true);
|
||||
}
|
||||
// but we can try to get timeout error by trying to fetch all DNS records
|
||||
$validation = new class extends DNSCheckValidation {
|
||||
protected const DNS_RECORD_TYPES_TO_CHECK = \DNS_ALL;
|
||||
};
|
||||
|
||||
$validation = new DNSCheckValidation($wrapper);
|
||||
$expectedError = new InvalidEmail(new UnableToGetDNSRecord(), '');
|
||||
|
||||
$validation->isValid('example@invalid.example.com', new EmailLexer());
|
||||
$this->assertEquals($expectedError, $validation->getError());
|
||||
}
|
||||
|
||||
public function testMissingTypeKey()
|
||||
{
|
||||
$wrapper = new class extends DNSGetRecordWrapper {
|
||||
public function getRecords(string $host, int $type): DNSRecords
|
||||
{
|
||||
return new DNSRecords(['host' => 'test']);
|
||||
}
|
||||
};
|
||||
|
||||
$validation = new DNSCheckValidation($wrapper);
|
||||
$expectedError = new InvalidEmail(new NoDNSRecord(), '');
|
||||
|
||||
$validation->isValid('example@invalid.example.com', new EmailLexer());
|
||||
$this->assertEquals($expectedError, $validation->getError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user