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