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