1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ParameterReader.php |
5
|
|
|
* |
6
|
|
|
* This class processes the input arguments from the GET or POST data of the request. |
7
|
|
|
* If this is a request for the initial page load, no arguments will be processed. |
8
|
|
|
* During a jaxon request, any arguments found in the GET or POST will be converted to a PHP array. |
9
|
|
|
* |
10
|
|
|
* @package jaxon-core |
|
|
|
|
11
|
|
|
* @author Jared White |
|
|
|
|
12
|
|
|
* @author J. Max Wilson |
|
|
|
|
13
|
|
|
* @author Joseph Woolley |
|
|
|
|
14
|
|
|
* @author Steffen Konerow |
|
|
|
|
15
|
|
|
* @author Thierry Feuzeu <[email protected]> |
16
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
17
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
18
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
20
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
|
23
|
|
|
namespace Jaxon\Request\Handler; |
24
|
|
|
|
25
|
|
|
use Jaxon\App\Config\ConfigManager; |
26
|
|
|
use Jaxon\App\I18n\Translator; |
27
|
|
|
use Jaxon\Di\Container; |
28
|
|
|
use Jaxon\Exception\RequestException; |
29
|
|
|
use Jaxon\Utils\Http\UriDetector; |
30
|
|
|
use Jaxon\Utils\Http\UriException; |
31
|
|
|
|
32
|
|
|
use function array_map; |
33
|
|
|
use function call_user_func; |
34
|
|
|
use function floor; |
35
|
|
|
use function function_exists; |
36
|
|
|
use function iconv; |
37
|
|
|
use function intval; |
38
|
|
|
use function is_array; |
39
|
|
|
use function is_string; |
40
|
|
|
use function json_decode; |
41
|
|
|
use function mb_convert_encoding; |
42
|
|
|
use function strcasecmp; |
43
|
|
|
use function strlen; |
44
|
|
|
use function substr; |
45
|
|
|
use function utf8_decode; |
46
|
|
|
|
47
|
|
|
class ParameterReader |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var Container |
51
|
|
|
*/ |
52
|
|
|
protected $di; |
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ConfigManager |
56
|
|
|
*/ |
57
|
|
|
protected $xConfigManager; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Translator |
61
|
|
|
*/ |
62
|
|
|
protected $xTranslator; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var UriDetector |
66
|
|
|
*/ |
67
|
|
|
private $xUriDetector; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The function which decodes input parameters. |
71
|
|
|
* |
72
|
|
|
* @var callable |
73
|
|
|
*/ |
74
|
|
|
private $cParamDecoder; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The function which decodes utf8 string. |
78
|
|
|
* |
79
|
|
|
* @var callable |
80
|
|
|
*/ |
81
|
|
|
private $cUtf8Decoder; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The constructor |
85
|
|
|
* |
86
|
|
|
* @param Container $di |
|
|
|
|
87
|
|
|
* @param ConfigManager $xConfigManager |
|
|
|
|
88
|
|
|
* @param Translator $xTranslator |
|
|
|
|
89
|
|
|
* @param UriDetector $xUriDetector |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
public function __construct(Container $di, ConfigManager $xConfigManager, |
|
|
|
|
92
|
|
|
Translator $xTranslator, UriDetector $xUriDetector) |
93
|
|
|
{ |
94
|
|
|
$this->di = $di; |
|
|
|
|
95
|
|
|
$this->xConfigManager = $xConfigManager; |
96
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
97
|
|
|
$this->xUriDetector = $xUriDetector; |
|
|
|
|
98
|
|
|
} |
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Choose the function to use to decode input data. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
private function setParamDecoder() |
106
|
|
|
{ |
107
|
|
|
// Parameters are url encoded when uploading files |
108
|
|
|
$aServerParams = $this->di->getRequest()->getServerParams(); |
109
|
|
|
$sContentType = ''; |
110
|
|
|
if(isset($aServerParams['CONTENT_TYPE'])) |
111
|
|
|
{ |
112
|
|
|
$sContentType = $aServerParams['CONTENT_TYPE']; |
113
|
|
|
} |
114
|
|
|
elseif(isset($aServerParams['HTTP_CONTENT_TYPE'])) |
115
|
|
|
{ |
116
|
|
|
$sContentType = $aServerParams['HTTP_CONTENT_TYPE']; |
117
|
|
|
} |
118
|
|
|
$sType = 'multipart/form-data'; |
119
|
|
|
if(strncmp($sContentType, $sType, strlen($sType)) !== 0) |
120
|
|
|
{ |
121
|
|
|
$this->cParamDecoder = function($sParam) { return $sParam; }; |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
$this->cParamDecoder = function($sParam) { return urldecode($sParam); }; |
125
|
|
|
} |
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Choose the function to use to decode input data. |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
* @throws RequestException |
132
|
|
|
*/ |
133
|
|
|
private function setUtf8Decoder() |
134
|
|
|
{ |
135
|
|
|
// By default, no decoding |
136
|
|
|
$this->cUtf8Decoder = function($sStr) { |
137
|
|
|
return $sStr; |
138
|
|
|
}; |
139
|
|
|
$sEncoding = $this->xConfigManager->getOption('core.encoding', ''); |
|
|
|
|
140
|
|
|
if(function_exists('iconv')) |
141
|
|
|
{ |
142
|
|
|
$this->cUtf8Decoder = function($sStr) use($sEncoding) { |
143
|
|
|
return iconv("UTF-8", $sEncoding . '//TRANSLIT', $sStr); |
144
|
|
|
}; |
145
|
|
|
} |
146
|
|
|
elseif(function_exists('mb_convert_encoding')) |
147
|
|
|
{ |
148
|
|
|
$this->cUtf8Decoder = function($sStr) use($sEncoding) { |
149
|
|
|
return mb_convert_encoding($sStr, $sEncoding, "UTF-8"); |
150
|
|
|
}; |
151
|
|
|
} |
152
|
|
|
elseif($sEncoding === "ISO-8859-1") |
153
|
|
|
{ |
154
|
|
|
$this->cUtf8Decoder = function($sStr) { |
155
|
|
|
return utf8_decode($sStr); |
156
|
|
|
}; |
157
|
|
|
} |
158
|
|
|
else |
159
|
|
|
{ |
160
|
|
|
throw new RequestException($this->xTranslator->trans('errors.request.conversion')); |
161
|
|
|
} |
162
|
|
|
} |
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Converts a string to a bool var |
166
|
|
|
* |
167
|
|
|
* @param string $sValue The string to be converted |
|
|
|
|
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
private function convertStringToBool(string $sValue): bool |
172
|
|
|
{ |
173
|
|
|
if(strcasecmp($sValue, 'true') === 0) |
174
|
|
|
{ |
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
if(strcasecmp($sValue, 'false') === 0) |
178
|
|
|
{ |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
return (intval($sValue) !== 0); |
182
|
|
|
} |
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Convert a Jaxon request argument to its value |
186
|
|
|
* |
187
|
|
|
* Depending on its first char, the Jaxon request argument is converted to a given type. |
188
|
|
|
* |
189
|
|
|
* @param string $sValue The keys of the options in the file |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @return string|bool|integer|double|null |
192
|
|
|
*/ |
193
|
|
|
private function convertValue(string $sValue) |
194
|
|
|
{ |
195
|
|
|
$cType = substr($sValue, 0, 1); |
196
|
|
|
$sValue = substr($sValue, 1); |
197
|
|
|
switch($cType) |
198
|
|
|
{ |
199
|
|
|
case 'S': |
200
|
|
|
return $sValue; |
201
|
|
|
case 'B': |
202
|
|
|
return $this->convertStringToBool($sValue); |
203
|
|
|
case 'N': |
204
|
|
|
return ($sValue == floor($sValue) ? (int)$sValue : (float)$sValue); |
|
|
|
|
205
|
|
|
case '*': |
206
|
|
|
default: |
207
|
|
|
return null; |
208
|
|
|
} |
209
|
|
|
} |
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Decode and convert a Jaxon request argument |
213
|
|
|
* |
214
|
|
|
* @param string $sParam The Jaxon request argument |
|
|
|
|
215
|
|
|
* |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
|
|
private function decodeRequestParameter(string $sParam) |
219
|
|
|
{ |
220
|
|
|
if($sParam === '') |
221
|
|
|
{ |
222
|
|
|
return $sParam; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$sParam = call_user_func($this->cParamDecoder, $sParam); |
226
|
|
|
|
227
|
|
|
$xJson = json_decode($sParam, true); |
228
|
|
|
if($xJson !== null && $sParam != $xJson) |
229
|
|
|
{ |
230
|
|
|
return $xJson; |
231
|
|
|
} |
232
|
|
|
return $this->convertValue($sParam); |
233
|
|
|
} |
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
|
private function getRequestParameters(): array |
239
|
|
|
{ |
240
|
|
|
$aParams = []; |
241
|
|
|
$xRequest = $this->di->getRequest(); |
242
|
|
|
$aBody = $xRequest->getParsedBody(); |
|
|
|
|
243
|
|
|
if(is_array($aBody)) |
244
|
|
|
{ |
245
|
|
|
if(isset($aBody['jxnargs'])) |
246
|
|
|
{ |
247
|
|
|
$aParams = $aBody['jxnargs']; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
else |
251
|
|
|
{ |
252
|
|
|
$aParams = $xRequest->getQueryParams(); |
253
|
|
|
if(isset($aParams['jxnargs'])) |
254
|
|
|
{ |
255
|
|
|
$aParams = $aParams['jxnargs']; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
return array_map(function($sParam) { |
259
|
|
|
return $this->decodeRequestParameter((string)$sParam); |
260
|
|
|
}, $aParams); |
261
|
|
|
} |
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Decode a Jaxon request argument from UTF8 |
265
|
|
|
* |
266
|
|
|
* @param mixed $xValue The value of the argument being decoded |
|
|
|
|
267
|
|
|
* |
268
|
|
|
* @return mixed |
269
|
|
|
*/ |
270
|
|
|
private function decodeUtf8Parameter($xValue) |
271
|
|
|
{ |
272
|
|
|
if(is_string($xValue)) |
273
|
|
|
{ |
274
|
|
|
return call_user_func($this->cUtf8Decoder, $xValue); |
275
|
|
|
} |
276
|
|
|
// elseif(is_numeric($xValue) || is_bool($xValue)) |
277
|
|
|
{ |
278
|
|
|
return $xValue; |
279
|
|
|
} |
280
|
|
|
} |
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Decode an array of Jaxon request arguments from UTF8 |
284
|
|
|
* |
285
|
|
|
* @param array $aParams |
|
|
|
|
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
private function decodeUtf8Parameters(array $aParams): array |
290
|
|
|
{ |
291
|
|
|
$aValues = []; |
292
|
|
|
foreach($aParams as $sKey => $xValue) |
293
|
|
|
{ |
294
|
|
|
// Decode the key |
295
|
|
|
$sKey = call_user_func($this->cUtf8Decoder, $sKey); |
296
|
|
|
// Decode the value |
297
|
|
|
$aValues[$sKey] = is_array($xValue) ? |
|
|
|
|
298
|
|
|
$this->decodeUtf8Parameters($xValue) : $this->decodeUtf8Parameter($xValue); |
299
|
|
|
} |
300
|
|
|
return $aValues; |
301
|
|
|
} |
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Return the array of arguments from the GET or POST data |
305
|
|
|
* |
306
|
|
|
* @return array |
307
|
|
|
* @throws RequestException |
308
|
|
|
*/ |
309
|
|
|
public function args(): array |
310
|
|
|
{ |
311
|
|
|
$this->setParamDecoder(); |
312
|
|
|
$aParams = $this->getRequestParameters(); |
313
|
|
|
if(!$this->xConfigManager->getOption('core.decode_utf8')) |
314
|
|
|
{ |
315
|
|
|
return $aParams; |
316
|
|
|
} |
317
|
|
|
$this->setUtf8Decoder(); |
318
|
|
|
$this->xConfigManager->setOption('core.decode_utf8', false); |
319
|
|
|
return $this->decodeUtf8Parameters($aParams); |
320
|
|
|
} |
|
|
|
|
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Get the URI of the current request |
324
|
|
|
* |
325
|
|
|
* @throws UriException |
326
|
|
|
*/ |
|
|
|
|
327
|
|
|
public function uri(): string |
328
|
|
|
{ |
329
|
|
|
return $this->xUriDetector->detect($this->di->getRequest()->getServerParams()); |
330
|
|
|
} |
|
|
|
|
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Make the specified URL suitable for redirect |
334
|
|
|
* |
335
|
|
|
* @param string $sURL The relative or fully qualified URL |
|
|
|
|
336
|
|
|
* |
337
|
|
|
* @return string |
338
|
|
|
*/ |
339
|
|
|
public function parseUrl(string $sURL): string |
340
|
|
|
{ |
341
|
|
|
return $this->xUriDetector->redirect($sURL, $this->di->getRequest()->getServerParams()); |
342
|
|
|
} |
|
|
|
|
343
|
|
|
} |
344
|
|
|
|