1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Form\Validator; |
15
|
|
|
|
16
|
|
|
use Eccube\Validator\EmailValidator\NoRFCEmailValidator; |
17
|
|
|
use Symfony\Component\Validator\Constraint; |
18
|
|
|
use Symfony\Component\Validator\Constraints\EmailValidator as BaseEmailValidator; |
19
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
20
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* フォームで使用するEmailのバリデータ. |
24
|
|
|
* |
25
|
|
|
* eccube_rfc_email_checkがtrueの場合, Symfony\Component\Validator\Constraints\EmailValidatorを使用してチェックを行います. |
26
|
|
|
* falseの場合は, Eccube\Validator\EmailValidator\NoRFCEmailValidatorを使用してチェックを行います. |
27
|
|
|
* NoRFCEmailValidatorは, 日本のキャリアメールで使用されていた, ..や.@の形式を許容します. |
28
|
|
|
*/ |
29
|
|
|
class EmailValidator extends ConstraintValidator |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function validate($value, Constraint $constraint) |
35
|
|
|
{ |
36
|
|
|
if (!$constraint instanceof Email) { |
37
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($constraint->strict) { |
41
|
|
|
$baseEmailValidator = new BaseEmailValidator($constraint->strict); |
42
|
|
|
$baseEmailValidator->initialize($this->context); |
43
|
|
|
$baseEmailValidator->validate($value, $constraint); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (null === $value || '' === $value) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { |
53
|
|
|
throw new UnexpectedTypeException($value, 'string'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$value = (string) $value; |
57
|
|
|
|
58
|
|
|
$noRfcValidator = new NoRFCEmailValidator(); |
59
|
|
View Code Duplication |
if (!$noRfcValidator->isValid($value)) { |
|
|
|
|
60
|
|
|
$this->context->buildViolation($constraint->message) |
61
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
62
|
|
|
->setCode(Email::INVALID_FORMAT_ERROR) |
63
|
|
|
->addViolation(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$host = (string) substr($value, strrpos($value, '@') + 1); |
67
|
|
|
|
68
|
|
|
// Check for host DNS resource records |
69
|
|
View Code Duplication |
if ($constraint->checkMX) { |
|
|
|
|
70
|
|
|
if (!$this->checkMX($host)) { |
71
|
|
|
$this->context->buildViolation($constraint->message) |
72
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
73
|
|
|
->setCode(Email::MX_CHECK_FAILED_ERROR) |
74
|
|
|
->addViolation(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
if ($constraint->checkHost && !$this->checkHost($host)) { |
|
|
|
|
81
|
|
|
$this->context->buildViolation($constraint->message) |
82
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
83
|
|
|
->setCode(Email::HOST_CHECK_FAILED_ERROR) |
84
|
|
|
->addViolation(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check DNS Records for MX type. |
90
|
|
|
* |
91
|
|
|
* @param string $host Host |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
private function checkMX($host) |
96
|
|
|
{ |
97
|
|
|
return '' !== $host && checkdnsrr($host, 'MX'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check if one of MX, A or AAAA DNS RR exists. |
102
|
|
|
* |
103
|
|
|
* @param string $host Host |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
private function checkHost($host) |
108
|
|
|
{ |
109
|
|
|
return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'))); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.