|
1
|
|
|
<?php /** MicroRequest */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Web; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Request class file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
9
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
10
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
|
11
|
|
|
* @license /LICENSE |
|
12
|
|
|
* @package Micro |
|
13
|
|
|
* @subpackage Web |
|
14
|
|
|
* @version 1.0 |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Request implements IRequest |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var bool $cli Is running as CLI */ |
|
20
|
|
|
protected $cli; |
|
21
|
|
|
/** @var array $files $_FILES from request */ |
|
22
|
|
|
protected $files; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor Request |
|
27
|
|
|
* |
|
28
|
|
|
* @access public |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $files |
|
31
|
|
|
* |
|
32
|
|
|
* @result void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $files = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->cli = PHP_SAPI === 'cli'; |
|
37
|
|
|
|
|
38
|
|
|
$this->files = $files; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get flag of running as CLI |
|
43
|
|
|
* |
|
44
|
|
|
* @access public |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
public function isCli() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->cli; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check request is AJAX ? |
|
55
|
|
|
* |
|
56
|
|
|
* @access public |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function isAjax() |
|
61
|
|
|
{ |
|
62
|
|
|
return strtolower(filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get request method |
|
67
|
|
|
* |
|
68
|
|
|
* @access public |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getMethod() |
|
73
|
|
|
{ |
|
74
|
|
|
return filter_input(INPUT_SERVER, 'REQUEST_METHOD'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get user IP-address |
|
79
|
|
|
* |
|
80
|
|
|
* @access public |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getUserIP() |
|
85
|
|
|
{ |
|
86
|
|
|
return ($ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR')) ? $ip : '127.0.0.1'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get browser data from user user agent string |
|
91
|
|
|
* |
|
92
|
|
|
* @access public |
|
93
|
|
|
* |
|
94
|
|
|
* @param null|string $agent User agent string |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getBrowser($agent = null) |
|
99
|
|
|
{ |
|
100
|
|
|
return get_browser($agent ?: filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), true); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get arguments from command line |
|
105
|
|
|
* |
|
106
|
|
|
* @access public |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $char -a .. -z option char |
|
109
|
|
|
* @param string $name --optionName_string |
|
110
|
|
|
* @param bool|null $required Required value? |
|
111
|
|
|
* |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getOption($char = '', $name = '', $required = null) |
|
115
|
|
|
{ |
|
116
|
|
|
if (!$char && !$name) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($char && (1 < strlen($char) || 1 !== preg_match('/^\w$/', $char))) { |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($name && (1 !== preg_match('/^\w+$/', $name))) { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
switch ($required) { |
|
129
|
|
|
case true: |
|
130
|
|
|
$char = $char ? $char.':' : $char; |
|
131
|
|
|
$name = $name ? $name.':' : $name; |
|
132
|
|
|
break; |
|
133
|
|
|
case false: |
|
134
|
|
|
$char = $char ? $char.'::' : $char; |
|
135
|
|
|
$name = $name ? $name.'::' : $name; |
|
136
|
|
|
break; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$argv = ($opts = getopt($char, [$name])) ? array_shift($opts) : []; |
|
140
|
|
|
|
|
141
|
|
|
return is_array($argv) ? array_shift($argv) : $argv; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get files mapper |
|
146
|
|
|
* |
|
147
|
|
|
* @access public |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $className Class name of mapper |
|
150
|
|
|
* |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getFiles($className = '\Micro\Web\Uploader') |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->files ? new $className($this->files) : false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get value by key from query storage |
|
160
|
|
|
* |
|
161
|
|
|
* @access public |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $name Key name |
|
164
|
|
|
* @param integer $filter |
|
165
|
|
|
* @param mixed $options |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
public function query($name, $filter = FILTER_DEFAULT, $options = null) |
|
170
|
|
|
{ |
|
171
|
|
|
return filter_input(INPUT_GET, $name, $filter, $options); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get value by key from post storage |
|
176
|
|
|
* |
|
177
|
|
|
* @access public |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $name Key name |
|
180
|
|
|
* @param integer $filter |
|
181
|
|
|
* @param mixed $options |
|
182
|
|
|
* |
|
183
|
|
|
* @return mixed |
|
184
|
|
|
*/ |
|
185
|
|
|
public function post($name, $filter = FILTER_DEFAULT, $options = null) |
|
186
|
|
|
{ |
|
187
|
|
|
return filter_input(INPUT_POST, $name, $filter, $options); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get value by key from cookie storage |
|
192
|
|
|
* |
|
193
|
|
|
* @access public |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $name Key name |
|
196
|
|
|
* @param integer $filter |
|
197
|
|
|
* @param mixed $options |
|
198
|
|
|
* |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
|
|
public function cookie($name, $filter = FILTER_DEFAULT, $options = null) |
|
202
|
|
|
{ |
|
203
|
|
|
return filter_input(INPUT_COOKIE, $name, $filter, $options); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get value by key from session storage |
|
208
|
|
|
* |
|
209
|
|
|
* @access public |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $name Key name |
|
212
|
|
|
* @param integer $filter |
|
213
|
|
|
* @param mixed $options |
|
214
|
|
|
* |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
|
|
public function session($name, $filter = FILTER_DEFAULT, $options = null) |
|
218
|
|
|
{ |
|
219
|
|
|
return filter_input(INPUT_SESSION, $name, $filter, $options); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get value by key from server storage |
|
224
|
|
|
* |
|
225
|
|
|
* @access public |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $name Key name |
|
228
|
|
|
* @param integer $filter |
|
229
|
|
|
* @param mixed $options |
|
230
|
|
|
* |
|
231
|
|
|
* @return bool |
|
232
|
|
|
*/ |
|
233
|
|
|
public function server($name, $filter = FILTER_DEFAULT, $options = null) |
|
234
|
|
|
{ |
|
235
|
|
|
return filter_input(INPUT_SERVER, $name, $filter, $options); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Get RequestPayload (RAW DATA) |
|
240
|
|
|
* |
|
241
|
|
|
* @return string|bool |
|
242
|
|
|
*/ |
|
243
|
|
|
public function requestPayload() |
|
244
|
|
|
{ |
|
245
|
|
|
return file_get_contents('php://input'); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Set value into session storage |
|
250
|
|
|
* |
|
251
|
|
|
* @access public |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $name Key name |
|
254
|
|
|
* @param string $value Key value |
|
255
|
|
|
* |
|
256
|
|
|
* @return void |
|
257
|
|
|
*/ |
|
258
|
|
|
public function setSession($name, $value) |
|
|
|
|
|
|
259
|
|
|
{ |
|
260
|
|
|
$_SESSION[$name] = $value; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
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: