first commit

This commit is contained in:
Sampanna Rimal
2024-08-27 17:48:06 +05:45
commit 53c0140f58
10839 changed files with 1125847 additions and 0 deletions

View File

@ -0,0 +1,32 @@
# EmailValidator Changelog
## New Features
* 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
* 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.
* Emails domain part are now intenteded to be RFC 1035 compliant, rendering previous valid emails (e.g example@examp&) invalid.
## PHP versions upgrade policy
PHP version upgrade requirement will happen via MINOR (3.x) version upgrades of the library, following the adoption level by major frameworks.
## Changes
* #235
* #215
* #130
* #258
* #188
* #181
* #217
* #214
* #249
* #236
* #257
* #210
## Thanks
To contributors, be it with PRs, reporting issues or supporting otherwise.

View File

@ -0,0 +1,153 @@
# Contributing
When contributing to this repository make sure to follow the Pull request process below.
Reduce to the minimum 3rd party dependencies.
Please note we have a [code of conduct](#Code of Conduct), please follow it in all your interactions with the project.
## Pull Request Process
When doing a PR to v2 remember that you also have to do the PR port to v3, or tests confirming the bug is not reproducible.
1. Supported version is v3. If you are fixing a bug in v2, please port to v3
2. Use the title as a brief description of the changes
3. Describe the changes you are proposing
1. If adding an extra validation state the benefits of adding it and the problem is solving
2. Document in the readme, by adding it to the list
4. Provide appropriate tests for the code you are submitting: aim to keep the existing coverage percentage.
5. Add your Twitter handle (if you have) so we can thank you there.
## License
By contributing, you agree that your contributions will be licensed under its MIT License.
## Code of Conduct
### Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
### Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
### Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
### Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
### Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at <emailvalidatorrfc.ccreport@gmail.com>.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
#### Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
#### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
#### 2. Warning
**Community Impact**: A violation through a single incident or series
of actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
#### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
#### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within
the community.
### Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
For answers to common questions about this code of conduct, see the FAQ at
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available
at [https://www.contributor-covenant.org/translations][translations].
[homepage]: https://www.contributor-covenant.org
[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
[translations]: https://www.contributor-covenant.org/translations

19
vendor/egulias/email-validator/LICENSE vendored Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2013-2023 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,37 @@
{
"name": "egulias/email-validator",
"description": "A library for validating emails against several RFCs",
"homepage": "https://github.com/egulias/EmailValidator",
"keywords": ["email", "validation", "validator", "emailvalidation", "emailvalidator"],
"license": "MIT",
"authors": [
{"name": "Eduardo Gulias Davis"}
],
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev"
}
},
"require": {
"php": ">=8.1",
"doctrine/lexer": "^2.0 || ^3.0",
"symfony/polyfill-intl-idn": "^1.26"
},
"require-dev": {
"phpunit/phpunit": "^10.2",
"vimeo/psalm": "^5.12"
},
"suggest": {
"ext-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation"
},
"autoload": {
"psr-4": {
"Egulias\\EmailValidator\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Egulias\\EmailValidator\\Tests\\": "tests"
}
}
}

View File

@ -0,0 +1,330 @@
<?php
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 = -1;
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;
/**
* US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
*
* @var array
*/
protected $charValue = [
'{' => self::S_OPENCURLYBRACES,
'}' => self::S_CLOSECURLYBRACES,
'(' => self::S_OPENPARENTHESIS,
')' => self::S_CLOSEPARENTHESIS,
'<' => self::S_LOWERTHAN,
'>' => self::S_GREATERTHAN,
'[' => self::S_OPENBRACKET,
']' => self::S_CLOSEBRACKET,
':' => self::S_COLON,
';' => self::S_SEMICOLON,
'@' => self::S_AT,
'\\' => self::S_BACKSLASH,
'/' => self::S_SLASH,
',' => self::S_COMMA,
'.' => self::S_DOT,
"'" => self::S_SQUOTE,
"`" => self::S_BACKTICK,
'"' => self::S_DQUOTE,
'-' => self::S_HYPHEN,
'::' => self::S_DOUBLECOLON,
' ' => self::S_SP,
"\t" => self::S_HTAB,
"\r" => self::S_CR,
"\n" => self::S_LF,
"\r\n" => self::CRLF,
'IPv6' => self::S_IPV6TAG,
'' => self::S_EMPTY,
'\0' => self::C_NUL,
'*' => self::ASTERISK,
'!' => self::EXCLAMATION,
'&' => self::AMPERSAND,
'^' => self::CARET,
'$' => self::DOLLAR,
'%' => self::PERCENTAGE,
'~' => self::S_TILDE,
'|' => self::S_PIPE,
'_' => self::S_UNDERSCORE,
'=' => self::S_EQUAL,
'+' => self::S_PLUS,
'¿' => self::INVERT_QUESTIONMARK,
'?' => self::QUESTIONMARK,
'#' => self::NUMBER_SIGN,
'¡' => self::INVERT_EXCLAMATION,
];
public const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
public const VALID_UTF8_REGEX = '/\p{Cc}+/u';
public const CATCHABLE_PATTERNS = [
'[a-zA-Z]+[46]?', //ASCII and domain literal
'[^\x00-\x7F]', //UTF-8
'[0-9]+',
'\r\n',
'::',
'\s+?',
'.',
];
public const NON_CATCHABLE_PATTERNS = [
'[\xA0-\xff]+',
];
public const MODIFIERS = 'iu';
/** @var bool */
protected $hasInvalidTokens = false;
/**
* @var Token<int, string>
*/
protected Token $previous;
/**
* The last matched/seen token.
*
* @var Token<int, string>
*/
public Token $current;
/**
* @var Token<int, string>
*/
private Token $nullToken;
/** @var string */
private $accumulator = '';
/** @var bool */
private $hasToRecord = false;
public function __construct()
{
/** @var Token<int, string> $nullToken */
$nullToken = new Token('', self::S_EMPTY, 0);
$this->nullToken = $nullToken;
$this->current = $this->previous = $this->nullToken;
$this->lookahead = null;
}
public function reset(): void
{
$this->hasInvalidTokens = false;
parent::reset();
$this->current = $this->previous = $this->nullToken;
}
/**
* @param int $type
* @throws \UnexpectedValueException
* @return boolean
*
* @psalm-suppress InvalidScalarArgument
*/
public function find($type): bool
{
$search = clone $this;
$search->skipUntil($type);
if (!$search->lookahead) {
throw new \UnexpectedValueException($type . ' not found');
}
return true;
}
/**
* moveNext
*
* @return boolean
*/
public function moveNext(): bool
{
if ($this->hasToRecord && $this->previous === $this->nullToken) {
$this->accumulator .= $this->current->value;
}
$this->previous = $this->current;
if ($this->lookahead === null) {
$this->lookahead = $this->nullToken;
}
$hasNext = parent::moveNext();
$this->current = $this->token ?? $this->nullToken;
if ($this->hasToRecord) {
$this->accumulator .= $this->current->value;
}
return $hasNext;
}
/**
* Retrieve token type. Also processes the token value if necessary.
*
* @param string $value
* @throws \InvalidArgumentException
* @return integer
*/
protected function getType(&$value): int
{
$encoded = $value;
if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') {
$encoded = mb_convert_encoding($value, 'UTF-8', 'Windows-1252');
}
if ($this->isValid($encoded)) {
return $this->charValue[$encoded];
}
if ($this->isNullType($encoded)) {
return self::C_NUL;
}
if ($this->isInvalidChar($encoded)) {
$this->hasInvalidTokens = true;
return self::INVALID;
}
return self::GENERIC;
}
protected function isValid(string $value): bool
{
return isset($this->charValue[$value]);
}
protected function isNullType(string $value): bool
{
return $value === "\0";
}
protected function isInvalidChar(string $value): bool
{
return !preg_match(self::INVALID_CHARS_REGEX, $value);
}
protected function isUTF8Invalid(string $value): bool
{
return preg_match(self::VALID_UTF8_REGEX, $value) !== false;
}
public function hasInvalidTokens(): bool
{
return $this->hasInvalidTokens;
}
/**
* getPrevious
*
* @return Token<int, string>
*/
public function getPrevious(): Token
{
return $this->previous;
}
/**
* Lexical catchable patterns.
*
* @return string[]
*/
protected function getCatchablePatterns(): array
{
return self::CATCHABLE_PATTERNS;
}
/**
* Lexical non-catchable patterns.
*
* @return string[]
*/
protected function getNonCatchablePatterns(): array
{
return self::NON_CATCHABLE_PATTERNS;
}
protected function getModifiers(): string
{
return self::MODIFIERS;
}
public function getAccumulatedValues(): string
{
return $this->accumulator;
}
public function startRecording(): void
{
$this->hasToRecord = true;
}
public function stopRecording(): void
{
$this->hasToRecord = false;
}
public function clearRecorded(): void
{
$this->accumulator = '';
}
}

