Making WordPress.org


Ignore:
Timestamp:
02/12/2024 06:44:57 AM (22 months ago)
Author:
dd32
Message:

Registration: Allow blocking/allowing an entire IP /24 netblock, with custom block lengths.

Allowing/Blocking an IP for 24hrs is not often useful, and instead it needs to be 3-7days.

File:
1 edited

Legend:

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

    r12208 r13201  
    198198            wp_cache_add_global_groups( array( 'registration-limit' ) );
    199199
     200            $expand_to_range = function( $ip ) {
     201                $ip  = trim( $ip );
     202                $ips = [ $ip ];
     203                if ( str_ends_with( $ip, '.*' ) ) {
     204                    $ips = [];
     205                    $ip  = substr( $ip, 0, -2 );
     206                    foreach ( range( 0, 255 ) as $i ) {
     207                        $ips[] = $ip . '.' . $i;
     208                    }
     209                }
     210
     211                return $ips;
     212            };
     213
    200214            if ( $ip_allow ) {
    201                 wp_cache_set( $ip_allow, 'whitelist', 'registration-limit', DAY_IN_SECONDS );
     215                $time_to_allow = wp_unslash( $_POST['ip_allow_time'] ?? DAY_IN_SECONDS );
     216                $ip_allow      = $expand_to_range( $ip_allow );
     217                foreach ( $ip_allow as $ip ) {
     218                    wp_cache_set( $ip, 'whitelist', 'registration-limit', $time_to_allow );
     219                }
     220
     221                printf( '<div class="notice notice-success"><p>%d IPs added to the allow list.</p></div>', count( $ip_allow ) );
    202222            }
    203223            if ( $ip_block ) {
    204                 wp_cache_set( $ip_block, 999, 'registration-limit', DAY_IN_SECONDS );
     224                $time_to_block = wp_unslash( $_POST['ip_block_time'] ?? DAY_IN_SECONDS );
     225                $ip_block      = $expand_to_range( $ip_block );
     226                foreach ( $ip_block as $ip ) {
     227                    wp_cache_set( $ip, 999, 'registration-limit', $time_to_block );
     228                }
     229
     230                printf( '<div class="notice notice-success"><p>%d IPs blocked from registration.</p></div>', count( $ip_block ) );
    205231            }
    206232        }
     
    253279
    254280    echo '<tr>
    255         <th>IP Block for 24hrs</th>
     281        <th>IP Block</th>
    256282        <td>
    257             <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">
    258             <p><em>One IP only. IP will be blocked from registrations for 24hrs. </em></p>
     283            <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">
     284            <select name="ip_block_time">
     285                <option value="86400">24hrs</option>
     286                <option value="604800">7 days</option>
     287                <option value="2592000">30 days</option>
     288            </select>
     289            <p><em>Single IP, or range specified as <code>1.2.3.*</code>. IP will be blocked from registrations for the selected time period. </em></p>
    259290        </td>
    260291    </tr>';
    261292
    262293    echo '<tr>
    263         <th>IP Allow for 24hrs</th>
     294        <th>IP Allow</th>
    264295        <td>
    265             <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">
    266             <p><em>One IP only. IP will bypass per-IP limits on registrations for 24hrs. Will also bypass Jetpack Protect login limiter.</em></p>
     296            <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">
     297            <select name="ip_allow_time">
     298                <option value="86400">24hrs</option>
     299                <option value="259200">3 days</option>
     300                <option value="604800">7 days</option>
     301            </select>
     302            <p><em>Single IP, or range specified as <code>1.2.3.*</code>. IP will bypass per-IP limits on registrations for the selected time period. Will also bypass Jetpack Protect login limiter.</em></p>
    267303        </td>
    268304    </tr>';
Note: See TracChangeset for help on using the changeset viewer.