| 1 |
<?php |
| 2 |
|
| 3 |
if (! (defined('ABSPATH') || defined('MCDATAPATH')) ) exit; |
| 4 |
if (!class_exists('BVWPRequest')) : |
| 5 |
|
| 6 |
class BVWPRequest { |
| 7 |
private $fileNames; |
| 8 |
private $files; |
| 9 |
private $headers; |
| 10 |
private $host; |
| 11 |
private $ip; |
| 12 |
private $method; |
| 13 |
private $path; |
| 14 |
private $getParams; |
| 15 |
private $timestamp; |
| 16 |
private $uri; |
| 17 |
private $postParams; |
| 18 |
private $cookies; |
| 19 |
private $respcode; |
| 20 |
private $status; |
| 21 |
private $rulesInfo; |
| 22 |
private $reqInfo; |
| 23 |
private $matchedRules; |
| 24 |
|
| 25 |
#status |
| 26 |
const ALLOWED = 1; |
| 27 |
const BLOCKED = 2; |
| 28 |
const BYPASSED = 3; |
| 29 |
|
| 30 |
#category |
| 31 |
const BLACKLISTED = 1; |
| 32 |
const NORMAL = 10; |
| 33 |
const WHITELISTED = 20; |
| 34 |
const BOT_BLOCKED = 30; |
| 35 |
const COUNTRY_BLOCKED = 40; |
| 36 |
const USER_BLACKLISTED = 50; |
| 37 |
const RULE_BLOCKED = 60; |
| 38 |
const RULE_ALLOWED = 70; |
| 39 |
const PRIVATEIP = 80; |
| 40 |
|
| 41 |
public function __construct($ip) { |
| 42 |
$fileNames = array(); |
| 43 |
$headers = array(); |
| 44 |
$host = ''; |
| 45 |
$method = ''; |
| 46 |
$path = ''; |
| 47 |
$this->ip = $ip; |
| 48 |
$this->rulesInfo = array(); |
| 49 |
$this->matchedRules = array(); |
| 50 |
$this->reqInfo = array(); |
| 51 |
$this->setRespCode(0); |
| 52 |
$this->setCategory(BVWPRequest::NORMAL); |
| 53 |
$this->setStatus(BVWpRequest::ALLOWED); |
| 54 |
$this->setTimestamp(time()); |
| 55 |
$this->setGetParams($_GET); |
| 56 |
$this->setCookies($_COOKIE); |
| 57 |
$this->setPostParams($_POST); |
| 58 |
$this->setFiles($_FILES); |
| 59 |
if (!empty($_FILES)) { |
| 60 |
foreach ($_FILES as $input => $file) { |
| 61 |
$fileNames[$input] = $file['name']; |
| 62 |
} |
| 63 |
} |
| 64 |
$this->setFileNames($fileNames); |
| 65 |
if (is_array($_SERVER)) { |
| 66 |
foreach ($_SERVER as $key => $value) { |
| 67 |
if (strpos($key, 'HTTP_') === 0) { |
| 68 |
$header = substr($key, 5); |
| 69 |
$header = str_replace(array(' ', '_'), array('', ' '), $header); |
| 70 |
$header = ucwords(strtolower($header)); |
| 71 |
$header = str_replace(' ', '-', $header); |
| 72 |
$headers[$header] = $value; |
| 73 |
} |
| 74 |
} |
| 75 |
if (array_key_exists('CONTENT_TYPE', $_SERVER)) { |
| 76 |
$headers['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
| 77 |
} |
| 78 |
if (array_key_exists('CONTENT_LENGTH', $_SERVER)) { |
| 79 |
$headers['Content-Length'] = $_SERVER['CONTENT_LENGTH']; |
| 80 |
} |
| 81 |
if (array_key_exists('REFERER', $_SERVER)) { |
| 82 |
$headers['Referer'] = $_SERVER['REFERER']; |
| 83 |
} |
| 84 |
if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) { |
| 85 |
$headers['User-Agent'] = $_SERVER['HTTP_USER_AGENT']; |
| 86 |
} |
| 87 |
|
| 88 |
if (array_key_exists('Host', $headers)) { |
| 89 |
$host = $headers['Host']; |
| 90 |
} else if (array_key_exists('SERVER_NAME', $_SERVER)) { |
| 91 |
$host = $_SERVER['SERVER_NAME']; |
| 92 |
} |
| 93 |
|
| 94 |
$method = array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
| 95 |
$uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : ''; |
| 96 |
$_uri = parse_url($uri); |
| 97 |
$path = (is_array($_uri) && array_key_exists('path', $_uri)) ? $_uri['path'] : $uri; |
| 98 |
} |
| 99 |
$this->setHeaders($headers); |
| 100 |
$this->setHost($host); |
| 101 |
$this->setMethod($method); |
| 102 |
$this->setUri($uri); |
| 103 |
$this->setPath($path); |
| 104 |
} |
| 105 |
|
| 106 |
public function setStatus($status) { |
| 107 |
$this->status = $status; |
| 108 |
} |
| 109 |
|
| 110 |
public function setCategory($category) { |
| 111 |
$this->category = $category; |
| 112 |
} |
| 113 |
|
| 114 |
public function setPostParams($postParams) { |
| 115 |
$this->postParams = $postParams; |
| 116 |
} |
| 117 |
|
| 118 |
public function setCookies($cookies) { |
| 119 |
$this->cookies = $cookies; |
| 120 |
} |
| 121 |
|
| 122 |
public function setFileNames($fileNames) { |
| 123 |
$this->fileNames = $fileNames; |
| 124 |
} |
| 125 |
|
| 126 |
public function setFiles($files) { |
| 127 |
$this->files = $files; |
| 128 |
} |
| 129 |
|
| 130 |
public function setHeaders($headers) { |
| 131 |
$this->headers = $headers; |
| 132 |
} |
| 133 |
|
| 134 |
public function setRespCode($code) { |
| 135 |
$this->respcode = $code; |
| 136 |
} |
| 137 |
|
| 138 |
public function getRespCode() { |
| 139 |
return $this->respcode; |
| 140 |
} |
| 141 |
|
| 142 |
public function setHost($host) { |
| 143 |
$this->host = $host; |
| 144 |
} |
| 145 |
|
| 146 |
public function setMethod($method) { |
| 147 |
$this->method = $method; |
| 148 |
} |
| 149 |
|
| 150 |
public function setPath($path) { |
| 151 |
$this->path = $path; |
| 152 |
} |
| 153 |
|
| 154 |
public function setGetParams($getParams) { |
| 155 |
$this->getParams = $getParams; |
| 156 |
} |
| 157 |
|
| 158 |
public function setTimestamp($timestamp) { |
| 159 |
$this->timestamp = $timestamp; |
| 160 |
} |
| 161 |
|
| 162 |
public function setUri($uri) { |
| 163 |
$this->uri = $uri; |
| 164 |
} |
| 165 |
|
| 166 |
public function updateMatchedRules($matched_rule_id) { |
| 167 |
$this->matchedRules[] = $matched_rule_id; |
| 168 |
} |
| 169 |
|
| 170 |
public function updateRulesInfo($category, $sub_category, $value) { |
| 171 |
$rule_info = (array_key_exists($category, $this->rulesInfo)) ? $this->rulesInfo[$category] : array(); |
| 172 |
$rule_info[$sub_category] = $value; |
| 173 |
$this->rulesInfo[$category] = $rule_info; |
| 174 |
} |
| 175 |
|
| 176 |
public function getRulesInfo() { |
| 177 |
return $this->rulesInfo; |
| 178 |
} |
| 179 |
|
| 180 |
public function getMatchedRules() { |
| 181 |
return $this->matchedRules; |
| 182 |
} |
| 183 |
|
| 184 |
public function hasMatchedRules() { |
| 185 |
return !empty($this->matchedRules); |
| 186 |
} |
| 187 |
|
| 188 |
public function updateReqInfo($info) { |
| 189 |
if (is_array($info)) { |
| 190 |
$this->reqInfo = $this->reqInfo + $info; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public function getReqInfo() { |
| 195 |
return $this->reqInfo; |
| 196 |
} |
| 197 |
|
| 198 |
public function getStatus() { |
| 199 |
return $this->status; |
| 200 |
} |
| 201 |
|
| 202 |
public function getCategory() { |
| 203 |
return $this->category; |
| 204 |
} |
| 205 |
|
| 206 |
public function getDataToLog() { |
| 207 |
$referer = $this->getHeader('Referer') ? $this->getHeader('Referer') : ''; |
| 208 |
$user_agent = $this->getHeader('User-Agent') ? $this->getHeader('User-Agent') : ''; |
| 209 |
$rules_info = serialize($this->getRulesInfo()); |
| 210 |
$req_info = serialize($this->getReqInfo()); |
| 211 |
if (strlen($req_info) > 16000) { |
| 212 |
$req_info = serialize(array("keys" => array_keys($this->getReqInfo()))); |
| 213 |
if (strlen($req_info) > 16000) { |
| 214 |
$req_info = serialize(array("bv_over_size" => true)); |
| 215 |
} |
| 216 |
} |
| 217 |
$data = array( |
| 218 |
"path" => $this->getPath(), |
| 219 |
"filenames" => serialize($this->getFileNames()), |
| 220 |
"host" => $this->getHost(), |
| 221 |
"time" => $this->getTimeStamp(), |
| 222 |
"ip" => $this->getIP(), |
| 223 |
"method" => $this->getMethod(), |
| 224 |
"query_string" => $req_info, |
| 225 |
"user_agent" => $user_agent, |
| 226 |
"resp_code" => $this->getRespCode(), |
| 227 |
"referer" => $referer, |
| 228 |
"status" => $this->getStatus(), |
| 229 |
"category" => $this->getCategory(), |
| 230 |
"rules_info" => $rules_info, |
| 231 |
"request_id" => $this->getRequestID(), |
| 232 |
"matched_rules"=> serialize($this->getMatchedRules()) |
| 233 |
); |
| 234 |
return $data; |
| 235 |
} |
| 236 |
|
| 237 |
protected function getKeyVal($array, $key) { |
| 238 |
if (is_array($array)) { |
| 239 |
if (is_array($key)) { |
| 240 |
$_key = array_shift($key); |
| 241 |
if (array_key_exists($_key, $array)) { |
| 242 |
if (count($key) > 0) { |
| 243 |
return $this->getKeyVal($array[$_key], $key); |
| 244 |
} else { |
| 245 |
return $array[$_key]; |
| 246 |
} |
| 247 |
} |
| 248 |
} else { |
| 249 |
return array_key_exists($key, $array) ? $array[$key] : null; |
| 250 |
} |
| 251 |
} |
| 252 |
return null; |
| 253 |
} |
| 254 |
|
| 255 |
public function getPostParams() { |
| 256 |
if (func_num_args() > 0) { |
| 257 |
$args = func_get_args(); |
| 258 |
return $this->getKeyVal($this->postParams, $args); |
| 259 |
} |
| 260 |
return $this->postParams; |
| 261 |
} |
| 262 |
|
| 263 |
public function getCookies() { |
| 264 |
if (func_num_args() > 0) { |
| 265 |
$args = func_get_args(); |
| 266 |
return $this->getKeyVal($this->cookies, $args); |
| 267 |
} |
| 268 |
return $this->cookies; |
| 269 |
} |
| 270 |
|
| 271 |
public function getGetParams() { |
| 272 |
if (func_num_args() > 0) { |
| 273 |
$args = func_get_args(); |
| 274 |
return $this->getKeyVal($this->getParams, $args); |
| 275 |
} |
| 276 |
return $this->getParams; |
| 277 |
} |
| 278 |
|
| 279 |
public function getAllParams() { |
| 280 |
return array("getParams" => $this->getParams, "postParams" => $this->postParams); |
| 281 |
} |
| 282 |
|
| 283 |
public function getHeader($key) { |
| 284 |
if (array_key_exists($key, $this->headers)) { |
| 285 |
return $this->headers[$key]; |
| 286 |
} |
| 287 |
return null; |
| 288 |
} |
| 289 |
|
| 290 |
public function getHeaders() { |
| 291 |
if (func_num_args() > 0) { |
| 292 |
$args = func_get_args(); |
| 293 |
return $this->getKeyVal($this->headers, $args); |
| 294 |
} |
| 295 |
return $this->headers; |
| 296 |
} |
| 297 |
|
| 298 |
public function getFiles() { |
| 299 |
if (func_num_args() > 0) { |
| 300 |
$args = func_get_args(); |
| 301 |
return $this->getKeyVal($this->files, $args); |
| 302 |
} |
| 303 |
return $this->files; |
| 304 |
} |
| 305 |
|
| 306 |
public function getFileNames() { |
| 307 |
if (func_num_args() > 0) { |
| 308 |
$args = func_get_args(); |
| 309 |
return $this->getKeyVal($this->fileNames, $args); |
| 310 |
} |
| 311 |
return $this->fileNames; |
| 312 |
} |
| 313 |
|
| 314 |
public function getHost() { |
| 315 |
return $this->host; |
| 316 |
} |
| 317 |
|
| 318 |
public function getURI() { |
| 319 |
return $this->uri; |
| 320 |
} |
| 321 |
|
| 322 |
public function getPath() { |
| 323 |
return $this->path; |
| 324 |
} |
| 325 |
|
| 326 |
public function getIP() { |
| 327 |
return $this->ip; |
| 328 |
} |
| 329 |
|
| 330 |
public function getMethod() { |
| 331 |
return $this->method; |
| 332 |
} |
| 333 |
|
| 334 |
public function getTimestamp() { |
| 335 |
return $this->timestamp; |
| 336 |
} |
| 337 |
|
| 338 |
public function getRequestID() { |
| 339 |
if (!defined("BV_REQUEST_ID")) { |
| 340 |
define("BV_REQUEST_ID", uniqid(mt_rand())); |
| 341 |
} |
| 342 |
return BV_REQUEST_ID; |
| 343 |
} |
| 344 |
|
| 345 |
public function getServerValue($key) { |
| 346 |
if (isset($_SERVER) && array_key_exists($key, $_SERVER)) { |
| 347 |
return $_SERVER[$key]; |
| 348 |
} |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
public function getUserRoleLevel() { |
| 353 |
// TODO |
| 354 |
// Handle prepend level using custom cookies. |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
public function isUserRoleLevel($level) { |
| 359 |
return ($level === $this->getUserRoleLevel()); |
| 360 |
} |
| 361 |
} |
| 362 |
endif; |