8000 [Validator] Added a check DNS option on URL validator · symfony/symfony@e7516db · GitHub
[go: up one dir, main page]

Skip to content

Commit e7516db

Browse files
committed
[Validator] Added a check DNS option on URL validator
1 parent 5fc4f27 commit e7516db

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/Validator/Constraints/Url.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
*/
2424
class Url extends Constraint
2525
{
26+
const HOST_CHECK_FAILED_ERROR = 1;
27+
28+
protected static $errorNames = array(
29+
self::HOST_CHECK_FAILED_ERROR => 'HOST_CHECK_FAILED_ERROR',
30+
);
31+
2632
public $message = 'This value is not a valid URL.';
2733
public $protocols = array('http', 'https');
34+
public $checkDNS = false;
2835
}

src/Symfony/Component/Validator/Constraints/UrlValidator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ public function validate($value, Constraint $constraint)
6262
$this->buildViolation($constraint->message)
6363
->setParameter('{{ value }}', $this->formatValue($value))
6464
->addViolation();
65+
66+
return;
67+
}
68+
69+
$host = parse_url($value, PHP_URL_HOST);
70+
71+
if ($constraint->checkDNS && !$this->checkHost($host)) {
72+
$this->buildViolation($constraint->message)
73+
->setParameter('{{ value }}', $this->formatValue($host))
74+
->setCode(Url::HOST_CHECK_FAILED_ERROR)
75+
->addViolation();
6576
}
6677
}
78+
79+
private function checkHost($host)
80+
{
81+
return checkdnsrr($host, "A") || checkdnsrr($host, "AAAA");
82+
}
6783
}

0 commit comments

Comments
 (0)
0