Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / response.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Response {
4
5 private static $content = '';
6 private static $headers = array();
7 private static $status = 200;
8
9 private static $statuses = array(
10 100 => 'Continue',
11 101 => 'Switching Protocols',
12 200 => 'OK',
13 201 => 'Created',
14 202 => 'Accepted',
15 203 => 'Non-Authoritative Information',
16 204 => 'No Content',
17 205 => 'Reset Content',
18 206 => 'Partial Content',
19 207 => 'Multi-Status',
20 300 => 'Multiple Choices',
21 301 => 'Moved Permanently',
22 302 => 'Found',
23 303 => 'See Other',
24 304 => 'Not Modified',
25 305 => 'Use Proxy',
26 307 => 'Temporary Redirect',
27 400 => 'Bad Request',
28 401 => 'Unauthorized',
29 402 => 'Payment Required',
30 403 => 'Forbidden',
31 404 => 'Not Found',
32 405 => 'Method Not Allowed',
33 406 => 'Not Acceptable',
34 407 => 'Proxy Authentication Required',
35 408 => 'Request Timeout',
36 409 => 'Conflict',
37 410 => 'Gone',
38 411 => 'Length Required',
39 412 => 'Precondition Failed',
40 413 => 'Request Entity Too Large',
41 414 => 'Request-URI Too Long',
42 415 => 'Unsupported Media Type',
43 416 => 'Requested Range Not Satisfiable',
44 417 => 'Expectation Failed',
45 422 => 'Unprocessable Entity',
46 423 => 'Locked',
47 424 => 'Failed Dependency',
48 500 => 'Internal Server Error',
49 501 => 'Not Implemented',
50 502 => 'Bad Gateway',
51 503 => 'Service Unavailable',
52 504 => 'Gateway Timeout',
53 505 => 'HTTP Version Not Supported',
54 507 => 'Insufficient Storage',
55 509 => 'Bandwidth Limit Exceeded'
56 );
57
58 public static function header($name, $value) {
59 static::$headers[$name] = $value;
60 }
61
62 public static function content($str) {
63 static::$content = $str;
64 }
65
66 public static function append($str) {
67 static::$content .= $str;
68 }
69
70 public static function error($code = 500) {
71 if(isset(static::$statuses[$code])) {
72 static::$status = $code;
73 }
74
75 switch($code) {
76 case 404:
77 Template::render(404);
78 }
79 }
80
81 public static function send() {
82 // set content type
83 if(array_key_exists('Content-Type', static::$headers) === false) {
84 static::$headers['Content-Type'] = 'text/html; charset=UTF-8';
85 }
86
87 // send headers
88 if(headers_sent() === false) {
89
90 $protocol = Input::server('server_protocol', 'HTTP/1.1');
91
92 header($protocol . ' ' . static::$status . ' ' . static::$statuses[static::$status]);
93
94 foreach(static::$headers as $name => $value) {
95 header($name . ': ' . $value, true);
96 }
97 }
98
99 // Send it to the browser!
100 echo static::$content;
101 }
102
103 public static function redirect($url) {
104 static::header('Location', Url::make($url));
105 static::$status = 302;
106 static::$content = '';
107 }
108
109 }