wp/wp-includes/Requests/IPv6.php
changeset 18 be944660c56a
parent 7 cf61fcea0001
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    38 		if (substr_count($ip, '::') !== 1) {
    38 		if (substr_count($ip, '::') !== 1) {
    39 			return $ip;
    39 			return $ip;
    40 		}
    40 		}
    41 
    41 
    42 		list($ip1, $ip2) = explode('::', $ip);
    42 		list($ip1, $ip2) = explode('::', $ip);
    43 		$c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
    43 		$c1              = ($ip1 === '') ? -1 : substr_count($ip1, ':');
    44 		$c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
    44 		$c2              = ($ip2 === '') ? -1 : substr_count($ip2, ':');
    45 
    45 
    46 		if (strpos($ip2, '.') !== false) {
    46 		if (strpos($ip2, '.') !== false) {
    47 			$c2++;
    47 			$c2++;
    48 		}
    48 		}
    49 		// ::
    49 		// ::
    50 		if ($c1 === -1 && $c2 === -1) {
    50 		if ($c1 === -1 && $c2 === -1) {
    51 			$ip = '0:0:0:0:0:0:0:0';
    51 			$ip = '0:0:0:0:0:0:0:0';
    52 		}
    52 		}
    53 		// ::xxx
    53 		// ::xxx
    54 		else if ($c1 === -1) {
    54 		elseif ($c1 === -1) {
    55 			$fill = str_repeat('0:', 7 - $c2);
    55 			$fill = str_repeat('0:', 7 - $c2);
    56 			$ip = str_replace('::', $fill, $ip);
    56 			$ip   = str_replace('::', $fill, $ip);
    57 		}
    57 		}
    58 		// xxx::
    58 		// xxx::
    59 		else if ($c2 === -1) {
    59 		elseif ($c2 === -1) {
    60 			$fill = str_repeat(':0', 7 - $c1);
    60 			$fill = str_repeat(':0', 7 - $c1);
    61 			$ip = str_replace('::', $fill, $ip);
    61 			$ip   = str_replace('::', $fill, $ip);
    62 		}
    62 		}
    63 		// xxx::xxx
    63 		// xxx::xxx
    64 		else {
    64 		else {
    65 			$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
    65 			$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
    66 			$ip = str_replace('::', $fill, $ip);
    66 			$ip   = str_replace('::', $fill, $ip);
    67 		}
    67 		}
    68 		return $ip;
    68 		return $ip;
    69 	}
    69 	}
    70 
    70 
    71 	/**
    71 	/**
    82 	 * @param string $ip An IPv6 address
    82 	 * @param string $ip An IPv6 address
    83 	 * @return string The compressed IPv6 address
    83 	 * @return string The compressed IPv6 address
    84 	 */
    84 	 */
    85 	public static function compress($ip) {
    85 	public static function compress($ip) {
    86 		// Prepare the IP to be compressed
    86 		// Prepare the IP to be compressed
    87 		$ip = self::uncompress($ip);
    87 		$ip       = self::uncompress($ip);
    88 		$ip_parts = self::split_v6_v4($ip);
    88 		$ip_parts = self::split_v6_v4($ip);
    89 
    89 
    90 		// Replace all leading zeros
    90 		// Replace all leading zeros
    91 		$ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
    91 		$ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
    92 
    92 
   124 	 * @param string $ip An IPv6 address
   124 	 * @param string $ip An IPv6 address
   125 	 * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part
   125 	 * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part
   126 	 */
   126 	 */
   127 	protected static function split_v6_v4($ip) {
   127 	protected static function split_v6_v4($ip) {
   128 		if (strpos($ip, '.') !== false) {
   128 		if (strpos($ip, '.') !== false) {
   129 			$pos = strrpos($ip, ':');
   129 			$pos       = strrpos($ip, ':');
   130 			$ipv6_part = substr($ip, 0, $pos);
   130 			$ipv6_part = substr($ip, 0, $pos);
   131 			$ipv4_part = substr($ip, $pos + 1);
   131 			$ipv4_part = substr($ip, $pos + 1);
   132 			return array($ipv6_part, $ipv4_part);
   132 			return array($ipv6_part, $ipv4_part);
   133 		}
   133 		}
   134 		else {
   134 		else {
   143 	 *
   143 	 *
   144 	 * @param string $ip An IPv6 address
   144 	 * @param string $ip An IPv6 address
   145 	 * @return bool true if $ip is a valid IPv6 address
   145 	 * @return bool true if $ip is a valid IPv6 address
   146 	 */
   146 	 */
   147 	public static function check_ipv6($ip) {
   147 	public static function check_ipv6($ip) {
   148 		$ip = self::uncompress($ip);
   148 		$ip                = self::uncompress($ip);
   149 		list($ipv6, $ipv4) = self::split_v6_v4($ip);
   149 		list($ipv6, $ipv4) = self::split_v6_v4($ip);
   150 		$ipv6 = explode(':', $ipv6);
   150 		$ipv6              = explode(':', $ipv6);
   151 		$ipv4 = explode('.', $ipv4);
   151 		$ipv4              = explode('.', $ipv4);
   152 		if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
   152 		if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
   153 			foreach ($ipv6 as $ipv6_part) {
   153 			foreach ($ipv6 as $ipv6_part) {
   154 				// The section can't be empty
   154 				// The section can't be empty
   155 				if ($ipv6_part === '') {
   155 				if ($ipv6_part === '') {
   156 					return false;
   156 					return false;