$errors ), 60 ); $args = array( 'tab' => 'security' ); if ( ! $errors ) { $args['updated'] = 1; } if ( '' !== $success ) { set_transient( 'bapi_forms_security_ok_' . get_current_user_id(), $success, 60 ); } wp_safe_redirect( SettingsPage::url( $args ) ); exit; } /** Verify a new key with Business API, swap it in and forget the old one. */ public function rotate(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'businessapi-forms' ), 403 ); } check_admin_referer( self::ROTATE_ACTION ); $options = $this->security->options(); $env = isset( $_POST['environment'] ) && Options::ENV_LIVE === $_POST['environment'] ? Options::ENV_LIVE : Options::ENV_TEST; $new = isset( $_POST['new_key'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['new_key'] ) ) ) : ''; if ( $options->key_is_constant( $env ) ) { self::done( array( __( 'This key is set in wp-config.php. Change it there.', 'businessapi-forms' ) ) ); } if ( ! Options::key_matches_environment( $new, $env ) ) { /* translators: %s: expected key prefix. */ self::done( array( sprintf( __( 'The new key was not saved. It must be a secret key starting with %s.', 'businessapi-forms' ), 'bapi_sk_' . $env . '_' ) ) ); } if ( $new === $options->key( $env ) ) { self::done( array( __( 'That is the key already in use.', 'businessapi-forms' ) ) ); } $check = $this->account_with( $new, $env ); if ( null === $check['account'] ) { /* translators: %s: error. */ self::done( array( sprintf( __( 'The new key was not saved: Business API did not accept it (%s).', 'businessapi-forms' ), $check['error'] ) ) ); } // Must be the same account as the current key, when that one still works. $old = '' !== $options->key( $env ) ? $this->account_with( $options->key( $env ), $env )['account'] : null; if ( null !== $old && $old !== $check['account'] ) { self::done( array( __( 'The new key belongs to a different Business API account than the current one. It was not saved. To switch accounts, remove the key on the Connection tab first.', 'businessapi-forms' ) ) ); } $plugin = Plugin::instance(); $this->security->rotating = true; $options->update( array( $env . '_key' => $new ) ); $this->security->rotating = false; $plugin->capabilities->flush(); $plugin->prices->flush(); $plugin->sites->register_if_missing( $env ); $this->security->audit->log( 'key_rotated', array( 'environment' => $env ) ); self::done( array(), __( 'Key rotated. The old key is no longer stored here: revoke it in your Business API dashboard (API keys) now.', 'businessapi-forms' ) ); } /** * Account name for a key that is not stored yet. * * @return array{account: ?string, error: string} account is null when refused. */ private function account_with( string $key, string $env ): array { $response = $this->security->client()->request( 'GET', 'account-status', array(), null, $env, array(), 0, $key ); if ( ! $response->ok() ) { return array( 'account' => null, /* translators: %d: HTTP status. */ 'error' => 0 === $response->status ? (string) $response->error : sprintf( __( 'HTTP %d', 'businessapi-forms' ), $response->status ), ); } $data = $response->data; return array( 'account' => strtolower( trim( (string) ( $data['accountName'] ?? '' ) ) ), 'error' => '', ); } public function check_now(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'businessapi-forms' ), 403 ); } check_admin_referer( self::CHECK_ACTION ); $this->security->checklist->debug_log_public( true ); ( new \BapiForms\Admin\Compatibility( \BapiForms\Plugin::instance()->options ) )->rest_check( true ); $result = $this->security->integrity->run(); $errors = array(); if ( Integrity::UNCHECKED === $result['status'] ) { /* translators: %s: error. */ $errors[] = sprintf( __( 'The integrity check could not run: %s', 'businessapi-forms' ), (string) ( $result['error'] ?? '' ) ); } self::done( $errors, Integrity::FAILED === $result['status'] ? '' : __( 'Checks ran again.', 'businessapi-forms' ) ); } public function render(): void { $ok = get_transient( 'bapi_forms_security_ok_' . get_current_user_id() ); delete_transient( 'bapi_forms_security_ok_' . get_current_user_id() ); if ( is_string( $ok ) && '' !== $ok ) { printf( '

%s

', esc_html( $ok ) ); } $this->render_twofa(); $this->render_checklist(); $this->render_integrity(); $this->render_keys(); $this->render_events(); } private static function pill( bool $ok, string $yes, string $no, bool $warn = false ): string { return sprintf( '%s', $ok ? 'bapi-forms-ok' : ( $warn ? 'bapi-forms-warn' : 'bapi-forms-error' ), esc_html( $ok ? $yes : $no ) ); } private function render_twofa(): void { $users = get_users( array( 'capability__in' => TwoFactor::CAPABILITIES, 'number' => 200, ) ); $active = array_keys( array_filter( TwoFactor::providers() ) ); ?>

user_login ); ?>

Site Health. BusinessAPI Forms is not a security plugin: use a dedicated one for firewall and malware scanning.', 'businessapi-forms' ); ?>

security->checklist->results() as $key => $check ) : ?>
()
security->integrity->last(); $status = (string) ( $last['status'] ?? '' ); $labels = array( Integrity::CLEAN => array( true, __( 'Clean', 'businessapi-forms' ), false ), Integrity::FAILED => array( false, __( 'Failed: forms paused', 'businessapi-forms' ), false ), Integrity::UNKNOWN => array( false, __( 'Version not published (live forms paused)', 'businessapi-forms' ), true ), Integrity::BLOCKED => array( false, __( 'This version is blocked: update the plugin', 'businessapi-forms' ), false ), Integrity::UNCHECKED => array( false, __( 'Could not check', 'businessapi-forms' ), true ), ); ?>

security->options(); ?>

__( 'Sandbox key', 'businessapi-forms' ), Options::ENV_LIVE => __( 'Live key', 'businessapi-forms' ), ) as $env => $label ) : ?>
key_is_constant( $env ) ) : ?> has_key( $env ) ? Options::mask_key( $options->key( $env ) ) : ( $options->key_unreadable( $env ) ? __( 'unreadable, enter it again', 'businessapi-forms' ) : __( 'none', 'businessapi-forms' ) ) ); ?>
security->options(); if ( ! $options->has_key() ) { return; } $cache = self::EVENTS_CACHE . $options->environment(); $events = get_transient( $cache ); if ( ! is_array( $events ) ) { $response = $this->security->client()->get( 'wordpress/audit-events', array( 'limit' => '15' ) ); $events = $response->ok() && is_array( $response->data['events'] ?? null ) ? $response->data['events'] : array(); set_transient( $cache, $events, 2 * MINUTE_IN_SECONDS ); } ?>