|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ArgumentManager.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\Utils\Config\Config; |
|
26
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
27
|
|
|
use Jaxon\Exception\RequestException; |
|
28
|
|
|
|
|
29
|
|
|
use function strcasecmp; |
|
30
|
|
|
use function is_numeric; |
|
31
|
|
|
use function is_string; |
|
32
|
|
|
use function is_array; |
|
33
|
|
|
use function is_bool; |
|
34
|
|
|
use function substr; |
|
35
|
|
|
use function strlen; |
|
36
|
|
|
use function floor; |
|
37
|
|
|
use function json_decode; |
|
38
|
|
|
use function call_user_func; |
|
39
|
|
|
use function array_walk; |
|
40
|
|
|
use function function_exists; |
|
41
|
|
|
use function iconv; |
|
42
|
|
|
use function mb_convert_encoding; |
|
43
|
|
|
use function utf8_decode; |
|
44
|
|
|
|
|
45
|
|
|
class ArgumentManager |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
/* |
|
48
|
|
|
* Request methods |
|
49
|
|
|
*/ |
|
|
|
|
|
|
50
|
|
|
const METHOD_UNKNOWN = 0; |
|
51
|
|
|
const METHOD_GET = 1; |
|
|
|
|
|
|
52
|
|
|
const METHOD_POST = 2; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Config |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $xConfig; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Translator |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $xTranslator; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* An array of arguments received via the GET or POST parameter jxnargs. |
|
66
|
|
|
* |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
private $aArgs; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Stores the method that was used to send the arguments from the client. |
|
73
|
|
|
* Will be one of: self::METHOD_UNKNOWN, self::METHOD_GET, self::METHOD_POST. |
|
74
|
|
|
* |
|
75
|
|
|
* @var integer |
|
76
|
|
|
*/ |
|
77
|
|
|
private $nMethod; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The function which decodes utf8 string. |
|
81
|
|
|
* |
|
82
|
|
|
* @var callable |
|
83
|
|
|
*/ |
|
84
|
|
|
private $cUtf8Decoder; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* The constructor |
|
88
|
|
|
* |
|
89
|
|
|
* Get and decode the arguments of the HTTP request |
|
90
|
|
|
* |
|
91
|
|
|
* @param Config $xConfig |
|
|
|
|
|
|
92
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct(Config $xConfig, Translator $xTranslator) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$this->xConfig = $xConfig; |
|
|
|
|
|
|
97
|
|
|
$this->xTranslator = $xTranslator; |
|
98
|
|
|
$this->aArgs = []; |
|
|
|
|
|
|
99
|
|
|
$this->nMethod = self::METHOD_UNKNOWN; |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
if(isset($_POST['jxnargs'])) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->nMethod = self::METHOD_POST; |
|
104
|
|
|
$this->aArgs = $_POST['jxnargs']; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
elseif(isset($_GET['jxnargs'])) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->nMethod = self::METHOD_GET; |
|
109
|
|
|
$this->aArgs = $_GET['jxnargs']; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return the method that was used to send the arguments from the client |
|
115
|
|
|
* |
|
116
|
|
|
* The method is one of: self::METHOD_UNKNOWN, self::METHOD_GET, self::METHOD_POST. |
|
117
|
|
|
* |
|
118
|
|
|
* @return int |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getRequestMethod(): int |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->nMethod; |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Return true if the current request method is GET |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function requestMethodIsGet(): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return ($this->getRequestMethod() === self::METHOD_GET); |
|
133
|
|
|
} |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Converts a string to a bool var |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $sValue The string to be converted |
|
|
|
|
|
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
private function __convertStringToBool(string $sValue): bool |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
if(strcasecmp($sValue, 'true') === 0) |
|
145
|
|
|
{ |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
if(strcasecmp($sValue, 'false') === 0) |
|
149
|
|
|
{ |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
if(is_numeric($sValue)) |
|
153
|
|
|
{ |
|
154
|
|
|
return ($sValue !== '0'); |
|
155
|
|
|
} |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Convert a Jaxon request argument to its value |
|
161
|
|
|
* |
|
162
|
|
|
* Depending on its first char, the Jaxon request argument is converted to a given type. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $sValue The keys of the options in the file |
|
|
|
|
|
|
165
|
|
|
* |
|
166
|
|
|
* @return string|bool|integer|double|null |
|
167
|
|
|
*/ |
|
168
|
|
|
private function __convertValue(string $sValue) |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
|
|
$cType = substr($sValue, 0, 1); |
|
171
|
|
|
$sValue = substr($sValue, 1); |
|
172
|
|
|
switch($cType) |
|
173
|
|
|
{ |
|
174
|
|
|
case 'S': |
|
175
|
|
|
$value = !$sValue ? '' : $sValue; |
|
176
|
|
|
break; |
|
177
|
|
|
case 'B': |
|
178
|
|
|
$value = $this->__convertStringToBool($sValue); |
|
179
|
|
|
break; |
|
180
|
|
|
case 'N': |
|
181
|
|
|
$value = ($sValue == floor($sValue) ? (int)$sValue : (float)$sValue); |
|
|
|
|
|
|
182
|
|
|
break; |
|
183
|
|
|
case '*': |
|
184
|
|
|
default: |
|
185
|
|
|
$value = null; |
|
186
|
|
|
break; |
|
187
|
|
|
} |
|
188
|
|
|
return $value; |
|
189
|
|
|
} |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Decode and convert an Jaxon request argument from JSON |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $sArg The Jaxon request argument |
|
|
|
|
|
|
195
|
|
|
* |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
|
|
private function __argumentDecode(string &$sArg) |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
|
|
if($sArg === '') |
|
201
|
|
|
{ |
|
202
|
|
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// Arguments are url encoded when uploading files |
|
206
|
|
|
$sType = 'multipart/form-data'; |
|
|
|
|
|
|
207
|
|
|
$nLen = strlen($sType); |
|
|
|
|
|
|
208
|
|
|
$sContentType = ''; |
|
209
|
|
|
if(isset($_SERVER['CONTENT_TYPE'])) |
|
210
|
|
|
{ |
|
211
|
|
|
$sContentType = substr($_SERVER['CONTENT_TYPE'], 0, $nLen); |
|
212
|
|
|
} |
|
213
|
|
|
elseif(isset($_SERVER['HTTP_CONTENT_TYPE'])) |
|
214
|
|
|
{ |
|
215
|
|
|
$sContentType = substr($_SERVER['HTTP_CONTENT_TYPE'], 0, $nLen); |
|
216
|
|
|
} |
|
217
|
|
|
if($sContentType == $sType) |
|
218
|
|
|
{ |
|
219
|
|
|
$sArg = urldecode($sArg); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
$data = json_decode($sArg, true); |
|
223
|
|
|
|
|
224
|
|
|
if($data !== null && $sArg != $data) |
|
225
|
|
|
{ |
|
226
|
|
|
$sArg = $data; |
|
227
|
|
|
} |
|
228
|
|
|
else |
|
229
|
|
|
{ |
|
230
|
|
|
$sArg = $this->__convertValue($sArg); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Decode an Jaxon request argument from UTF8 |
|
236
|
|
|
* |
|
237
|
|
|
* @param array $aDst An array to store the decoded arguments |
|
|
|
|
|
|
238
|
|
|
* @param string $sKey The key of the argument being decoded |
|
|
|
|
|
|
239
|
|
|
* @param string|array $mValue The value of the argument being decoded |
|
|
|
|
|
|
240
|
|
|
* |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
|
|
private function _decode_utf8_argument(array &$aDst, string $sKey, $mValue) |
|
|
|
|
|
|
244
|
|
|
{ |
|
245
|
|
|
// Decode the key |
|
246
|
|
|
$sDestKey = call_user_func($this->cUtf8Decoder, $sKey); |
|
247
|
|
|
|
|
248
|
|
|
if(is_array($mValue)) |
|
249
|
|
|
{ |
|
250
|
|
|
$aDst[$sDestKey] = []; |
|
251
|
|
|
foreach($mValue as $_sKey => &$_mValue) |
|
252
|
|
|
{ |
|
253
|
|
|
$this->_decode_utf8_argument($aDst[$sDestKey], $_sKey, $_mValue); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
elseif(is_numeric($mValue) || is_bool($mValue)) |
|
257
|
|
|
{ |
|
258
|
|
|
$aDst[$sDestKey] = $mValue; |
|
259
|
|
|
} |
|
260
|
|
|
elseif(is_string($mValue)) |
|
|
|
|
|
|
261
|
|
|
{ |
|
262
|
|
|
$aDst[$sDestKey] = call_user_func($this->cUtf8Decoder, $mValue); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Return the array of arguments that were extracted and parsed from the GET or POST data |
|
268
|
|
|
* |
|
269
|
|
|
* @return array |
|
270
|
|
|
* @throws RequestException |
|
271
|
|
|
*/ |
|
272
|
|
|
public function process(): array |
|
273
|
|
|
{ |
|
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
array_walk($this->aArgs, [$this, '__argumentDecode']); |
|
276
|
|
|
|
|
277
|
|
|
if(!$this->xConfig->getOption('core.decode_utf8')) |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->aArgs; |
|
280
|
|
|
} |
|
281
|
|
|
// By default, no decoding |
|
282
|
|
|
$this->cUtf8Decoder = function($sStr) { |
|
283
|
|
|
return $sStr; |
|
284
|
|
|
}; |
|
285
|
|
|
$sEncoding = $this->xConfig->getOption('core.encoding', ''); |
|
|
|
|
|
|
286
|
|
|
if(function_exists('iconv')) |
|
287
|
|
|
{ |
|
288
|
|
|
$this->cUtf8Decoder = function($sStr) use($sEncoding) { |
|
289
|
|
|
return iconv("UTF-8", $sEncoding . '//TRANSLIT', $sStr); |
|
290
|
|
|
}; |
|
291
|
|
|
} |
|
292
|
|
|
elseif(function_exists('mb_convert_encoding')) |
|
293
|
|
|
{ |
|
294
|
|
|
$this->cUtf8Decoder = function($sStr) use($sEncoding) { |
|
295
|
|
|
return mb_convert_encoding($sStr, $sEncoding, "UTF-8"); |
|
296
|
|
|
}; |
|
297
|
|
|
} |
|
298
|
|
|
elseif($sEncoding == "ISO-8859-1") |
|
299
|
|
|
{ |
|
300
|
|
|
$this->cUtf8Decoder = function($sStr) { |
|
301
|
|
|
return utf8_decode($sStr); |
|
302
|
|
|
}; |
|
303
|
|
|
} |
|
304
|
|
|
else |
|
305
|
|
|
{ |
|
306
|
|
|
throw new RequestException($this->xTranslator->trans('errors.request.conversion')); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
$aDst = []; |
|
310
|
|
|
foreach($this->aArgs as $sKey => &$mValue) |
|
311
|
|
|
{ |
|
312
|
|
|
$this->_decode_utf8_argument($aDst, $sKey, $mValue); |
|
313
|
|
|
}; |
|
314
|
|
|
$this->aArgs = $aDst; |
|
315
|
|
|
|
|
316
|
|
|
$this->xConfig->setOption('core.decode_utf8', false); |
|
317
|
|
|
|
|
318
|
|
|
return $this->aArgs; |
|
319
|
|
|
} |
|
|
|
|
|
|
320
|
|
|
} |
|
321
|
|
|
|