diff options
| author | 2026-08-01 00:50:51 -0600 | |
|---|---|---|
| committer | 2026-08-01 00:50:51 -0600 | |
| commit | e8b2e67e8588bc5ae7db5113086179ec40555b33 (patch) | |
| tree | 0f4ce95f2eb72fb40c48cd2ea1a84e591efa212e | |
| parent | add 418 explainer (diff) | |
| download | hostfiles-e8b2e67e8588bc5ae7db5113086179ec40555b33.tar.gz hostfiles-e8b2e67e8588bc5ae7db5113086179ec40555b33.tar.bz2 hostfiles-e8b2e67e8588bc5ae7db5113086179ec40555b33.zip | |
add cidr matching
| -rw-r--r-- | upload.php | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -155,5 +155,35 @@ function remap_files() { } function get_addr() { + foreach (KNOWN_CIDRS as $cidr => $name) { + if (cidr_matches($cidr, $_SERVER['REMOTE_ADDR'])) { + return sprintf('%s (%s)', $_SERVER['REMOTE_ADDR'], $name); + } + } return $_SERVER['REMOTE_ADDR']; } + +function ip_to_bits($ip) { + $splitted = str_split(inet_pton($ip)); + $binaryip = ''; + foreach ($splitted as $char) { + $binaryip .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT); + } + return $binaryip; +} +function cidr_matches($cidr, $ip) { + $binaryip = ip_to_bits($ip); + + $pieces = explode('/', $cidr); + $binarynet = ip_to_bits($pieces[0]); + if (count($pieces) > 1) { + $mask = $pieces[1]; + } else { + $mask = strlen($binarynet); + } + + $ip_net_bits = substr($binaryip, 0, $mask); + $net_bits = substr($binarynet, 0, $mask); + + return ($ip_net_bits === $net_bits); +} |
