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_name() === '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
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function getArguments() |
81
|
|
|
{ |
82
|
|
|
global $argv; |
|
|
|
|
83
|
|
|
|
84
|
|
|
return $argv; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function getFiles($className = '\Micro\web\Uploader') |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
if (!is_array($_FILES)) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new $className($_FILES); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
|
|
public function getStorage($name) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return $GLOBALS[$name]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function setStorage($name, array $data = []) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$GLOBALS[$name] = $data; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function query($name) |
119
|
|
|
{ |
120
|
|
|
return $this->getVar($name, '_GET'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function getVar($name, $storage) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
return array_key_exists($name, $GLOBALS[$storage]) ? $GLOBALS[$storage][$name] : null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
|
|
public function post($name) |
135
|
|
|
{ |
136
|
|
|
return $this->getVar($name, '_POST'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritdoc |
141
|
|
|
*/ |
142
|
|
|
public function cookie($name) |
143
|
|
|
{ |
144
|
|
|
return $this->getVar($name, '_COOKIE'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritdoc |
149
|
|
|
*/ |
150
|
|
|
public function session($name) |
151
|
|
|
{ |
152
|
|
|
return $this->getVar($name, '_SESSION'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get value by key from server storage |
157
|
|
|
* |
158
|
|
|
* @access public |
159
|
|
|
* |
160
|
|
|
* @param string $name Key name |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function server($name) |
165
|
|
|
{ |
166
|
|
|
return $this->getVar($name, '_SERVER'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
|
|
public function setQuery($name, $value) |
173
|
|
|
{ |
174
|
|
|
$this->setVar($name, $value, '_GET'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritdoc |
179
|
|
|
*/ |
180
|
|
|
public function setVar($name, $value, $storage) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$GLOBALS[$storage][$name] = $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @inheritdoc |
187
|
|
|
*/ |
188
|
|
|
public function setPost($name, $value) |
189
|
|
|
{ |
190
|
|
|
$this->setVar($name, $value, '_POST'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
|
|
public function setCookie($name, $value) |
197
|
|
|
{ |
198
|
|
|
$this->setVar($name, $value, '_COOKIE'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritdoc |
203
|
|
|
*/ |
204
|
|
|
public function setSession($name, $value) |
205
|
|
|
{ |
206
|
|
|
$this->setVar($name, $value, '_SESSION'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritdoc |
211
|
|
|
*/ |
212
|
|
|
public function unsetQuery($name) |
213
|
|
|
{ |
214
|
|
|
$this->unsetVar($name, '_GET'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritdoc |
219
|
|
|
*/ |
220
|
|
|
public function unsetVar($name, $storage) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
unset($GLOBALS[$storage][$name]); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @inheritdoc |
227
|
|
|
*/ |
228
|
|
|
public function unsetPost($name) |
229
|
|
|
{ |
230
|
|
|
$this->unsetVar($name, '_POST'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @inheritdoc |
235
|
|
|
*/ |
236
|
|
|
public function unsetSession($name) |
237
|
|
|
{ |
238
|
|
|
$this->unsetVar($name, '_SESSION'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @inheritdoc |
243
|
|
|
*/ |
244
|
|
|
public function getRequestPayload() |
245
|
|
|
{ |
246
|
|
|
return file_get_contents('php://input'); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
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: