Edit File by line
/home/barbar84/public_h.../wp-inclu.../Requests
File: IPv6.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Class to validate and to work with IPv6 addresses
[2] Fix | Delete
*
[3] Fix | Delete
* @package Requests
[4] Fix | Delete
* @subpackage Utilities
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Class to validate and to work with IPv6 addresses
[9] Fix | Delete
*
[10] Fix | Delete
* This was originally based on the PEAR class of the same name, but has been
[11] Fix | Delete
* entirely rewritten.
[12] Fix | Delete
*
[13] Fix | Delete
* @package Requests
[14] Fix | Delete
* @subpackage Utilities
[15] Fix | Delete
*/
[16] Fix | Delete
class Requests_IPv6 {
[17] Fix | Delete
/**
[18] Fix | Delete
* Uncompresses an IPv6 address
[19] Fix | Delete
*
[20] Fix | Delete
* RFC 4291 allows you to compress consecutive zero pieces in an address to
[21] Fix | Delete
* '::'. This method expects a valid IPv6 address and expands the '::' to
[22] Fix | Delete
* the required number of zero pieces.
[23] Fix | Delete
*
[24] Fix | Delete
* Example: FF01::101 -> FF01:0:0:0:0:0:0:101
[25] Fix | Delete
* ::1 -> 0:0:0:0:0:0:0:1
[26] Fix | Delete
*
[27] Fix | Delete
* @author Alexander Merz <alexander.merz@web.de>
[28] Fix | Delete
* @author elfrink at introweb dot nl
[29] Fix | Delete
* @author Josh Peck <jmp at joshpeck dot org>
[30] Fix | Delete
* @copyright 2003-2005 The PHP Group
[31] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php
[32] Fix | Delete
* @param string $ip An IPv6 address
[33] Fix | Delete
* @return string The uncompressed IPv6 address
[34] Fix | Delete
*/
[35] Fix | Delete
public static function uncompress($ip) {
[36] Fix | Delete
if (substr_count($ip, '::') !== 1) {
[37] Fix | Delete
return $ip;
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
list($ip1, $ip2) = explode('::', $ip);
[41] Fix | Delete
$c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
[42] Fix | Delete
$c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
[43] Fix | Delete
[44] Fix | Delete
if (strpos($ip2, '.') !== false) {
[45] Fix | Delete
$c2++;
[46] Fix | Delete
}
[47] Fix | Delete
// ::
[48] Fix | Delete
if ($c1 === -1 && $c2 === -1) {
[49] Fix | Delete
$ip = '0:0:0:0:0:0:0:0';
[50] Fix | Delete
}
[51] Fix | Delete
// ::xxx
[52] Fix | Delete
else if ($c1 === -1) {
[53] Fix | Delete
$fill = str_repeat('0:', 7 - $c2);
[54] Fix | Delete
$ip = str_replace('::', $fill, $ip);
[55] Fix | Delete
}
[56] Fix | Delete
// xxx::
[57] Fix | Delete
else if ($c2 === -1) {
[58] Fix | Delete
$fill = str_repeat(':0', 7 - $c1);
[59] Fix | Delete
$ip = str_replace('::', $fill, $ip);
[60] Fix | Delete
}
[61] Fix | Delete
// xxx::xxx
[62] Fix | Delete
else {
[63] Fix | Delete
$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
[64] Fix | Delete
$ip = str_replace('::', $fill, $ip);
[65] Fix | Delete
}
[66] Fix | Delete
return $ip;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Compresses an IPv6 address
[71] Fix | Delete
*
[72] Fix | Delete
* RFC 4291 allows you to compress consecutive zero pieces in an address to
[73] Fix | Delete
* '::'. This method expects a valid IPv6 address and compresses consecutive
[74] Fix | Delete
* zero pieces to '::'.
[75] Fix | Delete
*
[76] Fix | Delete
* Example: FF01:0:0:0:0:0:0:101 -> FF01::101
[77] Fix | Delete
* 0:0:0:0:0:0:0:1 -> ::1
[78] Fix | Delete
*
[79] Fix | Delete
* @see uncompress()
[80] Fix | Delete
* @param string $ip An IPv6 address
[81] Fix | Delete
* @return string The compressed IPv6 address
[82] Fix | Delete
*/
[83] Fix | Delete
public static function compress($ip) {
[84] Fix | Delete
// Prepare the IP to be compressed
[85] Fix | Delete
$ip = self::uncompress($ip);
[86] Fix | Delete
$ip_parts = self::split_v6_v4($ip);
[87] Fix | Delete
[88] Fix | Delete
// Replace all leading zeros
[89] Fix | Delete
$ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
[90] Fix | Delete
[91] Fix | Delete
// Find bunches of zeros
[92] Fix | Delete
if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
[93] Fix | Delete
$max = 0;
[94] Fix | Delete
$pos = null;
[95] Fix | Delete
foreach ($matches[0] as $match) {
[96] Fix | Delete
if (strlen($match[0]) > $max) {
[97] Fix | Delete
$max = strlen($match[0]);
[98] Fix | Delete
$pos = $match[1];
[99] Fix | Delete
}
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
$ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
if ($ip_parts[1] !== '') {
[106] Fix | Delete
return implode(':', $ip_parts);
[107] Fix | Delete
}
[108] Fix | Delete
else {
[109] Fix | Delete
return $ip_parts[0];
[110] Fix | Delete
}
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Splits an IPv6 address into the IPv6 and IPv4 representation parts
[115] Fix | Delete
*
[116] Fix | Delete
* RFC 4291 allows you to represent the last two parts of an IPv6 address
[117] Fix | Delete
* using the standard IPv4 representation
[118] Fix | Delete
*
[119] Fix | Delete
* Example: 0:0:0:0:0:0:13.1.68.3
[120] Fix | Delete
* 0:0:0:0:0:FFFF:129.144.52.38
[121] Fix | Delete
*
[122] Fix | Delete
* @param string $ip An IPv6 address
[123] Fix | Delete
* @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part
[124] Fix | Delete
*/
[125] Fix | Delete
protected static function split_v6_v4($ip) {
[126] Fix | Delete
if (strpos($ip, '.') !== false) {
[127] Fix | Delete
$pos = strrpos($ip, ':');
[128] Fix | Delete
$ipv6_part = substr($ip, 0, $pos);
[129] Fix | Delete
$ipv4_part = substr($ip, $pos + 1);
[130] Fix | Delete
return array($ipv6_part, $ipv4_part);
[131] Fix | Delete
}
[132] Fix | Delete
else {
[133] Fix | Delete
return array($ip, '');
[134] Fix | Delete
}
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Checks an IPv6 address
[139] Fix | Delete
*
[140] Fix | Delete
* Checks if the given IP is a valid IPv6 address
[141] Fix | Delete
*
[142] Fix | Delete
* @param string $ip An IPv6 address
[143] Fix | Delete
* @return bool true if $ip is a valid IPv6 address
[144] Fix | Delete
*/
[145] Fix | Delete
public static function check_ipv6($ip) {
[146] Fix | Delete
$ip = self::uncompress($ip);
[147] Fix | Delete
list($ipv6, $ipv4) = self::split_v6_v4($ip);
[148] Fix | Delete
$ipv6 = explode(':', $ipv6);
[149] Fix | Delete
$ipv4 = explode('.', $ipv4);
[150] Fix | Delete
if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
[151] Fix | Delete
foreach ($ipv6 as $ipv6_part) {
[152] Fix | Delete
// The section can't be empty
[153] Fix | Delete
if ($ipv6_part === '') {
[154] Fix | Delete
return false;
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
// Nor can it be over four characters
[158] Fix | Delete
if (strlen($ipv6_part) > 4) {
[159] Fix | Delete
return false;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
// Remove leading zeros (this is safe because of the above)
[163] Fix | Delete
$ipv6_part = ltrim($ipv6_part, '0');
[164] Fix | Delete
if ($ipv6_part === '') {
[165] Fix | Delete
$ipv6_part = '0';
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
// Check the value is valid
[169] Fix | Delete
$value = hexdec($ipv6_part);
[170] Fix | Delete
if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) {
[171] Fix | Delete
return false;
[172] Fix | Delete
}
[173] Fix | Delete
}
[174] Fix | Delete
if (count($ipv4) === 4) {
[175] Fix | Delete
foreach ($ipv4 as $ipv4_part) {
[176] Fix | Delete
$value = (int) $ipv4_part;
[177] Fix | Delete
if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) {
[178] Fix | Delete
return false;
[179] Fix | Delete
}
[180] Fix | Delete
}
[181] Fix | Delete
}
[182] Fix | Delete
return true;
[183] Fix | Delete
}
[184] Fix | Delete
else {
[185] Fix | Delete
return false;
[186] Fix | Delete
}
[187] Fix | Delete
}
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function