Host.php 5.3 KB

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