1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\RequestUtils as Utils; |
5
|
|
|
|
6
|
|
|
abstract class AbstractRequest extends \WebServCo\Framework\AbstractLibrary |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Sanitized _SERVER data. |
10
|
|
|
*/ |
11
|
|
|
public $server = []; |
12
|
|
|
/** |
13
|
|
|
* Request method. |
14
|
|
|
*/ |
15
|
|
|
public $method; |
16
|
|
|
/** |
17
|
|
|
* Current script filename. Should most commonly be index.php |
18
|
|
|
*/ |
19
|
|
|
public $filename; |
20
|
|
|
/** |
21
|
|
|
* Script path. |
22
|
|
|
* For HTTP requests this will be public web server subdirectory |
23
|
|
|
* the project is located in. |
24
|
|
|
* For CLI request this will be the script path |
25
|
|
|
* (full or relative, depending on how the script was called). |
26
|
|
|
*/ |
27
|
|
|
public $path = ''; |
28
|
|
|
/** |
29
|
|
|
* Sanitized Framework customized target path. |
30
|
|
|
*/ |
31
|
|
|
public $target = ''; |
32
|
|
|
/** |
33
|
|
|
* Sanitized request query. |
34
|
|
|
*/ |
35
|
|
|
public $query = []; |
36
|
|
|
/** |
37
|
|
|
* Sanitized Framework customized CLI arguments. |
38
|
|
|
* |
39
|
|
|
* Excludes the script name and the second argument |
40
|
|
|
* which is the Framework customized target path. |
41
|
|
|
*/ |
42
|
|
|
public $args = []; |
43
|
|
|
|
44
|
|
|
final public function sanitize($data) |
45
|
|
|
{ |
46
|
|
|
if (is_array($data)) { |
47
|
|
|
array_walk_recursive( |
48
|
|
|
$data, |
49
|
|
|
'WebServCo\Framework\RequestUtils::sanitizeString' |
50
|
|
|
); |
51
|
|
|
return $data; |
52
|
|
|
} |
53
|
|
|
return Utils::sanitizeString($data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function init($server, $post = []) |
57
|
|
|
{ |
58
|
|
|
$this->server = $this->sanitize($server); |
59
|
|
|
$this->method = $this->getMethod(); |
60
|
|
|
$this->filename = $this->getFilename(); |
61
|
|
|
$this->path = $this->getPath(); |
62
|
|
|
$this->process(); |
63
|
|
|
|
64
|
|
|
switch ($this->method) { |
65
|
|
|
case \WebServCo\Framework\Http::METHOD_GET: |
66
|
|
|
case \WebServCo\Framework\Http::METHOD_HEAD: |
67
|
|
|
break; |
68
|
|
|
case \WebServCo\Framework\Http::METHOD_POST: |
69
|
|
|
$this->processPost($post); |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
if ($this->setting('clear_globals', true)) { |
73
|
|
|
$this->clearGlobals(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function clearGlobals() |
78
|
|
|
{ |
79
|
|
|
if (!empty($_GET)) { |
80
|
|
|
foreach ($_GET as $k => $v) { |
81
|
|
|
unset($_REQUEST[$k]); |
82
|
|
|
} |
83
|
|
|
$_GET = []; |
84
|
|
|
} |
85
|
|
|
if (!empty($_POST)) { |
86
|
|
|
$_POST = []; |
87
|
|
|
} |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function processPost($post = []) |
92
|
|
|
{ |
93
|
|
|
$this->data = []; |
94
|
|
|
foreach ($post as $k => $v) { |
95
|
|
|
$this->data[$this->sanitize($k)] = $v; |
96
|
|
|
} |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function process() |
101
|
|
|
{ |
102
|
|
|
if (\WebServCo\Framework\Framework::isCLI()) { |
103
|
|
|
return $this->processCli(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->processHttp(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function processCli() |
110
|
|
|
{ |
111
|
|
|
if (isset($this->server['argv'][1])) { |
112
|
|
|
$this->target = $this->server['argv'][1]; |
113
|
|
|
} |
114
|
|
|
if (isset($this->server['argv'][2])) { |
115
|
|
|
foreach ($this->server['argv'] as $k => $v) { |
116
|
|
|
if (in_array($k, [0,1])) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
$this->args[] = $this->sanitize($v); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function processHttp() |
126
|
|
|
{ |
127
|
|
|
$string = null; |
128
|
|
|
switch (true) { |
129
|
|
|
case isset($this->server['REQUEST_URI']): |
130
|
|
|
$string = $this->server['REQUEST_URI']; |
131
|
|
|
break; |
132
|
|
|
case isset($this->server['PATH_INFO']): |
133
|
|
|
$string = $this->server['PATH_INFO']; |
134
|
|
|
break; |
135
|
|
|
case isset($this->server['ORIG_PATH_INFO']): |
136
|
|
|
$string = $this->server['ORIG_PATH_INFO']; |
137
|
|
|
break; |
138
|
|
|
case !empty($this->server['QUERY_STRING']): |
139
|
|
|
$string = $this->server['ORIG_PATH_INFO']; |
140
|
|
|
break; |
141
|
|
|
default: |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
list ($target, $queryString) = Utils::parse( |
145
|
|
|
$string, |
146
|
|
|
$this->path, |
147
|
|
|
$this->filename, |
148
|
|
|
$this->setting('suffixes') |
149
|
|
|
); |
150
|
|
|
$this->target = $this->sanitize(urldecode($target)); |
151
|
|
|
$this->query = Utils::format($this->sanitize($queryString)); |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function getMethod() |
156
|
|
|
{ |
157
|
|
|
return !empty($this->server['REQUEST_METHOD']) && |
158
|
|
|
in_array( |
159
|
|
|
$this->server['REQUEST_METHOD'], |
160
|
|
|
\WebServCo\Framework\Http::getMethods() |
161
|
|
|
) ? |
162
|
|
|
$this->server['REQUEST_METHOD'] : false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function getFilename() |
166
|
|
|
{ |
167
|
|
|
return !empty($this->server['SCRIPT_NAME']) ? |
168
|
|
|
basename($this->server['SCRIPT_NAME']) : false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function getPath() |
172
|
|
|
{ |
173
|
|
|
if (empty($this->server['SCRIPT_NAME'])) { |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
$path = str_replace($this->getFilename(), '', $this->server['SCRIPT_NAME']); |
|
|
|
|
177
|
|
|
return rtrim($path, DIRECTORY_SEPARATOR); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|