MDL-48766 moodlelib: introduce ip_is_public()

For determining if an IP is publicly addressable
This commit is contained in:
Dan Poltawski 2016-02-05 14:56:01 +00:00
parent 1f2744851f
commit 054da30ba9
2 changed files with 59 additions and 2 deletions

View file

@ -8641,8 +8641,6 @@ function getremoteaddr($default='0.0.0.0') {
function cleanremoteaddr($addr, $compress=false) {
$addr = trim($addr);
// TODO: maybe add a separate function is_addr_public() or something like this.
if (strpos($addr, ':') !== false) {
// Can be only IPv6.
$parts = explode(':', $addr);
@ -8735,6 +8733,17 @@ function cleanremoteaddr($addr, $compress=false) {
return implode('.', $parts);
}
/**
* Is IP address a public address?
*
* @param string $ip The ip to check
* @return bool true if the ip is public
*/
function ip_is_public($ip) {
return (bool) filter_var($ip, FILTER_VALIDATE_IP, (FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE));
}
/**
* This function will make a complete copy of anything it's given,
* regardless of whether it's an object or not.