'; echo ''; // Filled by the browser: cached pages and bots without JavaScript are told apart. echo ''; wp_register_script( 'svabg-comments-guard', false, array(), SOFTVOLT_AI_BODYGUARD_VERSION, true ); wp_enqueue_script( 'svabg-comments-guard' ); wp_add_inline_script( 'svabg-comments-guard', '(function(){var f=document.querySelectorAll(\'input[name="svabg_cm_js"]\');for(var i=0;i 0 && time() - (int) $m[1] < $min ) { return 'too_fast'; } if ( Softvolt_Ai_Bodyguard_Settings::get( 'comment_block_proxies', 0 ) && self::via_proxy() ) { return 'proxy'; } return ''; } /** * Proxy headers on a request that did not come through a proxy the site trusts. */ private static function via_proxy() { if ( '' !== Softvolt_Ai_Bodyguard_Helpers::server( 'HTTP_CF_CONNECTING_IP' ) || 'REMOTE_ADDR' !== Softvolt_Ai_Bodyguard_Settings::get( 'ip_source', 'auto' ) && 'auto' !== Softvolt_Ai_Bodyguard_Settings::get( 'ip_source', 'auto' ) ) { return false; } $remote = Softvolt_Ai_Bodyguard_Helpers::server( 'REMOTE_ADDR' ); $trusted = (string) Softvolt_Ai_Bodyguard_Settings::get( 'trusted_proxies', '' ); if ( '' !== trim( $trusted ) && Softvolt_Ai_Bodyguard_Helpers::ip_in_list( $remote, $trusted ) ) { return false; } foreach ( array( 'HTTP_VIA', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED', 'HTTP_X_FORWARDED', 'HTTP_CLIENT_IP', 'HTTP_FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION', 'HTTP_X_PROXY_ID' ) as $h ) { if ( '' !== Softvolt_Ai_Bodyguard_Helpers::server( $h ) ) { return true; } } return false; } public static function check( $data ) { self::$verdict = ''; if ( is_user_logged_in() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( isset( $data['comment_type'] ) && in_array( $data['comment_type'], array( 'pingback', 'trackback' ), true ) ) ) { return $data; } $why = self::reason( $data ); if ( '' === $why ) { return $data; } $ip = Softvolt_Ai_Bodyguard_Helpers::client_ip(); Softvolt_Ai_Bodyguard_Logger::log_throttled( 'cmspam' . $ip, 10, 'notice', 'comment_spam', sprintf( /* translators: 1: why, 2: IP address */ __( 'Comment spam stopped (%1$s) from %2$s.', 'ai-guardwall' ), $why, $ip ) ); self::bump(); if ( 'discard' === Softvolt_Ai_Bodyguard_Settings::get( 'comment_spam_action', 'spam' ) ) { self::count_and_block( $ip ); wp_die( esc_html__( 'Your comment could not be posted. If you are a person, please go back, wait a few seconds and try again.', 'ai-guardwall' ), esc_html__( 'Comment not posted', 'ai-guardwall' ), array( 'response' => 403, 'back_link' => true ) ); } self::$verdict = $why; return $data; } public static function approved( $approved, $data ) { return '' !== self::$verdict ? 'spam' : $approved; } public static function inserted( $id, $comment ) { if ( is_object( $comment ) && 'spam' === $comment->comment_approved ) { self::count_and_block( (string) $comment->comment_author_IP ); } } public static function marked_spam( $id ) { $c = get_comment( $id ); if ( $c ) { self::count_and_block( (string) $c->comment_author_IP ); } } /** * Lock out an address that has sent the configured number of spam comments. */ public static function count_and_block( $ip ) { global $wpdb; $after = (int) Softvolt_Ai_Bodyguard_Settings::get( 'comment_block_after', 3 ); if ( $after <= 0 || ! Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $ip ) || Softvolt_Ai_Bodyguard_Helpers::ip_in_list( $ip, Softvolt_Ai_Bodyguard_Settings::get( 'firewall_allowlist_ips', '' ) ) ) { return; } $stored = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_author_IP = %s AND comment_approved = 'spam'", $ip ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $tried = Softvolt_Ai_Bodyguard_Firewall::count( 'svabg_cms_' . md5( $ip ), DAY_IN_SECONDS ); if ( max( $stored, $tried ) >= $after && ! Softvolt_Ai_Bodyguard_Lockouts::is_blocked( $ip ) ) { Softvolt_Ai_Bodyguard_Lockouts::block( $ip, 'spam', sprintf( /* translators: %d: stored */ __( '%d spam comments', 'ai-guardwall' ), max( $stored, $tried ) ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'comment_block_minutes', 1440 ) ); } } private static function bump() { $n = (int) get_option( 'svabg_comment_spam_stopped', 0 ); update_option( 'svabg_comment_spam_stopped', $n + 1, false ); } /** * Daily: empty spam older than the configured number of days. * * @return int Deleted. */ public static function empty_old_spam() { global $wpdb; $days = (int) Softvolt_Ai_Bodyguard_Settings::get( 'comment_trash_days', 14 ); if ( $days <= 0 || ! Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'comment_spam' ) ) { return 0; } $ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_date_gmt < %s LIMIT 500", gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $ids as $id ) { wp_delete_comment( (int) $id, true ); } return count( $ids ); } /** * Addresses that sent the most spam. */ public static function spammers( $limit = 50 ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT comment_author_IP AS ip, COUNT(*) AS n, MAX(comment_date_gmt) AS last FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_author_IP <> '' GROUP BY comment_author_IP ORDER BY n DESC LIMIT %d", (int) $limit ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $rows as &$r ) { $r['n'] = (int) $r['n']; $r['blocked'] = (bool) Softvolt_Ai_Bodyguard_Lockouts::is_blocked( $r['ip'] ); $r['last_h'] = $r['last'] ? human_time_diff( strtotime( $r['last'] . ' UTC' ) ) : ''; } return $rows; } public static function stats() { global $wpdb; return array( 'stopped' => (int) get_option( 'svabg_comment_spam_stopped', 0 ), 'spam' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved = 'spam'" ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery ); } }