PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.4
Backup Migration v2.1.4
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 months ago bodies 2 months ago check 2 months ago cli 2 months ago cron 2 months ago dashboard 2 months ago database 2 months ago external 2 months ago extracter 2 months ago htaccess 2 months ago notices 2 months ago progress 2 months ago scanner 2 months ago staging 2 months ago traits 2 months ago uploader 2 months ago vendor 2 months ago zipper 2 months ago .htaccess 2 months ago activation.php 2 months ago ajax.php 2 months ago ajax_offline.php 2 months ago analyst.php 2 months ago backup-process.php 2 months ago class-backup-method-mananger.php 2 months ago cli-handler.php 2 months ago compatibility.php 2 months ago config.php 2 months ago constants.php 2 months ago file-explorer.php 2 months ago initializer.php 2 months ago logger.php 2 months ago offline.php 2 months ago
config.php
324 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 = (object)[];
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 if (is_array($bmi_config_json)) {
101 $bmi_config_json[$setting] = $value;
102 } else if (is_object($bmi_config_json)) {
103 $bmi_config_json->{$setting} = $value;
104 } else return false;
105
106 } else return false;
107
108 // Write edited settings
109 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
110 file_put_contents(BMI_CONFIG_PATH, "<?php //" . json_encode($bmi_config_json));
111 } else {
112 file_put_contents(BMI_CONFIG_PATH, json_encode($bmi_config_json));
113 }
114
115 return true;
116
117 }
118
119 return false;
120
121 }
122 }
123
124 if (!function_exists('bmi_try_checked')) {
125 function bmi_try_checked($setting, $reversed = false) {
126
127 if (!$reversed) {
128
129 if (bmi_get_config($setting) == 'true' || bmi_get_config($setting) === true) {
130 echo ' checked';
131 } else return '';
132
133 } else {
134
135 if (bmi_get_config($setting) == 'true' || bmi_get_config($setting) === true) {
136 return '';
137 } else {
138 echo ' checked';
139 }
140
141 }
142
143 }
144 }
145
146 if (!function_exists('bmi_try_value')) {
147 function bmi_try_value($setting) {
148
149 $res = bmi_get_config($setting);
150 if ($res !== false) {
151 echo ' value="' . esc_attr( sanitize_text_field($res) ) . '"';
152 } else echo '';
153
154 }
155 }
156
157 function bmi_try_convert_old_to_new_config(&$bmi_initial_config_filepath, &$bmi_initial_config_dirpath, $init = true) {
158
159 $newConfigStaticPath = BMI_STATIC_PHP_CONFIG;
160 if (file_exists($newConfigStaticPath)) {
161 $configContents = file_get_contents(BMI_STATIC_PHP_CONFIG);
162 if (strpos($configContents, "<?php //[]") !== false || strlen(trim($configContents)) == 0) {
163 unlink(BMI_STATIC_PHP_CONFIG);
164 }
165 }
166
167 if (file_exists($newConfigStaticPath)) {
168
169 if (!defined('BMI_CONFIG_PHP')) define('BMI_CONFIG_PHP', true);
170 if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true);
171 if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $newConfigStaticPath);
172 if (!defined('BMI_INITIAL_CONFIG_PATH')) define('BMI_INITIAL_CONFIG_PATH', $bmi_initial_config_filepath);
173
174 $localStoragePath = bmi_get_config('STORAGE::LOCAL::PATH', $newConfigStaticPath);
175 if ($localStoragePath == "default") $localStoragePath = BMI_BACKUPS_DEFAULT;
176
177 if (!(is_readable($localStoragePath) && is_dir($localStoragePath))) {
178 $localStoragePath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration-' . bmi_config_random_string(10);
179 bmi_set_config('STORAGE::LOCAL::PATH', $localStoragePath);
180 }
181
182 if (basename($localStoragePath) == 'backup-migration') {
183 $current_patch = get_option('bmi_hotfixes', array());
184 $key = array_search('BMI_D20_M07_01', $current_patch);
185 if ($key !== false) unset($current_patch[$key]);
186 update_option('bmi_hotfixes', $current_patch);
187 }
188
189 $bmiLogFilesSuffix = bmi_get_config('STORAGE::LOCAL::LOGS::SUFFIX', $newConfigStaticPath);
190 if ($bmiLogFilesSuffix == false) {
191 $bmiLogFilesSuffix = bmi_config_random_string(10);
192 bmi_set_config('STORAGE::LOCAL::LOGS::SUFFIX', $bmiLogFilesSuffix);
193 }
194
195 if (!defined('BMI_BACKUPS_ROOT')) define('BMI_BACKUPS_ROOT', $localStoragePath);
196 if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', $localStoragePath);
197 if (!defined('BMI_BACKUPS')) define('BMI_BACKUPS', $localStoragePath . DIRECTORY_SEPARATOR . 'backups');
198 if (!defined('BMI_STAGING')) define('BMI_STAGING', $localStoragePath . DIRECTORY_SEPARATOR . 'staging');
199 if (!defined('BMI_TMP')) define('BMI_TMP', BMI_BACKUPS_ROOT . DIRECTORY_SEPARATOR . 'tmp');
200 if (!defined('BMI_LOGS_SUFFIX')) define('BMI_LOGS_SUFFIX', $bmiLogFilesSuffix);
201
202 $bmi_initial_config_dirpath = $localStoragePath;
203 $bmi_initial_config_filepath = $newConfigStaticPath;
204
205 return true;
206
207 } else {
208
209 if (file_exists($bmi_initial_config_filepath)) {
210 file_put_contents($newConfigStaticPath, "<?php //" . file_get_contents($bmi_initial_config_filepath));
211 @unlink($bmi_initial_config_filepath);
212 if ($init) bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath, false);
213 $bmi_initial_config_filepath = $newConfigStaticPath;
214 }
215
216 return false;
217
218 }
219
220 }
221
222 function bmi_config_random_string($max = 16) {
223
224 $bank = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
225 $bank .= 'abcdefghijklmnopqrstuvwxyz';
226 $bank .= '0123456789';
227
228 $str = str_shuffle($bank);
229
230 while (is_numeric($str[0])) {
231 $str = str_shuffle($bank);
232 }
233
234 $str = substr($str, 0, $max);
235
236 return $str;
237
238 }
239
240 function bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath) {
241
242 if (!file_exists(dirname($bmi_initial_config_filepath)) && !is_dir(dirname($bmi_initial_config_filepath))) {
243 @mkdir(dirname($bmi_initial_config_filepath), 0755, true);
244 }
245
246 @copy(BMI_CONFIG_DEFAULT, $bmi_initial_config_filepath);
247 bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
248
249 if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true);
250 if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $bmi_initial_config_filepath);
251 if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', dirname($bmi_initial_config_filepath));
252
253 }
254
255 $bmi_initial_config_filepath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration' . DIRECTORY_SEPARATOR . 'config.json';
256 $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath);
257 $bmi_database_config_dirpath = get_option('BMI::STORAGE::LOCAL::PATH', false);
258
259 if ($bmi_database_config_dirpath != false && dirname($bmi_database_config_dirpath) != $bmi_initial_config_dirpath) {
260 $bmi_initial_config_filepath = $bmi_database_config_dirpath . DIRECTORY_SEPARATOR . 'config.json';
261 $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath);
262 }
263
264 // Get config and parse it
265 if (file_exists(BMI_STATIC_PHP_CONFIG)) {
266
267 $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
268
269 } elseif (file_exists($bmi_initial_config_filepath)) {
270
271 // Convert config
272 $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
273
274 // Get file contents
275 $bmi_config_contents = file_get_contents($bmi_initial_config_filepath);
276 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
277 $bmi_config_contents = substr($bmi_config_contents, 8);
278 }
279
280 $bmi_config_json = json_decode($bmi_config_contents);
281
282 // If config is correct set it
283 if (json_last_error() == JSON_ERROR_NONE) {
284
285 $localStoragePath = BMI_BACKUPS_ROOT;
286 if ($bmi_database_config_dirpath == false || $bmi_database_config_dirpath != $localStoragePath) {
287 $prev_path = dirname(BMI_INITIAL_CONFIG_PATH);
288 $prev_path_backups = dirname(BMI_INITIAL_CONFIG_PATH) . DIRECTORY_SEPARATOR . 'backups';
289
290 if (file_exists($prev_path_backups) && is_dir($prev_path_backups)) {
291 $scanned_directory_backups = array_diff(scandir($prev_path_backups), ['..', '.']);
292 foreach ($scanned_directory_backups as $i => $file) {
293 if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) {
294 rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file);
295 }
296 }
297 if ($prev_path != $localStoragePath && sizeof(scandir($prev_path_backups)) <= 2) {
298 @rmdir($prev_path_backups);
299 }
300 }
301
302 if (file_exists($prev_path) && is_dir($prev_path)) {
303 $scanned_directory = array_diff(scandir($prev_path), ['..', '.']);
304 foreach ($scanned_directory as $i => $file) {
305 if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) {
306 rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file);
307 }
308 }
309 if ($localStoragePath != $prev_path && sizeof(scandir($prev_path)) <= 2) {
310 @rmdir($prev_path);
311 }
312 }
313
314 update_option('BMI::STORAGE::LOCAL::PATH', $localStoragePath);
315 }
316
317 } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
318
319 } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
320
321 if (!bmi_get_config('REQUEST:SECRET')) {
322 bmi_set_config('REQUEST:SECRET', bmi_config_random_string(16));
323 }
324