Skip to content

Commit fce923f

Browse files
safroniksafronik
authored andcommitted
New: Firewall module logic.
1 parent 931c691 commit fce923f

File tree

1 file changed

+90
-76
lines changed

1 file changed

+90
-76
lines changed

cleantalk.php

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
// Database constants
121121
define('APBCT_TBL_FIREWALL_DATA', $apbct->db_prefix . 'cleantalk_sfw'); // Table with firewall data.
122122
define('APBCT_TBL_FIREWALL_LOG', $apbct->db_prefix . 'cleantalk_sfw_logs'); // Table with firewall logs.
123+
define('APBCT_TBL_AC_LOG', $apbct->db_prefix . 'cleantalk_ac_log'); // Table with firewall logs.
123124
define('APBCT_TBL_SESSIONS', $apbct->db_prefix . 'cleantalk_sessions'); // Table with session data.
124125
define('APBCT_SPAMSCAN_LOGS', $apbct->db_prefix . 'cleantalk_spamscan_logs'); // Table with session data.
125126
define('APBCT_SELECT_LIMIT', 5000); // Select limit for logs.
@@ -605,77 +606,65 @@ function apbct_sfw__check()
605606
}
606607
}
607608

608-
// Turn off the SpamFireWall if Remote Call is in progress
609-
if($apbct->rc_running || (!empty($spbc) && $spbc->rc_running))
610-
return;
611-
612-
$is_sfw_check = true;
613-
$sfw = new CleantalkSFW();
614-
$sfw->ip_array = (array)$sfw->ip__get(array('real'), true);
615-
616-
// Skip by cookie
617-
foreach($sfw->ip_array as $ct_cur_ip){
618-
if(isset($_COOKIE['ct_sfw_pass_key']) && $_COOKIE['ct_sfw_pass_key'] == md5($ct_cur_ip.$apbct->api_key)){
619-
$is_sfw_check=false;
620-
if(isset($_COOKIE['ct_sfw_passed'])){
621-
$sfw->logs__update($ct_cur_ip, 'passed');
622-
$apbct->data['sfw_counter']['all']++;
623-
$apbct->saveData();
624-
if(!headers_sent())
625-
\Cleantalk\Antispam\Helper::apbct_cookie__set ('ct_sfw_passed', '0', time()+86400*3, '/', null, false, true, 'Lax' );
626-
}
627-
break;
628-
}else{
629-
$is_sfw_check = true;
630-
}
631-
}
632-
633609
// Skip the check
634610
if(!empty($_GET['access'])){
635611
$spbc_settings = get_option('spbc_settings');
636612
$spbc_key = !empty($spbc_settings['spbc_key']) ? $spbc_settings['spbc_key'] : false;
637613
if($_GET['access'] === $apbct->api_key || ($spbc_key !== false && $_GET['access'] === $spbc_key)){
638-
$is_sfw_check = false;
639-
\Cleantalk\Antispam\Helper::apbct_cookie__set('spbc_firewall_pass_key', md5(apbct_get_server_variable( 'REMOTE_ADDR' ) . $spbc_key), time()+1200, '/', '');
640-
\Cleantalk\Antispam\Helper::apbct_cookie__set('ct_sfw_pass_key', md5(apbct_get_server_variable( 'REMOTE_ADDR' ) . $apbct->api_key), time()+1200, '/', null);
614+
\Cleantalk\Common\Helper::apbct_cookie__set('spbc_firewall_pass_key', md5(apbct_get_server_variable( 'REMOTE_ADDR' ) . $spbc_key), time()+1200, '/', '');
615+
\Cleantalk\Common\Helper::apbct_cookie__set('ct_sfw_pass_key', md5(apbct_get_server_variable( 'REMOTE_ADDR' ) . $apbct->api_key), time()+1200, '/', null);
616+
return;
641617
}
642618
unset($spbc_settings, $spbc_key);
643619
}
644620

