*/ public static function entries(): array { $stored = get_option( self::OPTION, array() ); $now = time(); $clean = array(); foreach ( is_array( $stored ) ? $stored : array() as $ip => $entry ) { if ( ! is_array( $entry ) ) { continue; } $entry = array( 'f' => array_values( array_filter( array_map( 'intval', (array) ( $entry['f'] ?? array() ) ), static fn( int $t ): bool => $t > $now - self::FAILURE_WINDOW ) ), 'l' => array_values( array_filter( array_map( 'intval', (array) ( $entry['l'] ?? array() ) ), static fn( int $t ): bool => $t > $now - self::LOCKOUT_WINDOW ) ), 'until' => (int) ( $entry['until'] ?? 0 ), 'user' => (string) ( $entry['user'] ?? '' ), ); if ( $entry['f'] || $entry['l'] || $entry['until'] > $now ) { $clean[ (string) $ip ] = $entry; } } return $clean; } /** * Save state, keeping the most recently active addresses. * * @param array $entries State. * @return void */ private static function save( array $entries ): void { if ( count( $entries ) > self::MAX_ENTRIES ) { uasort( $entries, static fn( array $a, array $b ): int => max( array_merge( $b['f'], $b['l'], array( $b['until'] ) ) ) <=> max( array_merge( $a['f'], $a['l'], array( $a['until'] ) ) ) ); $entries = array_slice( $entries, 0, self::MAX_ENTRIES, true ); } update_option( self::OPTION, $entries, false ); } /** * When an address's lockout ends (0 when it isn't locked out). * * @param string $ip Address. * @return int */ public static function locked_until( string $ip ): int { $until = (int) ( self::entries()[ $ip ]['until'] ?? 0 ); return $until > time() ? $until : 0; } /** * Refuse a locked-out address, and tell everyone else how many tries * are left once they start running low. * * @param WP_User|WP_Error|null $user Result so far. * @param string $username Submitted username. * @return WP_User|WP_Error|null */ public function check( $user, $username ) { $ip = Ip::current(); if ( '' === (string) $username || self::is_allowlisted( $ip ) ) { return $user; } $until = self::locked_until( $ip ); if ( $until ) { return new WP_Error( 'aiou_locked_out', self::lockout_message( $until ) ); } if ( $user instanceof WP_Error && array_intersect( $user->get_error_codes(), self::COUNTED_CODES ) ) { $used = count( self::entries()[ $ip ]['f'] ?? array() ) + 1; $left = self::max_attempts() - $used; if ( $left <= 0 ) { // This failure is the one that locks the address out. return $user; } if ( $left <= 2 ) { $user->add( 'aiou_attempts_left', sprintf( /* translators: %d: number of attempts */ _n( '%d attempt remaining before this device is locked out for a while.', '%d attempts remaining before this device is locked out for a while.', $left, 'all-in-one-utilities' ), $left ) ); } } return $user; } /** * The message a locked-out visitor sees. * * @param int $until Lockout end. * @return string */ private static function lockout_message( int $until ): string { return sprintf( /* translators: %s: time, e.g. "20 mins" */ __( 'Error: Too many failed login attempts. Please try again in %s.', 'all-in-one-utilities' ), human_time_diff( time(), $until ) ); } /** * Count a failed attempt, and lock the address out when it's one too * many. * * @param string $username Submitted username. * @param WP_Error $error Why it failed. * @return void */ public function record_failure( $username, $error = null ): void { if ( $error instanceof WP_Error && ! array_intersect( $error->get_error_codes(), self::COUNTED_CODES ) ) { return; } $ip = Ip::current(); if ( '' === $ip || self::is_allowlisted( $ip ) || self::locked_until( $ip ) ) { return; } $entries = self::entries(); $entry = $entries[ $ip ] ?? array( 'f' => array(), 'l' => array(), 'until' => 0, 'user' => '', ); $now = time(); $entry['f'][] = $now; $entry['user'] = substr( sanitize_user( (string) $username ), 0, 60 ); if ( count( $entry['f'] ) >= self::max_attempts() ) { $entry['l'][] = $now; $entry['f'] = array(); $long = count( $entry['l'] ) >= max( 1, (int) self::setting( 'max_lockouts', 4 ) ); $length = $long ? max( 1, (int) self::setting( 'long_lockout_hours', 24 ) ) * HOUR_IN_SECONDS : max( 1, (int) self::setting( 'lockout_minutes', 20 ) ) * MINUTE_IN_SECONDS; $entry['until'] = $now + $length; /** * Fires when an address is locked out of logging in. * * @param string $ip The address. * @param string $username The last username it tried. * @param int $until When the lockout ends. * @param bool $long Whether this is the longer lockout. */ do_action( 'aiou_login_lockout', $ip, $entry['user'], $entry['until'], $long ); } $entries[ $ip ] = $entry; self::save( $entries ); } /** * A successful login clears that address's failed attempts (its * lockout history stays, so repeated lockouts still add up). * * @return void */ public function clear_failures(): void { $ip = Ip::current(); $entries = self::entries(); if ( empty( $entries[ $ip ]['f'] ) ) { return; } $entries[ $ip ]['f'] = array(); self::save( $entries ); } /** * The current lockouts, newest first. * * @return array */ public static function lockouts(): array { $now = time(); $list = array(); foreach ( self::entries() as $ip => $entry ) { if ( $entry['until'] > $now ) { $list[ $ip ] = array( 'until' => $entry['until'], 'user' => $entry['user'], 'lockouts' => count( $entry['l'] ), ); } } uasort( $list, static fn( array $a, array $b ): int => $b['until'] <=> $a['until'] ); return $list; } /** * Unlock one address, or all of them (admin-post). * * @return void */ public function handle_unlock(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'all-in-one-utilities' ), 403 ); } check_admin_referer( self::UNLOCK_ACTION ); $ip = isset( $_GET['ip'] ) ? sanitize_text_field( wp_unslash( $_GET['ip'] ) ) : ''; $entries = self::entries(); if ( '' === $ip ) { $entries = array(); } else { unset( $entries[ $ip ] ); } self::save( $entries ); set_transient( 'aiou_settings_notice_' . get_current_user_id(), array( 'type' => 'success', 'message' => '' === $ip ? __( 'All lockouts were cleared.', 'all-in-one-utilities' ) /* translators: %s: IP address */ : sprintf( __( '%s was unlocked.', 'all-in-one-utilities' ), $ip ), ), MINUTE_IN_SECONDS ); wp_safe_redirect( admin_url( 'admin.php?page=aiou-settings&tab=security' ) ); exit; } /** * The lockouts table on the settings section. * * @return void */ public static function render_lockouts(): void { $lockouts = self::lockouts(); $unlock = static function ( string $ip ): string { $args = array( 'action' => self::UNLOCK_ACTION, 'ip' => $ip, ); return wp_nonce_url( add_query_arg( array_filter( $args ), admin_url( 'admin-post.php' ) ), self::UNLOCK_ACTION ); }; if ( defined( 'AIOU_DISABLE_LOGIN_LIMITS' ) && AIOU_DISABLE_LOGIN_LIMITS ) { echo '

' . esc_html__( 'Switched off by AIOU_DISABLE_LOGIN_LIMITS in wp-config.php.', 'all-in-one-utilities' ) . '

'; } if ( ! $lockouts ) { echo '

' . esc_html__( 'No addresses are locked out right now.', 'all-in-one-utilities' ) . '

'; return; } $long_after = max( 1, (int) self::setting( 'max_lockouts', 4 ) ); ?>
1 ) : ?>
$lockout ) : ?>
— = $long_after ) : ?>