'balanced', 'autoLockdown' => true, 'challengeGuestsInLockdown' => true, 'blockUnknownInLockdown' => true, 'guestMinimumInLockdown' => null, 'allowReturningCustomers' => true, 'allowStaff' => true, 'allowEmails' => array(), 'allowIps' => array(), 'alertEmails' => array(), 'alertOnStart' => true, 'alertOnEnd' => true, 'dailyDigest' => false, 'slackWebhook' => '', 'turnstileSiteKey' => '', 'turnstileSecret' => '', 'proxy' => 'auto', 'historyDays' => 30, ); } /** How long the event log may be kept, in days. Every choice is free. */ const HISTORY_CHOICES = array( 7, 30, 90 ); /** * All settings, defaults filled in. * * @return array */ public static function get() { if ( null === self::$cache ) { $stored = get_option( self::OPTION, array() ); self::$cache = array_merge( self::defaults(), is_array( $stored ) ? array_intersect_key( $stored, self::defaults() ) : array() ); } return self::$cache; } /** * One setting. * * @param string $key Setting name. * @return mixed */ public static function value( $key ) { $all = self::get(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Forgets the settings read this request (after a change, and in tests). */ public static function flush() { self::$cache = null; } /** * This site's secret, used to sign tokens and to hash emails. Created on first use. * * @return string */ public static function secret() { $secret = get_option( self::SECRET_OPTION, '' ); if ( ! is_string( $secret ) || strlen( $secret ) < 32 ) { try { $secret = bin2hex( random_bytes( 32 ) ); } catch ( \Exception $e ) { $secret = wp_generate_password( 64, false, false ); } update_option( self::SECRET_OPTION, $secret, true ); } return $secret; } /** * The keyed hash stored instead of an email address. * * @param string $email Address. * @return string */ public static function email_hash( $email ) { return Email::hash( $email, self::secret() ); } /** * Where alerts go: the addresses the merchant chose, or the site's admin address. * * @return string[] */ public static function alert_recipients() { $emails = array_filter( (array) self::value( 'alertEmails' ), 'is_email' ); if ( empty( $emails ) ) { $admin = get_option( 'admin_email' ); $emails = is_email( $admin ) ? array( $admin ) : array(); } return array_values( array_unique( $emails ) ); } /** * Checks and saves a set of changes. * * @param array $changes Any subset of the settings, as sent by the admin screen. * @return true|\WP_Error */ public static function update( array $changes ) { $next = self::get(); foreach ( array( 'autoLockdown', 'challengeGuestsInLockdown', 'blockUnknownInLockdown', 'allowReturningCustomers', 'allowStaff', 'alertOnStart', 'alertOnEnd', 'dailyDigest' ) as $flag ) { if ( array_key_exists( $flag, $changes ) ) { $next[ $flag ] = (bool) rest_sanitize_boolean( $changes[ $flag ] ); } } if ( array_key_exists( 'level', $changes ) ) { if ( ! in_array( $changes['level'], array( 'relaxed', 'balanced', 'strict' ), true ) ) { return self::invalid( __( 'Choose Relaxed, Balanced or Strict as the protection level.', 'brickmoat' ) ); } $next['level'] = $changes['level']; } if ( array_key_exists( 'historyDays', $changes ) ) { if ( ! in_array( (int) $changes['historyDays'], self::HISTORY_CHOICES, true ) ) { return self::invalid( __( 'Choose 7, 30 or 90 days of history.', 'brickmoat' ) ); } $next['historyDays'] = (int) $changes['historyDays']; } if ( array_key_exists( 'proxy', $changes ) ) { if ( ! in_array( $changes['proxy'], array( 'auto', 'direct', 'cloudflare', 'forwarded' ), true ) ) { return self::invalid( __( 'That is not one of the ways Brickmoat can find a visitor’s address.', 'brickmoat' ) ); } $next['proxy'] = $changes['proxy']; } if ( array_key_exists( 'guestMinimumInLockdown', $changes ) ) { $minimum = $changes['guestMinimumInLockdown']; if ( null === $minimum || '' === $minimum ) { $next['guestMinimumInLockdown'] = null; } elseif ( ! is_numeric( $minimum ) || (float) $minimum < 0 ) { return self::invalid( __( 'The minimum order total must be a number, or left empty.', 'brickmoat' ) ); } else { $next['guestMinimumInLockdown'] = round( (float) $minimum, 2 ); } } foreach ( array( 'allowEmails', 'alertEmails' ) as $list ) { if ( array_key_exists( $list, $changes ) ) { $emails = array(); foreach ( self::as_list( $changes[ $list ] ) as $entry ) { $email = sanitize_email( $entry ); if ( '' === $email || ! is_email( $email ) ) { /* translators: %s: what the merchant typed. */ return self::invalid( sprintf( __( '“%s” is not an email address.', 'brickmoat' ), sanitize_text_field( $entry ) ) ); } $emails[] = strtolower( $email ); } $next[ $list ] = array_slice( array_values( array_unique( $emails ) ), 0, 'alertEmails' === $list ? 10 : 500 ); } } if ( array_key_exists( 'allowIps', $changes ) ) { $ips = array(); foreach ( self::as_list( $changes['allowIps'] ) as $entry ) { $entry = strtolower( sanitize_text_field( $entry ) ); if ( ! Ip::is_valid_entry( $entry ) ) { /* translators: %s: what the merchant typed. */ return self::invalid( sprintf( __( '“%s” is not an internet address. It should look like 203.0.113.7.', 'brickmoat' ), $entry ) ); } $ips[] = $entry; } $next['allowIps'] = array_slice( array_values( array_unique( $ips ) ), 0, 500 ); } if ( array_key_exists( 'slackWebhook', $changes ) ) { $webhook = trim( (string) $changes['slackWebhook'] ); if ( '' !== $webhook && ! preg_match( self::SLACK_PATTERN, $webhook ) ) { return self::invalid( __( 'That does not look like a Slack webhook address. It should start with https://hooks.slack.com/.', 'brickmoat' ) ); } $next['slackWebhook'] = substr( $webhook, 0, 300 ); } if ( array_key_exists( 'turnstileSiteKey', $changes ) ) { $next['turnstileSiteKey'] = substr( sanitize_text_field( (string) $changes['turnstileSiteKey'] ), 0, 100 ); } if ( array_key_exists( 'turnstileSecret', $changes ) ) { $next['turnstileSecret'] = substr( sanitize_text_field( (string) $changes['turnstileSecret'] ), 0, 200 ); } update_option( self::OPTION, $next, true ); self::$cache = null; return true; } /** * Turns a value from the admin screen into a list of non-empty strings. * * @param mixed $value Array, or a comma/line separated string. * @return string[] */ private static function as_list( $value ) { if ( is_string( $value ) ) { $value = preg_split( '/[\s,;]+/', $value ); } $out = array(); foreach ( (array) $value as $entry ) { if ( is_scalar( $entry ) && '' !== trim( (string) $entry ) ) { $out[] = trim( (string) $entry ); } } return $out; } /** * An error for a setting that cannot be saved. * * @param string $message Plain-English problem. * @return \WP_Error */ private static function invalid( $message ) { return new \WP_Error( 'brickmoat_invalid_setting', $message, array( 'status' => 400 ) ); } /** * Days of event history kept on this site: the store's own choice of 7, 30 or 90. * * @return int */ public static function history_days() { $days = (int) self::value( 'historyDays' ); return in_array( $days, self::HISTORY_CHOICES, true ) ? $days : 30; } /** * Suggested privacy-policy text. It mentions sharing only when the store is connected to the Brickmoat service. * * @return string */ public static function privacy_text() { $text = sprintf( /* translators: %d: number of days the information is kept (7, 30 or 90). */ __( 'To protect our checkout from fraud, we use Brickmoat. When you place an order, Brickmoat checks your internet address, basic details about your browser and your email address to spot automated attacks. This information is kept for up to %d days and then deleted.', 'brickmoat' ), self::history_days() ); if ( Licence::service_on() ) { $text .= ' ' . __( 'If an attempt is identified as fraud, a coded version of these details is shared with Brickmoat to protect other stores.', 'brickmoat' ); } return $text . ' ' . __( 'Brickmoat never sees or stores your card details.', 'brickmoat' ); } /** * Onboarding progress. * * @return array{done:bool,alertEmailSet:bool,levelSet:bool} */ public static function onboarding() { $stored = get_option( self::ONBOARDING_OPTION, array() ); $stored = is_array( $stored ) ? $stored : array(); return array( 'done' => ! empty( $stored['done'] ), 'alertEmailSet' => ! empty( $stored['alertEmailSet'] ), 'levelSet' => ! empty( $stored['levelSet'] ), ); } /** * Saves onboarding progress. * * @param array $changes Flags to set. */ public static function set_onboarding( array $changes ) { update_option( self::ONBOARDING_OPTION, array_merge( self::onboarding(), $changes ), false ); } /** * The settings as the admin screen reads them. * * @return array */ public static function to_rest() { $s = self::get(); return array( 'level' => $s['level'], 'autoLockdown' => (bool) $s['autoLockdown'], 'challengeGuestsInLockdown' => (bool) $s['challengeGuestsInLockdown'], 'blockUnknownInLockdown' => (bool) $s['blockUnknownInLockdown'], 'guestMinimumInLockdown' => null === $s['guestMinimumInLockdown'] ? null : (float) $s['guestMinimumInLockdown'], 'allowReturningCustomers' => (bool) $s['allowReturningCustomers'], 'allowStaff' => (bool) $s['allowStaff'], 'allowEmails' => array_values( (array) $s['allowEmails'] ), 'allowIps' => array_values( (array) $s['allowIps'] ), 'blockedVisitors' => Blocks::manual_list(), 'alertEmails' => self::alert_recipients(), 'alertOnStart' => (bool) $s['alertOnStart'], 'alertOnEnd' => (bool) $s['alertOnEnd'], 'dailyDigest' => (bool) $s['dailyDigest'], // The address itself is never sent back to the browser: it works like a password for the channel. 'slackConnected' => '' !== (string) $s['slackWebhook'], 'turnstileSiteKey' => (string) $s['turnstileSiteKey'], 'turnstileSecretSet' => '' !== (string) $s['turnstileSecret'], 'proxy' => $s['proxy'], 'historyDays' => self::history_days(), 'yourIp' => Client_Ip::get(), 'privacyText' => self::privacy_text(), ); } }