| deactivate | refresh * wp brickmoat cleanup [run|undo] [--attack=] * * @package Brickmoat */ namespace Brickmoat; defined( 'ABSPATH' ) || exit; /** * Manage Brickmoat from the command line. */ final class Cli { /** * Shows the current mode, attack, licence and the numbers the attack triggers look at. * * ## OPTIONS * * [--format=] * : "table" (default) or "json". * * @param array $args Positional arguments. * @param array $assoc Named arguments. */ public function status( $args, $assoc ) { unset( $args ); global $wpdb; $state = Lockdown::state( true ); $stats = Lockdown::stats(); $licence = Licence::to_rest(); $network = Network::to_rest(); $events = Schema::table( 'events' ); $blocks = Schema::table( 'blocks' ); // phpcs:disable WordPress.DB.DirectDatabaseQuery -- The plugin's own tables. $data = array( 'version' => BRICKMOAT_VERSION, 'mode' => Lockdown::is_on() ? 'lockdown' : 'normal', 'lockdown_reason' => (string) $state['reason'], 'lockdown_until' => (string) Util::iso( $state['until'] ), 'open_attack' => $state['attack_id'], 'attempts_15m' => (int) $stats['attempts15'], 'declines_15m' => (int) $stats['declines15'], 'stopped_5m' => (int) $stats['stopped5'], 'baseline_15m' => (float) $stats['baseline15'], 'events' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $events ) ), 'active_blocks' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE kind = 'block' AND (ends_at = 0 OR ends_at > %d)", $blocks, time() ) ), 'counters' => Counters::uses_cache() ? 'object cache' : 'table', 'licence' => $licence['status'] . ( $licence['proActive'] ? ' (' . $licence['plan'] . ', Pro on)' : '' ), 'licence_key' => $licence['keyMasked'], 'network' => $network['state'], 'network_entries' => $network['entries'], 'network_updated' => (string) $network['updatedAt'], 'reports_waiting' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE report_state = 1', $events ) ), 'api_base' => Config::api_base(), ); // phpcs:enable if ( isset( $assoc['format'] ) && 'json' === $assoc['format'] ) { \WP_CLI::line( wp_json_encode( $data ) ); return; } foreach ( $data as $key => $value ) { \WP_CLI::line( str_pad( $key, 18 ) . $value ); } } /** * Switches lockdown on (as the "Under attack" button does, for 24 hours) or off. * * ## OPTIONS * * * : "on" or "off". * * @param array $args Positional arguments. */ public function lockdown( $args ) { $want = isset( $args[0] ) ? $args[0] : ''; if ( 'on' === $want ) { Lockdown::start(); } elseif ( 'off' === $want ) { Lockdown::stop(); } else { \WP_CLI::error( 'Say "on" or "off".' ); } \WP_CLI::success( 'Lockdown is ' . ( Lockdown::is_on() ? 'on' : 'off' ) . '.' ); } /** * Clears Brickmoat's event log, counters, blocks, attacks and lockdown. Settings, the licence * and the shared list are kept unless --all is given. Orders are never touched. * * ## OPTIONS * * [--all] * : Also forget the settings, the licence and the shared list. * * [--yes] * : Do not ask for confirmation. * * @param array $args Positional arguments. * @param array $assoc Named arguments. */ public function reset( $args, $assoc ) { unset( $args ); global $wpdb; \WP_CLI::confirm( 'Clear Brickmoat\'s event log, counters, blocks, attacks and lockdown on this site?', $assoc ); foreach ( array( 'events', 'counters', 'blocks', 'attacks' ) as $name ) { $table = Schema::table( $name ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Empties the plugin's own table. $wpdb->query( $wpdb->prepare( 'TRUNCATE TABLE %i', $table ) ); } Counters::clear(); Decider::reset(); Lockdown::reset(); Cleanup::reset(); Network::dismiss_warning(); delete_transient( 'brickmoat_protected_routes' ); delete_transient( 'brickmoat_sweep_queued' ); if ( ! empty( $assoc['all'] ) ) { Licence::deactivate(); delete_option( Settings::OPTION ); delete_option( Settings::ONBOARDING_OPTION ); Settings::flush(); } \WP_CLI::success( 'Brickmoat was reset. Mode: normal.' ); } /** * Runs one of the scheduled jobs right now. * * ## OPTIONS * * * : minute, sync, baseline, hourly, daily, sweep or reports. * * @param array $args Positional arguments. */ public function run( $args ) { $job = isset( $args[0] ) ? $args[0] : ''; switch ( $job ) { case 'minute': Scheduler::run_minute(); break; case 'sync': $result = Pro_Sync::run(); Reporter::flush(); Reporter::send_attacks(); \WP_CLI::line( 'check-in: ' . ( $result['checkin'] ? 'ok' : 'failed or not licensed' ) . ', shared list: ' . ( $result['blocklist'] ? 'ok' : 'failed or not licensed' ) ); break; case 'baseline': Scheduler::run_baseline(); break; case 'hourly': Scheduler::run_hourly(); break; case 'daily': Scheduler::run_daily(); break; case 'sweep': Outcomes::sweep(); break; case 'reports': Reporter::flush(); Reporter::send_attacks(); break; default: \WP_CLI::error( 'Unknown job. Use minute, sync, baseline, hourly, daily, sweep or reports.' ); } \WP_CLI::success( 'Ran "' . $job . '".' ); } /** * Activates, removes or re-checks the Pro licence. * * ## OPTIONS * * * : activate, deactivate or refresh. * * [] * : The licence key (for activate). * * @param array $args Positional arguments. */ public function licence( $args ) { $action = isset( $args[0] ) ? $args[0] : ''; if ( 'activate' === $action ) { $done = Licence::activate( isset( $args[1] ) ? $args[1] : '' ); if ( is_wp_error( $done ) ) { \WP_CLI::error( $done->get_error_message() ); } } elseif ( 'deactivate' === $action ) { Licence::deactivate(); } elseif ( 'refresh' === $action ) { Licence::refresh( true ); } else { \WP_CLI::error( 'Use activate , deactivate or refresh.' ); } $licence = Licence::to_rest(); \WP_CLI::success( 'Licence: ' . $licence['status'] . ', plan ' . $licence['plan'] . ', Pro ' . ( $licence['proActive'] ? 'on' : 'off' ) . ( '' !== $licence['message'] ? ' — ' . $licence['message'] : '' ) ); } /** * Shows, runs or undoes the junk-order cleanup. "run" and "undo" work through every batch * before returning. * * ## OPTIONS * * [] * : show (default), run or undo. * * [--attack=] * : The attack to clean up after. Defaults to the latest one; 0 with "run" sweeps the last 7 days. * * @param array $args Positional arguments. * @param array $assoc Named arguments. */ public function cleanup( $args, $assoc ) { $action = isset( $args[0] ) ? $args[0] : 'show'; $attack_id = isset( $assoc['attack'] ) ? absint( $assoc['attack'] ) : 0; if ( 'run' === $action || 'undo' === $action ) { if ( 'run' === $action ) { if ( ! isset( $assoc['attack'] ) ) { $target = Cleanup::target(); $attack_id = $target ? (int) $target['id'] : 0; } $started = Cleanup::run( $attack_id ); } else { $job = Cleanup::job_to_rest(); $started = Cleanup::undo( (string) $job['batchId'] ); } if ( is_wp_error( $started ) ) { \WP_CLI::error( $started->get_error_message() ); } for ( $i = 0; $i < 1000; $i++ ) { $job = Cleanup::job_to_rest(); if ( 'running' !== $job['status'] ) { break; } Cleanup::process_batch( (string) $job['batchId'] ); } } $view = Cleanup::view( $attack_id ); \WP_CLI::line( wp_json_encode( array_diff_key( $view, array( 'orders' => 1 ) ) ) ); } }