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
|
5 |
|
*/ |
27
|
5 |
|
public static function setValuesToObject($object, $values = null): void { |
28
|
|
|
if (! isset ( $values )) { |
29
|
|
|
$values = $_POST; |
30
|
5 |
|
} |
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
|
|
|
$object->_rest [$key] = $value; |
36
|
|
|
} |
37
|
5 |
|
} |
38
|
|
|
} |
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
|
1 |
|
*/ |
73
|
1 |
|
public static function getInput(): array { |
74
|
|
|
return Startup::getHttpInstance ()->getInput (); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the query data, regardless of the method |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
1 |
|
*/ |
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
|
|
|
return $_POST; |
88
|
|
|
} |
89
|
1 |
|
break; |
90
|
|
|
case 'get' : |
91
|
|
|
return $_GET; |
92
|
1 |
|
default : |
93
|
|
|
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
|
1 |
|
*/ |
116
|
1 |
|
public static function getDefaultLanguage(): string { |
117
|
|
|
if (isset ( $_SERVER ['HTTP_ACCEPT_LANGUAGE'] )) { |
118
|
|
|
return self::parseDefaultLanguage ( $_SERVER ['HTTP_ACCEPT_LANGUAGE'] ); |
119
|
|
|
} |
120
|
|
|
return self::parseDefaultLanguage ( NULL ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private static function parseDefaultLanguage($http_accept, $deflang = 'en'): string { |
124
|
|
|
if (isset ( $http_accept ) && \strlen ( $http_accept ) > 1) { |
125
|
|
|
$x = \explode ( ",", $http_accept ); |
126
|
|
|
$lang = [ ]; |
127
|
|
|
foreach ( $x as $val ) { |
128
|
|
|
if (\preg_match ( "/(.*);q=([0-1]{0,1}.\d{0,4})/i", $val, $matches )) |
129
|
|
|
$lang [$matches [1]] = ( float ) $matches [2]; |
130
|
|
|
else |
131
|
|
|
$lang [$val] = 1.0; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$qval = 0.0; |
135
|
1 |
|
foreach ( $lang as $key => $value ) { |
136
|
1 |
|
if ($value > $qval) { |
137
|
1 |
|
$qval = ( float ) $value; |
138
|
1 |
|
$deflang = $key; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
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
|
2 |
|
} |
150
|
2 |
|
} catch ( \Exception $e ) { |
151
|
2 |
|
// Nothing to do |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
2 |
|
/** |
157
|
2 |
|
* Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist |
158
|
2 |
|
* |
159
|
2 |
|
* @param string $key |
160
|
2 |
|
* @param string $default return value by default |
161
|
2 |
|
* @return string |
162
|
2 |
|
*/ |
163
|
|
|
public static function get($key, $default = NULL): ?string { |
164
|
2 |
|
return $_GET [$key] ?? $default; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
/** |
168
|
2 |
|
* Returns a boolean at the key position in request |
169
|
2 |
|
* |
170
|
2 |
|
* @param string $key the key to add or set |
171
|
2 |
|
* @return boolean |
172
|
|
|
*/ |
173
|
|
|
public static function getBoolean($key): bool { |
174
|
|
|
$ret = false; |
175
|
2 |
|
if (isset ( $_REQUEST [$key] )) { |
176
|
|
|
$ret = UString::isBooleanTrue ( $_REQUEST [$key] ); |
177
|
|
|
} |
178
|
|
|
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
|
|
|
public static function post($key, $default = NULL) { |
189
|
|
|
return $_POST [$key] ?? $default; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public static function getUrl($url): string { |
193
|
25 |
|
$config = Startup::getConfig (); |
194
|
25 |
|
$siteUrl = \rtrim ( $config ['siteUrl'], '/' ); |
195
|
|
|
if (UString::startswith ( $url, '/' ) === false) { |
196
|
|
|
$url = '/' . $url; |
197
|
|
|
} |
198
|
|
|
return $siteUrl . $url; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public static function getUrlParts(): array { |
202
|
8 |
|
return \explode ( '/', $_GET ['c'] ); |
203
|
8 |
|
} |
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
|
|
|
public static function getOrigin(): string { |
220
|
1 |
|
$headers = Startup::getHttpInstance ()->getAllHeaders (); |
221
|
1 |
|
if (isset ( $headers ['Origin'] )) { |
222
|
1 |
|
return $headers ['Origin']; |
223
|
|
|
} |
224
|
|
|
if (isset ( $_SERVER ['HTTP_ORIGIN'] )) { |
225
|
|
|
return $_SERVER ['HTTP_ORIGIN']; |
226
|
|
|
} else if (isset ( $_SERVER ['HTTP_REFERER'] )) { |
227
|
|
|
return $_SERVER ['HTTP_REFERER']; |
228
|
|
|
} else { |
229
|
|
|
return $_SERVER ['REMOTE_ADDR']; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public static function cleanUrl($url): string { |
234
|
|
|
$url = \str_replace ( "\\", "/", $url ); |
235
|
|
|
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
|
1 |
|
* @param string $source |
243
|
1 |
|
* @return string[] |
244
|
1 |
|
* @see https://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays#68667 |
245
|
1 |
|
*/ |
246
|
|
|
public static function getRealInput($source = 'post'): array { |
247
|
1 |
|
$pairs = \explode ( '&', \strtolower ( $source ) === 'get' ? $_SERVER ['QUERY_STRING'] : \file_get_contents ( 'php://input' ) ); |
248
|
|
|
$vars = array (); |
249
|
|
|
foreach ( $pairs as $pair ) { |
250
|
|
|
$nv = \explode ( "=", $pair ); |
251
|
|
|
$name = \urldecode ( $nv [0] ); |
252
|
|
|
$value = \urldecode ( $nv [1] ?? ''); |
253
|
|
|
$vars [$name] = $value; |
254
|
|
|
} |
255
|
|
|
return $vars; |
256
|
|
|
} |
257
|
8 |
|
|
258
|
8 |
|
public static function getRealGET(): array { |
259
|
|
|
return self::getRealInput ( 'get' ); |
260
|
|
|
} |
261
|
23 |
|
|
262
|
23 |
|
public static function getRealPOST(): array { |
263
|
23 |
|
return self::getRealInput ( 'post' ); |
264
|
23 |
|
} |
265
|
21 |
|
|
266
|
|
|
/** |
267
|
23 |
|
* 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
|
16 |
|
*/ |
289
|
16 |
|
public static function password_verify(string $passwordKey,string $hash):bool { |
290
|
16 |
|
if (isset ( $_POST [$passwordKey] )) { |
291
|
1 |
|
return \password_verify( $_POST [$passwordKey], $hash ); |
292
|
|
|
} |
293
|
15 |
|
return false; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|