PluginProbe
The WP Remote WordPress Plugin / 6.65
The WP Remote WordPress Plugin v6.65
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 / helper.php

helper.php in The WP Remote WordPress Plugin 6.65, at helper.php

367 lines 11.1 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') && !defined('PHP_ERR_MONIT_PATH')) exit;
3
4 if (!class_exists('WPRHelper')) :
5 class WPRHelper {
6 public static function safePregMatch($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) {
7 if (!is_string($pattern) || !is_string($subject)) {
8 return false;
9 }
10 return preg_match($pattern, $subject, $matches, $flags, $offset);
11 }
12
13 # XNOTE - The below function assumes valid input
14 # $array should be an array and $keys should be an array of string, or integer data
15 public static function filterArray($array, $keys) {
16 $filteredArray = array();
17 foreach ($keys as $key) {
18 if (array_key_exists($key, $array)) {
19 $filteredArray[$key] = $array[$key];
20 }
21 }
22 return $filteredArray;
23 }
24
25 # XNOTE - The below function assumes valid input
26 # $array should be an array and $keys should be an array of string, or integer data
27 public static function digArray($array, $keys) {
28 if (empty($keys)) {
29 return null;
30 }
31 $curr_array = $array;
32 foreach ($keys as $key) {
33 if (is_array($curr_array) && array_key_exists($key, $curr_array)) {
34 $curr_array = $curr_array[$key];
35 } else {
36 return null;
37 }
38 }
39 return $curr_array;
40 }
41
42 public static function arrayKeyFirst($array) {
43 if (!function_exists('array_key_first')) {
44 foreach ($array as $key => $value) {
45 return $key;
46 }
47 return null;
48 }
49
50 return array_key_first($array);
51 }
52
53 public static function safePregReplace($replace_regex, $replace_string, $element, $limit = -1) {
54 if (!is_string($replace_regex) || !is_string($replace_string) || !is_string($element) || !is_int($limit)) {
55 return $element;
56 }
57
58 $updated_element = preg_replace($replace_regex, $replace_string, $element, $limit);
59
60 if ($updated_element === null && preg_last_error() !== PREG_NO_ERROR) {
61 return $element;
62 }
63
64 return $updated_element;
65 }
66
67 public static function safeStrReplace($search, $replace, $subject) {
68 if (!is_string($search) || !is_string($replace) || !is_string($subject)) {
69 return $subject;
70 }
71 $updated_subject = str_replace($search, $replace, $subject);
72 if ($updated_subject === null) {
73 return $subject;
74 }
75 return $updated_subject;
76 }
77
78 public static function safeStrReplaceFirst($search, $replace, $subject) {
79 if (!is_string($search) || !is_string($replace) || !is_string($subject) || $search === '') {
80 return $subject;
81 }
82
83 $position = strpos($subject, $search);
84 if ($position === false) {
85 return $subject;
86 }
87
88 return substr_replace($subject, $replace, $position, strlen($search));
89 }
90
91 public static function preInitWPHook($hook_name, $function_name, $priority, $accepted_args) {
92 global $wp_filter;
93
94 // Check if $wp_filter is not initialized or not an array
95 if (!isset($wp_filter) || !is_array($wp_filter)) {
96 $wp_filter = array();
97 }
98
99 // Check if the hook exists in $wp_filter
100 if (!isset($wp_filter[$hook_name])) {
101 $wp_filter[$hook_name] = array();
102 }
103
104 // Check if the priority exists for the hook
105 if (!isset($wp_filter[$hook_name][$priority])) {
106 $wp_filter[$hook_name][$priority] = array();
107 }
108
109 // Add the filter function information to the $wp_filter array
110 $wp_filter[$hook_name][$priority][] = array(
111 'function' => $function_name,
112 'accepted_args' => $accepted_args,
113 );
114 }
115
116 public static function removePatternFromWpConfig($pattern) {
117 if (!defined('ABSPATH')) {
118 return;
119 }
120
121 $wp_conf_paths = array(
122 rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/wp-config.php",
123 rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "../wp-config.php"
124 );
125
126 if (file_exists($wp_conf_paths[0])) {
127 $fname = $wp_conf_paths[0];
128 } elseif (file_exists($wp_conf_paths[1])) {
129 $fname = $wp_conf_paths[1];
130 } else {
131 return;
132 }
133
134 self::fileRemovePattern($fname, $pattern);
135 }
136
137 public static function fileRemovePattern($fname, $pattern, $is_regex = false) {
138 if (!is_string($fname) || !is_string($pattern)) {
139 return;
140 }
141
142 if (!WPRWPFileSystem::getInstance()->exists($fname)) {
143 return;
144 }
145
146 $content = WPRWPFileSystem::getInstance()->getContents($fname);
147 if ($content !== false) {
148 if ($is_regex !== false) {
149 $modified_content = preg_replace($pattern, "", $content);
150 } else {
151 $modified_content = str_replace($pattern, "", $content);
152 }
153
154 if (empty($modified_content)) {
155 return;
156 }
157
158 if ($content !== $modified_content) {
159 WPRWPFileSystem::getInstance()->putContents($fname, $modified_content,
160 WPRWPFileSystem::getInstance()->getchmodOctal($fname));
161 }
162 }
163 }
164
165 public static function opensslEncrypt($plain_text, $cipher_algo, $encryption_key, $iv = null) {
166 if (!function_exists('openssl_encrypt') || !function_exists('openssl_get_cipher_methods') ||
167 !function_exists('openssl_random_pseudo_bytes') || !function_exists('openssl_cipher_iv_length')) {
168 return array(false, "OpenSSL extension not found.");
169 }
170
171 if (empty($plain_text) || !is_string($plain_text) ||
172 empty($encryption_key) || !is_string($encryption_key)) {
173 return array(false, "Plain text or encryption key is not a valid string.");
174 }
175
176 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
177 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
178 }
179
180 if ($iv === null) {
181 $iv_length = openssl_cipher_iv_length($cipher_algo);
182 if ($iv_length === false) {
183 return array(false, "IV length not found.");
184 }
185 $iv = openssl_random_pseudo_bytes($iv_length);
186 if ($iv === false) {
187 return array(false, "IV generation failed.");
188 }
189 }
190
191 if (strlen($iv) !== $iv_length) {
192 return array(false, "Invalid IV length. Expected length is " . $iv_length . " bytes.");
193 }
194
195 $encrypted_data = openssl_encrypt($plain_text, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
196 if ($encrypted_data === false) {
197 return array(false, "Encryption failed.");
198 }
199
200 return array(true, ($iv . $encrypted_data));
201 }
202
203 public static function opensslDecrypt($data, $cipher_algo, $encryption_key) {
204 if (!function_exists('openssl_decrypt') || !function_exists('openssl_get_cipher_methods') ||
205 !function_exists('openssl_cipher_iv_length')) {
206 return array(false, "OpenSSL extension not found.");
207 }
208
209 if (empty($data) || !is_string($data) || empty($encryption_key) || !is_string($encryption_key)) {
210 return array(false, "Encrypted secret or encryption key is not a valid string.");
211 }
212
213 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
214 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
215 }
216
217 $iv_length = openssl_cipher_iv_length($cipher_algo);
218 if ($iv_length === false) {
219 return array(false, "IV length not found.");
220 }
221
222 if (strlen($data) <= $iv_length) {
223 return array(false, "Data length is insufficient to contain IV.");
224 }
225
226 $iv = substr($data, 0, $iv_length);
227 $encrypted_data = substr($data, $iv_length);
228
229 if ($iv === false || $encrypted_data === false) {
230 return array(false, "IV or encrypted data not found.");
231 }
232
233 $decrypted_data = openssl_decrypt($encrypted_data, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
234
235 if ($decrypted_data === false) {
236 return array(false, "Decryption failed.");
237 }
238
239 return array(true, $decrypted_data);
240 }
241
242 public static function get_direct_filesystem() {
243 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
244 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
245 return new WP_Filesystem_Direct(new StdClass());
246 }
247
248 /**
249 * Maybe unslash a value if WordPress is loaded
250 *
251 * @param string $value The value to potentially unslash
252 * @return string The unslashed value if WP is loaded, original value otherwise
253 */
254 public static function maybeUnslashValue($value) {
255 if (function_exists('wp_unslash')) {
256 return wp_unslash($value);
257 }
258 return $value;
259 }
260
261 /**
262 * Get and sanitize a string parameter from superglobal
263 *
264 * @param string $superglobal The superglobal type ('GET', 'POST', etc.)
265 * @param string $key The parameter key to retrieve
266 * @param string $context The sanitization context ('text', 'email', 'url')
267 * @return string|null Sanitized string value or null if invalid or unknown context
268 */
269 public static function getStringParamSanitized($superglobal, $key, $context) {
270 $raw_value = self::getRawParam($superglobal, $key);
271
272 if (!is_string($raw_value)) {
273 return null;
274 }
275
276 switch ($context) {
277 case 'text':
278 if (!function_exists('sanitize_text_field')) {
279 return null;
280 }
281 return sanitize_text_field($raw_value);
282 case 'email':
283 if (!function_exists('sanitize_email')) {
284 return null;
285 }
286 return sanitize_email($raw_value);
287 case 'url':
288 if (!function_exists('esc_url_raw')) {
289 return null;
290 }
291 return esc_url_raw($raw_value);
292 default:
293 return null;
294 }
295 }
296
297 /**
298 * Get and escape a string parameter from superglobal
299 *
300 * @param string $superglobal The superglobal type ('GET', 'POST', etc.)
301 * @param string $key The parameter key to retrieve
302 * @param string $context The escaping context ('attr', 'html', 'url')
303 * @return string|null Escaped string value or null if invalid or unknown context
304 */
305 public static function getStringParamEscaped($superglobal, $key, $context) {
306 $raw_value = self::getRawParam($superglobal, $key);
307
308 if (!is_string($raw_value)) {
309 return null;
310 }
311
312 switch ($context) {
313 case 'attr':
314 if (!function_exists('esc_attr')) {
315 return null;
316 }
317 return esc_attr($raw_value);
318 case 'html':
319 if (!function_exists('esc_html')) {
320 return null;
321 }
322 return esc_html($raw_value);
323 case 'url':
324 if (!function_exists('esc_url')) {
325 return null;
326 }
327 return esc_url($raw_value);
328 default:
329 return null;
330 }
331 }
332
333 // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
334 /**
335 * Get raw parameter value from superglobal
336 *
337 * @param string $superglobal The superglobal type ('GET', 'POST', etc.)
338 * @param string $key The parameter key to retrieve
339 * @return mixed Raw parameter value or null if not found
340 */
341 public static function getRawParam($superglobal, $key) {
342 $value = null;
343
344 switch (strtoupper($superglobal)) {
345 case 'GET':
346 $value = isset($_GET[$key]) ? $_GET[$key] : null;
347 break;
348 case 'POST':
349 $value = isset($_POST[$key]) ? $_POST[$key] : null;
350 break;
351 case 'COOKIE':
352 $value = isset($_COOKIE[$key]) ? $_COOKIE[$key] : null;
353 break;
354 case 'REQUEST':
355 $value = isset($_REQUEST[$key]) ? $_REQUEST[$key] : null;
356 break;
357 case 'SERVER':
358 $value = isset($_SERVER[$key]) ? $_SERVER[$key] : null;
359 break;
360 }
361
362 return $value !== null ? self::maybeUnslashValue($value) : null;
363 }
364 // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
365 }
366 endif;
367