PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.3.7
Backup Migration v1.3.7
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / config.php
backup-backup / includes Last commit date
banner 2 years ago check 2 years ago cli 2 years ago cron 2 years ago dashboard 2 years ago database 2 years ago extracter 2 years ago htaccess 2 years ago progress 2 years ago scanner 2 years ago staging 2 years ago uploader 2 years ago zipper 2 years ago .htaccess 2 years ago activation.php 2 years ago ajax.php 2 years ago analyst.php 2 years ago backup-heart.php 2 years ago bypasser.php 2 years ago cli-handler.php 2 years ago compatibility.php 2 years ago config.php 2 years ago constants.php 2 years ago initializer.php 2 years ago logger.php 2 years ago restore-batching.php 2 years ago
config.php
302 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\Dashboard;
5
6 // Exit on direct access
7 if (!defined('ABSPATH')) exit;
8
9 if (!function_exists('bmi_get_config')) {
10 function bmi_get_config($setting, $configpath = false) {
11
12 if ($configpath == false && defined('BMI_CONFIG_PATH')) {
13 $configpath = BMI_CONFIG_PATH;
14 }
15
16 // Load default and additional
17 $defaults = json_decode(file_get_contents(BMI_CONFIG_DEFAULT));
18
19 // Result default
20 if (isset($defaults->{$setting}))
21 $result = $defaults->{$setting};
22 else $result = array();
23
24 // Load user config
25 if (file_exists($configpath) && defined('BMI_CONFIG_STATUS') && BMI_CONFIG_STATUS) {
26
27 // Get file contents
28 $bmi_config_contents = file_get_contents($configpath);
29 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
30 $bmi_config_contents = substr($bmi_config_contents, 8);
31 }
32
33 $bmi_config_json = json_decode($bmi_config_contents);
34
35 // If config is correct set it
36 if (json_last_error() == JSON_ERROR_NONE) {
37
38 // Setting exist?
39 if (isset($bmi_config_json->{$setting})) {
40
41 // Get result
42 $result = $bmi_config_json->{$setting};
43
44 }
45
46 }
47
48 }
49
50 // Replace exceptions
51 if ($setting == 'STORAGE::LOCAL::PATH' && $result == 'default') {
52 $result = BMI_BACKUPS_DEFAULT;
53 }
54
55 // Replace backshashes
56 if ($setting == 'STORAGE::LOCAL::PATH') {
57 $result = str_replace('\\\\', DIRECTORY_SEPARATOR, $result);
58 $result = str_replace('\\', DIRECTORY_SEPARATOR, $result);
59 $result = str_replace('/', DIRECTORY_SEPARATOR, $result);
60 }
61
62 // Return setting
63 return $result;
64
65 }
66 }
67
68 if (!function_exists('bmi_set_config')) {
69 function bmi_set_config($setting, $value) {
70
71 // Load default and additional
72 if (file_exists(BMI_CONFIG_PATH)) {
73
74 // Get file contents
75 $bmi_config_contents = file_get_contents(BMI_CONFIG_PATH);
76 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
77 $bmi_config_contents = substr($bmi_config_contents, 8);
78 }
79
80 $bmi_config_json = json_decode($bmi_config_contents);
81
82 // Result default
83 $default = bmi_get_config($setting);
84
85 // If config is correct set it
86 if (!(json_last_error() == JSON_ERROR_NONE)) {
87
88 // Setting refill base
89 $bmi_config_json = json_decode(json_encode(array()));
90
91 }
92
93 // Allow empty
94 $allow_empty = ['OTHER:CLI:PATH'];
95
96 // Check if setting is not empty
97 if (isset($value) && (!is_string($value) || (in_array($setting, $allow_empty) || strlen(trim($value)) > 0))) {
98
99 // Set new setting
100 @$bmi_config_json->{$setting} = $value;
101
102 } else return false;
103
104 // Write edited settings
105 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
106 file_put_contents(BMI_CONFIG_PATH, "<?php //" . json_encode($bmi_config_json));
107 } else {
108 file_put_contents(BMI_CONFIG_PATH, json_encode($bmi_config_json));
109 }
110
111 return true;
112
113 }
114
115 return false;
116
117 }
118 }
119
120 if (!function_exists('bmi_try_checked')) {
121 function bmi_try_checked($setting, $reversed = false) {
122
123 if (!$reversed) {
124
125 if (bmi_get_config($setting) == 'true' || bmi_get_config($setting) === true) {
126 echo ' checked';
127 } else return false;
128
129 } else {
130
131 if (bmi_get_config($setting) == 'true' || bmi_get_config($setting) === true) {
132 return false;
133 } else {
134 echo ' checked';
135 }
136
137 }
138
139 }
140 }
141
142 if (!function_exists('bmi_try_value')) {
143 function bmi_try_value($setting) {
144
145 $res = bmi_get_config($setting);
146 if ($res !== false) {
147 echo ' value="' . sanitize_text_field($res) . '"';
148 } else echo '';
149
150 }
151 }
152
153 function bmi_try_convert_old_to_new_config(&$bmi_initial_config_filepath, &$bmi_initial_config_dirpath, $init = true) {
154
155 $newConfigStaticPath = BMI_STATIC_PHP_CONFIG;
156 if (file_exists($newConfigStaticPath)) {
157
158 if (!defined('BMI_CONFIG_PHP')) define('BMI_CONFIG_PHP', true);
159 if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true);
160 if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $newConfigStaticPath);
161 if (!defined('BMI_INITIAL_CONFIG_PATH')) define('BMI_INITIAL_CONFIG_PATH', $bmi_initial_config_filepath);
162
163 $localStoragePath = bmi_get_config('STORAGE::LOCAL::PATH', $newConfigStaticPath);
164 if ($localStoragePath == "default") $localStoragePath = BMI_BACKUPS_DEFAULT;
165
166 if (!(is_readable($localStoragePath) && is_dir($localStoragePath))) {
167 $localStoragePath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration-' . bmi_config_random_string(10);
168 bmi_set_config('STORAGE::LOCAL::PATH', $localStoragePath);
169 }
170
171 if (basename($localStoragePath) == 'backup-migration') {
172 $current_patch = get_option('bmi_hotfixes', array());
173 $key = array_search('BMI_D20_M07_01', $current_patch);
174 if ($key !== false) unset($current_patch[$key]);
175 update_option('bmi_hotfixes', $current_patch);
176 }
177
178 if (!defined('BMI_BACKUPS_ROOT')) define('BMI_BACKUPS_ROOT', $localStoragePath);
179 if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', $localStoragePath);
180 if (!defined('BMI_BACKUPS')) define('BMI_BACKUPS', $localStoragePath . DIRECTORY_SEPARATOR . 'backups');
181 if (!defined('BMI_STAGING')) define('BMI_STAGING', $localStoragePath . DIRECTORY_SEPARATOR . 'staging');
182 if (!defined('BMI_TMP')) define('BMI_TMP', BMI_BACKUPS_ROOT . DIRECTORY_SEPARATOR . 'tmp');
183
184 $bmi_initial_config_dirpath = $localStoragePath;
185 $bmi_initial_config_filepath = $newConfigStaticPath;
186
187 return true;
188
189 } else {
190
191 if (file_exists($bmi_initial_config_filepath)) {
192 file_put_contents($newConfigStaticPath, "<?php //" . file_get_contents($bmi_initial_config_filepath));
193 @unlink($bmi_initial_config_filepath);
194 if ($init) bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath, false);
195 $bmi_initial_config_filepath = $newConfigStaticPath;
196 }
197
198 return false;
199
200 }
201
202 }
203
204 function bmi_config_random_string($max = 16) {
205
206 $bank = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
207 $bank .= 'abcdefghijklmnopqrstuvwxyz';
208 $bank .= '0123456789';
209
210 $str = str_shuffle($bank);
211
212 while (is_numeric($str[0])) {
213 $str = str_shuffle($bank);
214 }
215
216 $str = substr($str, 0, $max);
217
218 return $str;
219
220 }
221
222 function bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath) {
223
224 if (!file_exists(dirname($bmi_initial_config_filepath)) && !is_dir(dirname($bmi_initial_config_filepath))) {
225 @mkdir(dirname($bmi_initial_config_filepath), 0755, true);
226 }
227
228 @copy(BMI_CONFIG_DEFAULT, $bmi_initial_config_filepath);
229 bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
230
231 if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true);
232 if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $bmi_initial_config_filepath);
233 if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', dirname($bmi_initial_config_filepath));
234
235 }
236
237 $bmi_initial_config_filepath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration' . DIRECTORY_SEPARATOR . 'config.json';
238 $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath);
239 $bmi_database_config_dirpath = get_option('BMI::STORAGE::LOCAL::PATH', false);
240
241 if ($bmi_database_config_dirpath != false && dirname($bmi_database_config_dirpath) != $bmi_initial_config_dirpath) {
242 $bmi_initial_config_filepath = $bmi_database_config_dirpath . DIRECTORY_SEPARATOR . 'config.json';
243 $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath);
244 }
245
246 // Get config and parse it
247 if (file_exists(BMI_STATIC_PHP_CONFIG)) {
248
249 $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
250
251 } elseif (file_exists($bmi_initial_config_filepath)) {
252
253 // Convert config
254 $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
255
256 // Get file contents
257 $bmi_config_contents = file_get_contents($bmi_initial_config_filepath);
258 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
259 $bmi_config_contents = substr($bmi_config_contents, 8);
260 }
261
262 $bmi_config_json = json_decode($bmi_config_contents);
263
264 // If config is correct set it
265 if (json_last_error() == JSON_ERROR_NONE) {
266
267 $localStoragePath = BMI_BACKUPS_ROOT;
268 if ($bmi_database_config_dirpath == false || $bmi_database_config_dirpath != $localStoragePath) {
269 $prev_path = dirname(BMI_INITIAL_CONFIG_PATH);
270 $prev_path_backups = dirname(BMI_INITIAL_CONFIG_PATH) . DIRECTORY_SEPARATOR . 'backups';
271
272 if (file_exists($prev_path_backups) && is_dir($prev_path_backups)) {
273 $scanned_directory_backups = array_diff(scandir($prev_path_backups), ['..', '.']);
274 foreach ($scanned_directory_backups as $i => $file) {
275 if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) {
276 rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file);
277 }
278 }
279 if ($prev_path != $localStoragePath && sizeof(scandir($prev_path_backups)) <= 2) {
280 @rmdir($prev_path_backups);
281 }
282 }
283
284 if (file_exists($prev_path) && is_dir($prev_path)) {
285 $scanned_directory = array_diff(scandir($prev_path), ['..', '.']);
286 foreach ($scanned_directory as $i => $file) {
287 if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) {
288 rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file);
289 }
290 }
291 if ($localStoragePath != $prev_path && sizeof(scandir($prev_path)) <= 2) {
292 @rmdir($prev_path);
293 }
294 }
295
296 update_option('BMI::STORAGE::LOCAL::PATH', $localStoragePath);
297 }
298
299 } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
300
301 } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
302