645-
if($is_sfw_check){
646-
647-
$sfw->ip_check();
648-
649-
// Pass remote calls
650-
if($sfw->pass === false){
651-
if(isset($_GET['spbc_remote_call_token'], $_GET['spbc_remote_call_action'], $_GET['plugin_name'])){
652-
foreach($sfw->blocked_ips as $ip){
653-
$resolved = CleantalkHelper::ip__resolve($ip['ip']);
654-
if($resolved && preg_match('/cleantalk\.org/', $resolved) === 1 || $resolved === 'back'){
655-
$sfw->pass = true;
656-
}
657-
} unset($ip);
658-
}
659-
}
660-
661-
// if($sfw->test){
662-
// $sfw->sfw_die($apbct->api_key, '', parse_url(get_option('siteurl'),PHP_URL_HOST), 'test');
663-
// }
664-
665-
if($sfw->pass === false){
666-
foreach($sfw->blocked_ips as $ip){
667-
$sfw->logs__update($ip['ip'], 'blocked');
668-
}
669-
$apbct->data['sfw_counter']['blocked']++;
670-
$apbct->saveData();
671-
$sfw->sfw_die($apbct->api_key, '', parse_url(get_option('siteurl'),PHP_URL_HOST));
672-
}else{
673-
reset($sfw->passed_ips);
674-
if(!empty($apbct->settings['set_cookies']) && !headers_sent() && key($sfw->passed_ips))
675-
\Cleantalk\Antispam\Helper::apbct_cookie__set( 'ct_sfw_pass_key', md5( $sfw->passed_ips[ key( $sfw->passed_ips ) ]['ip'] . $apbct->api_key ), time() + 86400 * 30, '/', null, false );
676-
}
621+
// Turn off the SpamFireWall if Remote Call is in progress
622+
if($apbct->rc_running || (!empty($spbc) && $spbc->rc_running))
623+
return;
624+
625+
$fw_init_options = array(
626+
'set_cookies' => $apbct->settings['set_cookies']
627+
);
628+
629+
$firewall = new \Cleantalk\Common\Firewall(
630+
\Cleantalk\ApbctWP\DB::getInstance()
631+
);
632+
633+
$firewall->load_fw_module( new \Cleantalk\ApbctWP\Firewall\SFW(
634+
defined( 'APBCT_TBL_FIREWALL_LOG' ) ? APBCT_TBL_FIREWALL_LOG : $this->db->prefix . 'cleantalk_sfw_logs',
635+
defined( 'APBCT_TBL_FIREWALL_DATA' ) ? APBCT_TBL_FIREWALL_DATA : $this->db->prefix . 'cleantalk_sfw',
636+
array(
637+
'sfw_counter' => $apbct->settings['sfw_counter'],
638+
'api_key' => $apbct->api_key,
639+
'apbct' => $apbct,
640+
'cookie_domain' => parse_url( get_option( 'siteurl' ), PHP_URL_HOST ),
641+
'set_cookies' => $apbct->settings['set_cookies'],
642+
)
643+
) );
644+
645+
if( $apbct->settings['sfw__bot_protection']){
646+
$firewall->load_fw_module( new \Cleantalk\ApbctWP\Firewall\AntiBot(
647+
defined( 'APBCT_TBL_FIREWALL_LOG' ) ? APBCT_TBL_FIREWALL_LOG : $this->db->prefix . 'cleantalk_sfw_logs',
648+
defined( 'APBCT_TBL_AC_LOG' ) ? APBCT_TBL_AC_LOG : $this->db->prefix . 'cleantalk_ac_log',
649+
array(
650+
'api_key' => $apbct->api_key,
651+
'apbct' => $apbct,
652+
)
653+
) );
677654
}
678-
unset($is_sfw_check, $sfw, $sfw_ip, $ct_cur_ip);
655+
656+
if( $apbct->settings['sfw__anti_crawler'])
657+
$firewall->load_fw_module( new \Cleantalk\ApbctWP\Firewall\AntiCrawler(
658+
defined( 'APBCT_TBL_FIREWALL_LOG' ) ? APBCT_TBL_FIREWALL_LOG : $this->db->prefix . 'cleantalk_sfw_logs',
659+
defined( 'APBCT_TBL_AC_LOG' ) ? APBCT_TBL_AC_LOG : $this->db->prefix . 'cleantalk_ac_log',
660+
array(
661+
'view_limit' => $apbct->settings['sfw__anti_crawler__view_limit'],
662+
'apbct' => $apbct,
663+
)
664+
) );
665+
666+
$firewall->run();
667+
679668
}
680669

