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
|
<?php
if (! (defined('ABSPATH') || defined('MCDATAPATH')) ) exit; if (!class_exists('BVWPRequest')) : class BVWPRequest { private $fileNames; private $files; private $headers; private $host; private $ip; private $method; private $path; private $getParams; private $timestamp; private $uri; private $postParams; private $cookies; private $respcode; private $status; private $rulesInfo; private $reqInfo;
#status const ALLOWED = 1; const BLOCKED = 2; const BYPASSED = 3;
#category const BLACKLISTED = 1; const NORMAL = 10; const WHITELISTED = 20; const BOT_BLOCKED = 30; const COUNTRY_BLOCKED = 40; const USER_BLACKLISTED = 50; const RULE_BLOCKED = 60; const RULE_ALLOWED = 70;
public function __construct($ip) { $fileNames = array(); $headers = array(); $host = ''; $method = ''; $path = ''; $this->ip = $ip; $this->rulesInfo = array(); $this->reqInfo = array(); $this->setRespCode(0); $this->setCategory(BVWPRequest::NORMAL); $this->setStatus(BVWpRequest::ALLOWED); $this->setTimestamp(time()); $this->setGetParams($_GET); $this->setCookies($_COOKIE); $this->setPostParams($_POST); $this->setFiles($_FILES); if (!empty($_FILES)) { foreach ($_FILES as $input => $file) { $fileNames[$input] = $file['name']; } } $this->setFileNames($fileNames); if (is_array($_SERVER)) { foreach ($_SERVER as $key => $value) { if (strpos($key, 'HTTP_') === 0) { $header = substr($key, 5); $header = str_replace(array(' ', '_'), array('', ' '), $header); $header = ucwords(strtolower($header)); $header = str_replace(' ', '-', $header); $headers[$header] = $value; } } if (array_key_exists('CONTENT_TYPE', $_SERVER)) { $headers['Content-Type'] = $_SERVER['CONTENT_TYPE']; } if (array_key_exists('CONTENT_LENGTH', $_SERVER)) { $headers['Content-Length'] = $_SERVER['CONTENT_LENGTH']; } if (array_key_exists('REFERER', $_SERVER)) { $headers['Referer'] = $_SERVER['REFERER']; } if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) { $headers['User-Agent'] = $_SERVER['HTTP_USER_AGENT']; }
if (array_key_exists('Host', $headers)) { $host = $headers['Host']; } else if (array_key_exists('SERVER_NAME', $_SERVER)) { $host = $_SERVER['SERVER_NAME']; }
$method = array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET'; $uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : ''; $_uri = parse_url($uri); $path = (is_array($_uri) && array_key_exists('path', $_uri)) ? $_uri['path'] : $uri; } $this->setHeaders($headers); $this->setHost($host); $this->setMethod($method); $this->setUri($uri); $this->setPath($path); }
public function setStatus($status) { $this->status = $status; }
public function setCategory($category) { $this->category = $category; }
public function setPostParams($postParams) { $this->postParams = $postParams; }
public function setCookies($cookies) { $this->cookies = $cookies; }
public function setFileNames($fileNames) { $this->fileNames = $fileNames; }
public function setFiles($files) { $this->files = $files; } public function setHeaders($headers) { $this->headers = $headers; }
public function setRespCode($code) { $this->respcode = $code; }
public function getRespCode() { return $this->respcode; }
public function setHost($host) { $this->host = $host; }
public function setMethod($method) { $this->method = $method; }
public function setPath($path) { $this->path = $path; }
public function setGetParams($getParams) { $this->getParams = $getParams; }
public function setTimestamp($timestamp) { $this->timestamp = $timestamp; }
public function setUri($uri) { $this->uri = $uri; }
public function updateRulesInfo($category, $sub_category, $value) { $rule_info = (array_key_exists($category, $this->rulesInfo)) ? $this->rulesInfo[$category] : array(); $rule_info[$sub_category] = $value; $this->rulesInfo[$category] = $rule_info; } public function getRulesInfo() { return $this->rulesInfo; }
public function updateReqInfo($info) { if (is_array($info)) { $this->reqInfo = $this->reqInfo + $info; } }
public function getReqInfo() { return $this->reqInfo; }
public function getStatus() { return $this->status; }
public function getCategory() { return $this->category; }
public function getDataToLog() { $referer = $this->getHeader('Referer') ? $this->getHeader('Referer') : ''; $user_agent = $this->getHeader('User-Agent') ? $this->getHeader('User-Agent') : ''; $rules_info = serialize($this->getRulesInfo()); $req_info = serialize($this->getReqInfo()); if (strlen($req_info) > 16000) { $req_info = serialize(array("keys" => array_keys($this->getReqInfo()))); if (strlen($req_info) > 16000) { $req_info = serialize(array("bv_over_size" => true)); } } $data = array( "path" => $this->getPath(), "filenames" => serialize($this->getFileNames()), "host" => $this->getHost(), "time" => $this->getTimeStamp(), "ip" => $this->getIP(), "method" => $this->getMethod(), "query_string" => $req_info, "user_agent" => $user_agent, "resp_code" => $this->getRespCode(), "referer" => $referer, "status" => $this->getStatus(), "category" => $this->getCategory(), "rules_info" => $rules_info, "request_id" => $this->getRequestID() ); return $data; }
protected function getKeyVal($array, $key) { if (is_array($array)) { if (is_array($key)) { $_key = array_shift($key); if (array_key_exists($_key, $array)) { if (count($key) > 0) { return $this->getKeyVal($array[$_key], $key); } else { return $array[$_key]; } } } else { return array_key_exists($key, $array) ? $array[$key] : null; } } return null; }
public function getPostParams() { if (func_num_args() > 0) { $args = func_get_args(); return $this->getKeyVal($this->postParams, $args); } return $this->postParams; }
public function getCookies() { if (func_num_args() > 0) { $args = func_get_args(); return $this->getKeyVal($this->cookies, $args); } return $this->cookies; } public function getGetParams() { if (func_num_args() > 0) { $args = func_get_args(); return $this->getKeyVal($this->getParams, $args); } return $this->getParams; }
public function getAllParams() { return array("getParams" => $this->getParams, "postParams" => $this->postParams); }
public function getHeader($key) { if (array_key_exists($key, $this->headers)) { return $this->headers[$key]; } return null; }
public function getHeaders() { if (func_num_args() > 0) { $args = func_get_args(); return $this->getKeyVal($this->headers, $args); } return $this->headers; }
public function getFiles() { if (func_num_args() > 0) { $args = func_get_args(); return $this->getKeyVal($this->files, $args); } return $this->files; }
public function getFileNames() { if (func_num_args() > 0) { $args = func_get_args(); return $this->getKeyVal($this->fileNames, $args); } return $this->fileNames; }
public function getHost() { return $this->host; }
public function getURI() { return $this->uri; }
public function getPath() { return $this->path; }
public function getIP() { return $this->ip; }
public function getMethod() { return $this->method; }
public function getTimestamp() { return $this->timestamp; }
public function getRequestID() { if (!defined("BV_REQUEST_ID")) { define("BV_REQUEST_ID", uniqid(mt_rand())); } return BV_REQUEST_ID; }
public function getServerValue($key) { if (isset($_SERVER) && array_key_exists($key, $_SERVER)) { return $_SERVER[$key]; } return false; }
public function getUserRoleLevel() { // TODO // Handle prepend level using custom cookies. return 0; }
public function isUserRoleLevel($level) { return ($level === $this->getUserRoleLevel()); } } endif;
|