3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/URI/IPv4.php';
5 require_once 'HTMLPurifier/AttrDef/URI/IPv6.php';
8 * Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
10 class HTMLPurifier_AttrDef_URI_Host
extends HTMLPurifier_AttrDef
14 * Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator
19 * Instance of HTMLPurifier_AttrDef_URI_IPv6 sub-validator
23 function HTMLPurifier_AttrDef_URI_Host() {
24 $this->ipv4
= new HTMLPurifier_AttrDef_URI_IPv4();
25 $this->ipv6
= new HTMLPurifier_AttrDef_URI_IPv6();
28 function validate($string, $config, &$context) {
29 $length = strlen($string);
30 if ($string === '') return '';
31 if ($length > 1 && $string[0] === '[' && $string[$length-1] === ']') {
33 $ip = substr($string, 1, $length - 2);
34 $valid = $this->ipv6
->validate($ip, $config, $context);
35 if ($valid === false) return false;
36 return '['. $valid . ']';
39 // need to do checks on unusual encodings too
40 $ipv4 = $this->ipv4
->validate($string, $config, $context);
41 if ($ipv4 !== false) return $ipv4;
43 // A regular domain name.
45 // This breaks I18N domain names, but we don't have proper IRI support,
46 // so force users to insert Punycode. If there's complaining we'll
47 // try to fix things into an international friendly form.
49 // The productions describing this are:
50 $a = '[a-z]'; // alpha
51 $an = '[a-z0-9]'; // alphanum
52 $and = '[a-z0-9-]'; // alphanum | "-"
53 // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
54 $domainlabel = "$an($and*$an)?";
55 // toplabel = alpha | alpha *( alphanum | "-" ) alphanum
56 $toplabel = "$a($and*$an)?";
57 // hostname = *( domainlabel "." ) toplabel [ "." ]
58 $match = preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string);
59 if (!$match) return false;