@@ -31,81 +31,81 @@ |
||
31 | 31 | * @package OC\Security\Normalizer |
32 | 32 | */ |
33 | 33 | class IpAddress { |
34 | - /** @var string */ |
|
35 | - private $ip; |
|
34 | + /** @var string */ |
|
35 | + private $ip; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param string $ip IP to normalized |
|
39 | - */ |
|
40 | - public function __construct(string $ip) { |
|
41 | - $this->ip = $ip; |
|
42 | - } |
|
37 | + /** |
|
38 | + * @param string $ip IP to normalized |
|
39 | + */ |
|
40 | + public function __construct(string $ip) { |
|
41 | + $this->ip = $ip; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Return the given subnet for an IPv4 address and mask bits |
|
46 | - * |
|
47 | - * @param string $ip |
|
48 | - * @param int $maskBits |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - private function getIPv4Subnet(string $ip, int $maskBits = 32): string { |
|
52 | - $binary = \inet_pton($ip); |
|
53 | - for ($i = 32; $i > $maskBits; $i -= 8) { |
|
54 | - $j = \intdiv($i, 8) - 1; |
|
55 | - $k = (int) \min(8, $i - $maskBits); |
|
56 | - $mask = (0xff - ((2 ** $k) - 1)); |
|
57 | - $int = \unpack('C', $binary[$j]); |
|
58 | - $binary[$j] = \pack('C', $int[1] & $mask); |
|
59 | - } |
|
60 | - return \inet_ntop($binary).'/'.$maskBits; |
|
61 | - } |
|
44 | + /** |
|
45 | + * Return the given subnet for an IPv4 address and mask bits |
|
46 | + * |
|
47 | + * @param string $ip |
|
48 | + * @param int $maskBits |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + private function getIPv4Subnet(string $ip, int $maskBits = 32): string { |
|
52 | + $binary = \inet_pton($ip); |
|
53 | + for ($i = 32; $i > $maskBits; $i -= 8) { |
|
54 | + $j = \intdiv($i, 8) - 1; |
|
55 | + $k = (int) \min(8, $i - $maskBits); |
|
56 | + $mask = (0xff - ((2 ** $k) - 1)); |
|
57 | + $int = \unpack('C', $binary[$j]); |
|
58 | + $binary[$j] = \pack('C', $int[1] & $mask); |
|
59 | + } |
|
60 | + return \inet_ntop($binary).'/'.$maskBits; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Return the given subnet for an IPv6 address and mask bits |
|
65 | - * |
|
66 | - * @param string $ip |
|
67 | - * @param int $maskBits |
|
68 | - * @return string |
|
69 | - */ |
|
70 | - private function getIPv6Subnet(string $ip, int $maskBits = 48): string { |
|
71 | - if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1] |
|
72 | - $ip = substr($ip, 1, strlen($ip) - 2); |
|
73 | - } |
|
74 | - $binary = \inet_pton($ip); |
|
75 | - for ($i = 128; $i > $maskBits; $i -= 8) { |
|
76 | - $j = \intdiv($i, 8) - 1; |
|
77 | - $k = (int) \min(8, $i - $maskBits); |
|
78 | - $mask = (0xff - ((2 ** $k) - 1)); |
|
79 | - $int = \unpack('C', $binary[$j]); |
|
80 | - $binary[$j] = \pack('C', $int[1] & $mask); |
|
81 | - } |
|
82 | - return \inet_ntop($binary).'/'.$maskBits; |
|
83 | - } |
|
63 | + /** |
|
64 | + * Return the given subnet for an IPv6 address and mask bits |
|
65 | + * |
|
66 | + * @param string $ip |
|
67 | + * @param int $maskBits |
|
68 | + * @return string |
|
69 | + */ |
|
70 | + private function getIPv6Subnet(string $ip, int $maskBits = 48): string { |
|
71 | + if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1] |
|
72 | + $ip = substr($ip, 1, strlen($ip) - 2); |
|
73 | + } |
|
74 | + $binary = \inet_pton($ip); |
|
75 | + for ($i = 128; $i > $maskBits; $i -= 8) { |
|
76 | + $j = \intdiv($i, 8) - 1; |
|
77 | + $k = (int) \min(8, $i - $maskBits); |
|
78 | + $mask = (0xff - ((2 ** $k) - 1)); |
|
79 | + $int = \unpack('C', $binary[$j]); |
|
80 | + $binary[$j] = \pack('C', $int[1] & $mask); |
|
81 | + } |
|
82 | + return \inet_ntop($binary).'/'.$maskBits; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Gets either the /32 (IPv4) or the /128 (IPv6) subnet of an IP address |
|
87 | - * |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function getSubnet(): string { |
|
91 | - if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->ip)) { |
|
92 | - return $this->getIPv4Subnet( |
|
93 | - $this->ip, |
|
94 | - 32 |
|
95 | - ); |
|
96 | - } |
|
97 | - return $this->getIPv6Subnet( |
|
98 | - $this->ip, |
|
99 | - 128 |
|
100 | - ); |
|
101 | - } |
|
85 | + /** |
|
86 | + * Gets either the /32 (IPv4) or the /128 (IPv6) subnet of an IP address |
|
87 | + * |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function getSubnet(): string { |
|
91 | + if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->ip)) { |
|
92 | + return $this->getIPv4Subnet( |
|
93 | + $this->ip, |
|
94 | + 32 |
|
95 | + ); |
|
96 | + } |
|
97 | + return $this->getIPv6Subnet( |
|
98 | + $this->ip, |
|
99 | + 128 |
|
100 | + ); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Returns the specified IP address |
|
105 | - * |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - public function __toString(): string { |
|
109 | - return $this->ip; |
|
110 | - } |
|
103 | + /** |
|
104 | + * Returns the specified IP address |
|
105 | + * |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + public function __toString(): string { |
|
109 | + return $this->ip; |
|
110 | + } |
|
111 | 111 | } |