Coverage index

src/Helper/IpAnonymizer.php

Lines: 96.15%

LineHitsSource
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 {
3312
    if ($ip === '' || filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
342
      return $ip;
35
    }
36
3710
    $packed = inet_pton($ip);
3810
    if ($packed === FALSE) {
390
      return $ip;
40
    }
41
4210
    $mask_length = min(3, max(1, $mask_length));
43
4410
    if (strlen($packed) === 4) {
455
      self::zeroTrailing($packed, $mask_length);
46
    }
475
    elseif (self::isMappedIpv4($packed)) {
482
      $ipv4 = substr($packed, 12, 4);
492
      self::zeroTrailing($ipv4, $mask_length);
502
      $packed = substr($packed, 0, 12) . $ipv4;
51
    }
52
    else {
533
      $keep_bytes = match ($mask_length) {
541
        1 => 8,
551
        2 => 6,
561
        default => 4,
573
      };
583
      self::zeroFrom($packed, $keep_bytes);
59
    }
60
6110
    $masked = inet_ntop($packed);
6210
    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 {
747
    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 {
8610
    $length = strlen($packed);
8710
    for ($i = max(0, $offset); $i < $length; $i++) {
8810
      $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 {
1025
    return strlen($packed) === 16
1035
      && str_starts_with($packed, "\0\0\0\0\0\0\0\0\0\0\xff\xff");
104
  }
105
106
}