‪TYPO3CMS  10.4
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 {
27 
36  const ‪MASKV4 = [
37  1 => '255.255.255.0',
38  2 => '255.255.0.0'
39  ];
40 
49  const ‪MASKV6 = [
50  1 => 'ffff:ffff:ffff:ffff:0000:0000:0000:0000',
51  2 => 'ffff:ffff:ffff:0000:0000:0000:0000:0000',
52  ];
53 
62  public static function ‪anonymizeIp(string $address, int $mask = null): string
63  {
64  if ($mask === null) {
65  $mask = (int)‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ipAnonymization'];
66  }
67  if ($mask < 0 || $mask > 2) {
68  throw new \UnexpectedValueException(sprintf('The provided value "%d" is not an allowed value for the IP mask.', $mask), 1519739203);
69  }
70  if ($mask === 0) {
71  return $address;
72  }
73  if (empty($address)) {
74  return '';
75  }
76 
77  $packedAddress = @inet_pton($address);
78  if ($packedAddress === false) {
79  return '';
80  }
81  $length = strlen($packedAddress);
82 
83  if ($length === 4) {
84  $bitMask = self::MASKV4[$mask];
85  } elseif ($length === 16) {
86  $bitMask = self::MASKV6[$mask];
87  } else {
88  return '';
89  }
90  return inet_ntop($packedAddress & inet_pton($bitMask));
91  }
92 }
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\MASKV6
‪const MASKV6
Definition: IpAnonymizationUtility.php:49
‪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:5
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\anonymizeIp
‪static string anonymizeIp(string $address, int $mask=null)
Definition: IpAnonymizationUtility.php:62
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\MASKV4
‪const MASKV4
Definition: IpAnonymizationUtility.php:36