1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\IndexQueue; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2010-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\System\Configuration\ExtensionConfiguration; |
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Index Queue Page Indexer request with details about which actions to perform. |
33
|
|
|
* |
34
|
|
|
* @author Ingo Renner <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class PageIndexerRequest |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List of actions to perform during page rendering. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $actions = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parameters as sent from the Index Queue page indexer. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $parameters = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Headers as sent from the Index Queue page indexer. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $header = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Unique request ID. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $requestId; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Username to use for basic auth protected URLs. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $username = ''; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Password to use for basic auth protected URLs. |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
protected $password = ''; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* An Index Queue item related to this request. |
83
|
|
|
* |
84
|
|
|
* @var Item |
85
|
|
|
*/ |
86
|
|
|
protected $indexQueueItem = null; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Request timeout in seconds |
90
|
|
|
* |
91
|
|
|
* @var float |
92
|
|
|
*/ |
93
|
|
|
protected $timeout; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
97
|
|
|
*/ |
98
|
|
|
protected $logger = null; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var ExtensionConfiguration |
102
|
|
|
*/ |
103
|
|
|
protected $extensionConfiguration; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* PageIndexerRequest constructor. |
107
|
|
|
* |
108
|
|
|
* @param string $jsonEncodedParameters json encoded header |
109
|
|
|
* @param SolrLogManager|null $solrLogManager |
110
|
|
|
* @param ExtensionConfiguration|null $extensionConfiguration |
111
|
|
|
*/ |
112
|
21 |
|
public function __construct($jsonEncodedParameters = null, SolrLogManager $solrLogManager = null, ExtensionConfiguration $extensionConfiguration = null) |
113
|
|
|
{ |
114
|
21 |
|
$this->requestId = uniqid(); |
115
|
21 |
|
$this->timeout = (float)ini_get('default_socket_timeout'); |
116
|
|
|
|
117
|
21 |
|
$this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
118
|
21 |
|
$this->extensionConfiguration = is_null($extensionConfiguration) ? GeneralUtility::makeInstance(ExtensionConfiguration::class) : $extensionConfiguration; |
119
|
|
|
|
120
|
21 |
|
if (is_null($jsonEncodedParameters)) { |
121
|
14 |
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
$this->parameters = (array)json_decode($jsonEncodedParameters, true); |
125
|
7 |
|
$this->requestId = $this->parameters['requestId']; |
126
|
7 |
|
unset($this->parameters['requestId']); |
127
|
|
|
|
128
|
7 |
|
$actions = explode(',', $this->parameters['actions']); |
129
|
7 |
|
foreach ($actions as $action) { |
130
|
7 |
|
$this->addAction($action); |
131
|
|
|
} |
132
|
7 |
|
unset($this->parameters['actions']); |
133
|
7 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adds an action to perform during page rendering. |
137
|
|
|
* |
138
|
|
|
* @param string $action Action name. |
139
|
|
|
*/ |
140
|
8 |
|
public function addAction($action) |
141
|
|
|
{ |
142
|
8 |
|
$this->actions[] = $action; |
143
|
8 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Executes the request. |
147
|
|
|
* |
148
|
|
|
* Uses headers to submit additional data and avoiding to have these |
149
|
|
|
* arguments integrated into the URL when created by RealURL. |
150
|
|
|
* |
151
|
|
|
* @param string $url The URL to request. |
152
|
|
|
* @return PageIndexerResponse Response |
153
|
|
|
*/ |
154
|
5 |
|
public function send($url) |
155
|
|
|
{ |
156
|
|
|
/** @var $response PageIndexerResponse */ |
157
|
5 |
|
$response = GeneralUtility::makeInstance(PageIndexerResponse::class); |
158
|
5 |
|
$decodedResponse = $this->getUrlAndDecodeResponse($url, $response); |
159
|
|
|
|
160
|
4 |
|
if ($decodedResponse['requestId'] != $this->requestId) { |
161
|
1 |
|
throw new \RuntimeException( |
162
|
1 |
|
'Request ID mismatch. Request ID was ' . $this->requestId . ', received ' . $decodedResponse['requestId'] . '. Are requests cached?', |
163
|
1 |
|
1351260655 |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
3 |
|
$response->setRequestId($decodedResponse['requestId']); |
168
|
|
|
|
169
|
3 |
|
if (!is_array($decodedResponse['actionResults'])) { |
170
|
|
|
// nothing to parse |
171
|
|
|
return $response; |
172
|
|
|
} |
173
|
|
|
|
174
|
3 |
|
foreach ($decodedResponse['actionResults'] as $action => $actionResult) { |
175
|
3 |
|
$response->addActionResult($action, $actionResult); |
176
|
|
|
} |
177
|
|
|
|
178
|
3 |
|
return $response; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* This method is used to retrieve an url from the frontend and decode the response. |
183
|
|
|
* |
184
|
|
|
* @param string $url |
185
|
|
|
* @param PageIndexerResponse $response |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
5 |
|
protected function getUrlAndDecodeResponse($url, PageIndexerResponse $response) |
189
|
|
|
{ |
190
|
5 |
|
$headers = $this->getHeaders(); |
191
|
5 |
|
$rawResponse = $this->getUrl($url, $headers, $this->timeout); |
192
|
|
|
// convert JSON response to response object properties |
193
|
5 |
|
$decodedResponse = $response->getResultsFromJson($rawResponse); |
194
|
|
|
|
195
|
5 |
|
if ($rawResponse === false || $decodedResponse === false) { |
196
|
1 |
|
$this->logger->log( |
197
|
1 |
|
SolrLogManager::ERROR, |
198
|
1 |
|
'Failed to execute Page Indexer Request. Request ID: ' . $this->requestId, |
199
|
|
|
[ |
200
|
1 |
|
'request ID' => $this->requestId, |
201
|
1 |
|
'request url' => $url, |
202
|
1 |
|
'request headers' => $headers, |
203
|
1 |
|
'response headers' => $http_response_header, // automatically defined by file_get_contents() |
204
|
1 |
|
'raw response body' => $rawResponse |
205
|
|
|
] |
206
|
|
|
); |
207
|
|
|
|
208
|
1 |
|
throw new \RuntimeException('Failed to execute Page Indexer Request. See log for details. Request ID: ' . $this->requestId, 1319116885); |
209
|
|
|
} |
210
|
4 |
|
return $decodedResponse; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Generates the headers to be send with the request. |
215
|
|
|
* |
216
|
|
|
* @return string[] Array of HTTP headers. |
217
|
|
|
*/ |
218
|
6 |
|
public function getHeaders() |
219
|
|
|
{ |
220
|
6 |
|
$headers = $this->header; |
221
|
6 |
|
$headers[] = $this->getUserAgent(); |
222
|
6 |
|
$itemId = $this->indexQueueItem->getIndexQueueUid(); |
223
|
6 |
|
$pageId = $this->indexQueueItem->getRecordPageId(); |
224
|
|
|
|
225
|
|
|
$indexerRequestData = [ |
226
|
6 |
|
'requestId' => $this->requestId, |
227
|
6 |
|
'item' => $itemId, |
228
|
6 |
|
'page' => $pageId, |
229
|
6 |
|
'actions' => implode(',', $this->actions), |
230
|
6 |
|
'hash' => md5( |
231
|
6 |
|
$itemId . '|' . |
232
|
6 |
|
$pageId . '|' . |
233
|
6 |
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] |
234
|
|
|
) |
235
|
|
|
]; |
236
|
|
|
|
237
|
6 |
|
$indexerRequestData = array_merge($indexerRequestData, $this->parameters); |
238
|
6 |
|
$headers[] = 'X-Tx-Solr-Iq: ' . json_encode($indexerRequestData); |
239
|
|
|
|
240
|
6 |
|
if (!empty($this->username) && !empty($this->password)) { |
241
|
1 |
|
$headers[] = 'Authorization: Basic ' . base64_encode($this->username . ':' . $this->password); |
242
|
|
|
} |
243
|
|
|
|
244
|
6 |
|
return $headers; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
6 |
|
protected function getUserAgent() |
251
|
|
|
{ |
252
|
6 |
|
return $GLOBALS['TYPO3_CONF_VARS']['HTTP']['headers']['User-Agent'] ?? 'TYPO3'; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Adds an HTTP header to be send with the request. |
257
|
|
|
* |
258
|
|
|
* @param string $header HTTP header |
259
|
|
|
*/ |
260
|
|
|
public function addHeader($header) |
261
|
|
|
{ |
262
|
|
|
$this->header[] = $header; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Checks whether this is a legitimate request coming from the Index Queue |
267
|
|
|
* page indexer worker task. |
268
|
|
|
* |
269
|
|
|
* @return bool TRUE if it's a legitimate request, FALSE otherwise. |
270
|
|
|
*/ |
271
|
2 |
|
public function isAuthenticated() |
272
|
|
|
{ |
273
|
2 |
|
$authenticated = false; |
274
|
|
|
|
275
|
2 |
|
if (is_null($this->parameters)) { |
276
|
|
|
return $authenticated; |
277
|
|
|
} |
278
|
|
|
|
279
|
2 |
|
$calculatedHash = md5( |
280
|
2 |
|
$this->parameters['item'] . '|' . |
281
|
2 |
|
$this->parameters['page'] . '|' . |
282
|
2 |
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] |
283
|
|
|
); |
284
|
|
|
|
285
|
2 |
|
if ($this->parameters['hash'] === $calculatedHash) { |
286
|
1 |
|
$authenticated = true; |
287
|
|
|
} |
288
|
|
|
|
289
|
2 |
|
return $authenticated; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Gets the list of actions to perform during page rendering. |
294
|
|
|
* |
295
|
|
|
* @return array List of actions |
296
|
|
|
*/ |
297
|
|
|
public function getActions() |
298
|
|
|
{ |
299
|
|
|
return $this->actions; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Gets the request's parameters. |
304
|
|
|
* |
305
|
|
|
* @return array Request parameters. |
306
|
|
|
*/ |
307
|
|
|
public function getParameters() |
308
|
|
|
{ |
309
|
|
|
return $this->parameters; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Gets the request's unique ID. |
314
|
|
|
* |
315
|
|
|
* @return string Unique request ID. |
316
|
|
|
*/ |
317
|
3 |
|
public function getRequestId() |
318
|
|
|
{ |
319
|
3 |
|
return $this->requestId; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Gets a specific parameter's value. |
324
|
|
|
* |
325
|
|
|
* @param string $parameterName The parameter to retrieve. |
326
|
|
|
* @return mixed NULL if a parameter was not set or it's value otherwise. |
327
|
|
|
*/ |
328
|
11 |
|
public function getParameter($parameterName) |
329
|
|
|
{ |
330
|
11 |
|
return isset($this->parameters[$parameterName]) ? $this->parameters[$parameterName] : null; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Sets a request's parameter and its value. |
335
|
|
|
* |
336
|
|
|
* @param string $parameter Parameter name |
337
|
|
|
* @param string $value Parameter value. |
338
|
|
|
*/ |
339
|
12 |
|
public function setParameter($parameter, $value) |
340
|
|
|
{ |
341
|
12 |
|
if (is_bool($value)) { |
342
|
1 |
|
$value = $value ? '1' : '0'; |
343
|
|
|
} |
344
|
|
|
|
345
|
12 |
|
$this->parameters[$parameter] = $value; |
346
|
12 |
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Sets username and password to be used for a basic auth request header. |
350
|
|
|
* |
351
|
|
|
* @param string $username username. |
352
|
|
|
* @param string $password password. |
353
|
|
|
*/ |
354
|
1 |
|
public function setAuthorizationCredentials($username, $password) |
355
|
|
|
{ |
356
|
1 |
|
$this->username = $username; |
357
|
1 |
|
$this->password = $password; |
358
|
1 |
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Sets the Index Queue item this request is related to. |
362
|
|
|
* |
363
|
|
|
* @param Item $item Related Index Queue item. |
364
|
|
|
*/ |
365
|
6 |
|
public function setIndexQueueItem(Item $item) |
366
|
|
|
{ |
367
|
6 |
|
$this->indexQueueItem = $item; |
368
|
6 |
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Returns the request timeout in seconds |
372
|
|
|
* |
373
|
|
|
* @return float |
374
|
|
|
*/ |
375
|
1 |
|
public function getTimeout() |
376
|
|
|
{ |
377
|
1 |
|
return $this->timeout; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Sets the request timeout in seconds |
382
|
|
|
* |
383
|
|
|
* @param float $timeout Timeout seconds |
384
|
|
|
*/ |
385
|
|
|
public function setTimeout($timeout) |
386
|
|
|
{ |
387
|
|
|
$this->timeout = (float)$timeout; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Fetches a page by sending the configured headers. |
392
|
|
|
* |
393
|
|
|
* @param string $url |
394
|
|
|
* @param string[] $headers |
395
|
|
|
* @param float $timeout |
396
|
|
|
* @return string |
397
|
|
|
*/ |
398
|
1 |
|
protected function getUrl($url, $headers, $timeout) |
399
|
|
|
{ |
400
|
|
|
$options = [ |
401
|
|
|
'http' => [ |
402
|
1 |
|
'header' => implode(CRLF, $headers), |
403
|
1 |
|
'timeout' => $timeout |
404
|
|
|
], |
405
|
|
|
]; |
406
|
|
|
|
407
|
1 |
|
if ($this->extensionConfiguration->getIsSelfSignedCertificatesEnabled()) { |
408
|
|
|
$options['ssl'] = [ |
409
|
|
|
'verify_peer' => false, |
410
|
|
|
'allow_self_signed'=> true |
411
|
|
|
]; |
412
|
|
|
} |
413
|
|
|
|
414
|
1 |
|
$context = stream_context_create($options); |
415
|
1 |
|
$rawResponse = file_get_contents($url, false, $context); |
416
|
1 |
|
return $rawResponse; |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|