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\IndexQueue\FrontendHelper\Dispatcher; |
||
28 | use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
||
29 | use TYPO3\CMS\Core\SingletonInterface; |
||
30 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
31 | |||
32 | /** |
||
33 | * Checks for Index Queue page indexer requests and handles the actions |
||
34 | * requested by them. |
||
35 | * |
||
36 | * @author Ingo Renner <[email protected]> |
||
37 | */ |
||
38 | class PageIndexerRequestHandler implements SingletonInterface |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * Index Queue page indexer request. |
||
43 | * |
||
44 | * @var PageIndexerRequest |
||
45 | */ |
||
46 | protected $request; |
||
47 | |||
48 | /** |
||
49 | * Index Queue page indexer response. |
||
50 | * |
||
51 | * @var PageIndexerResponse |
||
52 | */ |
||
53 | protected $response; |
||
54 | |||
55 | /** |
||
56 | * Index Queue page indexer frontend helper dispatcher. |
||
57 | * |
||
58 | * @var Dispatcher |
||
59 | */ |
||
60 | protected $dispatcher; |
||
61 | |||
62 | /** |
||
63 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
64 | */ |
||
65 | protected $logger = null; |
||
66 | |||
67 | /** |
||
68 | * Constructor. |
||
69 | * |
||
70 | * Initializes request, response, and dispatcher. |
||
71 | */ |
||
72 | 2 | public function __construct() |
|
73 | { |
||
74 | 2 | $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
75 | 2 | $this->dispatcher = GeneralUtility::makeInstance(Dispatcher::class); |
|
76 | 2 | $this->request = GeneralUtility::makeInstance(PageIndexerRequest::class, $_SERVER['HTTP_X_TX_SOLR_IQ']); |
|
77 | 2 | $this->response = GeneralUtility::makeInstance(PageIndexerResponse::class); |
|
78 | 2 | $this->response->setRequestId($this->request->getRequestId()); |
|
79 | 2 | } |
|
80 | |||
81 | /** |
||
82 | * Authenticates the request, runs the frontend helpers defined by the |
||
83 | * request, and registers its own shutdown() method for execution at |
||
84 | * hook_eofe in tslib/class.tslib_fe.php. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function run() |
||
89 | { |
||
90 | if (!$this->request->isAuthenticated()) { |
||
91 | $this->logger->log( |
||
92 | SolrLogManager::ERROR, |
||
93 | 'Invalid Index Queue Frontend Request detected!', |
||
94 | [ |
||
95 | 'page indexer request' => (array)$this->request, |
||
96 | 'index queue header' => $_SERVER['HTTP_X_TX_SOLR_IQ'] |
||
97 | ] |
||
98 | ); |
||
99 | http_response_code(403); |
||
100 | die('Invalid Index Queue Request!'); |
||
0 ignored issues
–
show
|
|||
101 | } |
||
102 | |||
103 | $this->dispatcher->dispatch($this->request, $this->response); |
||
104 | |||
105 | // register shutdown method here instead of in ext_localconf.php to |
||
106 | // allow frontend helpers to execute at hook_eofe in |
||
107 | // tslib/class.tslib_fe.php before shutting down |
||
108 | $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_eofe'][__CLASS__] = self::class . '->shutdown'; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Completes the Index Queue page indexer request and returns the response |
||
113 | * with the collected results. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function shutdown() |
||
118 | { |
||
119 | $this->dispatcher->shutdown(); |
||
120 | |||
121 | // make sure that no other output messes up the data |
||
122 | ob_end_clean(); |
||
123 | |||
124 | $this->response->sendHeaders(); |
||
125 | echo $this->response->getContent(); |
||
126 | |||
127 | // exit since we don't want anymore output |
||
128 | exit; |
||
0 ignored issues
–
show
|
|||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Gets the Index Queue page indexer request. |
||
133 | * |
||
134 | * @return PageIndexerRequest |
||
135 | */ |
||
136 | 2 | public function getRequest() |
|
137 | { |
||
138 | 2 | return $this->request; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Gets the Index Queue page indexer response. |
||
143 | * |
||
144 | * @return PageIndexerResponse |
||
145 | */ |
||
146 | public function getResponse() |
||
147 | { |
||
148 | return $this->response; |
||
149 | } |
||
150 | } |
||
151 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.