View File

@ -0,0 +1,90 @@
<?php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Parser\LocalPart;
use Egulias\EmailValidator\Parser\DomainPart;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\EmailTooLong;
use Egulias\EmailValidator\Result\Reason\NoLocalPart;
class EmailParser extends Parser
{
public const EMAIL_MAX_LENGTH = 254;
/**
* @var string
*/
protected $domainPart = '';
/**
* @var string
*/
protected $localPart = '';
public function parse(string $str): Result
{
$result = parent::parse($str);
$this->addLongEmailWarning($this->localPart, $this->domainPart);
return $result;
}
protected function preLeftParsing(): Result
{
if (!$this->hasAtToken()) {
return new InvalidEmail(new NoLocalPart(), $this->lexer->current->value);
}
return new ValidEmail();
}
protected function parseLeftFromAt(): Result
{
return $this->processLocalPart();
}
protected function parseRightFromAt(): Result
{
return $this->processDomainPart();
}
private function processLocalPart(): Result
{
$localPartParser = new LocalPart($this->lexer);
$localPartResult = $localPartParser->parse();
$this->localPart = $localPartParser->localPart();
$this->warnings = [...$localPartParser->getWarnings(), ...$this->warnings];
return $localPartResult;
}
private function processDomainPart(): Result
{
$domainPartParser = new DomainPart($this->lexer);
$domainPartResult = $domainPartParser->parse();
$this->domainPart = $domainPartParser->domainPart();
$this->warnings = [...$domainPartParser->getWarnings(), ...$this->warnings];
return $domainPartResult;
}
public function getDomainPart(): string
{
return $this->domainPart;
}
public function getLocalPart(): string
{
return $this->localPart;
}
private function addLongEmailWarning(string $localPart, string $parsedDomainPart): void
{
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
$this->warnings[EmailTooLong::CODE] = new EmailTooLong();
}
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\EmailValidation;
class EmailValidator
{
/**
* @var EmailLexer
*/
private $lexer;
/**
* @var Warning\Warning[]
*/
private $warnings = [];
/**
* @var ?InvalidEmail
*/
private $error;
public function __construct()
{
$this->lexer = new EmailLexer();
}
/**
* @param string $email
* @param EmailValidation $emailValidation
* @return bool
*/
public function isValid(string $email, EmailValidation $emailValidation)
{
$isValid = $emailValidation->isValid($email, $this->lexer);
$this->warnings = $emailValidation->getWarnings();
$this->error = $emailValidation->getError();
return $isValid;
}
/**
* @return boolean
*/
public function hasWarnings()
{
return !empty($this->warnings);
}
/**
* @return array
*/
public function getWarnings()
{
return $this->warnings;
}
/**
* @return InvalidEmail|null
*/
public function getError()
{
return $this->error;
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Parser\IDLeftPart;
use Egulias\EmailValidator\Parser\IDRightPart;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\EmailTooLong;
use Egulias\EmailValidator\Result\Reason\NoLocalPart;
class MessageIDParser extends Parser
{
public const EMAILID_MAX_LENGTH = 254;
/**
* @var string
*/
protected $idLeft = '';
/**
* @var string
*/
protected $idRight = '';
public function parse(string $str): Result
{
$result = parent::parse($str);
$this->addLongEmailWarning($this->idLeft, $this->idRight);
return $result;
}
protected function preLeftParsing(): Result
{
if (!$this->hasAtToken()) {
return new InvalidEmail(new NoLocalPart(), $this->lexer->current->value);
}
return new ValidEmail();
}
protected function parseLeftFromAt(): Result
{
return $this->processIDLeft();
}
protected function parseRightFromAt(): Result
{
return $this->processIDRight();
}
private function processIDLeft(): Result
{
$localPartParser = new IDLeftPart($this->lexer);
$localPartResult = $localPartParser->parse();
$this->idLeft = $localPartParser->localPart();
$this->warnings = [...$localPartParser->getWarnings(), ...$this->warnings];
return $localPartResult;
}
private function processIDRight(): Result
{
$domainPartParser = new IDRightPart($this->lexer);
$domainPartResult = $domainPartParser->parse();
$this->idRight = $domainPartParser->domainPart();
$this->warnings = [...$domainPartParser->getWarnings(), ...$this->warnings];
return $domainPartResult;
}
public function getLeftPart(): string
{
return $this->idLeft;
}
public function getRightPart(): string
{
return $this->idRight;
}
private function addLongEmailWarning(string $localPart, string $parsedDomainPart): void
{
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
$this->warnings[EmailTooLong::CODE] = new EmailTooLong();
}
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
abstract class Parser
{
/**
* @var Warning\Warning[]
*/
protected $warnings = [];
/**
* @var EmailLexer
*/
protected $lexer;
/**
* id-left "@" id-right
*/
abstract protected function parseRightFromAt(): Result;
abstract protected function parseLeftFromAt(): Result;
abstract protected function preLeftParsing(): Result;
public function __construct(EmailLexer $lexer)
{
$this->lexer = $lexer;
}
public function parse(string $str): Result
{
$this->lexer->setInput($str);
if ($this->lexer->hasInvalidTokens()) {
return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->current->value);
}
$preParsingResult = $this->preLeftParsing();
if ($preParsingResult->isInvalid()) {
return $preParsingResult;
}
$localPartResult = $this->parseLeftFromAt();
if ($localPartResult->isInvalid()) {
return $localPartResult;
}
$domainPartResult = $this->parseRightFromAt();
if ($domainPartResult->isInvalid()) {
return $domainPartResult;
}
return new ValidEmail();
}
/**
* @return Warning\Warning[]
*/
public function getWarnings(): array
{
return $this->warnings;
}
protected function hasAtToken(): bool
{
$this->lexer->moveNext();
$this->lexer->moveNext();
return !$this->lexer->current->isA(EmailLexer::S_AT);
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Warning\QuotedPart;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Parser\CommentStrategy\CommentStrategy;
use Egulias\EmailValidator\Result\Reason\UnclosedComment;
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
use Egulias\EmailValidator\Warning\Comment as WarningComment;
class Comment extends PartParser
{
/**
* @var int
*/
private $openedParenthesis = 0;
/**
* @var CommentStrategy
*/
private $commentStrategy;
public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy)
{
$this->lexer = $lexer;
$this->commentStrategy = $commentStrategy;
}
public function parse(): Result
{
if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) {
$this->openedParenthesis++;
if ($this->noClosingParenthesis()) {
return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value);
}
}
if ($this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS)) {
return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value);
}
$this->warnings[WarningComment::CODE] = new WarningComment();
$moreTokens = true;
while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens) {
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
$this->openedParenthesis++;
}
$this->warnEscaping();
if ($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
$this->openedParenthesis--;
}
$moreTokens = $this->lexer->moveNext();
}
if ($this->openedParenthesis >= 1) {
return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value);
}
if ($this->openedParenthesis < 0) {
return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value);
}
$finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer);
$this->warnings = [...$this->warnings, ...$this->commentStrategy->getWarnings()];
return $finalValidations;
}
/**
* @return void
*/
private function warnEscaping(): void
{
//Backslash found
if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
return;
}
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return;
}
$this->warnings[QuotedPart::CODE] =
new QuotedPart($this->lexer->getPrevious()->type, $this->lexer->current->type);
}
private function noClosingParenthesis(): bool
{
try {
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
return false;
} catch (\RuntimeException $e) {
return true;
}
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Egulias\EmailValidator\Parser\CommentStrategy;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Warning\Warning;
interface CommentStrategy
{
/**
* Return "true" to continue, "false" to exit
*/
public function exitCondition(EmailLexer $lexer, int $openedParenthesis): bool;
public function endOfLoopValidations(EmailLexer $lexer): Result;
/**
* @return Warning[]
*/
public function getWarnings(): array;
}

View File

@ -0,0 +1,33 @@
<?php
namespace Egulias\EmailValidator\Parser\CommentStrategy;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
class DomainComment implements CommentStrategy
{
public function exitCondition(EmailLexer $lexer, int $openedParenthesis): bool
{
return !($openedParenthesis === 0 && $lexer->isNextToken(EmailLexer::S_DOT));
}
public function endOfLoopValidations(EmailLexer $lexer): Result
{
//test for end of string
if (!$lexer->isNextToken(EmailLexer::S_DOT)) {
return new InvalidEmail(new ExpectingATEXT('DOT not found near CLOSEPARENTHESIS'), $lexer->current->value);
}
//add warning
//Address is valid within the message but cannot be used unmodified for the envelope
return new ValidEmail();
}
public function getWarnings(): array
{
return [];
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace Egulias\EmailValidator\Parser\CommentStrategy;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Warning\CFWSNearAt;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
class LocalComment implements CommentStrategy
{
/**
* @var array
*/
private $warnings = [];
public function exitCondition(EmailLexer $lexer, int $openedParenthesis): bool
{
return !$lexer->isNextToken(EmailLexer::S_AT);
}
public function endOfLoopValidations(EmailLexer $lexer): Result
{
if (!$lexer->isNextToken(EmailLexer::S_AT)) {
return new InvalidEmail(new ExpectingATEXT('ATEX is not expected after closing comments'), $lexer->current->value);
}
$this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
return new ValidEmail();
}
public function getWarnings(): array
{
return $this->warnings;
}
}

View File

@ -0,0 +1,210 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\CFWSWithFWS;
use Egulias\EmailValidator\Warning\IPV6BadChar;
use Egulias\EmailValidator\Result\Reason\CRNoLF;
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
use Egulias\EmailValidator\Warning\IPV6MaxGroups;
use Egulias\EmailValidator\Warning\ObsoleteDTEXT;
use Egulias\EmailValidator\Warning\AddressLiteral;
use Egulias\EmailValidator\Warning\IPV6ColonStart;
use Egulias\EmailValidator\Warning\IPV6Deprecated;
use Egulias\EmailValidator\Warning\IPV6GroupCount;
use Egulias\EmailValidator\Warning\IPV6DoubleColon;
use Egulias\EmailValidator\Result\Reason\ExpectingDTEXT;
use Egulias\EmailValidator\Result\Reason\UnusualElements;
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]?)$/';
public const OBSOLETE_WARNINGS = [
EmailLexer::INVALID,
EmailLexer::C_DEL,
EmailLexer::S_LF,
EmailLexer::S_BACKSLASH
];
public function parse(): Result
{
$this->addTagWarnings();
$IPv6TAG = false;
$addressLiteral = '';
do {
if ($this->lexer->current->isA(EmailLexer::C_NUL)) {
return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->current->value);
}
$this->addObsoleteWarnings();
if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENBRACKET, EmailLexer::S_OPENBRACKET))) {
return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->current->value);
}
if ($this->lexer->isNextTokenAny(
array(EmailLexer::S_HTAB, EmailLexer::S_SP, EmailLexer::CRLF)
)) {
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
$this->parseFWS();
}
if ($this->lexer->isNextToken(EmailLexer::S_CR)) {
return new InvalidEmail(new CRNoLF(), $this->lexer->current->value);
}
if ($this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
return new InvalidEmail(new UnusualElements($this->lexer->current->value), $this->lexer->current->value);
}
if ($this->lexer->current->isA(EmailLexer::S_IPV6TAG)) {
$IPv6TAG = true;
}
if ($this->lexer->current->isA(EmailLexer::S_CLOSEBRACKET)) {
break;
}
$addressLiteral .= $this->lexer->current->value;
} while ($this->lexer->moveNext());
//Encapsulate
$addressLiteral = str_replace('[', '', $addressLiteral);
$isAddressLiteralIPv4 = $this->checkIPV4Tag($addressLiteral);
if (!$isAddressLiteralIPv4) {
return new ValidEmail();
}
$addressLiteral = $this->convertIPv4ToIPv6($addressLiteral);
if (!$IPv6TAG) {
$this->warnings[WarningDomainLiteral::CODE] = new WarningDomainLiteral();
return new ValidEmail();
}
$this->warnings[AddressLiteral::CODE] = new AddressLiteral();
$this->checkIPV6Tag($addressLiteral);
return new ValidEmail();
}
/**
* @param string $addressLiteral
* @param int $maxGroups
*/
public function checkIPV6Tag($addressLiteral, $maxGroups = 8): void
{
$prev = $this->lexer->getPrevious();
if ($prev->isA(EmailLexer::S_COLON)) {
$this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd();
}
$IPv6 = substr($addressLiteral, 5);
//Daniel Marschall's new IPv6 testing strategy
$matchesIP = explode(':', $IPv6);
$groupCount = count($matchesIP);
$colons = strpos($IPv6, '::');
if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) {
$this->warnings[IPV6BadChar::CODE] = new IPV6BadChar();
}
if ($colons === false) {
// We need exactly the right number of groups
if ($groupCount !== $maxGroups) {
$this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount();
}
return;
}
if ($colons !== strrpos($IPv6, '::')) {
$this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon();
return;
}
if ($colons === 0 || $colons === (strlen($IPv6) - 2)) {
// RFC 4291 allows :: at the start or end of an address
//with 7 other groups in addition
++$maxGroups;
}
if ($groupCount > $maxGroups) {
$this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups();
} elseif ($groupCount === $maxGroups) {
$this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated();
}
}
public function convertIPv4ToIPv6(string $addressLiteralIPv4): string
{
$matchesIP = [];
$IPv4Match = preg_match(self::IPV4_REGEX, $addressLiteralIPv4, $matchesIP);
// Extract IPv4 part from the end of the address-literal (if there is one)
if ($IPv4Match > 0) {
$index = (int) strrpos($addressLiteralIPv4, $matchesIP[0]);
//There's a match but it is at the start
if ($index > 0) {
// Convert IPv4 part to IPv6 format for further testing
return substr($addressLiteralIPv4, 0, $index) . '0:0';
}
}
return $addressLiteralIPv4;
}
/**
* @param string $addressLiteral
*
* @return bool
*/
protected function checkIPV4Tag($addressLiteral): bool
{
$matchesIP = [];
$IPv4Match = preg_match(self::IPV4_REGEX, $addressLiteral, $matchesIP);
// Extract IPv4 part from the end of the address-literal (if there is one)
if ($IPv4Match > 0) {
$index = strrpos($addressLiteral, $matchesIP[0]);
//There's a match but it is at the start
if ($index === 0) {
$this->warnings[AddressLiteral::CODE] = new AddressLiteral();
return false;
}
}
return true;
}
private function addObsoleteWarnings(): void
{
if (in_array($this->lexer->current->type, self::OBSOLETE_WARNINGS)) {
$this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT();
}
}
private function addTagWarnings(): void
{
if ($this->lexer->isNextToken(EmailLexer::S_COLON)) {
$this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart();
}
if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) {
$lexer = clone $this->lexer;
$lexer->moveNext();
if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) {
$this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart();
}
}
}
}

