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
(
([ \pL\pN\pS - \ . ]) + ( \ . ? ([ \pL ] | xn\ - \ - [ \pL\pN - ] + ) + \ . ? ) # a domain name
| # or
\d { 1 , 3 } \ . \d { 1 , 3 } \ . \d { 1 , 3 } \ . \d { 1 , 3 } # a IP address
| # or
\ [
( ? : ( ? : ( ? : ( ? : ( ? : ( ? : ( ? : [ 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 ]{ 1 , 4 }))) | ( ? : ( ? : ( ? : ( ? : ( ?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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 }))) ? :: ))))
\ ] # a IPv6 address
)
( : [ 0 - 9 ] + ) ? # a port (optional)
2015-11-17 13:42:33 -08:00
( / ? |/ \S +| \ ? | \ #) # a /, nothing, a / with something, a query or a fragment
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 ))
-> addViolation ();
} else {
$this -> buildViolation ( $constraint -> message )
-> setParameter ( '{{ value }}' , $this -> formatValue ( $value ))
-> 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 )
-> setParameter ( '{{ value }}' , $this -> formatValue ( $host ))
-> addViolation ();
} else {
$this -> buildViolation ( $constraint -> dnsMessage )
-> setParameter ( '{{ value }}' , $this -> formatValue ( $host ))
-> addViolation ();
}
}
}
}
}