Passed
Push — master ( 6d83d0...d2efa3 )
by Timo
45:45 queued 16:20
created

PageIndexerRequest::getRequestId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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
     * @param RequestFactory|null $requestFactory
123
     */
124 21
    public function __construct($jsonEncodedParameters = null, SolrLogManager $solrLogManager = null, ExtensionConfiguration $extensionConfiguration = null, RequestFactory $requestFactory = null)
125
    {
126 21
        $this->requestId = uniqid();
127 21
        $this->timeout = (float)ini_get('default_socket_timeout');
128
129 21
        $this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
130 21
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
131 21
        $this->requestFactory = $requestFactory ?? GeneralUtility::makeInstance(RequestFactory::class);
132
133 21
        if (is_null($jsonEncodedParameters)) {
134 13
            return;
135
        }
136
137 8
        $this->parameters = (array)json_decode($jsonEncodedParameters, true);
138 8
        $this->requestId = $this->parameters['requestId'];
139 8
        unset($this->parameters['requestId']);
140
141 8
        $actions = explode(',', $this->parameters['actions']);
142 8
        foreach ($actions as $action) {
143 8
            $this->addAction($action);
144
        }
145 8
        unset($this->parameters['actions']);
146 8
    }
147
148
    /**
149
     * Adds an action to perform during page rendering.
150
     *
151
     * @param string $action Action name.
152
     */
153 8
    public function addAction($action)
154
    {
155 8
        $this->actions[] = $action;
156 8
    }
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
     * @return PageIndexerResponse Response
166
     */
167 5
    public function send($url)
168
    {
169
        /** @var $response PageIndexerResponse */
170 5
        $response = GeneralUtility::makeInstance(PageIndexerResponse::class);
171 5
        $decodedResponse = $this->getUrlAndDecodeResponse($url, $response);
172
173 4
        if ($decodedResponse['requestId'] != $this->requestId) {
174 1
            throw new \RuntimeException(
175 1
                'Request ID mismatch. Request ID was ' . $this->requestId . ', received ' . $decodedResponse['requestId'] . '. Are requests cached?',
176 1
                1351260655
177
            );
178
        }
179
180 3
        $response->setRequestId($decodedResponse['requestId']);
181
182 3
        if (!is_array($decodedResponse['actionResults'])) {
183
            // nothing to parse
184
            return $response;
185
        }
186
187 3
        foreach ($decodedResponse['actionResults'] as $action => $actionResult) {
188 3
            $response->addActionResult($action, $actionResult);
189
        }
190
191 3
        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
     * @return mixed
200
     */
201 5
    protected function getUrlAndDecodeResponse($url, PageIndexerResponse $response)
202
    {
203 5
        $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
208 5
        if ($rawResponse === false || $decodedResponse === false) {
209 1
            $this->logger->log(
210 1
                SolrLogManager::ERROR,
211 1
                'Failed to execute Page Indexer Request. Request ID: ' . $this->requestId,
212
                [
213 1
                    'request ID' => $this->requestId,
214 1
                    'request url' => $url,
215 1
                    'request headers' => $headers,
216 1
                    'response headers' => $rawResponse->getHeaders(),
217 1
                    'raw response body' => $rawResponse->getBody()->getContents()
218
                ]
219
            );
220
221 1
            throw new \RuntimeException('Failed to execute Page Indexer Request. See log for details. Request ID: ' . $this->requestId, 1319116885);
222
        }
223 4
        return $decodedResponse;
224
    }
225
226
    /**
227
     * Generates the headers to be send with the request.
228
     *
229
     * @return string[] Array of HTTP headers.
230
     */
231 6
    public function getHeaders()
232
    {
233 6
        $headers = $this->header;
234 6
        $headers[] = 'User-Agent: ' . $this->getUserAgent();
235 6
        $itemId = $this->indexQueueItem->getIndexQueueUid();
236 6
        $pageId = $this->indexQueueItem->getRecordPageId();
237
238
        $indexerRequestData = [
239 6
            'requestId' => $this->requestId,
240 6
            'item' => $itemId,
241 6
            'page' => $pageId,
242 6
            'actions' => implode(',', $this->actions),
243
            'hash' => md5(
244 6
                $itemId . '|' .
245 6
                $pageId . '|' .
246 6
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
247
            )
248
        ];
249
250 6
        $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 6
        return $headers;
254
    }
255
256
    /**
257
     * @return string
258
     */
259 6
    protected function getUserAgent()
260
    {
261 6
        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
     * @return bool TRUE if it's a legitimate request, FALSE otherwise.
279
     */
280 2
    public function isAuthenticated()
281
    {
282 2
        $authenticated = false;
283
284 2
        if (is_null($this->parameters)) {
0 ignored issues
show
introduced by
The condition is_null($this->parameters) is always false.
Loading history...
285
            return $authenticated;
286
        }
287
288 2
        $calculatedHash = md5(
289 2
            $this->parameters['item'] . '|' .
290 2
            $this->parameters['page'] . '|' .
291 2
            $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
292
        );
293
294 2
        if ($this->parameters['hash'] === $calculatedHash) {
295 1
            $authenticated = true;
296
        }
297
298 2
        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
     * @return string Unique request ID.
325
     */
326 3
    public function getRequestId()
327
    {
328 3
        return $this->requestId;
329
    }
330
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 11
    public function getParameter($parameterName)
338
    {
339 11
        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
     * @param string $value Parameter value.
347
     */
348 11
    public function setParameter($parameter, $value)
349
    {
350 11
        if (is_bool($value)) {
0 ignored issues
show
introduced by
The condition is_bool($value) is always false.
Loading history...
351
            $value = $value ? '1' : '0';
352
        }
353
354 11
        $this->parameters[$parameter] = $value;
355 11
    }
356
357
    /**
358
     * 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 1
    public function setAuthorizationCredentials($username, $password)
364
    {
365 1
        $this->username = $username;
366 1
        $this->password = $password;
367 1
    }
368
369
    /**
370
     * Sets the Index Queue item this request is related to.
371
     *
372
     * @param Item $item Related Index Queue item.
373
     */
374 6
    public function setIndexQueueItem(Item $item)
375
    {
376 6
        $this->indexQueueItem = $item;
377 6
    }
378
379
    /**
380
     * Returns the request timeout in seconds
381
     *
382
     * @return float
383
     */
384 1
    public function getTimeout()
385
    {
386 1
        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
     * @throws \Exception
407
     */
408 1
    protected function getUrl($url, $headers, $timeout): ResponseInterface
409
    {
410
        try {
411 1
            $options = $this->buildGuzzleOptions($headers, $timeout);
412 1
            $response = $this->requestFactory->request($url, 'GET', $options);
413
        } catch (ClientException $e) {
414
            $response = $e->getResponse();
415
        } catch (ServerException $e) {
416
            $response = $e->getResponse();
417
        }
418
419 1
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
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 1
    protected function buildGuzzleOptions($headers, $timeout)
430
    {
431 1
        $finalHeaders = [];
432
433 1
        foreach ($headers as $header) {
434 1
            list($name, $value) = explode(':', $header, 2);
435 1
            $finalHeaders[$name] = trim($value);
436
        }
437
438 1
        $options = ['headers' => $finalHeaders, 'timeout' => $timeout];
439 1
        if (!empty($this->username) && !empty($this->password)) {
440 1
            $options['auth'] = [$this->username, $this->password];
441
        }
442
443 1
        if ($this->extensionConfiguration->getIsSelfSignedCertificatesEnabled()) {
444
            $options['verify'] = false;
445
        }
446
447 1
        return $options;
448
    }
449
}
450