1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Request |
5
|
|
|
* |
6
|
|
|
* Handles the HTTP request for the current execution. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2016 - http://caffeina.com |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Request { |
14
|
|
|
use Module; |
15
|
|
|
|
16
|
|
|
protected static $body, |
|
|
|
|
17
|
|
|
$accepts; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Handle Content Negotiation requests |
21
|
|
|
* |
22
|
|
|
* @param string $key The name of the negotiation subject |
23
|
|
|
* @param string $choices A query string for the negotiation choices (See RFC 7231) |
24
|
|
|
* |
25
|
|
|
* @return Object The preferred content if $choices is empty else return best match |
26
|
|
|
*/ |
27
|
|
|
public static function accept($key='type',$choices='') { |
28
|
|
|
if (null === static::$accepts) static::$accepts = [ |
29
|
|
|
'type' => new Negotiation(isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : ''), |
30
|
|
|
'language' => new Negotiation(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''), |
31
|
|
|
'encoding' => new Negotiation(isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : ''), |
32
|
|
|
'charset' => new Negotiation(isset($_SERVER['HTTP_ACCEPT_CHARSET']) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : ''), |
33
|
|
|
]; |
34
|
|
|
return empty(static::$accepts[$key]) |
35
|
|
|
? false |
36
|
|
|
: ( empty($choices) |
37
|
|
|
? static::$accepts[$key]->preferred() |
38
|
|
|
: static::$accepts[$key]->best($choices) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Retrive a value from generic input (from the $_REQUEST array) |
44
|
|
|
* Returns all elements if you pass `null` as $key |
45
|
|
|
* |
46
|
|
|
* @param string $key The name of the input value |
47
|
|
|
* |
48
|
|
|
* @return Object The returned value or $default. |
49
|
|
|
*/ |
50
|
|
|
public static function input($key=null,$default=null){ |
51
|
|
|
return $key ? (isset($_REQUEST[$key]) ? new Object($_REQUEST[$key]) : (is_callable($default)?call_user_func($default):$default)) : new Object($_REQUEST[$key]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrive a value from environment (from the $_ENV array) |
56
|
|
|
* Returns all elements if you pass `null` as $key |
57
|
|
|
* |
58
|
|
|
* @param string $key The name of the input value |
59
|
|
|
* |
60
|
|
|
* @return Object The returned value or $default. |
61
|
|
|
*/ |
62
|
|
|
public static function env($key=null,$default=null){ |
63
|
|
|
return $key ? (filter_input(INPUT_ENV,$key) ?: (is_callable($default)?call_user_func($default):$default)) : $_ENV; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retrive a value from server (from the $_SERVER array) |
68
|
|
|
* Returns all elements if you pass `null` as $key |
69
|
|
|
* |
70
|
|
|
* @param string $key The name of the input value |
71
|
|
|
* |
72
|
|
|
* @return Object The returned value or $default. |
73
|
|
|
*/ |
74
|
|
|
public static function server($key=null,$default=null){ |
75
|
|
|
return $key ? (isset($_SERVER[$key]) ? $_SERVER[$key] : (is_callable($default)?call_user_func($default):$default)) : $_SERVER; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrive a value from generic input (from the $_POST array) |
80
|
|
|
* Returns all elements if you pass `null` as $key |
81
|
|
|
* |
82
|
|
|
* @param string $key The name of the input value |
83
|
|
|
* |
84
|
|
|
* @return Object The returned value or $default. |
85
|
|
|
*/ |
86
|
|
|
public static function post($key=null,$default=null){ |
87
|
|
|
return $key ? (filter_input(INPUT_POST,$key) ?: (is_callable($default)?call_user_func($default):$default)) : $_POST; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrive a value from generic input (from the $_GET array) |
92
|
|
|
* Returns all elements if you pass `null` as $key |
93
|
|
|
* |
94
|
|
|
* @param string $key The name of the input value |
95
|
|
|
* |
96
|
|
|
* @return Object The returned value or $default. |
97
|
|
|
*/ |
98
|
|
|
public static function get($key=null,$default=null){ |
99
|
|
|
return $key ? (filter_input(INPUT_GET,$key) ?: (is_callable($default)?call_user_func($default):$default)) : $_GET; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrive uploaded file (from the $_FILES array) |
104
|
|
|
* Returns all uploaded files if you pass `null` as $key |
105
|
|
|
* |
106
|
|
|
* @param string $key The name of the input value |
107
|
|
|
* |
108
|
|
|
* @return Object The returned value or $default. |
109
|
|
|
*/ |
110
|
|
|
public static function files($key=null,$default=null){ |
111
|
|
|
return $key ? (isset($_FILES[$key]) ? $_FILES[$key] : (is_callable($default)?call_user_func($default):$default)) : $_FILES; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retrive cookie (from the $_COOKIE array) |
116
|
|
|
* Returns all cookies if you pass `null` as $key |
117
|
|
|
* |
118
|
|
|
* @param string $key The name of the input value |
119
|
|
|
* |
120
|
|
|
* @return Object The returned value or $default. |
121
|
|
|
*/ |
122
|
|
|
public static function cookie($key=null,$default=null){ |
123
|
|
|
return $key ? (filter_input(INPUT_COOKIE,$key) ?: (is_callable($default)?call_user_func($default):$default)) : $_COOKIE; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the current host and port (omitted if port 80), complete with protocol (pass `false` to omit). |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public static function host($protocol=true){ |
132
|
|
|
switch(true){ |
133
|
|
View Code Duplication |
case !empty($_SERVER['HTTP_X_FORWARDED_HOST']) : |
134
|
|
|
$host = trim(substr(strrchr($_SERVER['HTTP_X_FORWARDED_HOST'],','),1) ?: $_SERVER['HTTP_X_FORWARDED_HOST']); |
135
|
|
|
break; |
136
|
|
|
case !empty($_SERVER['HTTP_HOST']) : $host = $_SERVER['HTTP_HOST']; break; |
137
|
|
|
case !empty($_SERVER['SERVER_NAME']) : $host = $_SERVER['SERVER_NAME']; break; |
138
|
|
|
case !empty($_SERVER['HOSTNAME']) : $host = $_SERVER['HOSTNAME']; break; |
139
|
|
|
default : $host = 'localhost'; break; |
140
|
|
|
} |
141
|
|
|
$host = explode(':',$host,2); |
142
|
|
|
$port = isset($host[1]) ? (int)$host[1] : (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80); |
143
|
|
|
$host = $host[0] . (($port && $port != 80) ? ":$port" : ''); |
144
|
|
|
if ($port == 80) $port = ''; |
|
|
|
|
145
|
|
|
return ($protocol ? 'http' . (!empty($_SERVER['HTTPS'])&&(strtolower($_SERVER['HTTPS'])!=='off')?'s':'') . '://' : '') |
146
|
|
|
. Filter::with('core.request.host',$host); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the current request URL, complete with host and protocol. |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public static function URL(){ |
155
|
|
|
return static::host(true) . static::URI(false); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Retrive header |
160
|
|
|
* Returns all headers if you pass `null` as $key |
161
|
|
|
* |
162
|
|
|
* @param string $key The name of the input value |
163
|
|
|
* |
164
|
|
|
* @return Object The returned value or null. |
165
|
|
|
*/ |
166
|
|
|
public static function header($key=null,$default=null){ |
167
|
|
|
$key = 'HTTP_'.strtr(strtoupper($key),'-','_'); |
168
|
|
|
return $key ? (isset($_SERVER[$key])? $_SERVER[$key] : (is_callable($default)?call_user_func($default):$default)) : $_SERVER; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns the current request URI. |
173
|
|
|
* |
174
|
|
|
* @param boolean $relative If true, trim the URI relative to the application index.php script. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public static function URI($relative=true){ |
179
|
|
|
// On some web server configurations SCRIPT_NAME is not populated. |
180
|
|
|
$self = !empty($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF']; |
181
|
|
|
// Search REQUEST_URI in $_SERVER |
182
|
|
|
switch(true){ |
183
|
|
|
case !empty($_SERVER['REQUEST_URI']): $serv_uri = $_SERVER['REQUEST_URI']; break; |
184
|
|
|
case !empty($_SERVER['ORIG_PATH_INFO']): $serv_uri = $_SERVER['ORIG_PATH_INFO']; break; |
185
|
|
|
case !empty($_SERVER['PATH_INFO']): $serv_uri = $_SERVER['PATH_INFO']; break; |
186
|
|
|
default: $serv_uri = '/'; break; |
187
|
|
|
} |
188
|
|
|
$uri = strtok($serv_uri,'?'); |
189
|
|
|
$uri = ($uri == $self) ? '/' : $uri; |
190
|
|
|
|
191
|
|
|
// Add a filter here, for URL rewriting |
192
|
|
|
$uri = Filter::with('core.request.URI',$uri); |
193
|
|
|
|
194
|
|
|
$uri = rtrim($uri,'/'); |
195
|
|
|
|
196
|
|
|
if ($relative){ |
197
|
|
|
$base = rtrim(dirname($self),'/'); |
198
|
|
|
$uri = str_replace($base,'',$uri); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $uri ?: '/'; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns the current base URI (The front-controller directory) |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public static function baseURI(){ |
210
|
|
|
return dirname(empty($_SERVER['SCRIPT_NAME']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']) ?: '/'; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the HTTP Method |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public static function method(){ |
219
|
|
|
return Filter::with('core.request.method',strtolower(empty($_SERVER['REQUEST_METHOD'])?'get':$_SERVER['REQUEST_METHOD'])); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns the remote IP |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public static function IP(){ |
228
|
|
|
switch(true){ |
229
|
|
View Code Duplication |
case !empty($_SERVER['HTTP_X_FORWARDED_FOR']): |
230
|
|
|
$ip = trim(substr(strrchr($_SERVER['HTTP_X_FORWARDED_FOR'],','),1) ?: $_SERVER['HTTP_X_FORWARDED_FOR']); |
231
|
|
|
break; |
232
|
|
View Code Duplication |
case !empty($_SERVER['HTTP_X_FORWARDED_HOST']): |
233
|
|
|
$ip = trim(substr(strrchr($_SERVER['HTTP_X_FORWARDED_HOST'],','),1) ?: $_SERVER['HTTP_X_FORWARDED_HOST']); |
234
|
|
|
break; |
235
|
|
|
case !empty($_SERVER['REMOTE_ADDR']): $ip = $_SERVER['REMOTE_ADDR']; break; |
236
|
|
|
case !empty($_SERVER['HTTP_CLIENT_IP']): $ip = $_SERVER['HTTP_CLIENT_IP']; break; |
237
|
|
|
default: $ip = ''; break; |
238
|
|
|
} |
239
|
|
|
return Filter::with('core.request.IP',$ip); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Returns the remote UserAgent |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public static function UA(){ |
248
|
|
|
return Filter::with('core.request.UA',strtolower(empty($_SERVER['HTTP_USER_AGENT'])?'':$_SERVER['HTTP_USER_AGENT'])); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns request body data, convert to object if content type is JSON |
253
|
|
|
* Gives you all request data if you pass `null` as $key |
254
|
|
|
* |
255
|
|
|
* @param string $key The name of the key requested |
256
|
|
|
* |
257
|
|
|
* @return mixed The request body data |
258
|
|
|
*/ |
259
|
|
|
public static function data($key=null,$default=null){ |
260
|
|
|
if (null===static::$body){ |
261
|
|
|
$json = (false !== stripos(empty($_SERVER['HTTP_CONTENT_TYPE'])?'':$_SERVER['HTTP_CONTENT_TYPE'],'json')) |
262
|
|
|
|| (false !== stripos(empty($_SERVER['CONTENT_TYPE'])?'':$_SERVER['CONTENT_TYPE'],'json')); |
263
|
|
|
if ($json) { |
264
|
|
|
static::$body = json_decode(file_get_contents("php://input")); |
265
|
|
|
} else { |
266
|
|
|
if (empty($_POST)) { |
267
|
|
|
static::$body = file_get_contents("php://input"); |
268
|
|
|
} else { |
269
|
|
|
static::$body = (object)$_POST; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
return $key ? (isset(static::$body->$key) ? static::$body->$key : (is_callable($default)?call_user_func($default):$default)) : static::$body; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.