IPv6.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Validates an IPv6 address.
  4. * @author Feyd @ forums.devnetwork.net (public domain)
  5. * @note This function requires brackets to have been removed from address
  6. * in URI.
  7. */
  8. class HTMLPurifier_AttrDef_URI_IPv6 extends HTMLPurifier_AttrDef_URI_IPv4
  9. {
  10. public function validate($aIP, $config, $context) {
  11. if (!$this->ip4) $this->_loadRegex();
  12. $original = $aIP;
  13. $hex = '[0-9a-fA-F]';
  14. $blk = '(?:' . $hex . '{1,4})';
  15. $pre = '(?:/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))'; // /0 - /128
  16. // prefix check
  17. if (strpos($aIP, '/') !== false)
  18. {
  19. if (preg_match('#' . $pre . '$#s', $aIP, $find))
  20. {
  21. $aIP = substr($aIP, 0, 0-strlen($find[0]));
  22. unset($find);
  23. }
  24. else
  25. {
  26. return false;
  27. }
  28. }
  29. // IPv4-compatiblity check
  30. if (preg_match('#(?<=:'.')' . $this->ip4 . '$#s', $aIP, $find))
  31. {
  32. $aIP = substr($aIP, 0, 0-strlen($find[0]));
  33. $ip = explode('.', $find[0]);
  34. $ip = array_map('dechex', $ip);
  35. $aIP .= $ip[0] . $ip[1] . ':' . $ip[2] . $ip[3];
  36. unset($find, $ip);
  37. }
  38. // compression check
  39. $aIP = explode('::', $aIP);
  40. $c = count($aIP);
  41. if ($c > 2)
  42. {
  43. return false;
  44. }
  45. elseif ($c == 2)
  46. {
  47. list($first, $second) = $aIP;
  48. $first = explode(':', $first);
  49. $second = explode(':', $second);
  50. if (count($first) + count($second) > 8)
  51. {
  52. return false;
  53. }
  54. while(count($first) < 8)
  55. {
  56. array_push($first, '0');
  57. }
  58. array_splice($first, 8 - count($second), 8, $second);
  59. $aIP = $first;
  60. unset($first,$second);
  61. }
  62. else
  63. {
  64. $aIP = explode(':', $aIP[0]);
  65. }
  66. $c = count($aIP);
  67. if ($c != 8)
  68. {
  69. return false;
  70. }
  71. // All the pieces should be 16-bit hex strings. Are they?
  72. foreach ($aIP as $piece)
  73. {
  74. if (!preg_match('#^[0-9a-fA-F]{4}$#s', sprintf('%04s', $piece)))
  75. {
  76. return false;
  77. }
  78. }
  79. return $original;
  80. }
  81. }
  82. // vim: et sw=4 sts=4