Fix problem where stacked AttrTransforms clobber each other.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / URI / IPv6.php
blob9454e9be50eb96970d1eed77a61fd4976ff60911
1 <?php
3 /**
4 * Validates an IPv6 address.
5 * @author Feyd @ forums.devnetwork.net (public domain)
6 * @note This function requires brackets to have been removed from address
7 * in URI.
8 */
9 class HTMLPurifier_AttrDef_URI_IPv6 extends HTMLPurifier_AttrDef_URI_IPv4
12 public function validate($aIP, $config, $context) {
14 if (!$this->ip4) $this->_loadRegex();
16 $original = $aIP;
18 $hex = '[0-9a-fA-F]';
19 $blk = '(?:' . $hex . '{1,4})';
20 $pre = '(?:/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))'; // /0 - /128
22 // prefix check
23 if (strpos($aIP, '/') !== false)
25 if (preg_match('#' . $pre . '$#s', $aIP, $find))
27 $aIP = substr($aIP, 0, 0-strlen($find[0]));
28 unset($find);
30 else
32 return false;
36 // IPv4-compatiblity check
37 if (preg_match('#(?<=:'.')' . $this->ip4 . '$#s', $aIP, $find))
39 $aIP = substr($aIP, 0, 0-strlen($find[0]));
40 $ip = explode('.', $find[0]);
41 $ip = array_map('dechex', $ip);
42 $aIP .= $ip[0] . $ip[1] . ':' . $ip[2] . $ip[3];
43 unset($find, $ip);
46 // compression check
47 $aIP = explode('::', $aIP);
48 $c = count($aIP);
49 if ($c > 2)
51 return false;
53 elseif ($c == 2)
55 list($first, $second) = $aIP;
56 $first = explode(':', $first);
57 $second = explode(':', $second);
59 if (count($first) + count($second) > 8)
61 return false;
64 while(count($first) < 8)
66 array_push($first, '0');
69 array_splice($first, 8 - count($second), 8, $second);
70 $aIP = $first;
71 unset($first,$second);
73 else
75 $aIP = explode(':', $aIP[0]);
77 $c = count($aIP);
79 if ($c != 8)
81 return false;
84 // All the pieces should be 16-bit hex strings. Are they?
85 foreach ($aIP as $piece)
87 if (!preg_match('#^[0-9a-fA-F]{4}$#s', sprintf('%04s', $piece)))
89 return false;
93 return $original;
99 // vim: et sw=4 sts=4