Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / URI / IPv6.php
blobf48b803dd7f25247d01d55ef81f85f72d8d056fe
1 <?php
3 require_once 'HTMLPurifier/AttrDef/URI/IPv4.php';
5 /**
6 * Validates an IPv6 address.
7 * @author Feyd @ forums.devnetwork.net (public domain)
8 * @note This function requires brackets to have been removed from address
9 * in URI.
11 class HTMLPurifier_AttrDef_URI_IPv6 extends HTMLPurifier_AttrDef_URI_IPv4
14 function validate($aIP, $config, &$context) {
16 if (!$this->ip4) $this->_loadRegex();
18 $original = $aIP;
20 $hex = '[0-9a-fA-F]';
21 $blk = '(?:' . $hex . '{1,4})';
22 $pre = '(?:/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))'; // /0 - /128
24 // prefix check
25 if (strpos($aIP, '/') !== false)
27 if (preg_match('#' . $pre . '$#s', $aIP, $find))
29 $aIP = substr($aIP, 0, 0-strlen($find[0]));
30 unset($find);
32 else
34 return false;
38 // IPv4-compatiblity check
39 if (preg_match('#(?<=:'.')' . $this->ip4 . '$#s', $aIP, $find))
41 $aIP = substr($aIP, 0, 0-strlen($find[0]));
42 $ip = explode('.', $find[0]);
43 $ip = array_map('dechex', $ip);
44 $aIP .= $ip[0] . $ip[1] . ':' . $ip[2] . $ip[3];
45 unset($find, $ip);
48 // compression check
49 $aIP = explode('::', $aIP);
50 $c = count($aIP);
51 if ($c > 2)
53 return false;
55 elseif ($c == 2)
57 list($first, $second) = $aIP;
58 $first = explode(':', $first);
59 $second = explode(':', $second);
61 if (count($first) + count($second) > 8)
63 return false;
66 while(count($first) < 8)
68 array_push($first, '0');
71 array_splice($first, 8 - count($second), 8, $second);
72 $aIP = $first;
73 unset($first,$second);
75 else
77 $aIP = explode(':', $aIP[0]);
79 $c = count($aIP);
81 if ($c != 8)
83 return false;
86 // All the pieces should be 16-bit hex strings. Are they?
87 foreach ($aIP as $piece)
89 if (!preg_match('#^[0-9a-fA-F]{4}$#s', sprintf('%04s', $piece)))
91 return false;
95 return $original;