banner
11 months ago
bodies
11 months ago
check
11 months ago
cli
11 months ago
cron
11 months ago
dashboard
11 months ago
database
11 months ago
external
11 months ago
extracter
11 months ago
htaccess
11 months ago
notices
11 months ago
progress
11 months ago
scanner
11 months ago
staging
11 months ago
traits
11 months ago
uploader
11 months ago
zipper
11 months ago
.htaccess
11 months ago
activation.php
11 months ago
ajax.php
11 months ago
ajax_offline.php
11 months ago
analyst.php
11 months ago
backup-process.php
11 months ago
class-backup-method-mananger.php
11 months ago
cli-handler.php
11 months ago
compatibility.php
11 months ago
config.php
11 months ago
constants.php
11 months ago
initializer.php
11 months ago
logger.php
11 months ago
offline.php
11 months ago
config.php
317 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 false; |
| 132 | |
| 133 | } else { |
| 134 | |
| 135 | if (bmi_get_config($setting) == 'true' || bmi_get_config($setting) === true) { |
| 136 | return false; |
| 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="' . 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 | if (!defined('BMI_BACKUPS_ROOT')) define('BMI_BACKUPS_ROOT', $localStoragePath); |
| 190 | if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', $localStoragePath); |
| 191 | if (!defined('BMI_BACKUPS')) define('BMI_BACKUPS', $localStoragePath . DIRECTORY_SEPARATOR . 'backups'); |
| 192 | if (!defined('BMI_STAGING')) define('BMI_STAGING', $localStoragePath . DIRECTORY_SEPARATOR . 'staging'); |
| 193 | if (!defined('BMI_TMP')) define('BMI_TMP', BMI_BACKUPS_ROOT . DIRECTORY_SEPARATOR . 'tmp'); |
| 194 | |
| 195 | $bmi_initial_config_dirpath = $localStoragePath; |
| 196 | $bmi_initial_config_filepath = $newConfigStaticPath; |
| 197 | |
| 198 | return true; |
| 199 | |
| 200 | } else { |
| 201 | |
| 202 | if (file_exists($bmi_initial_config_filepath)) { |
| 203 | file_put_contents($newConfigStaticPath, "<?php //" . file_get_contents($bmi_initial_config_filepath)); |
| 204 | @unlink($bmi_initial_config_filepath); |
| 205 | if ($init) bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath, false); |
| 206 | $bmi_initial_config_filepath = $newConfigStaticPath; |
| 207 | } |
| 208 | |
| 209 | return false; |
| 210 | |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | function bmi_config_random_string($max = 16) { |
| 216 | |
| 217 | $bank = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 218 | $bank .= 'abcdefghijklmnopqrstuvwxyz'; |
| 219 | $bank .= '0123456789'; |
| 220 | |
| 221 | $str = str_shuffle($bank); |
| 222 | |
| 223 | while (is_numeric($str[0])) { |
| 224 | $str = str_shuffle($bank); |
| 225 | } |
| 226 | |
| 227 | $str = substr($str, 0, $max); |
| 228 | |
| 229 | return $str; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | function bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath) { |
| 234 | |
| 235 | if (!file_exists(dirname($bmi_initial_config_filepath)) && !is_dir(dirname($bmi_initial_config_filepath))) { |
| 236 | @mkdir(dirname($bmi_initial_config_filepath), 0755, true); |
| 237 | } |
| 238 | |
| 239 | @copy(BMI_CONFIG_DEFAULT, $bmi_initial_config_filepath); |
| 240 | bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath); |
| 241 | |
| 242 | if (!defined('BMI_CONFIG_STATUS')) define('BMI_CONFIG_STATUS', true); |
| 243 | if (!defined('BMI_CONFIG_PATH')) define('BMI_CONFIG_PATH', $bmi_initial_config_filepath); |
| 244 | if (!defined('BMI_CONFIG_DIR')) define('BMI_CONFIG_DIR', dirname($bmi_initial_config_filepath)); |
| 245 | |
| 246 | } |
| 247 | |
| 248 | $bmi_initial_config_filepath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration' . DIRECTORY_SEPARATOR . 'config.json'; |
| 249 | $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath); |
| 250 | $bmi_database_config_dirpath = get_option('BMI::STORAGE::LOCAL::PATH', false); |
| 251 | |
| 252 | if ($bmi_database_config_dirpath != false && dirname($bmi_database_config_dirpath) != $bmi_initial_config_dirpath) { |
| 253 | $bmi_initial_config_filepath = $bmi_database_config_dirpath . DIRECTORY_SEPARATOR . 'config.json'; |
| 254 | $bmi_initial_config_dirpath = dirname($bmi_initial_config_filepath); |
| 255 | } |
| 256 | |
| 257 | // Get config and parse it |
| 258 | if (file_exists(BMI_STATIC_PHP_CONFIG)) { |
| 259 | |
| 260 | $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath); |
| 261 | |
| 262 | } elseif (file_exists($bmi_initial_config_filepath)) { |
| 263 | |
| 264 | // Convert config |
| 265 | $bmi_php_config = bmi_try_convert_old_to_new_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath); |
| 266 | |
| 267 | // Get file contents |
| 268 | $bmi_config_contents = file_get_contents($bmi_initial_config_filepath); |
| 269 | if (defined('BMI_CONFIG_PHP') && BMI_CONFIG_PHP) { |
| 270 | $bmi_config_contents = substr($bmi_config_contents, 8); |
| 271 | } |
| 272 | |
| 273 | $bmi_config_json = json_decode($bmi_config_contents); |
| 274 | |
| 275 | // If config is correct set it |
| 276 | if (json_last_error() == JSON_ERROR_NONE) { |
| 277 | |
| 278 | $localStoragePath = BMI_BACKUPS_ROOT; |
| 279 | if ($bmi_database_config_dirpath == false || $bmi_database_config_dirpath != $localStoragePath) { |
| 280 | $prev_path = dirname(BMI_INITIAL_CONFIG_PATH); |
| 281 | $prev_path_backups = dirname(BMI_INITIAL_CONFIG_PATH) . DIRECTORY_SEPARATOR . 'backups'; |
| 282 | |
| 283 | if (file_exists($prev_path_backups) && is_dir($prev_path_backups)) { |
| 284 | $scanned_directory_backups = array_diff(scandir($prev_path_backups), ['..', '.']); |
| 285 | foreach ($scanned_directory_backups as $i => $file) { |
| 286 | if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) { |
| 287 | rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file); |
| 288 | } |
| 289 | } |
| 290 | if ($prev_path != $localStoragePath && sizeof(scandir($prev_path_backups)) <= 2) { |
| 291 | @rmdir($prev_path_backups); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (file_exists($prev_path) && is_dir($prev_path)) { |
| 296 | $scanned_directory = array_diff(scandir($prev_path), ['..', '.']); |
| 297 | foreach ($scanned_directory as $i => $file) { |
| 298 | if (file_exists($prev_path . DIRECTORY_SEPARATOR . $file) && !is_dir($prev_path . DIRECTORY_SEPARATOR . $file)) { |
| 299 | rename($prev_path . DIRECTORY_SEPARATOR . $file, $localStoragePath . DIRECTORY_SEPARATOR . $file); |
| 300 | } |
| 301 | } |
| 302 | if ($localStoragePath != $prev_path && sizeof(scandir($prev_path)) <= 2) { |
| 303 | @rmdir($prev_path); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | update_option('BMI::STORAGE::LOCAL::PATH', $localStoragePath); |
| 308 | } |
| 309 | |
| 310 | } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath); |
| 311 | |
| 312 | } else bmi_render_default_config($bmi_initial_config_filepath, $bmi_initial_config_dirpath); |
| 313 | |
| 314 | if (!bmi_get_config('REQUEST:SECRET')) { |
| 315 | bmi_set_config('REQUEST:SECRET', bmi_config_random_string(16)); |
| 316 | } |
| 317 |