| 1 |
<?php |
| 2 |
if (! (defined('ABSPATH') || defined('MCDATAPATH')) ) exit; |
| 3 |
if (!class_exists('BVProtectBase')) : |
| 4 |
|
| 5 |
class BVProtectBase { |
| 6 |
public static function getIP($ipHeader) { |
| 7 |
$ip = '127.0.0.1'; |
| 8 |
if ($ipHeader && is_array($ipHeader)) { |
| 9 |
if (array_key_exists($ipHeader['hdr'], $_SERVER)) { |
| 10 |
$_ips = preg_split("/(,| |\t)/", $_SERVER[$ipHeader['hdr']]); |
| 11 |
if (array_key_exists(intval($ipHeader['pos']), $_ips)) { |
| 12 |
$ip = $_ips[intval($ipHeader['pos'])]; |
| 13 |
} |
| 14 |
} |
| 15 |
} else if (array_key_exists('REMOTE_ADDR', $_SERVER)) { |
| 16 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 17 |
} |
| 18 |
|
| 19 |
$ip = trim($ip); |
| 20 |
if (preg_match('/^\[([0-9a-fA-F:]+)\](:[0-9]+)$/', $ip, $matches)) { |
| 21 |
$ip = $matches[1]; |
| 22 |
} elseif (preg_match('/^([0-9.]+)(:[0-9]+)$/', $ip, $matches)) { |
| 23 |
$ip = $matches[1]; |
| 24 |
} |
| 25 |
|
| 26 |
return $ip; |
| 27 |
} |
| 28 |
|
| 29 |
public static function hasIPv6Support() { |
| 30 |
return defined('AF_INET6'); |
| 31 |
} |
| 32 |
|
| 33 |
public static function isValidIP($ip) { |
| 34 |
return filter_var($ip, FILTER_VALIDATE_IP) !== false; |
| 35 |
} |
| 36 |
|
| 37 |
public static function bvInetPton($ip) { |
| 38 |
$pton = self::isValidIP($ip) ? (self::hasIPv6Support() ? inet_pton($ip) : self::_bvInetPton($ip)) : false; |
| 39 |
return $pton; |
| 40 |
} |
| 41 |
|
| 42 |
public static function _bvInetPton($ip) { |
| 43 |
if (preg_match('/^(?:\d{1,3}(?:\.|$)){4}/', $ip)) { |
| 44 |
$octets = explode('.', $ip); |
| 45 |
$bin = chr($octets[0]) . chr($octets[1]) . chr($octets[2]) . chr($octets[3]); |
| 46 |
return $bin; |
| 47 |
} |
| 48 |
|
| 49 |
if (preg_match('/^((?:[\da-f]{1,4}(?::|)){0,8})(::)?((?:[\da-f]{1,4}(?::|)){0,8})$/i', $ip)) { |
| 50 |
if ($ip === '::') { |
| 51 |
return "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
| 52 |
} |
| 53 |
$colon_count = substr_count($ip, ':'); |
| 54 |
$dbl_colon_pos = strpos($ip, '::'); |
| 55 |
if ($dbl_colon_pos !== false) { |
| 56 |
$ip = str_replace('::', str_repeat(':0000', |
| 57 |
(($dbl_colon_pos === 0 || $dbl_colon_pos === strlen($ip) - 2) ? 9 : 8) - $colon_count) . ':', $ip); |
| 58 |
$ip = trim($ip, ':'); |
| 59 |
} |
| 60 |
|
| 61 |
$ip_groups = explode(':', $ip); |
| 62 |
$ipv6_bin = ''; |
| 63 |
foreach ($ip_groups as $ip_group) { |
| 64 |
$ipv6_bin .= pack('H*', str_pad($ip_group, 4, '0', STR_PAD_LEFT)); |
| 65 |
} |
| 66 |
|
| 67 |
return strlen($ipv6_bin) === 16 ? $ipv6_bin : false; |
| 68 |
} |
| 69 |
|
| 70 |
if (preg_match('/^(?:\:(?:\:0{1,4}){0,4}\:|(?:0{1,4}\:){5})ffff\:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/i', $ip, $matches)) { |
| 71 |
$octets = explode('.', $matches[1]); |
| 72 |
return chr($octets[0]) . chr($octets[1]) . chr($octets[2]) . chr($octets[3]); |
| 73 |
} |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
public static function isIPInRange($start_ip_range, $end_ip_range, $ip) { |
| 79 |
$bin_ip = null; |
| 80 |
if ($ip) { |
| 81 |
$bin_ip = self::bvInetPton($ip); |
| 82 |
} |
| 83 |
if ($bin_ip && $bin_ip >= self::bvInetPton($start_ip_range) |
| 84 |
&& $bin_ip <= self::bvInetPton($end_ip_range)) { |
| 85 |
return true; |
| 86 |
} |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
public static function isPrivateIP($ip) { |
| 91 |
$private_ip_ranges = array( |
| 92 |
array("10.0.0.0", "10.255.255.255"), |
| 93 |
array("172.16.0.0", "172.31.255.255"), |
| 94 |
array("192.168.0.0", "192.168.255.255"), |
| 95 |
array("127.0.0.1", "127.255.255.255"), |
| 96 |
array("::1","::1"), |
| 97 |
array("fc00::","fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") |
| 98 |
); |
| 99 |
|
| 100 |
$result = false; |
| 101 |
foreach ($private_ip_ranges as $ip_range) { |
| 102 |
$result = self::isIPInRange($ip_range[0], $ip_range[1], $ip); |
| 103 |
if($result) { |
| 104 |
return $result; |
| 105 |
} |
| 106 |
} |
| 107 |
return $result; |
| 108 |
} |
| 109 |
} |
| 110 |
endif; |