File indexing completed on 2025-01-26 05:29:06

0001 <?php
0002 
0003 /**
0004  * Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
0005  */
0006 class HTMLPurifier_AttrDef_URI_Host extends HTMLPurifier_AttrDef
0007 {
0008 
0009     /**
0010      * IPv4 sub-validator.
0011      * @type HTMLPurifier_AttrDef_URI_IPv4
0012      */
0013     protected $ipv4;
0014 
0015     /**
0016      * IPv6 sub-validator.
0017      * @type HTMLPurifier_AttrDef_URI_IPv6
0018      */
0019     protected $ipv6;
0020 
0021     public function __construct()
0022     {
0023         $this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
0024         $this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
0025     }
0026 
0027     /**
0028      * @param string $string
0029      * @param HTMLPurifier_Config $config
0030      * @param HTMLPurifier_Context $context
0031      * @return bool|string
0032      */
0033     public function validate($string, $config, $context)
0034     {
0035         $length = strlen($string);
0036         // empty hostname is OK; it's usually semantically equivalent:
0037         // the default host as defined by a URI scheme is used:
0038         //
0039         //      If the URI scheme defines a default for host, then that
0040         //      default applies when the host subcomponent is undefined
0041         //      or when the registered name is empty (zero length).
0042         if ($string === '') {
0043             return '';
0044         }
0045         if ($length > 1 && $string[0] === '[' && $string[$length - 1] === ']') {
0046             //IPv6
0047             $ip = substr($string, 1, $length - 2);
0048             $valid = $this->ipv6->validate($ip, $config, $context);
0049             if ($valid === false) {
0050                 return false;
0051             }
0052             return '[' . $valid . ']';
0053         }
0054 
0055         // need to do checks on unusual encodings too
0056         $ipv4 = $this->ipv4->validate($string, $config, $context);
0057         if ($ipv4 !== false) {
0058             return $ipv4;
0059         }
0060 
0061         // A regular domain name.
0062 
0063         // This doesn't match I18N domain names, but we don't have proper IRI support,
0064         // so force users to insert Punycode.
0065 
0066         // There is not a good sense in which underscores should be
0067         // allowed, since it's technically not! (And if you go as
0068         // far to allow everything as specified by the DNS spec...
0069         // well, that's literally everything, modulo some space limits
0070         // for the components and the overall name (which, by the way,
0071         // we are NOT checking!).  So we (arbitrarily) decide this:
0072         // let's allow underscores wherever we would have allowed
0073         // hyphens, if they are enabled.  This is a pretty good match
0074         // for browser behavior, for example, a large number of browsers
0075         // cannot handle foo_.example.com, but foo_bar.example.com is
0076         // fairly well supported.
0077         $underscore = $config->get('Core.AllowHostnameUnderscore') ? '_' : '';
0078 
0079         // Based off of RFC 1738, but amended so that
0080         // as per RFC 3696, the top label need only not be all numeric.
0081         // The productions describing this are:
0082         $a   = '[a-z]';     // alpha
0083         $an  = '[a-z0-9]';  // alphanum
0084         $and = "[a-z0-9-$underscore]"; // alphanum | "-"
0085         // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
0086         $domainlabel = "$an(?:$and*$an)?";
0087         // AMENDED as per RFC 3696
0088         // toplabel    = alphanum | alphanum *( alphanum | "-" ) alphanum
0089         //      side condition: not all numeric
0090         $toplabel = "$an(?:$and*$an)?";
0091         // hostname    = *( domainlabel "." ) toplabel [ "." ]
0092         if (preg_match("/^(?:$domainlabel\.)*($toplabel)\.?$/i", $string, $matches)) {
0093             if (!ctype_digit($matches[1])) {
0094                 return $string;
0095             }
0096         }
0097 
0098         // PHP 5.3 and later support this functionality natively
0099         if (function_exists('idn_to_ascii')) {
0100             $string = idn_to_ascii($string);
0101 
0102         // If we have Net_IDNA2 support, we can support IRIs by
0103         // punycoding them. (This is the most portable thing to do,
0104         // since otherwise we have to assume browsers support
0105         } elseif ($config->get('Core.EnableIDNA')) {
0106             $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true));
0107             // we need to encode each period separately
0108             $parts = explode('.', $string);
0109             try {
0110                 $new_parts = array();
0111                 foreach ($parts as $part) {
0112                     $encodable = false;
0113                     for ($i = 0, $c = strlen($part); $i < $c; $i++) {
0114                         if (ord($part[$i]) > 0x7a) {
0115                             $encodable = true;
0116                             break;
0117                         }
0118                     }
0119                     if (!$encodable) {
0120                         $new_parts[] = $part;
0121                     } else {
0122                         $new_parts[] = $idna->encode($part);
0123                     }
0124                 }
0125                 $string = implode('.', $new_parts);
0126             } catch (Exception $e) {
0127                 // XXX error reporting
0128             }
0129         }
0130         // Try again
0131         if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
0132             return $string;
0133         }
0134         return false;
0135     }
0136 }
0137 
0138 // vim: et sw=4 sts=4