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.3 |
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
|
5 |
|
} |
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
|
2 |
|
$qval = 0.0; |
140
|
2 |
|
foreach ( $lang as $key => $value ) { |
141
|
2 |
|
if ($value > $qval) { |
142
|
2 |
|
$qval = ( float ) $value; |
143
|
2 |
|
$deflang = $key; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
2 |
|
return $deflang; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function setLocale(string $locale): void { |
151
|
|
|
try { |
152
|
|
|
if (\class_exists ( 'Locale', false )) { |
153
|
|
|
\Locale::setDefault ( $locale ); |
154
|
|
|
} |
155
|
|
|
} catch ( \Exception $e ) { |
156
|
|
|
// Nothing to do |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist |
163
|
|
|
* |
164
|
|
|
* @param string $key |
165
|
|
|
* @param string $default return value by default |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public static function get($key, $default = NULL): ?string { |
169
|
|
|
return $_GET [$key] ?? $default; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns a boolean at the key position in request |
174
|
|
|
* |
175
|
|
|
* @param string $key the key to add or set |
176
|
|
|
* @return boolean |
177
|
|
|
*/ |
178
|
1 |
|
public static function getBoolean($key): bool { |
179
|
1 |
|
$ret = false; |
180
|
1 |
|
if (isset ( $_REQUEST [$key] )) { |
181
|
1 |
|
$ret = UString::isBooleanTrue ( $_REQUEST [$key] ); |
182
|
|
|
} |
183
|
1 |
|
return $ret; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist |
188
|
|
|
* |
189
|
|
|
* @param string $key |
190
|
|
|
* @param string $default return value by default |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
9 |
|
public static function post($key, $default = NULL) { |
194
|
9 |
|
return $_POST [$key] ?? $default; |
195
|
|
|
} |
196
|
|
|
|
197
|
25 |
|
public static function getUrl($url): string { |
198
|
25 |
|
$config = Startup::getConfig (); |
199
|
25 |
|
$siteUrl = \rtrim ( $config ['siteUrl'], '/' ); |
200
|
25 |
|
if (UString::startswith ( $url, '/' ) === false) { |
201
|
22 |
|
$url = '/' . $url; |
202
|
|
|
} |
203
|
25 |
|
return $siteUrl . $url; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public static function getUrlParts(): array { |
207
|
|
|
return \explode ( '/', $_GET ['c'] ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns the http method |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public static function getMethod(): string { |
216
|
|
|
return \strtolower ( $_SERVER ['REQUEST_METHOD'] ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns the request origin |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
13 |
|
public static function getOrigin(): string { |
225
|
13 |
|
$headers = Startup::getHttpInstance ()->getAllHeaders (); |
226
|
13 |
|
if (isset ( $headers ['Origin'] )) { |
227
|
1 |
|
return $headers ['Origin']; |
228
|
|
|
} |
229
|
12 |
|
if (isset ( $_SERVER ['HTTP_ORIGIN'] )) { |
230
|
|
|
return $_SERVER ['HTTP_ORIGIN']; |
231
|
12 |
|
} else if (isset ( $_SERVER ['HTTP_REFERER'] )) { |
232
|
4 |
|
return $_SERVER ['HTTP_REFERER']; |
233
|
|
|
} else { |
234
|
8 |
|
return $_SERVER ['REMOTE_ADDR']; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
public static function cleanUrl($url): string { |
239
|
1 |
|
$url = \str_replace ( "\\", "/", $url ); |
240
|
1 |
|
return \str_replace ( "//", "/", $url ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Fix up PHP's messing up input containing dots, etc. |
245
|
|
|
* `$source` can be either 'post' or 'get' |
246
|
|
|
* |
247
|
|
|
* @param string $source |
248
|
|
|
* @return string[] |
249
|
|
|
* @see https://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays#68667 |
250
|
|
|
*/ |
251
|
1 |
|
public static function getRealInput($source = 'post'): array { |
252
|
1 |
|
$pairs = \explode ( '&', \strtolower ( $source ) === 'get' ? $_SERVER ['QUERY_STRING'] : \file_get_contents ( 'php://input' ) ); |
253
|
1 |
|
$vars = array (); |
254
|
1 |
|
foreach ( $pairs as $pair ) { |
255
|
1 |
|
$nv = \explode ( "=", $pair ); |
256
|
1 |
|
$name = \urldecode ( $nv [0] ); |
257
|
1 |
|
$value = \urldecode ( $nv [1] ?? ''); |
258
|
1 |
|
$vars [$name] = $value; |
259
|
|
|
} |
260
|
1 |
|
return $vars; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public static function getRealGET(): array { |
264
|
|
|
return self::getRealInput ( 'get' ); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public static function getRealPOST(): array { |
268
|
|
|
return self::getRealInput ( 'post' ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Creates a password hash for a posted value at $key position |
273
|
|
|
* |
274
|
|
|
* @param string $key |
275
|
|
|
* @param string $algo |
276
|
|
|
* @return string|boolean |
277
|
|
|
*/ |
278
|
|
|
public static function password_hash(string $key, string $algo = PASSWORD_DEFAULT) { |
279
|
|
|
if (isset ( $_POST [$key] )) { |
280
|
|
|
return $_POST [$key] = \password_hash ( $_POST [$key], $algo ); |
281
|
|
|
} |
282
|
|
|
return false; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Verifies that a posted password matches a hash at $passwordKey position. |
288
|
|
|
* |
289
|
|
|
* @param string $passwordKey |
290
|
|
|
* @param string $hash |
291
|
|
|
* @return bool |
292
|
|
|
* @since 2.4.0 |
293
|
|
|
*/ |
294
|
|
|
public static function password_verify(string $passwordKey,string $hash):bool { |
295
|
|
|
if (isset ( $_POST [$passwordKey] )) { |
296
|
|
|
return \password_verify( $_POST [$passwordKey], $hash ); |
297
|
|
|
} |
298
|
|
|
return false; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Internal use for async servers (Swoole and Workerman). |
303
|
|
|
* @param string $uri |
304
|
|
|
* @param string $basedir |
305
|
|
|
* @return array |
306
|
|
|
*/ |
307
|
|
|
public static function parseURI(string $uri,string $basedir):array { |
308
|
|
|
return self::$uriInfos[$uri]??=self::_parseURI($uri,$basedir); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
private static function _parseURI(string $uri,string $basedir):array { |
312
|
|
|
$uri = \ltrim(\urldecode(\parse_url($uri, PHP_URL_PATH)), '/'); |
313
|
|
|
$isAction = ($uri == null || ! ($fe = \file_exists($basedir . '/../' . $uri))) && ($uri != 'favicon.ico'); |
314
|
|
|
return [ |
315
|
|
|
'uri' => $uri, |
316
|
|
|
'isAction' => $isAction, |
317
|
|
|
'file' => $fe??false |
318
|
|
|
]; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|