TYPO3 CMS  TYPO3_7-6
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  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 
120 
124  public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
125  {
126  $this->environmentService = $environmentService;
127  }
128 
138  public function setStatus($code, $message = null)
139  {
140  if (!is_int($code)) {
141  throw new \InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
142  }
143  if ($message === null && !isset($this->statusMessages[$code])) {
144  throw new \InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
145  }
146  $this->statusCode = $code;
147  $this->statusMessage = $message === null ? $this->statusMessages[$code] : $message;
148  }
149 
156  public function getStatus()
157  {
158  return $this->statusCode . ' ' . $this->statusMessage;
159  }
160 
171  public function setHeader($name, $value, $replaceExistingHeader = true)
172  {
173  if (strtoupper(substr($name, 0, 4)) === 'HTTP') {
174  throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
175  }
176  if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
177  $this->headers[$name] = [$value];
178  } else {
179  $this->headers[$name][] = $value;
180  }
181  }
182 
189  public function getHeaders()
190  {
191  $preparedHeaders = [];
192  if ($this->statusCode !== null) {
193  $protocolVersion = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
194  $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
195  $preparedHeaders[] = $statusHeader;
196  }
197  foreach ($this->headers as $name => $values) {
198  foreach ($values as $value) {
199  $preparedHeaders[] = $name . ': ' . $value;
200  }
201  }
202  return $preparedHeaders;
203  }
204 
213  public function sendHeaders()
214  {
215  if (headers_sent() === true) {
216  return;
217  }
218  foreach ($this->getHeaders() as $header) {
219  header($header);
220  }
221  }
222 
229  public function send()
230  {
231  $this->sendHeaders();
232  if ($this->content !== null) {
233  echo $this->getContent();
234  }
235  }
236 
248  public function addAdditionalHeaderData($additionalHeaderData)
249  {
250  if (!is_string($additionalHeaderData)) {
251  throw new \InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
252  }
253  if ($this->request->isCached()) {
255  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
256  $pageRenderer->addHeaderData($additionalHeaderData);
257  } else {
258  $this->additionalHeaderData[] = $additionalHeaderData;
259  }
260  }
261 
268  public function getAdditionalHeaderData()
269  {
271  }
272 
276  public function setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
277  {
278  $this->request = $request;
279  }
280 
284  public function getRequest()
285  {
286  return $this->request;
287  }
288 
294  public function shutdown()
295  {
296  if (!empty($this->getAdditionalHeaderData())) {
297  $this->getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $this->getAdditionalHeaderData());
298  }
299  $this->sendHeaders();
300  return parent::shutdown();
301  }
302 
306  protected function getTypoScriptFrontendController()
307  {
308  return $GLOBALS['TSFE'];
309  }
310 }
setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
Definition: Response.php:276
setStatus($code, $message=null)
Definition: Response.php:138
injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
Definition: Response.php:124
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:171