summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar steering72532026-08-01 00:50:51 -0600
committerGravatar steering72532026-08-01 00:50:51 -0600
commite8b2e67e8588bc5ae7db5113086179ec40555b33 (patch)
tree0f4ce95f2eb72fb40c48cd2ea1a84e591efa212e
parentadd 418 explainer (diff)
downloadhostfiles-e8b2e67e8588bc5ae7db5113086179ec40555b33.tar.gz
hostfiles-e8b2e67e8588bc5ae7db5113086179ec40555b33.tar.bz2
hostfiles-e8b2e67e8588bc5ae7db5113086179ec40555b33.zip
add cidr matching
-rw-r--r--upload.php30
1 files changed, 30 insertions, 0 deletions
diff --git a/upload.php b/upload.php
index 180f508..dcdccb1 100644
--- a/upload.php
+++ b/upload.php
@@ -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);
+}