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