PluginProbe
Migrate to WordPress.com / 5.72
Migrate to WordPress.com v5.72
6.72 6.65 trunk 5.72 5.81 5.88
wpcom-migration / helper.php

helper.php in Migrate to WordPress.com 5.72, at helper.php

222 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('WPCOMHelper')) :
5 class WPCOMHelper {
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) {
54 if (!is_string($replace_regex) || !is_string($replace_string) || !is_string($element)) {
55 return $element;
56 }
57 $updated_element = preg_replace($replace_regex, $replace_string, $element);
58 if ($updated_element === null && preg_last_error() !== PREG_NO_ERROR) {
59 return $element;
60 }
61 return $updated_element;
62 }
63
64 public static function safeStrReplace($search, $replace, $subject) {
65 if (!is_string($search) || !is_string($replace) || !is_string($subject)) {
66 return $subject;
67 }
68 $updated_subject = str_replace($search, $replace, $subject);
69 if ($updated_subject === null) {
70 return $subject;
71 }
72 return $updated_subject;
73 }
74
75 public static function preInitWPHook($hook_name, $function_name, $priority, $accepted_args) {
76 global $wp_filter;
77
78 // Check if $wp_filter is not initialized or not an array
79 if (!isset($wp_filter) || !is_array($wp_filter)) {
80 $wp_filter = array();
81 }
82
83 // Check if the hook exists in $wp_filter
84 if (!isset($wp_filter[$hook_name])) {
85 $wp_filter[$hook_name] = array();
86 }
87
88 // Check if the priority exists for the hook
89 if (!isset($wp_filter[$hook_name][$priority])) {
90 $wp_filter[$hook_name][$priority] = array();
91 }
92
93 // Add the filter function information to the $wp_filter array
94 $wp_filter[$hook_name][$priority][] = array(
95 'function' => $function_name,
96 'accepted_args' => $accepted_args,
97 );
98 }
99
100 public static function removePatternFromWpConfig($pattern) {
101 if (!defined('ABSPATH')) {
102 return;
103 }
104
105 $wp_conf_paths = array(
106 rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/wp-config.php",
107 rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "../wp-config.php"
108 );
109
110 if (file_exists($wp_conf_paths[0])) {
111 $fname = $wp_conf_paths[0];
112 } elseif (file_exists($wp_conf_paths[1])) {
113 $fname = $wp_conf_paths[1];
114 } else {
115 return;
116 }
117
118 self::fileRemovePattern($fname, $pattern);
119 }
120
121 public static function fileRemovePattern($fname, $pattern, $is_regex = false) {
122 if (!is_string($fname) || !is_string($pattern)) {
123 return;
124 }
125 if (!file_exists($fname)) return;
126
127 $content = file_get_contents($fname);
128 if ($content !== false) {
129 if ($is_regex !== false) {
130 $modified_content = preg_replace($pattern, "", $content);
131 } else {
132 $modified_content = str_replace($pattern, "", $content);
133 }
134
135 if (empty($modified_content)) {
136 return;
137 }
138
139 if ($content !== $modified_content) {
140 file_put_contents($fname, $modified_content);
141 }
142 }
143 }
144
145 public static function opensslEncrypt($plain_text, $cipher_algo, $encryption_key, $iv = null) {
146 if (!function_exists('openssl_encrypt') || !function_exists('openssl_get_cipher_methods') ||
147 !function_exists('openssl_random_pseudo_bytes') || !function_exists('openssl_cipher_iv_length')) {
148 return array(false, "OpenSSL extension not found.");
149 }
150
151 if (empty($plain_text) || !is_string($plain_text) ||
152 empty($encryption_key) || !is_string($encryption_key)) {
153 return array(false, "Plain text or encryption key is not a valid string.");
154 }
155
156 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
157 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
158 }
159
160 if ($iv === null) {
161 $iv_length = openssl_cipher_iv_length($cipher_algo);
162 if ($iv_length === false) {
163 return array(false, "IV length not found.");
164 }
165 $iv = openssl_random_pseudo_bytes($iv_length);
166 if ($iv === false) {
167 return array(false, "IV generation failed.");
168 }
169 }
170
171 if (strlen($iv) !== $iv_length) {
172 return array(false, "Invalid IV length. Expected length is " . $iv_length . " bytes.");
173 }
174
175 $encrypted_data = openssl_encrypt($plain_text, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
176 if ($encrypted_data === false) {
177 return array(false, "Encryption failed.");
178 }
179
180 return array(true, ($iv . $encrypted_data));
181 }
182
183 public static function opensslDecrypt($data, $cipher_algo, $encryption_key) {
184 if (!function_exists('openssl_decrypt') || !function_exists('openssl_get_cipher_methods') ||
185 !function_exists('openssl_cipher_iv_length')) {
186 return array(false, "OpenSSL extension not found.");
187 }
188
189 if (empty($data) || !is_string($data) || empty($encryption_key) || !is_string($encryption_key)) {
190 return array(false, "Encrypted secret or encryption key is not a valid string.");
191 }
192
193 if (!in_array($cipher_algo, openssl_get_cipher_methods(), true)) {
194 return array(false, "Invalid cipher algorithm - " . $cipher_algo);
195 }
196
197 $iv_length = openssl_cipher_iv_length($cipher_algo);
198 if ($iv_length === false) {
199 return array(false, "IV length not found.");
200 }
201
202 if (strlen($data) <= $iv_length) {
203 return array(false, "Data length is insufficient to contain IV.");
204 }
205
206 $iv = substr($data, 0, $iv_length);
207 $encrypted_data = substr($data, $iv_length);
208
209 if ($iv === false || $encrypted_data === false) {
210 return array(false, "IV or encrypted data not found.");
211 }
212
213 $decrypted_data = openssl_decrypt($encrypted_data, $cipher_algo, $encryption_key, OPENSSL_RAW_DATA, $iv);
214
215 if ($decrypted_data === false) {
216 return array(false, "Decryption failed.");
217 }
218
219 return array(true, $decrypted_data);
220 }
221 }
222 endif;