PluginProbe
The WP Remote WordPress Plugin / 6.72
The WP Remote WordPress Plugin v6.72
6.72 6.69 6.65 6.62 6.48 6.47 4.87 4.97 5.05 5.09 5.16 5.22 5.24 5.25 5.38 5.41 5.42 5.45 5.47 5.53 5.56 5.65 5.68 5.72 5.73 All 53 releases
wpremote / protect / lib / utils.php

utils.php in The WP Remote WordPress Plugin 6.72, at protect/lib/utils.php

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