Lines: 96.15%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Helper; | |
| 6 | ||
| 7 | /** | |
| 8 | * Masks an IP address before it is stored on a visit. | |
| 9 | * | |
| 10 | * Call this when the site has turned on IP anonymization. The full | |
| 11 | * address should be passed in, and only the return value stored or | |
| 12 | * passed to the location plugin. Length 1 removes the last IPv4 octet or | |
| 13 | * keeps an IPv6 /64. Length 2 and 3 remove more of the address. | |
| 14 | */ | |
| 15 | final class IpAnonymizer { | |
| 16 | ||
| 17 | /** | |
| 18 | * Masks an IP address. | |
| 19 | * | |
| 20 | * An empty or invalid address is returned unchanged. The mask length | |
| 21 | * is clamped to 1, 2, or 3 octets. IPv4-mapped IPv6 addresses are | |
| 22 | * masked on the embedded IPv4 address. | |
| 23 | * | |
| 24 | * @param string $ip | |
| 25 | * The IP address. | |
| 26 | * @param int $mask_length | |
| 27 | * How many trailing IPv4 octets to remove. | |
| 28 | * | |
| 29 | * @return string | |
| 30 | * The masked address, or the original value when it is not an IP. | |
| 31 | */ | |
| 32 | public static function apply(string $ip, int $mask_length): string { | |
| 33 | 12 | if ($ip === '' || filter_var($ip, FILTER_VALIDATE_IP) === FALSE) { |
| 34 | 2 | return $ip; |
| 35 | } | |
| 36 | ||
| 37 | 10 | $packed = inet_pton($ip); |
| 38 | 10 | if ($packed === FALSE) { |
| 39 | 0 | return $ip; |
| 40 | } | |
| 41 | ||
| 42 | 10 | $mask_length = min(3, max(1, $mask_length)); |
| 43 | ||
| 44 | 10 | if (strlen($packed) === 4) { |
| 45 | 5 | self::zeroTrailing($packed, $mask_length); |
| 46 | } | |
| 47 | 5 | elseif (self::isMappedIpv4($packed)) { |
| 48 | 2 | $ipv4 = substr($packed, 12, 4); |
| 49 | 2 | self::zeroTrailing($ipv4, $mask_length); |
| 50 | 2 | $packed = substr($packed, 0, 12) . $ipv4; |
| 51 | } | |
| 52 | else { | |
| 53 | 3 | $keep_bytes = match ($mask_length) { |
| 54 | 1 | 1 => 8, |
| 55 | 1 | 2 => 6, |
| 56 | 1 | default => 4, |
| 57 | 3 | }; |
| 58 | 3 | self::zeroFrom($packed, $keep_bytes); |
| 59 | } | |
| 60 | ||
| 61 | 10 | $masked = inet_ntop($packed); |
| 62 | 10 | return $masked === FALSE ? $ip : $masked; |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Zeros the last bytes of a packed address. | |
| 67 | * | |
| 68 | * @param string $packed | |
| 69 | * The packed address, modified in place. | |
| 70 | * @param int $bytes | |
| 71 | * How many trailing bytes to zero. | |
| 72 | */ | |
| 73 | private static function zeroTrailing(string &$packed, int $bytes): void { | |
| 74 | 7 | self::zeroFrom($packed, strlen($packed) - $bytes); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Zeros a packed address from a byte offset to the end. | |
| 79 | * | |
| 80 | * @param string $packed | |
| 81 | * The packed address, modified in place. | |
| 82 | * @param int $offset | |
| 83 | * The first byte to zero. | |
| 84 | */ | |
| 85 | private static function zeroFrom(string &$packed, int $offset): void { | |
| 86 | 10 | $length = strlen($packed); |
| 87 | 10 | for ($i = max(0, $offset); $i < $length; $i++) { |
| 88 | 10 | $packed[$i] = "\0"; |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * Checks for an IPv4 address mapped into IPv6. | |
| 94 | * | |
| 95 | * @param string $packed | |
| 96 | * A 16-byte packed IPv6 address. | |
| 97 | * | |
| 98 | * @return bool | |
| 99 | * TRUE when the address is ::ffff:0:0/96. | |
| 100 | */ | |
| 101 | private static function isMappedIpv4(string $packed): bool { | |
| 102 | 5 | return strlen($packed) === 16 |
| 103 | 5 | && str_starts_with($packed, "\0\0\0\0\0\0\0\0\0\0\xff\xff"); |
| 104 | } | |
| 105 | ||
| 106 | } |