2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Validator\Constraints ;
use Symfony\Component\Validator\Context\ExecutionContextInterface ;
use Symfony\Component\Validator\Constraint ;
use Symfony\Component\Validator\ConstraintValidator ;
use Symfony\Component\Validator\Exception\UnexpectedTypeException ;
/**
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
class UrlValidator extends ConstraintValidator
{
const PATTERN = ' ~^
( % s ) :// # protocol
(([ \pL\pN - ] +: ) ? ([ \pL\pN - ] + ) @ ) ? # basic auth
(
2017-02-02 16:28:38 -08:00
([ \pL\pN\pS - \ . ]) + ( \ . ? ([ \pL\pN ] | xn\ - \ - [ \pL\pN - ] + ) + \ . ? ) # a domain name
| # or
\d { 1 , 3 } \ . \d { 1 , 3 } \ . \d { 1 , 3 } \ . \d { 1 , 3 } # an IP address
| # or
2015-08-17 17:00:26 -07:00
\ [
( ? : ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 6 })( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) | ( ? : ( ? : ( ? : ( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ])) \ . ){ 3 }( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ]))))))) | ( ? : ( ? ::: ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 5 })( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ( ? : ( ? : [ 0 - 9 a - f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 ]) ? [ 0 - 9 ]))))))) | ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 0 , 2 }( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) ? :: ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 2 })( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) | ( ? : ( ? : ( ? : ( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ])) \ . ){ 3 }( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ]))))))) | ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 0 , 3 }( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) ? :: ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : )( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) | ( ? : ( ? : ( ? : ( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ])) \ . ){ 3 }( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ]))))))) | ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 0 , 4 }( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) ? :: )( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) | ( ? : ( ? : ( ? : ( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ])) \ . ){ 3 }( ? : ( ? : 25 [ 0 - 5 ] | ( ? : [ 1 - 9 ] | 1 [ 0 - 9 ] | 2 [ 0 - 4 ]) ? [ 0 - 9 ]))))))) | ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 0 , 5 }( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) ? :: )( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) | ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 })) : ){ 0 , 6 }( ? : ( ? : [ 0 - 9 a - f ]{ 1 , 4 }))) ? :: ))))
2017-02-02 16:28:38 -08:00
\ ] # an IPv6 address
2015-08-17 17:00:26 -07:00
)
( : [ 0 - 9 ] + ) ? # a port (optional)
2017-04-13 15:53:35 +01:00
( ? :/ ( ? : [ \pL\pN\ -. _\ ~! $ & \ ' () *+ ,; =:@ ] |%% [ 0 - 9 A - Fa - f ]{ 2 }) * ) * # a path
( ? : \ ? ( ? : [ \pL\pN\ -. _\ ~! $ & \ ' () *+ ,; =:@/ ? ] |%% [ 0 - 9 A - Fa - f ]{ 2 }) * ) ? # a query (optional)
( ? : \ # (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional)
2015-08-17 17:00:26 -07:00
$ ~ ixu ' ;
/**
* { @ inheritdoc }
*/
public function validate ( $value , Constraint $constraint )
{
if ( ! $constraint instanceof Url ) {
throw new UnexpectedTypeException ( $constraint , __NAMESPACE__ . '\Url' );
}
2015-11-17 13:42:33 -08:00
if ( null === $value ) {
2015-08-17 17:00:26 -07:00
return ;
}
if ( ! is_scalar ( $value ) && ! ( is_object ( $value ) && method_exists ( $value , '__toString' ))) {
throw new UnexpectedTypeException ( $value , 'string' );
}
$value = ( string ) $value ;
2015-11-17 13:42:33 -08:00
if ( '' === $value ) {
return ;
}
2015-08-17 17:00:26 -07:00
$pattern = sprintf ( static :: PATTERN , implode ( '|' , $constraint -> protocols ));
if ( ! preg_match ( $pattern , $value )) {
if ( $this -> context instanceof ExecutionContextInterface ) {
$this -> context -> buildViolation ( $constraint -> message )
-> setParameter ( '{{ value }}' , $this -> formatValue ( $value ))
2016-04-20 09:56:34 -07:00
-> setCode ( Url :: INVALID_URL_ERROR )
2015-08-17 17:00:26 -07:00
-> addViolation ();
} else {
$this -> buildViolation ( $constraint -> message )
-> setParameter ( '{{ value }}' , $this -> formatValue ( $value ))
2016-04-20 09:56:34 -07:00
-> setCode ( Url :: INVALID_URL_ERROR )
2015-08-17 17:00:26 -07:00
-> addViolation ();
}
return ;
}
if ( $constraint -> checkDNS ) {
$host = parse_url ( $value , PHP_URL_HOST );
if ( ! checkdnsrr ( $host , 'ANY' )) {
if ( $this -> context instanceof ExecutionContextInterface ) {
$this -> context -> buildViolation ( $constraint -> dnsMessage )
2016-04-20 09:56:34 -07:00
-> setParameter ( '{{ value }}' , $this -> formatValue ( $host ))
-> setCode ( Url :: INVALID_URL_ERROR )
-> addViolation ();
2015-08-17 17:00:26 -07:00
} else {
$this -> buildViolation ( $constraint -> dnsMessage )
2016-04-20 09:56:34 -07:00
-> setParameter ( '{{ value }}' , $this -> formatValue ( $host ))
-> setCode ( Url :: INVALID_URL_ERROR )
-> addViolation ();
2015-08-17 17:00:26 -07:00
}
}
}
}
}