Completed
Pull Request — master (#271)
by
unknown
03:48
created

IPv6::uncompress()   D

Complexity

Conditions 9
Paths 33

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 33
nop 1
dl 0
loc 33
rs 4.909
c 0
b 0
f 0
1
<?php
2
namespace Rmccue\Requests;
3
4
/**
5
 * Class to validate and to work with IPv6 addresses
6
 *
7
 * @package Rmccue\Requests
8
 * @subpackage Utilities
9
 */
10
11
/**
12
 * Class to validate and to work with IPv6 addresses
13
 *
14
 * This was originally based on the PEAR class of the same name, but has been
15
 * entirely rewritten.
16
 *
17
 * @package Rmccue\Requests
18
 * @subpackage Utilities
19
 */
20
class IPv6 {
21
	/**
22
	 * Uncompresses an IPv6 address
23
	 *
24
	 * RFC 4291 allows you to compress consecutive zero pieces in an address to
25
	 * '::'. This method expects a valid IPv6 address and expands the '::' to
26
	 * the required number of zero pieces.
27
	 *
28
	 * Example:  FF01::101   ->  FF01:0:0:0:0:0:0:101
29
	 *           ::1         ->  0:0:0:0:0:0:0:1
30
	 *
31
	 * @author Alexander Merz <[email protected]>
32
	 * @author elfrink at introweb dot nl
33
	 * @author Josh Peck <jmp at joshpeck dot org>
34
	 * @copyright 2003-2005 The PHP Group
35
	 * @license http://www.opensource.org/licenses/bsd-license.php
36
	 * @param string $ip An IPv6 address
37
	 * @return string The uncompressed IPv6 address
38
	 */
39
	public static function uncompress($ip) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ip. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
40
		if (substr_count($ip, '::') !== 1) {
41
			return $ip;
42
		}
43
44
		list($ip1, $ip2) = explode('::', $ip);
45
		$c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c1. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
46
		$c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c2. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
47
48
		if (strpos($ip2, '.') !== false) {
49
			$c2++;
50
		}
51
		// ::
52
		if ($c1 === -1 && $c2 === -1) {
53
			$ip = '0:0:0:0:0:0:0:0';
54
		}
55
		// ::xxx
56
		else if ($c1 === -1) {
57
			$fill = str_repeat('0:', 7 - $c2);
58
			$ip = str_replace('::', $fill, $ip);
59
		}
60
		// xxx::
61
		else if ($c2 === -1) {
62
			$fill = str_repeat(':0', 7 - $c1);
63
			$ip = str_replace('::', $fill, $ip);
64
		}
65
		// xxx::xxx
66
		else {
67
			$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
68
			$ip = str_replace('::', $fill, $ip);
69
		}
70
		return $ip;
71
	}
72
73
	/**
74
	 * Compresses an IPv6 address
75
	 *
76
	 * RFC 4291 allows you to compress consecutive zero pieces in an address to
77
	 * '::'. This method expects a valid IPv6 address and compresses consecutive
78
	 * zero pieces to '::'.
79
	 *
80
	 * Example:  FF01:0:0:0:0:0:0:101   ->  FF01::101
81
	 *           0:0:0:0:0:0:0:1        ->  ::1
82
	 *
83
	 * @see uncompress()
84
	 * @param string $ip An IPv6 address
85
	 * @return string The compressed IPv6 address
86
	 */
87
	public static function compress($ip) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ip. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
88
		// Prepare the IP to be compressed
89
		$ip = self::uncompress($ip);
90
		$ip_parts = self::split_v6_v4($ip);
91
92
		// Replace all leading zeros
93
		$ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
94
95
		// Find bunches of zeros
96
		if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
97
			$max = 0;
98
			$pos = null;
99
			foreach ($matches[0] as $match) {
100
				if (strlen($match[0]) > $max) {
101
					$max = strlen($match[0]);
102
					$pos = $match[1];
103
				}
104
			}
105
106
			$ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
107
		}
108
109
		if ($ip_parts[1] !== '') {
110
			return implode(':', $ip_parts);
111
		}
112
		else {
113
			return $ip_parts[0];
114
		}
115
	}
116
117
	/**
118
	 * Splits an IPv6 address into the IPv6 and IPv4 representation parts
119
	 *
120
	 * RFC 4291 allows you to represent the last two parts of an IPv6 address
121
	 * using the standard IPv4 representation
122
	 *
123
	 * Example:  0:0:0:0:0:0:13.1.68.3
124
	 *           0:0:0:0:0:FFFF:129.144.52.38
125
	 *
126
	 * @param string $ip An IPv6 address
127
	 * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part
128
	 */
129
	protected static function split_v6_v4($ip) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ip. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
130
		if (strpos($ip, '.') !== false) {
131
			$pos = strrpos($ip, ':');
132
			$ipv6_part = substr($ip, 0, $pos);
133
			$ipv4_part = substr($ip, $pos + 1);
134
			return array($ipv6_part, $ipv4_part);
135
		}
136
		else {
137
			return array($ip, '');
138
		}
139
	}
140
141
	/**
142
	 * Checks an IPv6 address
143
	 *
144
	 * Checks if the given IP is a valid IPv6 address
145
	 *
146
	 * @param string $ip An IPv6 address
147
	 * @return bool true if $ip is a valid IPv6 address
148
	 */
149
	public static function check_ipv6($ip) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ip. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
150
		$ip = self::uncompress($ip);
151
		list($ipv6, $ipv4) = self::split_v6_v4($ip);
152
		$ipv6 = explode(':', $ipv6);
153
		$ipv4 = explode('.', $ipv4);
154
		if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
155
			foreach ($ipv6 as $ipv6_part) {
156
				// The section can't be empty
157
				if ($ipv6_part === '') {
158
					return false;
159
				}
160
161
				// Nor can it be over four characters
162
				if (strlen($ipv6_part) > 4) {
163
					return false;
164
				}
165
166
				// Remove leading zeros (this is safe because of the above)
167
				$ipv6_part = ltrim($ipv6_part, '0');
168
				if ($ipv6_part === '') {
169
					$ipv6_part = '0';
170
				}
171
172
				// Check the value is valid
173
				$value = hexdec($ipv6_part);
174
				if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) {
175
					return false;
176
				}
177
			}
178
			if (count($ipv4) === 4) {
179
				foreach ($ipv4 as $ipv4_part) {
180
					$value = (int) $ipv4_part;
181
					if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) {
182
						return false;
183
					}
184
				}
185
			}
186
			return true;
187
		}
188
		else {
189
			return false;
190
		}
191
	}
192
}
193