TYPO3 CMS  TYPO3_7-6
IpAnonymizationUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 {
24 
34  public static $MASKV4 = [
35  1 => '255.255.255.0',
36  2 => '255.255.0.0'
37  ];
38 
48  public static $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($address, $mask = null)
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  $length = strlen($packedAddress);
78 
79  if ($length === 4) {
80  $bitMask = self::$MASKV4[$mask];
81  } elseif ($length === 16) {
82  $bitMask = self::$MASKV6[$mask];
83  } else {
84  return '';
85  }
86  return inet_ntop($packedAddress & inet_pton($bitMask));
87  }
88 }
static static static anonymizeIp($address, $mask=null)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']