ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Response.php
Go to the documentation of this file.
1<?php
50
54 protected $request;
55
59 protected $httpVersion = '1.1';
60
64 protected $status = 200;
65
69 protected $headers = array();
70
74 protected $body = '';
75
79 protected $length = 0;
80
84 protected static $messages = array(
85 //Informational 1xx
86 100 => '100 Continue',
87 101 => '101 Switching Protocols',
88 //Successful 2xx
89 200 => '200 OK',
90 201 => '201 Created',
91 202 => '202 Accepted',
92 203 => '203 Non-Authoritative Information',
93 204 => '204 No Content',
94 205 => '205 Reset Content',
95 206 => '206 Partial Content',
96 //Redirection 3xx
97 300 => '300 Multiple Choices',
98 301 => '301 Moved Permanently',
99 302 => '302 Found',
100 303 => '303 See Other',
101 304 => '304 Not Modified',
102 305 => '305 Use Proxy',
103 306 => '306 (Unused)',
104 307 => '307 Temporary Redirect',
105 //Client Error 4xx
106 400 => '400 Bad Request',
107 401 => '401 Unauthorized',
108 402 => '402 Payment Required',
109 403 => '403 Forbidden',
110 404 => '404 Not Found',
111 405 => '405 Method Not Allowed',
112 406 => '406 Not Acceptable',
113 407 => '407 Proxy Authentication Required',
114 408 => '408 Request Timeout',
115 409 => '409 Conflict',
116 410 => '410 Gone',
117 411 => '411 Length Required',
118 412 => '412 Precondition Failed',
119 413 => '413 Request Entity Too Large',
120 414 => '414 Request-URI Too Long',
121 415 => '415 Unsupported Media Type',
122 416 => '416 Requested Range Not Satisfiable',
123 417 => '417 Expectation Failed',
124 422 => '422 Unprocessable Entity',
125 423 => '423 Locked',
126 //Server Error 5xx
127 500 => '500 Internal Server Error',
128 501 => '501 Not Implemented',
129 502 => '502 Bad Gateway',
130 503 => '503 Service Unavailable',
131 504 => '504 Gateway Timeout',
132 505 => '505 HTTP Version Not Supported'
133 );
134
138 protected $cookieJar;
139
143 public function __construct( Slim_Http_Request $req ) {
144 $this->request = $req;
145 $this->header('Content-Type', 'text/html');
146 }
147
154 public function httpVersion( $version = null ) {
155 if ( $version ) {
156 $version = (string)$version;
157 if ( $version === '1.0' || $version === '1.1' ) {
158 $this->httpVersion = $version;
159 } else {
160 throw new InvalidArgumentException('Invalid HTTP version in Response object');
161 }
162 }
163 return $this->httpVersion;
164 }
165
172 public function status( $status = null ) {
173 if ( !is_null($status) ) {
174 if ( !in_array(intval($status), array_keys(self::$messages)) ) {
175 throw new InvalidArgumentException('Cannot set Response status. Provided status code "' . $status . '" is not a valid HTTP response code.');
176 }
177 $this->status = intval($status);
178 }
179 return $this->status;
180 }
181
186 public function headers() {
187 return $this->headers;
188 }
189
196 public function header( $key, $value = null ) {
197 if ( !is_null($value) ) {
198 $this->headers[$key] = $value;
199 }
200 return isset($this->headers[$key]) ? $this->headers[$key] : null;
201 }
202
208 public function body( $body = null ) {
209 if ( !is_null($body) ) {
210 $this->body = '';
211 $this->length = 0;
212 $this->write($body);
213 }
214 return $this->body;
215 }
216
222 public function write( $body ) {
223 $body = (string)$body;
224 $this->length += strlen($body);
225 $this->body .= $body;
226 $this->header('Content-Length', $this->length);
227 return $body;
228 }
229
236 $this->cookieJar = $cookieJar;
237 }
238
243 public function getCookieJar() {
244 return $this->cookieJar;
245 }
246
251 public function finalize() {
252 if ( in_array($this->status, array(204, 304)) ) {
253 $this->body('');
254 unset($this->headers['Content-Type']);
255 }
256 }
257
262 public static function getMessageForCode( $status ) {
263 return isset(self::$messages[$status]) ? self::$messages[$status] : null;
264 }
265
270 public function canHaveBody() {
271 return ( $this->status < 100 || $this->status >= 200 ) && $this->status != 204 && $this->status != 304;
272 }
273
278 protected function sendHeaders() {
279 //Finalize response
280 $this->finalize();
281
282 if ( substr(PHP_SAPI, 0, 3) === 'cgi') {
283 //Send Status header if running with fastcgi
284 header('Status: ' . self::getMessageForCode($this->status()));
285 } else {
286 //Else send HTTP message
287 header(sprintf('HTTP/%s %s', $this->httpVersion, self::getMessageForCode($this->status())));
288 }
289
290 //Send headers
291 foreach ( $this->headers() as $name => $value ) {
292 header("$name: $value");
293 }
294
295 //Send cookies
296 foreach ( $this->getCookieJar()->getResponseCookies() as $name => $cookie ) {
297 /* httponly option is only available for PHP version >= 5.2 */
298 if ( version_compare(PHP_VERSION, '5.2.0', '>=') ) {
299 setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpires(), $cookie->getPath(), $cookie->getDomain(), $cookie->getSecure(), $cookie->getHttpOnly());
300 } else {
301 setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpires(), $cookie->getPath(), $cookie->getDomain(), $cookie->getSecure());
302 }
303 }
304
305 //Flush all output to client
306 flush();
307 }
308
317 public function send() {
318 if ( !headers_sent() ) {
319 $this->sendHeaders();
320 }
321 if ( $this->canHaveBody() && $this->request->isHead() === false ) {
322 echo $this->body;
323 }
324 }
325
326}
Slim - a micro PHP 5 framework.
Definition: CookieJar.php:54
body( $body=null)
Set the HTTP response body.
Definition: Response.php:208
sendHeaders()
Send headers for HTTP response.
Definition: Response.php:278
header( $key, $value=null)
Get and/or set an HTTP response header.
Definition: Response.php:196
httpVersion( $version=null)
Set and/or get the HTTP response version.
Definition: Response.php:154
__construct(Slim_Http_Request $req)
Constructor.
Definition: Response.php:143
static getMessageForCode( $status)
Get message for HTTP status code.
Definition: Response.php:262
status( $status=null)
Set and/or get the HTTP response status code.
Definition: Response.php:172
headers()
Get HTTP response headers.
Definition: Response.php:186
getCookieJar()
Get cookie jar.
Definition: Response.php:243
setCookieJar(Slim_Http_CookieJar $cookieJar)
Set cookie jar.
Definition: Response.php:235
finalize()
Finalize response headers before response is sent.
Definition: Response.php:251
write( $body)
Append the HTTP response body.
Definition: Response.php:222
canHaveBody()
Can this HTTP response have a body?
Definition: Response.php:270
send()
Send HTTP response.
Definition: Response.php:317
$messages
Definition: en-x-test.php:7