PluginProbe
The WP Remote WordPress Plugin / 5.88
The WP Remote WordPress Plugin v5.88
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 5.88, at protect/lib/utils.php

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