$max ) { return false; } $bytes = intdiv( $bits, 8 ); if ( $bytes > 0 && 0 !== strncmp( $a, $b, $bytes ) ) { return false; } $rest = $bits % 8; if ( 0 === $rest ) { return true; } $mask = 0xFF << ( 8 - $rest ) & 0xFF; return ( ord( $a[ $bytes ] ) & $mask ) === ( ord( $b[ $bytes ] ) & $mask ); } /** * Whether an address matches any entry of a short list of addresses and ranges. * * @param string $ip Address. * @param string[] $entries Addresses or CIDR ranges. * @return bool */ public static function in_list( $ip, array $entries ) { foreach ( $entries as $entry ) { if ( self::in_cidr( $ip, $entry ) ) { return true; } } return false; } /** * Whether a value is an address or a CIDR range a merchant may put on a list. * * @param string $entry Value. * @return bool */ public static function is_valid_entry( $entry ) { $entry = trim( (string) $entry ); if ( false === strpos( $entry, '/' ) ) { return self::is_valid( $entry ); } list( $ip, $bits ) = explode( '/', $entry, 2 ); if ( ! self::is_valid( $ip ) || ! ctype_digit( $bits ) ) { return false; } return (int) $bits <= ( self::is_v6( $ip ) ? 128 : 32 ); } /** * An address safe to show on screen, with the last part hidden: "203.0.113.·". * * @param string $ip Address. * @return string */ public static function mask( $ip ) { $ip = (string) $ip; if ( ! self::is_valid( $ip ) ) { return ''; } if ( self::is_v6( $ip ) ) { $prefix = self::prefix( $ip ); return rtrim( substr( $prefix, 0, (int) strpos( $prefix, '/' ) ), ':' ) . ':·'; } return substr( $ip, 0, (int) strrpos( $ip, '.' ) ) . '.·'; } /** * An IPv4 address as a whole number. * * @param string $ip IPv4 address. * @return int|null */ public static function v4_to_int( $ip ) { $long = ip2long( (string) $ip ); if ( false === $long ) { return null; } // 32-bit PHP gives negative numbers for the upper half. return $long < 0 ? $long + 4294967296 : $long; } /** * The first 64 bits of an IPv6 address as 16 hex digits. * * @param string $ip IPv6 address. * @return string|null */ public static function v6_top64( $ip ) { $bin = @inet_pton( (string) $ip ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Invalid input is expected and handled. if ( false === $bin || 16 !== strlen( $bin ) ) { return null; } return bin2hex( substr( $bin, 0, 8 ) ); } /** * Finds which range of a sorted list holds an address, by binary search. * * The list is flat, three values per range: first, last, owner. For IPv4 first and last * are whole numbers; for IPv6 they are the first 64 bits as 16 hex digits. * * @param string $ip Address. * @param array $data Array with the keys "v4" and "v6". * @return int|null The owner value of the matching range, or null. */ public static function find_range( $ip, array $data ) { if ( self::is_v6( $ip ) ) { $needle = self::v6_top64( $ip ); $list = isset( $data['v6'] ) ? $data['v6'] : array(); } else { $needle = self::v4_to_int( $ip ); $list = isset( $data['v4'] ) ? $data['v4'] : array(); } if ( null === $needle ) { return null; } $low = 0; $high = intdiv( count( $list ), 3 ) - 1; while ( $low <= $high ) { $mid = ( $low + $high ) >> 1; $at = $mid * 3; if ( $needle < $list[ $at ] ) { $high = $mid - 1; } elseif ( $needle > $list[ $at + 1 ] ) { $low = $mid + 1; } else { return (int) $list[ $at + 2 ]; } } return null; } }