forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
147 lines (119 loc) · 3.17 KB
/
Copy pathRequest.php
File metadata and controls
147 lines (119 loc) · 3.17 KB
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
<?php
namespace PHPCensor\Http;
class Request
{
/**
* @var array
*/
protected $params = [];
/**
* Request data.
*/
protected $data = [];
/**
* Set up the request.
*/
public function __construct()
{
$this->parseInput();
$this->data['path'] = $this->getRequestPath();
$this->data['parts'] = array_values(array_filter(explode('/', $this->data['path'])));
}
protected function getRequestPath()
{
$path = '';
// Start out with the REQUEST_URI:
if (!empty($_SERVER['REQUEST_URI'])) {
$path = $_SERVER['REQUEST_URI'];
}
if ($_SERVER['SCRIPT_NAME'] != $_SERVER['REQUEST_URI']) {
$scriptPath = str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']);
$path = str_replace($scriptPath, '', $path);
}
// Remove index.php from the URL if it is present:
$path = str_replace(['/index.php', 'index.php'], '', $path);
// Also cut out the query string:
$path = explode('?', $path);
$path = array_shift($path);
return $path;
}
/**
* Parse incoming variables, incl. $_GET, $_POST and also reads php://input for PUT/DELETE.
*/
protected function parseInput()
{
$params = $_REQUEST;
if (!isset($_SERVER['REQUEST_METHOD']) || in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'DELETE'])) {
$vars = file_get_contents('php://input');
if (!is_string($vars) || strlen(trim($vars)) === 0) {
$vars = '';
}
$inputData = [];
parse_str($vars, $inputData);
$params = array_merge($params, $inputData);
}
$this->setParams($params);
}
/**
* Returns all request parameters.
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Return a specific request parameter, or a default value if not set.
*/
public function getParam($key, $default = null)
{
if (isset($this->params[$key])) {
return $this->params[$key];
} else {
return $default;
}
}
/**
* Set or override a request parameter.
*/
public function setParam($key, $value = null)
{
$this->params[$key] = $value;
}
/**
* Set an array of request parameters.
*/
public function setParams(array $params)
{
$this->params = array_merge($this->params, $params);
}
/**
* Un-set a specific parameter.
*/
public function unsetParam($key)
{
unset($this->params[$key]);
}
public function getMethod()
{
return strtoupper($_SERVER['REQUEST_METHOD']);
}
public function getPath()
{
return $this->data['path'];
}
public function getPathParts()
{
return $this->data['parts'];
}
public function isAjax()
{
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
return false;
}
if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return true;
}
return false;
}
}