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