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