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 / helper.php

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

242 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') && !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 preInitWPHook($hook_name, $function_name, $priority, $accepted_args) {
79 global $wp_filter;
80
81 // Check if $wp_filter is not initialized or not an array
82 if (!isset($wp_filter) || !is_array($wp_filter)) {
83 $wp_filter = array();
84 }
85
86 // Check if the hook exists in $wp_filter
87 if (!isset($wp_filter[$hook_name])) {
88 $wp_filter[$hook_name] = array();
89 }
90
91 // Check if the priority exists for the hook
92 if (!isset($wp_filter[$hook_name][$priority])) {
93 $wp_filter[$hook_name][$priority] = array();
94 }
95
96 // Add the filter function information to the $wp_filter array
97 $wp_filter[$hook_name][$priority][] = array(
98 'function' => $function_name,
99 'accepted_args' => $accepted_args,
100 );
101 }
102
103 public static function removePatternFromWpConfig($pattern) {
104 if (!defined('ABSPATH')) {
105 return;
106 }
107
108 $wp_conf_paths = array(
109 rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/wp-config.php",
110 rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "../wp-config.php"
111 );
112
113 if (file_exists($wp_conf_paths[0])) {
114 $fname = $wp_conf_paths[0];
115 } elseif (file_exists($wp_conf_paths[1])) {
116 $fname = $wp_conf_paths[1];
117 } else {
118 return;
119 }
120
121 self::fileRemovePattern($fname, $pattern);
122 }
123
124 public static function fileRemovePattern($fname, $pattern, $is_regex = false) {
125 if (!is_string($fname) || !is_string($pattern)) {
126 return;
127 }
128
129 $filesystem = self::get_direct_filesystem();
130
131 if (!$filesystem->exists($fname)) {
132 return;
133 }
134
135 $content = $filesystem->get_contents($fname);
136 if ($content !== false) {
137 if ($is_regex !== false) {
138 $modified_content = preg_replace($pattern, "", $content);
139 } else {
140 $modified_content = str_replace($pattern, "", $content);
141 }
142
143 if (empty($modified_content)) {
144 return;
145 }
146
147 if ($content !== $modified_content) {
148 $filesystem->put_contents($fname, $modified_content, intval($filesystem->getchmod($fname), 8));
149 }
150 }
151 }
152
153 public static function opensslEncrypt($plain_text, $cipher_algo, $encryption_key, $iv = null) {
154 if (!function_exists('openssl_encrypt') || !function_exists('openssl_get_cipher_methods') ||
155 !function_exists('openssl_random_pseudo_bytes') || !function_exists('openssl_cipher_iv_length')) {
156 return array(false, "OpenSSL extension not found.");
157 }
158
159 if (empty($plain_text) || !is_string($plain_text) ||
160 empty($encryption_key) || !is_string($encryption_key)) {
161 return array(false, "Plain text or encryption key is not a valid string.");
162 }
163
164 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
165 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
166 }
167
168 if ($iv === null) {
169 $iv_length = openssl_cipher_iv_length($cipher_algo);
170 if ($iv_length === false) {
171 return array(false, "IV length not found.");
172 }
173 $iv = openssl_random_pseudo_bytes($iv_length);
174 if ($iv === false) {
175 return array(false, "IV generation failed.");
176 }
177 }
178
179 if (strlen($iv) !== $iv_length) {
180 return array(false, "Invalid IV length. Expected length is " . $iv_length . " bytes.");
181 }
182
183 $encrypted_data = openssl_encrypt($plain_text, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
184 if ($encrypted_data === false) {
185 return array(false, "Encryption failed.");
186 }
187
188 return array(true, ($iv . $encrypted_data));
189 }
190
191 public static function opensslDecrypt($data, $cipher_algo, $encryption_key) {
192 if (!function_exists('openssl_decrypt') || !function_exists('openssl_get_cipher_methods') ||
193 !function_exists('openssl_cipher_iv_length')) {
194 return array(false, "OpenSSL extension not found.");
195 }
196
197 if (empty($data) || !is_string($data) || empty($encryption_key) || !is_string($encryption_key)) {
198 return array(false, "Encrypted secret or encryption key is not a valid string.");
199 }
200
201 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
202 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
203 }
204
205 $iv_length = openssl_cipher_iv_length($cipher_algo);
206 if ($iv_length === false) {
207 return array(false, "IV length not found.");
208 }
209
210 if (strlen($data) <= $iv_length) {
211 return array(false, "Data length is insufficient to contain IV.");
212 }
213
214 $iv = substr($data, 0, $iv_length);
215 $encrypted_data = substr($data, $iv_length);
216
217 if ($iv === false || $encrypted_data === false) {
218 return array(false, "IV or encrypted data not found.");
219 }
220
221 $decrypted_data = openssl_decrypt($encrypted_data, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
222
223 if ($decrypted_data === false) {
224 return array(false, "Decryption failed.");
225 }
226
227 return array(true, $decrypted_data);
228 }
229 public static function get_direct_filesystem() {
230 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
231 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
232 return new WP_Filesystem_Direct(new StdClass());
233 }
234
235 public static function unslashIfWPLoaded($str) {
236 if (function_exists('wp_unslash')) {
237 return wp_unslash($str);
238 }
239 return $str;
240 }
241 }
242 endif;