diff options
author | David Flatz | 2022-06-20 15:34:58 +0200 |
---|---|---|
committer | David Flatz | 2022-06-20 15:34:58 +0200 |
commit | fff47a5651778018c6b437519b9ab1a535aa3e1d (patch) | |
tree | dfc6a6e8b71d329f0de4366bf7836c67ec1609dc /it.class | |
parent | 5e520bc918cc559cf2497c4c97199ecd33331b86 (diff) | |
download | itools-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.class | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -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) |