1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
<?php
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# HTTP class #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# #
# Simple static class with more or less useful methods to simplify some checks #
# for the current HTTP request which was sent by the client. #
# #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
class HTTP {
private static $GET = NULL;
private static $POST = NULL;
private static $FILE = NULL;
#===============================================================================
# HTTP protocol versions
#===============================================================================
const HTTP_VERSION_1_0 = 'HTTP/1.0';
const HTTP_VERSION_1_1 = 'HTTP/1.1';
const HTTP_VERSION_2_0 = 'HTTP/2.0';
#===============================================================================
# HTTP header fields
#===============================================================================
const HEADER_CONTENT_TYPE = 'Content-Type';
const HEADER_TRANSFER_ENCODING = 'Transfer-Encoding';
const HEADER_ACCESS_CONTROL = 'Access-Control-Allow-Origin';
#===============================================================================
# Values for HTTP header fields
#===============================================================================
const CONTENT_TYPE_JSCRIPT = 'application/x-javascript; charset=UTF-8';
const CONTENT_TYPE_TEXT = 'text/plain; charset=UTF-8';
const CONTENT_TYPE_HTML = 'text/html; charset=UTF-8';
const CONTENT_TYPE_JSON = 'application/json; charset=UTF-8';
const CONTENT_TYPE_XML = 'text/xml; charset=UTF-8';
#===============================================================================
# Initialize $GET, $POST and $FILE
#===============================================================================
public static function init($trimValues = TRUE) {
self::$GET = ($trimValues === TRUE ? self::trim($_GET) : $_GET );
self::$POST = ($trimValues === TRUE ? self::trim($_POST) : $_POST);
self::$FILE = $_FILES;
}
#===============================================================================
# Return a HTTP status code header description
#===============================================================================
private static function getStatuscode($code) {
return [
200 => 'OK',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Authorization Required',
403 => 'Forbidden',
404 => 'Not Found',
500 => 'Internal Server Error',
503 => 'Service Temporarily Unavailable',
][$code];
}
#===============================================================================
# Trim all strings in argument
#===============================================================================
private static function trim($mixed) {
if(is_array($mixed)) {
return array_map('self::trim', $mixed);
}
return trim($mixed);
}
#===============================================================================
# Check if all elements of $arguments are set as key of $data
#===============================================================================
private static function issetData($data, $arguments) {
foreach($arguments as $key) {
if(is_array($key)) {
if(!isset($data[key($key)]) OR $data[key($key)] !== $key[key($key)]) {
return FALSE;
}
}
else if(!isset($data[$key]) OR !is_string($data[$key])) {
return FALSE;
}
}
return TRUE;
}
#===============================================================================
# Return null or the value (if set) from one of the three requests attributes
#===============================================================================
public static function returnKey($data, Array $paths) {
$current = &$data;
foreach($paths as $path) {
if(!isset($current[$path]) OR (!is_string($current[$path]) AND !is_array($current[$path]))) {
return NULL;
}
$current = &$current[$path];
}
return $current;
}
#===============================================================================
# Return GET value
#===============================================================================
public static function GET() {
return self::returnKey(self::$GET, func_get_args());
}
#===============================================================================
# Return POST value
#===============================================================================
public static function POST() {
return self::returnKey(self::$POST, func_get_args());
}
#===============================================================================
# Return FILE value
#===============================================================================
public static function FILE() {
return self::returnKey(self::$FILE, func_get_args());
}
#===============================================================================
# Check if all elements of func_get_args() are set key of self::$POST
#===============================================================================
public static function issetPOST() {
return self::issetData(self::$POST, func_get_args());
}
#===============================================================================
# Check if all elements of func_get_args() are set key of self::$GET
#===============================================================================
public static function issetGET() {
return self::issetData(self::$GET, func_get_args());
}
#===============================================================================
# Check if all elements of func_get_args() are set key of self::$FILE
#===============================================================================
public static function issetFILE() {
return self::issetData(self::$FILE, func_get_args());
}
#===============================================================================
# Return HTTP request method or check if request method equals with $method
#===============================================================================
public static function requestMethod($method = NULL) {
if(!empty($method)) {
return ($_SERVER['REQUEST_METHOD'] === $method);
}
return $_SERVER['REQUEST_METHOD'];
}
#===============================================================================
# Return REQUEST_URI
#===============================================================================
public static function requestURI() {
return $_SERVER['REQUEST_URI'] ?? FALSE;
}
#===============================================================================
# Return HTTP_USER_AGENT
#===============================================================================
public static function useragent() {
return trim($_SERVER['HTTP_USER_AGENT'] ?? '');
}
#===============================================================================
# Return HTTP_REFERER
#===============================================================================
public static function referer() {
return trim($_SERVER['HTTP_REFERER'] ?? '');
}
#===============================================================================
# Return response status
#===============================================================================
public static function responseStatus($code = NULL) {
if(!empty($code)) {
self::sendHeader(sprintf('HTTP/1.1 %d %s', $code, self::getStatuscode($code)));
}
return http_response_code();
}
#===============================================================================
# Send a HTTP header line to the client
#===============================================================================
public static function responseHeader($field, $value) {
self::sendHeader("{$field}: {$value}");
}
#===============================================================================
# Send a HTTP redirect to the client
#===============================================================================
public static function redirect($location, $code = 303, $exit = TRUE) {
self::sendHeader(sprintf('HTTP/1.1 %d %s', $code, self::getStatuscode($code)));
self::sendHeader("Location: {$location}");
$exit AND exit();
}
#===============================================================================
# Send a new HTTP header line to the client if headers are not already sent
#===============================================================================
private static function sendHeader($header) {
if(!headers_sent()) {
header($header);
}
}
}
?>
|