@@ -6,6 +6,6 @@ |
||
6 | 6 | |
7 | 7 | class RFCWarnings extends InvalidEmail |
8 | 8 | { |
9 | - const CODE = 997; |
|
10 | - const REASON = 'Warnings were found.'; |
|
9 | + const CODE = 997; |
|
10 | + const REASON = 'Warnings were found.'; |
|
11 | 11 | } |
@@ -11,156 +11,156 @@ |
||
11 | 11 | |
12 | 12 | class DNSCheckValidation implements EmailValidation |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - private $warnings = []; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var InvalidEmail|null |
|
21 | - */ |
|
22 | - private $error; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - private $mxRecords = []; |
|
28 | - |
|
29 | - |
|
30 | - public function __construct() |
|
31 | - { |
|
32 | - if (!function_exists('idn_to_ascii')) { |
|
33 | - throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); |
|
34 | - } |
|
35 | - } |
|
36 | - |
|
37 | - public function isValid($email, EmailLexer $emailLexer) |
|
38 | - { |
|
39 | - // use the input to check DNS if we cannot extract something similar to a domain |
|
40 | - $host = $email; |
|
41 | - |
|
42 | - // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email |
|
43 | - if (false !== $lastAtPos = strrpos($email, '@')) { |
|
44 | - $host = substr($email, $lastAtPos + 1); |
|
45 | - } |
|
46 | - |
|
47 | - // Get the domain parts |
|
48 | - $hostParts = explode('.', $host); |
|
49 | - |
|
50 | - // Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2), |
|
51 | - // mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G) |
|
52 | - $reservedTopLevelDnsNames = [ |
|
53 | - // Reserved Top Level DNS Names |
|
54 | - 'test', |
|
55 | - 'example', |
|
56 | - 'invalid', |
|
57 | - 'localhost', |
|
58 | - |
|
59 | - // mDNS |
|
60 | - 'local', |
|
61 | - |
|
62 | - // Private DNS Namespaces |
|
63 | - 'intranet', |
|
64 | - 'internal', |
|
65 | - 'private', |
|
66 | - 'corp', |
|
67 | - 'home', |
|
68 | - 'lan', |
|
69 | - ]; |
|
70 | - |
|
71 | - $isLocalDomain = count($hostParts) <= 1; |
|
72 | - $isReservedTopLevel = in_array($hostParts[(count($hostParts) - 1)], $reservedTopLevelDnsNames, true); |
|
73 | - |
|
74 | - // Exclude reserved top level DNS names |
|
75 | - if ($isLocalDomain || $isReservedTopLevel) { |
|
76 | - $this->error = new LocalOrReservedDomain(); |
|
77 | - return false; |
|
78 | - } |
|
79 | - |
|
80 | - return $this->checkDns($host); |
|
81 | - } |
|
82 | - |
|
83 | - public function getError() |
|
84 | - { |
|
85 | - return $this->error; |
|
86 | - } |
|
87 | - |
|
88 | - public function getWarnings() |
|
89 | - { |
|
90 | - return $this->warnings; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param string $host |
|
95 | - * |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - protected function checkDns($host) |
|
99 | - { |
|
100 | - $variant = INTL_IDNA_VARIANT_UTS46; |
|
101 | - |
|
102 | - $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.'; |
|
103 | - |
|
104 | - return $this->validateDnsRecords($host); |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - /** |
|
109 | - * Validate the DNS records for given host. |
|
110 | - * |
|
111 | - * @param string $host A set of DNS records in the format returned by dns_get_record. |
|
112 | - * |
|
113 | - * @return bool True on success. |
|
114 | - */ |
|
115 | - private function validateDnsRecords($host) |
|
116 | - { |
|
117 | - // Get all MX, A and AAAA DNS records for host |
|
118 | - // Using @ as workaround to fix https://bugs.php.net/bug.php?id=73149 |
|
119 | - $dnsRecords = @dns_get_record($host, DNS_MX + DNS_A + DNS_AAAA); |
|
120 | - |
|
121 | - |
|
122 | - // No MX, A or AAAA DNS records |
|
123 | - if (empty($dnsRecords)) { |
|
124 | - $this->error = new NoDNSRecord(); |
|
125 | - return false; |
|
126 | - } |
|
127 | - |
|
128 | - // For each DNS record |
|
129 | - foreach ($dnsRecords as $dnsRecord) { |
|
130 | - if (!$this->validateMXRecord($dnsRecord)) { |
|
131 | - return false; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - // No MX records (fallback to A or AAAA records) |
|
136 | - if (empty($this->mxRecords)) { |
|
137 | - $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); |
|
138 | - } |
|
139 | - |
|
140 | - return true; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Validate an MX record |
|
145 | - * |
|
146 | - * @param array $dnsRecord Given DNS record. |
|
147 | - * |
|
148 | - * @return bool True if valid. |
|
149 | - */ |
|
150 | - private function validateMxRecord($dnsRecord) |
|
151 | - { |
|
152 | - if ($dnsRecord['type'] !== 'MX') { |
|
153 | - return true; |
|
154 | - } |
|
155 | - |
|
156 | - // "Null MX" record indicates the domain accepts no mail (https://tools.ietf.org/html/rfc7505) |
|
157 | - if (empty($dnsRecord['target']) || $dnsRecord['target'] === '.') { |
|
158 | - $this->error = new DomainAcceptsNoMail(); |
|
159 | - return false; |
|
160 | - } |
|
161 | - |
|
162 | - $this->mxRecords[] = $dnsRecord; |
|
163 | - |
|
164 | - return true; |
|
165 | - } |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + private $warnings = []; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var InvalidEmail|null |
|
21 | + */ |
|
22 | + private $error; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + private $mxRecords = []; |
|
28 | + |
|
29 | + |
|
30 | + public function __construct() |
|
31 | + { |
|
32 | + if (!function_exists('idn_to_ascii')) { |
|
33 | + throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); |
|
34 | + } |
|
35 | + } |
|
36 | + |
|
37 | + public function isValid($email, EmailLexer $emailLexer) |
|
38 | + { |
|
39 | + // use the input to check DNS if we cannot extract something similar to a domain |
|
40 | + $host = $email; |
|
41 | + |
|
42 | + // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email |
|
43 | + if (false !== $lastAtPos = strrpos($email, '@')) { |
|
44 | + $host = substr($email, $lastAtPos + 1); |
|
45 | + } |
|
46 | + |
|
47 | + // Get the domain parts |
|
48 | + $hostParts = explode('.', $host); |
|
49 | + |
|
50 | + // Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2), |
|
51 | + // mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G) |
|
52 | + $reservedTopLevelDnsNames = [ |
|
53 | + // Reserved Top Level DNS Names |
|
54 | + 'test', |
|
55 | + 'example', |
|
56 | + 'invalid', |
|
57 | + 'localhost', |
|
58 | + |
|
59 | + // mDNS |
|
60 | + 'local', |
|
61 | + |
|
62 | + // Private DNS Namespaces |
|
63 | + 'intranet', |
|
64 | + 'internal', |
|
65 | + 'private', |
|
66 | + 'corp', |
|
67 | + 'home', |
|
68 | + 'lan', |
|
69 | + ]; |
|
70 | + |
|
71 | + $isLocalDomain = count($hostParts) <= 1; |
|
72 | + $isReservedTopLevel = in_array($hostParts[(count($hostParts) - 1)], $reservedTopLevelDnsNames, true); |
|
73 | + |
|
74 | + // Exclude reserved top level DNS names |
|
75 | + if ($isLocalDomain || $isReservedTopLevel) { |
|
76 | + $this->error = new LocalOrReservedDomain(); |
|
77 | + return false; |
|
78 | + } |
|
79 | + |
|
80 | + return $this->checkDns($host); |
|
81 | + } |
|
82 | + |
|
83 | + public function getError() |
|
84 | + { |
|
85 | + return $this->error; |
|
86 | + } |
|
87 | + |
|
88 | + public function getWarnings() |
|
89 | + { |
|
90 | + return $this->warnings; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param string $host |
|
95 | + * |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + protected function checkDns($host) |
|
99 | + { |
|
100 | + $variant = INTL_IDNA_VARIANT_UTS46; |
|
101 | + |
|
102 | + $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.'; |
|
103 | + |
|
104 | + return $this->validateDnsRecords($host); |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + /** |
|
109 | + * Validate the DNS records for given host. |
|
110 | + * |
|
111 | + * @param string $host A set of DNS records in the format returned by dns_get_record. |
|
112 | + * |
|
113 | + * @return bool True on success. |
|
114 | + */ |
|
115 | + private function validateDnsRecords($host) |
|
116 | + { |
|
117 | + // Get all MX, A and AAAA DNS records for host |
|
118 | + // Using @ as workaround to fix https://bugs.php.net/bug.php?id=73149 |
|
119 | + $dnsRecords = @dns_get_record($host, DNS_MX + DNS_A + DNS_AAAA); |
|
120 | + |
|
121 | + |
|
122 | + // No MX, A or AAAA DNS records |
|
123 | + if (empty($dnsRecords)) { |
|
124 | + $this->error = new NoDNSRecord(); |
|
125 | + return false; |
|
126 | + } |
|
127 | + |
|
128 | + // For each DNS record |
|
129 | + foreach ($dnsRecords as $dnsRecord) { |
|
130 | + if (!$this->validateMXRecord($dnsRecord)) { |
|
131 | + return false; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + // No MX records (fallback to A or AAAA records) |
|
136 | + if (empty($this->mxRecords)) { |
|
137 | + $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); |
|
138 | + } |
|
139 | + |
|
140 | + return true; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Validate an MX record |
|
145 | + * |
|
146 | + * @param array $dnsRecord Given DNS record. |
|
147 | + * |
|
148 | + * @return bool True if valid. |
|
149 | + */ |
|
150 | + private function validateMxRecord($dnsRecord) |
|
151 | + { |
|
152 | + if ($dnsRecord['type'] !== 'MX') { |
|
153 | + return true; |
|
154 | + } |
|
155 | + |
|
156 | + // "Null MX" record indicates the domain accepts no mail (https://tools.ietf.org/html/rfc7505) |
|
157 | + if (empty($dnsRecord['target']) || $dnsRecord['target'] === '.') { |
|
158 | + $this->error = new DomainAcceptsNoMail(); |
|
159 | + return false; |
|
160 | + } |
|
161 | + |
|
162 | + $this->mxRecords[] = $dnsRecord; |
|
163 | + |
|
164 | + return true; |
|
165 | + } |
|
166 | 166 | } |
@@ -99,7 +99,7 @@ |
||
99 | 99 | { |
100 | 100 | $variant = INTL_IDNA_VARIANT_UTS46; |
101 | 101 | |
102 | - $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.'; |
|
102 | + $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.').'.'; |
|
103 | 103 | |
104 | 104 | return $this->validateDnsRecords($host); |
105 | 105 | } |
@@ -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 | } |
@@ -27,7 +27,7 @@ |
||
27 | 27 | { |
28 | 28 | $this->parser = new EmailParser($emailLexer); |
29 | 29 | try { |
30 | - $this->parser->parse((string)$email); |
|
30 | + $this->parser->parse((string) $email); |
|
31 | 31 | } catch (InvalidEmail $invalid) { |
32 | 32 | $this->error = $invalid; |
33 | 33 | return false; |
@@ -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 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | throw new CRNoLF(); |
121 | 121 | } |
122 | 122 | |
123 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
123 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
124 | 124 | throw new AtextAfterCFWS(); |
125 | 125 | } |
126 | 126 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | throw new ExpectingCTEXT(); |
129 | 129 | } |
130 | 130 | |
131 | - if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
131 | + if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
132 | 132 | $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
133 | 133 | } else { |
134 | 134 | $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |