prefix . 'brickmoat_' . $name; } /** * Creates or upgrades the tables when the stored version is behind. */ public static function maybe_install() { if ( get_option( self::DB_VERSION_OPTION ) === self::DB_VERSION ) { return; } // When the tables could not be created, try again at most once every ten minutes, not on every page. if ( get_transient( 'brickmoat_install_retry' ) ) { return; } if ( self::install() ) { // The shared list may be in a new or emptied table: download it whole next time, not as changes. delete_option( Network::ETAG_OPTION ); } else { set_transient( 'brickmoat_install_retry', 1, 10 * MINUTE_IN_SECONDS ); } } /** * The tables that do not exist. * * @return string[] Short names: events, counters, blocks, attacks, network. */ public static function missing_tables() { global $wpdb; $missing = array(); foreach ( self::TABLES as $name ) { $table = self::table( $name ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Checks that the plugin's own table exists; not worth caching. if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) !== $table ) { $missing[] = $name; } } return $missing; } /** * The names of a table's columns, or nothing when the table does not exist. * * @param string $table Full table name. * @return string[] */ private static function columns( $table ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Checks that the plugin's own table exists; runs during an upgrade only. if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) !== $table ) { return array(); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Reads the columns of the plugin's own table; runs during an upgrade only. return (array) $wpdb->get_col( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $table ) ); } /** * Earlier builds had four columns whose names are also words of the SQL language: "until", * "level" and "source" in the blocks table, and "data" in the attacks table. They are now * "ends_at", "strikes", "added_by" and "details", so that no column or index of the plugin * shares its name with a keyword of any database WordPress runs on. A table made by an * earlier build is renamed in place, and its rows are kept. */ private static function rename_old_columns() { global $wpdb; $blocks = self::table( 'blocks' ); $attacks = self::table( 'attacks' ); // phpcs:disable WordPress.DB.DirectDatabaseQuery -- One-off upgrade of the plugin's own tables. $columns = self::columns( $blocks ); if ( in_array( 'until', $columns, true ) && ! in_array( 'ends_at', $columns, true ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Renames a column of the plugin's own table. $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i CHANGE COLUMN %i ends_at int(10) unsigned NOT NULL DEFAULT 0', $blocks, 'until' ) ); $indexes = (array) $wpdb->get_col( $wpdb->prepare( 'SHOW INDEX FROM %i', $blocks ), 2 ); if ( in_array( 'until', $indexes, true ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Drops the old index; dbDelta adds the new one. $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i DROP INDEX %i', $blocks, 'until' ) ); } } if ( in_array( 'level', $columns, true ) && ! in_array( 'strikes', $columns, true ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Renames a column of the plugin's own table. $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i CHANGE COLUMN %i strikes tinyint(3) unsigned NOT NULL DEFAULT 0', $blocks, 'level' ) ); } if ( in_array( 'source', $columns, true ) && ! in_array( 'added_by', $columns, true ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Renames a column of the plugin's own table. $wpdb->query( $wpdb->prepare( "ALTER TABLE %i CHANGE COLUMN %i added_by varchar(8) NOT NULL DEFAULT 'auto'", $blocks, 'source' ) ); } $columns = self::columns( $attacks ); if ( in_array( 'data', $columns, true ) && ! in_array( 'details', $columns, true ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Renames a column of the plugin's own table. $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i CHANGE COLUMN %i details longtext NULL', $attacks, 'data' ) ); } // phpcs:enable } /** * Creates or upgrades every table. The version is only recorded once every table exists, * so a failed attempt is tried again instead of being taken for done. * * @return bool Whether every table exists afterwards. */ public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; self::rename_old_columns(); $collate = $wpdb->get_charset_collate(); $events = self::table( 'events' ); $counters = self::table( 'counters' ); $blocks = self::table( 'blocks' ); $attacks = self::table( 'attacks' ); $network = self::table( 'network' ); // Every decision, with its reasons. Kept 7, 30 or 90 days, as the store chooses. dbDelta( "CREATE TABLE {$events} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, ts int(10) unsigned NOT NULL DEFAULT 0, route varchar(24) NOT NULL DEFAULT '', decision varchar(10) NOT NULL DEFAULT '', score smallint(5) unsigned NOT NULL DEFAULT 0, reasons varchar(160) NOT NULL DEFAULT '', signals text NULL, ip varchar(45) NOT NULL DEFAULT '', ip_prefix varchar(50) NOT NULL DEFAULT '', country varchar(2) NOT NULL DEFAULT '', hosting varchar(40) NOT NULL DEFAULT '', email_hash varchar(64) NOT NULL DEFAULT '', device_hash varchar(64) NOT NULL DEFAULT '', addr_hash varchar(32) NOT NULL DEFAULT '', sid varchar(40) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, order_id bigint(20) unsigned NOT NULL DEFAULT 0, attack_id bigint(20) unsigned NOT NULL DEFAULT 0, outcome varchar(20) NOT NULL DEFAULT 'unknown', passed tinyint(1) NOT NULL DEFAULT 0, network tinyint(1) NOT NULL DEFAULT 0, policy tinyint(1) NOT NULL DEFAULT 0, gateway varchar(40) NOT NULL DEFAULT '', total decimal(14,2) NULL, items smallint(5) unsigned NOT NULL DEFAULT 0, report_state tinyint(1) NOT NULL DEFAULT 0, report_type varchar(20) NOT NULL DEFAULT '', report_email varchar(190) NULL, PRIMARY KEY (id), KEY ts (ts), KEY decision_ts (decision,ts), KEY outcome_ts (outcome,ts), KEY ip (ip), KEY ip_prefix (ip_prefix), KEY email_hash (email_hash), KEY device_hash (device_hash), KEY order_id (order_id), KEY attack_id (attack_id), KEY report_state (report_state), KEY sid (sid) ) {$collate};" ); // One-minute counter buckets. Used only when the site has no persistent object cache. dbDelta( "CREATE TABLE {$counters} ( ckey varchar(80) NOT NULL, bucket int(10) unsigned NOT NULL DEFAULT 0, n int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (ckey,bucket), KEY bucket (bucket) ) {$collate};" ); // Blocks (self-expiring), plus the visitors a merchant marked as real and devices that paid. dbDelta( "CREATE TABLE {$blocks} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, kind varchar(8) NOT NULL DEFAULT 'block', ktype varchar(8) NOT NULL DEFAULT '', kval varchar(64) NOT NULL DEFAULT '', ends_at int(10) unsigned NOT NULL DEFAULT 0, strikes tinyint(3) unsigned NOT NULL DEFAULT 0, reason varchar(40) NOT NULL DEFAULT '', added_by varchar(8) NOT NULL DEFAULT 'auto', ref bigint(20) unsigned NOT NULL DEFAULT 0, label varchar(190) NOT NULL DEFAULT '', created int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY entry (kind,ktype,kval), KEY lookup (ktype,kval), KEY ends_at (ends_at), KEY ref (ref) ) {$collate};" ); // Attack windows and their summary numbers. Kept 1 year. dbDelta( "CREATE TABLE {$attacks} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, started int(10) unsigned NOT NULL DEFAULT 0, detected int(10) unsigned NOT NULL DEFAULT 0, lockdown_at int(10) unsigned NOT NULL DEFAULT 0, ended int(10) unsigned NOT NULL DEFAULT 0, trigger_name varchar(20) NOT NULL DEFAULT '', attempts int(10) unsigned NOT NULL DEFAULT 0, declined int(10) unsigned NOT NULL DEFAULT 0, blocked int(10) unsigned NOT NULL DEFAULT 0, challenged int(10) unsigned NOT NULL DEFAULT 0, unique_ips int(10) unsigned NOT NULL DEFAULT 0, unique_devices int(10) unsigned NOT NULL DEFAULT 0, junk_orders int(10) unsigned NOT NULL DEFAULT 0, junk_cleaned int(10) unsigned NOT NULL DEFAULT 0, emails_held int(10) unsigned NOT NULL DEFAULT 0, baseline decimal(10,4) NOT NULL DEFAULT 0, report_url varchar(255) NOT NULL DEFAULT '', shared tinyint(1) NOT NULL DEFAULT 0, details longtext NULL, PRIMARY KEY (id), KEY started (started), KEY ended (ended) ) {$collate};" ); // Pro: the shared list of attackers, downloaded every 5 minutes. dbDelta( "CREATE TABLE {$network} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, ntype varchar(8) NOT NULL DEFAULT '', nvalue varchar(64) NOT NULL DEFAULT '', score tinyint(3) unsigned NOT NULL DEFAULT 0, expires int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY entry (ntype,nvalue), KEY expires (expires) ) {$collate};" ); $missing = self::missing_tables(); if ( ! empty( $missing ) ) { update_option( self::MISSING_OPTION, $missing, false ); Util::log( 'Could not create the tables: ' . implode( ', ', $missing ) . ( '' !== (string) $wpdb->last_error ? '. The database said: ' . $wpdb->last_error : '' ) ); return false; } delete_option( self::MISSING_OPTION ); delete_transient( 'brickmoat_install_retry' ); // Slack alerts are sent by the plugin itself since version 5; the old "connected on the service" flag is no longer used. delete_option( 'brickmoat_slack_connected' ); update_option( self::DB_VERSION_OPTION, self::DB_VERSION ); return true; } /** * Removes every table. Used by uninstall only. */ public static function drop() { global $wpdb; foreach ( self::TABLES as $name ) { $table = self::table( $name ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Removes the plugin's own tables. $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); } delete_option( self::DB_VERSION_OPTION ); delete_option( self::MISSING_OPTION ); } }