time() ) { return $cached['blocked'] ? $cached : false; } } $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); $row = $wpdb->get_row( $wpdb->prepare( "SELECT type, reason, locked_until FROM %i WHERE ip = %s AND (locked_until IS NULL OR locked_until > %s) ORDER BY id DESC LIMIT 1", $table, $ip, gmdate( 'Y-m-d H:i:s' ) ), ARRAY_A ); if ( $row ) { $data = array( 'blocked' => true, 'type' => $row['type'], 'reason' => $row['reason'], 'until' => $row['locked_until'] ? strtotime( $row['locked_until'] . ' UTC' ) : 0, ); set_transient( $key, $data, MINUTE_IN_SECONDS ); return $data; } set_transient( $key, array( 'blocked' => false, 'until' => 0 ), MINUTE_IN_SECONDS ); return false; } /** * Block an IP. * * @since 1.0.0 * @param string $ip IP. * @param string $type login|firewall|rate|manual * @param string $reason Human reason. * @param int $minutes 0 = permanent. * @return bool */ public static function block( $ip, $type, $reason, $minutes ) { global $wpdb; if ( ! Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $ip ) ) { return false; } $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); $until = $minutes > 0 ? gmdate( 'Y-m-d H:i:s', time() + (int) $minutes * MINUTE_IN_SECONDS ) : null; $ok = $wpdb->insert( $table, array( 'ip' => $ip, 'type' => substr( $type, 0, 16 ), 'reason' => Softvolt_Ai_Bodyguard_Helpers::trim_text( $reason, 255 ), 'hits' => 1, 'locked_until' => $until, 'created_at' => gmdate( 'Y-m-d H:i:s' ), ) ); delete_transient( 'svabg_blk_' . md5( $ip ) ); Softvolt_Ai_Bodyguard_Logger::log( 'warning', 'login' === $type ? 'login_lockout' : 'ip_blocked', sprintf( /* translators: 1: IP address, 2: type, 3: reason */ __( 'IP %1$s blocked (%2$s): %3$s', 'ai-guardwall' ), $ip, $type, $reason ), array( 'minutes' => (int) $minutes ), null, $ip ); do_action( 'svabg_lockout', $ip, $type, $reason ); return (bool) $ok; } public static function unblock( $ip ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); $n = $wpdb->query( $wpdb->prepare( "UPDATE %i SET locked_until = %s WHERE ip = %s AND (locked_until IS NULL OR locked_until > %s)", $table, gmdate( 'Y-m-d H:i:s', time() - 1 ), $ip, gmdate( 'Y-m-d H:i:s' ) ) ); delete_transient( 'svabg_blk_' . md5( $ip ) ); delete_transient( 'svabg_lf_' . md5( $ip ) ); delete_transient( 'svabg_fh_' . md5( $ip ) ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'ip_blocked', sprintf( /* translators: %s: IP address */ __( 'IP %s unblocked.', 'ai-guardwall' ), $ip ) ); return (int) $n; } public static function purge_expired() { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); return $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE locked_until IS NOT NULL AND locked_until < %s", $table, gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ) ) ); } public static function active( $limit = 100 ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE locked_until IS NULL OR locked_until > %s ORDER BY id DESC LIMIT %d", $table, gmdate( 'Y-m-d H:i:s' ), (int) $limit ), ARRAY_A ); foreach ( (array) $rows as $i => $row ) { $rows[ $i ]['until'] = $row['locked_until']; // The key the screens and the REST answer use. } return $rows ? $rows : array(); } public static function count_active() { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT ip) FROM %i WHERE locked_until IS NULL OR locked_until > %s", $table, gmdate( 'Y-m-d H:i:s' ) ) ); } public static function count_since( $seconds ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE created_at >= %s", $table, gmdate( 'Y-m-d H:i:s', time() - (int) $seconds ) ) ); } /** * Lockouts of one IP in a period (for escalating to a long lockout). */ public static function recent_count( $ip, $seconds ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'lockouts' ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE ip = %s AND created_at >= %s", $table, $ip, gmdate( 'Y-m-d H:i:s', time() - (int) $seconds ) ) ); } }