| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit; |
| 3 | 3 | |
| 4 | -if (!class_exists('WPRProtectFW_V568')) : | |
| 4 | +if (!class_exists('WPRProtectFW_V672')) : | |
| 5 | 5 | require_once dirname( __FILE__ ) . '/fw/rule/errors.php'; |
| 6 | 6 | require_once dirname( __FILE__ ) . '/fw/rule/engine.php'; |
| 7 | 7 | require_once dirname( __FILE__ ) . '/fw/rule.php'; |
| 8 | 8 | |
| 9 | -class WPRProtectFW_V568 { | |
| 9 | +class WPRProtectFW_V672 { | |
| 10 | 10 | private $brand_name; |
| 11 | 11 | private $protect_mode; |
| 12 | 12 | private $request; |
| 13 | 13 | private $ipstore; |
| @@ -19,15 +19,16 @@ | ||
| 19 | 19 | private $is_ip_cookie_set = false; |
| 20 | 20 | private $is_request_profiled = false; |
| 21 | 21 | private $is_on_boot_rules_executed = false; |
| 22 | 22 | private $is_ip_checked_for_blacklisted = false; |
| 23 | + private $is_ip_whitelisted = null; | |
| 23 | 24 | private $has_valid_bypass_cookie; |
| 24 | 25 | |
| 25 | - private $mode = WPRProtectFW_V568::MODE_DISABLED; | |
| 26 | - private $ip_cookie_mode = WPRProtectFW_V568::IP_COOKIE_MODE_DISABLED; | |
| 27 | - private $admin_cookie_mode = WPRProtectFW_V568::ADMIN_COOKIE_MODE_DISABLED; | |
| 28 | - private $bypass_level = WPRProtectFW_V568::WP_USER_ROLE_LEVEL_CONTRIBUTOR; | |
| 29 | - private $wpf_rule_init_mode = WPRProtectFW_V568::WPF_RULE_INIT_MODE_WP; | |
| 26 | + private $mode = WPRProtectFW_V672::MODE_DISABLED; | |
| 27 | + private $ip_cookie_mode = WPRProtectFW_V672::IP_COOKIE_MODE_DISABLED; | |
| 28 | + private $admin_cookie_mode = WPRProtectFW_V672::ADMIN_COOKIE_MODE_DISABLED; | |
| 29 | + private $bypass_level = WPRProtectFW_V672::WP_USER_ROLE_LEVEL_CONTRIBUTOR; | |
| 30 | + private $wpf_rule_init_mode = WPRProtectFW_V672::WPF_RULE_INIT_MODE_WP; | |
| 30 | 31 | private $custom_roles = array(); |
| 31 | 32 | private $cookie_key = ""; |
| 32 | 33 | private $cookie_path = ""; |
| 33 | 34 | private $cookie_domain = ""; |
| @@ -32,14 +33,14 @@ | ||
| 32 | 33 | private $cookie_path = ""; |
| 33 | 34 | private $cookie_domain = ""; |
| 34 | 35 | private $cookie_validity = 2592000; |
| 35 | 36 | private $can_set_cache_prevention_cookie = false; |
| 36 | - private $rules_mode = WPRProtectFW_V568::RULES_MODE_DISABLED; | |
| 37 | + private $rules_mode = WPRProtectFW_V672::RULES_MODE_DISABLED; | |
| 37 | 38 | private $is_geo_blocking = false; |
| 38 | 39 | private $is_wp_user_cookie_enabled = false; |
| 39 | 40 | private $log_config = array(); |
| 40 | - private $request_profiling_mode = WPRProtectFW_V568::REQ_PROFILING_MODE_DISABLED; | |
| 41 | - private $logging_mode = WPRProtectFW_V568::LOGGING_MODE_VISITOR; | |
| 41 | + private $request_profiling_mode = WPRProtectFW_V672::REQ_PROFILING_MODE_DISABLED; | |
| 42 | + private $logging_mode = WPRProtectFW_V672::LOGGING_MODE_VISITOR; | |
| 42 | 43 | private $skip_log_config = array(); |
| 43 | 44 | private $skip_log_cookies = array(); |
| 44 | 45 | private $skip_log_headers = array(); |
| 45 | 46 | private $skip_log_post_params = array(); |
| @@ -52,9 +53,9 @@ | ||
| 52 | 53 | private $rule_log = array(); |
| 53 | 54 | private $matched_rules = array(); |
| 54 | 55 | private $break_rule_matching = false; |
| 55 | 56 | private $can_log_raw_body = false; |
| 56 | - private $log_slice_size = WPRProtectFW_V568::LOG_SLICE_SIZE; | |
| 57 | + private $log_slice_size = WPRProtectFW_V672::LOG_SLICE_SIZE; | |
| 57 | 58 | |
| 58 | 59 | private static $instance = null; |
| 59 | 60 | |
| 60 | 61 | const MODE_DISABLED = 1; |
| @@ -98,18 +99,18 @@ | ||
| 98 | 99 | const LOGGING_MODE_COMPLETE = 2; |
| 99 | 100 | const LOGGING_MODE_DISABLED = 3; |
| 100 | 101 | |
| 101 | 102 | const DEFAULT_WP_USER_ROLE_LEVELS = array( |
| 102 | - 'administrator' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_ADMIN, | |
| 103 | - 'editor' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_EDITOR, | |
| 104 | - 'author' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_AUTHOR, | |
| 105 | - 'contributor' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_CONTRIBUTOR, | |
| 106 | - 'subscriber' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_SUBSCRIBER | |
| 103 | + 'administrator' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_ADMIN, | |
| 104 | + 'editor' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_EDITOR, | |
| 105 | + 'author' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_AUTHOR, | |
| 106 | + 'contributor' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_CONTRIBUTOR, | |
| 107 | + 'subscriber' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_SUBSCRIBER | |
| 107 | 108 | ); |
| 108 | 109 | |
| 109 | 110 | const EXTRA_WP_USER_ROLE_LEVELS = array( |
| 110 | - 'custom' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_CUSTOM, | |
| 111 | - 'unknown' => WPRProtectFW_V568::WP_USER_ROLE_LEVEL_UNKNOWN | |
| 111 | + 'custom' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_CUSTOM, | |
| 112 | + 'unknown' => WPRProtectFW_V672::WP_USER_ROLE_LEVEL_UNKNOWN | |
| 112 | 113 | ); |
| 113 | 114 | |
| 114 | 115 | const TABLE_NAME = "fw_requests"; |
| 115 | 116 | const IP_COOKIE_NAME = "mcfw-ip-cookie"; |
| @@ -246,13 +247,13 @@ | ||
| 246 | 247 | } |
| 247 | 248 | |
| 248 | 249 | if ($this->isPrependMode()) { |
| 249 | 250 | $log_file = MCDATAPATH . MCCONFKEY . '-mc.log'; |
| 250 | - $this->ipstore = new WPRProtectIpstore_V568(WPRProtectIpstore_V568::STORAGE_TYPE_FS); | |
| 251 | - $this->logger = new WPRProtectLogger_V568($log_file, WPRProtectLogger_V568::TYPE_FS); | |
| 251 | + $this->ipstore = new WPRProtectIpstore_V672(WPRProtectIpstore_V672::STORAGE_TYPE_FS); | |
| 252 | + $this->logger = new WPRProtectLogger_V672($log_file, WPRProtectLogger_V672::TYPE_FS); | |
| 252 | 253 | } else { |
| 253 | - $this->ipstore = new WPRProtectIpstore_V568(WPRProtectIpstore_V568::STORAGE_TYPE_DB); | |
| 254 | - $this->logger = new WPRProtectLogger_V568(WPRProtectFW_V568::TABLE_NAME, WPRProtectLogger_V568::TYPE_DB); | |
| 254 | + $this->ipstore = new WPRProtectIpstore_V672(WPRProtectIpstore_V672::STORAGE_TYPE_DB); | |
| 255 | + $this->logger = new WPRProtectLogger_V672(WPRProtectFW_V672::TABLE_NAME, WPRProtectLogger_V672::TYPE_DB); | |
| 255 | 256 | } |
| 256 | 257 | |
| 257 | 258 | if ($this->is_wp_user_cookie_enabled) { |
| 258 | 259 | $this->loadWPUser(); |
| @@ -263,12 +264,12 @@ | ||
| 263 | 264 | |
| 264 | 265 | public static function getInstance($protect_mode, $request, $config, $brand_name) { |
| 265 | 266 | if (!isset(self::$instance)) { |
| 266 | 267 | self::$instance = new self($protect_mode, $request, $config, $brand_name); |
| 267 | - } elseif (self::$instance->protect_mode != $protect_mode && $protect_mode == WPRProtect_V568::MODE_WP) { | |
| 268 | + } elseif (self::$instance->protect_mode != $protect_mode && $protect_mode == WPRProtect_V672::MODE_WP) { | |
| 268 | 269 | self::$instance->protect_mode = $protect_mode; |
| 269 | 270 | self::$instance->brand_name = $brand_name; |
| 270 | - self::$instance->ipstore = new WPRProtectIpstore_V568(WPRProtectIpstore_V568::STORAGE_TYPE_DB); | |
| 271 | + self::$instance->ipstore = new WPRProtectIpstore_V672(WPRProtectIpstore_V672::STORAGE_TYPE_DB); | |
| 271 | 272 | self::$instance->initRules(); |
| 272 | 273 | } |
| 273 | 274 | |
| 274 | 275 | return self::$instance; |
| @@ -274,9 +275,9 @@ | ||
| 274 | 275 | return self::$instance; |
| 275 | 276 | } |
| 276 | 277 | |
| 277 | 278 | public static function uninstall() { |
| 278 | - WPRProtect_V568::$db->dropBVTable(WPRProtectFW_V568::TABLE_NAME); | |
| 279 | + WPRProtect_V672::$db->dropBVTable(WPRProtectFW_V672::TABLE_NAME); | |
| 279 | 280 | } |
| 280 | 281 | |
| 281 | 282 | public function init() { |
| 282 | 283 | if (!$this->isModeDisabled()) { |
| @@ -294,42 +295,42 @@ | ||
| 294 | 295 | } |
| 295 | 296 | } |
| 296 | 297 | |
| 297 | 298 | private function isPrependMode() { |
| 298 | - return ($this->protect_mode === WPRProtect_V568::MODE_PREPEND); | |
| 299 | + return ($this->protect_mode === WPRProtect_V672::MODE_PREPEND); | |
| 299 | 300 | } |
| 300 | 301 | |
| 301 | 302 | private function isWPMode() { |
| 302 | - return ($this->protect_mode === WPRProtect_V568::MODE_WP); | |
| 303 | + return ($this->protect_mode === WPRProtect_V672::MODE_WP); | |
| 303 | 304 | } |
| 304 | 305 | |
| 305 | 306 | private function isModeDisabled() { |
| 306 | - return ($this->mode === WPRProtectFW_V568::MODE_DISABLED); | |
| 307 | + return ($this->mode === WPRProtectFW_V672::MODE_DISABLED); | |
| 307 | 308 | } |
| 308 | 309 | |
| 309 | 310 | private function isModeProtect() { |
| 310 | - return ($this->mode === WPRProtectFW_V568::MODE_PROTECT); | |
| 311 | + return ($this->mode === WPRProtectFW_V672::MODE_PROTECT); | |
| 311 | 312 | } |
| 312 | 313 | |
| 313 | 314 | private function isAdminCookieEnabled() { |
| 314 | - return ($this->admin_cookie_mode === WPRProtectFW_V568::ADMIN_COOKIE_MODE_ENABLED); | |
| 315 | + return ($this->admin_cookie_mode === WPRProtectFW_V672::ADMIN_COOKIE_MODE_ENABLED); | |
| 315 | 316 | } |
| 316 | 317 | |
| 317 | 318 | private function isIPCookieEnabled() { |
| 318 | - return ($this->ip_cookie_mode === WPRProtectFW_V568::IP_COOKIE_MODE_ENABLED); | |
| 319 | + return ($this->ip_cookie_mode === WPRProtectFW_V672::IP_COOKIE_MODE_ENABLED); | |
| 319 | 320 | } |
| 320 | 321 | |
| 321 | 322 | private function isRequestProfilingDisabled() { |
| 322 | - return ($this->request_profiling_mode === WPRProtectFW_V568::REQ_PROFILING_MODE_DISABLED); | |
| 323 | + return ($this->request_profiling_mode === WPRProtectFW_V672::REQ_PROFILING_MODE_DISABLED); | |
| 323 | 324 | } |
| 324 | 325 | |
| 325 | 326 | private function isRequestProfilingModeDebug() { |
| 326 | - return ($this->request_profiling_mode === WPRProtectFW_V568::REQ_PROFILING_MODE_DEBUG); | |
| 327 | + return ($this->request_profiling_mode === WPRProtectFW_V672::REQ_PROFILING_MODE_DEBUG); | |
| 327 | 328 | } |
| 328 | 329 | |
| 329 | 330 | private function isRequestHasValidBypassCookie() { |
| 330 | 331 | if (!isset($this->has_valid_bypass_cookie)) { |
| 331 | - $cookie = (string) $this->request->getCookies(WPRProtectFW_V568::BYPASS_COOKIE_NAME); | |
| 332 | + $cookie = (string) $this->request->getCookies(WPRProtectFW_V672::BYPASS_COOKIE_NAME); | |
| 332 | 333 | $new_cookie = $this->generateBypassCookie(); |
| 333 | 334 | $is_valid = ($this->isAdminCookieEnabled() && $new_cookie && ($cookie === $new_cookie)); |
| 334 | 335 | $this->has_valid_bypass_cookie = $is_valid; |
| 335 | 336 | } |
| @@ -337,17 +338,17 @@ | ||
| 337 | 338 | return $this->has_valid_bypass_cookie; |
| 338 | 339 | } |
| 339 | 340 | |
| 340 | 341 | private function isRulesModeProtect() { |
| 341 | - return ($this->rules_mode === WPRProtectFW_V568::RULES_MODE_PROTECT); | |
| 342 | + return ($this->rules_mode === WPRProtectFW_V672::RULES_MODE_PROTECT); | |
| 342 | 343 | } |
| 343 | 344 | |
| 344 | 345 | public function isLoggingModeComplete() { |
| 345 | - return ($this->logging_mode === WPRProtectFW_V568::LOGGING_MODE_COMPLETE); | |
| 346 | + return ($this->logging_mode === WPRProtectFW_V672::LOGGING_MODE_COMPLETE); | |
| 346 | 347 | } |
| 347 | 348 | |
| 348 | 349 | public function isLoggingModeVisitor() { |
| 349 | - return ($this->logging_mode === WPRProtectFW_V568::LOGGING_MODE_VISITOR); | |
| 350 | + return ($this->logging_mode === WPRProtectFW_V672::LOGGING_MODE_VISITOR); | |
| 350 | 351 | } |
| 351 | 352 | |
| 352 | 353 | public function isGeoBlockingEnabled() { |
| 353 | 354 | return ($this->is_geo_blocking === true); |
| @@ -353,13 +354,13 @@ | ||
| 353 | 354 | return ($this->is_geo_blocking === true); |
| 354 | 355 | } |
| 355 | 356 | |
| 356 | 357 | private function isWPFRuleInitModePrepend() { |
| 357 | - return ($this->wpf_rule_init_mode === WPRProtectFW_V568::WPF_RULE_INIT_MODE_PREPEND); | |
| 358 | + return ($this->wpf_rule_init_mode === WPRProtectFW_V672::WPF_RULE_INIT_MODE_PREPEND); | |
| 358 | 359 | } |
| 359 | 360 | |
| 360 | 361 | private function isWPFRuleInitModeWP() { |
| 361 | - return ($this->wpf_rule_init_mode === WPRProtectFW_V568::WPF_RULE_INIT_MODE_WP); | |
| 362 | + return ($this->wpf_rule_init_mode === WPRProtectFW_V672::WPF_RULE_INIT_MODE_WP); | |
| 362 | 363 | } |
| 363 | 364 | |
| 364 | 365 | private function canInitWPFRules() { |
| 365 | 366 | if (!$this->isWPFRuleInitModePrepend() && $this->isPrependMode()) { |
| @@ -369,9 +370,9 @@ | ||
| 369 | 370 | return true; |
| 370 | 371 | } |
| 371 | 372 | |
| 372 | 373 | private function generateBypassCookie() { |
| 373 | - $time = floor(time() / 43200); | |
| 374 | + $time = floor(time() / $this->cookie_validity); | |
| 374 | 375 | |
| 375 | 376 | return hash('sha256', $this->bypass_level . $time . $this->cookie_key); |
| 376 | 377 | } |
| 377 | 378 | |
| @@ -386,18 +387,18 @@ | ||
| 386 | 387 | if (function_exists('is_user_logged_in') && is_user_logged_in()) { |
| 387 | 388 | $current_wp_user = $this->getCurrentWPUser(); |
| 388 | 389 | |
| 389 | 390 | if (!$current_wp_user->isIdentical($this->request->wp_user)) { |
| 390 | - $serialized_wp_user = WPRProtectWPUser_V568::_serialize($current_wp_user); | |
| 391 | + $serialized_wp_user = WPRProtectWPUser_V672::_serialize($current_wp_user); | |
| 391 | 392 | $cookie_val = $serialized_wp_user . '_' . |
| 392 | - WPRProtectUtils_V568::signMessage($serialized_wp_user, $this->cookie_key); | |
| 393 | + WPRProtectUtils_V672::signMessage($serialized_wp_user, $this->cookie_key); | |
| 393 | 394 | $cookie_val = base64_encode($cookie_val); |
| 394 | 395 | |
| 395 | - $this->setCookie(WPRProtectWPUser_V568::COOKIE_NAME, $cookie_val); | |
| 396 | + $this->setCookie(WPRProtectWPUser_V672::COOKIE_NAME, $cookie_val); | |
| 396 | 397 | } |
| 397 | 398 | } elseif ($this->request->wp_user->isLoggedIn()) { |
| 398 | - $this->request->wp_user = WPRProtectWPUser_V568::defaultUser(); | |
| 399 | - $this->unsetCookie(WPRProtectWPUser_V568::COOKIE_NAME); | |
| 399 | + $this->request->wp_user = WPRProtectWPUser_V672::defaultUser(); | |
| 400 | + $this->unsetCookie(WPRProtectWPUser_V672::COOKIE_NAME); | |
| 400 | 401 | } |
| 401 | 402 | } |
| 402 | 403 | |
| 403 | 404 | private function getCurrentWPUser() { |
| @@ -403,9 +404,9 @@ | ||
| 403 | 404 | private function getCurrentWPUser() { |
| 404 | 405 | $id = 0; |
| 405 | 406 | $role_level = 0; |
| 406 | 407 | $capabilities = array(); |
| 407 | - $time = (int) floor(time() / 43200); | |
| 408 | + $time = (int) floor(time() / $this->cookie_validity); | |
| 408 | 409 | |
| 409 | 410 | if (function_exists('wp_get_current_user')) { |
| 410 | 411 | $user = wp_get_current_user(); |
| 411 | 412 | $id = $user->ID; |
| @@ -412,9 +413,9 @@ | ||
| 412 | 413 | $role_level = $this->getCurrentWPUserRoleLevel(); |
| 413 | 414 | $capabilities = $this->getCurrentWPUserCapabilities(); |
| 414 | 415 | } |
| 415 | 416 | |
| 416 | - return (new WPRProtectWPUser_V568($id, $role_level, $capabilities, $time)); | |
| 417 | + return (new WPRProtectWPUser_V672($id, $role_level, $capabilities, $time)); | |
| 417 | 418 | } |
| 418 | 419 | |
| 419 | 420 | private function getCurrentWPUserCapabilities() { |
| 420 | 421 | $capabilities = array(); |
| @@ -431,11 +432,11 @@ | ||
| 431 | 432 | return $capabilities; |
| 432 | 433 | } |
| 433 | 434 | |
| 434 | 435 | private function loadWPUser() { |
| 435 | - $this->request->wp_user = WPRProtectWPUser_V568::defaultUser(); | |
| 436 | + $this->request->wp_user = WPRProtectWPUser_V672::defaultUser(); | |
| 436 | 437 | |
| 437 | - $cookie_val = $this->request->getCookies(WPRProtectWPUser_V568::COOKIE_NAME); | |
| 438 | + $cookie_val = $this->request->getCookies(WPRProtectWPUser_V672::COOKIE_NAME); | |
| 438 | 439 | if (!is_string($cookie_val)) { |
| 439 | 440 | return; |
| 440 | 441 | } |
| 441 | 442 | |
| @@ -449,12 +450,12 @@ | ||
| 449 | 450 | return; |
| 450 | 451 | } |
| 451 | 452 | list($serialized_user, $signature) = $cookie_val_array; |
| 452 | 453 | |
| 453 | - if (WPRProtectUtils_V568::verifyMessage($serialized_user, $signature, $this->cookie_key) === true) { | |
| 454 | - $wp_user = WPRProtectWPUser_V568::_unserialize($serialized_user); | |
| 454 | + if (WPRProtectUtils_V672::verifyMessage($serialized_user, $signature, $this->cookie_key) === true) { | |
| 455 | + $wp_user = WPRProtectWPUser_V672::_unserialize($serialized_user); | |
| 455 | 456 | |
| 456 | - if (!isset($wp_user) || $wp_user->time !== (int) floor(time() / 43200)) { | |
| 457 | + if (!isset($wp_user) || $wp_user->time !== (int) floor(time() / $this->cookie_validity)) { | |
| 457 | 458 | return; |
| 458 | 459 | } |
| 459 | 460 | |
| 460 | 461 | $this->request->wp_user = $wp_user; |
| @@ -465,10 +466,10 @@ | ||
| 465 | 466 | $this->request->wp_user->capability_names[] = $capability_names[$capability]; |
| 466 | 467 | } |
| 467 | 468 | } |
| 468 | 469 | |
| 469 | - $role_by_level = array_flip(array_merge(WPRProtectFW_V568::DEFAULT_WP_USER_ROLE_LEVELS, | |
| 470 | - WPRProtectFW_V568::EXTRA_WP_USER_ROLE_LEVELS)); | |
| 470 | + $role_by_level = array_flip(array_merge(WPRProtectFW_V672::DEFAULT_WP_USER_ROLE_LEVELS, | |
| 471 | + WPRProtectFW_V672::EXTRA_WP_USER_ROLE_LEVELS)); | |
| 471 | 472 | $this->request->wp_user->role = $role_by_level[$this->request->wp_user->role_level]; |
| 472 | 473 | } |
| 473 | 474 | } |
| 474 | 475 | |
| @@ -490,11 +491,11 @@ | ||
| 490 | 491 | } |
| 491 | 492 | |
| 492 | 493 | if ($this->isPrependMode()) { |
| 493 | 494 | $rules_file = MCDATAPATH . MCCONFKEY . '-' . 'mc_rules.json'; |
| 494 | - $rule_arrays = WPRProtectUtils_V568::parseFile($rules_file); | |
| 495 | + $rule_arrays = WPRProtectUtils_V672::parseFile($rules_file); | |
| 495 | 496 | } else { |
| 496 | - $rule_arrays = WPRProtect_V568::$settings->getOption('bvruleset'); | |
| 497 | + $rule_arrays = WPRProtect_V672::$settings->getOption('bvruleset'); | |
| 497 | 498 | if(!is_array($rule_arrays)) { |
| 498 | 499 | $rule_arrays = array(); |
| 499 | 500 | } |
| 500 | 501 | } |
| @@ -504,9 +505,9 @@ | ||
| 504 | 505 | return; |
| 505 | 506 | } |
| 506 | 507 | |
| 507 | 508 | foreach($rule_arrays as $rule_array) { |
| 508 | - $rule = WPRProtectFWRule_V568::init($rule_array); | |
| 509 | + $rule = WPRProtectFWRule_V672::init($rule_array); | |
| 509 | 510 | |
| 510 | 511 | if ($rule) { |
| 511 | 512 | if (!$this->is_rule_initialized && $rule->isExeOnBoot()) { |
| 512 | 513 | if (!$this->isRequestHasValidBypassCookie()) { |
| @@ -528,40 +529,108 @@ | ||
| 528 | 529 | $this->rules[] = $rule; |
| 529 | 530 | } |
| 530 | 531 | |
| 531 | 532 | private function initWPFRule($rule) { |
| 532 | - if ($rule->isExeOnPreUpdateOption()) { | |
| 533 | + switch ($rule->execute_on) { | |
| 534 | + case WPRProtectFWRule_V672::EXE_ON_PRE_UPDATE_OPTION: | |
| 533 | 535 | $this->addWPHook($rule, 'pre_update_option', 'handleRequestOnPreUpdateOption', 3); |
| 534 | - } elseif ($rule->isExeOnPreDeletePost()) { | |
| 536 | + break; | |
| 537 | + case WPRProtectFWRule_V672::EXE_ON_PRE_DELETE_POST: | |
| 535 | 538 | $this->addWPHook($rule, 'pre_delete_post', 'handleRequestOnPreDeletePost', 3); |
| 536 | - } elseif ($rule->isExeOnWPInsertPostEmptyContent()) { | |
| 537 | - $this->addWPHook($rule, 'wp_insert_post_empty_content', 'handleRequestOnWPInsertPostEmptyContent', 2); | |
| 538 | - } elseif ($rule->isExeOnInsertUserMeta()) { | |
| 539 | + break; | |
| 540 | + case WPRProtectFWRule_V672::EXE_ON_WP_INSERT_POST_EMPTY_CONTENT: | |
| 541 | + $this->addWPHook($rule, 'wp_insert_post_empty_content', | |
| 542 | + 'handleRequestOnWPInsertPostEmptyContent', 2); | |
| 543 | + break; | |
| 544 | + case WPRProtectFWRule_V672::EXE_ON_INSERT_USER_META: | |
| 539 | 545 | $this->addWPHook($rule, 'insert_user_meta', 'handleRequestOnInsertUserMeta', 4); |
| 540 | - } elseif ($rule->isExeOnDeleteOption()) { | |
| 546 | + break; | |
| 547 | + case WPRProtectFWRule_V672::EXE_ON_DELETE_OPTION: | |
| 541 | 548 | $this->addWPHook($rule, 'delete_option', 'handleRequestOnDeleteOption', 1, 'action'); |
| 542 | - } elseif ($rule->isExeOnDeleteUser()) { | |
| 549 | + break; | |
| 550 | + case WPRProtectFWRule_V672::EXE_ON_DELETE_USER: | |
| 543 | 551 | $this->addWPHook($rule, 'delete_user', 'handleRequestOnDeleteUser', 3, 'action'); |
| 544 | - } elseif ($rule->isExeOnPasswordReset()) { | |
| 552 | + break; | |
| 553 | + case WPRProtectFWRule_V672::EXE_ON_PASSWORD_RESET: | |
| 545 | 554 | $this->addWPHook($rule, 'password_reset', 'handleRequestOnPasswordReset', 2, 'action'); |
| 546 | - } elseif ($rule->isExeOnSendAuthCookies()) { | |
| 555 | + break; | |
| 556 | + case WPRProtectFWRule_V672::EXE_ON_SEND_AUTH_COOKIES: | |
| 547 | 557 | $this->addWPHook($rule, 'send_auth_cookies', 'handleRequestOnSendAuthCookies', 6); |
| 548 | - } elseif ($rule->isExeOnSetAuthCookie()) { | |
| 558 | + break; | |
| 559 | + case WPRProtectFWRule_V672::EXE_ON_SET_AUTH_COOKIE: | |
| 549 | 560 | $this->addWPHook($rule, 'set_auth_cookie', 'handleRequestOnSetAuthCookie', 6, 'action'); |
| 550 | - } elseif ($rule->isExeOnInit()) { | |
| 561 | + break; | |
| 562 | + case WPRProtectFWRule_V672::EXE_ON_INIT: | |
| 551 | 563 | $this->addWPHook($rule, 'init', 'handleRequestOnInit', 0, 'action'); |
| 552 | - } elseif ($rule->isExeOnUserRegister()) { | |
| 564 | + break; | |
| 565 | + case WPRProtectFWRule_V672::EXE_ON_USER_REGISTER: | |
| 553 | 566 | $this->addWPHook($rule, 'user_register', 'handleRequestOnUserRegister', 2, 'action'); |
| 554 | - } elseif ($rule->isExeOnAddUserMeta()) { | |
| 567 | + break; | |
| 568 | + case WPRProtectFWRule_V672::EXE_ON_ADD_USER_META: | |
| 555 | 569 | $this->addWPHook($rule, 'add_user_meta', 'handleRequestOnAddUserMeta', 3, 'action'); |
| 556 | - } elseif ($rule->isExeOnUpdateUserMetadata()) { | |
| 570 | + break; | |
| 571 | + case WPRProtectFWRule_V672::EXE_ON_UPDATE_USER_METADATA: | |
| 557 | 572 | $this->addWPHook($rule, 'update_user_metadata', 'handleRequestOnUpdateUserMetadata', 5); |
| 558 | - } elseif ($rule->isExeOnUpdateUserMeta()) { | |
| 573 | + break; | |
| 574 | + case WPRProtectFWRule_V672::EXE_ON_UPDATE_USER_META: | |
| 559 | 575 | $this->addWPHook($rule, 'update_user_meta', 'handleRequestOnUpdateUserMeta', 4, 'action'); |
| 560 | - } elseif ($rule->isExeOnAddOption()) { | |
| 576 | + break; | |
| 577 | + case WPRProtectFWRule_V672::EXE_ON_ADD_OPTION: | |
| 561 | 578 | $this->addWPHook($rule, 'add_option', 'handleRequestOnAddOption', 2, 'action'); |
| 562 | - } elseif ($rule->isExeOnWPPreInsertUserData()) { | |
| 579 | + break; | |
| 580 | + case WPRProtectFWRule_V672::EXE_ON_WP_PRE_INSERT_USER_DATA: | |
| 563 | 581 | $this->addWPHook($rule, 'wp_pre_insert_user_data', 'handleRequestOnWPPreInsertUserData', 4); |
| 582 | + break; | |
| 583 | + case WPRProtectFWRule_V672::EXE_ON_REST_REQUEST_BEFORE_CALLBACKS: | |
| 584 | + $this->addWPHook($rule, 'rest_request_before_callbacks', | |
| 585 | + 'handleRequestOnRestRequestBeforeCallbacks', 3); | |
| 586 | + break; | |
| 587 | + case WPRProtectFWRule_V672::EXE_ON_ADMIN_INIT: | |
| 588 | + $this->addWPHook($rule, 'admin_init', 'handleRequestOnAdminInit', 0, 'action'); | |
| 589 | + break; | |
| 590 | + case WPRProtectFWRule_V672::EXE_ON_WP_HANDLE_UPLOAD_PREFILTER: | |
| 591 | + $this->addWPHook($rule, 'wp_handle_upload_prefilter', | |
| 592 | + 'handleRequestOnWPHandleUploadPrefilter', 1); | |
| 593 | + break; | |
| 594 | + case WPRProtectFWRule_V672::EXE_ON_TEMPLATE_REDIRECT: | |
| 595 | + $this->addWPHook($rule, 'template_redirect', 'handleRequestOnTemplateRedirect', 0, 'action'); | |
| 596 | + break; | |
| 597 | + case WPRProtectFWRule_V672::EXE_ON_WP_LOADED: | |
| 598 | + $this->addWPHook($rule, 'wp_loaded', 'handleRequestOnWPLoaded', 0, 'action'); | |
| 599 | + break; | |
| 600 | + case WPRProtectFWRule_V672::EXE_ON_ADD_POST_METADATA: | |
| 601 | + $this->addWPHook($rule, 'add_post_metadata', 'handleRequestOnAddPostMetadata', 5); | |
| 602 | + break; | |
| 603 | + case WPRProtectFWRule_V672::EXE_ON_UPDATE_POST_METADATA: | |
| 604 | + $this->addWPHook($rule, 'update_post_metadata', 'handleRequestOnUpdatePostMetadata', 5); | |
| 605 | + break; | |
| 606 | + case WPRProtectFWRule_V672::EXE_ON_DELETE_POST_METADATA: | |
| 607 | + $this->addWPHook($rule, 'delete_post_metadata', 'handleRequestOnDeletePostMetadata', 5); | |
| 608 | + break; | |
| 609 | + case WPRProtectFWRule_V672::EXE_ON_ADD_TERM_METADATA: | |
| 610 | + $this->addWPHook($rule, 'add_term_metadata', 'handleRequestOnAddTermMetadata', 5); | |
| 611 | + break; | |
| 612 | + case WPRProtectFWRule_V672::EXE_ON_UPDATE_TERM_METADATA: | |
| 613 | + $this->addWPHook($rule, 'update_term_metadata', 'handleRequestOnUpdateTermMetadata', 5); | |
| 614 | + break; | |
| 615 | + case WPRProtectFWRule_V672::EXE_ON_DELETE_TERM_METADATA: | |
| 616 | + $this->addWPHook($rule, 'delete_term_metadata', 'handleRequestOnDeleteTermMetadata', 5); | |
| 617 | + break; | |
| 618 | + case WPRProtectFWRule_V672::EXE_ON_ADD_COMMENT_METADATA: | |
| 619 | + $this->addWPHook($rule, 'add_comment_metadata', 'handleRequestOnAddCommentMetadata', 5); | |
| 620 | + break; | |
| 621 | + case WPRProtectFWRule_V672::EXE_ON_UPDATE_COMMENT_METADATA: | |
| 622 | + $this->addWPHook($rule, 'update_comment_metadata', 'handleRequestOnUpdateCommentMetadata', 5); | |
| 623 | + break; | |
| 624 | + case WPRProtectFWRule_V672::EXE_ON_DELETE_COMMENT_METADATA: | |
| 625 | + $this->addWPHook($rule, 'delete_comment_metadata', 'handleRequestOnDeleteCommentMetadata', 5); | |
| 626 | + break; | |
| 627 | + case WPRProtectFWRule_V672::EXE_ON_ADD_USER_METADATA: | |
| 628 | + $this->addWPHook($rule, 'add_user_metadata', 'handleRequestOnAddUserMetadata', 5); | |
| 629 | + break; | |
| 630 | + case WPRProtectFWRule_V672::EXE_ON_DELETE_USER_METADATA: | |
| 631 | + $this->addWPHook($rule, 'delete_user_metadata', 'handleRequestOnDeleteUserMetadata', 5); | |
| 632 | + break; | |
| 564 | 633 | } |
| 565 | 634 | } |
| 566 | 635 | |
| 567 | 636 | private function addWPHook($rule, $hook_name, $function_name, $accepted_args, $hook_type = 'filter') { |
| @@ -575,9 +644,9 @@ | ||
| 575 | 644 | } else { |
| 576 | 645 | add_filter($hook_name, $callback, -9999999, $accepted_args); |
| 577 | 646 | } |
| 578 | 647 | } else { |
| 579 | - WPRProtectUtils_V568::preInitWPHook($hook_name, $callback, -9999999, $accepted_args); | |
| 648 | + WPRProtectUtils_V672::preInitWPHook($hook_name, $callback, -9999999, $accepted_args); | |
| 580 | 649 | } |
| 581 | 650 | } |
| 582 | 651 | |
| 583 | 652 | $this->pushWPFRule($function_name, $rule); |
| @@ -900,8 +969,199 @@ | ||
| 900 | 969 | $this->handleRequestOnRuleMatch($rules, $variables, $log_data); |
| 901 | 970 | } |
| 902 | 971 | } |
| 903 | 972 | |
| 973 | + private function handleRequestOnLifecycleHook($function_name, $hook_name) { | |
| 974 | + $rules = $this->getWPFRules($function_name); | |
| 975 | + | |
| 976 | + if (!empty($rules)) { | |
| 977 | + $variables = $this->getLifecycleHookVariables($hook_name); | |
| 978 | + $this->handleRequestOnRuleMatch($rules, $variables, $variables); | |
| 979 | + } | |
| 980 | + } | |
| 981 | + | |
| 982 | + private function getLifecycleHookVariables($hook_name) { | |
| 983 | + $variables = array('hook' => $hook_name); | |
| 984 | + | |
| 985 | + if (function_exists('is_admin')) { | |
| 986 | + $variables['is_admin'] = is_admin(); | |
| 987 | + } | |
| 988 | + | |
| 989 | + if (function_exists('wp_doing_ajax')) { | |
| 990 | + $variables['is_ajax'] = wp_doing_ajax(); | |
| 991 | + } else { | |
| 992 | + $variables['is_ajax'] = (defined('DOING_AJAX') && DOING_AJAX); | |
| 993 | + } | |
| 994 | + | |
| 995 | + $variables['is_cron'] = (defined('DOING_CRON') && DOING_CRON); | |
| 996 | + $variables['is_rest_request'] = (defined('REST_REQUEST') && REST_REQUEST); | |
| 997 | + | |
| 998 | + return $variables; | |
| 999 | + } | |
| 1000 | + | |
| 1001 | + public function handleRequestOnAdminInit() { | |
| 1002 | + $this->handleRequestOnLifecycleHook('handleRequestOnAdminInit', 'admin_init'); | |
| 1003 | + } | |
| 1004 | + | |
| 1005 | + public function handleRequestOnTemplateRedirect() { | |
| 1006 | + $this->handleRequestOnLifecycleHook('handleRequestOnTemplateRedirect', 'template_redirect'); | |
| 1007 | + } | |
| 1008 | + | |
| 1009 | + public function handleRequestOnWPLoaded() { | |
| 1010 | + $this->handleRequestOnLifecycleHook('handleRequestOnWPLoaded', 'wp_loaded'); | |
| 1011 | + } | |
| 1012 | + | |
| 1013 | + public function handleRequestOnRestRequestBeforeCallbacks($response, $handler, $request) { | |
| 1014 | + $rules = $this->getWPFRules('handleRequestOnRestRequestBeforeCallbacks'); | |
| 1015 | + | |
| 1016 | + if (!empty($rules)) { | |
| 1017 | + $this->handleRequestOnRuleMatch($rules); | |
| 1018 | + } | |
| 1019 | + | |
| 1020 | + return $response; | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + private function getSafeUploadFileData($file) { | |
| 1024 | + $data = array(); | |
| 1025 | + | |
| 1026 | + if (!is_array($file)) { | |
| 1027 | + return $data; | |
| 1028 | + } | |
| 1029 | + | |
| 1030 | + foreach (array('name', 'type', 'size', 'error') as $key) { | |
| 1031 | + if (array_key_exists($key, $file) && (is_scalar($file[$key]) || is_null($file[$key]))) { | |
| 1032 | + $data[$key] = $file[$key]; | |
| 1033 | + } | |
| 1034 | + } | |
| 1035 | + | |
| 1036 | + if (array_key_exists('name', $data) && is_string($data['name'])) { | |
| 1037 | + $data['extension'] = strtolower(pathinfo($data['name'], PATHINFO_EXTENSION)); | |
| 1038 | + } | |
| 1039 | + | |
| 1040 | + return $data; | |
| 1041 | + } | |
| 1042 | + | |
| 1043 | + private function getMetadataValueLogData($key, $value) { | |
| 1044 | + $data = array( | |
| 1045 | + $key . '_type' => gettype($value), | |
| 1046 | + $key . '_size' => null | |
| 1047 | + ); | |
| 1048 | + | |
| 1049 | + if (is_null($value)) { | |
| 1050 | + $data[$key . '_size'] = 0; | |
| 1051 | + } elseif (is_scalar($value)) { | |
| 1052 | + $data[$key . '_size'] = strlen((string) $value); | |
| 1053 | + } elseif (is_array($value)) { | |
| 1054 | + $data[$key . '_count'] = count($value); | |
| 1055 | + } elseif (is_object($value)) { | |
| 1056 | + $data[$key . '_count'] = count(get_object_vars($value)); | |
| 1057 | + } | |
| 1058 | + | |
| 1059 | + return $data; | |
| 1060 | + } | |
| 1061 | + | |
| 1062 | + public function handleRequestOnWPHandleUploadPrefilter($file) { | |
| 1063 | + $rules = $this->getWPFRules('handleRequestOnWPHandleUploadPrefilter'); | |
| 1064 | + | |
| 1065 | + if (!empty($rules)) { | |
| 1066 | + $safe_file = $this->getSafeUploadFileData($file); | |
| 1067 | + $variables = array('file' => $safe_file); | |
| 1068 | + $this->handleRequestOnRuleMatch($rules, $variables, $variables); | |
| 1069 | + } | |
| 1070 | + | |
| 1071 | + return $file; | |
| 1072 | + } | |
| 1073 | + | |
| 1074 | + private function handleRequestOnMetadataFilter($function_name, $meta_type, $operation, $check, | |
| 1075 | + $object_id, $meta_key, $meta_value, $extra = array()) { | |
| 1076 | + | |
| 1077 | + $rules = $this->getWPFRules($function_name); | |
| 1078 | + | |
| 1079 | + if (!empty($rules)) { | |
| 1080 | + $variables = array_merge(array( | |
| 1081 | + 'check' => $check, | |
| 1082 | + 'meta_type' => $meta_type, | |
| 1083 | + 'operation' => $operation, | |
| 1084 | + 'object_id' => $object_id, | |
| 1085 | + 'meta_key' => $meta_key, | |
| 1086 | + 'meta_value' => $meta_value | |
| 1087 | + ), $extra); | |
| 1088 | + | |
| 1089 | + $log_extra = $extra; | |
| 1090 | + if (array_key_exists('prev_value', $log_extra)) { | |
| 1091 | + $prev_value = $log_extra['prev_value']; | |
| 1092 | + unset($log_extra['prev_value']); | |
| 1093 | + $log_extra = array_merge($log_extra, $this->getMetadataValueLogData('prev_value', $prev_value)); | |
| 1094 | + } | |
| 1095 | + | |
| 1096 | + $log_data = array_merge(array( | |
| 1097 | + 'meta_type' => $meta_type, | |
| 1098 | + 'operation' => $operation, | |
| 1099 | + 'object_id' => $object_id, | |
| 1100 | + 'meta_key' => $meta_key | |
| 1101 | + ), $this->getMetadataValueLogData('meta_value', $meta_value), $log_extra); | |
| 1102 | + | |
| 1103 | + $this->handleRequestOnRuleMatch($rules, $variables, $log_data); | |
| 1104 | + } | |
| 1105 | + | |
| 1106 | + return $check; | |
| 1107 | + } | |
| 1108 | + | |
| 1109 | + public function handleRequestOnAddPostMetadata($check, $object_id, $meta_key, $meta_value, $unique) { | |
| 1110 | + return $this->handleRequestOnMetadataFilter('handleRequestOnAddPostMetadata', | |
| 1111 | + 'post', 'add', $check, $object_id, $meta_key, $meta_value, array('unique' => $unique)); | |
| 1112 | + } | |
| 1113 | + | |
| 1114 | + public function handleRequestOnUpdatePostMetadata($check, $object_id, $meta_key, $meta_value, $prev_value) { | |
| 1115 | + return $this->handleRequestOnMetadataFilter('handleRequestOnUpdatePostMetadata', | |
| 1116 | + 'post', 'update', $check, $object_id, $meta_key, $meta_value, array('prev_value' => $prev_value)); | |
| 1117 | + } | |
| 1118 | + | |
| 1119 | + public function handleRequestOnDeletePostMetadata($check, $object_id, $meta_key, $meta_value, $delete_all) { | |
| 1120 | + return $this->handleRequestOnMetadataFilter('handleRequestOnDeletePostMetadata', | |
| 1121 | + 'post', 'delete', $check, $object_id, $meta_key, $meta_value, array('delete_all' => $delete_all)); | |
| 1122 | + } | |
| 1123 | + | |
| 1124 | + public function handleRequestOnAddTermMetadata($check, $object_id, $meta_key, $meta_value, $unique) { | |
| 1125 | + return $this->handleRequestOnMetadataFilter('handleRequestOnAddTermMetadata', | |
| 1126 | + 'term', 'add', $check, $object_id, $meta_key, $meta_value, array('unique' => $unique)); | |
| 1127 | + } | |
| 1128 | + | |
| 1129 | + public function handleRequestOnUpdateTermMetadata($check, $object_id, $meta_key, $meta_value, $prev_value) { | |
| 1130 | + return $this->handleRequestOnMetadataFilter('handleRequestOnUpdateTermMetadata', | |
| 1131 | + 'term', 'update', $check, $object_id, $meta_key, $meta_value, array('prev_value' => $prev_value)); | |
| 1132 | + } | |
| 1133 | + | |
| 1134 | + public function handleRequestOnDeleteTermMetadata($check, $object_id, $meta_key, $meta_value, $delete_all) { | |
| 1135 | + return $this->handleRequestOnMetadataFilter('handleRequestOnDeleteTermMetadata', | |
| 1136 | + 'term', 'delete', $check, $object_id, $meta_key, $meta_value, array('delete_all' => $delete_all)); | |
| 1137 | + } | |
| 1138 | + | |
| 1139 | + public function handleRequestOnAddCommentMetadata($check, $object_id, $meta_key, $meta_value, $unique) { | |
| 1140 | + return $this->handleRequestOnMetadataFilter('handleRequestOnAddCommentMetadata', | |
| 1141 | + 'comment', 'add', $check, $object_id, $meta_key, $meta_value, array('unique' => $unique)); | |
| 1142 | + } | |
| 1143 | + | |
| 1144 | + public function handleRequestOnUpdateCommentMetadata($check, $object_id, $meta_key, $meta_value, $prev_value) { | |
| 1145 | + return $this->handleRequestOnMetadataFilter('handleRequestOnUpdateCommentMetadata', | |
| 1146 | + 'comment', 'update', $check, $object_id, $meta_key, $meta_value, array('prev_value' => $prev_value)); | |
| 1147 | + } | |
| 1148 | + | |
| 1149 | + public function handleRequestOnDeleteCommentMetadata($check, $object_id, $meta_key, $meta_value, $delete_all) { | |
| 1150 | + return $this->handleRequestOnMetadataFilter('handleRequestOnDeleteCommentMetadata', | |
| 1151 | + 'comment', 'delete', $check, $object_id, $meta_key, $meta_value, array('delete_all' => $delete_all)); | |
| 1152 | + } | |
| 1153 | + | |
| 1154 | + public function handleRequestOnAddUserMetadata($check, $object_id, $meta_key, $meta_value, $unique) { | |
| 1155 | + return $this->handleRequestOnMetadataFilter('handleRequestOnAddUserMetadata', | |
| 1156 | + 'user', 'add', $check, $object_id, $meta_key, $meta_value, array('unique' => $unique)); | |
| 1157 | + } | |
| 1158 | + | |
| 1159 | + public function handleRequestOnDeleteUserMetadata($check, $object_id, $meta_key, $meta_value, $delete_all) { | |
| 1160 | + return $this->handleRequestOnMetadataFilter('handleRequestOnDeleteUserMetadata', | |
| 1161 | + 'user', 'delete', $check, $object_id, $meta_key, $meta_value, array('delete_all' => $delete_all)); | |
| 1162 | + } | |
| 1163 | + | |
| 904 | 1164 | private function setShutdownCallback() { |
| 905 | 1165 | if (!$this->is_shutdown_cb_set) { |
| 906 | 1166 | register_shutdown_function(array($this, 'log')); |
| 907 | 1167 | $this->is_shutdown_cb_set = true; |
| @@ -942,14 +1202,14 @@ | ||
| 942 | 1202 | } |
| 943 | 1203 | |
| 944 | 1204 | private function setIPCookie() { |
| 945 | 1205 | if (!$this->is_ip_cookie_set && $this->isIPCookieEnabled() && |
| 946 | - !$this->request->getCookies(WPRProtectFW_V568::IP_COOKIE_NAME)) { | |
| 1206 | + !$this->request->getCookies(WPRProtectFW_V672::IP_COOKIE_NAME)) { | |
| 947 | 1207 | |
| 948 | 1208 | $time = floor(time() / 86400); |
| 949 | 1209 | $cookie = hash('sha256', $this->request->ip . $time . $this->cookie_key); |
| 950 | 1210 | if ($cookie) { |
| 951 | - $this->setCookie(WPRProtectFW_V568::IP_COOKIE_NAME, $cookie, time() + 86400); | |
| 1211 | + $this->setCookie(WPRProtectFW_V672::IP_COOKIE_NAME, $cookie, time() + 86400); | |
| 952 | 1212 | } |
| 953 | 1213 | } |
| 954 | 1214 | } |
| 955 | 1215 | |
| @@ -955,18 +1215,18 @@ | ||
| 955 | 1215 | |
| 956 | 1216 | private function getCurrentWPUserRoleLevel() { |
| 957 | 1217 | if (function_exists('current_user_can')) { |
| 958 | 1218 | if (function_exists('is_super_admin') && is_super_admin()) { |
| 959 | - return WPRProtectFW_V568::WP_USER_ROLE_LEVEL_ADMIN; | |
| 1219 | + return WPRProtectFW_V672::WP_USER_ROLE_LEVEL_ADMIN; | |
| 960 | 1220 | } |
| 961 | 1221 | |
| 962 | 1222 | foreach ($this->custom_roles as $role) { |
| 963 | 1223 | if (current_user_can($role)) { |
| 964 | - return WPRProtectFW_V568::WP_USER_ROLE_LEVEL_CUSTOM; | |
| 1224 | + return WPRProtectFW_V672::WP_USER_ROLE_LEVEL_CUSTOM; | |
| 965 | 1225 | } |
| 966 | 1226 | } |
| 967 | 1227 | |
| 968 | - foreach (WPRProtectFW_V568::DEFAULT_WP_USER_ROLE_LEVELS as $role => $level) { | |
| 1228 | + foreach (WPRProtectFW_V672::DEFAULT_WP_USER_ROLE_LEVELS as $role => $level) { | |
| 969 | 1229 | if (current_user_can($role)) { |
| 970 | 1230 | return $level; |
| 971 | 1231 | } |
| 972 | 1232 | } |
| @@ -1072,11 +1332,11 @@ | ||
| 1072 | 1332 | |
| 1073 | 1333 | private function getBVCookies() { |
| 1074 | 1334 | $cookies = array(); |
| 1075 | 1335 | |
| 1076 | - if ($this->request->getCookies(WPRProtectFW_V568::IP_COOKIE_NAME) !== NULL) { | |
| 1077 | - $cookie_val = (string) $this->request->getCookies(WPRProtectFW_V568::IP_COOKIE_NAME); | |
| 1078 | - $cookies[WPRProtectFW_V568::IP_COOKIE_NAME] = $cookie_val; | |
| 1336 | + if ($this->request->getCookies(WPRProtectFW_V672::IP_COOKIE_NAME) !== NULL) { | |
| 1337 | + $cookie_val = (string) $this->request->getCookies(WPRProtectFW_V672::IP_COOKIE_NAME); | |
| 1338 | + $cookies[WPRProtectFW_V672::IP_COOKIE_NAME] = $cookie_val; | |
| 1079 | 1339 | } |
| 1080 | 1340 | |
| 1081 | 1341 | return $cookies; |
| 1082 | 1342 | } |
| @@ -1228,13 +1488,19 @@ | ||
| 1228 | 1488 | $this->updateRuleLog('inspect', "cookies", $this->getCookiesToLog($this->request->getCookies())); |
| 1229 | 1489 | $this->updateRuleLog('inspect', "getParams", $this->request->getGetParams()); |
| 1230 | 1490 | $this->updateRuleLog('inspect', "postParams", $this->getParamsToLog($this->request->getPostParams(), "POST")); |
| 1231 | 1491 | $this->updateRuleLog('inspect', "jsonParams", $this->getParamsToLog($this->request->getJsonParams(), "JSON")); |
| 1232 | - $this->updateRuleLog('inspect', "rawBody", $this->getRawBodyToLog($this->request->getRawBody())); | |
| 1492 | + $this->updateRuleLog('inspect', "bodyParserStatus", $this->request->getBodyParserStatus()); | |
| 1493 | + if ($this->can_log_raw_body) { | |
| 1494 | + $raw_body = $this->request->getRawBody(); | |
| 1495 | + if (!empty($raw_body)) { | |
| 1496 | + $this->updateRuleLog('inspect', "rawBody", $this->getRawBodyToLog($raw_body)); | |
| 1497 | + } | |
| 1498 | + } | |
| 1233 | 1499 | } |
| 1234 | 1500 | |
| 1235 | 1501 | private function getUserBy($attribute, $value) { |
| 1236 | - if (isset($value) && function_exists('get_user_by') && WPRProtectUtils_V568::havePluginsLoaded()) { | |
| 1502 | + if (isset($value) && function_exists('get_user_by') && WPRProtectUtils_V672::havePluginsLoaded()) { | |
| 1237 | 1503 | return get_user_by($attribute, $value); |
| 1238 | 1504 | } |
| 1239 | 1505 | } |
| 1240 | 1506 | |
| @@ -1336,9 +1602,9 @@ | ||
| 1336 | 1602 | xlsx|psd|mp3|m4a|ogg|wav|mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2|php|html|phtml|js|css)/ix', $value)) { |
| 1337 | 1603 | $profiled_data[$key]["file"] = true; |
| 1338 | 1604 | } |
| 1339 | 1605 | |
| 1340 | - if ($this->matchCount(WPRProtectFWRule_V568::SQLIREGEX, $value) > 2) { | |
| 1606 | + if ($this->matchCount(WPRProtectFWRule_V672::SQLIREGEX, $value) > 2) { | |
| 1341 | 1607 | $profiled_data[$key]["sql"] = true; |
| 1342 | 1608 | } |
| 1343 | 1609 | |
| 1344 | 1610 | if (WPRHelper::safePregMatch('/(?:\.{2}[\/]+)/', $value)) { |
| @@ -1404,8 +1670,14 @@ | ||
| 1404 | 1670 | if ($this->can_log_raw_body && $is_debug_mode && $log_raw_body && !empty($this->request->getRawBody())) { |
| 1405 | 1671 | $profiled_data += array("RAW_BODY" => $this->getRawBodyToLog($this->request->getRawBody())); |
| 1406 | 1672 | } |
| 1407 | 1673 | |
| 1674 | + $body_parser_status = $this->request->getBodyParserStatus(); | |
| 1675 | + $profiled_data = array( | |
| 1676 | + "BODY_RAW_STATUS" => $body_parser_status['raw_body_status'], | |
| 1677 | + "JSON_PARAMS_STATUS" => $body_parser_status['json_params_status'] | |
| 1678 | + ) + $profiled_data; | |
| 1679 | + | |
| 1408 | 1680 | $this->request_profiled_data = $profiled_data; |
| 1409 | 1681 | $this->is_request_profiled = true; |
| 1410 | 1682 | } |
| 1411 | 1683 | } |
| @@ -1410,20 +1682,24 @@ | ||
| 1410 | 1682 | } |
| 1411 | 1683 | } |
| 1412 | 1684 | |
| 1413 | 1685 | private function isRequestIPWhitelisted() { |
| 1414 | - return $this->ipstore->isFWIPWhitelisted($this->request->ip); | |
| 1686 | + if (!isset($this->is_ip_whitelisted)) { | |
| 1687 | + $this->is_ip_whitelisted = $this->ipstore->isFWIPWhitelisted($this->request->ip); | |
| 1688 | + } | |
| 1689 | + | |
| 1690 | + return $this->is_ip_whitelisted; | |
| 1415 | 1691 | } |
| 1416 | 1692 | |
| 1417 | 1693 | private function canRequestBypassFirewall() { |
| 1418 | 1694 | if ($this->isRequestIPWhitelisted() || $this->isRequestHasValidBypassCookie()) { |
| 1419 | - $this->request->category = WPRProtectRequest_V568::CATEGORY_WHITELISTED; | |
| 1420 | - $this->request->status = WPRProtectRequest_V568::STATUS_BYPASSED; | |
| 1695 | + $this->request->category = WPRProtectRequest_V672::CATEGORY_WHITELISTED; | |
| 1696 | + $this->request->status = WPRProtectRequest_V672::STATUS_BYPASSED; | |
| 1421 | 1697 | |
| 1422 | 1698 | return true; |
| 1423 | - } elseif (WPRProtectUtils_V568::isPrivateIP($this->request->ip)) { | |
| 1424 | - $this->request->category = WPRProtectRequest_V568::CATEGORY_PRIVATEIP; | |
| 1425 | - $this->request->status = WPRProtectRequest_V568::STATUS_BYPASSED; | |
| 1699 | + } elseif (WPRProtectUtils_V672::isPrivateIP($this->request->ip)) { | |
| 1700 | + $this->request->category = WPRProtectRequest_V672::CATEGORY_PRIVATEIP; | |
| 1701 | + $this->request->status = WPRProtectRequest_V672::STATUS_BYPASSED; | |
| 1426 | 1702 | |
| 1427 | 1703 | return true; |
| 1428 | 1704 | } |
| 1429 | 1705 | |
| @@ -1445,19 +1721,22 @@ | ||
| 1445 | 1721 | } |
| 1446 | 1722 | } |
| 1447 | 1723 | |
| 1448 | 1724 | private function handleRequestOnRuleMatch($rules, $engine_vars = array(), $log_data = array()) { |
| 1725 | + $normalized_engine_vars = WPRProtectFWRuleEngine_V672::normalizeVariables($engine_vars); | |
| 1726 | + | |
| 1449 | 1727 | foreach ($rules as $rule) { |
| 1450 | 1728 | if ($this->break_rule_matching) { |
| 1451 | 1729 | break; |
| 1452 | 1730 | } |
| 1453 | 1731 | |
| 1454 | - $_engine_vars = $engine_vars; | |
| 1732 | + $_engine_vars = $normalized_engine_vars; | |
| 1455 | 1733 | if (array_key_exists('variables', $rule->opts)) { |
| 1456 | - $_engine_vars = array_merge($_engine_vars, $rule->opts['variables']); | |
| 1734 | + $_engine_vars = array_merge($_engine_vars, | |
| 1735 | + WPRProtectFWRuleEngine_V672::normalizeVariables($rule->opts['variables'])); | |
| 1457 | 1736 | } |
| 1458 | 1737 | |
| 1459 | - $rule_engine = new WPRProtectFWRuleEngine_V568($this->request, $_engine_vars); | |
| 1738 | + $rule_engine = new WPRProtectFWRuleEngine_V672($this->request, $_engine_vars); | |
| 1460 | 1739 | |
| 1461 | 1740 | if ($rule_engine->evaluate($rule) && !$rule_engine->hasError()) { |
| 1462 | 1741 | if (!empty($log_data)) { |
| 1463 | 1742 | $this->updateRuleLog("info", (string) $rule->id, $log_data); |
| @@ -1468,13 +1747,13 @@ | ||
| 1468 | 1747 | foreach($rule->actions as $action) { |
| 1469 | 1748 | switch ($action["type"]) { |
| 1470 | 1749 | case "ALLOW": |
| 1471 | 1750 | $this->break_rule_matching = true; |
| 1472 | - $this->request->category = WPRProtectRequest_V568::CATEGORY_RULE_ALLOWED; | |
| 1751 | + $this->request->category = WPRProtectRequest_V672::CATEGORY_RULE_ALLOWED; | |
| 1473 | 1752 | return; |
| 1474 | 1753 | case "BLOCK": |
| 1475 | 1754 | if ($this->isModeProtect()) { |
| 1476 | - $this->terminateRequest(WPRProtectRequest_V568::CATEGORY_RULE_BLOCKED); | |
| 1755 | + $this->terminateRequest(WPRProtectRequest_V672::CATEGORY_RULE_BLOCKED); | |
| 1477 | 1756 | } |
| 1478 | 1757 | return; |
| 1479 | 1758 | case "INSPECT": |
| 1480 | 1759 | $this->inspectRequest(); |
| @@ -1488,15 +1767,15 @@ | ||
| 1488 | 1767 | } |
| 1489 | 1768 | |
| 1490 | 1769 | private function terminateRequest($category) { |
| 1491 | 1770 | $this->request->category = $category; |
| 1492 | - $this->request->status = WPRProtectRequest_V568::STATUS_BLOCKED; | |
| 1771 | + $this->request->status = WPRProtectRequest_V672::STATUS_BLOCKED; | |
| 1493 | 1772 | $this->request->setRespCode(403); |
| 1494 | 1773 | |
| 1495 | 1774 | if ($this->can_set_cache_prevention_cookie && |
| 1496 | - !$this->request->getCookies(WPRProtectFW_V568::PREVENT_CACHE_COOKIE_NAME)) { | |
| 1775 | + !$this->request->getCookies(WPRProtectFW_V672::PREVENT_CACHE_COOKIE_NAME)) { | |
| 1497 | 1776 | $value = "Prevent Caching Response."; |
| 1498 | - $this->setCookie(WPRProtectFW_V568::PREVENT_CACHE_COOKIE_NAME, $value, time() + 43200); | |
| 1777 | + $this->setCookie(WPRProtectFW_V672::PREVENT_CACHE_COOKIE_NAME, $value, time() + 43200); | |
| 1499 | 1778 | } |
| 1500 | 1779 | |
| 1501 | 1780 | header("Cache-Control: no-cache, no-store, must-revalidate"); |
| 1502 | 1781 | header("Pragma: no-cache"); |
| @@ -1501,8 +1780,9 @@ | ||
| 1501 | 1780 | header("Cache-Control: no-cache, no-store, must-revalidate"); |
| 1502 | 1781 | header("Pragma: no-cache"); |
| 1503 | 1782 | header("Expires: 0"); |
| 1504 | 1783 | header('HTTP/1.0 403 Forbidden'); |
| 1784 | + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 1505 | 1785 | die(" |
| 1506 | 1786 | <div style='height: 98vh;'> |
| 1507 | 1787 | <div style='text-align: center; padding: 10% 0; font-family: Arial, Helvetica, sans-serif;'> |
| 1508 | 1788 | <div><p>" . $this->brand_name . " Firewall</p></div> |
| @@ -1510,8 +1790,9 @@ | ||
| 1510 | 1790 | <p>Reference ID: " . $this->request->getRequestID() . "</p> |
| 1511 | 1791 | </div> |
| 1512 | 1792 | </div> |
| 1513 | 1793 | "); |
| 1794 | + // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 1514 | 1795 | } |
| 1515 | 1796 | |
| 1516 | 1797 | public function setBypassCookie() { |
| 1517 | 1798 | if (function_exists('is_user_logged_in') && is_user_logged_in() && |
| @@ -1520,11 +1801,11 @@ | ||
| 1520 | 1801 | $role_level = $this->getCurrentWPUserRoleLevel(); |
| 1521 | 1802 | if ($role_level >= $this->bypass_level) { |
| 1522 | 1803 | $cookie = $this->generateBypassCookie(); |
| 1523 | 1804 | if ($cookie) { |
| 1524 | - $this->setCookie(WPRProtectFW_V568::BYPASS_COOKIE_NAME, $cookie); | |
| 1805 | + $this->setCookie(WPRProtectFW_V672::BYPASS_COOKIE_NAME, $cookie); | |
| 1525 | 1806 | } |
| 1526 | 1807 | } |
| 1527 | 1808 | } |
| 1528 | 1809 | } |
| 1529 | 1810 | } |
| 1530 | -endif; | |
| 1811 | +endif; | |