`$_SERVER` key => label. */ public static function sources(): array { return array( 'REMOTE_ADDR' => __( 'Direct connection (REMOTE_ADDR) - recommended', 'all-in-one-utilities' ), 'HTTP_CF_CONNECTING_IP' => __( 'Cloudflare (CF-Connecting-IP header)', 'all-in-one-utilities' ), 'HTTP_X_FORWARDED_FOR' => __( 'Proxy or load balancer (X-Forwarded-For header)', 'all-in-one-utilities' ), 'HTTP_X_REAL_IP' => __( 'Proxy (X-Real-IP header)', 'all-in-one-utilities' ), ); } /** * The chosen source. * * @return string */ public static function source(): string { $source = (string) aiou_get_setting( 'security', self::SECTION, 'source', 'REMOTE_ADDR' ); return isset( self::sources()[ $source ] ) ? $source : 'REMOTE_ADDR'; } /** * The address a source currently reports, or '' when it has none. * * For X-Forwarded-For the last address is used: that's the one the * nearest proxy added, while earlier ones can be made up by the * visitor. * * @param string $source `$_SERVER` key. * @return string */ public static function from( string $source ): string { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- validated with FILTER_VALIDATE_IP below. $raw = isset( $_SERVER[ $source ] ) ? (string) $_SERVER[ $source ] : ''; if ( 'HTTP_X_FORWARDED_FOR' === $source ) { $parts = array_map( 'trim', explode( ',', $raw ) ); $raw = (string) end( $parts ); } $ip = filter_var( trim( $raw ), FILTER_VALIDATE_IP ); return false === $ip ? '' : $ip; } /** * The visitor's address. * * @return string */ public static function current(): string { $ip = self::from( self::source() ); if ( '' === $ip ) { $ip = self::from( 'REMOTE_ADDR' ); } /** * Filters the visitor IP address used by security features. * * @param string $ip The address ('' when unknown). */ return (string) apply_filters( 'aiou_client_ip', $ip ); } /** * Split a textarea of addresses into valid and invalid entries. * * @param string $text One entry per line (commas also separate). * @return array{valid:string[],invalid:string[]} */ public static function parse_list( string $text ): array { $valid = array(); $invalid = array(); $lines = preg_split( '/[\r\n,]+/', $text ); foreach ( false === $lines ? array() : $lines as $line ) { $entry = trim( (string) preg_replace( '/#.*$/', '', $line ) ); if ( '' === $entry ) { continue; } if ( self::is_valid_rule( $entry ) ) { $valid[] = strtolower( $entry ); } else { $invalid[] = $entry; } } return array( 'valid' => array_values( array_unique( $valid ) ), 'invalid' => $invalid, ); } /** * Clean a textarea value: keeps valid entries and comment lines, drops * the rest. * * @param mixed $value Submitted value. * @return string */ public static function sanitize_list( mixed $value ): string { $lines = array(); $raw = preg_split( '/[\r\n]+/', (string) $value ); foreach ( false === $raw ? array() : $raw as $line ) { $line = trim( sanitize_text_field( $line ) ); $entry = trim( (string) preg_replace( '/#.*$/', '', $line ) ); if ( '' === $line ) { continue; } if ( '' === $entry || self::is_valid_rule( $entry ) ) { $lines[] = $line; } } return implode( "\n", $lines ); } /** * Whether a list entry is understood. * * @param string $rule Entry. * @return bool */ public static function is_valid_rule( string $rule ): bool { if ( false !== filter_var( $rule, FILTER_VALIDATE_IP ) ) { return true; } // A bare "*" would match everyone, so at least one number is required. if ( preg_match( '/^(\d{1,3}\.){1,3}\*$/', $rule ) ) { return true; } if ( preg_match( '#^([^/]+)/(\d{1,3})$#', $rule, $m ) && false !== filter_var( $m[1], FILTER_VALIDATE_IP ) ) { $max = str_contains( $m[1], ':' ) ? 128 : 32; return (int) $m[2] <= $max; } return false; } /** * Whether an address matches any entry. * * @param string $ip Address. * @param string[] $rules Valid entries (see `parse_list()`). * @return bool */ public static function matches( string $ip, array $rules ): bool { if ( '' === $ip ) { return false; } foreach ( $rules as $rule ) { if ( self::matches_rule( $ip, (string) $rule ) ) { return true; } } return false; } /** * Whether an address matches one entry. * * @param string $ip Address. * @param string $rule Entry. * @return bool */ public static function matches_rule( string $ip, string $rule ): bool { $ip = strtolower( $ip ); $rule = strtolower( trim( $rule ) ); if ( str_ends_with( $rule, '*' ) ) { return str_starts_with( $ip, rtrim( $rule, '*' ) ) && ! str_contains( $ip, ':' ); } if ( ! str_contains( $rule, '/' ) ) { $a = @inet_pton( $ip ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- returns false on bad input, which is handled. $b = @inet_pton( $rule ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- as above. return false !== $a && $a === $b; } list( $subnet, $bits ) = explode( '/', $rule, 2 ); $ip_bin = @inet_pton( $ip ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- as above. $subnet_bin = @inet_pton( $subnet ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- as above. if ( false === $ip_bin || false === $subnet_bin || strlen( $ip_bin ) !== strlen( $subnet_bin ) ) { return false; } $bits = (int) $bits; $bytes = intdiv( $bits, 8 ); if ( substr( $ip_bin, 0, $bytes ) !== substr( $subnet_bin, 0, $bytes ) ) { return false; } $rest = $bits % 8; if ( 0 === $rest ) { return true; } $mask = ( 0xff << ( 8 - $rest ) ) & 0xff; return ( ord( $ip_bin[ $bytes ] ) & $mask ) === ( ord( $subnet_bin[ $bytes ] ) & $mask ); } }