$keys Key type (ip, prefix, email, device, order) => value. * @param int|null $now Unix time; now when null. * @return array{block:array|null,allow:bool,allow_types:string[],trust:bool,manual:bool} * allow_types lists which kinds of identity carry a "real customer" mark, so the caller * can ignore one it has no proof of (an email address anyone could have typed). */ public static function lookup( array $keys, $now = null ) { global $wpdb; $result = array( 'block' => null, 'allow' => false, 'allow_types' => array(), 'trust' => false, 'manual' => false, ); $value = static function ( $type ) use ( $keys ) { return isset( $keys[ $type ] ) && is_scalar( $keys[ $type ] ) ? (string) $keys[ $type ] : ''; }; if ( '' === $value( 'ip' ) . $value( 'prefix' ) . $value( 'email' ) . $value( 'device' ) . $value( 'order' ) ) { return $result; } $now = null === $now ? time() : (int) $now; $table = Schema::table( 'blocks' ); // One fixed query for the five kinds of identity. An identity that is not known is sent as // an empty value, which matches nothing: entries are never stored with an empty value. // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table, read on every checkout decision. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, kind, ktype, kval, ends_at, strikes, reason, added_by FROM %i WHERE (ktype = 'ip' AND kval = %s) OR (ktype = 'prefix' AND kval = %s) OR (ktype = 'email' AND kval = %s) OR (ktype = 'device' AND kval = %s) OR (ktype = 'order' AND kval = %s)", $table, $value( 'ip' ), $value( 'prefix' ), $value( 'email' ), $value( 'device' ), $value( 'order' ) ), ARRAY_A ); foreach ( (array) $rows as $row ) { if ( '' === (string) $row['kval'] ) { continue; } if ( 'allow' === $row['kind'] ) { $result['allow'] = true; $result['allow_types'][] = (string) $row['ktype']; } elseif ( 'trust' === $row['kind'] ) { $result['trust'] = true; } elseif ( 0 === (int) $row['ends_at'] || (int) $row['ends_at'] > $now ) { if ( 'manual' === $row['added_by'] ) { $result['manual'] = true; $result['block'] = $row; } elseif ( null === $result['block'] ) { $result['block'] = $row; } } } return $result; } /** * Blocks a key for a while. A key that was blocked before is blocked for twice as long * each time, up to 7 days. A block already in force is left as it is. * * @param string $ktype ip, prefix, email, device or order. * @param string $kval Value. * @param int $base Length of a first block, in seconds. * @param string $reason Signal code that caused it. * @param int|null $now Unix time; now when null. * @return int Unix time the block ends, or 0 when nothing was done. */ public static function add( $ktype, $kval, $base, $reason, $now = null ) { global $wpdb; $kval = (string) $kval; if ( '' === $kval ) { return 0; } $now = null === $now ? time() : (int) $now; $table = Schema::table( 'blocks' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $row = $wpdb->get_row( $wpdb->prepare( "SELECT id, ends_at, strikes, added_by FROM %i WHERE kind = 'block' AND ktype = %s AND kval = %s", $table, $ktype, $kval ), ARRAY_A ); if ( $row ) { if ( 0 === (int) $row['ends_at'] || (int) $row['ends_at'] > $now ) { return (int) $row['ends_at']; } $strikes = (int) $row['strikes'] + 1; $ends_at = $now + Limits::block_seconds( $base, $strikes ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table. $wpdb->update( $table, array( 'ends_at' => $ends_at, 'strikes' => $strikes, 'reason' => $reason, ), array( 'id' => (int) $row['id'] ) ); return $ends_at; } $ends_at = $now + Limits::block_seconds( $base, 0 ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO %i (kind, ktype, kval, ends_at, strikes, reason, added_by, created) VALUES ('block', %s, %s, %d, 0, %s, 'auto', %d)", $table, $ktype, $kval, $ends_at, $reason, $now ) ); return $ends_at; } /** * Adds a permanent entry: a manual block, an "allow" mark or a "trust" mark. * * @param string $kind block, allow or trust. * @param string $ktype ip, email or device. * @param string $kval Value. * @param int $ref Event the entry came from. * @param string $label Words shown in Settings for a manual block. */ public static function mark( $kind, $ktype, $kval, $ref = 0, $label = '' ) { global $wpdb; $kval = (string) $kval; if ( '' === $kval ) { return; } $table = Schema::table( 'blocks' ); $reason = 'block' === $kind ? 'manual_block' : ''; $added_by = 'trust' === $kind ? 'auto' : 'manual'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (kind, ktype, kval, ends_at, strikes, reason, added_by, ref, label, created) VALUES (%s, %s, %s, 0, 0, %s, %s, %d, %s, %d) ON DUPLICATE KEY UPDATE ends_at = 0, reason = %s, added_by = %s, ref = %d, label = %s', $table, $kind, $ktype, $kval, $reason, $added_by, (int) $ref, $label, time(), $reason, $added_by, (int) $ref, $label ) ); } /** * Removes entries of one kind for the given keys. * * @param string $kind block, allow or trust. * @param array $keys Key type => value. */ public static function remove( $kind, array $keys ) { global $wpdb; $table = Schema::table( 'blocks' ); foreach ( array_filter( $keys, 'strlen' ) as $type => $value ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table. $wpdb->delete( $table, array( 'kind' => $kind, 'ktype' => $type, 'kval' => (string) $value, ) ); } } /** * Whether "mark as real" or "always block" was used on the visitor behind an event. * * @param int $event_id Event id. * @return array{markedReal:bool,alwaysBlocked:bool} */ public static function marks_for_event( $event_id ) { global $wpdb; $table = Schema::table( 'blocks' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $kinds = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT kind FROM %i WHERE ref = %d AND added_by = 'manual'", $table, (int) $event_id ) ); return array( 'markedReal' => in_array( 'allow', (array) $kinds, true ), 'alwaysBlocked' => in_array( 'block', (array) $kinds, true ), ); } /** * The visitors a merchant chose to always block, one line per visitor. * * @return array */ public static function manual_list() { global $wpdb; $table = Schema::table( 'blocks' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT MIN(id) AS id, ref, MAX(label) AS label, MIN(created) AS created FROM %i WHERE kind = 'block' AND added_by = 'manual' GROUP BY ref ORDER BY created DESC LIMIT 200", $table ), ARRAY_A ); $out = array(); foreach ( (array) $rows as $row ) { $out[] = array( 'id' => (int) $row['id'], 'label' => '' !== $row['label'] ? (string) $row['label'] : __( 'A visitor you blocked', 'brickmoat' ), 'addedAt' => Util::iso( (int) $row['created'] ), ); } return $out; } /** * Removes a manual block (every identity of that visitor). * * @param int $id Id from manual_list(). * @return bool Whether anything was removed. */ public static function remove_manual( $id ) { global $wpdb; $table = Schema::table( 'blocks' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $row = $wpdb->get_row( $wpdb->prepare( "SELECT id, ref FROM %i WHERE id = %d AND kind = 'block' AND added_by = 'manual'", $table, (int) $id ), ARRAY_A ); if ( ! $row ) { return false; } if ( (int) $row['ref'] > 0 ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE kind = 'block' AND added_by = 'manual' AND ref = %d", $table, (int) $row['ref'] ) ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table. $wpdb->delete( $table, array( 'id' => (int) $row['id'] ) ); } return true; } /** * Removes automatic blocks that ended more than 7 days ago. They are kept that long so a * repeat offender's next block is longer. */ public static function prune() { global $wpdb; $table = Schema::table( 'blocks' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE kind = 'block' AND added_by = 'auto' AND ends_at > 0 AND ends_at < %d", $table, time() - Limits::MAX_BLOCK ) ); } }