Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save htuscher/7237063 to your computer and use it in GitHub Desktop.
Save htuscher/7237063 to your computer and use it in GitHub Desktop.
Solr Statistic anonymizeIP
<?php
/**
* Copyright notice
*
* (c) typovision GmbH, www.typovision.de
*
* @author Hans Höchtl <hans.hoechtl@typovision.de>
*
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
*/
/**
* Class ux_tx_solr_response_processor_StatisticsWriter
* @author Hans Höchtl <hans.hoechtl@typovision.de>
*/
class ux_tx_solr_response_processor_StatisticsWriter extends tx_solr_response_processor_StatisticsWriter {
/**
* Internal function to mask portions of the visitor IP address
*
* @param string $ip IP address in network address format
* @param integer $maskLength Number of octets to reset
* @return string
*/
protected function applyIPMask($ip, $maskLength)
{
// IPv4 or mapped IPv4 in IPv6
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$i = strlen($ip);
if ($maskLength > $i) {
$maskLength = $i;
}
while ($maskLength-- > 0) {
$ip[--$i] = chr(0);
}
} else {
$masks = array(
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
'ffff:ffff:ffff:ffff::',
'ffff:ffff:ffff:0000::',
'ffff:ff00:0000:0000::'
);
return $ip & pack('a16', inet_pton($masks[$maskLength]));
}
return $ip;
}
/**
* Processes a query and its response after searching for that query.
*
* @param tx_solr_Query The query that has been searched for.
* @param Apache_Solr_Response The response for the last query.
*/
public function processResponse(tx_solr_Query $query, Apache_Solr_Response $response) {
$urlParameters = t3lib_div::_GP('tx_solr');
$keywords = $query->getKeywords();
$filters = isset($urlParameters['filter']) ? $urlParameters['filter'] : array();
if (empty($keywords)) {
// do not track empty queries
return;
}
$keywords = t3lib_div::removeXSS($keywords);
$keywords = htmlentities($keywords, ENT_QUOTES, $GLOBALS['TSFE']->metaCharset);
$insertFields = array(
'pid' => $GLOBALS['TSFE']->id,
'root_pid' => $GLOBALS['TSFE']->tmpl->rootLine[0]['uid'],
'tstamp' => $GLOBALS['EXEC_TIME'],
'language' => $GLOBALS['TSFE']->sys_language_uid,
'num_found' => $response->response->numFound,
'suggestions_shown' => (int) get_object_vars($response->spellcheck->suggestions),
'time_total' => $response->debug->timing->time,
'time_preparation' => $response->debug->timing->prepare->time,
'time_processing' => $response->debug->timing->process->time,
'feuser_id' => (int) $GLOBALS['TSFE']->fe_user->user['uid'],
'cookie' => $GLOBALS['TSFE']->fe_user->id,
'ip' => $this->applyIPMask(t3lib_div::getIndpEnv('REMOTE_ADDR'),1),
'page' => (int) $urlParameters['page'],
'keywords' => $keywords,
'filters' => serialize($filters),
'sorting' => $urlParameters['sort'], // FIXME sanitize me!
'parameters' => serialize($response->responseHeader->params)
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_solr_statistics', $insertFields);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/solr/classes/response/processor/class.tx_solr_response_processor_statisticswriter.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/solr/classes/response/processor/class.tx_solr_response_processor_statisticswriter.php']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment