‪TYPO3CMS  11.5
IpAnonymizationUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 {
35  public const ‪MASKV4 = [
36  1 => '255.255.255.0',
37  2 => '255.255.0.0',
38  ];
39 
48  public const ‪MASKV6 = [
49  1 => 'ffff:ffff:ffff:ffff:0000:0000:0000:0000',
50  2 => 'ffff:ffff:ffff:0000:0000:0000:0000:0000',
51  ];
52 
61  public static function ‪anonymizeIp(string $address, int $mask = null): string
62  {
63  if ($mask === null) {
64  $mask = (int)‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ipAnonymization'];
65  }
66  if ($mask < 0 || $mask > 2) {
67  throw new \UnexpectedValueException(sprintf('The provided value "%d" is not an allowed value for the IP mask.', $mask), 1519739203);
68  }
69  if ($mask === 0) {
70  return $address;
71  }
72  if (empty($address)) {
73  return '';
74  }
75 
76  $packedAddress = @inet_pton($address);
77  if ($packedAddress === false) {
78  return '';
79  }
80  $length = strlen($packedAddress);
81 
82  if ($length === 4) {
83  $bitMask = self::MASKV4[$mask];
84  } elseif ($length === 16) {
85  $bitMask = self::MASKV6[$mask];
86  } else {
87  return '';
88  }
89  return inet_ntop($packedAddress & inet_pton($bitMask));
90  }
91 }
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\MASKV6
‪const MASKV6
Definition: IpAnonymizationUtility.php:48
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility
Definition: IpAnonymizationUtility.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\anonymizeIp
‪static string anonymizeIp(string $address, int $mask=null)
Definition: IpAnonymizationUtility.php:61
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\MASKV4
‪const MASKV4
Definition: IpAnonymizationUtility.php:35