681670
/**
@@ -695,14 +684,16 @@ function apbct_activation( $network = false ) {
695684

696685
// SFW log
697686
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_sfw_logs` (
687+
`id` VARCHAR(40) NOT NULL,
698688
`ip` VARCHAR(15) NOT NULL,
689+
`status` ENUM(\'PASS_SFW\',\'DENY_SFW\',\'PASS_SFW_BY_WHITELIST\',\'PASS_SFW_BY_COOKIE\',\'DENY_ANTIBOT\',\'DENY_ANTICRAWLER\') NULL DEFAULT NULL,
699690
`all_entries` INT NOT NULL,
700691
`blocked_entries` INT NOT NULL,
701692
`entries_timestamp` INT NOT NULL,
702-
PRIMARY KEY (`ip`));';
693+
PRIMARY KEY (`id`));';
703694

704-
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_sfw__flood_logs` (
705-
`id` VARCHAR(32) NOT NULL,
695+
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_ac_log` (
696+
`id` VARCHAR(40) NOT NULL,
706697
`ip` VARCHAR(40) NOT NULL,
707698
`entries` INT DEFAULT 0,
708699
`interval_start` INT NOT NULL,
@@ -793,14 +784,23 @@ function apbct_activation__new_blog($blog_id, $user_id, $domain, $path, $site_id
793784
`mask` int(11) unsigned NOT NULL,
794785
INDEX ( `network` , `mask` )
795786
);';
796-
797-
// SFW log
798-
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_sfw_logs` (
799-
`ip` VARCHAR(15) NOT NULL,
800-
`all_entries` INT NOT NULL,
801-
`blocked_entries` INT NOT NULL,
802-
`entries_timestamp` INT NOT NULL,
803-
PRIMARY KEY (`ip`));';
787+
788+
// SFW log
789+
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_sfw_logs` (
790+
`id` VARCHAR(40) NOT NULL,
791+
`ip` VARCHAR(15) NOT NULL,
792+
`status` ENUM(\'PASS_SFW\',\'DENY_SFW\',\'PASS_SFW_BY_WHITELIST\',\'PASS_SFW_BY_COOKIE\',\'DENY_ANTIBOT\',\'DENY_ANTICRAWLER\') NULL DEFAULT NULL,
793+
`all_entries` INT NOT NULL,
794+
`blocked_entries` INT NOT NULL,
795+
`entries_timestamp` INT NOT NULL,
796+
PRIMARY KEY (`id`));';
797+
798+
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_ac_log` (
799+
`id` VARCHAR(40) NOT NULL,
800+
`ip` VARCHAR(40) NOT NULL,
801+
`entries` INT DEFAULT 0,
802+
`interval_start` INT NOT NULL,
803+
PRIMARY KEY (`id`));';
804804

805805
// Sessions
806806
$sqls[] = 'CREATE TABLE IF NOT EXISTS `%scleantalk_sessions` (
@@ -917,14 +917,16 @@ function apbct_deactivation__delete_common_tables() {
917917
global $wpdb;
918918
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->base_prefix.'cleantalk_sfw`;'); // Deleting SFW data
919919
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->base_prefix.'cleantalk_sfw_logs`;'); // Deleting SFW logs
920+
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->base_prefix.'cleantalk_ac_log`;'); // Deleting SFW logs
920921
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->base_prefix.'cleantalk_sessions`;'); // Deleting session table
921922
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->base_prefix.'cleantalk_spamscan_logs`;'); // Deleting user/comments scan result table
922923
}
923924

924925
function apbct_deactivation__delete_blog_tables() {
925926
global $wpdb;
926927
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->prefix.'cleantalk_sfw`;'); // Deleting SFW data
927-
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->prefix.'cleantalk_sfw_logs`;'); // Deleting SFW logs
928+
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->prefix.'cleantalk_sfw_logs`;'); // Deleting SFW logs
929+
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->prefix.'cleantalk_ac_log`;'); // Deleting SFW logs
928930
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->prefix.'cleantalk_sessions`;'); // Deleting session table
929931
$wpdb->query('DROP TABLE IF EXISTS `'. $wpdb->prefix.'cleantalk_spamscan_logs`;'); // Deleting user/comments scan result table
930932
}
@@ -1015,16 +1017,28 @@ function ct_sfw_update($api_key = '', $immediate = false){
10151017
//Reset previous entries count
10161018
$apbct->stats['sfw']['entries'] = 0;
10171019
$apbct->save('stats');
1018-
1019-
$sfw->sfw_update($api_key, null, $immediate);
1020+
1021+
$result = \Cleantalk\ApbctWP\Firewall\SFW::update(
1022+
\Cleantalk\ApbctWP\DB::getInstance(),
1023+
defined( 'APBCT_TBL_FIREWALL_DATA' ) ? APBCT_TBL_FIREWALL_DATA : $this->db->prefix . 'cleantalk_sfw',
1024+
$api_key,
1025+
null,
1026+
$immediate
1027+
);
10201028

10211029
return ! empty( $result['error'] )
10221030
? $result
10231031
: true;
10241032

10251033
}elseif( is_array( $file_urls ) && count( $file_urls ) ){
10261034

1027-
$result = $sfw->sfw_update($api_key, $file_urls[0], $immediate);
1035+
$result = \Cleantalk\ApbctWP\Firewall\SFW::update(
1036+
\Cleantalk\ApbctWP\DB::getInstance(),
1037+
defined( 'APBCT_TBL_FIREWALL_DATA' ) ? APBCT_TBL_FIREWALL_DATA : $this->db->prefix . 'cleantalk_sfw',
1038+
$api_key,
1039+
$file_urls[0],
1040+
$immediate
1041+
);
10281042

10291043
if( empty( $result['error'] ) ){
10301044

0 commit comments

Comments
 (0)