View File

@ -0,0 +1,326 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Doctrine\Common\Lexer\Token;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Warning\TLD;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
use Egulias\EmailValidator\Result\Reason\DotAtStart;
use Egulias\EmailValidator\Warning\DeprecatedComment;
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd;
use Egulias\EmailValidator\Result\Reason\LabelTooLong;
use Egulias\EmailValidator\Result\Reason\NoDomainPart;
use Egulias\EmailValidator\Result\Reason\ConsecutiveAt;
use Egulias\EmailValidator\Result\Reason\DomainTooLong;
use Egulias\EmailValidator\Result\Reason\CharNotAllowed;
use Egulias\EmailValidator\Result\Reason\DomainHyphened;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
use Egulias\EmailValidator\Parser\CommentStrategy\DomainComment;
use Egulias\EmailValidator\Result\Reason\ExpectingDomainLiteralClose;
use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser;
class DomainPart extends PartParser
{
public const DOMAIN_MAX_LENGTH = 253;
public const LABEL_MAX_LENGTH = 63;
/**
* @var string
*/
protected $domainPart = '';
/**
* @var string
*/
protected $label = '';
public function parse(): Result
{
$this->lexer->clearRecorded();
$this->lexer->startRecording();
$this->lexer->moveNext();
$domainChecks = $this->performDomainStartChecks();
if ($domainChecks->isInvalid()) {
return $domainChecks;
}
if ($this->lexer->current->isA(EmailLexer::S_AT)) {
return new InvalidEmail(new ConsecutiveAt(), $this->lexer->current->value);
}
$result = $this->doParseDomainPart();
if ($result->isInvalid()) {
return $result;
}
$end = $this->checkEndOfDomain();
if ($end->isInvalid()) {
return $end;
}
$this->lexer->stopRecording();
$this->domainPart = $this->lexer->getAccumulatedValues();
$length = strlen($this->domainPart);
if ($length > self::DOMAIN_MAX_LENGTH) {
return new InvalidEmail(new DomainTooLong(), $this->lexer->current->value);
}
return new ValidEmail();
}
private function checkEndOfDomain(): Result
{
$prev = $this->lexer->getPrevious();
if ($prev->isA(EmailLexer::S_DOT)) {
return new InvalidEmail(new DotAtEnd(), $this->lexer->current->value);
}
if ($prev->isA(EmailLexer::S_HYPHEN)) {
return new InvalidEmail(new DomainHyphened('Hypen found at the end of the domain'), $prev->value);
}
if ($this->lexer->current->isA(EmailLexer::S_SP)) {
return new InvalidEmail(new CRLFAtTheEnd(), $prev->value);
}
return new ValidEmail();
}
private function performDomainStartChecks(): Result
{
$invalidTokens = $this->checkInvalidTokensAfterAT();
if ($invalidTokens->isInvalid()) {
return $invalidTokens;
}
$missingDomain = $this->checkEmptyDomain();
if ($missingDomain->isInvalid()) {
return $missingDomain;
}
if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) {
$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
}
return new ValidEmail();
}
private function checkEmptyDomain(): Result
{
$thereIsNoDomain = $this->lexer->current->isA(EmailLexer::S_EMPTY) ||
($this->lexer->current->isA(EmailLexer::S_SP) &&
!$this->lexer->isNextToken(EmailLexer::GENERIC));
if ($thereIsNoDomain) {
return new InvalidEmail(new NoDomainPart(), $this->lexer->current->value);
}
return new ValidEmail();
}
private function checkInvalidTokensAfterAT(): Result
{
if ($this->lexer->current->isA(EmailLexer::S_DOT)) {
return new InvalidEmail(new DotAtStart(), $this->lexer->current->value);
}
if ($this->lexer->current->isA(EmailLexer::S_HYPHEN)) {
return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->current->value);
}
return new ValidEmail();
}
protected function parseComments(): Result
{
$commentParser = new Comment($this->lexer, new DomainComment());
$result = $commentParser->parse();
$this->warnings = [...$this->warnings, ...$commentParser->getWarnings()];
return $result;
}
protected function doParseDomainPart(): Result
{
$tldMissing = true;
$hasComments = false;
$domain = '';
do {
$prev = $this->lexer->getPrevious();
$notAllowedChars = $this->checkNotAllowedChars($this->lexer->current);
if ($notAllowedChars->isInvalid()) {
return $notAllowedChars;
}
if (
$this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS) ||
$this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS)
) {
$hasComments = true;
$commentsResult = $this->parseComments();
//Invalid comment parsing
if ($commentsResult->isInvalid()) {
return $commentsResult;
}
}
$dotsResult = $this->checkConsecutiveDots();
if ($dotsResult->isInvalid()) {
return $dotsResult;
}
if ($this->lexer->current->isA(EmailLexer::S_OPENBRACKET)) {
$literalResult = $this->parseDomainLiteral();
$this->addTLDWarnings($tldMissing);
return $literalResult;
}
$labelCheck = $this->checkLabelLength();
if ($labelCheck->isInvalid()) {
return $labelCheck;
}
$FwsResult = $this->parseFWS();
if ($FwsResult->isInvalid()) {
return $FwsResult;
}
$domain .= $this->lexer->current->value;
if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::GENERIC)) {
$tldMissing = false;
}
$exceptionsResult = $this->checkDomainPartExceptions($prev, $hasComments);
if ($exceptionsResult->isInvalid()) {
return $exceptionsResult;
}
$this->lexer->moveNext();
} while (!$this->lexer->current->isA(EmailLexer::S_EMPTY));
$labelCheck = $this->checkLabelLength(true);
if ($labelCheck->isInvalid()) {
return $labelCheck;
}
$this->addTLDWarnings($tldMissing);
$this->domainPart = $domain;
return new ValidEmail();
}
/**
* @param Token<int, string> $token
*
* @return Result
*/
private function checkNotAllowedChars(Token $token): Result
{
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH => true];
if (isset($notAllowed[$token->type])) {
return new InvalidEmail(new CharNotAllowed(), $token->value);
}
return new ValidEmail();
}
/**
* @return Result
*/
protected function parseDomainLiteral(): Result
{
try {
$this->lexer->find(EmailLexer::S_CLOSEBRACKET);
} catch (\RuntimeException $e) {
return new InvalidEmail(new ExpectingDomainLiteralClose(), $this->lexer->current->value);
}
$domainLiteralParser = new DomainLiteralParser($this->lexer);
$result = $domainLiteralParser->parse();
$this->warnings = [...$this->warnings, ...$domainLiteralParser->getWarnings()];
return $result;
}
/**
* @param Token<int, string> $prev
* @param bool $hasComments
*
* @return Result
*/
protected function checkDomainPartExceptions(Token $prev, bool $hasComments): Result
{
if ($this->lexer->current->isA(EmailLexer::S_OPENBRACKET) && $prev->type !== EmailLexer::S_AT) {
return new InvalidEmail(new ExpectingATEXT('OPENBRACKET not after AT'), $this->lexer->current->value);
}
if ($this->lexer->current->isA(EmailLexer::S_HYPHEN) && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
return new InvalidEmail(new DomainHyphened('Hypen found near DOT'), $this->lexer->current->value);
}
if (
$this->lexer->current->isA(EmailLexer::S_BACKSLASH)
&& $this->lexer->isNextToken(EmailLexer::GENERIC)
) {
return new InvalidEmail(new ExpectingATEXT('Escaping following "ATOM"'), $this->lexer->current->value);
}
return $this->validateTokens($hasComments);
}
protected function validateTokens(bool $hasComments): Result
{
$validDomainTokens = array(
EmailLexer::GENERIC => true,
EmailLexer::S_HYPHEN => true,
EmailLexer::S_DOT => true,
);
if ($hasComments) {
$validDomainTokens[EmailLexer::S_OPENPARENTHESIS] = true;
$validDomainTokens[EmailLexer::S_CLOSEPARENTHESIS] = true;
}
if (!isset($validDomainTokens[$this->lexer->current->type])) {
return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->current->value), $this->lexer->current->value);
}
return new ValidEmail();
}
private function checkLabelLength(bool $isEndOfDomain = false): Result
{
if ($this->lexer->current->isA(EmailLexer::S_DOT) || $isEndOfDomain) {
if ($this->isLabelTooLong($this->label)) {
return new InvalidEmail(new LabelTooLong(), $this->lexer->current->value);
}
$this->label = '';
}
$this->label .= $this->lexer->current->value;
return new ValidEmail();
}
private function isLabelTooLong(string $label): bool
{
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;
}
private function addTLDWarnings(bool $isTLDMissing): void
{
if ($isTLDMissing) {
$this->warnings[TLD::CODE] = new TLD();
}
}
public function domainPart(): string
{
return $this->domainPart;
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\CFWSWithFWS;
use Egulias\EmailValidator\Warning\QuotedString;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
use Egulias\EmailValidator\Result\Reason\UnclosedQuotedString;
use Egulias\EmailValidator\Result\Result;
class DoubleQuote extends PartParser
{
public function parse(): Result
{
$validQuotedString = $this->checkDQUOTE();
if ($validQuotedString->isInvalid()) {
return $validQuotedString;
}
$special = [
EmailLexer::S_CR => true,
EmailLexer::S_HTAB => true,
EmailLexer::S_LF => true
];
$invalid = [
EmailLexer::C_NUL => true,
EmailLexer::S_HTAB => true,
EmailLexer::S_CR => true,
EmailLexer::S_LF => true
];
$setSpecialsWarning = true;
$this->lexer->moveNext();
while (!$this->lexer->current->isA(EmailLexer::S_DQUOTE) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) {
if (isset($special[$this->lexer->current->type]) && $setSpecialsWarning) {
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
$setSpecialsWarning = false;
}
if ($this->lexer->current->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
$this->lexer->moveNext();
}
$this->lexer->moveNext();
if (!$this->escaped() && isset($invalid[$this->lexer->current->type])) {
return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value);
}
}
$prev = $this->lexer->getPrevious();
if ($prev->isA(EmailLexer::S_BACKSLASH)) {
$validQuotedString = $this->checkDQUOTE();
if ($validQuotedString->isInvalid()) {
return $validQuotedString;
}
}
if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) {
return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value);
}
return new ValidEmail();
}
protected function checkDQUOTE(): Result
{
$previous = $this->lexer->getPrevious();
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous->isA(EmailLexer::GENERIC)) {
$description = 'https://tools.ietf.org/html/rfc5322#section-3.2.4 - quoted string should be a unit';
return new InvalidEmail(new ExpectingATEXT($description), $this->lexer->current->value);
}
try {
$this->lexer->find(EmailLexer::S_DQUOTE);
} catch (\Exception $e) {
return new InvalidEmail(new UnclosedQuotedString(), $this->lexer->current->value);
}
$this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->current->value);
return new ValidEmail();
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Warning\CFWSNearAt;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\CFWSWithFWS;
use Egulias\EmailValidator\Result\Reason\CRNoLF;
use Egulias\EmailValidator\Result\Reason\AtextAfterCFWS;
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd;
use Egulias\EmailValidator\Result\Reason\CRLFX2;
use Egulias\EmailValidator\Result\Reason\ExpectingCTEXT;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
class FoldingWhiteSpace extends PartParser
{
public const FWS_TYPES = [
EmailLexer::S_SP,
EmailLexer::S_HTAB,
EmailLexer::S_CR,
EmailLexer::S_LF,
EmailLexer::CRLF
];
public function parse(): Result
{
if (!$this->isFWS()) {
return new ValidEmail();
}
$previous = $this->lexer->getPrevious();
$resultCRLF = $this->checkCRLFInFWS();
if ($resultCRLF->isInvalid()) {
return $resultCRLF;
}
if ($this->lexer->current->isA(EmailLexer::S_CR)) {
return new InvalidEmail(new CRNoLF(), $this->lexer->current->value);
}
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && !$previous->isA(EmailLexer::S_AT)) {
return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->current->value);
}
if ($this->lexer->current->isA(EmailLexer::S_LF) || $this->lexer->current->isA(EmailLexer::C_NUL)) {
return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->current->value);
}
if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous->isA(EmailLexer::S_AT)) {
$this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
} else {
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
}
return new ValidEmail();
}
protected function checkCRLFInFWS(): Result
{
if (!$this->lexer->current->isA(EmailLexer::CRLF)) {
return new ValidEmail();
}
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
return new InvalidEmail(new CRLFX2(), $this->lexer->current->value);
}
//this has no coverage. Condition is repeated from above one
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->current->value);
}
return new ValidEmail();
}
protected function isFWS(): bool
{
if ($this->escaped()) {
return false;
}
return in_array($this->lexer->current->type, self::FWS_TYPES);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\CommentsInIDRight;
class IDLeftPart extends LocalPart
{
protected function parseComments(): Result
{
return new InvalidEmail(new CommentsInIDRight(), $this->lexer->current->value);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
class IDRightPart extends DomainPart
{
protected function validateTokens(bool $hasComments): Result
{
$invalidDomainTokens = [
EmailLexer::S_DQUOTE => true,
EmailLexer::S_SQUOTE => true,
EmailLexer::S_BACKTICK => true,
EmailLexer::S_SEMICOLON => true,
EmailLexer::S_GREATERTHAN => true,
EmailLexer::S_LOWERTHAN => true,
];
if (isset($invalidDomainTokens[$this->lexer->current->type])) {
return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->current->value), $this->lexer->current->value);
}
return new ValidEmail();
}
}

View File

@ -0,0 +1,162 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\LocalTooLong;
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
use Egulias\EmailValidator\Result\Reason\DotAtStart;
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
use Egulias\EmailValidator\Parser\CommentStrategy\LocalComment;
class LocalPart extends PartParser
{
public const INVALID_TOKENS = [
EmailLexer::S_COMMA => EmailLexer::S_COMMA,
EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET,
EmailLexer::S_OPENBRACKET => EmailLexer::S_OPENBRACKET,
EmailLexer::S_GREATERTHAN => EmailLexer::S_GREATERTHAN,
EmailLexer::S_LOWERTHAN => EmailLexer::S_LOWERTHAN,
EmailLexer::S_COLON => EmailLexer::S_COLON,
EmailLexer::S_SEMICOLON => EmailLexer::S_SEMICOLON,
EmailLexer::INVALID => EmailLexer::INVALID
];
/**
* @var string
*/
private $localPart = '';
public function parse(): Result
{
$this->lexer->startRecording();
while (!$this->lexer->current->isA(EmailLexer::S_AT) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) {
if ($this->hasDotAtStart()) {
return new InvalidEmail(new DotAtStart(), $this->lexer->current->value);
}
if ($this->lexer->current->isA(EmailLexer::S_DQUOTE)) {
$dquoteParsingResult = $this->parseDoubleQuote();
//Invalid double quote parsing
if ($dquoteParsingResult->isInvalid()) {
return $dquoteParsingResult;
}
}
if (
$this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS) ||
$this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS)
) {
$commentsResult = $this->parseComments();
//Invalid comment parsing
if ($commentsResult->isInvalid()) {
return $commentsResult;
}
}
if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
return new InvalidEmail(new ConsecutiveDot(), $this->lexer->current->value);
}
if (
$this->lexer->current->isA(EmailLexer::S_DOT) &&
$this->lexer->isNextToken(EmailLexer::S_AT)
) {
return new InvalidEmail(new DotAtEnd(), $this->lexer->current->value);
}
$resultEscaping = $this->validateEscaping();
if ($resultEscaping->isInvalid()) {
return $resultEscaping;
}
$resultToken = $this->validateTokens(false);
if ($resultToken->isInvalid()) {
return $resultToken;
}
$resultFWS = $this->parseLocalFWS();
if ($resultFWS->isInvalid()) {
return $resultFWS;
}
$this->lexer->moveNext();
}
$this->lexer->stopRecording();
$this->localPart = rtrim($this->lexer->getAccumulatedValues(), '@');
if (strlen($this->localPart) > LocalTooLong::LOCAL_PART_LENGTH) {
$this->warnings[LocalTooLong::CODE] = new LocalTooLong();
}
return new ValidEmail();
}
protected function validateTokens(bool $hasComments): Result
{
if (isset(self::INVALID_TOKENS[$this->lexer->current->type])) {
return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->current->value);
}
return new ValidEmail();
}
public function localPart(): string
{
return $this->localPart;
}
private function parseLocalFWS(): Result
{
$foldingWS = new FoldingWhiteSpace($this->lexer);
$resultFWS = $foldingWS->parse();
if ($resultFWS->isValid()) {
$this->warnings = [...$this->warnings, ...$foldingWS->getWarnings()];
}
return $resultFWS;
}
private function hasDotAtStart(): bool
{
return $this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->getPrevious()->isA(EmailLexer::S_EMPTY);
}
private function parseDoubleQuote(): Result
{
$dquoteParser = new DoubleQuote($this->lexer);
$parseAgain = $dquoteParser->parse();
$this->warnings = [...$this->warnings, ...$dquoteParser->getWarnings()];
return $parseAgain;
}
protected function parseComments(): Result
{
$commentParser = new Comment($this->lexer, new LocalComment());
$result = $commentParser->parse();
$this->warnings = [...$this->warnings, ...$commentParser->getWarnings()];
return $result;
}
private function validateEscaping(): Result
{
//Backslash found
if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
return new ValidEmail();
}
if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->current->value);
}
return new ValidEmail();
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Warning\Warning;
abstract class PartParser
{
/**
* @var Warning[]
*/
protected $warnings = [];
/**
* @var EmailLexer
*/
protected $lexer;
public function __construct(EmailLexer $lexer)
{
$this->lexer = $lexer;
}
abstract public function parse(): Result;
/**
* @return Warning[]
*/
public function getWarnings()
{
return $this->warnings;
}
protected function parseFWS(): Result
{
$foldingWS = new FoldingWhiteSpace($this->lexer);
$resultFWS = $foldingWS->parse();
$this->warnings = [...$this->warnings, ...$foldingWS->getWarnings()];
return $resultFWS;
}
protected function checkConsecutiveDots(): Result
{
if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
return new InvalidEmail(new ConsecutiveDot(), $this->lexer->current->value);
}
return new ValidEmail();
}
protected function escaped(): bool
{
$previous = $this->lexer->getPrevious();
return $previous->isA(EmailLexer::S_BACKSLASH)
&& !$this->lexer->current->isA(EmailLexer::GENERIC);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Egulias\EmailValidator\Result;
use Egulias\EmailValidator\Result\Reason\Reason;
class InvalidEmail implements Result
{
/**
* @var string
*/
private string $token;
/**
* @var Reason
*/
protected Reason $reason;
public function __construct(Reason $reason, string $token)
{
$this->token = $token;
$this->reason = $reason;
}
public function isValid(): bool
{
return false;
}
public function isInvalid(): bool
{
return true;
}
public function description(): string
{
return $this->reason->description() . " in char " . $this->token;
}
public function code(): int
{
return $this->reason->code();
}
public function reason(): Reason
{
return $this->reason;
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Egulias\EmailValidator\Result;
use Egulias\EmailValidator\Result\Reason\EmptyReason;
use Egulias\EmailValidator\Result\Reason\Reason;
/**
* @psalm-suppress PropertyNotSetInConstructor
*/
class MultipleErrors extends InvalidEmail
{
/**
* @var Reason[]
*/
private $reasons = [];
public function __construct()
{
}
public function addReason(Reason $reason) : void
{
$this->reasons[$reason->code()] = $reason;
}
/**
* @return Reason[]
*/
public function getReasons() : array
{
return $this->reasons;
}
public function reason() : Reason
{
return 0 !== count($this->reasons)
? current($this->reasons)
: new EmptyReason();
}
public function description() : string
{
$description = '';
foreach($this->reasons as $reason) {
$description .= $reason->description() . PHP_EOL;
}
return $description;
}
public function code() : int
{
return 0;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class AtextAfterCFWS implements Reason
{
public function code() : int
{
return 133;
}
public function description() : string
{
return 'ATEXT found after CFWS';
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class CRLFAtTheEnd implements Reason
{
public const CODE = 149;
public const REASON = "CRLF at the end";
public function code() : int
{
return 149;
}
public function description() : string
{
return 'CRLF at the end';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class CRLFX2 implements Reason
{
public function code() : int
{
return 148;
}
public function description() : string
{
return 'CR LF tokens found twice';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class CRNoLF implements Reason
{
public function code() : int
{
return 150;
}
public function description() : string
{
return 'Missing LF after CR';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class CharNotAllowed implements Reason
{
public function code() : int
{
return 1;
}
public function description() : string
{
return "Character not allowed";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class CommaInDomain implements Reason
{
public function code() : int
{
return 200;
}
public function description() : string
{
return "Comma ',' is not allowed in domain part";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class CommentsInIDRight implements Reason
{
public function code() : int
{
return 400;
}
public function description() : string
{
return 'Comments are not allowed in IDRight for message-id';
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ConsecutiveAt implements Reason
{
public function code() : int
{
return 128;
}
public function description() : string
{
return '@ found after another @';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ConsecutiveDot implements Reason
{
public function code() : int
{
return 132;
}
public function description() : string
{
return 'Concecutive DOT found';
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
abstract class DetailedReason implements Reason
{
protected $detailedDescription;
public function __construct(string $details)
{
$this->detailedDescription = $details;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class DomainAcceptsNoMail implements Reason
{
public function code() : int
{
return 154;
}
public function description() : string
{
return 'Domain accepts no mail (Null MX, RFC7505)';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class DomainHyphened extends DetailedReason
{
public function code() : int
{
return 144;
}
public function description() : string
{
return 'S_HYPHEN found in domain';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class DomainTooLong implements Reason
{
public function code() : int
{
return 244;
}
public function description() : string
{
return 'Domain is longer than 253 characters';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class DotAtEnd implements Reason
{
public function code() : int
{
return 142;
}
public function description() : string
{
return 'Dot at the end';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class DotAtStart implements Reason
{
public function code() : int
{
return 141;
}
public function description() : string
{
return "Starts with a DOT";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class EmptyReason implements Reason
{
public function code() : int
{
return 0;
}
public function description() : string
{
return 'Empty reason';
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ExceptionFound implements Reason
{
/**
* @var \Exception
*/
private $exception;
public function __construct(\Exception $exception)
{
$this->exception = $exception;
}
public function code() : int
{
return 999;
}
public function description() : string
{
return $this->exception->getMessage();
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ExpectingATEXT extends DetailedReason
{
public function code() : int
{
return 137;
}
public function description() : string
{
return "Expecting ATEXT (Printable US-ASCII). Extended: " . $this->detailedDescription;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ExpectingCTEXT implements Reason
{
public function code() : int
{
return 139;
}
public function description() : string
{
return 'Expecting CTEXT';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ExpectingDTEXT implements Reason
{
public function code() : int
{
return 129;
}
public function description() : string
{
return 'Expecting DTEXT';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class ExpectingDomainLiteralClose implements Reason
{
public function code() : int
{
return 137;
}
public function description() : string
{
return "Closing bracket ']' for domain literal not found";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class LabelTooLong implements Reason
{
public function code() : int
{
return 245;
}
public function description() : string
{
return 'Domain "label" is longer than 63 characters';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class LocalOrReservedDomain implements Reason
{
public function code() : int
{
return 153;
}
public function description() : string
{
return 'Local, mDNS or reserved domain (RFC2606, RFC6762)';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class NoDNSRecord implements Reason
{
public function code() : int
{
return 5;
}
public function description() : string
{
return 'No MX or A DSN record was found for this email';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class NoDomainPart implements Reason
{
public function code() : int
{
return 131;
}
public function description() : string
{
return 'No domain part found';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class NoLocalPart implements Reason
{
public function code() : int
{
return 130;
}
public function description() : string
{
return "No local part";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class RFCWarnings implements Reason
{
public function code() : int
{
return 997;
}
public function description() : string
{
return 'Warnings found after validating';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
interface Reason
{
/**
* Code for user land to act upon;
*/
public function code() : int;
/**
* Short description of the result, human readable.
*/
public function description() : string;
}

View File

@ -0,0 +1,17 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class SpoofEmail implements Reason
{
public function code() : int
{
return 298;
}
public function description() : string
{
return 'The email contains mixed UTF8 chars that makes it suspicious';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class UnOpenedComment implements Reason
{
public function code() : int
{
return 152;
}
public function description(): string
{
return 'Missing opening comment parentheses - https://tools.ietf.org/html/rfc5322#section-3.2.2';
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
/**
* Used on SERVFAIL, TIMEOUT or other runtime and network errors
*/
class UnableToGetDNSRecord extends NoDNSRecord
{
public function code() : int
{
return 3;
}
public function description() : string
{
return 'Unable to get DNS records for the host';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class UnclosedComment implements Reason
{
public function code() : int
{
return 146;
}
public function description(): string
{
return 'No closing comment token found';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class UnclosedQuotedString implements Reason
{
public function code() : int
{
return 145;
}
public function description() : string
{
return "Unclosed quoted string";
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class UnusualElements implements Reason
{
/**
* @var string $element
*/
private $element;
public function __construct(string $element)
{
$this->element = $element;
}
public function code() : int
{
return 201;
}
public function description() : string
{
return 'Unusual element found, wourld render invalid in majority of cases. Element found: ' . $this->element;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Egulias\EmailValidator\Result;
interface Result
{
/**
* Is validation result valid?
*/
public function isValid() : bool;
/**
* Is validation result invalid?
* Usually the inverse of isValid()
*/
public function isInvalid() : bool;
/**
* Short description of the result, human readable.
*/
public function description() : string;
/**
* Code for user land to act upon.
*/
public function code() : int;
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Result;
use Egulias\EmailValidator\Result\Reason\SpoofEmail as ReasonSpoofEmail;
class SpoofEmail extends InvalidEmail
{
public function __construct()
{
$this->reason = new ReasonSpoofEmail();
parent::__construct($this->reason, '');
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Egulias\EmailValidator\Result;
class ValidEmail implements Result
{
public function isValid(): bool
{
return true;
}
public function isInvalid(): bool
{
return false;
}
public function description(): string
{
return "Valid email";
}
public function code(): int
{
return 0;
}
}

View File

@ -0,0 +1,211 @@
<?php
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\DomainAcceptsNoMail;
use Egulias\EmailValidator\Result\Reason\LocalOrReservedDomain;
use Egulias\EmailValidator\Result\Reason\NoDNSRecord as ReasonNoDNSRecord;
use Egulias\EmailValidator\Result\Reason\UnableToGetDNSRecord;
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
use Egulias\EmailValidator\Warning\Warning;
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)
*
* @var string[]
*/
public const RESERVED_DNS_TOP_LEVEL_NAMES = [
// Reserved Top Level DNS Names
'test',
'example',
'invalid',
'localhost',
// mDNS
'local',
// Private DNS Namespaces
'intranet',
'internal',
'private',
'corp',
'home',
'lan',
];
/**
* @var Warning[]
*/
private $warnings = [];
/**
* @var InvalidEmail|null
*/
private $error;
/**
* @var array
*/
private $mxRecords = [];
/**
* @var DNSGetRecordWrapper
*/
private $dnsGetRecord;
public function __construct(?DNSGetRecordWrapper $dnsGetRecord = null)
{
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
{
// use the input to check DNS if we cannot extract something similar to a domain
$host = $email;
// Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
if (false !== $lastAtPos = strrpos($email, '@')) {
$host = substr($email, $lastAtPos + 1);
}
// Get the domain parts
$hostParts = explode('.', $host);
$isLocalDomain = count($hostParts) <= 1;
$isReservedTopLevel = in_array($hostParts[(count($hostParts) - 1)], self::RESERVED_DNS_TOP_LEVEL_NAMES, true);
// Exclude reserved top level DNS names
if ($isLocalDomain || $isReservedTopLevel) {
$this->error = new InvalidEmail(new LocalOrReservedDomain(), $host);
return false;
}
return $this->checkDns($host);
}
public function getError(): ?InvalidEmail
{
return $this->error;
}
/**
* @return Warning[]
*/
public function getWarnings(): array
{
return $this->warnings;
}
/**
* @param string $host
*
* @return bool
*/
protected function checkDns($host)
{
$variant = INTL_IDNA_VARIANT_UTS46;
$host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.');
$hostParts = explode('.', $host);
$host = array_pop($hostParts);
while (count($hostParts) > 0) {
$host = array_pop($hostParts) . '.' . $host;
if ($this->validateDnsRecords($host)) {
return true;
}
}
return false;
}
/**
* Validate the DNS records for given host.
*
* @param string $host A set of DNS records in the format returned by dns_get_record.
*
* @return bool True on success.
*/
private function validateDnsRecords($host): bool
{
$dnsRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_A + DNS_MX);
if ($dnsRecordsResult->withError()) {
$this->error = new InvalidEmail(new UnableToGetDNSRecord(), '');
return false;
}
$dnsRecords = $dnsRecordsResult->getRecords();
// Combined check for A+MX+AAAA can fail with SERVFAIL, even in the presence of valid A/MX records
$aaaaRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_AAAA);
if (! $aaaaRecordsResult->withError()) {
$dnsRecords = array_merge($dnsRecords, $aaaaRecordsResult->getRecords());
}
// No MX, A or AAAA DNS records
if ($dnsRecords === []) {
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
return false;
}
// For each DNS record
foreach ($dnsRecords as $dnsRecord) {
if (!$this->validateMXRecord($dnsRecord)) {
// No MX records (fallback to A or AAAA records)
if (empty($this->mxRecords)) {
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
}
return false;
}
}
return true;
}
/**
* Validate an MX record
*
* @param array $dnsRecord Given DNS record.
*
* @return bool True if valid.
*/
private function validateMxRecord($dnsRecord): bool
{
if (!isset($dnsRecord['type'])) {
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
return false;
}
if ($dnsRecord['type'] !== 'MX') {
return true;
}
// "Null MX" record indicates the domain accepts no mail (https://tools.ietf.org/html/rfc7505)
if (empty($dnsRecord['target']) || $dnsRecord['target'] === '.') {
$this->error = new InvalidEmail(new DomainAcceptsNoMail(), "");
return false;
}
$this->mxRecords[] = $dnsRecord;
return true;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Egulias\EmailValidator\Validation;
class DNSGetRecordWrapper
{
/**
* @param string $host
* @param int $type
*
* @return DNSRecords
*/
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): never {
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();
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Egulias\EmailValidator\Validation;
class DNSRecords
{
/**
* @param array $records
* @param bool $error
*/
public function __construct(private readonly array $records, private readonly bool $error = false)
{
}
/**
* @return array
*/
public function getRecords(): array
{
return $this->records;
}
public function withError(): bool
{
return $this->error;
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\Warning;
interface EmailValidation
{
/**
* Returns true if the given email is valid.
*
* @param string $email The email you want to validate.
* @param EmailLexer $emailLexer The email lexer.
*
* @return bool
*/
public function isValid(string $email, EmailLexer $emailLexer) : bool;
/**
* Returns the validation error.
*
* @return InvalidEmail|null
*/
public function getError() : ?InvalidEmail;
/**
* Returns the validation warnings.
*
* @return Warning[]
*/
public function getWarnings() : array;
}

View File

@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Validation\Exception;
use Exception;
class EmptyValidationList extends \InvalidArgumentException
{
/**
* @param int $code
*/
public function __construct($code = 0, ?Exception $previous = null)
{
parent::__construct("Empty validation list is not allowed", $code, $previous);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Egulias\EmailValidator\Validation\Extra;
use \Spoofchecker;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\SpoofEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\EmailValidation;
class SpoofCheckValidation implements EmailValidation
{
/**
* @var InvalidEmail|null
*/
private $error;
public function __construct()
{
if (!extension_loaded('intl')) {
throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
}
}
/**
* @psalm-suppress InvalidArgument
*/
public function isValid(string $email, EmailLexer $emailLexer) : bool
{
$checker = new Spoofchecker();
$checker->setChecks(Spoofchecker::SINGLE_SCRIPT);
if ($checker->isSuspicious($email)) {
$this->error = new SpoofEmail();
}
return $this->error === null;
}
public function getError() : ?InvalidEmail
{
return $this->error;
}
public function getWarnings() : array
{
return [];
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\MessageIDParser;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ExceptionFound;
use Egulias\EmailValidator\Warning\Warning;
class MessageIDValidation implements EmailValidation
{
/**
* @var Warning[]
*/
private $warnings = [];
/**
* @var ?InvalidEmail
*/
private $error;
public function isValid(string $email, EmailLexer $emailLexer): bool
{
$parser = new MessageIDParser($emailLexer);
try {
$result = $parser->parse($email);
$this->warnings = $parser->getWarnings();
if ($result->isInvalid()) {
/** @psalm-suppress PropertyTypeCoercion */
$this->error = $result;
return false;
}
} catch (\Exception $invalid) {
$this->error = new InvalidEmail(new ExceptionFound($invalid), '');
return false;
}
return true;
}
/**
* @return Warning[]
*/
public function getWarnings(): array
{
return $this->warnings;
}
public function getError(): ?InvalidEmail
{
return $this->error;
}
}

View File

@ -0,0 +1,105 @@
<?php
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
use Egulias\EmailValidator\Result\MultipleErrors;
use Egulias\EmailValidator\Warning\Warning;
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;
/**
* 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;
/**
* @var Warning[]
*/
private $warnings = [];
/**
* @var MultipleErrors|null
*/
private $error;
/**
* @param EmailValidation[] $validations The validations.
* @param int $mode The validation mode (one of the constants).
*/
public function __construct(private readonly array $validations, private readonly int $mode = self::ALLOW_ALL_ERRORS)
{
if (count($validations) == 0) {
throw new EmptyValidationList();
}
}
/**
* {@inheritdoc}
*/
public function isValid(string $email, EmailLexer $emailLexer): bool
{
$result = true;
foreach ($this->validations as $validation) {
$emailLexer->reset();
$validationResult = $validation->isValid($email, $emailLexer);
$result = $result && $validationResult;
$this->warnings = [...$this->warnings, ...$validation->getWarnings()];
if (!$validationResult) {
$this->processError($validation);
}
if ($this->shouldStop($result)) {
break;
}
}
return $result;
}
private function initErrorStorage(): void
{
if (null === $this->error) {
$this->error = new MultipleErrors();
}
}
private function processError(EmailValidation $validation): void
{
if (null !== $validation->getError()) {
$this->initErrorStorage();
/** @psalm-suppress PossiblyNullReference */
$this->error->addReason($validation->getError()->reason());
}
}
private function shouldStop(bool $result): bool
{
return !$result && $this->mode === self::STOP_ON_ERROR;
}
/**
* Returns the validation errors.
*/
public function getError(): ?InvalidEmail
{
return $this->error;
}
/**
* @return Warning[]
*/
public function getWarnings(): array
{
return $this->warnings;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\RFCWarnings;
class NoRFCWarningsValidation extends RFCValidation
{
/**
* @var InvalidEmail|null
*/
private $error;
/**
* {@inheritdoc}
*/
public function isValid(string $email, EmailLexer $emailLexer) : bool
{
if (!parent::isValid($email, $emailLexer)) {
return false;
}
if (empty($this->getWarnings())) {
return true;
}
$this->error = new InvalidEmail(new RFCWarnings(), '');
return false;
}
/**
* {@inheritdoc}
*/
public function getError() : ?InvalidEmail
{
return $this->error ?: parent::getError();
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\EmailParser;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ExceptionFound;
use Egulias\EmailValidator\Warning\Warning;
class RFCValidation implements EmailValidation
{
/**
* @var Warning[]
*/
private array $warnings = [];
/**
* @var ?InvalidEmail
*/
private $error;
public function isValid(string $email, EmailLexer $emailLexer): bool
{
$parser = new EmailParser($emailLexer);
try {
$result = $parser->parse($email);
$this->warnings = $parser->getWarnings();
if ($result->isInvalid()) {
/** @psalm-suppress PropertyTypeCoercion */
$this->error = $result;
return false;
}
} catch (\Exception $invalid) {
$this->error = new InvalidEmail(new ExceptionFound($invalid), '');
return false;
}
return true;
}
public function getError(): ?InvalidEmail
{
return $this->error;
}
/**
* @return Warning[]
*/
public function getWarnings(): array
{
return $this->warnings;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class AddressLiteral extends Warning
{
public const CODE = 12;
public function __construct()
{
$this->message = 'Address literal in domain part';
$this->rfcNumber = 5321;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Warning;
class CFWSNearAt extends Warning
{
public const CODE = 49;
public function __construct()
{
$this->message = "Deprecated folding white space near @";
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Warning;
class CFWSWithFWS extends Warning
{
public const CODE = 18;
public function __construct()
{
$this->message = 'Folding whites space followed by folding white space';
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Warning;
class Comment extends Warning
{
public const CODE = 17;
public function __construct()
{
$this->message = "Comments found in this email";
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Warning;
class DeprecatedComment extends Warning
{
public const CODE = 37;
public function __construct()
{
$this->message = 'Deprecated comments';
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class DomainLiteral extends Warning
{
public const CODE = 70;
public function __construct()
{
$this->message = 'Domain Literal';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Egulias\EmailValidator\Warning;
use Egulias\EmailValidator\EmailParser;
class EmailTooLong extends Warning
{
public const CODE = 66;
public function __construct()
{
$this->message = 'Email is too long, exceeds ' . EmailParser::EMAIL_MAX_LENGTH;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6BadChar extends Warning
{
public const CODE = 74;
public function __construct()
{
$this->message = 'Bad char in IPV6 domain literal';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6ColonEnd extends Warning
{
public const CODE = 77;
public function __construct()
{
$this->message = ':: found at the end of the domain literal';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6ColonStart extends Warning
{
public const CODE = 76;
public function __construct()
{
$this->message = ':: found at the start of the domain literal';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6Deprecated extends Warning
{
public const CODE = 13;
public function __construct()
{
$this->message = 'Deprecated form of IPV6';
$this->rfcNumber = 5321;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6DoubleColon extends Warning
{
public const CODE = 73;
public function __construct()
{
$this->message = 'Double colon found after IPV6 tag';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6GroupCount extends Warning
{
public const CODE = 72;
public function __construct()
{
$this->message = 'Group count is not IPV6 valid';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class IPV6MaxGroups extends Warning
{
public const CODE = 75;
public function __construct()
{
$this->message = 'Reached the maximum number of IPV6 groups allowed';
$this->rfcNumber = 5321;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Egulias\EmailValidator\Warning;
class LocalTooLong extends Warning
{
public const CODE = 64;
public const LOCAL_PART_LENGTH = 64;
public function __construct()
{
$this->message = 'Local part is too long, exceeds 64 chars (octets)';
$this->rfcNumber = 5322;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class NoDNSMXRecord extends Warning
{
public const CODE = 6;
public function __construct()
{
$this->message = 'No MX DSN record was found for this email';
$this->rfcNumber = 5321;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Egulias\EmailValidator\Warning;
class ObsoleteDTEXT extends Warning
{
public const CODE = 71;
public function __construct()
{
$this->rfcNumber = 5322;
$this->message = 'Obsolete DTEXT in domain literal';
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Egulias\EmailValidator\Warning;
class QuotedPart extends Warning
{
public const CODE = 36;
/**
* @param scalar|null $prevToken
* @param scalar|null $postToken
*/
public function __construct($prevToken, $postToken)
{
$this->message = "Deprecated Quoted String found between $prevToken and $postToken";
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Egulias\EmailValidator\Warning;
class QuotedString extends Warning
{
public const CODE = 11;
/**
* @param scalar $prevToken
* @param scalar $postToken
*/
public function __construct($prevToken, $postToken)
{
$this->message = "Quoted String found between $prevToken and $postToken";
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Egulias\EmailValidator\Warning;
class TLD extends Warning
{
public const CODE = 9;
public function __construct()
{
$this->message = "RFC5321, TLD";
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Egulias\EmailValidator\Warning;
abstract class Warning
{
/**
* @var int CODE
*/
public const CODE = 0;
/**
* @var string
*/
protected $message = '';
/**
* @var int
*/
protected $rfcNumber = 0;
/**
* @return string
*/
public function message()
{
return $this->message;
}
/**
* @return int
*/
public function code()
{
return self::CODE;
}
/**
* @return int
*/
public function RFCNumber()
{
return $this->rfcNumber;
}
/**
* @return string
*/
public function __toString(): string
{
return $this->message() . " rfc: " . $this->rfcNumber . "internal code: " . static::CODE;
}
}