| 1 |
<?php |
| 2 |
if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('WPRProtectUtils_V541')) : |
| 5 |
class WPRProtectUtils_V541 { |
| 6 |
public static function getIP($ip_header) { |
| 7 |
$ip = null; |
| 8 |
|
| 9 |
if (is_array($ip_header)) { |
| 10 |
if ((array_key_exists('hdr', $ip_header) && is_string($ip_header['hdr'])) && |
| 11 |
(array_key_exists('pos', $ip_header) && is_int($ip_header['pos']))) { |
| 12 |
|
| 13 |
if (array_key_exists($ip_header['hdr'], $_SERVER) && is_string($_SERVER[$ip_header['hdr']])) { |
| 14 |
$_ips = preg_split("/(,| |\t)/", $_SERVER[$ip_header['hdr']]); |
| 15 |
|
| 16 |
if (array_key_exists($ip_header['pos'], $_ips)) { |
| 17 |
$ip = $_ips[$ip_header['pos']]; |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
} elseif (array_key_exists('REMOTE_ADDR', $_SERVER)) { |
| 22 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 23 |
} |
| 24 |
|
| 25 |
if (is_string($ip)) { |
| 26 |
$ip = trim($ip); |
| 27 |
|
| 28 |
if (WPRHelper::safePregMatch('/^\[([0-9a-fA-F:]+)\](:[0-9]+)$/', $ip, $matches)) { |
| 29 |
$ip = $matches[1]; |
| 30 |
} elseif (WPRHelper::safePregMatch('/^([0-9.]+)(:[0-9]+)$/', $ip, $matches)) { |
| 31 |
$ip = $matches[1]; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
return self::isValidIP($ip) ? $ip : '127.0.0.1'; |
| 36 |
} |
| 37 |
|
| 38 |
public static function isIPv6($ip) { |
| 39 |
return (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? false : true; |
| 40 |
} |
| 41 |
|
| 42 |
public static function hasIPv6Support() { |
| 43 |
return defined('AF_INET6'); |
| 44 |
} |
| 45 |
|
| 46 |
public static function isValidIP($ip) { |
| 47 |
return filter_var($ip, FILTER_VALIDATE_IP) !== false; |
| 48 |
} |
| 49 |
|
| 50 |
public static function bvInetPton($ip) { |
| 51 |
$pton = self::isValidIP($ip) ? (self::hasIPv6Support() ? inet_pton($ip) : self::_bvInetPton($ip)) : false; |
| 52 |
return $pton; |
| 53 |
} |
| 54 |
|
| 55 |
public static function _bvInetPton($ip) { |
| 56 |
if (WPRHelper::safePregMatch('/^(?:\d{1,3}(?:\.|$)){4}/', $ip)) { |
| 57 |
$octets = explode('.', $ip); |
| 58 |
$bin = chr($octets[0]) . chr($octets[1]) . chr($octets[2]) . chr($octets[3]); |
| 59 |
return $bin; |
| 60 |
} |
| 61 |
|
| 62 |
if (WPRHelper::safePregMatch('/^((?:[\da-f]{1,4}(?::|)){0,8})(::)?((?:[\da-f]{1,4}(?::|)){0,8})$/i', $ip)) { |
| 63 |
if ($ip === '::') { |
| 64 |
return "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
| 65 |
} |
| 66 |
$colon_count = substr_count($ip, ':'); |
| 67 |
$dbl_colon_pos = strpos($ip, '::'); |
| 68 |
if ($dbl_colon_pos !== false) { |
| 69 |
$ip = str_replace('::', str_repeat(':0000', |
| 70 |
(($dbl_colon_pos === 0 || $dbl_colon_pos === strlen($ip) - 2) ? 9 : 8) - $colon_count) . ':', $ip); |
| 71 |
$ip = trim($ip, ':'); |
| 72 |
} |
| 73 |
|
| 74 |
$ip_groups = explode(':', $ip); |
| 75 |
$ipv6_bin = ''; |
| 76 |
foreach ($ip_groups as $ip_group) { |
| 77 |
$ipv6_bin .= pack('H*', str_pad($ip_group, 4, '0', STR_PAD_LEFT)); |
| 78 |
} |
| 79 |
|
| 80 |
return strlen($ipv6_bin) === 16 ? $ipv6_bin : false; |
| 81 |
} |
| 82 |
|
| 83 |
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)) { |
| 84 |
$octets = explode('.', $matches[1]); |
| 85 |
return chr($octets[0]) . chr($octets[1]) . chr($octets[2]) . chr($octets[3]); |
| 86 |
} |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
public static function isIPInRange($start_ip_range, $end_ip_range, $ip) { |
| 92 |
$bin_ip = null; |
| 93 |
if ($ip) { |
| 94 |
$bin_ip = self::bvInetPton($ip); |
| 95 |
} |
| 96 |
if ($bin_ip && $bin_ip >= self::bvInetPton($start_ip_range) |
| 97 |
&& $bin_ip <= self::bvInetPton($end_ip_range)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
public static function isPrivateIP($ip) { |
| 104 |
$private_ip_ranges = array( |
| 105 |
array("10.0.0.0", "10.255.255.255"), |
| 106 |
array("172.16.0.0", "172.31.255.255"), |
| 107 |
array("192.168.0.0", "192.168.255.255"), |
| 108 |
array("127.0.0.1", "127.255.255.255"), |
| 109 |
array("::1","::1"), |
| 110 |
array("fc00::","fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") |
| 111 |
); |
| 112 |
|
| 113 |
$result = false; |
| 114 |
foreach ($private_ip_ranges as $ip_range) { |
| 115 |
$result = self::isIPInRange($ip_range[0], $ip_range[1], $ip); |
| 116 |
if($result) { |
| 117 |
return $result; |
| 118 |
} |
| 119 |
} |
| 120 |
return $result; |
| 121 |
} |
| 122 |
|
| 123 |
public static function rrmdir($dir) { |
| 124 |
if (is_dir($dir)) { |
| 125 |
$objects = scandir($dir); |
| 126 |
foreach ($objects as $object) { |
| 127 |
if ($object != "." && $object != "..") { |
| 128 |
if (is_dir($dir . "/" . $object) && !is_link($dir . "/" . $object)) { |
| 129 |
WPRProtectUtils_V541::rrmdir($dir . "/" . $object); |
| 130 |
} else { |
| 131 |
unlink($dir . "/" . $object); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
rmdir($dir); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public static function getLength($val) { |
| 140 |
$length = 0; |
| 141 |
|
| 142 |
if (is_array($val)) { |
| 143 |
foreach ($val as $e) { |
| 144 |
$length += WPRProtectUtils_V541::getLength($e); |
| 145 |
} |
| 146 |
|
| 147 |
return $length; |
| 148 |
} else { |
| 149 |
return strlen((string) $val); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public static function parseFile($fname) { |
| 154 |
$result = array(); |
| 155 |
|
| 156 |
if (file_exists($fname)) { |
| 157 |
$content = file_get_contents($fname); |
| 158 |
if (($content !== false) && is_string($content)) { |
| 159 |
$result = json_decode($content, true); |
| 160 |
|
| 161 |
if (!is_array($result)) { |
| 162 |
$result = array(); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return $result; |
| 168 |
} |
| 169 |
|
| 170 |
public static function fileRemovePattern($fname, $pattern, $regex_pattern = false) { |
| 171 |
if (!file_exists($fname)) return; |
| 172 |
|
| 173 |
$content = file_get_contents($fname); |
| 174 |
if ($content) { |
| 175 |
if ($regex_pattern) { |
| 176 |
$modified_content = preg_replace($pattern, "", $content); |
| 177 |
} else { |
| 178 |
$modified_content = str_replace($pattern, "", $content); |
| 179 |
} |
| 180 |
|
| 181 |
if ($content !== $modified_content) { |
| 182 |
file_put_contents($fname, $modified_content); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public static function havePluginsLoaded() { |
| 188 |
return (function_exists('did_action') && (did_action('plugins_loaded') > 0)); |
| 189 |
} |
| 190 |
|
| 191 |
public static function haveMupluginsLoaded() { |
| 192 |
return (function_exists('did_action') && (did_action('muplugins_loaded') > 0)); |
| 193 |
} |
| 194 |
|
| 195 |
public static function isWPVersionCompatible($required) { |
| 196 |
global $wp_version; |
| 197 |
|
| 198 |
// Strip off any -alpha, -RC, -beta, -src suffixes. |
| 199 |
list( $version ) = explode( '-', $wp_version ); |
| 200 |
|
| 201 |
return empty( $required ) || version_compare( $version, $required, '>=' ); |
| 202 |
} |
| 203 |
|
| 204 |
public static function preInitWPHook($hook_name, $function_name, $priority, $accepted_args) { |
| 205 |
global $wp_filter; |
| 206 |
|
| 207 |
// Check if $wp_filter is not initialized or not an array |
| 208 |
if (!isset($wp_filter) || !is_array($wp_filter)) { |
| 209 |
$wp_filter = array(); |
| 210 |
} |
| 211 |
|
| 212 |
// Check if the hook exists in $wp_filter |
| 213 |
if (!isset($wp_filter[$hook_name])) { |
| 214 |
$wp_filter[$hook_name] = array(); |
| 215 |
} |
| 216 |
|
| 217 |
// Check if the priority exists for the hook |
| 218 |
if (!isset($wp_filter[$hook_name][$priority])) { |
| 219 |
$wp_filter[$hook_name][$priority] = array(); |
| 220 |
} |
| 221 |
|
| 222 |
// Add the filter function information to the $wp_filter array |
| 223 |
$wp_filter[$hook_name][$priority][] = array( |
| 224 |
'function' => $function_name, |
| 225 |
'accepted_args' => $accepted_args, |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
public static function signMessage($message, $key, $algorithm = 'sha256') { |
| 230 |
if (!is_string($message) || !is_string($key)) { |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
return hash_hmac($algorithm, $message, $key); |
| 235 |
} |
| 236 |
|
| 237 |
public static function verifyMessage($message, $signature, $key, $algorithm = 'sha256') { |
| 238 |
if (!is_string($message) || !is_string($signature) || !is_string($key)) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
$calc_signature = self::signMessage($message, $key, $algorithm); |
| 243 |
|
| 244 |
return hash_equals($calc_signature, $signature); |
| 245 |
} |
| 246 |
} |
| 247 |
endif; |