| 1 |
<?php |
| 2 |
if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('WPRProtectRequest_V565')) : |
| 5 |
class WPRProtectRequest_V565 { |
| 6 |
public $ip; |
| 7 |
public $host = ''; |
| 8 |
public $uri; |
| 9 |
public $method = ''; |
| 10 |
public $path = ''; |
| 11 |
public $timestamp; |
| 12 |
public $get_params; |
| 13 |
public $post_params; |
| 14 |
public $cookies; |
| 15 |
public $headers = array(); |
| 16 |
public $file_names = array(); |
| 17 |
public $json_params = array(); |
| 18 |
public $raw_body = ''; |
| 19 |
public $files; |
| 20 |
public $respcode; |
| 21 |
public $status = WPRProtectRequest_V565::STATUS_ALLOWED; |
| 22 |
public $category = WPRProtectRequest_V565::CATEGORY_NORMAL; |
| 23 |
|
| 24 |
public $wp_user; |
| 25 |
|
| 26 |
private $can_get_raw_body = false; |
| 27 |
private $max_raw_body_length = 1000000; |
| 28 |
private $can_decode_json = false; |
| 29 |
private $max_json_decode_depth = 512; |
| 30 |
|
| 31 |
#XNOTE: SHould be part of Protect. |
| 32 |
const STATUS_ALLOWED = 1; |
| 33 |
const STATUS_BLOCKED = 2; |
| 34 |
const STATUS_BYPASSED = 3; |
| 35 |
|
| 36 |
const CATEGORY_BLACKLISTED = 1; |
| 37 |
const CATEGORY_NORMAL = 10; |
| 38 |
const CATEGORY_WHITELISTED = 20; |
| 39 |
const CATEGORY_BOT_BLOCKED = 30; |
| 40 |
const CATEGORY_COUNTRY_BLOCKED = 40; |
| 41 |
const CATEGORY_USER_BLACKLISTED = 50; |
| 42 |
const CATEGORY_RULE_BLOCKED = 60; |
| 43 |
const CATEGORY_RULE_ALLOWED = 70; |
| 44 |
const CATEGORY_PRIVATEIP = 80; |
| 45 |
const CATEGORY_GLOBAL_BOT_BLOCKED = 90; |
| 46 |
|
| 47 |
public function __construct($ip_header, $config) { |
| 48 |
$this->ip = WPRProtectUtils_V565::getIP($ip_header); |
| 49 |
$this->timestamp = time(); |
| 50 |
$this->get_params = $_GET; |
| 51 |
$this->cookies = $_COOKIE; |
| 52 |
$this->post_params = $_POST; |
| 53 |
$this->files = $_FILES; |
| 54 |
|
| 55 |
if (array_key_exists('cangetrawbody', $config) && is_bool($config['cangetrawbody'])) { |
| 56 |
$this->can_get_raw_body = $config['cangetrawbody']; |
| 57 |
} |
| 58 |
|
| 59 |
if (array_key_exists('maxrawbodylength', $config) && is_int($config['maxrawbodylength'])) { |
| 60 |
$this->max_raw_body_length = $config['maxrawbodylength']; |
| 61 |
} |
| 62 |
|
| 63 |
if (array_key_exists('candecodejson', $config) && is_bool($config['candecodejson'])) { |
| 64 |
$this->can_decode_json = $config['candecodejson']; |
| 65 |
} |
| 66 |
|
| 67 |
if (array_key_exists('maxjsondecodedepth', $config) && is_int($config['maxjsondecodedepth'])) { |
| 68 |
$this->max_json_decode_depth = $config['maxjsondecodedepth']; |
| 69 |
} |
| 70 |
|
| 71 |
if (!empty($_FILES)) { |
| 72 |
foreach ($_FILES as $input => $file) { |
| 73 |
$this->file_names[$input] = $file['name']; |
| 74 |
} |
| 75 |
} |
| 76 |
if (is_array($_SERVER)) { |
| 77 |
foreach ($_SERVER as $key => $value) { |
| 78 |
if (strpos($key, 'HTTP_') === 0) { |
| 79 |
$header = substr($key, 5); |
| 80 |
$header = str_replace(array(' ', '_'), array('', ' '), $header); |
| 81 |
$header = ucwords(strtolower($header)); |
| 82 |
$header = str_replace(' ', '-', $header); |
| 83 |
$this->headers[$header] = $value; |
| 84 |
} |
| 85 |
} |
| 86 |
if (array_key_exists('CONTENT_TYPE', $_SERVER)) { |
| 87 |
$this->headers['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
| 88 |
} |
| 89 |
if (array_key_exists('CONTENT_LENGTH', $_SERVER)) { |
| 90 |
$this->headers['Content-Length'] = $_SERVER['CONTENT_LENGTH']; |
| 91 |
} |
| 92 |
if (array_key_exists('REFERER', $_SERVER)) { |
| 93 |
$this->headers['Referer'] = $_SERVER['REFERER']; |
| 94 |
} |
| 95 |
if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) { |
| 96 |
$this->headers['User-Agent'] = $_SERVER['HTTP_USER_AGENT']; |
| 97 |
} |
| 98 |
|
| 99 |
if (array_key_exists('Host', $this->headers)) { |
| 100 |
$this->host = $this->headers['Host']; |
| 101 |
} elseif (array_key_exists('SERVER_NAME', $_SERVER)) { |
| 102 |
$this->host = $_SERVER['SERVER_NAME']; |
| 103 |
} |
| 104 |
|
| 105 |
$this->method = array_key_exists('REQUEST_METHOD', $_SERVER) |
| 106 |
? $_SERVER['REQUEST_METHOD'] : 'GET'; |
| 107 |
$this->uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : ''; |
| 108 |
$_uri = parse_url($this->uri); |
| 109 |
$this->path = (is_array($_uri) && array_key_exists('path', $_uri)) ? $_uri['path'] : $this->uri; |
| 110 |
} |
| 111 |
|
| 112 |
if ($this->can_get_raw_body) { |
| 113 |
$_raw_body = file_get_contents("php://input", false, null, 0, $this->max_raw_body_length); |
| 114 |
if ($_raw_body !== false) { |
| 115 |
$this->raw_body = $_raw_body; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if ($this->can_decode_json) { |
| 120 |
if ($this->getContentType() === "application/json" && !empty($this->raw_body)) { |
| 121 |
$_json_params = WPRProtectUtils_V565::safeDecodeJSON($this->raw_body, |
| 122 |
true, $this->max_json_decode_depth); |
| 123 |
if (isset($_json_params)) { |
| 124 |
$this->json_params['JSON'] = $_json_params; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public static function blacklistedCategories() { |
| 131 |
return array( |
| 132 |
WPRProtectRequest_V565::CATEGORY_BOT_BLOCKED, |
| 133 |
WPRProtectRequest_V565::CATEGORY_COUNTRY_BLOCKED, |
| 134 |
WPRProtectRequest_V565::CATEGORY_USER_BLACKLISTED, |
| 135 |
WPRProtectRequest_V565::CATEGORY_GLOBAL_BOT_BLOCKED |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
public static function whitelistedCategories() { |
| 140 |
return array(WPRProtectRequest_V565::CATEGORY_WHITELISTED); |
| 141 |
} |
| 142 |
|
| 143 |
public function setRespCode($code) { |
| 144 |
$this->respcode = $code; |
| 145 |
} |
| 146 |
|
| 147 |
public function getRespCode() { |
| 148 |
if (!isset($this->respcode) && function_exists('http_response_code')) { |
| 149 |
$this->respcode = http_response_code(); |
| 150 |
} |
| 151 |
|
| 152 |
return $this->respcode; |
| 153 |
} |
| 154 |
|
| 155 |
public function getStatus() { |
| 156 |
return $this->status; |
| 157 |
} |
| 158 |
|
| 159 |
public function getCategory() { |
| 160 |
return $this->category; |
| 161 |
} |
| 162 |
|
| 163 |
private function getKeyVal($array, $key) { |
| 164 |
if (is_array($array)) { |
| 165 |
if (is_array($key)) { |
| 166 |
$_key = array_shift($key); |
| 167 |
if (array_key_exists($_key, $array)) { |
| 168 |
if (count($key) > 0) { |
| 169 |
return $this->getKeyVal($array[$_key], $key); |
| 170 |
} else { |
| 171 |
return $array[$_key]; |
| 172 |
} |
| 173 |
} |
| 174 |
} else { |
| 175 |
return array_key_exists($key, $array) ? $array[$key] : null; |
| 176 |
} |
| 177 |
} |
| 178 |
return null; |
| 179 |
} |
| 180 |
|
| 181 |
public function getPostParams() { |
| 182 |
if (func_num_args() > 0) { |
| 183 |
$args = func_get_args(); |
| 184 |
return $this->getKeyVal($this->post_params, $args); |
| 185 |
} |
| 186 |
return $this->post_params; |
| 187 |
} |
| 188 |
|
| 189 |
public function getCookies() { |
| 190 |
if (func_num_args() > 0) { |
| 191 |
$args = func_get_args(); |
| 192 |
return $this->getKeyVal($this->cookies, $args); |
| 193 |
} |
| 194 |
return $this->cookies; |
| 195 |
} |
| 196 |
|
| 197 |
public function getGetParams() { |
| 198 |
if (func_num_args() > 0) { |
| 199 |
$args = func_get_args(); |
| 200 |
return $this->getKeyVal($this->get_params, $args); |
| 201 |
} |
| 202 |
return $this->get_params; |
| 203 |
} |
| 204 |
|
| 205 |
public function getAllParams() { |
| 206 |
return array("getParams" => $this->get_params, "postParams" => $this->post_params, "jsonParams" => $this->json_params); |
| 207 |
} |
| 208 |
|
| 209 |
public function getHeader($key) { |
| 210 |
if (array_key_exists($key, $this->headers)) { |
| 211 |
return $this->headers[$key]; |
| 212 |
} |
| 213 |
return null; |
| 214 |
} |
| 215 |
|
| 216 |
public function getHeaders() { |
| 217 |
if (func_num_args() > 0) { |
| 218 |
$args = func_get_args(); |
| 219 |
return $this->getKeyVal($this->headers, $args); |
| 220 |
} |
| 221 |
return $this->headers; |
| 222 |
} |
| 223 |
|
| 224 |
public function getFiles() { |
| 225 |
if (func_num_args() > 0) { |
| 226 |
$args = func_get_args(); |
| 227 |
return $this->getKeyVal($this->files, $args); |
| 228 |
} |
| 229 |
return $this->files; |
| 230 |
} |
| 231 |
|
| 232 |
public function getFileNames() { |
| 233 |
if (func_num_args() > 0) { |
| 234 |
$args = func_get_args(); |
| 235 |
return $this->getKeyVal($this->file_names, $args); |
| 236 |
} |
| 237 |
return $this->file_names; |
| 238 |
} |
| 239 |
|
| 240 |
public function getHost() { |
| 241 |
return $this->host; |
| 242 |
} |
| 243 |
|
| 244 |
public function getURI() { |
| 245 |
return $this->uri; |
| 246 |
} |
| 247 |
|
| 248 |
public function getAction() { |
| 249 |
$post_action = $this->getPostParams('action'); |
| 250 |
if (isset($post_action)) { |
| 251 |
return $post_action; |
| 252 |
} else { |
| 253 |
return $this->getGetParams('action'); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
public function getPath() { |
| 258 |
return $this->path; |
| 259 |
} |
| 260 |
|
| 261 |
public function getIP() { |
| 262 |
return $this->ip; |
| 263 |
} |
| 264 |
|
| 265 |
public function getMethod() { |
| 266 |
return $this->method; |
| 267 |
} |
| 268 |
|
| 269 |
public function getTimestamp() { |
| 270 |
return $this->timestamp; |
| 271 |
} |
| 272 |
|
| 273 |
public function getRequestID() { |
| 274 |
if (!defined("BV_REQUEST_ID")) { |
| 275 |
define("BV_REQUEST_ID", uniqid(mt_rand())); |
| 276 |
} |
| 277 |
|
| 278 |
return BV_REQUEST_ID; |
| 279 |
} |
| 280 |
|
| 281 |
public function getServerValue($key) { |
| 282 |
if (isset($_SERVER) && array_key_exists($key, $_SERVER)) { |
| 283 |
return $_SERVER[$key]; |
| 284 |
} |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
public function getHeadersV2() { |
| 289 |
return $this->headers; |
| 290 |
} |
| 291 |
|
| 292 |
public function getFilesV2() { |
| 293 |
return $this->files; |
| 294 |
} |
| 295 |
|
| 296 |
public function getFileNamesV2() { |
| 297 |
return $this->file_names; |
| 298 |
} |
| 299 |
|
| 300 |
public function getPostParamsV2() { |
| 301 |
return $this->post_params; |
| 302 |
} |
| 303 |
|
| 304 |
public function getGetParamsV2() { |
| 305 |
return $this->get_params; |
| 306 |
} |
| 307 |
|
| 308 |
public function getCookiesV2() { |
| 309 |
return $this->cookies; |
| 310 |
} |
| 311 |
|
| 312 |
public function getJsonParams() { |
| 313 |
return $this->json_params; |
| 314 |
} |
| 315 |
|
| 316 |
public function getRawBody() { |
| 317 |
return $this->raw_body; |
| 318 |
} |
| 319 |
|
| 320 |
public function getContentType() { |
| 321 |
if (array_key_exists('Content-Type', $this->headers)) { |
| 322 |
return $this->headers['Content-Type']; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
public function getContentLength() { |
| 327 |
if (array_key_exists('Content-Length', $this->headers)) { |
| 328 |
return $this->headers['Content-Length']; |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
endif; |