1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Statistics; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2009-2015 Ingo Renner <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetProcessor; |
30
|
|
|
use ApacheSolrForTypo3\Solr\HtmlContentExtractor; |
31
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; |
32
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
33
|
|
|
use TYPO3\CMS\Core\Context\Context; |
34
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
35
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Writes statistics after searches have been conducted. |
39
|
|
|
* |
40
|
|
|
* @author Ingo Renner <[email protected]> |
41
|
|
|
* @author Dimitri Ebert <[email protected]> |
42
|
|
|
* @author Timo Hund <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class StatisticsWriterProcessor implements SearchResultSetProcessor |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var StatisticsRepository |
48
|
|
|
*/ |
49
|
|
|
protected $statisticsRepository; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var SiteRepository |
53
|
|
|
*/ |
54
|
|
|
protected $siteRepository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param StatisticsRepository $statisticsRepository |
58
|
|
|
* @param SiteRepository $siteRepository |
59
|
|
|
*/ |
60
|
1 |
|
public function __construct(StatisticsRepository $statisticsRepository = null, SiteRepository $siteRepository = null) |
61
|
|
|
{ |
62
|
1 |
|
$this->statisticsRepository = $statisticsRepository ?? GeneralUtility::makeInstance(StatisticsRepository::class); |
63
|
1 |
|
$this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param SearchResultSet $resultSet |
68
|
|
|
* @return SearchResultSet |
69
|
|
|
*/ |
70
|
1 |
|
public function process(SearchResultSet $resultSet) { |
71
|
1 |
|
$searchRequest = $resultSet->getUsedSearchRequest(); |
72
|
1 |
|
$response = $resultSet->getResponse(); |
73
|
1 |
|
$configuration = $searchRequest->getContextTypoScriptConfiguration(); |
74
|
1 |
|
$keywords = $this->getProcessedKeywords($resultSet->getUsedQuery(), $configuration->getSearchFrequentSearchesUseLowercaseKeywords()); |
75
|
|
|
|
76
|
1 |
|
if (empty($keywords)) { |
77
|
|
|
// do not track empty queries |
78
|
|
|
return $resultSet; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$filters = $searchRequest->getActiveFacets(); |
82
|
1 |
|
$sorting = $this->sanitizeString($searchRequest->getSorting()); |
83
|
1 |
|
$page = (int)$searchRequest->getPage(); |
84
|
1 |
|
$ipMaskLength = (int)$configuration->getStatisticsAnonymizeIP(); |
85
|
|
|
|
86
|
1 |
|
$TSFE = $this->getTSFE(); |
87
|
1 |
|
$root_pid = $this->siteRepository->getSiteByPageId($TSFE->id)->getRootPageId(); |
|
|
|
|
88
|
1 |
|
$statisticData = [ |
89
|
1 |
|
'pid' => $TSFE->id, |
90
|
1 |
|
'root_pid' => $root_pid, |
91
|
1 |
|
'tstamp' => $this->getTime(), |
92
|
1 |
|
'language' => Util::getLanguageUid(), |
93
|
|
|
// @extensionScannerIgnoreLine |
94
|
1 |
|
'num_found' => (int)$resultSet->getAllResultCount(), |
95
|
1 |
|
'suggestions_shown' => is_object($response->spellcheck->suggestions ?? null) ? (int)get_object_vars($response->spellcheck->suggestions) : 0, |
96
|
|
|
// @extensionScannerIgnoreLine |
97
|
1 |
|
'time_total' => $response->debug->timing->time ?? 0, |
98
|
|
|
// @extensionScannerIgnoreLine |
99
|
1 |
|
'time_preparation' => $response->debug->timing->prepare->time ?? 0, |
100
|
|
|
// @extensionScannerIgnoreLine |
101
|
1 |
|
'time_processing' => $response->debug->timing->process->time ?? 0, |
102
|
1 |
|
'feuser_id' => isset($TSFE->fe_user->user) ? (int)$TSFE->fe_user->user['uid'] ?? 0 : 0, |
103
|
1 |
|
'cookie' => $TSFE->fe_user->id ?? '', |
104
|
1 |
|
'ip' => $this->applyIpMask((string)$this->getUserIp(), $ipMaskLength), |
105
|
1 |
|
'page' => (int)$page, |
106
|
1 |
|
'keywords' => $keywords, |
107
|
1 |
|
'filters' => serialize($filters), |
108
|
1 |
|
'sorting' => $sorting, |
109
|
1 |
|
'parameters' => isset($response->responseHeader->params) ? serialize($response->responseHeader->params) : '' |
110
|
|
|
]; |
111
|
|
|
|
112
|
1 |
|
$this->statisticsRepository->saveStatisticsRecord($statisticData); |
113
|
|
|
|
114
|
1 |
|
return $resultSet; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Query $query |
119
|
|
|
* @param boolean $lowerCaseQuery |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
|
protected function getProcessedKeywords(Query $query, $lowerCaseQuery = false) |
123
|
|
|
{ |
124
|
1 |
|
$keywords = $query->getQuery(); |
125
|
1 |
|
$keywords = $this->sanitizeString($keywords); |
126
|
1 |
|
if ($lowerCaseQuery) { |
127
|
|
|
$keywords = mb_strtolower($keywords); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $keywords; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sanitizes a string |
135
|
|
|
* |
136
|
|
|
* @param $string String to sanitize |
137
|
|
|
* @return string Sanitized string |
138
|
|
|
*/ |
139
|
1 |
|
protected function sanitizeString($string): string |
140
|
|
|
{ |
141
|
|
|
// clean content |
142
|
1 |
|
$string = HtmlContentExtractor::cleanContent($string); |
143
|
1 |
|
$string = htmlspecialchars(strip_tags($string), ENT_QUOTES, 'UTF-8'); // after entity decoding we might have tags again |
144
|
1 |
|
return trim($string); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Internal function to mask portions of the visitor IP address |
149
|
|
|
* |
150
|
|
|
* @param string $ip IP address in network address format |
151
|
|
|
* @param int $maskLength Number of octets to reset |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
1 |
|
protected function applyIpMask(string $ip, int $maskLength): string |
155
|
|
|
{ |
156
|
1 |
|
if (empty($ip) || $maskLength === 0) { |
157
|
1 |
|
return $ip; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// IPv4 or mapped IPv4 in IPv6 |
161
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
162
|
|
|
return $this->applyIpV4Mask($ip, $maskLength); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->applyIpV6Mask($ip, $maskLength); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Apply a mask filter on the ip v4 address. |
170
|
|
|
* |
171
|
|
|
* @param string $ip |
172
|
|
|
* @param int $maskLength |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function applyIpV4Mask($ip, $maskLength) |
176
|
|
|
{ |
177
|
|
|
$i = strlen($ip); |
178
|
|
|
if ($maskLength > $i) { |
179
|
|
|
$maskLength = $i; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
while ($maskLength-- > 0) { |
183
|
|
|
$ip[--$i] = chr(0); |
184
|
|
|
} |
185
|
|
|
return (string)$ip; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Apply a mask filter on the ip v6 address. |
190
|
|
|
* |
191
|
|
|
* @param string $ip |
192
|
|
|
* @param int $maskLength |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
protected function applyIpV6Mask($ip, $maskLength):string |
196
|
|
|
{ |
197
|
|
|
$masks = ['ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'ffff:ffff:ffff:ffff::', 'ffff:ffff:ffff:0000::', 'ffff:ff00:0000:0000::']; |
198
|
|
|
$packedAddress = inet_pton($masks[$maskLength]); |
199
|
|
|
$binaryString = pack('a16', $packedAddress); |
200
|
|
|
return (string)($ip & $binaryString); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return TypoScriptFrontendController |
205
|
|
|
*/ |
206
|
|
|
protected function getTSFE() |
207
|
|
|
{ |
208
|
|
|
return $GLOBALS['TSFE']; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
protected function getUserIp() |
215
|
|
|
{ |
216
|
|
|
return GeneralUtility::getIndpEnv('REMOTE_ADDR'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
protected function getTime() |
223
|
|
|
{ |
224
|
|
|
return GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp'); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|