‪TYPO3CMS  ‪main
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 
60  public static function ‪anonymizeIp(string $address, int $mask = null): string
61  {
62  if ($mask === null) {
63  $mask = (int)‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ipAnonymization'];
64  }
65  if ($mask < 0 || $mask > 2) {
66  throw new \UnexpectedValueException(sprintf('The provided value "%d" is not an allowed value for the IP mask.', $mask), 1519739203);
67  }
68  if ($mask === 0) {
69  return $address;
70  }
71  if (empty($address)) {
72  return '';
73  }
74 
75  $packedAddress = @inet_pton($address);
76  if ($packedAddress === false) {
77  return '';
78  }
79  $length = strlen($packedAddress);
80 
81  if ($length === 4) {
82  $bitMask = self::MASKV4[$mask];
83  } elseif ($length === 16) {
84  $bitMask = self::MASKV6[$mask];
85  } else {
86  return '';
87  }
88  return inet_ntop($packedAddress & inet_pton($bitMask));
89  }
90 }
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\MASKV6
‪const MASKV6
Definition: IpAnonymizationUtility.php:48
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility
Definition: IpAnonymizationUtility.php:26
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\anonymizeIp
‪static anonymizeIp(string $address, int $mask=null)
Definition: IpAnonymizationUtility.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\MASKV4
‪const MASKV4
Definition: IpAnonymizationUtility.php:35