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