Skip to content

Commit

Permalink
[TASK] Use TYPO3 IpAnonymizationUtility
Browse files Browse the repository at this point in the history
  • Loading branch information
dkd-kaehm committed Oct 4, 2022
1 parent fbf456d commit 4aead0f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 58 deletions.
59 changes: 2 additions & 57 deletions Classes/Domain/Search/Statistics/StatisticsWriterProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\Util;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\IpAnonymizationUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ public function process(SearchResultSet $resultSet) {
'time_processing' => isset($response->debug->timing->process->time) ? $response->debug->timing->process->time : 0,
'feuser_id' => (int)$TSFE->fe_user->user['uid'],
'cookie' => $TSFE->fe_user->id ?? '',
'ip' => $this->applyIpMask((string)$this->getUserIp(), $ipMaskLength),
'ip' => IpAnonymizationUtility::anonymizeIp($this->getUserIp(), $ipMaskLength),
'page' => (int)$page,
'keywords' => $keywords,
'filters' => serialize($filters),
Expand Down Expand Up @@ -148,62 +149,6 @@ protected function sanitizeString($string)
return $string;
}

/**
* Internal function to mask portions of the visitor IP address
*
* @param string $ip IP address in network address format
* @param int $maskLength Number of octets to reset
* @return string
*/
protected function applyIpMask(string $ip, int $maskLength): string
{
if (empty($ip) || $maskLength === 0) {
return $ip;
}

// IPv4 or mapped IPv4 in IPv6
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return $this->applyIpV4Mask($ip, $maskLength);
}

return $this->applyIpV6Mask($ip, $maskLength);
}

/**
* Apply a mask filter on the ip v4 address.
*
* @param string $ip
* @param int $maskLength
* @return string
*/
protected function applyIpV4Mask($ip, $maskLength)
{
$i = strlen($ip);
if ($maskLength > $i) {
$maskLength = $i;
}

while ($maskLength-- > 0) {
$ip[--$i] = chr(0);
}
return (string)$ip;
}

/**
* Apply a mask filter on the ip v6 address.
*
* @param string $ip
* @param int $maskLength
* @return string
*/
protected function applyIpV6Mask($ip, $maskLength):string
{
$masks = ['ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'ffff:ffff:ffff:ffff::', 'ffff:ffff:ffff:0000::', 'ffff:ff00:0000:0000::'];
$packedAddress = inet_pton($masks[$maskLength]);
$binaryString = pack('a16', $packedAddress);
return (string)($ip & $binaryString);
}

/**
* @return TypoScriptFrontendController
*/
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Configuration/Reference/TxSolrStatistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ statistics.anonymizeIP
:Since: 2.0
:Default: 0

Defines the number of octets of the IP address to anonymize in the statistics log records.
Allowed values are 0 (masking disabled), 1 (mask host), 2 (mask host and subnet).

statistics.addDebugData
-----------------------
Expand Down
2 changes: 2 additions & 0 deletions Tests/Integration/Controller/SearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function setUp()
$this->searchRequest = $this->getPreparedRequest();
$this->searchResponse = $this->getPreparedResponse();
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPageSubstitutePageDocument'][PageFieldMappingIndexer::class] = PageFieldMappingIndexer::class;
$_SERVER['REMOTE_ADDR'] = '192.168.1.1';
}

/**
Expand All @@ -83,6 +84,7 @@ public function setUp()
public function tearDown()
{
$this->cleanUpSolrServerAndAssertEmpty();
unset($_SERVER['REMOTE_ADDR']);
parent::tearDown();
}

Expand Down

0 comments on commit 4aead0f

Please sign in to comment.