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/lugnsk/micro |
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 $data Data from request */ |
22
|
|
|
protected $data; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor Request |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
* |
30
|
|
|
* @result void |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->cli = PHP_SAPI === 'cli'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function isCli() |
41
|
|
|
{ |
42
|
|
|
return $this->cli; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function isAjax() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return !empty($_SERVER['HTTP_X_REQUEST_WITH']) && $_SERVER['HTTP_X_REQUEST_WITH'] === 'XMLHttpRequest'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function getMethod() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return $_SERVER['REQUEST_METHOD']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function getUserIP() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
return !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public function getBrowser($agent = null) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return get_browser($agent ?: $_SERVER['HTTP_USER_AGENT'], true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get arguments from command line |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* |
82
|
|
|
* @param string $char -a .. -z option char |
83
|
|
|
* @param string $name --optionName_string |
84
|
|
|
* @param bool|null $required Required value? |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function getOption($char = '', $name = '', $required = null) |
89
|
|
|
{ |
90
|
|
|
if (!$char && !$name) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($char && (1 < strlen($char) || 1 !== preg_match('/^\w$/', $char))) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($name && (1 !== preg_match('/^\w+$/', $name))) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
switch ($required) { |
103
|
|
|
case true: |
104
|
|
|
$char = $char ? $char . ':' : $char; |
105
|
|
|
$name = $name ? $name . ':' : $name; |
106
|
|
|
break; |
107
|
|
|
case false: |
108
|
|
|
$char = $char ? $char . '::' : $char; |
109
|
|
|
$name = $name ? $name . '::' : $name; |
110
|
|
|
break; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$argv = ($opts = getopt($char, [$name])) ? array_shift($opts) : []; |
114
|
|
|
|
115
|
|
|
return is_array($argv) ? array_shift($argv) : $argv; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function getFiles($className = '\Micro\Web\Uploader') |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
if (!is_array($_FILES)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return new $className($_FILES); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
|
|
public function getStorage($name) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return $GLOBALS[$name]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
|
|
public function setStorage($name, array $data = []) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$GLOBALS[$name] = $data; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
|
|
public function query($name) |
150
|
|
|
{ |
151
|
|
|
return $this->getVar($name, '_GET'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
public function getVar($name, $storage) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
return array_key_exists($name, $GLOBALS[$storage]) ? $GLOBALS[$storage][$name] : null; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function post($name) |
166
|
|
|
{ |
167
|
|
|
return $this->getVar($name, '_POST'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
|
public function cookie($name) |
174
|
|
|
{ |
175
|
|
|
return $this->getVar($name, '_COOKIE'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritdoc |
180
|
|
|
*/ |
181
|
|
|
public function session($name) |
182
|
|
|
{ |
183
|
|
|
return $this->getVar($name, '_SESSION'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get value by key from server storage |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* |
191
|
|
|
* @param string $name Key name |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function server($name) |
196
|
|
|
{ |
197
|
|
|
return $this->getVar($name, '_SERVER'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
|
|
public function setQuery($name, $value) |
204
|
|
|
{ |
205
|
|
|
$this->setVar($name, $value, '_GET'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritdoc |
210
|
|
|
*/ |
211
|
|
|
public function setVar($name, $value, $storage) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$GLOBALS[$storage][$name] = $value; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
|
|
public function setPost($name, $value) |
220
|
|
|
{ |
221
|
|
|
$this->setVar($name, $value, '_POST'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritdoc |
226
|
|
|
*/ |
227
|
|
|
public function setCookie($name, $value) |
228
|
|
|
{ |
229
|
|
|
$this->setVar($name, $value, '_COOKIE'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @inheritdoc |
234
|
|
|
*/ |
235
|
|
|
public function setSession($name, $value) |
236
|
|
|
{ |
237
|
|
|
$this->setVar($name, $value, '_SESSION'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @inheritdoc |
242
|
|
|
*/ |
243
|
|
|
public function unsetQuery($name) |
244
|
|
|
{ |
245
|
|
|
$this->unsetVar($name, '_GET'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @inheritdoc |
250
|
|
|
*/ |
251
|
|
|
public function unsetVar($name, $storage) |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
unset($GLOBALS[$storage][$name]); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @inheritdoc |
258
|
|
|
*/ |
259
|
|
|
public function unsetPost($name) |
260
|
|
|
{ |
261
|
|
|
$this->unsetVar($name, '_POST'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @inheritdoc |
266
|
|
|
*/ |
267
|
|
|
public function unsetSession($name) |
268
|
|
|
{ |
269
|
|
|
$this->unsetVar($name, '_SESSION'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @inheritdoc |
274
|
|
|
*/ |
275
|
|
|
public function getRequestPayload() |
276
|
|
|
{ |
277
|
|
|
return file_get_contents('php://input'); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
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: