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 | * @var RequestFactory |
||
113 | */ |
||
114 | protected $requestFactory; |
||
115 | |||
116 | /** |
||
117 | * PageIndexerRequest constructor. |
||
118 | * |
||
119 | * @param string $jsonEncodedParameters json encoded header |
||
120 | * @param SolrLogManager|null $solrLogManager |
||
121 | * @param ExtensionConfiguration|null $extensionConfiguration |
||
122 | 21 | * @param RequestFactory|null $requestFactory |
|
123 | */ |
||
124 | 21 | public function __construct($jsonEncodedParameters = null, SolrLogManager $solrLogManager = null, ExtensionConfiguration $extensionConfiguration = null, RequestFactory $requestFactory = null) |
|
125 | 21 | { |
|
126 | $this->requestId = uniqid(); |
||
127 | 21 | $this->timeout = (float)ini_get('default_socket_timeout'); |
|
128 | 21 | ||
129 | 21 | $this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
|
130 | $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class); |
||
131 | 21 | $this->requestFactory = $requestFactory ?? GeneralUtility::makeInstance(RequestFactory::class); |
|
132 | 13 | ||
133 | if (is_null($jsonEncodedParameters)) { |
||
134 | return; |
||
135 | 8 | } |
|
136 | 8 | ||
137 | 8 | $this->parameters = (array)json_decode($jsonEncodedParameters, true); |
|
138 | $this->requestId = $this->parameters['requestId']; |
||
139 | 8 | unset($this->parameters['requestId']); |
|
140 | 8 | ||
141 | 8 | $actions = explode(',', $this->parameters['actions']); |
|
142 | foreach ($actions as $action) { |
||
143 | 8 | $this->addAction($action); |
|
144 | 8 | } |
|
145 | unset($this->parameters['actions']); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Adds an action to perform during page rendering. |
||
150 | * |
||
151 | 8 | * @param string $action Action name. |
|
152 | */ |
||
153 | 8 | public function addAction($action) |
|
154 | 8 | { |
|
155 | $this->actions[] = $action; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Executes the request. |
||
160 | * |
||
161 | * Uses headers to submit additional data and avoiding to have these |
||
162 | * arguments integrated into the URL when created by RealURL. |
||
163 | * |
||
164 | * @param string $url The URL to request. |
||
165 | 5 | * @return PageIndexerResponse Response |
|
166 | */ |
||
167 | public function send($url) |
||
168 | 5 | { |
|
169 | 5 | /** @var $response PageIndexerResponse */ |
|
170 | $response = GeneralUtility::makeInstance(PageIndexerResponse::class); |
||
171 | 4 | $decodedResponse = $this->getUrlAndDecodeResponse($url, $response); |
|
172 | 1 | ||
173 | 1 | if ($decodedResponse['requestId'] != $this->requestId) { |
|
174 | 1 | throw new \RuntimeException( |
|
175 | 'Request ID mismatch. Request ID was ' . $this->requestId . ', received ' . $decodedResponse['requestId'] . '. Are requests cached?', |
||
176 | 1351260655 |
||
177 | ); |
||
178 | 3 | } |
|
179 | |||
180 | 3 | $response->setRequestId($decodedResponse['requestId']); |
|
181 | |||
182 | if (!is_array($decodedResponse['actionResults'])) { |
||
183 | // nothing to parse |
||
184 | return $response; |
||
185 | 3 | } |
|
186 | 3 | ||
187 | foreach ($decodedResponse['actionResults'] as $action => $actionResult) { |
||
188 | $response->addActionResult($action, $actionResult); |
||
189 | 3 | } |
|
190 | |||
191 | return $response; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * This method is used to retrieve an url from the frontend and decode the response. |
||
196 | * |
||
197 | * @param string $url |
||
198 | * @param PageIndexerResponse $response |
||
199 | 5 | * @return mixed |
|
200 | */ |
||
201 | 5 | protected function getUrlAndDecodeResponse($url, PageIndexerResponse $response) |
|
202 | 5 | { |
|
203 | $headers = $this->getHeaders(); |
||
204 | 5 | $rawResponse = $this->getUrl($url, $headers, $this->timeout); |
|
205 | // convert JSON response to response object properties |
||
206 | 5 | $decodedResponse = $response->getResultsFromJson($rawResponse->getBody()->getContents()); |
|
207 | 1 | ||
208 | 1 | if ($rawResponse === false || $decodedResponse === false) { |
|
209 | 1 | $this->logger->log( |
|
210 | SolrLogManager::ERROR, |
||
211 | 1 | 'Failed to execute Page Indexer Request. Request ID: ' . $this->requestId, |
|
212 | 1 | [ |
|
213 | 1 | 'request ID' => $this->requestId, |
|
214 | 1 | 'request url' => $url, |
|
215 | 1 | 'request headers' => $headers, |
|
216 | 'response headers' => $rawResponse->getHeaders(), |
||
217 | 'raw response body' => $rawResponse->getBody()->getContents() |
||
218 | ] |
||
219 | 1 | ); |
|
220 | |||
221 | 4 | throw new \RuntimeException('Failed to execute Page Indexer Request. See log for details. Request ID: ' . $this->requestId, 1319116885); |
|
222 | } |
||
223 | return $decodedResponse; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Generates the headers to be send with the request. |
||
228 | * |
||
229 | 6 | * @return string[] Array of HTTP headers. |
|
230 | */ |
||
231 | 6 | public function getHeaders() |
|
232 | 6 | { |
|
233 | 6 | $headers = $this->header; |
|
234 | 6 | $headers[] = 'User-Agent: ' . $this->getUserAgent(); |
|
235 | $itemId = $this->indexQueueItem->getIndexQueueUid(); |
||
236 | $pageId = $this->indexQueueItem->getRecordPageId(); |
||
237 | 6 | ||
238 | 6 | $indexerRequestData = [ |
|
239 | 6 | 'requestId' => $this->requestId, |
|
240 | 6 | 'item' => $itemId, |
|
241 | 6 | 'page' => $pageId, |
|
242 | 6 | 'actions' => implode(',', $this->actions), |
|
243 | 6 | 'hash' => md5( |
|
244 | 6 | $itemId . '|' . |
|
245 | $pageId . '|' . |
||
246 | $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] |
||
247 | ) |
||
248 | 6 | ]; |
|
249 | 6 | ||
250 | $indexerRequestData = array_merge($indexerRequestData, $this->parameters); |
||
251 | 6 | $headers[] = self::SOLR_INDEX_HEADER . ': ' . json_encode($indexerRequestData, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES); |
|
252 | |||
253 | return $headers; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | 6 | * @return string |
|
258 | */ |
||
259 | 6 | 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 | $this->header[] = $header; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Checks whether this is a legitimate request coming from the Index Queue |
||
276 | * page indexer worker task. |
||
277 | * |
||
278 | 2 | * @return bool TRUE if it's a legitimate request, FALSE otherwise. |
|
279 | */ |
||
280 | 2 | public function isAuthenticated() |
|
281 | { |
||
282 | 2 | $authenticated = false; |
|
283 | |||
284 | if (is_null($this->parameters)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
285 | return $authenticated; |
||
286 | 2 | } |
|
287 | 2 | ||
288 | 2 | $calculatedHash = md5( |
|
289 | 2 | $this->parameters['item'] . '|' . |
|
290 | $this->parameters['page'] . '|' . |
||
291 | $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] |
||
292 | 2 | ); |
|
293 | 1 | ||
294 | if ($this->parameters['hash'] === $calculatedHash) { |
||
295 | $authenticated = true; |
||
296 | 2 | } |
|
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 | { |
||
318 | return $this->parameters; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Gets the request's unique ID. |
||
323 | * |
||
324 | 3 | * @return string Unique request ID. |
|
325 | */ |
||
326 | 3 | public function getRequestId() |
|
327 | { |
||
328 | return $this->requestId; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Gets a specific parameter's value. |
||
333 | * |
||
334 | * @param string $parameterName The parameter to retrieve. |
||
335 | 11 | * @return mixed NULL if a parameter was not set or it's value otherwise. |
|
336 | */ |
||
337 | 11 | public function getParameter($parameterName) |
|
338 | { |
||
339 | return isset($this->parameters[$parameterName]) ? $this->parameters[$parameterName] : null; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Sets a request's parameter and its value. |
||
344 | * |
||
345 | * @param string $parameter Parameter name |
||
346 | 11 | * @param string $value Parameter value. |
|
347 | */ |
||
348 | 11 | public function setParameter($parameter, $value) |
|
349 | { |
||
350 | if (is_bool($value)) { |
||
0 ignored issues
–
show
|
|||
351 | $value = $value ? '1' : '0'; |
||
352 | 11 | } |
|
353 | 11 | ||
354 | $this->parameters[$parameter] = $value; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Sets username and password to be used for a basic auth request header. |
||
359 | * |
||
360 | * @param string $username username. |
||
361 | 1 | * @param string $password password. |
|
362 | */ |
||
363 | 1 | public function setAuthorizationCredentials($username, $password) |
|
364 | 1 | { |
|
365 | 1 | $this->username = $username; |
|
366 | $this->password = $password; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Sets the Index Queue item this request is related to. |
||
371 | * |
||
372 | 6 | * @param Item $item Related Index Queue item. |
|
373 | */ |
||
374 | 6 | public function setIndexQueueItem(Item $item) |
|
375 | 6 | { |
|
376 | $this->indexQueueItem = $item; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Returns the request timeout in seconds |
||
381 | * |
||
382 | 1 | * @return float |
|
383 | */ |
||
384 | 1 | 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 | |||
399 | /** |
||
400 | * Fetches a page by sending the configured headers. |
||
401 | * |
||
402 | * @param string $url |
||
403 | * @param string[] $headers |
||
404 | * @param float $timeout |
||
405 | * @return ResponseInterface |
||
406 | 1 | * @throws \Exception |
|
407 | */ |
||
408 | protected function getUrl($url, $headers, $timeout): ResponseInterface |
||
409 | 1 | { |
|
410 | 1 | try { |
|
411 | $options = $this->buildGuzzleOptions($headers, $timeout); |
||
412 | $response = $this->requestFactory->request($url, 'GET', $options); |
||
413 | } catch (ClientException $e) { |
||
414 | $response = $e->getResponse(); |
||
415 | } catch (ServerException $e) { |
||
416 | $response = $e->getResponse(); |
||
417 | 1 | } |
|
418 | |||
419 | return $response; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Build the options array for the guzzle client. |
||
424 | * |
||
425 | * @param array $headers |
||
426 | * @param float $timeout |
||
427 | 1 | * @return array |
|
428 | */ |
||
429 | 1 | protected function buildGuzzleOptions($headers, $timeout) |
|
430 | { |
||
431 | 1 | $finalHeaders = []; |
|
432 | 1 | ||
433 | 1 | foreach ($headers as $header) { |
|
434 | list($name, $value) = explode(':', $header, 2); |
||
435 | $finalHeaders[$name] = trim($value); |
||
436 | 1 | } |
|
437 | 1 | ||
438 | 1 | $options = ['headers' => $finalHeaders, 'timeout' => $timeout]; |
|
439 | if (!empty($this->username) && !empty($this->password)) { |
||
440 | $options['auth'] = [$this->username, $this->password]; |
||
441 | 1 | } |
|
442 | |||
443 | if ($this->extensionConfiguration->getIsSelfSignedCertificatesEnabled()) { |
||
444 | $options['verify'] = false; |
||
445 | 1 | } |
|
446 | |||
447 | return $options; |
||
448 | } |
||
449 | } |
||
450 |