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