ea744ce6b3935d5a91caff84f89053558fcd99fa
[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 * Sets the specified HTTP header
162 *
163 * @param string $name Name of the header, for example "Location", "Content-Description" etc.
164 * @param mixed $value The value of the given header
165 * @param bool $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
166 * @throws \InvalidArgumentException
167 * @api
168 */
169 public function setHeader($name, $value, $replaceExistingHeader = true)
170 {
171 if (strtoupper(substr($name, 0, 4)) === 'HTTP') {
172 throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
173 }
174 if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
175 $this->headers[$name] = [$value];
176 } else {
177 $this->headers[$name][] = $value;
178 }
179 }
180
181 /**
182 * Returns the HTTP headers - including the status header - of this web response
183 *
184 * @return string[] The HTTP headers
185 * @api
186 */
187 public function getHeaders()
188 {
189 $preparedHeaders = [];
190 if ($this->statusCode !== null) {
191 $protocolVersion = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
192 $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
193 $preparedHeaders[] = $statusHeader;
194 }
195 foreach ($this->headers as $name => $values) {
196 foreach ($values as $value) {
197 $preparedHeaders[] = $name . ': ' . $value;
198 }
199 }
200 return $preparedHeaders;
201 }
202
203 /**
204 * Sends the HTTP headers.
205 *
206 * If headers have already been sent, this method fails silently.
207 *
208 * @api
209 */
210 public function sendHeaders()
211 {
212 if (headers_sent() === true) {
213 return;
214 }
215 foreach ($this->getHeaders() as $header) {
216 header($header);
217 }
218 }
219
220 /**
221 * Renders and sends the whole web response
222 *
223 * @api
224 */
225 public function send()
226 {
227 $this->sendHeaders();
228 if ($this->content !== null) {
229 echo $this->getContent();
230 }
231 }
232
233 /**
234 * Adds an additional header data (something like
235 * '<script src="myext/Resources/JavaScript/my.js" type="text/javascript"></script>'
236 * )
237 *
238 * @TODO The workround and the $request member should be removed again, once the PageRender does support non-cached USER_INTs
239 * @param string $additionalHeaderData The value additional header
240 * @throws \InvalidArgumentException
241 * @api
242 */
243 public function addAdditionalHeaderData($additionalHeaderData)
244 {
245 if (!is_string($additionalHeaderData)) {
246 throw new \InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
247 }
248 if ($this->request->isCached()) {
249 /** @var PageRenderer $pageRenderer */
250 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
251 $pageRenderer->addHeaderData($additionalHeaderData);
252 } else {
253 $this->additionalHeaderData[] = $additionalHeaderData;
254 }
255 }
256
257 /**
258 * Returns the additional header data
259 *
260 * @return array The additional header data
261 * @api
262 */
263 public function getAdditionalHeaderData()
264 {
265 return $this->additionalHeaderData;
266 }
267
268 /**
269 * @param \TYPO3\CMS\Extbase\Mvc\Web\Request $request
270 */
271 public function setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
272 {
273 $this->request = $request;
274 }
275
276 /**
277 * @return \TYPO3\CMS\Extbase\Mvc\Web\Request
278 */
279 public function getRequest()
280 {
281 return $this->request;
282 }
283
284 /**
285 * Sends additional headers and returns the content
286 *
287 * @return null|string
288 */
289 public function shutdown()
290 {
291 if (!empty($this->getAdditionalHeaderData())) {
292 $this->getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $this->getAdditionalHeaderData());
293 }
294 $this->sendHeaders();
295 return parent::shutdown();
296 }
297
298 /**
299 * @return TypoScriptFrontendController
300 */
301 protected function getTypoScriptFrontendController()
302 {
303 return $GLOBALS['TSFE'];
304 }
305 }