: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit;
if (!class_exists('BVProtectFW_V565')) :
require_once dirname( __FILE__ ) . '/fw/rule/errors.php';
require_once dirname( __FILE__ ) . '/fw/rule/engine.php';
require_once dirname( __FILE__ ) . '/fw/rule.php';
private $is_shutdown_cb_set = false;
private $is_rule_initialized = false;
private $is_wpf_rule_initialized = false;
private $is_ip_cookie_set = false;
private $is_request_profiled = false;
private $is_on_boot_rules_executed = false;
private $is_ip_checked_for_blacklisted = false;
private $has_valid_bypass_cookie;
private $mode = BVProtectFW_V565::MODE_DISABLED;
private $ip_cookie_mode = BVProtectFW_V565::IP_COOKIE_MODE_DISABLED;
private $admin_cookie_mode = BVProtectFW_V565::ADMIN_COOKIE_MODE_DISABLED;
private $bypass_level = BVProtectFW_V565::WP_USER_ROLE_LEVEL_CONTRIBUTOR;
private $wpf_rule_init_mode = BVProtectFW_V565::WPF_RULE_INIT_MODE_WP;
private $custom_roles = array();
private $cookie_key = "";
private $cookie_path = "";
private $cookie_domain = "";
private $cookie_validity = 2592000;
private $can_set_cache_prevention_cookie = false;
private $rules_mode = BVProtectFW_V565::RULES_MODE_DISABLED;
private $is_geo_blocking = false;
private $is_wp_user_cookie_enabled = false;
private $log_config = array();
private $request_profiling_mode = BVProtectFW_V565::REQ_PROFILING_MODE_DISABLED;
private $logging_mode = BVProtectFW_V565::LOGGING_MODE_VISITOR;
private $skip_log_config = array();
private $skip_log_cookies = array();
private $skip_log_headers = array();
private $skip_log_post_params = array();
private $skip_log_json_params = array();
private $wp_user_caps_to_consider = array();
private $request_profiled_data = array();
private $rules = array();
private $wpf_rules = array();
private $rule_log = array();
private $matched_rules = array();
private $break_rule_matching = false;
private $can_log_raw_body = false;
private $log_slice_size = BVProtectFW_V565::LOG_SLICE_SIZE;
private static $instance = null;
const RULES_MODE_DISABLED = 1;
const RULES_MODE_AUDIT = 2;
const RULES_MODE_PROTECT = 3;
const REQ_PROFILING_MODE_DISABLED = 1;
const REQ_PROFILING_MODE_NORMAL = 2;
const REQ_PROFILING_MODE_DEBUG = 3;
const IP_COOKIE_MODE_ENABLED = 1;
const IP_COOKIE_MODE_DISABLED = 2;
const WPF_RULE_INIT_MODE_PREPEND = 1;
const WPF_RULE_INIT_MODE_WP = 2;
const ADMIN_COOKIE_MODE_ENABLED = 1;
const ADMIN_COOKIE_MODE_DISABLED = 2;
const WP_USER_ROLE_LEVEL_UNKNOWN = 0;
const WP_USER_ROLE_LEVEL_SUBSCRIBER = 1;
const WP_USER_ROLE_LEVEL_CONTRIBUTOR = 2;
const WP_USER_ROLE_LEVEL_AUTHOR = 3;
const WP_USER_ROLE_LEVEL_EDITOR = 4;
const WP_USER_ROLE_LEVEL_ADMIN = 5;
const WP_USER_ROLE_LEVEL_CUSTOM = 6;
const WS_CONF_MODE_APACHEMODPHP = 1;
const WS_CONF_MODE_APACHESUPHP = 2;
const WS_CONF_MODE_CGI_FASTCGI = 3;
const WS_CONF_MODE_NGINX = 4;
const WS_CONF_MODE_LITESPEED = 5;
const WS_CONF_MODE_IIS = 6;
const LOGGING_MODE_VISITOR = 1;
const LOGGING_MODE_COMPLETE = 2;
const LOGGING_MODE_DISABLED = 3;
const DEFAULT_WP_USER_ROLE_LEVELS = array(
'administrator' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_ADMIN,
'editor' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_EDITOR,
'author' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_AUTHOR,
'contributor' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_CONTRIBUTOR,
'subscriber' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_SUBSCRIBER
const EXTRA_WP_USER_ROLE_LEVELS = array(
'custom' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_CUSTOM,
'unknown' => BVProtectFW_V565::WP_USER_ROLE_LEVEL_UNKNOWN
const TABLE_NAME = "fw_requests";
const IP_COOKIE_NAME = "mcfw-ip-cookie";
const BYPASS_COOKIE_NAME = "mcfw-bypass-cookie";
const PREVENT_CACHE_COOKIE_NAME = "wp-mcfw-prevent-cache-cookie";
const LOG_SLICE_SIZE = 1024;
private function __construct($protect_mode, $request, $config, $brand_name) {
$this->request = $request;
$this->brand_name = $brand_name;
$this->protect_mode = $protect_mode;
if (array_key_exists('mode', $config) && is_int($config['mode'])) {
$this->mode = $config['mode'];
if (array_key_exists('ipcookiemode', $config) && is_int($config['ipcookiemode'])) {
$this->ip_cookie_mode = $config['ipcookiemode'];
if (array_key_exists('admincookiemode', $config) && is_int($config['admincookiemode'])) {
$this->admin_cookie_mode = $config['admincookiemode'];
if (array_key_exists('iswpusercookieenabled', $config) &&
is_bool($config['iswpusercookieenabled'])) {
$this->is_wp_user_cookie_enabled = $config['iswpusercookieenabled'];
if (array_key_exists('bypasslevel', $config) && is_int($config['bypasslevel'])) {
$this->bypass_level = $config['bypasslevel'];
if (array_key_exists('wpfruleinitmode', $config) && is_int($config['wpfruleinitmode'])) {
$this->wpf_rule_init_mode = $config['wpfruleinitmode'];
if (array_key_exists('customroles', $config) && is_array($config['customroles'])) {
$this->custom_roles = $config['customroles'];
if (array_key_exists('wpusercapstoconsider', $config) &&
is_array($config['wpusercapstoconsider'])) {
$this->wp_user_caps_to_consider = $config['wpusercapstoconsider'];
if (array_key_exists('cookiekey', $config) && is_string($config['cookiekey'])) {
$this->cookie_key = $config['cookiekey'];
if (array_key_exists('cookiepath', $config) && is_string($config['cookiepath'])) {
$this->cookie_path = $config['cookiepath'];
if (array_key_exists('cookiedomain', $config) && is_string($config['cookiedomain'])) {
$this->cookie_domain = $config['cookiedomain'];
if (array_key_exists('cookievalidity', $config) && is_int($config['cookievalidity'])) {
$this->cookie_validity = $config['cookievalidity'];
if (array_key_exists('cansetcachepreventioncookie', $config) &&
is_bool($config['cansetcachepreventioncookie'])) {
$this->can_set_cache_prevention_cookie = $config['cansetcachepreventioncookie'];
if (array_key_exists('rulesmode', $config) && is_int($config['rulesmode'])) {
$this->rules_mode = $config['rulesmode'];
if (array_key_exists('isgeoblocking', $config) && is_bool($config['isgeoblocking'])) {
$this->is_geo_blocking = $config['isgeoblocking'];
if (array_key_exists('logconfig', $config) && is_array($config['logconfig'])) {
$this->log_config = $config['logconfig'];
if (array_key_exists('canlograwbody', $this->log_config) &&
is_bool($this->log_config['canlograwbody'])) {
$this->can_log_raw_body = $this->log_config['canlograwbody'];
if (array_key_exists('logslicesize', $this->log_config) &&
is_int($this->log_config['logslicesize'])) {
$this->log_slice_size = $this->log_config['logslicesize'];
if (array_key_exists('reqprofilingmode', $this->log_config) &&
is_int($this->log_config['reqprofilingmode'])) {
$this->request_profiling_mode = $this->log_config['reqprofilingmode'];
if (array_key_exists('loggingmode', $this->log_config) &&
is_int($this->log_config['loggingmode'])) {
$this->logging_mode = $this->log_config['loggingmode'];
if (array_key_exists('except', $this->log_config) && is_array($this->log_config['except'])) {
$this->skip_log_config = $this->log_config['except'];
if (array_key_exists('cookies', $this->skip_log_config) &&
is_array($this->skip_log_config['cookies'])) {
$this->skip_log_cookies = $this->skip_log_config['cookies'];
if (array_key_exists('headers', $this->skip_log_config) &&
is_array($this->skip_log_config['headers'])) {
$this->skip_log_headers = $this->skip_log_config['headers'];
if (array_key_exists('post', $this->skip_log_config) &&
is_array($this->skip_log_config['post'])) {
$this->skip_log_post_params = $this->skip_log_config['post'];
if (array_key_exists('json', $this->skip_log_config) &&
is_array($this->skip_log_config['json'])) {
$this->skip_log_json_params = $this->skip_log_config['json'];
if ($this->isPrependMode()) {
$log_file = MCDATAPATH . MCCONFKEY . '-mc.log';
$this->ipstore = new BVProtectIpstore_V565(BVProtectIpstore_V565::STORAGE_TYPE_FS);
$this->logger = new BVProtectLogger_V565($log_file, BVProtectLogger_V565::TYPE_FS);
$this->ipstore = new BVProtectIpstore_V565(BVProtectIpstore_V565::STORAGE_TYPE_DB);
$this->logger = new BVProtectLogger_V565(BVProtectFW_V565::TABLE_NAME, BVProtectLogger_V565::TYPE_DB);
if ($this->is_wp_user_cookie_enabled) {
public static function getInstance($protect_mode, $request, $config, $brand_name) {
if (!isset(self::$instance)) {
self::$instance = new self($protect_mode, $request, $config, $brand_name);
} elseif (self::$instance->protect_mode != $protect_mode && $protect_mode == BVProtect_V565::MODE_WP) {
self::$instance->protect_mode = $protect_mode;
self::$instance->brand_name = $brand_name;
self::$instance->ipstore = new BVProtectIpstore_V565(BVProtectIpstore_V565::STORAGE_TYPE_DB);
self::$instance->initRules();
public static function uninstall() {
BVProtect_V565::$db->dropBVTable(BVProtectFW_V565::TABLE_NAME);
if (!$this->isModeDisabled()) {
$this->setShutdownCallback();
$this->setWPUserCookie();
$this->blockRequestForBlacklistedIP();
if (!$this->is_on_boot_rules_executed) {
$this->handleRequestOnRuleMatch($this->rules);
$this->is_on_boot_rules_executed = true;
private function isPrependMode() {
return ($this->protect_mode === BVProtect_V565::MODE_PREPEND);
private function isWPMode() {
return ($this->protect_mode === BVProtect_V565::MODE_WP);
private function isModeDisabled() {
return ($this->mode === BVProtectFW_V565::MODE_DISABLED);
private function isModeProtect() {
return ($this->mode === BVProtectFW_V565::MODE_PROTECT);
private function isAdminCookieEnabled() {
return ($this->admin_cookie_mode === BVProtectFW_V565::ADMIN_COOKIE_MODE_ENABLED);
private function isIPCookieEnabled() {
return ($this->ip_cookie_mode === BVProtectFW_V565::IP_COOKIE_MODE_ENABLED);
private function isRequestProfilingDisabled() {
return ($this->request_profiling_mode === BVProtectFW_V565::REQ_PROFILING_MODE_DISABLED);
private function isRequestProfilingModeDebug() {
return ($this->request_profiling_mode === BVProtectFW_V565::REQ_PROFILING_MODE_DEBUG);
private function isRequestHasValidBypassCookie() {
if (!isset($this->has_valid_bypass_cookie)) {
$cookie = (string) $this->request->getCookies(BVProtectFW_V565::BYPASS_COOKIE_NAME);
$new_cookie = $this->generateBypassCookie();
$is_valid = ($this->isAdminCookieEnabled() && $new_cookie && ($cookie === $new_cookie));
$this->has_valid_bypass_cookie = $is_valid;
return $this->has_valid_bypass_cookie;
private function isRulesModeProtect() {
return ($this->rules_mode === BVProtectFW_V565::RULES_MODE_PROTECT);
public function isLoggingModeComplete() {
return ($this->logging_mode === BVProtectFW_V565::LOGGING_MODE_COMPLETE);
public function isLoggingModeVisitor() {
return ($this->logging_mode === BVProtectFW_V565::LOGGING_MODE_VISITOR);
public function isGeoBlockingEnabled() {
return ($this->is_geo_blocking === true);
private function isWPFRuleInitModePrepend() {
return ($this->wpf_rule_init_mode === BVProtectFW_V565::WPF_RULE_INIT_MODE_PREPEND);
private function isWPFRuleInitModeWP() {
return ($this->wpf_rule_init_mode === BVProtectFW_V565::WPF_RULE_INIT_MODE_WP);
private function canInitWPFRules() {
if (!$this->isWPFRuleInitModePrepend() && $this->isPrependMode()) {
private function generateBypassCookie() {
$time = floor(time() / 43200);
return hash('sha256', $this->bypass_level . $time . $this->cookie_key);
private function getWPFRules($action_name) {
if (!array_key_exists($action_name, $this->wpf_rules)) {
return $this->wpf_rules[$action_name];
public function setWPUserCookieHandler() {
if (function_exists('is_user_logged_in') && is_user_logged_in()) {
$current_wp_user = $this->getCurrentWPUser();
if (!$current_wp_user->isIdentical($this->request->wp_user)) {
$serialized_wp_user = BVProtectWPUser_V565::_serialize($current_wp_user);
$cookie_val = $serialized_wp_user . '_' .
BVProtectUtils_V565::signMessage($serialized_wp_user, $this->cookie_key);
$cookie_val = base64_encode($cookie_val);
$this->setCookie(BVProtectWPUser_V565::COOKIE_NAME, $cookie_val);
} elseif ($this->request->wp_user->isLoggedIn()) {
$this->request->wp_user = BVProtectWPUser_V565::defaultUser();
$this->unsetCookie(BVProtectWPUser_V565::COOKIE_NAME);
private function getCurrentWPUser() {
$time = (int) floor(time() / 43200);
if (function_exists('wp_get_current_user')) {
$user = wp_get_current_user();
$role_level = $this->getCurrentWPUserRoleLevel();
$capabilities = $this->getCurrentWPUserCapabilities();
return (new BVProtectWPUser_V565($id, $role_level, $capabilities, $time));
private function getCurrentWPUserCapabilities() {
if (function_exists('current_user_can')) {
foreach ($this->wp_user_caps_to_consider as $capability => $id) {
if (current_user_can($capability)) {
private function loadWPUser() {
$this->request->wp_user = BVProtectWPUser_V565::defaultUser();
$cookie_val = $this->request->getCookies(BVProtectWPUser_V565::COOKIE_NAME);
if (!is_string($cookie_val)) {
$cookie_val = base64_decode($cookie_val, true);
if ($cookie_val === false) {
$cookie_val_array = explode('_', $cookie_val);
if (count($cookie_val_array) !== 2) {
list($serialized_user, $signature) = $cookie_val_array;
if (BVProtectUtils_V565::verifyMessage($serialized_user, $signature, $this->cookie_key) === true) {
$wp_user = BVProtectWPUser_V565::_unserialize($serialized_user);
if (!isset($wp_user) || $wp_user->time !== (int) floor(time() / 43200)) {
$this->request->wp_user = $wp_user;
$capability_names = array_flip($this->wp_user_caps_to_consider);
foreach ($this->request->wp_user->capabilities as $capability) {
if (array_key_exists($capability, $capability_names)) {
$this->request->wp_user->capability_names[] = $capability_names[$capability];
$role_by_level = array_flip(array_merge(BVProtectFW_V565::DEFAULT_WP_USER_ROLE_LEVELS,
BVProtectFW_V565::EXTRA_WP_USER_ROLE_LEVELS));
$this->request->wp_user->role = $role_by_level[$this->request->wp_user->role_level];
private function pushWPFRule($action_name, $rule) {
if (!array_key_exists($action_name, $this->wpf_rules)) {
$this->wpf_rules[$action_name] = array();
$this->wpf_rules[$action_name][] = $rule;
private function initRules() {
if (!$this->isRulesModeProtect() || $this->isRequestIPWhitelisted()) {
if ($this->is_rule_initialized && $this->is_wpf_rule_initialized) {
if ($this->isPrependMode()) {
$rules_file = MCDATAPATH . MCCONFKEY . '-' . 'mc_rules.json';
$rule_arrays = BVProtectUtils_V565::parseFile($rules_file);
$rule_arrays = BVProtect_V565::$settings->getOption('bvruleset');
if(!is_array($rule_arrays)) {