PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.4.6
Backup Migration v1.4.6
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 1 year ago check 1 year ago cli 1 year ago cron 1 year ago dashboard 1 year ago database 1 year ago extracter 1 year ago htaccess 1 year ago progress 1 year ago scanner 1 year ago staging 1 year ago uploader 1 year ago zipper 1 year ago .htaccess 1 year ago activation.php 1 year ago ajax.php 1 year ago analyst.php 1 year ago backup-process.php 1 year ago cli-handler.php 1 year ago compatibility.php 1 year ago config.php 1 year ago constants.php 1 year ago initializer.php 1 year ago logger.php 1 year ago
config.php
313 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 $configContents = file_get_contents(BMI_STATIC_PHP_CONFIG);
158 if (strpos($configContents, "<?php //[]") !== false || strlen(trim($configContents)) == 0) {
159 unlink(BMI_STATIC_PHP_CONFIG);
160 }
161 }
162
163 if (file_exists($newConfigStaticPath)) {
164
165 if (!defined('BMI_CONFIG_PHP')) define('BMI_CONFIG_PHP', true);
166 if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true);
167 if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $newConfigStaticPath);
168 if (!defined('BMI_INITIAL_CONFIG_PATH')) define('BMI_INITIAL_CONFIG_PATH', $bmi_initial_config_filepath);
169
170 $localStoragePath = bmi_get_config('STORAGE::LOCAL::PATH', $newConfigStaticPath);
171 if ($localStoragePath == "default") $localStoragePath = BMI_BACKUPS_DEFAULT;
172
173 if (!(is_readable($localStoragePath) && is_dir($localStoragePath))) {
174 $localStoragePath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration-' . bmi_config_random_string(10);
175 bmi_set_config('STORAGE::LOCAL::PATH', $localStoragePath);
176 }
177
178 if (basename($localStoragePath) == 'backup-migration') {
179 $current_patch = get_option('bmi_hotfixes', array());
180 $key = array_search('BMI_D20_M07_01', $current_patch);
181 if ($key !== false) unset($current_patch[$key]);
182 update_option('bmi_hotfixes', $current_patch);
183 }
184
185 if (!defined('BMI_BACKUPS_ROOT')) define('BMI_BACKUPS_ROOT', $localStoragePath);
186 if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', $localStoragePath);
187 if (!defined('BMI_BACKUPS')) define('BMI_BACKUPS', $localStoragePath . DIRECTORY_SEPARATOR . 'backups');
188 if (!defined('BMI_STAGING')) define('BMI_STAGING', $localStoragePath . DIRECTORY_SEPARATOR . 'staging');
189 if (!defined('BMI_TMP')) define('BMI_TMP', BMI_BACKUPS_ROOT . DIRECTORY_SEPARATOR . 'tmp');
190
191 $bmi_initial_config_dirpath = $localStoragePath;
192 $bmi_initial_config_filepath = $newConfigStaticPath;
193
194 return true;
195
196 } else {
197
198 if (file_exists($bmi_initial_config_filepath)) {
199 file_put_contents($newConfigStaticPath, "<?php //" . file_get_contents($bmi_initial_config_filepath));
200 @unlink($bmi_initial_config_filepath);
201 if ($init) bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath, false);
202 $bmi_initial_config_filepath = $newConfigStaticPath;
203 }
204
205 return false;
206
207 }
208
209 }
210
211 function bmi_config_random_string($max = 16) {
212
213 $bank = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
214 $bank .= 'abcdefghijklmnopqrstuvwxyz';
215 $bank .= '0123456789';
216
217 $str = str_shuffle($bank);
218
219 while (is_numeric($str[0])) {
220 $str = str_shuffle($bank);
221 }
222
223 $str = substr($str, 0, $max);
224
225 return $str;
226
227 }
228
229 function bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath) {
230
231 if (!file_exists(dirname($bmi_initial_config_filepath)) && !is_dir(dirname($bmi_initial_config_filepath))) {
232 @mkdir(dirname($bmi_initial_config_filepath), 0755, true);
233 }
234
235 @copy(BMI_CONFIG_DEFAULT, $bmi_initial_config_filepath);
236 bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
237
238 if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true);
239 if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $bmi_initial_config_filepath);
240 if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', dirname($bmi_initial_config_filepath));
241
242 }
243
244 $bmi_initial_config_filepath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration' . DIRECTORY_SEPARATOR . 'config.json';
245 $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath);
246 $bmi_database_config_dirpath = get_option('BMI::STORAGE::LOCAL::PATH', false);
247
248 if ($bmi_database_config_dirpath != false && dirname($bmi_database_config_dirpath) != $bmi_initial_config_dirpath) {
249 $bmi_initial_config_filepath = $bmi_database_config_dirpath . DIRECTORY_SEPARATOR . 'config.json';
250 $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath);
251 }
252
253 // Get config and parse it
254 if (file_exists(BMI_STATIC_PHP_CONFIG)) {
255
256 $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
257
258 } elseif (file_exists($bmi_initial_config_filepath)) {
259
260 // Convert config
261 $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
262
263 // Get file contents
264 $bmi_config_contents = file_get_contents($bmi_initial_config_filepath);
265 if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) {
266 $bmi_config_contents = substr($bmi_config_contents, 8);
267 }
268
269 $bmi_config_json = json_decode($bmi_config_contents);
270
271 // If config is correct set it
272 if (json_last_error() == JSON_ERROR_NONE) {
273
274 $localStoragePath = BMI_BACKUPS_ROOT;
275 if ($bmi_database_config_dirpath == false || $bmi_database_config_dirpath != $localStoragePath) {
276 $prev_path = dirname(BMI_INITIAL_CONFIG_PATH);
277 $prev_path_backups = dirname(BMI_INITIAL_CONFIG_PATH) . DIRECTORY_SEPARATOR . 'backups';
278
279 if (file_exists($prev_path_backups) && is_dir($prev_path_backups)) {
280 $scanned_directory_backups = array_diff(scandir($prev_path_backups), ['..', '.']);
281 foreach ($scanned_directory_backups as $i => $file) {
282 if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) {
283 rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file);
284 }
285 }
286 if ($prev_path != $localStoragePath && sizeof(scandir($prev_path_backups)) <= 2) {
287 @rmdir($prev_path_backups);
288 }
289 }
290
291 if (file_exists($prev_path) && is_dir($prev_path)) {
292 $scanned_directory = array_diff(scandir($prev_path), ['..', '.']);
293 foreach ($scanned_directory as $i => $file) {
294 if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) {
295 rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file);
296 }
297 }
298 if ($localStoragePath != $prev_path && sizeof(scandir($prev_path)) <= 2) {
299 @rmdir($prev_path);
300 }
301 }
302
303 update_option('BMI::STORAGE::LOCAL::PATH', $localStoragePath);
304 }
305
306 } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
307
308 } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath);
309
310 if (!bmi_get_config('REQUEST:SECRET')) {
311 bmi_set_config('REQUEST:SECRET', bmi_config_random_string(16));
312 }
313