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