TYPO3 CMS  TYPO3_8-7
Response.php
Go to the documentation of this file.
1 <?php
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 
20 
27 {
33  protected $headers = [];
34 
40  protected $additionalHeaderData = [];
41 
47  protected $statusCode;
48 
54  protected $statusMessage = 'OK';
55 
61  protected $request;
62 
68  protected $statusMessages = [
69  // INFORMATIONAL CODES
70  100 => 'Continue',
71  101 => 'Switching Protocols',
72  102 => 'Processing',
73  103 => 'Early Hints',
74  // SUCCESS CODES
75  200 => 'OK',
76  201 => 'Created',
77  202 => 'Accepted',
78  203 => 'Non-Authoritative Information',
79  204 => 'No Content',
80  205 => 'Reset Content',
81  206 => 'Partial Content',
82  207 => 'Multi-status',
83  208 => 'Already Reported',
84  226 => 'IM Used',
85  // REDIRECTION CODES
86  300 => 'Multiple Choices',
87  301 => 'Moved Permanently',
88  302 => 'Found',
89  303 => 'See Other',
90  304 => 'Not Modified',
91  305 => 'Use Proxy',
92  306 => 'Switch Proxy', // Deprecated
93  307 => 'Temporary Redirect',
94  308 => 'Permanent Redirect',
95  // CLIENT ERROR
96  400 => 'Bad Request',
97  401 => 'Unauthorized',
98  402 => 'Payment Required',
99  403 => 'Forbidden',
100  404 => 'Not Found',
101  405 => 'Method Not Allowed',
102  406 => 'Not Acceptable',
103  407 => 'Proxy Authentication Required',
104  408 => 'Request Timeout',
105  409 => 'Conflict',
106  410 => 'Gone',
107  411 => 'Length Required',
108  412 => 'Precondition Failed',
109  413 => 'Request Entity Too Large',
110  414 => 'URI Too Long',
111  415 => 'Unsupported Media Type',
112  416 => 'Requested range not satisfiable',
113  417 => 'Expectation Failed',
114  418 => 'I\'m a teapot',
115  422 => 'Unprocessable Entity',
116  423 => 'Locked',
117  424 => 'Failed Dependency',
118  425 => 'Unordered Collection',
119  426 => 'Upgrade Required',
120  428 => 'Precondition Required',
121  429 => 'Too Many Requests',
122  431 => 'Request Header Fields Too Large',
123  451 => 'Unavailable For Legal Reasons',
124  // SERVER ERROR
125  500 => 'Internal Server Error',
126  501 => 'Not Implemented',
127  502 => 'Bad Gateway',
128  503 => 'Service Unavailable',
129  504 => 'Gateway Time-out',
130  505 => 'HTTP Version not supported',
131  506 => 'Variant Also Negotiates',
132  507 => 'Insufficient Storage',
133  508 => 'Loop Detected',
134  509 => 'Bandwidth Limit Exceeded',
135  511 => 'Network Authentication Required',
136  ];
137 
142 
146  public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
147  {
148  $this->environmentService = $environmentService;
149  }
150 
159  public function setStatus($code, $message = null)
160  {
161  if (!is_int($code)) {
162  throw new \InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
163  }
164  if ($message === null && !isset($this->statusMessages[$code])) {
165  throw new \InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
166  }
167  $this->statusCode = $code;
168  $this->statusMessage = $message === null ? $this->statusMessages[$code] : $message;
169  }
170 
177  public function getStatus()
178  {
179  return $this->statusCode . ' ' . $this->statusMessage;
180  }
181 
191  public function setHeader($name, $value, $replaceExistingHeader = true)
192  {
193  if (strtoupper(substr($name, 0, 4)) === 'HTTP') {
194  throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
195  }
196  if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
197  $this->headers[$name] = [$value];
198  } else {
199  $this->headers[$name][] = $value;
200  }
201  }
202 
209  public function getHeaders()
210  {
211  $preparedHeaders = [];
212  if ($this->statusCode !== null) {
213  $protocolVersion = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
214  $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
215  $preparedHeaders[] = $statusHeader;
216  }
217  foreach ($this->headers as $name => $values) {
218  foreach ($values as $value) {
219  $preparedHeaders[] = $name . ': ' . $value;
220  }
221  }
222  return $preparedHeaders;
223  }
224 
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 
247  public function send()
248  {
249  $this->sendHeaders();
250  if ($this->content !== null) {
251  echo $this->getContent();
252  }
253  }
254 
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()) {
272  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
273  $pageRenderer->addHeaderData($additionalHeaderData);
274  } else {
275  $this->additionalHeaderData[] = $additionalHeaderData;
276  }
277  }
278 
285  public function getAdditionalHeaderData()
286  {
288  }
289 
293  public function setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
294  {
295  $this->request = $request;
296  }
297 
301  public function getRequest()
302  {
303  return $this->request;
304  }
305 
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 
323  protected function getTypoScriptFrontendController()
324  {
325  return $GLOBALS['TSFE'];
326  }
327 }
setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
Definition: Response.php:293
static makeInstance($className,... $constructorArguments)
setStatus($code, $message=null)
Definition: Response.php:159
injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
Definition: Response.php:146
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
setHeader($name, $value, $replaceExistingHeader=true)
Definition: Response.php:191