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