-
Notifications
You must be signed in to change notification settings - Fork 239
/
Request.php
376 lines (328 loc) · 9.68 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2009, Union of Rad, Inc. (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\action;
use \lithium\util\Validator;
/**
* Request is responsible for identifying and storing all the information about the http request.
*
* @see lithium\action\Dispatcher
*/
class Request extends \lithium\core\Object {
public $url = null;
public $params = array();
public $data = array();
public $query = array();
/**
* Holds the environment variables for the request. Retrieved with env().
*
* @var array
* @see lithium\http\Request::env()
*/
protected $_env = array();
protected $_type = 'html';
protected $_base = null;
protected $_classes = array('media' => '\lithium\http\Media');
protected $_detectors = array(
'mobile' => array('HTTP_USER_AGENT', null),
'ajax' => array('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'),
'flash' => array('HTTP_USER_AGENT', 'Shockwave Flash'),
'ssl' => 'HTTPS'
);
/**
* Content-types accepted by the client. If extension parsing is enabled in the
* Router, and an extension is detected, the corresponding content-type will be
* used as the overriding primary content-type accepted.
*
* @var array
*/
protected $_acceptTypes = array();
protected $_autoConfig = array('classes' => 'merge', 'detectors' => 'merge', 'base', 'type');
/**
* Pulls request data from superglobals.
*
* @return void
* @todo Replace $_FILES loops with Felix's code (or Marc's?)
* @todo Consider disabling magic quotes stripping, or only having it explicitly enabled, since
* it's deprecated now.
*/
protected function _init() {
parent::_init();
$this->_base = $this->_base ?: $this->_base();
$m = '/(iPhone|MIDP|AvantGo|BlackBerry|J2ME|Opera Mini|DoCoMo|NetFront|Nokia|PalmOS|';
$m .= 'PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|';
$m .= 'Windows CE|Xiino)/i';
$this->_detectors['mobile'][1] ?: $m;
$this->url = isset($_GET['url']) ? rtrim($_GET['url'], '/') : '';
$this->url = $this->url ?: '/';
$this->_env = (array)$_SERVER + (array)$_ENV;
$envs = array('isapi' => 'IIS', 'cgi' => 'cgi');
$env = php_sapi_name();
$this->_env['PLATFORM'] = array_key_exists($env, $envs) ? $envs[$env] : null;
if (!empty($_POST)) {
$this->data = $_POST;
if (!empty($this->_env['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$this->data['_method'] = $this->_env['HTTP_X_HTTP_METHOD_OVERRIDE'];
}
if (isset($this->data['_method'])) {
if (isset($_SERVER) && !empty($_SERVER)) {
$_SERVER['REQUEST_METHOD'] = $params['data']['_method'];
} else {
$_ENV['REQUEST_METHOD'] = $params['data']['_method'];
}
unset($this->params['form']['_method']);
}
}
if (!empty($_FILES)) {
foreach ($_FILES as $name => $data) {
if ($name != 'data') {
$params['form'][$name] = $data;
}
}
// Replace this:
if (isset($_FILES['data'])) {
foreach ($_FILES['data'] as $key => $data) {
foreach ($data as $model => $fields) {
foreach ($fields as $field => $value) {
if (is_array($value)) {
$params['data'][$model][$field][key($value)][$key] = current($value);
} else {
$params['data'][$model][$field][$key] = $value;
}
}
}
}
}
}
}
/**
* Allows request parameters to be accessed as object properties, i.e. `$this->request->action`
* instead of `$this->request->params['action']`.
*
* @param string $name The property name/parameter key to return.
* @return mixed Returns the value of `$params[$name]` if it is set, otherwise returns null.
* @see lithium\action\Request::$params
*/
public function __get($name) {
if (isset($this->params[$name])) {
return $this->params[$name];
}
}
/**
* Queries PHP's environment settings, and provides an abstraction for standardizing expected
* environment values across varying platforms, as well as specify custom environment flags.
*
* @param string $key
* @return void
* @todo Refactor to lazy-load environment settings
*/
public function env($key) {
if ($key == 'base') {
return $this->_base;
}
if ($key == 'HTTPS') {
if (array_key_exists($this->_env['HTTPS'])) {
return (isset($this->_env['HTTPS']) && $this->_env['HTTPS'] == 'on');
}
return (strpos($this->_env['SCRIPT_URI'], 'https://') === 0);
}
if ($key == 'SCRIPT_NAME') {
if ($this->_env['PLATFORM'] == 'CGI' || isset($this->_env['SCRIPT_URL'])) {
$key = 'SCRIPT_URL';
}
}
$val = null;
if (!array_key_exists($key, $this->_env)) {
$val = getenv($key);
} else {
$val = $this->_env[$key];
}
if ($key == 'REMOTE_ADDR' && $val == $this->env('SERVER_ADDR')) {
if (($addr = $this->env('HTTP_PC_REMOTE_ADDR')) != null) {
$val = $addr;
}
}
if ($val !== null && $val !== false) {
return $val;
}
switch ($key) {
case 'SCRIPT_FILENAME':
if ($this->_env['PLATFORM'] == 'IIS') {
return str_replace('\\\\', '\\', $this->env('PATH_TRANSLATED'));
}
break;
case 'DOCUMENT_ROOT':
$fileName = $this->env('SCRIPT_FILENAME');
$offset = (!strpos($this->env('SCRIPT_NAME'), '.php')) ? 4 : 0;
$offset = strlen($fileName) - (strlen($this->env('SCRIPT_NAME')) + $offset);
return substr($fileName, 0, $offset);
break;
case 'PHP_SELF':
return str_replace($this->env('DOCUMENT_ROOT'), '', $this->env('SCRIPT_FILENAME'));
break;
case 'CGI':
case 'CGI_MODE':
return ($this->_env['PLATFORM'] == 'CGI');
break;
case 'HTTP_BASE':
return preg_replace ('/^([^.])*/i', null, $this->_env['HTTP_HOST']);
break;
}
return null;
}
/**
* undocumented function
*
* @param string $key
* @return void
*/
public function get($key) {
list($var, $key) = explode(':', $key);
switch (true) {
case in_array($var, array('params', 'data', 'query')):
return isset($this->{$key[0]}[$key[1]]) ? $this->{$key[0]}[$key[1]] : null;
break;
case ($var == 'env'):
return $this->env($key);
break;
}
return null;
}
/**
* Detects properties of the request and returns a boolean response
*
* @return boolean
* @see lithium\http\Request::detect()
* @todo Remove $content and refer to Media class instead
*/
public function is($flag) {
$flag = strtolower($flag);
$content = array('xml', 'rss', 'atom');
if (array_key_exists($flag, $this->_detectors)) {
$detector = $this->_detectors[$flag];
if (is_array($detector)) {
if (is_string($detector[1]) && Validator::isRegex($detector[1])) {
return (bool)preg_match($detector[1], $this->env($detector[0]));
}
return ($this->env($detector[0]) == $detectors[1]);
} elseif (is_object($detector)) {
return $detector($this);
}
return (bool)$this->env($detector);
}
return false;
}
/**
* Returns the content type of the response.
*
* @return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending
* on the content type of the request.
*/
public function type() {
return $this->_type;
}
/**
* Creates a 'detector' used with Request::is(). A detector is a boolean check that is created
* to determine something about a request.
*
* @return void
* @see lithium\http\Request::is()
*/
public function detect($flag, $detector = null) {
if (is_array($flag)) {
$this->_detectors = $flag + $this->_detectors;
} else {
$this->_detectors[$flag] = $detector;
}
}
/**
* Gets the referring URL of this request
*
* @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
* @param boolean $local If true, restrict referring URLs to local server
* @return string Referring URL
* @todo Rewrite me to remove constant dependencies
*/
function referer($default = null, $local = false) {
$ref = $this->env('HTTP_REFERER');
if (!empty($ref) && defined('FULL_BASE_URL')) {
$base = FULL_BASE_URL . $this->webroot;
if (strpos($ref, $base) === 0) {
$return = substr($ref, strlen($base));
if ($return[0] != '/') {
$return = '/'.$return;
}
return $return;
} elseif (!$local) {
return $ref;
}
}
return ($default != null) ? $default : '/';
}
public function normalizeFiles($original) {
$r = array();
$files = array();
foreach ($original as $file) {
if (!is_array($file['name'])) {
$files[] = $file;
continue;
}
$nested = array();
foreach ($file as $key => $items) {
while (is_array(current($items))) {
$items = $items[key($items)];
}
$items = array_values($items);
foreach ($items as $i => $item) {
$nested[$i][$key] = $item;
}
}
$files = array_merge($files, $nested);
}
return $files;
foreach ($files as $field => $values) {
if (!is_array($values['name'])) {
$r[$field] = $values;
continue;
}
foreach ($values as $key => $fields) {
while ($fields) {
foreach ($fields as $tmpField => $val) {
unset($fields[$tmpField]);
if (!is_array($val)) {
$tmpField = preg_replace('/(^[^\[]+)(.*)/', '[\\1]\\2', $tmpField);
$r[$field . $tmpField][$key] = $val;
continue;
}
foreach ($val as $subField => $subVal) {
$fields[$tmpField . '[' . $subField . ']'] = $subVal;
}
}
}
}
}
foreach ($r as $field => $values) {
$r[$field] = compact('field') + $values;
}
return array_values($r);
}
/**
* @todo Replace string directory names with configuration
* @return void
*/
protected function _base() {
$base = dirname($this->env('PHP_SELF'));
if ($base === '/') {
return null;
}
while (in_array(basename($base), array('app', 'webroot'))) {
$base = ltrim(dirname($base), '.');
}
return rtrim($base, '/');
}
}
?>