Making WordPress.org


Ignore:
Timestamp:
03/02/2022 06:07:14 AM (5 years ago)
Author:
dd32
Message:

Login: Add an admin UI to manage blocked words/phrases/email domains for registration spam.

This also allows whitelisting/blocking individual IP addresses.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/ui.php

    r11262 r11620  
    1313                1
    1414        );
     15
     16        add_submenu_page(
     17                'user-registrations',
     18                'Settings',
     19                'Settings',
     20                'promote_users',
     21                'user-registration-settings',
     22                'wporg_login_admin_settings_page',
     23                'dashicons-admin-settings'
     24        );
     25
    1526});
    1627
    … …  
    97108}
    98109
     110function wporg_login_admin_settings_page() {
     111        if ( $_POST && check_admin_referer( 'update_login_settings' ) ) {
     112                $recaptcha_v3_threshold = wp_unslash( $_POST['recaptcha_v3_threshold'] ?? '' );
     113                if ( $recaptcha_v3_threshold ) {
     114                        $recaptcha_v3_threshold = sprintf( "%.1f", $recaptcha_v3_threshold );
     115                        update_option( 'recaptcha_v3_threshold', $recaptcha_v3_threshold );
     116                }
     117
     118                $block_words = wp_unslash( $_POST['registration_block_words'] ?? '' );
     119                if ( $block_words ) {
     120                        $block_words = explode( "\n", $block_words );
     121                        $block_words = array_values( array_unique( array_filter( array_map( 'trim', $block_words ) ) ) );
     122
     123                        // Sanity; Don't let it change more than 20%.
     124                        if ( count( $block_words ) < count( get_option( 'registration_block_words' ) ) * 0.8 ) {
     125                                wp_die( "Are you sure you wanted to do that? You attempted to change registration_block_words to less than 80% of the previous value." );
     126                        }
     127
     128                        update_option( 'registration_block_words', $block_words );
     129                }
     130
     131                $banned_email_domains = wp_unslash( $_POST['banned_email_domains'] ?? '' );
     132                if ( $banned_email_domains ) {
     133                        $banned_email_domains = explode( "\n", $banned_email_domains );
     134                        $banned_email_domains = array_values( array_unique( array_filter( array_map( 'trim', $banned_email_domains ) ) ) );
     135
     136                        // Sanity; Don't let it change more than 20%.
     137                        if ( count( $banned_email_domains ) < count( get_site_option( 'banned_email_domains' ) ) * 0.8 ) {
     138                                wp_die( "Are you sure you wanted to do that? You attempted to change banned_email_domains to less than 80% of the previous value." );
     139                        }
     140
     141                        // Network-wide option.
     142                        update_site_option( 'banned_email_domains', $banned_email_domains );
     143                }
     144
     145                $ip_block = wp_unslash( $_POST['ip_block'] ?? '' );
     146                $ip_allow = wp_unslash( $_POST['ip_allow'] ?? '' );
     147                if ( $ip_block || $ip_allow ) {
     148                        wp_cache_add_global_groups( array( 'registration-limit' ) );
     149
     150                        if ( $ip_allow ) {
     151                                wp_cache_set( $ip_allow, 'whitelist', 'registration-limit', DAY_IN_SECONDS );
     152                        }
     153                        if ( $ip_block ) {
     154                                wp_cache_set( $ip_block, 999, 'registration-limit', DAY_IN_SECONDS );
     155                        }
     156                }
     157
     158                echo '<div class="notice notice-success"><p>Settings saved.</p></div>';
     159        }
     160
     161        echo '<div class="wrap">';
     162        echo '<h1 class="wp-heading-inline">Registration &amp; Login Settings</h1>';
     163        echo '<hr class="wp-header-end">';
     164        echo '<form method="POST">';
     165        wp_nonce_field( 'update_login_settings' );
     166        echo '<table class="form-table">';
     167
     168        printf(
     169                '<tr>
     170                        <th>reCaptcha v3 low-score threshold for Registration</th>
     171                        <td><input name="recaptcha_v3_threshold" type="number" min="0.0" max="1.0" step="0.1" name="" value="%s"></td>
     172                </tr>',
     173                esc_attr( get_option( 'recaptcha_v3_threshold', 0.2 ) )
     174        );
     175
     176        printf(
     177                '<tr>
     178                        <th>Block words for registration</th>
     179                        <td>
     180                                <textarea name="registration_block_words" rows="10" cols="80">%s</textarea>
     181                                <p><em>Any registrations with any of these phrases within their username, email address, or profile fields will be put into manual review. One phrase per line.</em></p>
     182                        </td>
     183                </tr>',
     184                esc_textarea( implode( "\n", get_option( 'registration_block_words', [] ) ) )
     185        );
     186
     187        printf(
     188                '<tr>
     189                        <th>Banned Email Domains</th>
     190                        <td>
     191                                <textarea name="banned_email_domains" rows="10" cols="80">%s</textarea>
     192                                <p id="banned-email-domains-desc"><em>These email domains are WordPress.org-wide. No emails will be sent to them. No users can set their email address to it.<br>One email domain per line. This is the same list as <a href="https://wordpress.org/wp-admin/network/settings.php#banned_email_domains">https://wordpress.org/wp-admin/network/settings.php#banned_email_domains</a>.</em></p>
     193                        </td>
     194                </tr>',
     195                esc_textarea( implode( "\n", get_site_option( 'banned_email_domains', [] ) ) ),
     196        );
     197
     198        echo '<tr>
     199                <th>IP Block for 24hrs</th>
     200                <td>
     201                        <input class="regular-text" type="text" name="ip_block" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" placeholder="xxx.xxx.xxx.xxx">
     202                        <p><em>One IP only. IP will be blocked from registrations for 24hrs. </em></p>
     203                </td>
     204        </tr>';
     205
     206        echo '<tr>
     207                <th>IP Allow for 24hrs</th>
     208                <td>
     209                        <input class="regular-text" type="text" name="ip_allow" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" placeholder="xxx.xxx.xxx.xxx">
     210                        <p><em>One IP only. IP will bypass per-IP limits on registrations for 24hrs. Will also bypass Jetpack Protect login limiter.</em></p>
     211                </td>
     212        </tr>';
     213
     214        echo '</table>';
     215        echo '<p class="submit">
     216                <input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
     217        </p>';
     218        echo '</form>';
     219        echo '</div>';
     220}
     221
    99222add_action( 'admin_post_login_resend_email', function() {
    100223        if ( ! current_user_can( 'promote_users' ) ) {
    … …  
    231354
    232355                // bbPress roles still aren't quite right, need to switch away and back..
    233                 // This is hacky, but otherwise the bbp_set_user_role() call below will appear to succeed, but no role aleration will actually happen.
     356                // This is hacky, but otherwise the bbp_set_user_role() call below will appear to succeed, but no role alteration will actually happen.
    234357                restore_current_blog();
    235358                switch_to_blog( WPORG_SUPPORT_FORUMS_BLOGID );
Note: See TracChangeset for help on using the changeset viewer.