File: //proc/self/cwd/wp-content/plugins/bot-nemesis_6551354a614844b2a78837b5afc250af/inc/Settings.php
<?php
namespace BotNemesis;
use BotNemesis\Entities\CountryIPAddressEntity;
use BotNemesis\Entities\InfoBlurbEntity;
use BotNemesis\Entities\UserAgentKeywordEntity;
use BotNemesis\Entities\IPAddressEntity;
use BotNemesis\Entities\CountryEntity;;
class Settings
{
public static function init()
{
add_action( 'wp_ajax_bn_set_automatic_updates', array( get_called_class(), 'setAutomaticUpdates' ) );
add_action( 'wp_ajax_bn_register_plugin', array( get_called_class(), 'registerPlugin' ) );
add_action( 'wp_ajax_bn_save_settings', array( get_called_class(), 'saveSettings' ) );
add_action( 'bn_get_automatic_updates_from_master', array( get_called_class(), 'getAutomaticUpdatesFromMaster' ) );
}
public static function setAutomaticUpdates()
{
check_ajax_referer('bn_set_automatic_updates', 'bn_set_automatic_updates_nonce');
$r = update_option('bn_automatic_updates', $_POST['value'] ? 1 : 0);
wp_send_json( array( 'status' => $r ? '0' : '-1', 'message' => '' ) );
}
public static function saveSettings()
{
check_ajax_referer('bn_save_settings', 'bn_save_settings_nonce');
if(isset($_POST['reset_settings'])) {
self::resetSettings();
}
$r = FALSE;
$block_user_agent_keyword = isset($_POST['block_user_agent_keyword']) ?
stripslashes_deep($_POST['block_user_agent_keyword']) : array();
$block_ip_address = isset($_POST['block_ip_address']) ?
stripslashes_deep($_POST['block_ip_address']) : array();
$block_country_ip_address = isset($_POST['block_country_ip_address']) ?
$_POST['block_country_ip_address'] : array();
try {
self::saveUserAgentKeywords($block_user_agent_keyword);
self::saveIPAddresses($block_ip_address);
self::saveBlockedCountries($block_country_ip_address);
update_option('bn_client_package', array(
'user_agent_keywords' => UserAgentKeywordEntity::getEntitiesByProperty(array(
'user_agent_keyword_is_master' => '0',
)),
'ip_addresses' => IPAddressEntity::getEntitiesByProperty(array(
'ip_address_is_master' => '0',
)),
));
Client::sendClientSettings(get_option('bn_client_uid', ''));
$r = TRUE;
}
catch(\Exception $e) {}
wp_send_json( array( 'status' => $r ? '0' : '-1', 'message' => $r ? 'refresh' : '' ) );
}
public static function registerPlugin()
{
check_ajax_referer('bn_register_plugin', 'bn_register_plugin_nonce');
$r = Client::registerPlugin($_POST['license_key'], get_option('siteurl'), get_option('bn_client_uid', ''));
if($r && $r->valid_license) {
update_option('bn_valid_license', $r ? 1 : 0);
update_option('bn_registration_email', $r->registration_email);
update_option('bn_license_key', $_POST['license_key']);
$master_settings = json_decode(base64_decode($r->master_settings));
self::applyMasterSettings($master_settings);
}
$r = $r && $r->valid_license;
wp_send_json( array( 'status' => $r ? '0' : '-4', 'message' => $r ? 'refresh' : '' ) );
}
public static function getMasterServer($path = '')
{
return 'http://www.botnemesis.com' . $path;
}
public static function applyMasterSettings($settings)
{
if( ! (isset($settings->user_agent_keywords) && isset($settings->ip_addresses) &&
isset($settings->blocked_countries) && isset($settings->country_ip_addresses) &&
isset($settings->blocked_return_status) && isset($settings->info_blurbs)) )
{
return FALSE;
}
try {
self::saveUserAgentKeywords( (array) $settings->user_agent_keywords, TRUE);
self::saveIPAddresses( (array) $settings->ip_addresses, TRUE);
self::saveBlockedCountries( (array) $settings->blocked_countries, TRUE);
self::saveCountryIPAddresses( (array) $settings->country_ip_addresses);
update_option('bn_blocked_return_status', ($settings->blocked_return_status ?: '408 Request Timeout'));
self::saveInfoBlurbs( (array) $settings->info_blurbs);
return TRUE;
}
catch(\Exception $e) {
return FALSE;
}
}
public static function getAutomaticUpdatesFromMaster()
{
if(get_option('bn_automatic_updates', 1) != 1) {
return;
}
$response = Client::getMasterSettings(get_option('bn_license_key', ''), get_option('bn_client_uid', ''));
if($response && isset($response->master_settings) &&
$master_settings = json_decode(base64_decode($response->master_settings)))
{
self::applyMasterSettings($master_settings);
}
}
public static function getClientSettings()
{
return base64_encode(json_encode(get_option('bn_client_package', '')));
}
protected static function resetSettings()
{
UserAgentKeywordEntity::deleteAll();
IPAddressEntity::deleteAll();
CountryEntity::removeAll();
$response = Client::getMasterSettings(get_option('bn_license_key', ''), get_option('bn_client_uid', ''));
if($response && isset($response->master_settings) &&
$master_settings = json_decode(base64_decode($response->master_settings)))
{
self::applyMasterSettings($master_settings);
}
wp_send_json( array( 'status' => $response ? '0' : '-1', 'message' => $response ? 'refresh' : '' ) );
}
protected static function saveBlockedCountries($country_ids, $is_master = FALSE)
{
if( ! is_array($country_ids)) {
return;
}
$blocked_countries = array_unique(array_filter(array_map(function($e) { return (int) trim($e); }, $country_ids)));
$db_blocked_countries = CountryEntity::getEntitiesByProperty(array(
'country_blocked' => '1',
), 'ARRAY_A');
$db_blocked_countries = array_map(function($c) { return $c['country_id']; }, $db_blocked_countries);
$new_blocked_countries = array_diff($blocked_countries, $db_blocked_countries);
$existing_blocked_countries = array_intersect($db_blocked_countries, $blocked_countries);
$blocked_countries_to_remove = array_diff($db_blocked_countries, $blocked_countries);
// Array to hold Entity fields to update
$values = $is_master ? array('country_is_master' => 1) : array();
// Newly blocked ones
$values['country_blocked'] = 1;
$values['country_removed'] = 0;
foreach($new_blocked_countries as $blocked_country) {
CountryEntity::update($blocked_country, $values);
}
// Restore - In case of any existing country was "removed" - if master don't restore
$values['country_blocked'] = 1;
$values['country_removed'] = 0;
if($is_master) {
unset($values['country_removed']);
}
foreach($existing_blocked_countries as $existing_blocked_country) {
CountryEntity::update($existing_blocked_country, $values);
}
// "removing" - if master (and to_be_removed), then mark as not master
if($is_master) {
unset($values['country_blocked']);
unset($values['country_removed']);
$values['country_is_master'] = 0;
}
// Otherwise "remove"
else {
$values['country_blocked'] = 0;
$values['country_removed'] = 1;
}
foreach($blocked_countries_to_remove as $blocked_country_to_remove) {
CountryEntity::update($blocked_country_to_remove, $values);
}
}
protected static function saveUserAgentKeywords($keywords, $is_master = FALSE)
{
if( ! is_array($keywords)) {
return;
}
$block_user_agent_keyword = array_unique(array_filter(
array_map(function($e) {
return trim(wp_strip_all_tags($e, TRUE));
}, $keywords)
));
$db_user_agent_keywords = UserAgentKeywordEntity::getEntities();
$db_user_agent_keywords = array_map(function($e) { return $e['user_agent_keyword_value']; }, $db_user_agent_keywords);
$new_keywords = array_diff($block_user_agent_keyword, $db_user_agent_keywords);
$existing_keywords = array_intersect($db_user_agent_keywords, $block_user_agent_keyword);
$keywords_to_remove = array_diff($db_user_agent_keywords, $block_user_agent_keyword);
// Array to hold Entity fields to update
$values = $is_master ? array('user_agent_keyword_is_master' => 1) : array();
// Add new unique keywords
$values['user_agent_keyword_value'] = '';
$values['user_agent_keyword_removed'] = 0;
foreach($new_keywords as $user_agent_keyword) {
$values['user_agent_keyword_value'] = $user_agent_keyword;
UserAgentKeywordEntity::create($values);
}
// Unset unnecessary fields - do not unset "is_master" to switch client settings to master
unset($values['user_agent_keyword_value']);
// Restore - In case of any existing keyword was "removed" - if master don't restore
$values['user_agent_keyword_removed'] = 0;
if($is_master) {
unset($values['user_agent_keyword_removed']);
}
foreach($existing_keywords as $id => $existing_keyword) {
UserAgentKeywordEntity::update($id, $values);
}
// "removing" - if master (and to_be_removed), then mark as not master
if($is_master) {
unset($values['user_agent_keyword_removed']);
$values['user_agent_keyword_is_master'] = 0;
}
// Otherwise "remove"
else {
$values['user_agent_keyword_removed'] = 1;
}
foreach($keywords_to_remove as $id => $keyword_to_remove) {
UserAgentKeywordEntity::update($id, $values);
}
}
protected static function saveIPAddresses($ip_addresses, $is_master = FALSE)
{
if( ! is_array($ip_addresses)) {
return;
}
$block_ip_addresses = array_map('bn_validate_ip_entry', $ip_addresses);
// Mark duplicate IPs
array_walk($block_ip_addresses, 'bn_mark_duplicate_ips', $block_ip_addresses);
// Filter duplicate and invalid IPs
$block_ip_addresses = array_filter($block_ip_addresses);
$db_block_ip_addresses = IPAddressEntity::getEntities();
$db_block_ip_addresses = array_map(function($e) { return $e['ip_address_value']; }, $db_block_ip_addresses);
$compare_function = function($a, $b) {
$a_parts = explode('#', $a);
$a_value = str_replace(' ', '', ($a_parts[0]));
$b_parts = explode('#', $b);
$b_value = str_replace(' ', '', ($b_parts[0]));
return $a_value === $b_value ? 0 : ($a_value > $b_value ? 1 : -1);
};
$new_ips = array_udiff($block_ip_addresses, $db_block_ip_addresses, $compare_function);
$existing_ips = array_uintersect($db_block_ip_addresses, $block_ip_addresses, $compare_function);
$ips_to_remove = array_udiff($db_block_ip_addresses, $block_ip_addresses, $compare_function);
// Array to hold Entity fields to update
$values = $is_master ? array('ip_address_is_master' => 1) : array();
// Add new unique IPs
$values['ip_address_removed'] = 0;
foreach($new_ips as $ip_address) {
$values['ip_address_value'] = $ip_address;
IPAddressEntity::create($values);
}
// Unset unnecessary fields - do not unset "is_master" to switch client settings to master
unset($values['ip_address_value']);
// Restore - In case of any existing IP was "removed" - if master don't restore
$values['ip_address_removed'] = 0;
if($is_master) {
unset($values['ip_address_removed']);
}
foreach($existing_ips as $id => $existing_ip) {
IPAddressEntity::update($id, $values);
}
// "removing" - if master (and to_be_removed), then mark as not master
if($is_master) {
unset($values['ip_address_removed']);
$values['ip_address_is_master'] = 0;
}
// Otherwise "remove"
else {
$values['ip_address_removed'] = 1;
}
foreach($ips_to_remove as $id => $ip_to_remove) {
IPAddressEntity::update($id, $values);
}
}
protected static function saveCountryIPAddresses($ip_addresses)
{
if( ! is_array($ip_addresses)) {
return;
}
CountryIPAddressEntity::deleteAll();
foreach($ip_addresses as $ip_address) {
if( ! ($ip_address_value = bn_validate_ip_entry($ip_address->country_ip_address_value))) {
continue;
}
CountryIPAddressEntity::create(array(
'country_ip_address_country_id' => $ip_address->country_ip_address_country_id,
'country_ip_address_value' => $ip_address_value,
));
}
}
protected static function saveInfoBlurbs($info_blurbs)
{
if( ! is_array($info_blurbs) ) {
return;
}
InfoBlurbEntity::deleteAll();
foreach($info_blurbs as $info_blurb) {
InfoBlurbEntity::create(array(
'info_blurb_key' => $info_blurb->info_blurb_key,
'info_blurb_text' => trim(wp_strip_all_tags($info_blurb->info_blurb_text))
));
}
}
}