summaryrefslogtreecommitdiff
path: root/it.class
diff options
context:
space:
mode:
authorDavid Flatz2022-06-20 15:34:58 +0200
committerDavid Flatz2022-06-20 15:34:58 +0200
commitfff47a5651778018c6b437519b9ab1a535aa3e1d (patch)
treedfc6a6e8b71d329f0de4366bf7836c67ec1609dc /it.class
parent5e520bc918cc559cf2497c4c97199ecd33331b86 (diff)
downloaditools-fff47a5651778018c6b437519b9ab1a535aa3e1d.tar.gz
itools-fff47a5651778018c6b437519b9ab1a535aa3e1d.tar.bz2
itools-fff47a5651778018c6b437519b9ab1a535aa3e1d.zip
add function to check whether an ip is from a private range which should be useful for security checks of user provided ip-addresses
Diffstat (limited to 'it.class')
-rw-r--r--it.class26
1 files changed, 26 insertions, 0 deletions
diff --git a/it.class b/it.class
index 886ae06..87ceb9b 100644
--- a/it.class
+++ b/it.class
@@ -430,6 +430,32 @@ static function cidr_match($ip, $cidrs)
/**
+ * check whether an IP address is a private, loopback or link-local address.
+ * Supports IPv6 and IPv6
+ * @param $ip IP address as string (192.168.42.123,
+ * 2a02:169:200:d:0:1337:babe:d00d)
+ * @return true if $ip is in a private, loopback or link-local network block
+ */
+static function is_private_ip($ip)
+{
+ $private_cidrs = [
+ '10.0.0.0/8',
+ '127.0.0.0/8',
+ '169.254.0.0/16',
+ '172.16.0.0/12',
+ '192.0.0.0/24',
+ '192.168.0.0/16',
+ '198.18.0.0/15',
+ '::1/128',
+ 'fc00::/7',
+ 'fe80::/64',
+ ];
+
+ return it::cidr_match($ip, $private_cidrs);
+}
+
+
+/**
* Convert regex for preg (adds and escapes delimiter, adds modifiers)
* @param $pattern Regex to convert
* @param $p['casesensitive'] Regex is case sensitive (omit modifier i)