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