4 * Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
6 class HTMLPurifier_AttrDef_URI_Host
extends HTMLPurifier_AttrDef
10 * Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator
15 * Instance of HTMLPurifier_AttrDef_URI_IPv6 sub-validator
19 public function __construct() {
20 $this->ipv4
= new HTMLPurifier_AttrDef_URI_IPv4();
21 $this->ipv6
= new HTMLPurifier_AttrDef_URI_IPv6();
24 public function validate($string, $config, $context) {
25 $length = strlen($string);
26 // empty hostname is OK; it's usually semantically equivalent:
27 // the default host as defined by a URI scheme is used:
29 // If the URI scheme defines a default for host, then that
30 // default applies when the host subcomponent is undefined
31 // or when the registered name is empty (zero length).
32 if ($string === '') return '';
33 if ($length > 1 && $string[0] === '[' && $string[$length-1] === ']') {
35 $ip = substr($string, 1, $length - 2);
36 $valid = $this->ipv6
->validate($ip, $config, $context);
37 if ($valid === false) return false;
38 return '['. $valid . ']';
41 // need to do checks on unusual encodings too
42 $ipv4 = $this->ipv4
->validate($string, $config, $context);
43 if ($ipv4 !== false) return $ipv4;
45 // A regular domain name.
47 // This breaks I18N domain names, but we don't have proper IRI support,
48 // so force users to insert Punycode. If there's complaining we'll
49 // try to fix things into an international friendly form.
51 // The productions describing this are:
52 $a = '[a-z]'; // alpha
53 $an = '[a-z0-9]'; // alphanum
54 $and = '[a-z0-9-]'; // alphanum | "-"
55 // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
56 $domainlabel = "$an($and*$an)?";
57 // toplabel = alpha | alpha *( alphanum | "-" ) alphanum
58 $toplabel = "$a($and*$an)?";
59 // hostname = *( domainlabel "." ) toplabel [ "." ]
60 $match = preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string);
61 if (!$match) return false;