55775bf5b0973f872a605a7295300825c0fc28ba
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Web / Response.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Web;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Page\PageRenderer;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
20
21 /**
22 * A web specific response implementation
23 *
24 * @api
25 */
26 class Response extends \TYPO3\CMS\Extbase\Mvc\Response
27 {
28 /**
29 * The HTTP headers which will be sent in the response
30 *
31 * @var array
32 */
33 protected $headers = [];
34
35 /**
36 * Additional header tags
37 *
38 * @var array
39 */
40 protected $additionalHeaderData = [];
41
42 /**
43 * The HTTP status code
44 *
45 * @var int
46 */
47 protected $statusCode;
48
49 /**
50 * The HTTP status message
51 *
52 * @var string
53 */
54 protected $statusMessage = 'OK';
55
56 /**
57 * The Request which generated the Response
58 *
59 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
60 */
61 protected $request;
62
63 /**
64 * The standardized and other important HTTP Status messages
65 *
66 * @var array
67 */
68 protected $statusMessages = [
69 100 => 'Continue',
70 101 => 'Switching Protocols',
71 102 => 'Processing',
72 // RFC 2518
73 200 => 'OK',
74 201 => 'Created',
75 202 => 'Accepted',
76 203 => 'Non-Authoritative Information',
77 204 => 'No Content',
78 205 => 'Reset Content',
79 206 => 'Partial Content',
80 207 => 'Multi-Status',
81 300 => 'Multiple Choices',
82 301 => 'Moved Permanently',
83 302 => 'Found',
84 303 => 'See Other',
85 304 => 'Not Modified',
86 305 => 'Use Proxy',
87 307 => 'Temporary Redirect',
88 400 => 'Bad Request',
89 401 => 'Unauthorized',
90 402 => 'Payment Required',
91 403 => 'Forbidden',
92 404 => 'Not Found',
93 405 => 'Method Not Allowed',
94 406 => 'Not Acceptable',
95 407 => 'Proxy Authentication Required',
96 408 => 'Request Timeout',
97 409 => 'Conflict',
98 410 => 'Gone',
99 411 => 'Length Required',
100 412 => 'Precondition Failed',
101 413 => 'Request Entity Too Large',
102 414 => 'Request-URI Too Long',
103 415 => 'Unsupported Media Type',
104 416 => 'Requested Range Not Satisfiable',
105 417 => 'Expectation Failed',
106 500 => 'Internal Server Error',
107 501 => 'Not Implemented',
108 502 => 'Bad Gateway',
109 503 => 'Service Unavailable',
110 504 => 'Gateway Timeout',
111 505 => 'HTTP Version Not Supported',
112 507 => 'Insufficient Storage',
113 509 => 'Bandwidth Limit Exceeded'
114 ];
115
116 /**
117 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
118 */
119 protected $environmentService;
120
121 /**
122 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
123 */
124 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
125 {
126 $this->environmentService = $environmentService;
127 }
128
129 /**
130 * Sets the HTTP status code and (optionally) a customized message.
131 *
132 * @param int $code The status code
133 * @param string $message If specified, this message is sent instead of the standard message
134 * @throws \InvalidArgumentException if the specified status code is not valid
135 * @api
136 */
137 public function setStatus($code, $message = null)
138 {
139 if (!is_int($code)) {
140 throw new \InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
141 }
142 if ($message === null && !isset($this->statusMessages[$code])) {
143 throw new \InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
144 }
145 $this->statusCode = $code;
146 $this->statusMessage = $message === null ? $this->statusMessages[$code] : $message;
147 }
148
149 /**
150 * Returns status code and status message.
151 *
152 * @return string The status code and status message, eg. "404 Not Found
153 * @api
154 */
155 public function getStatus()
156 {
157 return $this->statusCode . ' ' . $this->statusMessage;
158 }
159
160 /**
161 * Returns the status code, if not set, uses the OK status code 200
162 *
163 * @return int
164 * @internal only use for backend module handling
165 */
166 public function getStatusCode()
167 {
168 return $this->statusCode ?: 200;
169 }
170
171 /**
172 * Sets the specified HTTP header
173 *
174 * @param string $name Name of the header, for example "Location", "Content-Description" etc.
175 * @param mixed $value The value of the given header
176 * @param bool $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
177 * @throws \InvalidArgumentException
178 * @api
179 */
180 public function setHeader($name, $value, $replaceExistingHeader = true)
181 {
182 if (strtoupper(substr($name, 0, 4)) === 'HTTP') {
183 throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
184 }
185 if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
186 $this->headers[$name] = [$value];
187 } else {
188 $this->headers[$name][] = $value;
189 }
190 }
191
192 /**
193 * Returns the HTTP headers - including the status header - of this web response
194 *
195 * @return string[] The HTTP headers
196 * @api
197 */
198 public function getHeaders()
199 {
200 $preparedHeaders = [];
201 if ($this->statusCode !== null) {
202 $protocolVersion = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
203 $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
204 $preparedHeaders[] = $statusHeader;
205 }
206 foreach ($this->headers as $name => $values) {
207 foreach ($values as $value) {
208 $preparedHeaders[] = $name . ': ' . $value;
209 }
210 }
211 return $preparedHeaders;
212 }
213
214 /**
215 * Returns the HTTP headers grouped by name without the status header
216 *
217 * @return array all headers set for this request
218 * @internal only used within TYPO3 Core to convert to PSR-7 response headers
219 */
220 public function getUnpreparedHeaders(): array
221 {
222 return $this->headers;
223 }
224
225 /**
226 * Sends the HTTP headers.
227 *
228 * If headers have already been sent, this method fails silently.
229 *
230 * @api
231 */
232 public function sendHeaders()
233 {
234 if (headers_sent() === true) {
235 return;
236 }
237 foreach ($this->getHeaders() as $header) {
238 header($header);
239 }
240 }
241
242 /**
243 * Renders and sends the whole web response
244 *
245 * @api
246 */
247 public function send()
248 {
249 $this->sendHeaders();
250 if ($this->content !== null) {
251 echo $this->getContent();
252 }
253 }
254
255 /**
256 * Adds an additional header data (something like
257 * '<script src="myext/Resources/JavaScript/my.js" type="text/javascript"></script>'
258 * )
259 *
260 * @TODO The workround and the $request member should be removed again, once the PageRender does support non-cached USER_INTs
261 * @param string $additionalHeaderData The value additional header
262 * @throws \InvalidArgumentException
263 * @api
264 */
265 public function addAdditionalHeaderData($additionalHeaderData)
266 {
267 if (!is_string($additionalHeaderData)) {
268 throw new \InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
269 }
270 if ($this->request->isCached()) {
271 /** @var PageRenderer $pageRenderer */
272 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
273 $pageRenderer->addHeaderData($additionalHeaderData);
274 } else {
275 $this->additionalHeaderData[] = $additionalHeaderData;
276 }
277 }
278
279 /**
280 * Returns the additional header data
281 *
282 * @return array The additional header data
283 * @api
284 */
285 public function getAdditionalHeaderData()
286 {
287 return $this->additionalHeaderData;
288 }
289
290 /**
291 * @param \TYPO3\CMS\Extbase\Mvc\Web\Request $request
292 */
293 public function setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
294 {
295 $this->request = $request;
296 }
297
298 /**
299 * @return \TYPO3\CMS\Extbase\Mvc\Web\Request
300 */
301 public function getRequest()
302 {
303 return $this->request;
304 }
305
306 /**
307 * Sends additional headers and returns the content
308 *
309 * @return string|null
310 */
311 public function shutdown()
312 {
313 if (!empty($this->getAdditionalHeaderData())) {
314 $this->getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $this->getAdditionalHeaderData());
315 }
316 $this->sendHeaders();
317 return parent::shutdown();
318 }
319
320 /**
321 * @return TypoScriptFrontendController
322 */
323 protected function getTypoScriptFrontendController()
324 {
325 return $GLOBALS['TSFE'];
326 }
327 }