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