HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/AttrDef/URI/IPv6.php
Go to the documentation of this file.
00001 <?php
00002 
00009 class HTMLPurifier_AttrDef_URI_IPv6 extends HTMLPurifier_AttrDef_URI_IPv4
00010 {
00011 
00012     public function validate($aIP, $config, $context) {
00013 
00014         if (!$this->ip4) $this->_loadRegex();
00015 
00016         $original = $aIP;
00017 
00018         $hex = '[0-9a-fA-F]';
00019         $blk = '(?:' . $hex . '{1,4})';
00020         $pre = '(?:/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))';   // /0 - /128
00021 
00022         //      prefix check
00023         if (strpos($aIP, '/') !== false)
00024         {
00025                 if (preg_match('#' . $pre . '$#s', $aIP, $find))
00026                 {
00027                         $aIP = substr($aIP, 0, 0-strlen($find[0]));
00028                         unset($find);
00029                 }
00030                 else
00031                 {
00032                         return false;
00033                 }
00034         }
00035 
00036         //      IPv4-compatiblity check
00037         if (preg_match('#(?<=:'.')' . $this->ip4 . '$#s', $aIP, $find))
00038         {
00039                 $aIP = substr($aIP, 0, 0-strlen($find[0]));
00040                 $ip = explode('.', $find[0]);
00041                 $ip = array_map('dechex', $ip);
00042                 $aIP .= $ip[0] . $ip[1] . ':' . $ip[2] . $ip[3];
00043                 unset($find, $ip);
00044         }
00045 
00046         //      compression check
00047         $aIP = explode('::', $aIP);
00048         $c = count($aIP);
00049         if ($c > 2)
00050         {
00051                 return false;
00052         }
00053         elseif ($c == 2)
00054         {
00055                 list($first, $second) = $aIP;
00056                 $first = explode(':', $first);
00057                 $second = explode(':', $second);
00058 
00059                 if (count($first) + count($second) > 8)
00060                 {
00061                         return false;
00062                 }
00063 
00064                 while(count($first) < 8)
00065                 {
00066                         array_push($first, '0');
00067                 }
00068 
00069                 array_splice($first, 8 - count($second), 8, $second);
00070                 $aIP = $first;
00071                 unset($first,$second);
00072         }
00073         else
00074         {
00075                 $aIP = explode(':', $aIP[0]);
00076         }
00077         $c = count($aIP);
00078 
00079         if ($c != 8)
00080         {
00081                 return false;
00082         }
00083 
00084         //      All the pieces should be 16-bit hex strings. Are they?
00085         foreach ($aIP as $piece)
00086         {
00087                 if (!preg_match('#^[0-9a-fA-F]{4}$#s', sprintf('%04s', $piece)))
00088                 {
00089                         return false;
00090                 }
00091         }
00092 
00093         return $original;
00094 
00095     }
00096 
00097 }
00098 
00099 // vim: et sw=4 sts=4