2006-08-17 01:05:35 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates an IPv6 address.
|
|
|
|
* @author Feyd @ forums.devnetwork.net (public domain)
|
|
|
|
* @note This function requires brackets to have been removed from address
|
|
|
|
* in URI.
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_URI_IPv6 extends HTMLPurifier_AttrDef_URI_IPv4
|
2006-08-17 01:05:35 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($aIP, $config, $context) {
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
if (!$this->ip4) $this->_loadRegex();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
$original = $aIP;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
$hex = '[0-9a-fA-F]';
|
|
|
|
$blk = '(?:' . $hex . '{1,4})';
|
|
|
|
$pre = '(?:/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))'; // /0 - /128
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
// prefix check
|
|
|
|
if (strpos($aIP, '/') !== false)
|
|
|
|
{
|
|
|
|
if (preg_match('#' . $pre . '$#s', $aIP, $find))
|
|
|
|
{
|
|
|
|
$aIP = substr($aIP, 0, 0-strlen($find[0]));
|
|
|
|
unset($find);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
|
|
|
// IPv4-compatiblity check
|
2006-08-17 01:05:35 +00:00
|
|
|
if (preg_match('#(?<=:'.')' . $this->ip4 . '$#s', $aIP, $find))
|
|
|
|
{
|
|
|
|
$aIP = substr($aIP, 0, 0-strlen($find[0]));
|
|
|
|
$ip = explode('.', $find[0]);
|
|
|
|
$ip = array_map('dechex', $ip);
|
|
|
|
$aIP .= $ip[0] . $ip[1] . ':' . $ip[2] . $ip[3];
|
|
|
|
unset($find, $ip);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
// compression check
|
|
|
|
$aIP = explode('::', $aIP);
|
|
|
|
$c = count($aIP);
|
|
|
|
if ($c > 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
elseif ($c == 2)
|
|
|
|
{
|
|
|
|
list($first, $second) = $aIP;
|
|
|
|
$first = explode(':', $first);
|
|
|
|
$second = explode(':', $second);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
if (count($first) + count($second) > 8)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
while(count($first) < 8)
|
|
|
|
{
|
|
|
|
array_push($first, '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
array_splice($first, 8 - count($second), 8, $second);
|
|
|
|
$aIP = $first;
|
|
|
|
unset($first,$second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$aIP = explode(':', $aIP[0]);
|
|
|
|
}
|
|
|
|
$c = count($aIP);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
if ($c != 8)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
// All the pieces should be 16-bit hex strings. Are they?
|
|
|
|
foreach ($aIP as $piece)
|
|
|
|
{
|
|
|
|
if (!preg_match('#^[0-9a-fA-F]{4}$#s', sprintf('%04s', $piece)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
return $original;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 01:05:35 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|