1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asymptix\web; |
4
|
|
|
|
5
|
|
|
use Asymptix\core\Tools; |
6
|
|
|
use Asymptix\helpers\Naming; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Request functionality. |
10
|
|
|
* |
11
|
|
|
* @category Asymptix PHP Framework |
12
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
13
|
|
|
* @copyright (c) 2009 - 2017, Dmytro Zarezenko |
14
|
|
|
* |
15
|
|
|
* @git https://github.com/Asymptix/Framework |
16
|
|
|
* @license http://opensource.org/licenses/MIT |
17
|
|
|
*/ |
18
|
|
|
class Request { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Check if form was submitted. |
22
|
|
|
* |
23
|
|
|
* @param string $submitFieldName Name of the submit field (button). |
24
|
|
|
* Default: "submitBtn" |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
public static function isFormSubmitted($submitFieldName = "submitBtn") { |
29
|
|
|
return (self::getFieldValue($submitFieldName) !== null); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Verify if field is exists in the request. |
34
|
|
|
* |
35
|
|
|
* @param mixed $fieldName String name of the field or complex name as array. |
36
|
|
|
* @param string $source Http::GET or Http::POST constant. |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public static function issetField($fieldName, $source = null) { |
41
|
|
|
return !is_null(self::getFieldValue($fieldName, $source)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets value of the field from $_REQUEST or $_SESSION (is some REQUEST values |
46
|
|
|
* needs to be stored by scenario). Also it takes values from $_GET or $_POST |
47
|
|
|
* separately if second parameter is passed. |
48
|
|
|
* |
49
|
|
|
* @param mixed $fieldName String name of the field or complex hierarchy name. |
50
|
|
|
* @param string $source Http::GET or Http::POST constant. |
51
|
|
|
* |
52
|
|
|
* @return mixed Value of the field, NULL otherwise. |
53
|
|
|
*/ |
54
|
|
|
public static function getFieldValue($fieldName, $source = null) { |
|
|
|
|
55
|
|
|
$value = null; |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
switch ($source) { |
59
|
|
|
case (Http::GET): |
60
|
|
|
$value = Naming::getValueByComplexName($_GET, $fieldName); |
61
|
|
|
break; |
62
|
|
|
case (Http::POST): |
63
|
|
|
$value = Naming::getValueByComplexName($_POST, $fieldName); |
64
|
|
|
break; |
65
|
|
|
default: |
66
|
|
|
$value = Naming::getValueByComplexName($_REQUEST, $fieldName); |
67
|
|
|
} |
68
|
|
|
} catch (\Exception $ex) { |
69
|
|
|
try { |
70
|
|
|
if (Session::exists('_post')) { |
71
|
|
|
$value = Naming::getValueByComplexName( |
72
|
|
|
Session::get('_post'), $fieldName |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} catch (\Exception $ex) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!is_null($value)) { |
81
|
|
|
if (is_array($value)) { |
82
|
|
|
return $value; |
83
|
|
|
} elseif (is_int($value)) { |
84
|
|
|
return intval($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns value of the HTTP GET requet field. |
95
|
|
|
* |
96
|
|
|
* @param mixed $fieldName String name of the field or complex name as array. |
97
|
|
|
* @param mixed $defaultValue Default value. |
98
|
|
|
* |
99
|
|
|
* @return mixed Value of the field, NULL otherwise. |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public static function _get($fieldName, $defaultValue = null) { |
|
|
|
|
102
|
|
|
$fieldValue = self::getFieldValue($fieldName, Http::GET); |
103
|
|
|
if (is_null($fieldValue) && !is_null($defaultValue)) { |
104
|
|
|
return $defaultValue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $fieldValue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns value of the HTTP POST requet field. |
112
|
|
|
* |
113
|
|
|
* @param mixed $fieldName String name of the field or complex name as array. |
114
|
|
|
* @param mixed $defaultValue Default value. |
115
|
|
|
* |
116
|
|
|
* @return mixed Value of the field, NULL otherwise. |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public static function _post($fieldName, $defaultValue = null) { |
|
|
|
|
119
|
|
|
$fieldValue = self::getFieldValue($fieldName, Http::POST); |
120
|
|
|
if (is_null($fieldValue) && !is_null($defaultValue)) { |
121
|
|
|
return $defaultValue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $fieldValue; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns value of the HTTP POST or GET requet field. |
129
|
|
|
* |
130
|
|
|
* @param mixed $fieldName String name of the field or complex name as array. |
131
|
|
|
* @param mixed $defaultValue Default value. |
132
|
|
|
* |
133
|
|
|
* @return mixed Value of the field, NULL otherwise. |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public static function _field($fieldName, $defaultValue = null) { |
|
|
|
|
136
|
|
|
$fieldValue = self::getFieldValue($fieldName); |
137
|
|
|
if (is_null($fieldValue) && !is_null($defaultValue)) { |
138
|
|
|
return $defaultValue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $fieldValue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns value of the filter field. |
146
|
|
|
* |
147
|
|
|
* @param string $filterName Name of the filter field. |
148
|
|
|
* @param mixed $defaultValue Default value. |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public static function _filter($filterName, $defaultValue) { |
153
|
|
|
return Tools::getFilterValue($filterName, $defaultValue); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Sets value of the field or creates new field by pair $fieldName => $fieldValue. |
158
|
|
|
* |
159
|
|
|
* @global array<mixed> $_FIELDS Global fields list. |
160
|
|
|
* @param mixed $fieldName Name of the field as a string or complex name as |
161
|
|
|
* an array. |
162
|
|
|
* @param mixed $fieldValue Value of the field. |
163
|
|
|
* |
164
|
|
|
* @throws \Exception |
165
|
|
|
*/ |
166
|
|
|
public static function setFieldValue($fieldName, $fieldValue) { |
167
|
|
|
global $_FIELDS; |
|
|
|
|
168
|
|
|
|
169
|
|
|
Naming::setValueWithComplexName($_FIELDS, $fieldName, $fieldValue); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Remembers field in session. |
174
|
|
|
* |
175
|
|
|
* @param string $fieldName Name of the field. |
176
|
|
|
* @param mixed $fieldValue |
177
|
|
|
*/ |
178
|
|
|
public static function rememberField($fieldName, $fieldValue) { |
179
|
|
|
Session::set("_post[{$fieldName}]", serialize($fieldValue)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Forget cross session field. |
184
|
|
|
* |
185
|
|
|
* @param string $fieldName Field name. |
186
|
|
|
*/ |
187
|
|
|
public static function forgetField($fieldName) { |
|
|
|
|
188
|
|
|
Naming::unsetValueWithComplexName($_SESSION, "_post[{$fieldName}]"); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Forget all cross session fields. |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public static function forgetFields() { |
197
|
|
|
return Session::remove('_post'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Clean all request parameters from provided source. |
202
|
|
|
* |
203
|
|
|
* @param string $source Http::GET or Http::POST constant. |
204
|
|
|
*/ |
205
|
|
|
public static function clean($source = null) { |
|
|
|
|
206
|
|
|
switch ($source) { |
207
|
|
|
case (Http::GET): |
208
|
|
|
$_GET = []; |
209
|
|
|
|
210
|
|
|
break; |
211
|
|
|
case (Http::POST): |
212
|
|
|
$_POST = []; |
213
|
|
|
|
214
|
|
|
break; |
215
|
|
|
default: |
216
|
|
|
$_REQUEST = $_GET = $_POST = []; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Change value of the existing field. |
222
|
|
|
* |
223
|
|
|
* @global array<mixed> $_FIELDS Global fields list. |
224
|
|
|
* @param string $fieldName Name of the field. |
225
|
|
|
* @param mixed $fieldValue Value of the field. |
226
|
|
|
*/ |
227
|
|
|
public static function changeFieldValue($fieldName, $fieldValue) { |
228
|
|
|
global $_FIELDS; |
|
|
|
|
229
|
|
|
|
230
|
|
|
if (isset($_FIELDS[$fieldName])) { |
231
|
|
|
$_FIELDS[$fieldName] = $fieldValue; |
232
|
|
|
} else { |
233
|
|
|
throw new \Exception("No field '" . $fieldName . "' in global fields list."); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Casts value if the existing field to specified type. |
239
|
|
|
* |
240
|
|
|
* @global array<mixed> $_FIELDS Global fields list. |
241
|
|
|
* @param string $fieldName Name of the field. |
242
|
|
|
* @param string $type New field value type. |
243
|
|
|
*/ |
244
|
|
|
public static function castFieldValue($fieldName, $type) { |
245
|
|
|
global $_FIELDS; |
|
|
|
|
246
|
|
|
|
247
|
|
|
if (isset($_FIELDS[$fieldName])) { |
248
|
|
|
switch ($type) { |
249
|
|
|
case ('integer'): |
250
|
|
|
case ('int'): |
251
|
|
|
case ('i'): |
252
|
|
|
$_FIELDS[$fieldName] = intval($_FIELDS[$fieldName]); |
253
|
|
|
break; |
254
|
|
|
case ('real'): |
255
|
|
|
case ('float'): |
256
|
|
|
case ('double'): |
257
|
|
|
case ('d'): |
258
|
|
|
$_FIELDS[$fieldName] = floatval($_FIELDS[$fieldName]); |
259
|
|
|
break; |
260
|
|
|
case ('string'): |
261
|
|
|
case ('str'): |
262
|
|
|
case ('s'): |
263
|
|
|
$_FIELDS[$fieldName] = strval($_FIELDS[$fieldName]); |
264
|
|
|
break; |
265
|
|
|
case ('boolean'): |
266
|
|
|
case ('bool'): |
267
|
|
|
case ('b'): |
268
|
|
|
$_FIELDS[$fieldName] = (bool)$_FIELDS[$fieldName]; |
269
|
|
|
} |
270
|
|
|
} else { |
271
|
|
|
throw new \Exception("No field '" . $fieldName . "' in global fields list."); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Normilize all boolean checkboxes even they are not checked. |
277
|
|
|
* |
278
|
|
|
* @global array $_FIELDS Submitted form fields. |
279
|
|
|
* @param array<string> $fieldNames Names of all boolean checkboxes what need |
280
|
|
|
* fixes. |
281
|
|
|
*/ |
282
|
|
|
public static function normalizeCheckboxes($fieldNames) { |
283
|
|
|
global $_FIELDS; |
|
|
|
|
284
|
|
|
|
285
|
|
|
foreach ($fieldNames as $fieldName) { |
286
|
|
|
$_FIELDS[$fieldName] = (int)(bool)self::getFieldValue($fieldName); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Removes fields from global fields list. |
292
|
|
|
* |
293
|
|
|
* @param array<string> $fieldNames Names of all boolean checkboxes what need |
294
|
|
|
* fixes (may be list of complex field names). |
295
|
|
|
*/ |
296
|
|
|
public static function removeFields($fieldNames) { |
297
|
|
|
global $_FIELDS; |
|
|
|
|
298
|
|
|
|
299
|
|
|
foreach ($fieldNames as $fieldName) { |
300
|
|
|
Naming::unsetValueWithComplexName($_FIELDS, $fieldName); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: