|
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 2 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
|
20 |
|
public function __construct($jsonEncodedParameters = null, SolrLogManager $solrLogManager = null, ExtensionConfiguration $extensionConfiguration = null) |
|
113
|
|
|
{ |
|
114
|
20 |
|
$this->requestId = uniqid(); |
|
115
|
20 |
|
$this->timeout = (float)ini_get('default_socket_timeout'); |
|
116
|
|
|
|
|
117
|
20 |
|
$this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
|
118
|
20 |
|
$this->extensionConfiguration = is_null($extensionConfiguration) ? GeneralUtility::makeInstance(ExtensionConfiguration::class) : $extensionConfiguration; |
|
119
|
|
|
|
|
120
|
20 |
|
if (is_null($jsonEncodedParameters)) { |
|
121
|
13 |
|
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[] = TYPO3_user_agent; |
|
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
|
|
|
* Adds an HTTP header to be send with the request. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $header HTTP header |
|
251
|
|
|
*/ |
|
252
|
|
|
public function addHeader($header) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->header[] = $header; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Checks whether this is a legitimate request coming from the Index Queue |
|
259
|
|
|
* page indexer worker task. |
|
260
|
|
|
* |
|
261
|
|
|
* @return bool TRUE if it's a legitimate request, FALSE otherwise. |
|
262
|
|
|
*/ |
|
263
|
2 |
|
public function isAuthenticated() |
|
264
|
|
|
{ |
|
265
|
2 |
|
$authenticated = false; |
|
266
|
|
|
|
|
267
|
2 |
|
if (is_null($this->parameters)) { |
|
268
|
|
|
return $authenticated; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
2 |
|
$calculatedHash = md5( |
|
272
|
2 |
|
$this->parameters['item'] . '|' . |
|
273
|
2 |
|
$this->parameters['page'] . '|' . |
|
274
|
2 |
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] |
|
275
|
|
|
); |
|
276
|
|
|
|
|
277
|
2 |
|
if ($this->parameters['hash'] === $calculatedHash) { |
|
278
|
1 |
|
$authenticated = true; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
2 |
|
return $authenticated; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Gets the list of actions to perform during page rendering. |
|
286
|
|
|
* |
|
287
|
|
|
* @return array List of actions |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getActions() |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->actions; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Gets the request's parameters. |
|
296
|
|
|
* |
|
297
|
|
|
* @return array Request parameters. |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getParameters() |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->parameters; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Gets the request's unique ID. |
|
306
|
|
|
* |
|
307
|
|
|
* @return string Unique request ID. |
|
308
|
|
|
*/ |
|
309
|
3 |
|
public function getRequestId() |
|
310
|
|
|
{ |
|
311
|
3 |
|
return $this->requestId; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Gets a specific parameter's value. |
|
316
|
|
|
* |
|
317
|
|
|
* @param string $parameterName The parameter to retrieve. |
|
318
|
|
|
* @return mixed NULL if a parameter was not set or it's value otherwise. |
|
319
|
|
|
*/ |
|
320
|
10 |
|
public function getParameter($parameterName) |
|
321
|
|
|
{ |
|
322
|
10 |
|
return isset($this->parameters[$parameterName]) ? $this->parameters[$parameterName] : null; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Sets a request's parameter and its value. |
|
327
|
|
|
* |
|
328
|
|
|
* @param string $parameter Parameter name |
|
329
|
|
|
* @param string $value Parameter value. |
|
330
|
|
|
*/ |
|
331
|
11 |
|
public function setParameter($parameter, $value) |
|
332
|
|
|
{ |
|
333
|
11 |
|
if (is_bool($value)) { |
|
334
|
1 |
|
$value = $value ? '1' : '0'; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
11 |
|
$this->parameters[$parameter] = $value; |
|
338
|
11 |
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Sets username and password to be used for a basic auth request header. |
|
342
|
|
|
* |
|
343
|
|
|
* @param string $username username. |
|
344
|
|
|
* @param string $password password. |
|
345
|
|
|
*/ |
|
346
|
1 |
|
public function setAuthorizationCredentials($username, $password) |
|
347
|
|
|
{ |
|
348
|
1 |
|
$this->username = $username; |
|
349
|
1 |
|
$this->password = $password; |
|
350
|
1 |
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Sets the Index Queue item this request is related to. |
|
354
|
|
|
* |
|
355
|
|
|
* @param Item $item Related Index Queue item. |
|
356
|
|
|
*/ |
|
357
|
6 |
|
public function setIndexQueueItem(Item $item) |
|
358
|
|
|
{ |
|
359
|
6 |
|
$this->indexQueueItem = $item; |
|
360
|
6 |
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Returns the request timeout in seconds |
|
364
|
|
|
* |
|
365
|
|
|
* @return float |
|
366
|
|
|
*/ |
|
367
|
1 |
|
public function getTimeout() |
|
368
|
|
|
{ |
|
369
|
1 |
|
return $this->timeout; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Sets the request timeout in seconds |
|
374
|
|
|
* |
|
375
|
|
|
* @param float $timeout Timeout seconds |
|
376
|
|
|
*/ |
|
377
|
|
|
public function setTimeout($timeout) |
|
378
|
|
|
{ |
|
379
|
|
|
$this->timeout = (float)$timeout; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Fetches a page by sending the configured headers. |
|
384
|
|
|
* |
|
385
|
|
|
* @param string $url |
|
386
|
|
|
* @param string[] $headers |
|
387
|
|
|
* @param float $timeout |
|
388
|
|
|
* @return string |
|
389
|
|
|
*/ |
|
390
|
1 |
|
protected function getUrl($url, $headers, $timeout) |
|
391
|
|
|
{ |
|
392
|
|
|
$options = [ |
|
393
|
|
|
'http' => [ |
|
394
|
1 |
|
'header' => implode(CRLF, $headers), |
|
395
|
1 |
|
'timeout' => $timeout |
|
396
|
|
|
], |
|
397
|
|
|
]; |
|
398
|
|
|
|
|
399
|
1 |
|
if ($this->extensionConfiguration->getIsSelfSignedCertificatesEnabled()) { |
|
400
|
|
|
$options['ssl'] = [ |
|
401
|
|
|
'verify_peer' => false, |
|
402
|
|
|
'allow_self_signed'=> true |
|
403
|
|
|
]; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
1 |
|
$context = stream_context_create($options); |
|
407
|
1 |
|
$rawResponse = file_get_contents($url, false, $context); |
|
408
|
1 |
|
return $rawResponse; |
|
409
|
|
|
} |
|
410
|
|
|
} |
|
411
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.