172ed4985cbd5de2c1f5c3c3e826ad6ca5edff67
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Web / TX_EXTMVC_Web_Response.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 /**
25 * A web specific response implementation
26 *
27 * @version $Id:$
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 * @scope prototype
30 */
31 class TX_EXTMVC_Web_Response extends TX_EXTMVC_Response {
32
33 /**
34 * The HTTP headers which will be sent in the response
35 *
36 * @var array
37 */
38 protected $headers = array();
39
40 /**
41 * The HTTP status code
42 *
43 * @var integer
44 */
45 protected $statusCode = 200;
46
47 /**
48 * The HTTP status message
49 *
50 * @var string
51 */
52 protected $statusMessage = 'OK';
53
54 /**
55 * The standardized and other important HTTP Status messages
56 *
57 * @var array
58 */
59 protected $statusMessages = array(
60 100 => 'Continue',
61 101 => 'Switching Protocols',
62 102 => 'Processing', # RFC 2518
63 200 => 'OK',
64 201 => 'Created',
65 202 => 'Accepted',
66 203 => 'Non-Authoritative Information',
67 204 => 'No Content',
68 205 => 'Reset Content',
69 206 => 'Partial Content',
70 207 => 'Multi-Status',
71 300 => 'Multiple Choices',
72 301 => 'Moved Permanently',
73 302 => 'Found',
74 303 => 'See Other',
75 304 => 'Not Modified',
76 305 => 'Use Proxy',
77 307 => 'Temporary Redirect',
78 400 => 'Bad Request',
79 401 => 'Unauthorized',
80 402 => 'Payment Required',
81 403 => 'Forbidden',
82 404 => 'Not Found',
83 405 => 'Method Not Allowed',
84 406 => 'Not Acceptable',
85 407 => 'Proxy Authentication Required',
86 408 => 'Request Timeout',
87 409 => 'Conflict',
88 410 => 'Gone',
89 411 => 'Length Required',
90 412 => 'Precondition Failed',
91 413 => 'Request Entity Too Large',
92 414 => 'Request-URI Too Long',
93 415 => 'Unsupported Media Type',
94 416 => 'Requested Range Not Satisfiable',
95 417 => 'Expectation Failed',
96 500 => 'Internal Server Error',
97 501 => 'Not Implemented',
98 502 => 'Bad Gateway',
99 503 => 'Service Unavailable',
100 504 => 'Gateway Timeout',
101 505 => 'HTTP Version Not Supported',
102 507 => 'Insufficient Storage',
103 509 => 'Bandwidth Limit Exceeded',
104 );
105
106 /**
107 * Sets the HTTP status code and (optionally) a customized message.
108 *
109 * @param integer $code The status code
110 * @param string $message If specified, this message is sent instead of the standard message
111 * @return void
112 * @throws InvalidArgumentException if the specified status code is not valid
113 * @author Robert Lemke <robert@typo3.org>
114 */
115 public function setStatus($code, $message = NULL) {
116 if (!is_int($code)) throw new InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
117 if ($message === NULL && !isset($this->statusMessages[$code])) throw new InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
118
119 $this->statusCode = $code;
120 $this->statusMessage = ($message === NULL) ? $this->statusMessages[$code] : $message;
121 }
122
123 /**
124 * Returns status code and status message.
125 *
126 * @return string The status code and status message, eg. "404 Not Found"
127 * @author Robert Lemke <robert@typo3.org>
128 */
129 public function getStatus() {
130 return $this->statusCode . ' ' . $this->statusMessage;
131 }
132
133 /**
134 * Sets the specified HTTP header
135 *
136 * @param string $name Name of the header, for example "Location", "Content-Description" etc.
137 * @param mixed $value The value of the given header
138 * @param boolean $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
139 * @return void
140 * @author Robert Lemke <robert@typo3.org>
141 */
142 public function setHeader($name, $value, $replaceExistingHeader = TRUE) {
143 if (strtoupper(substr($name, 0, 4)) == 'HTTP') throw new InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
144 if ($replaceExistingHeader === TRUE || !isset($this->headers[$name])) {
145 $this->headers[$name] = array($value);
146 } else {
147 $this->headers[$name][] = $value;
148 }
149 }
150
151 /**
152 * Returns the HTTP headers - including the status header - of this web response
153 *
154 * @return string The HTTP headers
155 * @author Robert Lemke <robert@typo3.org>
156 */
157 public function getHeaders() {
158 $preparedHeaders = array();
159 $statusHeader = 'HTTP/1.1 ' . $this->statusCode . ' ' . $this->statusMessage;
160
161 $preparedHeaders[] = $statusHeader;
162 foreach ($this->headers as $name => $values) {
163 foreach ($values as $value) {
164 $preparedHeaders[] = $name . ': ' . $value;
165 }
166 }
167 return $preparedHeaders;
168 }
169
170 }
171 ?>