PluginProbe
The WP Remote WordPress Plugin / 5.81
The WP Remote WordPress Plugin v5.81
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.81, at helper.php

225 lines 6.7 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 if (!file_exists($fname)) return;
129
130 $content = file_get_contents($fname);
131 if ($content !== false) {
132 if ($is_regex !== false) {
133 $modified_content = preg_replace($pattern, "", $content);
134 } else {
135 $modified_content = str_replace($pattern, "", $content);
136 }
137
138 if (empty($modified_content)) {
139 return;
140 }
141
142 if ($content !== $modified_content) {
143 file_put_contents($fname, $modified_content);
144 }
145 }
146 }
147
148 public static function opensslEncrypt($plain_text, $cipher_algo, $encryption_key, $iv = null) {
149 if (!function_exists('openssl_encrypt') || !function_exists('openssl_get_cipher_methods') ||
150 !function_exists('openssl_random_pseudo_bytes') || !function_exists('openssl_cipher_iv_length')) {
151 return array(false, "OpenSSL extension not found.");
152 }
153
154 if (empty($plain_text) || !is_string($plain_text) ||
155 empty($encryption_key) || !is_string($encryption_key)) {
156 return array(false, "Plain text or encryption key is not a valid string.");
157 }
158
159 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
160 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
161 }
162
163 if ($iv === null) {
164 $iv_length = openssl_cipher_iv_length($cipher_algo);
165 if ($iv_length === false) {
166 return array(false, "IV length not found.");
167 }
168 $iv = openssl_random_pseudo_bytes($iv_length);
169 if ($iv === false) {
170 return array(false, "IV generation failed.");
171 }
172 }
173
174 if (strlen($iv) !== $iv_length) {
175 return array(false, "Invalid IV length. Expected length is " . $iv_length . " bytes.");
176 }
177
178 $encrypted_data = openssl_encrypt($plain_text, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
179 if ($encrypted_data === false) {
180 return array(false, "Encryption failed.");
181 }
182
183 return array(true, ($iv . $encrypted_data));
184 }
185
186 public static function opensslDecrypt($data, $cipher_algo, $encryption_key) {
187 if (!function_exists('openssl_decrypt') || !function_exists('openssl_get_cipher_methods') ||
188 !function_exists('openssl_cipher_iv_length')) {
189 return array(false, "OpenSSL extension not found.");
190 }
191
192 if (empty($data) || !is_string($data) || empty($encryption_key) || !is_string($encryption_key)) {
193 return array(false, "Encrypted secret or encryption key is not a valid string.");
194 }
195
196 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
197 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
198 }
199
200 $iv_length = openssl_cipher_iv_length($cipher_algo);
201 if ($iv_length === false) {
202 return array(false, "IV length not found.");
203 }
204
205 if (strlen($data) <= $iv_length) {
206 return array(false, "Data length is insufficient to contain IV.");
207 }
208
209 $iv = substr($data, 0, $iv_length);
210 $encrypted_data = substr($data, $iv_length);
211
212 if ($iv === false || $encrypted_data === false) {
213 return array(false, "IV or encrypted data not found.");
214 }
215
216 $decrypted_data = openssl_decrypt($encrypted_data, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
217
218 if ($decrypted_data === false) {
219 return array(false, "Decryption failed.");
220 }
221
222 return array(true, $decrypted_data);
223 }
224 }
225 endif;