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