Whoops, forgot to edit WHATSNEW
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / URI / Host.php
blobe54a3344a74ffdff81562babdd7a898d4f05e836
1 <?php
3 /**
4 * Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
5 */
6 class HTMLPurifier_AttrDef_URI_Host extends HTMLPurifier_AttrDef
9 /**
10 * IPv4 sub-validator.
11 * @type HTMLPurifier_AttrDef_URI_IPv4
13 protected $ipv4;
15 /**
16 * IPv6 sub-validator.
17 * @type HTMLPurifier_AttrDef_URI_IPv6
19 protected $ipv6;
21 public function __construct()
23 $this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
24 $this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
27 /**
28 * @param string $string
29 * @param HTMLPurifier_Config $config
30 * @param HTMLPurifier_Context $context
31 * @return bool|string
33 public function validate($string, $config, $context)
35 $length = strlen($string);
36 // empty hostname is OK; it's usually semantically equivalent:
37 // the default host as defined by a URI scheme is used:
39 // If the URI scheme defines a default for host, then that
40 // default applies when the host subcomponent is undefined
41 // or when the registered name is empty (zero length).
42 if ($string === '') {
43 return '';
45 if ($length > 1 && $string[0] === '[' && $string[$length - 1] === ']') {
46 //IPv6
47 $ip = substr($string, 1, $length - 2);
48 $valid = $this->ipv6->validate($ip, $config, $context);
49 if ($valid === false) {
50 return false;
52 return '[' . $valid . ']';
55 // need to do checks on unusual encodings too
56 $ipv4 = $this->ipv4->validate($string, $config, $context);
57 if ($ipv4 !== false) {
58 return $ipv4;
61 // A regular domain name.
63 // This doesn't match I18N domain names, but we don't have proper IRI support,
64 // so force users to insert Punycode.
66 // There is not a good sense in which underscores should be
67 // allowed, since it's technically not! (And if you go as
68 // far to allow everything as specified by the DNS spec...
69 // well, that's literally everything, modulo some space limits
70 // for the components and the overall name (which, by the way,
71 // we are NOT checking!). So we (arbitrarily) decide this:
72 // let's allow underscores wherever we would have allowed
73 // hyphens, if they are enabled. This is a pretty good match
74 // for browser behavior, for example, a large number of browsers
75 // cannot handle foo_.example.com, but foo_bar.example.com is
76 // fairly well supported.
77 $underscore = $config->get('Core.AllowHostnameUnderscore') ? '_' : '';
79 // Based off of RFC 1738, but amended so that
80 // as per RFC 3696, the top label need only not be all numeric.
81 // The productions describing this are:
82 $a = '[a-z]'; // alpha
83 $an = '[a-z0-9]'; // alphanum
84 $and = "[a-z0-9-$underscore]"; // alphanum | "-"
85 // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
86 $domainlabel = "$an(?:$and*$an)?";
87 // AMENDED as per RFC 3696
88 // toplabel = alphanum | alphanum *( alphanum | "-" ) alphanum
89 // side condition: not all numeric
90 $toplabel = "$an(?:$and*$an)?";
91 // hostname = *( domainlabel "." ) toplabel [ "." ]
92 if (preg_match("/^(?:$domainlabel\.)*($toplabel)\.?$/i", $string, $matches)) {
93 if (!ctype_digit($matches[1])) {
94 return $string;
98 // PHP 5.3 and later support this functionality natively
99 if (function_exists('idn_to_ascii')) {
100 $string = idn_to_ascii($string, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
102 // If we have Net_IDNA2 support, we can support IRIs by
103 // punycoding them. (This is the most portable thing to do,
104 // since otherwise we have to assume browsers support
105 } elseif ($config->get('Core.EnableIDNA')) {
106 $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true));
107 // we need to encode each period separately
108 $parts = explode('.', $string);
109 try {
110 $new_parts = array();
111 foreach ($parts as $part) {
112 $encodable = false;
113 for ($i = 0, $c = strlen($part); $i < $c; $i++) {
114 if (ord($part[$i]) > 0x7a) {
115 $encodable = true;
116 break;
119 if (!$encodable) {
120 $new_parts[] = $part;
121 } else {
122 $new_parts[] = $idna->encode($part);
125 $string = implode('.', $new_parts);
126 } catch (Exception $e) {
127 // XXX error reporting
130 // Try again
131 if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
132 return $string;
134 return false;
138 // vim: et sw=4 sts=4