Raised DBAL version from 1.1.5 to 1.1.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Web / Response.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A web specific response implementation
30 *
31 * @package Extbase
32 * @subpackage MVC\Web
33 * @version $ID:$
34 * @scope prototype
35 * @api
36 */
37 class Tx_Extbase_MVC_Web_Response extends Tx_Extbase_MVC_Response {
38
39 /**
40 * The HTTP headers which will be sent in the response
41 *
42 * @var array
43 */
44 protected $headers = array();
45
46 /**
47 * Additional header tags
48 *
49 * @var array
50 */
51 protected $additionalHeaderData = array();
52
53 /**
54 * The HTTP status code
55 *
56 * @var integer
57 */
58 protected $statusCode = 200;
59
60 /**
61 * The HTTP status message
62 *
63 * @var string
64 */
65 protected $statusMessage = 'OK';
66
67 /**
68 * The standardized and other important HTTP Status messages
69 *
70 * @var array
71 */
72 protected $statusMessages = array(
73 100 => 'Continue',
74 101 => 'Switching Protocols',
75 102 => 'Processing', # RFC 2518
76 200 => 'OK',
77 201 => 'Created',
78 202 => 'Accepted',
79 203 => 'Non-Authoritative Information',
80 204 => 'No Content',
81 205 => 'Reset Content',
82 206 => 'Partial Content',
83 207 => 'Multi-Status',
84 300 => 'Multiple Choices',
85 301 => 'Moved Permanently',
86 302 => 'Found',
87 303 => 'See Other',
88 304 => 'Not Modified',
89 305 => 'Use Proxy',
90 307 => 'Temporary Redirect',
91 400 => 'Bad Request',
92 401 => 'Unauthorized',
93 402 => 'Payment Required',
94 403 => 'Forbidden',
95 404 => 'Not Found',
96 405 => 'Method Not Allowed',
97 406 => 'Not Acceptable',
98 407 => 'Proxy Authentication Required',
99 408 => 'Request Timeout',
100 409 => 'Conflict',
101 410 => 'Gone',
102 411 => 'Length Required',
103 412 => 'Precondition Failed',
104 413 => 'Request Entity Too Large',
105 414 => 'Request-URI Too Long',
106 415 => 'Unsupported Media Type',
107 416 => 'Requested Range Not Satisfiable',
108 417 => 'Expectation Failed',
109 500 => 'Internal Server Error',
110 501 => 'Not Implemented',
111 502 => 'Bad Gateway',
112 503 => 'Service Unavailable',
113 504 => 'Gateway Timeout',
114 505 => 'HTTP Version Not Supported',
115 507 => 'Insufficient Storage',
116 509 => 'Bandwidth Limit Exceeded',
117 );
118
119 /**
120 * Sets the HTTP status code and (optionally) a customized message.
121 *
122 * @param integer $code The status code
123 * @param string $message If specified, this message is sent instead of the standard message
124 * @return void
125 * @throws InvalidArgumentException if the specified status code is not valid
126 * @api
127 */
128 public function setStatus($code, $message = NULL) {
129 if (!is_int($code)) throw new InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
130 if ($message === NULL && !isset($this->statusMessages[$code])) throw new InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
131
132 $this->statusCode = $code;
133 $this->statusMessage = ($message === NULL) ? $this->statusMessages[$code] : $message;
134 }
135
136 /**
137 * Returns status code and status message.
138 *
139 * @return string The status code and status message, eg. "404 Not Found"
140 * @api
141 */
142 public function getStatus() {
143 return $this->statusCode . ' ' . $this->statusMessage;
144 }
145
146 /**
147 * Sets the specified HTTP header
148 *
149 * @param string $name Name of the header, for example "Location", "Content-Description" etc.
150 * @param mixed $value The value of the given header
151 * @param boolean $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
152 * @return void
153 * @api
154 */
155 public function setHeader($name, $value, $replaceExistingHeader = TRUE) {
156 if (strtoupper(substr($name, 0, 4)) === 'HTTP') throw new InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
157 if ($replaceExistingHeader === TRUE || !isset($this->headers[$name])) {
158 $this->headers[$name] = array($value);
159 } else {
160 $this->headers[$name][] = $value;
161 }
162 }
163
164 /**
165 * Returns the HTTP headers - including the status header - of this web response
166 *
167 * @return string The HTTP headers
168 * @api
169 */
170 public function getHeaders() {
171 $preparedHeaders = array();
172 $protocolVersion = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
173 $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
174
175 $preparedHeaders[] = $statusHeader;
176 foreach ($this->headers as $name => $values) {
177 foreach ($values as $value) {
178 $preparedHeaders[] = $name . ': ' . $value;
179 }
180 }
181 return $preparedHeaders;
182 }
183
184 /**
185 * Sends the HTTP headers.
186 *
187 * If headers have already been sent, this method fails silently.
188 *
189 * @return void
190 * @api
191 */
192 public function sendHeaders() {
193 if (headers_sent() === TRUE) return;
194 foreach ($this->getHeaders() as $header) {
195 header($header);
196 }
197 }
198
199 /**
200 * Renders and sends the whole web response
201 *
202 * @return void
203 * @api
204 */
205 public function send() {
206 $this->sendHeaders();
207 if ($this->content !== NULL) {
208 echo $this->getContent();
209 }
210 }
211
212 /**
213 * Adds an additional header data (something like
214 * '<script src="myext/Resources/JavaScript/my.js" type="text/javascript"></script>'
215 * )
216 *
217 * @param string $additionalHeaderData The value additonal header
218 * @return void
219 * @api
220 */
221 public function addAdditionalHeaderData($additionalHeaderData) {
222 if (!is_string($additionalHeaderData)) throw new InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
223 $this->additionalHeaderData[] = $additionalHeaderData;
224 }
225
226 /**
227 * Returns the additional header data
228 *
229 * @return array The additional header data
230 * @api
231 */
232 public function getAdditionalHeaderData() {
233 return $this->additionalHeaderData;
234 }
235
236 }
237 ?>