Edit File by line
/home/barbar84/public_h.../wp-inclu.../SimplePi.../Net
File: IPv6.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* SimplePie
[2] Fix | Delete
*
[3] Fix | Delete
* A PHP-Based RSS and Atom Feed Framework.
[4] Fix | Delete
* Takes the hard work out of managing a complete RSS/Atom solution.
[5] Fix | Delete
*
[6] Fix | Delete
* Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
[7] Fix | Delete
* All rights reserved.
[8] Fix | Delete
*
[9] Fix | Delete
* Redistribution and use in source and binary forms, with or without modification, are
[10] Fix | Delete
* permitted provided that the following conditions are met:
[11] Fix | Delete
*
[12] Fix | Delete
* * Redistributions of source code must retain the above copyright notice, this list of
[13] Fix | Delete
* conditions and the following disclaimer.
[14] Fix | Delete
*
[15] Fix | Delete
* * Redistributions in binary form must reproduce the above copyright notice, this list
[16] Fix | Delete
* of conditions and the following disclaimer in the documentation and/or other materials
[17] Fix | Delete
* provided with the distribution.
[18] Fix | Delete
*
[19] Fix | Delete
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
[20] Fix | Delete
* to endorse or promote products derived from this software without specific prior
[21] Fix | Delete
* written permission.
[22] Fix | Delete
*
[23] Fix | Delete
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
[24] Fix | Delete
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
[25] Fix | Delete
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
[26] Fix | Delete
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
[27] Fix | Delete
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
[28] Fix | Delete
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
[29] Fix | Delete
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
[30] Fix | Delete
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
[31] Fix | Delete
* POSSIBILITY OF SUCH DAMAGE.
[32] Fix | Delete
*
[33] Fix | Delete
* @package SimplePie
[34] Fix | Delete
* @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
[35] Fix | Delete
* @author Ryan Parman
[36] Fix | Delete
* @author Sam Sneddon
[37] Fix | Delete
* @author Ryan McCue
[38] Fix | Delete
* @link http://simplepie.org/ SimplePie
[39] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
[40] Fix | Delete
*/
[41] Fix | Delete
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Class to validate and to work with IPv6 addresses.
[45] Fix | Delete
*
[46] Fix | Delete
* @package SimplePie
[47] Fix | Delete
* @subpackage HTTP
[48] Fix | Delete
* @copyright 2003-2005 The PHP Group
[49] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php
[50] Fix | Delete
* @link http://pear.php.net/package/Net_IPv6
[51] Fix | Delete
* @author Alexander Merz <alexander.merz@web.de>
[52] Fix | Delete
* @author elfrink at introweb dot nl
[53] Fix | Delete
* @author Josh Peck <jmp at joshpeck dot org>
[54] Fix | Delete
* @author Sam Sneddon <geoffers@gmail.com>
[55] Fix | Delete
*/
[56] Fix | Delete
class SimplePie_Net_IPv6
[57] Fix | Delete
{
[58] Fix | Delete
/**
[59] Fix | Delete
* Uncompresses an IPv6 address
[60] Fix | Delete
*
[61] Fix | Delete
* RFC 4291 allows you to compress concecutive zero pieces in an address to
[62] Fix | Delete
* '::'. This method expects a valid IPv6 address and expands the '::' to
[63] Fix | Delete
* the required number of zero pieces.
[64] Fix | Delete
*
[65] Fix | Delete
* Example: FF01::101 -> FF01:0:0:0:0:0:0:101
[66] Fix | Delete
* ::1 -> 0:0:0:0:0:0:0:1
[67] Fix | Delete
*
[68] Fix | Delete
* @author Alexander Merz <alexander.merz@web.de>
[69] Fix | Delete
* @author elfrink at introweb dot nl
[70] Fix | Delete
* @author Josh Peck <jmp at joshpeck dot org>
[71] Fix | Delete
* @copyright 2003-2005 The PHP Group
[72] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php
[73] Fix | Delete
* @param string $ip An IPv6 address
[74] Fix | Delete
* @return string The uncompressed IPv6 address
[75] Fix | Delete
*/
[76] Fix | Delete
public static function uncompress($ip)
[77] Fix | Delete
{
[78] Fix | Delete
$c1 = -1;
[79] Fix | Delete
$c2 = -1;
[80] Fix | Delete
if (substr_count($ip, '::') === 1)
[81] Fix | Delete
{
[82] Fix | Delete
list($ip1, $ip2) = explode('::', $ip);
[83] Fix | Delete
if ($ip1 === '')
[84] Fix | Delete
{
[85] Fix | Delete
$c1 = -1;
[86] Fix | Delete
}
[87] Fix | Delete
else
[88] Fix | Delete
{
[89] Fix | Delete
$c1 = substr_count($ip1, ':');
[90] Fix | Delete
}
[91] Fix | Delete
if ($ip2 === '')
[92] Fix | Delete
{
[93] Fix | Delete
$c2 = -1;
[94] Fix | Delete
}
[95] Fix | Delete
else
[96] Fix | Delete
{
[97] Fix | Delete
$c2 = substr_count($ip2, ':');
[98] Fix | Delete
}
[99] Fix | Delete
if (strpos($ip2, '.') !== false)
[100] Fix | Delete
{
[101] Fix | Delete
$c2++;
[102] Fix | Delete
}
[103] Fix | Delete
// ::
[104] Fix | Delete
if ($c1 === -1 && $c2 === -1)
[105] Fix | Delete
{
[106] Fix | Delete
$ip = '0:0:0:0:0:0:0:0';
[107] Fix | Delete
}
[108] Fix | Delete
// ::xxx
[109] Fix | Delete
else if ($c1 === -1)
[110] Fix | Delete
{
[111] Fix | Delete
$fill = str_repeat('0:', 7 - $c2);
[112] Fix | Delete
$ip = str_replace('::', $fill, $ip);
[113] Fix | Delete
}
[114] Fix | Delete
// xxx::
[115] Fix | Delete
else if ($c2 === -1)
[116] Fix | Delete
{
[117] Fix | Delete
$fill = str_repeat(':0', 7 - $c1);
[118] Fix | Delete
$ip = str_replace('::', $fill, $ip);
[119] Fix | Delete
}
[120] Fix | Delete
// xxx::xxx
[121] Fix | Delete
else
[122] Fix | Delete
{
[123] Fix | Delete
$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
[124] Fix | Delete
$ip = str_replace('::', $fill, $ip);
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
return $ip;
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* Compresses an IPv6 address
[132] Fix | Delete
*
[133] Fix | Delete
* RFC 4291 allows you to compress concecutive zero pieces in an address to
[134] Fix | Delete
* '::'. This method expects a valid IPv6 address and compresses consecutive
[135] Fix | Delete
* zero pieces to '::'.
[136] Fix | Delete
*
[137] Fix | Delete
* Example: FF01:0:0:0:0:0:0:101 -> FF01::101
[138] Fix | Delete
* 0:0:0:0:0:0:0:1 -> ::1
[139] Fix | Delete
*
[140] Fix | Delete
* @see uncompress()
[141] Fix | Delete
* @param string $ip An IPv6 address
[142] Fix | Delete
* @return string The compressed IPv6 address
[143] Fix | Delete
*/
[144] Fix | Delete
public static function compress($ip)
[145] Fix | Delete
{
[146] Fix | Delete
// Prepare the IP to be compressed
[147] Fix | Delete
$ip = self::uncompress($ip);
[148] Fix | Delete
$ip_parts = self::split_v6_v4($ip);
[149] Fix | Delete
[150] Fix | Delete
// Replace all leading zeros
[151] Fix | Delete
$ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
[152] Fix | Delete
[153] Fix | Delete
// Find bunches of zeros
[154] Fix | Delete
if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE))
[155] Fix | Delete
{
[156] Fix | Delete
$max = 0;
[157] Fix | Delete
$pos = null;
[158] Fix | Delete
foreach ($matches[0] as $match)
[159] Fix | Delete
{
[160] Fix | Delete
if (strlen($match[0]) > $max)
[161] Fix | Delete
{
[162] Fix | Delete
$max = strlen($match[0]);
[163] Fix | Delete
$pos = $match[1];
[164] Fix | Delete
}
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
$ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
if ($ip_parts[1] !== '')
[171] Fix | Delete
{
[172] Fix | Delete
return implode(':', $ip_parts);
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
return $ip_parts[0];
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
/**
[179] Fix | Delete
* Splits an IPv6 address into the IPv6 and IPv4 representation parts
[180] Fix | Delete
*
[181] Fix | Delete
* RFC 4291 allows you to represent the last two parts of an IPv6 address
[182] Fix | Delete
* using the standard IPv4 representation
[183] Fix | Delete
*
[184] Fix | Delete
* Example: 0:0:0:0:0:0:13.1.68.3
[185] Fix | Delete
* 0:0:0:0:0:FFFF:129.144.52.38
[186] Fix | Delete
*
[187] Fix | Delete
* @param string $ip An IPv6 address
[188] Fix | Delete
* @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
[189] Fix | Delete
*/
[190] Fix | Delete
private static function split_v6_v4($ip)
[191] Fix | Delete
{
[192] Fix | Delete
if (strpos($ip, '.') !== false)
[193] Fix | Delete
{
[194] Fix | Delete
$pos = strrpos($ip, ':');
[195] Fix | Delete
$ipv6_part = substr($ip, 0, $pos);
[196] Fix | Delete
$ipv4_part = substr($ip, $pos + 1);
[197] Fix | Delete
return array($ipv6_part, $ipv4_part);
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return array($ip, '');
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Checks an IPv6 address
[205] Fix | Delete
*
[206] Fix | Delete
* Checks if the given IP is a valid IPv6 address
[207] Fix | Delete
*
[208] Fix | Delete
* @param string $ip An IPv6 address
[209] Fix | Delete
* @return bool true if $ip is a valid IPv6 address
[210] Fix | Delete
*/
[211] Fix | Delete
public static function check_ipv6($ip)
[212] Fix | Delete
{
[213] Fix | Delete
$ip = self::uncompress($ip);
[214] Fix | Delete
list($ipv6, $ipv4) = self::split_v6_v4($ip);
[215] Fix | Delete
$ipv6 = explode(':', $ipv6);
[216] Fix | Delete
$ipv4 = explode('.', $ipv4);
[217] Fix | Delete
if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
[218] Fix | Delete
{
[219] Fix | Delete
foreach ($ipv6 as $ipv6_part)
[220] Fix | Delete
{
[221] Fix | Delete
// The section can't be empty
[222] Fix | Delete
if ($ipv6_part === '')
[223] Fix | Delete
return false;
[224] Fix | Delete
[225] Fix | Delete
// Nor can it be over four characters
[226] Fix | Delete
if (strlen($ipv6_part) > 4)
[227] Fix | Delete
return false;
[228] Fix | Delete
[229] Fix | Delete
// Remove leading zeros (this is safe because of the above)
[230] Fix | Delete
$ipv6_part = ltrim($ipv6_part, '0');
[231] Fix | Delete
if ($ipv6_part === '')
[232] Fix | Delete
$ipv6_part = '0';
[233] Fix | Delete
[234] Fix | Delete
// Check the value is valid
[235] Fix | Delete
$value = hexdec($ipv6_part);
[236] Fix | Delete
if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
[237] Fix | Delete
return false;
[238] Fix | Delete
}
[239] Fix | Delete
if (count($ipv4) === 4)
[240] Fix | Delete
{
[241] Fix | Delete
foreach ($ipv4 as $ipv4_part)
[242] Fix | Delete
{
[243] Fix | Delete
$value = (int) $ipv4_part;
[244] Fix | Delete
if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)
[245] Fix | Delete
return false;
[246] Fix | Delete
}
[247] Fix | Delete
}
[248] Fix | Delete
return true;
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
return false;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
/**
[255] Fix | Delete
* Checks if the given IP is a valid IPv6 address
[256] Fix | Delete
*
[257] Fix | Delete
* @codeCoverageIgnore
[258] Fix | Delete
* @deprecated Use {@see SimplePie_Net_IPv6::check_ipv6()} instead
[259] Fix | Delete
* @see check_ipv6
[260] Fix | Delete
* @param string $ip An IPv6 address
[261] Fix | Delete
* @return bool true if $ip is a valid IPv6 address
[262] Fix | Delete
*/
[263] Fix | Delete
public static function checkIPv6($ip)
[264] Fix | Delete
{
[265] Fix | Delete
return self::check_ipv6($ip);
[266] Fix | Delete
}
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function