<?php
// сохранение конфига
if(!defined('ANTIBOT')) die('access denied');

$title = abTranslate('Edit Config');

$bad_symbol = array('\'', '"', '\\');

if (isset($_POST['save_conf'])) {

include(__DIR__.'/../data/conf.php');

if (!is_dir(__DIR__.'/../data/counters')) {
mkdir(__DIR__.'/../data/counters');
}

$_POST['disable'] = isset($_POST['disable']) ? trim($_POST['disable']) : 0;
if ($_POST['disable'] != 1) {$_POST['disable'] = 0;}

$_POST['phperror'] = isset($_POST['phperror']) ? preg_replace("/[^0-9]/","", $_POST['phperror']) : 1;
$_POST['phperror'] = $_POST['phperror'] + 0;

if ($_POST['phperror'] == 2) {
$phperrorsave = 'error_reporting(E_ALL);
ini_set(\'display_errors\', 1);
ini_set(\'display_startup_errors\', 1);
ini_set(\'html_errors\', 1);
ini_set(\'error_log\', __DIR__.\'/errorlog.txt\');';
} elseif ($_POST['phperror'] == 1) {
$phperrorsave = 'error_reporting(E_ALL);
ini_set(\'display_errors\', 0);
ini_set(\'error_log\', __DIR__.\'/errorlog.txt\');';
} else {
$phperrorsave = 'error_reporting(0);
ini_set(\'display_errors\', 0); // off or on';
}

$_POST['webdir'] = isset($_POST['webdir']) ? trim(strip_tags($_POST['webdir'])) : $ab_webdir.'/';
$_POST['webdir'] = str_ireplace($bad_symbol, '', $_POST['webdir']);

$_POST['timezone'] = isset($_POST['timezone']) ? trim(preg_replace("/[^a-zA-Z\-\_\/]/","", $_POST['timezone'])) : '';
if ($_POST['timezone'] != '') {
$timezonesave = 'date_default_timezone_set(\''.$_POST['timezone'].'\');';
} else {
$timezonesave = '';
}

$_POST['email'] = isset($_POST['email']) ? trim(strip_tags($_POST['email'])) : '';
$_POST['email'] = mb_strtolower($_POST['email'], 'utf-8'); // в нижний регистр
$_POST['email'] = str_ireplace($bad_symbol, '', $_POST['email']);

$_POST['pass'] = isset($_POST['pass']) ? trim(strip_tags($_POST['pass'])) : '';
$_POST['pass'] = str_ireplace($bad_symbol, '', $_POST['pass']);

$_POST['secondpass'] = isset($_POST['secondpass']) ? trim($_POST['secondpass']) : '';
if ($_POST['secondpass'] == '**********') {
$_POST['secondpass'] = $ab_config['secondpass']; // не менять
} elseif ($_POST['secondpass'] != '') {
$_POST['secondpass'] = md5('antibot'.$_POST['secondpass']);
} else {
$_POST['secondpass'] = '';
}

if (isset($_POST['newsalt'])) {
file_put_contents(__DIR__.'/../data/subsalt.php', '<?php $ab_config[\'subsalt\'] = \''.abRandword(9).'\';', LOCK_EX);
}

$_POST['old_salt'] = explode('_', $ab_config['salt']);
$_POST['newsalt'] = isset($_POST['newsalt']) ? abRandword(4) : trim($_POST['old_salt'][0]);

$_POST['timesalt'] = isset($_POST['timesalt']) ? trim(strip_tags($_POST['timesalt'])) : '';
$_POST['timesalts'] = array('YW', 'Yz', 'Y');
if (!in_array($_POST['timesalt'], $_POST['timesalts'])) {$_POST['timesalt'] = 'Y';}

$_POST['check_url'] = isset($_POST['check_url']) ? trim(strip_tags($_POST['check_url'])) : '';
$_POST['check_url'] = str_ireplace($bad_symbol, '', $_POST['check_url']);

$_POST['is_bitrix'] = isset($_POST['is_bitrix']) ? trim($_POST['is_bitrix']) : 0;
if ($_POST['is_bitrix'] != 1) {$_POST['is_bitrix'] = 0;}

$_POST['timer'] = isset($_POST['timer']) ? preg_replace("/[^0-9]/","", $_POST['timer']) : 5;
$_POST['timer'] = $_POST['timer'] + 0;

$_POST['hits_per_user'] = isset($_POST['hits_per_user']) ? preg_replace("/[^0-9]/","", $_POST['hits_per_user']) : 1000;
$_POST['hits_per_user'] = $_POST['hits_per_user'] + 0;

$_POST['input_button'] = isset($_POST['input_button']) ? trim($_POST['input_button']) : 0;
if ($_POST['input_button'] != 1) {$_POST['input_button'] = 0;}

$_POST['buttons'] = isset($_POST['buttons']) ? (int)trim($_POST['buttons']) : 0;

$_POST['time_ban'] = isset($_POST['time_ban']) ? trim(preg_replace("/[^0-9\.]/","", $_POST['time_ban'])) : 0;
if ($_POST['time_ban'] == '') {$_POST['time_ban'] = 0;}
$_POST['time_ban'] = explode('.', $_POST['time_ban']);
$_POST['time_ban'][0] = (int)$_POST['time_ban'][0] + 0;
if (isset($_POST['time_ban'][1])) {
$_POST['time_ban'][1] = (int)$_POST['time_ban'][1] + 0;
$_POST['time_ban'] = $_POST['time_ban'][0].'.'.$_POST['time_ban'][1];
} else {
$_POST['time_ban'] = $_POST['time_ban'][0];
}

$_POST['tpl_lang'] = isset($_POST['tpl_lang']) ? trim(preg_replace("/[^a-z]/","", $_POST['tpl_lang'])) : '';
if ($_POST['tpl_lang'] != '' AND !file_exists(__DIR__.'/../lang/tpl/'.$_POST['tpl_lang'].'.php')) {
$_POST['tpl_lang'] = '';
}

$_POST['recaptcha_key'] = isset($_POST['recaptcha_key']) ? trim(strip_tags($_POST['recaptcha_key'])) : '';
$_POST['recaptcha_key'] = str_ireplace($bad_symbol, '', $_POST['recaptcha_key']);

$_POST['recaptcha_secret'] = isset($_POST['recaptcha_secret']) ? trim(strip_tags($_POST['recaptcha_secret'])) : '';
$_POST['recaptcha_secret'] = str_ireplace($bad_symbol, '', $_POST['recaptcha_secret']);

$_POST['re_check'] = isset($_POST['re_check']) ? trim($_POST['re_check']) : 0;
if ($_POST['re_check'] != 1 OR $_POST['recaptcha_key'] == '' OR $_POST['recaptcha_secret'] == '') {$_POST['re_check'] = 0;}

$_POST['utm_referrer'] = isset($_POST['utm_referrer']) ? trim($_POST['utm_referrer']) : 0;
if ($_POST['utm_referrer'] != 1) {$_POST['utm_referrer'] = 0;}

$_POST['utm_noindex'] = isset($_POST['utm_noindex']) ? trim($_POST['utm_noindex']) : 0;
if ($_POST['utm_noindex'] != 1) {$_POST['utm_noindex'] = 0;}

$_POST['check_get_ref'] = isset($_POST['check_get_ref']) ? trim($_POST['check_get_ref']) : 0;
if ($_POST['check_get_ref'] != 1) {$_POST['check_get_ref'] = 0;}

$_POST['bad_get_ref'] = isset($_POST['bad_get_ref']) ? trim(strip_tags($_POST['bad_get_ref'])) : '';
$_POST['bad_get_ref'] = str_ireplace($bad_symbol, '', $_POST['bad_get_ref']);

$_POST['secret_allow_get'] = isset($_POST['secret_allow_get']) ? trim(strip_tags($_POST['secret_allow_get'])) : '';
$_POST['secret_allow_get'] = str_ireplace($bad_symbol, '', $_POST['secret_allow_get']);

$_POST['ptrcache_time'] = isset($_POST['ptrcache_time']) ? preg_replace("/[^0-9]/","", $_POST['ptrcache_time']) : 7;
$_POST['ptrcache_time'] = $_POST['ptrcache_time'] + 0;

$_POST['antibot_log_tests'] = isset($_POST['antibot_log_tests']) ? trim($_POST['antibot_log_tests']) : 0;
if ($_POST['antibot_log_tests'] != 1) {$_POST['antibot_log_tests'] = 0;}

$_POST['antibot_log_local'] = isset($_POST['antibot_log_local']) ? trim($_POST['antibot_log_local']) : 0;
if ($_POST['antibot_log_local'] != 1) {$_POST['antibot_log_local'] = 0;}

$_POST['antibot_log_allow'] = isset($_POST['antibot_log_allow']) ? trim($_POST['antibot_log_allow']) : 0;
if ($_POST['antibot_log_allow'] != 1) {$_POST['antibot_log_allow'] = 0;}

$_POST['antibot_log_fake'] = isset($_POST['antibot_log_fake']) ? trim($_POST['antibot_log_fake']) : 0;
if ($_POST['antibot_log_fake'] != 1) {$_POST['antibot_log_fake'] = 0;}

$_POST['antibot_log_goodip'] = isset($_POST['antibot_log_goodip']) ? trim($_POST['antibot_log_goodip']) : 0;
if ($_POST['antibot_log_goodip'] != 1) {$_POST['antibot_log_goodip'] = 0;}

$_POST['antibot_log_block'] = isset($_POST['antibot_log_block']) ? trim($_POST['antibot_log_block']) : 0;
if ($_POST['antibot_log_block'] != 1) {$_POST['antibot_log_block'] = 0;}

$_POST['header_test_code'] = isset($_POST['header_test_code']) ? preg_replace("/[^0-9]/","", $_POST['header_test_code']) : 200;
$_POST['header_test_code'] = $_POST['header_test_code'] + 0;

$_POST['header_error_code'] = isset($_POST['header_error_code']) ? preg_replace("/[^0-9]/","", $_POST['header_error_code']) : 403;
$_POST['header_error_code'] = $_POST['header_error_code'] + 0;

$_POST['period_cleaning'] = isset($_POST['period_cleaning']) ? trim(preg_replace("/[^a-z]/","", $_POST['period_cleaning'])) : 'lastmonth';

$_POST['noarchive'] = isset($_POST['noarchive']) ? trim($_POST['noarchive']) : 0;
if ($_POST['noarchive'] != 1) {$_POST['noarchive'] = 0;}

$_POST['last_rule'] = isset($_POST['last_rule']) ? trim(strip_tags($_POST['last_rule'])) : '';
$_POST['last_rules'] = array('allow', 'block', 'dark', 'gray', '');
if (!in_array($_POST['last_rule'], $_POST['last_rules'])) {$_POST['last_rule'] = '';}

$_POST['conf'] = '<?php
// '.date("F d, Y - H:i:s", $ab_config['time']).'
'.$phperrorsave.'
'.$timezonesave.'
$ab_config[\'disable\'] = '.$_POST['disable'].';
$ab_config[\'phperror\'] = '.$_POST['phperror'].';
$ab_config[\'timezone\'] = \''.$_POST['timezone'].'\';
$ab_config[\'webdir\'] = \''.$_POST['webdir'].'\'; // change if renamed Antibot directory
$ab_config[\'email\'] = \''.$_POST['email'].'\'; // change to your email
$ab_config[\'pass\'] = \''.$_POST['pass'].'\'; // change to your password
$ab_config[\'secondpass\'] = \''.$_POST['secondpass'].'\'; // delete this line if you forgot your password
$ab_config[\'salt\'] = \''.$_POST['newsalt'].'_\'.date(\''.$_POST['timesalt'].'\', time());
$ab_config[\'timesalt\'] = \''.$_POST['timesalt'].'\';
$ab_config[\'check_url\'] = \''.$_POST['check_url'].'\';
$ab_config[\'is_bitrix\'] = '.$_POST['is_bitrix'].';
$ab_config[\'timer\'] = '.$_POST['timer'].';
$ab_config[\'hits_per_user\'] = '.$_POST['hits_per_user'].';
$ab_config[\'input_button\'] = '.$_POST['input_button'].';
$ab_config[\'tpl_lang\'] = \''.$_POST['tpl_lang'].'\';
$ab_config[\'buttons\'] = '.$_POST['buttons'].';
$ab_config[\'time_ban\'] = \''.$_POST['time_ban'].'\';
$ab_config[\'re_check\'] = '.$_POST['re_check'].';
$ab_config[\'recaptcha_key\'] = \''.$_POST['recaptcha_key'].'\';
$ab_config[\'recaptcha_secret\'] = \''.$_POST['recaptcha_secret'].'\';
$ab_config[\'utm_referrer\'] = '.$_POST['utm_referrer'].'; 
$ab_config[\'utm_noindex\'] = '.$_POST['utm_noindex'].';
$ab_config[\'check_get_ref\'] = '.$_POST['check_get_ref'].';
$ab_config[\'bad_get_ref\'] = \''.$_POST['bad_get_ref'].'\';
$ab_config[\'secret_allow_get\'] = \''.$_POST['secret_allow_get'].'\';
$ab_config[\'ptrcache_time\'] = '.$_POST['ptrcache_time'].';
$ab_config[\'antibot_log_tests\'] = '.$_POST['antibot_log_tests'].';
$ab_config[\'antibot_log_local\'] = '.$_POST['antibot_log_local'].';
$ab_config[\'antibot_log_allow\'] = '.$_POST['antibot_log_allow'].';
$ab_config[\'antibot_log_fake\'] = '.$_POST['antibot_log_fake'].';
$ab_config[\'antibot_log_goodip\'] = '.$_POST['antibot_log_goodip'].';
$ab_config[\'antibot_log_block\'] = '.$_POST['antibot_log_block'].';
$ab_config[\'header_test_code\'] = '.$_POST['header_test_code'].';
$ab_config[\'header_error_code\'] = '.$_POST['header_error_code'].';
$ab_config[\'period_cleaning\'] = \''.$_POST['period_cleaning'].'\';
$ab_config[\'noarchive\'] = \''.$_POST['noarchive'].'\';
$ab_config[\'last_rule\'] = \''.$_POST['last_rule'].'\';
';

file_put_contents(__DIR__.'/../data/conf.php', $_POST['conf'], LOCK_EX);

clearstatcache(true); // Clears file status cache
if(function_exists('opcache_reset')) {
opcache_reset();
}

}
echo '<script>document.location.href="?'.$abw.$abp.'=conf";</script>';
