*/ const SOURCES = array( 'remote_addr' => 'REMOTE_ADDR', 'cf' => 'HTTP_CF_CONNECTING_IP', 'xff' => 'HTTP_X_FORWARDED_FOR', 'xri' => 'HTTP_X_REAL_IP', ); /** * Constructor. * * @param Settings $settings Settings. */ public function __construct( Settings $settings ) { $this->settings = $settings; } /** * The visitor's IP address. Proxy headers are only trusted when the site owner * picked that source in the settings; otherwise REMOTE_ADDR is used. * * @return string Empty string when no valid address is available. */ public function client_ip() { if ( null !== $this->cached ) { return $this->cached; } $source = (string) $this->settings->get( 'ip_source' ); $ip = ''; if ( 'remote_addr' !== $source ) { $ip = self::from_server( isset( self::SOURCES[ $source ] ) ? self::SOURCES[ $source ] : '' ); } if ( '' === $ip ) { $ip = self::from_server( 'REMOTE_ADDR' ); } $this->cached = $ip; return $ip; } /** * Reads and validates the first address in a server variable. * * @param string $key $_SERVER key. * @return string */ public static function from_server( $key ) { if ( '' === $key || empty( $_SERVER[ $key ] ) ) { return ''; } $value = sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) ); $first = trim( explode( ',', $value )[0] ); return filter_var( $first, FILTER_VALIDATE_IP ) ? $first : ''; } /** * Which proxy headers are present on this request (helps the owner pick a source). * * @return array source => detected IP. */ public static function detected_sources() { $out = array(); foreach ( self::SOURCES as $source => $key ) { $ip = self::from_server( $key ); if ( '' !== $ip ) { $out[ $source ] = $ip; } } return $out; } /** * Cleans a textarea of IPs/CIDR ranges into a de-duplicated list of valid entries. * * @param string $text One entry per line (commas and spaces also separate). * @return string[] */ public static function parse_list( $text ) { $out = array(); foreach ( preg_split( '/[\s,]+/', (string) $text ) as $entry ) { $entry = trim( $entry ); if ( '' !== $entry && self::valid_entry( $entry ) ) { $out[] = strtolower( $entry ); } } return array_values( array_unique( $out ) ); } /** * Whether a list entry is a valid IP or CIDR range. * * @param string $entry Entry. * @return bool */ public static function valid_entry( $entry ) { if ( filter_var( $entry, FILTER_VALIDATE_IP ) ) { return true; } if ( ! preg_match( '#^([0-9a-fA-F:.]+)/(\d{1,3})$#', $entry, $m ) || ! filter_var( $m[1], FILTER_VALIDATE_IP ) ) { return false; } $max = false !== strpos( $m[1], ':' ) ? 128 : 32; return (int) $m[2] <= $max; } /** * Whether an IP matches any entry of a stored list. * * @param string $ip IP address. * @param string $entries Newline-separated entries. * @return bool */ public static function in_list( $ip, $entries ) { if ( '' === $ip || '' === trim( (string) $entries ) ) { return false; } foreach ( explode( "\n", (string) $entries ) as $entry ) { if ( self::matches( $ip, trim( $entry ) ) ) { return true; } } return false; } /** * Matches one IP against one IP or CIDR entry (IPv4 and IPv6). * * @param string $ip IP address. * @param string $entry IP or CIDR. * @return bool */ public static function matches( $ip, $entry ) { if ( '' === $entry ) { return false; } if ( false === strpos( $entry, '/' ) ) { $a = inet_pton( $ip ); $b = inet_pton( $entry ); return false !== $a && false !== $b && $a === $b; } list( $subnet, $bits ) = explode( '/', $entry, 2 ); $ip_bin = inet_pton( $ip ); $net_bin = inet_pton( $subnet ); if ( false === $ip_bin || false === $net_bin || strlen( $ip_bin ) !== strlen( $net_bin ) ) { return false; } $bits = (int) $bits; $bytes = intdiv( $bits, 8 ); if ( substr( $ip_bin, 0, $bytes ) !== substr( $net_bin, 0, $bytes ) ) { return false; } $rest = $bits % 8; if ( 0 === $rest ) { return true; } $mask = chr( ( 0xff << ( 8 - $rest ) ) & 0xff ); return ( $ip_bin[ $bytes ] & $mask ) === ( $net_bin[ $bytes ] & $mask ); } /** * Short, non-reversible key for an IP, used in transient names. * * @param string $ip IP address. * @return string */ public static function key( $ip ) { return substr( hash_hmac( 'sha256', $ip, wp_salt( 'nonce' ) ), 0, 20 ); } }