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