1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Parser; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Lexer\Token; |
6
|
|
|
use Egulias\EmailValidator\EmailLexer; |
7
|
|
|
use Egulias\EmailValidator\Warning\TLD; |
8
|
|
|
use Egulias\EmailValidator\Result\Result; |
9
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
10
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
11
|
|
|
use Egulias\EmailValidator\Result\Reason\DotAtEnd; |
12
|
|
|
use Egulias\EmailValidator\Result\Reason\DotAtStart; |
13
|
|
|
use Egulias\EmailValidator\Warning\DeprecatedComment; |
14
|
|
|
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd; |
15
|
|
|
use Egulias\EmailValidator\Result\Reason\LabelTooLong; |
16
|
|
|
use Egulias\EmailValidator\Result\Reason\NoDomainPart; |
17
|
|
|
use Egulias\EmailValidator\Result\Reason\ConsecutiveAt; |
18
|
|
|
use Egulias\EmailValidator\Result\Reason\DomainTooLong; |
19
|
|
|
use Egulias\EmailValidator\Result\Reason\CharNotAllowed; |
20
|
|
|
use Egulias\EmailValidator\Result\Reason\DomainHyphened; |
21
|
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT; |
22
|
|
|
use Egulias\EmailValidator\Parser\CommentStrategy\DomainComment; |
23
|
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingDomainLiteralClose; |
24
|
|
|
use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser; |
25
|
|
|
|
26
|
|
|
class DomainPart extends PartParser |
27
|
|
|
{ |
28
|
|
|
public const DOMAIN_MAX_LENGTH = 253; |
29
|
|
|
public const LABEL_MAX_LENGTH = 63; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $domainPart = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $label = ''; |
40
|
|
|
|
41
|
148 |
|
public function parse(): Result |
42
|
|
|
{ |
43
|
148 |
|
$this->lexer->clearRecorded(); |
44
|
148 |
|
$this->lexer->startRecording(); |
45
|
|
|
|
46
|
148 |
|
$this->lexer->moveNext(); |
47
|
|
|
|
48
|
148 |
|
$domainChecks = $this->performDomainStartChecks(); |
49
|
148 |
|
if ($domainChecks->isInvalid()) { |
50
|
12 |
|
return $domainChecks; |
51
|
|
|
} |
52
|
|
|
|
53
|
136 |
|
if ($this->lexer->current->isA(EmailLexer::S_AT)) { |
|
|
|
|
54
|
1 |
|
return new InvalidEmail(new ConsecutiveAt(), $this->lexer->current->value); |
55
|
|
|
} |
56
|
|
|
|
57
|
135 |
|
$result = $this->doParseDomainPart(); |
58
|
135 |
|
if ($result->isInvalid()) { |
59
|
67 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
68 |
|
$end = $this->checkEndOfDomain(); |
63
|
68 |
|
if ($end->isInvalid()) { |
64
|
4 |
|
return $end; |
65
|
|
|
} |
66
|
|
|
|
67
|
64 |
|
$this->lexer->stopRecording(); |
68
|
64 |
|
$this->domainPart = $this->lexer->getAccumulatedValues(); |
69
|
|
|
|
70
|
64 |
|
$length = strlen($this->domainPart); |
71
|
64 |
|
if ($length > self::DOMAIN_MAX_LENGTH) { |
72
|
|
|
return new InvalidEmail(new DomainTooLong(), $this->lexer->current->value); |
73
|
|
|
} |
74
|
|
|
|
75
|
64 |
|
return new ValidEmail(); |
76
|
|
|
} |
77
|
|
|
|
78
|
68 |
|
private function checkEndOfDomain(): Result |
79
|
|
|
{ |
80
|
68 |
|
$prev = $this->lexer->getPrevious(); |
81
|
68 |
|
if ($prev->isA(EmailLexer::S_DOT)) { |
|
|
|
|
82
|
3 |
|
return new InvalidEmail(new DotAtEnd(), $this->lexer->current->value); |
83
|
|
|
} |
84
|
65 |
|
if ($prev->isA(EmailLexer::S_HYPHEN)) { |
85
|
1 |
|
return new InvalidEmail(new DomainHyphened('Hypen found at the end of the domain'), $prev->value); |
86
|
|
|
} |
87
|
|
|
|
88
|
64 |
|
if ($this->lexer->current->isA(EmailLexer::S_SP)) { |
89
|
|
|
return new InvalidEmail(new CRLFAtTheEnd(), $prev->value); |
90
|
|
|
} |
91
|
64 |
|
return new ValidEmail(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function performDomainStartChecks(): Result |
95
|
148 |
|
{ |
96
|
|
|
$invalidTokens = $this->checkInvalidTokensAfterAT(); |
97
|
148 |
|
if ($invalidTokens->isInvalid()) { |
98
|
148 |
|
return $invalidTokens; |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
$missingDomain = $this->checkEmptyDomain(); |
102
|
146 |
|
if ($missingDomain->isInvalid()) { |
103
|
146 |
|
return $missingDomain; |
104
|
10 |
|
} |
105
|
|
|
|
106
|
|
|
if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) { |
|
|
|
|
107
|
136 |
|
$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); |
108
|
3 |
|
} |
109
|
|
|
return new ValidEmail(); |
110
|
136 |
|
} |
111
|
|
|
|
112
|
|
|
private function checkEmptyDomain(): Result |
113
|
146 |
|
{ |
114
|
|
|
$thereIsNoDomain = $this->lexer->current->isA(EmailLexer::S_EMPTY) || |
115
|
146 |
|
($this->lexer->current->isA(EmailLexer::S_SP) && |
|
|
|
|
116
|
146 |
|
!$this->lexer->isNextToken(EmailLexer::GENERIC)); |
117
|
146 |
|
|
118
|
|
|
if ($thereIsNoDomain) { |
119
|
146 |
|
return new InvalidEmail(new NoDomainPart(), $this->lexer->current->value); |
120
|
10 |
|
} |
121
|
|
|
|
122
|
|
|
return new ValidEmail(); |
123
|
136 |
|
} |
124
|
|
|
|
125
|
|
|
private function checkInvalidTokensAfterAT(): Result |
126
|
148 |
|
{ |
127
|
|
|
if ($this->lexer->current->isA(EmailLexer::S_DOT)) { |
|
|
|
|
128
|
148 |
|
return new InvalidEmail(new DotAtStart(), $this->lexer->current->value); |
129
|
1 |
|
} |
130
|
|
|
if ($this->lexer->current->isA(EmailLexer::S_HYPHEN)) { |
131
|
147 |
|
return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->current->value); |
132
|
1 |
|
} |
133
|
|
|
return new ValidEmail(); |
134
|
146 |
|
} |
135
|
|
|
|
136
|
|
|
protected function parseComments(): Result |
137
|
7 |
|
{ |
138
|
|
|
$commentParser = new Comment($this->lexer, new DomainComment()); |
139
|
7 |
|
$result = $commentParser->parse(); |
140
|
7 |
|
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings()); |
141
|
7 |
|
|
142
|
|
|
return $result; |
143
|
7 |
|
} |
144
|
|
|
|
145
|
|
|
protected function doParseDomainPart(): Result |
146
|
135 |
|
{ |
147
|
|
|
$tldMissing = true; |
148
|
135 |
|
$hasComments = false; |
149
|
135 |
|
$domain = ''; |
150
|
135 |
|
do { |
151
|
|
|
$prev = $this->lexer->getPrevious(); |
152
|
135 |
|
|
153
|
|
|
$notAllowedChars = $this->checkNotAllowedChars($this->lexer->current); |
154
|
135 |
|
if ($notAllowedChars->isInvalid()) { |
155
|
135 |
|
return $notAllowedChars; |
156
|
4 |
|
} |
157
|
|
|
|
158
|
|
|
if ( |
159
|
135 |
|
$this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS) || |
|
|
|
|
160
|
135 |
|
$this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS) |
161
|
7 |
|
) { |
162
|
7 |
|
$hasComments = true; |
163
|
|
|
$commentsResult = $this->parseComments(); |
164
|
|
|
|
165
|
7 |
|
//Invalid comment parsing |
166
|
5 |
|
if ($commentsResult->isInvalid()) { |
167
|
|
|
return $commentsResult; |
168
|
|
|
} |
169
|
|
|
} |
170
|
132 |
|
|
171
|
132 |
|
$dotsResult = $this->checkConsecutiveDots(); |
172
|
2 |
|
if ($dotsResult->isInvalid()) { |
173
|
|
|
return $dotsResult; |
174
|
|
|
} |
175
|
132 |
|
|
176
|
18 |
|
if ($this->lexer->current->isA(EmailLexer::S_OPENBRACKET)) { |
177
|
|
|
$literalResult = $this->parseDomainLiteral(); |
178
|
18 |
|
|
179
|
18 |
|
$this->addTLDWarnings($tldMissing); |
180
|
|
|
return $literalResult; |
181
|
|
|
} |
182
|
114 |
|
|
183
|
114 |
|
$labelCheck = $this->checkLabelLength(); |
184
|
5 |
|
if ($labelCheck->isInvalid()) { |
185
|
|
|
return $labelCheck; |
186
|
|
|
} |
187
|
114 |
|
|
188
|
114 |
|
$FwsResult = $this->parseFWS(); |
189
|
5 |
|
if ($FwsResult->isInvalid()) { |
190
|
|
|
return $FwsResult; |
191
|
|
|
} |
192
|
114 |
|
|
193
|
|
|
$domain .= $this->lexer->current->value; |
194
|
114 |
|
|
195
|
56 |
|
if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::GENERIC)) { |
196
|
|
|
$tldMissing = false; |
197
|
|
|
} |
198
|
114 |
|
|
199
|
114 |
|
$exceptionsResult = $this->checkDomainPartExceptions($prev, $hasComments); |
200
|
41 |
|
if ($exceptionsResult->isInvalid()) { |
201
|
|
|
return $exceptionsResult; |
202
|
112 |
|
} |
203
|
|
|
$this->lexer->moveNext(); |
204
|
112 |
|
} while (!$this->lexer->current->isA(EmailLexer::S_EMPTY)); |
205
|
|
|
|
206
|
55 |
|
$labelCheck = $this->checkLabelLength(true); |
207
|
55 |
|
if ($labelCheck->isInvalid()) { |
208
|
2 |
|
return $labelCheck; |
209
|
|
|
} |
210
|
53 |
|
$this->addTLDWarnings($tldMissing); |
211
|
|
|
|
212
|
53 |
|
$this->domainPart = $domain; |
213
|
53 |
|
return new ValidEmail(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param Token<int, string> $token |
218
|
|
|
* |
219
|
135 |
|
* @return Result |
220
|
|
|
*/ |
221
|
135 |
|
private function checkNotAllowedChars(Token $token): Result |
222
|
135 |
|
{ |
223
|
4 |
|
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH => true]; |
224
|
|
|
if (isset($notAllowed[$token->type])) { |
225
|
135 |
|
return new InvalidEmail(new CharNotAllowed(), $token->value); |
226
|
|
|
} |
227
|
|
|
return new ValidEmail(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
18 |
|
* @return Result |
232
|
|
|
*/ |
233
|
|
|
protected function parseDomainLiteral(): Result |
234
|
18 |
|
{ |
235
|
|
|
try { |
236
|
|
|
$this->lexer->find(EmailLexer::S_CLOSEBRACKET); |
237
|
|
|
} catch (\RuntimeException $e) { |
238
|
|
|
return new InvalidEmail(new ExpectingDomainLiteralClose(), $this->lexer->current->value); |
239
|
18 |
|
} |
240
|
18 |
|
|
241
|
18 |
|
$domainLiteralParser = new DomainLiteralParser($this->lexer); |
242
|
18 |
|
$result = $domainLiteralParser->parse(); |
243
|
|
|
$this->warnings = array_merge($this->warnings, $domainLiteralParser->getWarnings()); |
244
|
|
|
return $result; |
245
|
114 |
|
} |
246
|
|
|
|
247
|
114 |
|
/** |
248
|
|
|
* @param Token<int, string> $prev |
249
|
|
|
* @param bool $hasComments |
250
|
|
|
* |
251
|
114 |
|
* @return Result |
252
|
1 |
|
*/ |
253
|
|
|
protected function checkDomainPartExceptions(Token $prev, bool $hasComments): Result |
254
|
|
|
{ |
255
|
114 |
|
if ($this->lexer->current->isA(EmailLexer::S_OPENBRACKET) && $prev->type !== EmailLexer::S_AT) { |
|
|
|
|
256
|
114 |
|
return new InvalidEmail(new ExpectingATEXT('OPENBRACKET not after AT'), $this->lexer->current->value); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if ($this->lexer->current->isA(EmailLexer::S_HYPHEN) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
260
|
114 |
|
return new InvalidEmail(new DomainHyphened('Hypen found near DOT'), $this->lexer->current->value); |
261
|
|
|
} |
262
|
|
|
|
263
|
108 |
|
if ( |
264
|
|
|
$this->lexer->current->isA(EmailLexer::S_BACKSLASH) |
265
|
108 |
|
&& $this->lexer->isNextToken(EmailLexer::GENERIC) |
266
|
108 |
|
) { |
267
|
108 |
|
return new InvalidEmail(new ExpectingATEXT('Escaping following "ATOM"'), $this->lexer->current->value); |
268
|
108 |
|
} |
269
|
108 |
|
|
270
|
|
|
return $this->validateTokens($hasComments); |
271
|
108 |
|
} |
272
|
2 |
|
|
273
|
2 |
|
protected function validateTokens(bool $hasComments): Result |
274
|
|
|
{ |
275
|
|
|
$validDomainTokens = array( |
276
|
108 |
|
EmailLexer::GENERIC => true, |
277
|
40 |
|
EmailLexer::S_HYPHEN => true, |
278
|
|
|
EmailLexer::S_DOT => true, |
279
|
|
|
); |
280
|
106 |
|
|
281
|
|
|
if ($hasComments) { |
282
|
|
|
$validDomainTokens[EmailLexer::S_OPENPARENTHESIS] = true; |
283
|
114 |
|
$validDomainTokens[EmailLexer::S_CLOSEPARENTHESIS] = true; |
284
|
|
|
} |
285
|
114 |
|
|
286
|
72 |
|
if (!isset($validDomainTokens[$this->lexer->current->type])) { |
287
|
7 |
|
return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->current->value), $this->lexer->current->value); |
288
|
|
|
} |
289
|
66 |
|
|
290
|
|
|
return new ValidEmail(); |
291
|
114 |
|
} |
292
|
114 |
|
|
293
|
|
|
private function checkLabelLength(bool $isEndOfDomain = false): Result |
294
|
|
|
{ |
295
|
|
|
if ($this->lexer->current->isA(EmailLexer::S_DOT) || $isEndOfDomain) { |
|
|
|
|
296
|
72 |
|
if ($this->isLabelTooLong($this->label)) { |
297
|
|
|
return new InvalidEmail(new LabelTooLong(), $this->lexer->current->value); |
298
|
72 |
|
} |
299
|
10 |
|
$this->label = ''; |
300
|
10 |
|
} |
301
|
|
|
$this->label .= $this->lexer->current->value; |
302
|
66 |
|
return new ValidEmail(); |
303
|
|
|
} |
304
|
|
|
|
305
|
71 |
|
|
306
|
|
|
private function isLabelTooLong(string $label): bool |
307
|
71 |
|
{ |
308
|
26 |
|
if (preg_match('/[^\x00-\x7F]/', $label)) { |
309
|
|
|
idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
310
|
|
|
return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG); |
311
|
|
|
} |
312
|
148 |
|
return strlen($label) > self::LABEL_MAX_LENGTH; |
313
|
|
|
} |
314
|
148 |
|
|
315
|
|
|
private function addTLDWarnings(bool $isTLDMissing): void |
316
|
|
|
{ |
317
|
|
|
if ($isTLDMissing) { |
318
|
|
|
$this->warnings[TLD::CODE] = new TLD(); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function domainPart(): string |
323
|
|
|
{ |
324
|
|
|
return $this->domainPart; |
325
|
|
|
} |
326
|
|
|
} |