Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

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