1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\http; |
4
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Startup; |
6
|
|
|
use Ubiquity\utils\base\UString; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input. |
10
|
|
|
* Ubiquity\utils\http$URequest |
11
|
|
|
* This class is part of Ubiquity |
12
|
|
|
* |
13
|
|
|
* @author jcheron <[email protected]> |
14
|
|
|
* @version 1.1.0 |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class URequest { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Affects member to member the values of the associative array $values to the members of the object $object |
21
|
|
|
* Used for example to retrieve the variables posted and assign them to the members of an object |
22
|
|
|
* |
23
|
|
|
* @param object $object |
24
|
|
|
* @param associative array $values |
25
|
|
|
*/ |
26
|
3 |
|
public static function setValuesToObject($object, $values = null) { |
27
|
3 |
|
if (! isset ( $values )) |
28
|
|
|
$values = $_POST; |
29
|
3 |
|
foreach ( $values as $key => $value ) { |
30
|
3 |
|
$accessor = "set" . ucfirst ( $key ); |
31
|
3 |
|
if (method_exists ( $object, $accessor )) { |
32
|
3 |
|
$object->$accessor ( $value ); |
33
|
3 |
|
$object->_rest [$key] = $value; |
34
|
|
|
} |
35
|
|
|
} |
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Affects member to member the values of $_GET to the members of the object $object |
40
|
|
|
* $object must have accessors to each members |
41
|
|
|
* |
42
|
|
|
* @param object $object |
43
|
|
|
*/ |
44
|
|
|
public static function setGetValuesToObject($object) { |
45
|
|
|
self::setValuesToObject ( $object, $_GET ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Affects member to member the values of $_POST to the members of the object $object |
50
|
|
|
* $object must have accessors to each members |
51
|
|
|
* |
52
|
|
|
* @param object $object |
53
|
|
|
*/ |
54
|
|
|
public static function setPostValuesToObject($object) { |
55
|
|
|
self::setValuesToObject ( $object, $_POST ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Call a cleaning function on the post |
60
|
|
|
* |
61
|
|
|
* @param string $function the cleaning function, default htmlentities |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public static function getPost($function = "htmlentities") { |
65
|
|
|
return array_map ( $function, $_POST ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the query data, for PUT, DELETE PATCH methods |
70
|
|
|
*/ |
71
|
|
|
public static function getInput() { |
72
|
|
|
return Startup::getHttpInstance ()->getInput (); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the query data, regardless of the method |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public static function getDatas() { |
81
|
|
|
$method = \strtolower ( $_SERVER ['REQUEST_METHOD'] ); |
82
|
|
|
switch ($method) { |
83
|
|
|
case 'post' : |
84
|
|
|
if (self::getContentType () == 'application/x-www-form-urlencoded') { |
85
|
|
|
return $_POST; |
86
|
|
|
} |
87
|
|
|
break; |
88
|
|
|
case 'get' : |
89
|
|
|
return $_GET; |
90
|
|
|
default : |
91
|
|
|
return self::getInput (); |
92
|
|
|
} |
93
|
|
|
return self::getInput (); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests if a value is present on the request and is not empty |
98
|
|
|
* |
99
|
|
|
* @param string $key |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public static function filled($key) { |
104
|
|
|
return isset ( $_REQUEST [$key] ) && $_REQUEST [$key] != null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tests if a value is present on the request |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public static function has($key) { |
115
|
|
|
return isset ( $_REQUEST [$key] ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the request content-type header |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public static function getContentType() { |
124
|
|
|
$headers = Startup::getHttpInstance ()->getAllHeaders (); |
125
|
|
|
if (isset ( $headers ["Content-Type"] )) { |
126
|
|
|
return $headers ["Content-Type"]; |
127
|
|
|
} |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Copyright © 2008 Darrin Yeager |
133
|
|
|
* https://www.dyeager.org/ |
134
|
|
|
* Licensed under BSD license. |
135
|
|
|
* https://www.dyeager.org/downloads/license-bsd.txt |
136
|
|
|
*/ |
137
|
2 |
|
public static function getDefaultLanguage() { |
138
|
2 |
|
if (isset ( $_SERVER ["HTTP_ACCEPT_LANGUAGE"] )) |
139
|
2 |
|
return self::parseDefaultLanguage ( $_SERVER ["HTTP_ACCEPT_LANGUAGE"] ); |
140
|
|
|
else |
141
|
|
|
return self::parseDefaultLanguage ( NULL ); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
private static function parseDefaultLanguage($http_accept, $deflang = "en") { |
145
|
2 |
|
if (isset ( $http_accept ) && strlen ( $http_accept ) > 1) { |
146
|
2 |
|
$x = explode ( ",", $http_accept ); |
147
|
2 |
|
$lang = [ ]; |
148
|
2 |
|
foreach ( $x as $val ) { |
149
|
2 |
|
if (preg_match ( "/(.*);q=([0-1]{0,1}.\d{0,4})/i", $val, $matches )) |
150
|
2 |
|
$lang [$matches [1]] = ( float ) $matches [2]; |
151
|
|
|
else |
152
|
2 |
|
$lang [$val] = 1.0; |
153
|
|
|
} |
154
|
|
|
|
155
|
2 |
|
$qval = 0.0; |
156
|
2 |
|
foreach ( $lang as $key => $value ) { |
157
|
2 |
|
if ($value > $qval) { |
158
|
2 |
|
$qval = ( float ) $value; |
159
|
2 |
|
$deflang = $key; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
2 |
|
return $deflang; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public static function setLocale(string $locale) { |
167
|
|
|
try { |
168
|
|
|
if (class_exists ( 'Locale', false )) { |
169
|
|
|
\Locale::setDefault ( $locale ); |
170
|
|
|
} |
171
|
|
|
} catch ( \Exception $e ) { |
172
|
|
|
// Nothing to do |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns true if the request is an Ajax request |
178
|
|
|
* |
179
|
|
|
* @return boolean |
180
|
|
|
*/ |
181
|
24 |
|
public static function isAjax() { |
182
|
24 |
|
return (isset ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && ! empty ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && strtolower ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns true if the request is sent by the POST method |
187
|
|
|
* |
188
|
|
|
* @return boolean |
189
|
|
|
*/ |
190
|
8 |
|
public static function isPost() { |
191
|
8 |
|
return $_SERVER ['REQUEST_METHOD'] === 'POST'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns true if the request is cross site |
196
|
|
|
* |
197
|
|
|
* @return boolean |
198
|
|
|
*/ |
199
|
|
|
public static function isCrossSite() { |
200
|
|
|
return stripos ( $_SERVER ['HTTP_REFERER'], $_SERVER ['SERVER_NAME'] ) === FALSE; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns true if request contentType is set to json |
205
|
|
|
* |
206
|
|
|
* @return boolean |
207
|
|
|
*/ |
208
|
|
|
public static function isJSON() { |
209
|
|
|
$contentType = self::getContentType (); |
210
|
|
|
return \stripos ( $contentType, "json" ) !== false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist |
215
|
|
|
* |
216
|
|
|
* @param string $key |
217
|
|
|
* @param string $default return value by default |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public static function get($key, $default = NULL) { |
221
|
|
|
return isset ( $_GET [$key] ) ? $_GET [$key] : $default; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns a boolean at the key position in request |
226
|
|
|
* |
227
|
|
|
* @param string $key the key to add or set |
228
|
|
|
* @return boolean |
229
|
|
|
*/ |
230
|
1 |
|
public static function getBoolean($key) { |
231
|
1 |
|
$ret = false; |
232
|
1 |
|
if (isset ( $_REQUEST [$key] )) { |
233
|
1 |
|
$ret = UString::isBooleanTrue ( $_REQUEST [$key] ); |
234
|
|
|
} |
235
|
1 |
|
return $ret; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist |
240
|
|
|
* |
241
|
|
|
* @param string $key |
242
|
|
|
* @param string $default return value by default |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
6 |
|
public static function post($key, $default = NULL) { |
246
|
6 |
|
return isset ( $_POST [$key] ) ? $_POST [$key] : $default; |
247
|
|
|
} |
248
|
|
|
|
249
|
22 |
|
public static function getUrl($url) { |
250
|
22 |
|
$config = Startup::getConfig (); |
251
|
22 |
|
$siteUrl = \rtrim ( $config ["siteUrl"], '/' ); |
252
|
22 |
|
if (UString::startswith ( $url, "/" ) === false) { |
253
|
21 |
|
$url = "/" . $url; |
254
|
|
|
} |
255
|
22 |
|
return $siteUrl . $url; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public static function getUrlParts() { |
259
|
|
|
return \explode ( "/", $_GET ["c"] ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns the http method |
264
|
|
|
* |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
11 |
|
public static function getMethod() { |
268
|
11 |
|
return \strtolower ( $_SERVER ['REQUEST_METHOD'] ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Returns the request origin |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
13 |
|
public static function getOrigin() { |
277
|
13 |
|
$headers = Startup::getHttpInstance ()->getAllHeaders (); |
278
|
13 |
|
if (isset ( $headers ['Origin'] )) { |
279
|
|
|
return $headers ['Origin']; |
280
|
|
|
} |
281
|
13 |
|
if (isset ( $_SERVER ['HTTP_ORIGIN'] )) { |
282
|
|
|
return $_SERVER ['HTTP_ORIGIN']; |
283
|
13 |
|
} else if (isset ( $_SERVER ['HTTP_REFERER'] )) { |
284
|
2 |
|
return $_SERVER ['HTTP_REFERER']; |
285
|
|
|
} else { |
286
|
11 |
|
return $_SERVER ['REMOTE_ADDR']; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public static function cleanUrl($url) { |
291
|
|
|
$url = \str_replace ( "\\", "/", $url ); |
292
|
|
|
return \str_replace ( "//", "/", $url ); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Fix up PHP's messing up input containing dots, etc. |
297
|
|
|
* `$source` can be either 'post' or 'get' |
298
|
|
|
* |
299
|
|
|
* @param string $source |
300
|
|
|
* @return string[] |
301
|
|
|
* @see https://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays#68667 |
302
|
|
|
*/ |
303
|
|
|
public static function getRealInput($source = 'post') { |
304
|
|
|
$pairs = explode ( "&", strtolower ( $source ) === 'get' ? $_SERVER ['QUERY_STRING'] : file_get_contents ( "php://input" ) ); |
305
|
|
|
$vars = array (); |
306
|
|
|
foreach ( $pairs as $pair ) { |
307
|
|
|
$nv = explode ( "=", $pair ); |
308
|
|
|
$name = urldecode ( $nv [0] ); |
309
|
|
|
$value = urldecode ( $nv [1] ?? ''); |
310
|
|
|
$vars [$name] = $value; |
311
|
|
|
} |
312
|
|
|
return $vars; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public static function getRealGET() { |
316
|
|
|
return self::getRealInput ( 'get' ); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public static function getRealPOST() { |
320
|
|
|
return self::getRealInput ( 'post' ); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|