Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

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