@@ -8,42 +8,42 @@ |
||
| 8 | 8 | |
| 9 | 9 | class RFCValidation implements EmailValidation |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var EmailParser|null |
|
| 13 | - */ |
|
| 14 | - private $parser; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - private $warnings = []; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var InvalidEmail|null |
|
| 23 | - */ |
|
| 24 | - private $error; |
|
| 25 | - |
|
| 26 | - public function isValid($email, EmailLexer $emailLexer) |
|
| 27 | - { |
|
| 28 | - $this->parser = new EmailParser($emailLexer); |
|
| 29 | - try { |
|
| 30 | - $this->parser->parse((string)$email); |
|
| 31 | - } catch (InvalidEmail $invalid) { |
|
| 32 | - $this->error = $invalid; |
|
| 33 | - return false; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - $this->warnings = $this->parser->getWarnings(); |
|
| 37 | - return true; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function getError() |
|
| 41 | - { |
|
| 42 | - return $this->error; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function getWarnings() |
|
| 46 | - { |
|
| 47 | - return $this->warnings; |
|
| 48 | - } |
|
| 11 | + /** |
|
| 12 | + * @var EmailParser|null |
|
| 13 | + */ |
|
| 14 | + private $parser; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + private $warnings = []; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var InvalidEmail|null |
|
| 23 | + */ |
|
| 24 | + private $error; |
|
| 25 | + |
|
| 26 | + public function isValid($email, EmailLexer $emailLexer) |
|
| 27 | + { |
|
| 28 | + $this->parser = new EmailParser($emailLexer); |
|
| 29 | + try { |
|
| 30 | + $this->parser->parse((string)$email); |
|
| 31 | + } catch (InvalidEmail $invalid) { |
|
| 32 | + $this->error = $invalid; |
|
| 33 | + return false; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + $this->warnings = $this->parser->getWarnings(); |
|
| 37 | + return true; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function getError() |
|
| 41 | + { |
|
| 42 | + return $this->error; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function getWarnings() |
|
| 46 | + { |
|
| 47 | + return $this->warnings; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -8,27 +8,27 @@ |
||
| 8 | 8 | |
| 9 | 9 | interface EmailValidation |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Returns true if the given email is valid. |
|
| 13 | - * |
|
| 14 | - * @param string $email The email you want to validate. |
|
| 15 | - * @param EmailLexer $emailLexer The email lexer. |
|
| 16 | - * |
|
| 17 | - * @return bool |
|
| 18 | - */ |
|
| 19 | - public function isValid($email, EmailLexer $emailLexer); |
|
| 11 | + /** |
|
| 12 | + * Returns true if the given email is valid. |
|
| 13 | + * |
|
| 14 | + * @param string $email The email you want to validate. |
|
| 15 | + * @param EmailLexer $emailLexer The email lexer. |
|
| 16 | + * |
|
| 17 | + * @return bool |
|
| 18 | + */ |
|
| 19 | + public function isValid($email, EmailLexer $emailLexer); |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Returns the validation error. |
|
| 23 | - * |
|
| 24 | - * @return InvalidEmail|null |
|
| 25 | - */ |
|
| 26 | - public function getError(); |
|
| 21 | + /** |
|
| 22 | + * Returns the validation error. |
|
| 23 | + * |
|
| 24 | + * @return InvalidEmail|null |
|
| 25 | + */ |
|
| 26 | + public function getError(); |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Returns the validation warnings. |
|
| 30 | - * |
|
| 31 | - * @return Warning[] |
|
| 32 | - */ |
|
| 33 | - public function getWarnings(); |
|
| 28 | + /** |
|
| 29 | + * Returns the validation warnings. |
|
| 30 | + * |
|
| 31 | + * @return Warning[] |
|
| 32 | + */ |
|
| 33 | + public function getWarnings(); |
|
| 34 | 34 | } |
@@ -6,11 +6,11 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EmptyValidationList extends \InvalidArgumentException |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @param int $code |
|
| 11 | - */ |
|
| 12 | - public function __construct($code = 0, Exception $previous = null) |
|
| 13 | - { |
|
| 14 | - parent::__construct("Empty validation list is not allowed", $code, $previous); |
|
| 15 | - } |
|
| 9 | + /** |
|
| 10 | + * @param int $code |
|
| 11 | + */ |
|
| 12 | + public function __construct($code = 0, Exception $previous = null) |
|
| 13 | + { |
|
| 14 | + parent::__construct("Empty validation list is not allowed", $code, $previous); |
|
| 15 | + } |
|
| 16 | 16 | } |
@@ -9,43 +9,43 @@ |
||
| 9 | 9 | |
| 10 | 10 | class SpoofCheckValidation implements EmailValidation |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var InvalidEmail|null |
|
| 14 | - */ |
|
| 15 | - private $error; |
|
| 16 | - |
|
| 17 | - public function __construct() |
|
| 18 | - { |
|
| 19 | - if (!extension_loaded('intl')) { |
|
| 20 | - throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); |
|
| 21 | - } |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @psalm-suppress InvalidArgument |
|
| 26 | - */ |
|
| 27 | - public function isValid($email, EmailLexer $emailLexer) |
|
| 28 | - { |
|
| 29 | - $checker = new Spoofchecker(); |
|
| 30 | - $checker->setChecks(Spoofchecker::SINGLE_SCRIPT); |
|
| 31 | - |
|
| 32 | - if ($checker->isSuspicious($email)) { |
|
| 33 | - $this->error = new SpoofEmail(); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - return $this->error === null; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @return InvalidEmail|null |
|
| 41 | - */ |
|
| 42 | - public function getError() |
|
| 43 | - { |
|
| 44 | - return $this->error; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function getWarnings() |
|
| 48 | - { |
|
| 49 | - return []; |
|
| 50 | - } |
|
| 12 | + /** |
|
| 13 | + * @var InvalidEmail|null |
|
| 14 | + */ |
|
| 15 | + private $error; |
|
| 16 | + |
|
| 17 | + public function __construct() |
|
| 18 | + { |
|
| 19 | + if (!extension_loaded('intl')) { |
|
| 20 | + throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); |
|
| 21 | + } |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @psalm-suppress InvalidArgument |
|
| 26 | + */ |
|
| 27 | + public function isValid($email, EmailLexer $emailLexer) |
|
| 28 | + { |
|
| 29 | + $checker = new Spoofchecker(); |
|
| 30 | + $checker->setChecks(Spoofchecker::SINGLE_SCRIPT); |
|
| 31 | + |
|
| 32 | + if ($checker->isSuspicious($email)) { |
|
| 33 | + $this->error = new SpoofEmail(); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + return $this->error === null; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @return InvalidEmail|null |
|
| 41 | + */ |
|
| 42 | + public function getError() |
|
| 43 | + { |
|
| 44 | + return $this->error; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function getWarnings() |
|
| 48 | + { |
|
| 49 | + return []; |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -8,34 +8,34 @@ |
||
| 8 | 8 | |
| 9 | 9 | class NoRFCWarningsValidation extends RFCValidation |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var InvalidEmail|null |
|
| 13 | - */ |
|
| 14 | - private $error; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function isValid($email, EmailLexer $emailLexer) |
|
| 20 | - { |
|
| 21 | - if (!parent::isValid($email, $emailLexer)) { |
|
| 22 | - return false; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - if (empty($this->getWarnings())) { |
|
| 26 | - return true; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - $this->error = new RFCWarnings(); |
|
| 30 | - |
|
| 31 | - return false; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - */ |
|
| 37 | - public function getError() |
|
| 38 | - { |
|
| 39 | - return $this->error ?: parent::getError(); |
|
| 40 | - } |
|
| 11 | + /** |
|
| 12 | + * @var InvalidEmail|null |
|
| 13 | + */ |
|
| 14 | + private $error; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function isValid($email, EmailLexer $emailLexer) |
|
| 20 | + { |
|
| 21 | + if (!parent::isValid($email, $emailLexer)) { |
|
| 22 | + return false; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + if (empty($this->getWarnings())) { |
|
| 26 | + return true; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + $this->error = new RFCWarnings(); |
|
| 30 | + |
|
| 31 | + return false; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + */ |
|
| 37 | + public function getError() |
|
| 38 | + { |
|
| 39 | + return $this->error ?: parent::getError(); |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -6,27 +6,27 @@ |
||
| 6 | 6 | |
| 7 | 7 | class MultipleErrors extends InvalidEmail |
| 8 | 8 | { |
| 9 | - const CODE = 999; |
|
| 10 | - const REASON = "Accumulated errors for multiple validations"; |
|
| 11 | - /** |
|
| 12 | - * @var InvalidEmail[] |
|
| 13 | - */ |
|
| 14 | - private $errors = []; |
|
| 9 | + const CODE = 999; |
|
| 10 | + const REASON = "Accumulated errors for multiple validations"; |
|
| 11 | + /** |
|
| 12 | + * @var InvalidEmail[] |
|
| 13 | + */ |
|
| 14 | + private $errors = []; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @param InvalidEmail[] $errors |
|
| 18 | - */ |
|
| 19 | - public function __construct(array $errors) |
|
| 20 | - { |
|
| 21 | - $this->errors = $errors; |
|
| 22 | - parent::__construct(); |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * @param InvalidEmail[] $errors |
|
| 18 | + */ |
|
| 19 | + public function __construct(array $errors) |
|
| 20 | + { |
|
| 21 | + $this->errors = $errors; |
|
| 22 | + parent::__construct(); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @return InvalidEmail[] |
|
| 27 | - */ |
|
| 28 | - public function getErrors() |
|
| 29 | - { |
|
| 30 | - return $this->errors; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * @return InvalidEmail[] |
|
| 27 | + */ |
|
| 28 | + public function getErrors() |
|
| 29 | + { |
|
| 30 | + return $this->errors; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -21,229 +21,229 @@ |
||
| 21 | 21 | |
| 22 | 22 | abstract class Parser |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $warnings = []; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var EmailLexer |
|
| 31 | - */ |
|
| 32 | - protected $lexer; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var int |
|
| 36 | - */ |
|
| 37 | - protected $openedParenthesis = 0; |
|
| 38 | - |
|
| 39 | - public function __construct(EmailLexer $lexer) |
|
| 40 | - { |
|
| 41 | - $this->lexer = $lexer; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return \Egulias\EmailValidator\Warning\Warning[] |
|
| 46 | - */ |
|
| 47 | - public function getWarnings() |
|
| 48 | - { |
|
| 49 | - return $this->warnings; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param string $str |
|
| 54 | - */ |
|
| 55 | - abstract public function parse($str); |
|
| 56 | - |
|
| 57 | - /** @return int */ |
|
| 58 | - public function getOpenedParenthesis() |
|
| 59 | - { |
|
| 60 | - return $this->openedParenthesis; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * validateQuotedPair |
|
| 65 | - */ |
|
| 66 | - protected function validateQuotedPair() |
|
| 67 | - { |
|
| 68 | - if (!($this->lexer->token['type'] === EmailLexer::INVALID |
|
| 69 | - || $this->lexer->token['type'] === EmailLexer::C_DEL)) { |
|
| 70 | - throw new ExpectingQPair(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $this->warnings[QuotedPart::CODE] = |
|
| 74 | - new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - protected function parseComments() |
|
| 78 | - { |
|
| 79 | - $this->openedParenthesis = 1; |
|
| 80 | - $this->isUnclosedComment(); |
|
| 81 | - $this->warnings[Comment::CODE] = new Comment(); |
|
| 82 | - while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
| 83 | - if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
|
| 84 | - $this->openedParenthesis++; |
|
| 85 | - } |
|
| 86 | - $this->warnEscaping(); |
|
| 87 | - $this->lexer->moveNext(); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $this->lexer->moveNext(); |
|
| 91 | - if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) { |
|
| 92 | - throw new ExpectingATEXT(); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - if ($this->lexer->isNextToken(EmailLexer::S_AT)) { |
|
| 96 | - $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - protected function isUnclosedComment() |
|
| 104 | - { |
|
| 105 | - try { |
|
| 106 | - $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
|
| 107 | - return true; |
|
| 108 | - } catch (\RuntimeException $e) { |
|
| 109 | - throw new UnclosedComment(); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - protected function parseFWS() |
|
| 114 | - { |
|
| 115 | - $previous = $this->lexer->getPrevious(); |
|
| 116 | - |
|
| 117 | - $this->checkCRLFInFWS(); |
|
| 118 | - |
|
| 119 | - if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
|
| 120 | - throw new CRNoLF(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
| 124 | - throw new AtextAfterCFWS(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
| 128 | - throw new ExpectingCTEXT(); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
| 132 | - $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
| 133 | - } else { |
|
| 134 | - $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - protected function checkConsecutiveDots() |
|
| 139 | - { |
|
| 140 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
| 141 | - throw new ConsecutiveDot(); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - protected function isFWS() |
|
| 149 | - { |
|
| 150 | - if ($this->escaped()) { |
|
| 151 | - return false; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - if ($this->lexer->token['type'] === EmailLexer::S_SP || |
|
| 155 | - $this->lexer->token['type'] === EmailLexer::S_HTAB || |
|
| 156 | - $this->lexer->token['type'] === EmailLexer::S_CR || |
|
| 157 | - $this->lexer->token['type'] === EmailLexer::S_LF || |
|
| 158 | - $this->lexer->token['type'] === EmailLexer::CRLF |
|
| 159 | - ) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @return bool |
|
| 168 | - */ |
|
| 169 | - protected function escaped() |
|
| 170 | - { |
|
| 171 | - $previous = $this->lexer->getPrevious(); |
|
| 172 | - |
|
| 173 | - if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH |
|
| 174 | - && |
|
| 175 | - $this->lexer->token['type'] !== EmailLexer::GENERIC |
|
| 176 | - ) { |
|
| 177 | - return true; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return false; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @return bool |
|
| 185 | - */ |
|
| 186 | - protected function warnEscaping() |
|
| 187 | - { |
|
| 188 | - if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
| 193 | - throw new ExpectingATEXT(); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $this->warnings[QuotedPart::CODE] = |
|
| 201 | - new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
| 202 | - return true; |
|
| 203 | - |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param bool $hasClosingQuote |
|
| 208 | - * |
|
| 209 | - * @return bool |
|
| 210 | - */ |
|
| 211 | - protected function checkDQUOTE($hasClosingQuote) |
|
| 212 | - { |
|
| 213 | - if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) { |
|
| 214 | - return $hasClosingQuote; |
|
| 215 | - } |
|
| 216 | - if ($hasClosingQuote) { |
|
| 217 | - return $hasClosingQuote; |
|
| 218 | - } |
|
| 219 | - $previous = $this->lexer->getPrevious(); |
|
| 220 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) { |
|
| 221 | - throw new ExpectingATEXT(); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - try { |
|
| 225 | - $this->lexer->find(EmailLexer::S_DQUOTE); |
|
| 226 | - $hasClosingQuote = true; |
|
| 227 | - } catch (\Exception $e) { |
|
| 228 | - throw new UnclosedQuotedString(); |
|
| 229 | - } |
|
| 230 | - $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']); |
|
| 231 | - |
|
| 232 | - return $hasClosingQuote; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - protected function checkCRLFInFWS() |
|
| 236 | - { |
|
| 237 | - if ($this->lexer->token['type'] !== EmailLexer::CRLF) { |
|
| 238 | - return; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
| 242 | - throw new CRLFX2(); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
| 246 | - throw new CRLFAtTheEnd(); |
|
| 247 | - } |
|
| 248 | - } |
|
| 24 | + /** |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $warnings = []; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var EmailLexer |
|
| 31 | + */ |
|
| 32 | + protected $lexer; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var int |
|
| 36 | + */ |
|
| 37 | + protected $openedParenthesis = 0; |
|
| 38 | + |
|
| 39 | + public function __construct(EmailLexer $lexer) |
|
| 40 | + { |
|
| 41 | + $this->lexer = $lexer; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return \Egulias\EmailValidator\Warning\Warning[] |
|
| 46 | + */ |
|
| 47 | + public function getWarnings() |
|
| 48 | + { |
|
| 49 | + return $this->warnings; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param string $str |
|
| 54 | + */ |
|
| 55 | + abstract public function parse($str); |
|
| 56 | + |
|
| 57 | + /** @return int */ |
|
| 58 | + public function getOpenedParenthesis() |
|
| 59 | + { |
|
| 60 | + return $this->openedParenthesis; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * validateQuotedPair |
|
| 65 | + */ |
|
| 66 | + protected function validateQuotedPair() |
|
| 67 | + { |
|
| 68 | + if (!($this->lexer->token['type'] === EmailLexer::INVALID |
|
| 69 | + || $this->lexer->token['type'] === EmailLexer::C_DEL)) { |
|
| 70 | + throw new ExpectingQPair(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $this->warnings[QuotedPart::CODE] = |
|
| 74 | + new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + protected function parseComments() |
|
| 78 | + { |
|
| 79 | + $this->openedParenthesis = 1; |
|
| 80 | + $this->isUnclosedComment(); |
|
| 81 | + $this->warnings[Comment::CODE] = new Comment(); |
|
| 82 | + while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
| 83 | + if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
|
| 84 | + $this->openedParenthesis++; |
|
| 85 | + } |
|
| 86 | + $this->warnEscaping(); |
|
| 87 | + $this->lexer->moveNext(); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $this->lexer->moveNext(); |
|
| 91 | + if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) { |
|
| 92 | + throw new ExpectingATEXT(); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + if ($this->lexer->isNextToken(EmailLexer::S_AT)) { |
|
| 96 | + $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + protected function isUnclosedComment() |
|
| 104 | + { |
|
| 105 | + try { |
|
| 106 | + $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
|
| 107 | + return true; |
|
| 108 | + } catch (\RuntimeException $e) { |
|
| 109 | + throw new UnclosedComment(); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + protected function parseFWS() |
|
| 114 | + { |
|
| 115 | + $previous = $this->lexer->getPrevious(); |
|
| 116 | + |
|
| 117 | + $this->checkCRLFInFWS(); |
|
| 118 | + |
|
| 119 | + if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
|
| 120 | + throw new CRNoLF(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
| 124 | + throw new AtextAfterCFWS(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
| 128 | + throw new ExpectingCTEXT(); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
| 132 | + $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
| 133 | + } else { |
|
| 134 | + $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + protected function checkConsecutiveDots() |
|
| 139 | + { |
|
| 140 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
| 141 | + throw new ConsecutiveDot(); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + protected function isFWS() |
|
| 149 | + { |
|
| 150 | + if ($this->escaped()) { |
|
| 151 | + return false; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + if ($this->lexer->token['type'] === EmailLexer::S_SP || |
|
| 155 | + $this->lexer->token['type'] === EmailLexer::S_HTAB || |
|
| 156 | + $this->lexer->token['type'] === EmailLexer::S_CR || |
|
| 157 | + $this->lexer->token['type'] === EmailLexer::S_LF || |
|
| 158 | + $this->lexer->token['type'] === EmailLexer::CRLF |
|
| 159 | + ) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @return bool |
|
| 168 | + */ |
|
| 169 | + protected function escaped() |
|
| 170 | + { |
|
| 171 | + $previous = $this->lexer->getPrevious(); |
|
| 172 | + |
|
| 173 | + if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH |
|
| 174 | + && |
|
| 175 | + $this->lexer->token['type'] !== EmailLexer::GENERIC |
|
| 176 | + ) { |
|
| 177 | + return true; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @return bool |
|
| 185 | + */ |
|
| 186 | + protected function warnEscaping() |
|
| 187 | + { |
|
| 188 | + if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
| 193 | + throw new ExpectingATEXT(); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $this->warnings[QuotedPart::CODE] = |
|
| 201 | + new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
| 202 | + return true; |
|
| 203 | + |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param bool $hasClosingQuote |
|
| 208 | + * |
|
| 209 | + * @return bool |
|
| 210 | + */ |
|
| 211 | + protected function checkDQUOTE($hasClosingQuote) |
|
| 212 | + { |
|
| 213 | + if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) { |
|
| 214 | + return $hasClosingQuote; |
|
| 215 | + } |
|
| 216 | + if ($hasClosingQuote) { |
|
| 217 | + return $hasClosingQuote; |
|
| 218 | + } |
|
| 219 | + $previous = $this->lexer->getPrevious(); |
|
| 220 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) { |
|
| 221 | + throw new ExpectingATEXT(); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + try { |
|
| 225 | + $this->lexer->find(EmailLexer::S_DQUOTE); |
|
| 226 | + $hasClosingQuote = true; |
|
| 227 | + } catch (\Exception $e) { |
|
| 228 | + throw new UnclosedQuotedString(); |
|
| 229 | + } |
|
| 230 | + $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']); |
|
| 231 | + |
|
| 232 | + return $hasClosingQuote; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + protected function checkCRLFInFWS() |
|
| 236 | + { |
|
| 237 | + if ($this->lexer->token['type'] !== EmailLexer::CRLF) { |
|
| 238 | + return; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
| 242 | + throw new CRLFX2(); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
| 246 | + throw new CRLFAtTheEnd(); |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | 249 | } |
@@ -14,132 +14,132 @@ |
||
| 14 | 14 | |
| 15 | 15 | class LocalPart extends Parser |
| 16 | 16 | { |
| 17 | - public function parse($localPart) |
|
| 18 | - { |
|
| 19 | - $parseDQuote = true; |
|
| 20 | - $closingQuote = false; |
|
| 21 | - $openedParenthesis = 0; |
|
| 22 | - $totalLength = 0; |
|
| 23 | - |
|
| 24 | - while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { |
|
| 25 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) { |
|
| 26 | - throw new DotAtStart(); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - $closingQuote = $this->checkDQUOTE($closingQuote); |
|
| 30 | - if ($closingQuote && $parseDQuote) { |
|
| 31 | - $parseDQuote = $this->parseDoubleQuote(); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
| 35 | - $this->parseComments(); |
|
| 36 | - $openedParenthesis += $this->getOpenedParenthesis(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
| 40 | - if ($openedParenthesis === 0) { |
|
| 41 | - throw new UnopenedComment(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $openedParenthesis--; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $this->checkConsecutiveDots(); |
|
| 48 | - |
|
| 49 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT && |
|
| 50 | - $this->lexer->isNextToken(EmailLexer::S_AT) |
|
| 51 | - ) { |
|
| 52 | - throw new DotAtEnd(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $this->warnEscaping(); |
|
| 56 | - $this->isInvalidToken($this->lexer->token, $closingQuote); |
|
| 57 | - |
|
| 58 | - if ($this->isFWS()) { |
|
| 59 | - $this->parseFWS(); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $totalLength += strlen($this->lexer->token['value']); |
|
| 63 | - $this->lexer->moveNext(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) { |
|
| 67 | - $this->warnings[LocalTooLong::CODE] = new LocalTooLong(); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return bool |
|
| 73 | - */ |
|
| 74 | - protected function parseDoubleQuote() |
|
| 75 | - { |
|
| 76 | - $parseAgain = true; |
|
| 77 | - $special = array( |
|
| 78 | - EmailLexer::S_CR => true, |
|
| 79 | - EmailLexer::S_HTAB => true, |
|
| 80 | - EmailLexer::S_LF => true |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - $invalid = array( |
|
| 84 | - EmailLexer::C_NUL => true, |
|
| 85 | - EmailLexer::S_HTAB => true, |
|
| 86 | - EmailLexer::S_CR => true, |
|
| 87 | - EmailLexer::S_LF => true |
|
| 88 | - ); |
|
| 89 | - $setSpecialsWarning = true; |
|
| 90 | - |
|
| 91 | - $this->lexer->moveNext(); |
|
| 92 | - |
|
| 93 | - while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) { |
|
| 94 | - $parseAgain = false; |
|
| 95 | - if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { |
|
| 96 | - $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
| 97 | - $setSpecialsWarning = false; |
|
| 98 | - } |
|
| 99 | - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) { |
|
| 100 | - $this->lexer->moveNext(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $this->lexer->moveNext(); |
|
| 104 | - |
|
| 105 | - if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) { |
|
| 106 | - throw new ExpectingATEXT(); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $prev = $this->lexer->getPrevious(); |
|
| 111 | - |
|
| 112 | - if ($prev['type'] === EmailLexer::S_BACKSLASH) { |
|
| 113 | - if (!$this->checkDQUOTE(false)) { |
|
| 114 | - throw new UnclosedQuotedString(); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) { |
|
| 119 | - throw new ExpectingAT(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $parseAgain; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param bool $closingQuote |
|
| 127 | - */ |
|
| 128 | - protected function isInvalidToken(array $token, $closingQuote) |
|
| 129 | - { |
|
| 130 | - $forbidden = array( |
|
| 131 | - EmailLexer::S_COMMA, |
|
| 132 | - EmailLexer::S_CLOSEBRACKET, |
|
| 133 | - EmailLexer::S_OPENBRACKET, |
|
| 134 | - EmailLexer::S_GREATERTHAN, |
|
| 135 | - EmailLexer::S_LOWERTHAN, |
|
| 136 | - EmailLexer::S_COLON, |
|
| 137 | - EmailLexer::S_SEMICOLON, |
|
| 138 | - EmailLexer::INVALID |
|
| 139 | - ); |
|
| 140 | - |
|
| 141 | - if (in_array($token['type'], $forbidden) && !$closingQuote) { |
|
| 142 | - throw new ExpectingATEXT(); |
|
| 143 | - } |
|
| 144 | - } |
|
| 17 | + public function parse($localPart) |
|
| 18 | + { |
|
| 19 | + $parseDQuote = true; |
|
| 20 | + $closingQuote = false; |
|
| 21 | + $openedParenthesis = 0; |
|
| 22 | + $totalLength = 0; |
|
| 23 | + |
|
| 24 | + while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { |
|
| 25 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) { |
|
| 26 | + throw new DotAtStart(); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + $closingQuote = $this->checkDQUOTE($closingQuote); |
|
| 30 | + if ($closingQuote && $parseDQuote) { |
|
| 31 | + $parseDQuote = $this->parseDoubleQuote(); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
| 35 | + $this->parseComments(); |
|
| 36 | + $openedParenthesis += $this->getOpenedParenthesis(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
| 40 | + if ($openedParenthesis === 0) { |
|
| 41 | + throw new UnopenedComment(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $openedParenthesis--; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $this->checkConsecutiveDots(); |
|
| 48 | + |
|
| 49 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT && |
|
| 50 | + $this->lexer->isNextToken(EmailLexer::S_AT) |
|
| 51 | + ) { |
|
| 52 | + throw new DotAtEnd(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $this->warnEscaping(); |
|
| 56 | + $this->isInvalidToken($this->lexer->token, $closingQuote); |
|
| 57 | + |
|
| 58 | + if ($this->isFWS()) { |
|
| 59 | + $this->parseFWS(); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + $totalLength += strlen($this->lexer->token['value']); |
|
| 63 | + $this->lexer->moveNext(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) { |
|
| 67 | + $this->warnings[LocalTooLong::CODE] = new LocalTooLong(); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return bool |
|
| 73 | + */ |
|
| 74 | + protected function parseDoubleQuote() |
|
| 75 | + { |
|
| 76 | + $parseAgain = true; |
|
| 77 | + $special = array( |
|
| 78 | + EmailLexer::S_CR => true, |
|
| 79 | + EmailLexer::S_HTAB => true, |
|
| 80 | + EmailLexer::S_LF => true |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + $invalid = array( |
|
| 84 | + EmailLexer::C_NUL => true, |
|
| 85 | + EmailLexer::S_HTAB => true, |
|
| 86 | + EmailLexer::S_CR => true, |
|
| 87 | + EmailLexer::S_LF => true |
|
| 88 | + ); |
|
| 89 | + $setSpecialsWarning = true; |
|
| 90 | + |
|
| 91 | + $this->lexer->moveNext(); |
|
| 92 | + |
|
| 93 | + while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) { |
|
| 94 | + $parseAgain = false; |
|
| 95 | + if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { |
|
| 96 | + $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
| 97 | + $setSpecialsWarning = false; |
|
| 98 | + } |
|
| 99 | + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) { |
|
| 100 | + $this->lexer->moveNext(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->lexer->moveNext(); |
|
| 104 | + |
|
| 105 | + if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) { |
|
| 106 | + throw new ExpectingATEXT(); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $prev = $this->lexer->getPrevious(); |
|
| 111 | + |
|
| 112 | + if ($prev['type'] === EmailLexer::S_BACKSLASH) { |
|
| 113 | + if (!$this->checkDQUOTE(false)) { |
|
| 114 | + throw new UnclosedQuotedString(); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) { |
|
| 119 | + throw new ExpectingAT(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $parseAgain; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param bool $closingQuote |
|
| 127 | + */ |
|
| 128 | + protected function isInvalidToken(array $token, $closingQuote) |
|
| 129 | + { |
|
| 130 | + $forbidden = array( |
|
| 131 | + EmailLexer::S_COMMA, |
|
| 132 | + EmailLexer::S_CLOSEBRACKET, |
|
| 133 | + EmailLexer::S_OPENBRACKET, |
|
| 134 | + EmailLexer::S_GREATERTHAN, |
|
| 135 | + EmailLexer::S_LOWERTHAN, |
|
| 136 | + EmailLexer::S_COLON, |
|
| 137 | + EmailLexer::S_SEMICOLON, |
|
| 138 | + EmailLexer::INVALID |
|
| 139 | + ); |
|
| 140 | + |
|
| 141 | + if (in_array($token['type'], $forbidden) && !$closingQuote) { |
|
| 142 | + throw new ExpectingATEXT(); |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | } |
@@ -34,410 +34,410 @@ |
||
| 34 | 34 | |
| 35 | 35 | class DomainPart extends Parser |
| 36 | 36 | { |
| 37 | - const DOMAIN_MAX_LENGTH = 254; |
|
| 38 | - const LABEL_MAX_LENGTH = 63; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - protected $domainPart = ''; |
|
| 44 | - |
|
| 45 | - public function parse($domainPart) |
|
| 46 | - { |
|
| 47 | - $this->lexer->moveNext(); |
|
| 48 | - |
|
| 49 | - $this->performDomainStartChecks(); |
|
| 50 | - |
|
| 51 | - $domain = $this->doParseDomainPart(); |
|
| 52 | - |
|
| 53 | - $prev = $this->lexer->getPrevious(); |
|
| 54 | - $length = strlen($domain); |
|
| 55 | - |
|
| 56 | - if ($prev['type'] === EmailLexer::S_DOT) { |
|
| 57 | - throw new DotAtEnd(); |
|
| 58 | - } |
|
| 59 | - if ($prev['type'] === EmailLexer::S_HYPHEN) { |
|
| 60 | - throw new DomainHyphened(); |
|
| 61 | - } |
|
| 62 | - if ($length > self::DOMAIN_MAX_LENGTH) { |
|
| 63 | - $this->warnings[DomainTooLong::CODE] = new DomainTooLong(); |
|
| 64 | - } |
|
| 65 | - if ($prev['type'] === EmailLexer::S_CR) { |
|
| 66 | - throw new CRLFAtTheEnd(); |
|
| 67 | - } |
|
| 68 | - $this->domainPart = $domain; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - private function performDomainStartChecks() |
|
| 72 | - { |
|
| 73 | - $this->checkInvalidTokensAfterAT(); |
|
| 74 | - $this->checkEmptyDomain(); |
|
| 75 | - |
|
| 76 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
| 77 | - $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); |
|
| 78 | - $this->parseDomainComments(); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - private function checkEmptyDomain() |
|
| 83 | - { |
|
| 84 | - $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY || |
|
| 85 | - ($this->lexer->token['type'] === EmailLexer::S_SP && |
|
| 86 | - !$this->lexer->isNextToken(EmailLexer::GENERIC)); |
|
| 87 | - |
|
| 88 | - if ($thereIsNoDomain) { |
|
| 89 | - throw new NoDomainPart(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - private function checkInvalidTokensAfterAT() |
|
| 94 | - { |
|
| 95 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
| 96 | - throw new DotAtStart(); |
|
| 97 | - } |
|
| 98 | - if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) { |
|
| 99 | - throw new DomainHyphened(); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function getDomainPart() |
|
| 107 | - { |
|
| 108 | - return $this->domainPart; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $addressLiteral |
|
| 113 | - * @param int $maxGroups |
|
| 114 | - */ |
|
| 115 | - public function checkIPV6Tag($addressLiteral, $maxGroups = 8) |
|
| 116 | - { |
|
| 117 | - $prev = $this->lexer->getPrevious(); |
|
| 118 | - if ($prev['type'] === EmailLexer::S_COLON) { |
|
| 119 | - $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $IPv6 = substr($addressLiteral, 5); |
|
| 123 | - //Daniel Marschall's new IPv6 testing strategy |
|
| 124 | - $matchesIP = explode(':', $IPv6); |
|
| 125 | - $groupCount = count($matchesIP); |
|
| 126 | - $colons = strpos($IPv6, '::'); |
|
| 127 | - |
|
| 128 | - if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) { |
|
| 129 | - $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar(); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($colons === false) { |
|
| 133 | - // We need exactly the right number of groups |
|
| 134 | - if ($groupCount !== $maxGroups) { |
|
| 135 | - $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount(); |
|
| 136 | - } |
|
| 137 | - return; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ($colons !== strrpos($IPv6, '::')) { |
|
| 141 | - $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon(); |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ($colons === 0 || $colons === (strlen($IPv6) - 2)) { |
|
| 146 | - // RFC 4291 allows :: at the start or end of an address |
|
| 147 | - //with 7 other groups in addition |
|
| 148 | - ++$maxGroups; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if ($groupCount > $maxGroups) { |
|
| 152 | - $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups(); |
|
| 153 | - } elseif ($groupCount === $maxGroups) { |
|
| 154 | - $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated(); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @return string |
|
| 160 | - */ |
|
| 161 | - protected function doParseDomainPart() |
|
| 162 | - { |
|
| 163 | - $domain = ''; |
|
| 164 | - $label = ''; |
|
| 165 | - $openedParenthesis = 0; |
|
| 166 | - do { |
|
| 167 | - $prev = $this->lexer->getPrevious(); |
|
| 168 | - |
|
| 169 | - $this->checkNotAllowedChars($this->lexer->token); |
|
| 170 | - |
|
| 171 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
| 172 | - $this->parseComments(); |
|
| 173 | - $openedParenthesis += $this->getOpenedParenthesis(); |
|
| 174 | - $this->lexer->moveNext(); |
|
| 175 | - $tmpPrev = $this->lexer->getPrevious(); |
|
| 176 | - if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
| 177 | - $openedParenthesis--; |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
| 181 | - if ($openedParenthesis === 0) { |
|
| 182 | - throw new UnopenedComment(); |
|
| 183 | - } else { |
|
| 184 | - $openedParenthesis--; |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $this->checkConsecutiveDots(); |
|
| 189 | - $this->checkDomainPartExceptions($prev); |
|
| 190 | - |
|
| 191 | - if ($this->hasBrackets()) { |
|
| 192 | - $this->parseDomainLiteral(); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
| 196 | - $this->checkLabelLength($label); |
|
| 197 | - $label = ''; |
|
| 198 | - } else { |
|
| 199 | - $label .= $this->lexer->token['value']; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if ($this->isFWS()) { |
|
| 203 | - $this->parseFWS(); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - $domain .= $this->lexer->token['value']; |
|
| 207 | - $this->lexer->moveNext(); |
|
| 208 | - if ($this->lexer->token['type'] === EmailLexer::S_SP) { |
|
| 209 | - throw new CharNotAllowed(); |
|
| 210 | - } |
|
| 211 | - } while (null !== $this->lexer->token['type']); |
|
| 212 | - |
|
| 213 | - $this->checkLabelLength($label); |
|
| 214 | - |
|
| 215 | - return $domain; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - private function checkNotAllowedChars(array $token) |
|
| 219 | - { |
|
| 220 | - $notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true]; |
|
| 221 | - if (isset($notAllowed[$token['type']])) { |
|
| 222 | - throw new CharNotAllowed(); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @return string|false |
|
| 228 | - */ |
|
| 229 | - protected function parseDomainLiteral() |
|
| 230 | - { |
|
| 231 | - if ($this->lexer->isNextToken(EmailLexer::S_COLON)) { |
|
| 232 | - $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
| 233 | - } |
|
| 234 | - if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) { |
|
| 235 | - $lexer = clone $this->lexer; |
|
| 236 | - $lexer->moveNext(); |
|
| 237 | - if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) { |
|
| 238 | - $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - return $this->doParseDomainLiteral(); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * @return string|false |
|
| 247 | - */ |
|
| 248 | - protected function doParseDomainLiteral() |
|
| 249 | - { |
|
| 250 | - $IPv6TAG = false; |
|
| 251 | - $addressLiteral = ''; |
|
| 252 | - do { |
|
| 253 | - if ($this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
| 254 | - throw new ExpectingDTEXT(); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if ($this->lexer->token['type'] === EmailLexer::INVALID || |
|
| 258 | - $this->lexer->token['type'] === EmailLexer::C_DEL || |
|
| 259 | - $this->lexer->token['type'] === EmailLexer::S_LF |
|
| 260 | - ) { |
|
| 261 | - $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENQBRACKET, EmailLexer::S_OPENBRACKET))) { |
|
| 265 | - throw new ExpectingDTEXT(); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - if ($this->lexer->isNextTokenAny( |
|
| 269 | - array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF) |
|
| 270 | - )) { |
|
| 271 | - $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
| 272 | - $this->parseFWS(); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - if ($this->lexer->isNextToken(EmailLexer::S_CR)) { |
|
| 276 | - throw new CRNoLF(); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) { |
|
| 280 | - $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
| 281 | - $addressLiteral .= $this->lexer->token['value']; |
|
| 282 | - $this->lexer->moveNext(); |
|
| 283 | - $this->validateQuotedPair(); |
|
| 284 | - } |
|
| 285 | - if ($this->lexer->token['type'] === EmailLexer::S_IPV6TAG) { |
|
| 286 | - $IPv6TAG = true; |
|
| 287 | - } |
|
| 288 | - if ($this->lexer->token['type'] === EmailLexer::S_CLOSEQBRACKET) { |
|
| 289 | - break; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $addressLiteral .= $this->lexer->token['value']; |
|
| 293 | - |
|
| 294 | - } while ($this->lexer->moveNext()); |
|
| 295 | - |
|
| 296 | - $addressLiteral = str_replace('[', '', $addressLiteral); |
|
| 297 | - $addressLiteral = $this->checkIPV4Tag($addressLiteral); |
|
| 298 | - |
|
| 299 | - if (false === $addressLiteral) { |
|
| 300 | - return $addressLiteral; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - if (!$IPv6TAG) { |
|
| 304 | - $this->warnings[DomainLiteral::CODE] = new DomainLiteral(); |
|
| 305 | - return $addressLiteral; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
| 309 | - |
|
| 310 | - $this->checkIPV6Tag($addressLiteral); |
|
| 311 | - |
|
| 312 | - return $addressLiteral; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * @param string $addressLiteral |
|
| 317 | - * |
|
| 318 | - * @return string|false |
|
| 319 | - */ |
|
| 320 | - protected function checkIPV4Tag($addressLiteral) |
|
| 321 | - { |
|
| 322 | - $matchesIP = array(); |
|
| 323 | - |
|
| 324 | - // Extract IPv4 part from the end of the address-literal (if there is one) |
|
| 325 | - if (preg_match( |
|
| 326 | - '/\\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]?)$/', |
|
| 327 | - $addressLiteral, |
|
| 328 | - $matchesIP |
|
| 329 | - ) > 0 |
|
| 330 | - ) { |
|
| 331 | - $index = strrpos($addressLiteral, $matchesIP[0]); |
|
| 332 | - if ($index === 0) { |
|
| 333 | - $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - // Convert IPv4 part to IPv6 format for further testing |
|
| 337 | - $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0'; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - return $addressLiteral; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - protected function checkDomainPartExceptions(array $prev) |
|
| 344 | - { |
|
| 345 | - $invalidDomainTokens = array( |
|
| 346 | - EmailLexer::S_DQUOTE => true, |
|
| 347 | - EmailLexer::S_SQUOTE => true, |
|
| 348 | - EmailLexer::S_BACKTICK => true, |
|
| 349 | - EmailLexer::S_SEMICOLON => true, |
|
| 350 | - EmailLexer::S_GREATERTHAN => true, |
|
| 351 | - EmailLexer::S_LOWERTHAN => true, |
|
| 352 | - ); |
|
| 353 | - |
|
| 354 | - if (isset($invalidDomainTokens[$this->lexer->token['type']])) { |
|
| 355 | - throw new ExpectingATEXT(); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - if ($this->lexer->token['type'] === EmailLexer::S_COMMA) { |
|
| 359 | - throw new CommaInDomain(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if ($this->lexer->token['type'] === EmailLexer::S_AT) { |
|
| 363 | - throw new ConsecutiveAt(); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) { |
|
| 367 | - throw new ExpectingATEXT(); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
| 371 | - throw new DomainHyphened(); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH |
|
| 375 | - && $this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
| 376 | - throw new ExpectingATEXT(); |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * @return bool |
|
| 382 | - */ |
|
| 383 | - protected function hasBrackets() |
|
| 384 | - { |
|
| 385 | - if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) { |
|
| 386 | - return false; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - try { |
|
| 390 | - $this->lexer->find(EmailLexer::S_CLOSEBRACKET); |
|
| 391 | - } catch (\RuntimeException $e) { |
|
| 392 | - throw new ExpectingDomainLiteralClose(); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - return true; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * @param string $label |
|
| 400 | - */ |
|
| 401 | - protected function checkLabelLength($label) |
|
| 402 | - { |
|
| 403 | - if ($this->isLabelTooLong($label)) { |
|
| 404 | - $this->warnings[LabelTooLong::CODE] = new LabelTooLong(); |
|
| 405 | - } |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * @param string $label |
|
| 410 | - * @return bool |
|
| 411 | - */ |
|
| 412 | - private function isLabelTooLong($label) |
|
| 413 | - { |
|
| 414 | - if (preg_match('/[^\x00-\x7F]/', $label)) { |
|
| 415 | - idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
|
| 416 | - |
|
| 417 | - return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - return strlen($label) > self::LABEL_MAX_LENGTH; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - protected function parseDomainComments() |
|
| 424 | - { |
|
| 425 | - $this->isUnclosedComment(); |
|
| 426 | - while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
| 427 | - $this->warnEscaping(); |
|
| 428 | - $this->lexer->moveNext(); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - $this->lexer->moveNext(); |
|
| 432 | - if ($this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
| 433 | - throw new ExpectingATEXT(); |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - protected function addTLDWarnings() |
|
| 438 | - { |
|
| 439 | - if ($this->warnings[DomainLiteral::CODE]) { |
|
| 440 | - $this->warnings[TLD::CODE] = new TLD(); |
|
| 441 | - } |
|
| 442 | - } |
|
| 37 | + const DOMAIN_MAX_LENGTH = 254; |
|
| 38 | + const LABEL_MAX_LENGTH = 63; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + protected $domainPart = ''; |
|
| 44 | + |
|
| 45 | + public function parse($domainPart) |
|
| 46 | + { |
|
| 47 | + $this->lexer->moveNext(); |
|
| 48 | + |
|
| 49 | + $this->performDomainStartChecks(); |
|
| 50 | + |
|
| 51 | + $domain = $this->doParseDomainPart(); |
|
| 52 | + |
|
| 53 | + $prev = $this->lexer->getPrevious(); |
|
| 54 | + $length = strlen($domain); |
|
| 55 | + |
|
| 56 | + if ($prev['type'] === EmailLexer::S_DOT) { |
|
| 57 | + throw new DotAtEnd(); |
|
| 58 | + } |
|
| 59 | + if ($prev['type'] === EmailLexer::S_HYPHEN) { |
|
| 60 | + throw new DomainHyphened(); |
|
| 61 | + } |
|
| 62 | + if ($length > self::DOMAIN_MAX_LENGTH) { |
|
| 63 | + $this->warnings[DomainTooLong::CODE] = new DomainTooLong(); |
|
| 64 | + } |
|
| 65 | + if ($prev['type'] === EmailLexer::S_CR) { |
|
| 66 | + throw new CRLFAtTheEnd(); |
|
| 67 | + } |
|
| 68 | + $this->domainPart = $domain; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + private function performDomainStartChecks() |
|
| 72 | + { |
|
| 73 | + $this->checkInvalidTokensAfterAT(); |
|
| 74 | + $this->checkEmptyDomain(); |
|
| 75 | + |
|
| 76 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
| 77 | + $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); |
|
| 78 | + $this->parseDomainComments(); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + private function checkEmptyDomain() |
|
| 83 | + { |
|
| 84 | + $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY || |
|
| 85 | + ($this->lexer->token['type'] === EmailLexer::S_SP && |
|
| 86 | + !$this->lexer->isNextToken(EmailLexer::GENERIC)); |
|
| 87 | + |
|
| 88 | + if ($thereIsNoDomain) { |
|
| 89 | + throw new NoDomainPart(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + private function checkInvalidTokensAfterAT() |
|
| 94 | + { |
|
| 95 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
| 96 | + throw new DotAtStart(); |
|
| 97 | + } |
|
| 98 | + if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) { |
|
| 99 | + throw new DomainHyphened(); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function getDomainPart() |
|
| 107 | + { |
|
| 108 | + return $this->domainPart; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $addressLiteral |
|
| 113 | + * @param int $maxGroups |
|
| 114 | + */ |
|
| 115 | + public function checkIPV6Tag($addressLiteral, $maxGroups = 8) |
|
| 116 | + { |
|
| 117 | + $prev = $this->lexer->getPrevious(); |
|
| 118 | + if ($prev['type'] === EmailLexer::S_COLON) { |
|
| 119 | + $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $IPv6 = substr($addressLiteral, 5); |
|
| 123 | + //Daniel Marschall's new IPv6 testing strategy |
|
| 124 | + $matchesIP = explode(':', $IPv6); |
|
| 125 | + $groupCount = count($matchesIP); |
|
| 126 | + $colons = strpos($IPv6, '::'); |
|
| 127 | + |
|
| 128 | + if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) { |
|
| 129 | + $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar(); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($colons === false) { |
|
| 133 | + // We need exactly the right number of groups |
|
| 134 | + if ($groupCount !== $maxGroups) { |
|
| 135 | + $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount(); |
|
| 136 | + } |
|
| 137 | + return; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ($colons !== strrpos($IPv6, '::')) { |
|
| 141 | + $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon(); |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ($colons === 0 || $colons === (strlen($IPv6) - 2)) { |
|
| 146 | + // RFC 4291 allows :: at the start or end of an address |
|
| 147 | + //with 7 other groups in addition |
|
| 148 | + ++$maxGroups; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if ($groupCount > $maxGroups) { |
|
| 152 | + $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups(); |
|
| 153 | + } elseif ($groupCount === $maxGroups) { |
|
| 154 | + $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated(); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @return string |
|
| 160 | + */ |
|
| 161 | + protected function doParseDomainPart() |
|
| 162 | + { |
|
| 163 | + $domain = ''; |
|
| 164 | + $label = ''; |
|
| 165 | + $openedParenthesis = 0; |
|
| 166 | + do { |
|
| 167 | + $prev = $this->lexer->getPrevious(); |
|
| 168 | + |
|
| 169 | + $this->checkNotAllowedChars($this->lexer->token); |
|
| 170 | + |
|
| 171 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
| 172 | + $this->parseComments(); |
|
| 173 | + $openedParenthesis += $this->getOpenedParenthesis(); |
|
| 174 | + $this->lexer->moveNext(); |
|
| 175 | + $tmpPrev = $this->lexer->getPrevious(); |
|
| 176 | + if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
| 177 | + $openedParenthesis--; |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
| 181 | + if ($openedParenthesis === 0) { |
|
| 182 | + throw new UnopenedComment(); |
|
| 183 | + } else { |
|
| 184 | + $openedParenthesis--; |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $this->checkConsecutiveDots(); |
|
| 189 | + $this->checkDomainPartExceptions($prev); |
|
| 190 | + |
|
| 191 | + if ($this->hasBrackets()) { |
|
| 192 | + $this->parseDomainLiteral(); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
| 196 | + $this->checkLabelLength($label); |
|
| 197 | + $label = ''; |
|
| 198 | + } else { |
|
| 199 | + $label .= $this->lexer->token['value']; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if ($this->isFWS()) { |
|
| 203 | + $this->parseFWS(); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + $domain .= $this->lexer->token['value']; |
|
| 207 | + $this->lexer->moveNext(); |
|
| 208 | + if ($this->lexer->token['type'] === EmailLexer::S_SP) { |
|
| 209 | + throw new CharNotAllowed(); |
|
| 210 | + } |
|
| 211 | + } while (null !== $this->lexer->token['type']); |
|
| 212 | + |
|
| 213 | + $this->checkLabelLength($label); |
|
| 214 | + |
|
| 215 | + return $domain; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + private function checkNotAllowedChars(array $token) |
|
| 219 | + { |
|
| 220 | + $notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true]; |
|
| 221 | + if (isset($notAllowed[$token['type']])) { |
|
| 222 | + throw new CharNotAllowed(); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @return string|false |
|
| 228 | + */ |
|
| 229 | + protected function parseDomainLiteral() |
|
| 230 | + { |
|
| 231 | + if ($this->lexer->isNextToken(EmailLexer::S_COLON)) { |
|
| 232 | + $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
| 233 | + } |
|
| 234 | + if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) { |
|
| 235 | + $lexer = clone $this->lexer; |
|
| 236 | + $lexer->moveNext(); |
|
| 237 | + if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) { |
|
| 238 | + $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + return $this->doParseDomainLiteral(); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * @return string|false |
|
| 247 | + */ |
|
| 248 | + protected function doParseDomainLiteral() |
|
| 249 | + { |
|
| 250 | + $IPv6TAG = false; |
|
| 251 | + $addressLiteral = ''; |
|
| 252 | + do { |
|
| 253 | + if ($this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
| 254 | + throw new ExpectingDTEXT(); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if ($this->lexer->token['type'] === EmailLexer::INVALID || |
|
| 258 | + $this->lexer->token['type'] === EmailLexer::C_DEL || |
|
| 259 | + $this->lexer->token['type'] === EmailLexer::S_LF |
|
| 260 | + ) { |
|
| 261 | + $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENQBRACKET, EmailLexer::S_OPENBRACKET))) { |
|
| 265 | + throw new ExpectingDTEXT(); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + if ($this->lexer->isNextTokenAny( |
|
| 269 | + array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF) |
|
| 270 | + )) { |
|
| 271 | + $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
| 272 | + $this->parseFWS(); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + if ($this->lexer->isNextToken(EmailLexer::S_CR)) { |
|
| 276 | + throw new CRNoLF(); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) { |
|
| 280 | + $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
| 281 | + $addressLiteral .= $this->lexer->token['value']; |
|
| 282 | + $this->lexer->moveNext(); |
|
| 283 | + $this->validateQuotedPair(); |
|
| 284 | + } |
|
| 285 | + if ($this->lexer->token['type'] === EmailLexer::S_IPV6TAG) { |
|
| 286 | + $IPv6TAG = true; |
|
| 287 | + } |
|
| 288 | + if ($this->lexer->token['type'] === EmailLexer::S_CLOSEQBRACKET) { |
|
| 289 | + break; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $addressLiteral .= $this->lexer->token['value']; |
|
| 293 | + |
|
| 294 | + } while ($this->lexer->moveNext()); |
|
| 295 | + |
|
| 296 | + $addressLiteral = str_replace('[', '', $addressLiteral); |
|
| 297 | + $addressLiteral = $this->checkIPV4Tag($addressLiteral); |
|
| 298 | + |
|
| 299 | + if (false === $addressLiteral) { |
|
| 300 | + return $addressLiteral; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + if (!$IPv6TAG) { |
|
| 304 | + $this->warnings[DomainLiteral::CODE] = new DomainLiteral(); |
|
| 305 | + return $addressLiteral; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
| 309 | + |
|
| 310 | + $this->checkIPV6Tag($addressLiteral); |
|
| 311 | + |
|
| 312 | + return $addressLiteral; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * @param string $addressLiteral |
|
| 317 | + * |
|
| 318 | + * @return string|false |
|
| 319 | + */ |
|
| 320 | + protected function checkIPV4Tag($addressLiteral) |
|
| 321 | + { |
|
| 322 | + $matchesIP = array(); |
|
| 323 | + |
|
| 324 | + // Extract IPv4 part from the end of the address-literal (if there is one) |
|
| 325 | + if (preg_match( |
|
| 326 | + '/\\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]?)$/', |
|
| 327 | + $addressLiteral, |
|
| 328 | + $matchesIP |
|
| 329 | + ) > 0 |
|
| 330 | + ) { |
|
| 331 | + $index = strrpos($addressLiteral, $matchesIP[0]); |
|
| 332 | + if ($index === 0) { |
|
| 333 | + $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + // Convert IPv4 part to IPv6 format for further testing |
|
| 337 | + $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0'; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + return $addressLiteral; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + protected function checkDomainPartExceptions(array $prev) |
|
| 344 | + { |
|
| 345 | + $invalidDomainTokens = array( |
|
| 346 | + EmailLexer::S_DQUOTE => true, |
|
| 347 | + EmailLexer::S_SQUOTE => true, |
|
| 348 | + EmailLexer::S_BACKTICK => true, |
|
| 349 | + EmailLexer::S_SEMICOLON => true, |
|
| 350 | + EmailLexer::S_GREATERTHAN => true, |
|
| 351 | + EmailLexer::S_LOWERTHAN => true, |
|
| 352 | + ); |
|
| 353 | + |
|
| 354 | + if (isset($invalidDomainTokens[$this->lexer->token['type']])) { |
|
| 355 | + throw new ExpectingATEXT(); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + if ($this->lexer->token['type'] === EmailLexer::S_COMMA) { |
|
| 359 | + throw new CommaInDomain(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if ($this->lexer->token['type'] === EmailLexer::S_AT) { |
|
| 363 | + throw new ConsecutiveAt(); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) { |
|
| 367 | + throw new ExpectingATEXT(); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
| 371 | + throw new DomainHyphened(); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH |
|
| 375 | + && $this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
| 376 | + throw new ExpectingATEXT(); |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * @return bool |
|
| 382 | + */ |
|
| 383 | + protected function hasBrackets() |
|
| 384 | + { |
|
| 385 | + if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) { |
|
| 386 | + return false; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + try { |
|
| 390 | + $this->lexer->find(EmailLexer::S_CLOSEBRACKET); |
|
| 391 | + } catch (\RuntimeException $e) { |
|
| 392 | + throw new ExpectingDomainLiteralClose(); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + return true; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * @param string $label |
|
| 400 | + */ |
|
| 401 | + protected function checkLabelLength($label) |
|
| 402 | + { |
|
| 403 | + if ($this->isLabelTooLong($label)) { |
|
| 404 | + $this->warnings[LabelTooLong::CODE] = new LabelTooLong(); |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * @param string $label |
|
| 410 | + * @return bool |
|
| 411 | + */ |
|
| 412 | + private function isLabelTooLong($label) |
|
| 413 | + { |
|
| 414 | + if (preg_match('/[^\x00-\x7F]/', $label)) { |
|
| 415 | + idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
|
| 416 | + |
|
| 417 | + return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + return strlen($label) > self::LABEL_MAX_LENGTH; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + protected function parseDomainComments() |
|
| 424 | + { |
|
| 425 | + $this->isUnclosedComment(); |
|
| 426 | + while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
| 427 | + $this->warnEscaping(); |
|
| 428 | + $this->lexer->moveNext(); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + $this->lexer->moveNext(); |
|
| 432 | + if ($this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
| 433 | + throw new ExpectingATEXT(); |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + protected function addTLDWarnings() |
|
| 438 | + { |
|
| 439 | + if ($this->warnings[DomainLiteral::CODE]) { |
|
| 440 | + $this->warnings[TLD::CODE] = new TLD(); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | 443 | } |