banner
2 weeks ago
bodies
2 weeks ago
check
2 weeks ago
cli
2 weeks ago
cron
2 weeks ago
dashboard
2 weeks ago
database
2 weeks ago
external
2 weeks ago
extracter
2 weeks ago
htaccess
2 weeks ago
notices
2 weeks ago
progress
2 weeks ago
scanner
2 weeks ago
services
2 weeks ago
staging
2 weeks ago
traits
2 weeks ago
uploader
2 weeks ago
vendor
2 weeks ago
zipper
2 weeks ago
.htaccess
2 weeks ago
activation.php
2 weeks ago
ajax.php
2 weeks ago
ajax_offline.php
2 weeks ago
analyst.php
2 weeks ago
backup-process.php
2 weeks ago
class-backup-method-mananger.php
2 weeks ago
cli-handler.php
2 weeks ago
compatibility.php
2 weeks ago
config.php
2 weeks ago
config_v2.php
2 weeks ago
constants.php
2 weeks ago
file-explorer.php
2 weeks ago
initializer.php
2 weeks ago
logger.php
2 weeks ago
offline.php
2 weeks ago
config_v2.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | namespace BMI\Plugin\Dashboard; |
| 4 | |
| 5 | // Exit on direct access |
| 6 | if (!defined('ABSPATH')) exit; |
| 7 | |
| 8 | class BMI_Config_V2 { |
| 9 | |
| 10 | private static $instance = null; |
| 11 | private $settings = null; |
| 12 | const OPTION_NAME = 'bmi_settings'; |
| 13 | |
| 14 | private function __construct() { |
| 15 | $this->load_settings(); |
| 16 | } |
| 17 | |
| 18 | public static function get_instance() { |
| 19 | if (self::$instance == null) { |
| 20 | self::$instance = new self(); |
| 21 | } |
| 22 | return self::$instance; |
| 23 | } |
| 24 | |
| 25 | private function load_settings() { |
| 26 | $db_settings = get_option(self::OPTION_NAME, false); |
| 27 | |
| 28 | if ($db_settings === false || empty($db_settings)) { |
| 29 | if (!$this->migrate_old_config()) { |
| 30 | $this->initialize_defaults(); |
| 31 | } |
| 32 | } else { |
| 33 | $this->settings = is_string($db_settings) ? json_decode($db_settings, true) : $db_settings; |
| 34 | if (!is_array($this->settings)) { |
| 35 | $this->settings = (array) $this->settings; |
| 36 | } |
| 37 | if (isset($this->settings['STORAGE::LOCAL::PATH'])) { |
| 38 | $this->settings['STORAGE::LOCAL::PATH'] = $this->normalizePathSeparators($this->settings['STORAGE::LOCAL::PATH']); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private function migrate_old_config() { |
| 44 | $config_to_migrate = []; |
| 45 | $old_config_path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration-config.php'; |
| 46 | |
| 47 | // Try modern PHP config file first |
| 48 | if (file_exists($old_config_path)) { |
| 49 | $bmi_config_contents = file_get_contents($old_config_path); |
| 50 | if (strpos($bmi_config_contents, '<?php //') !== false) { |
| 51 | $bmi_config_contents = substr($bmi_config_contents, 8); |
| 52 | } |
| 53 | $config_to_migrate = json_decode($bmi_config_contents, true); |
| 54 | @unlink($old_config_path); |
| 55 | } |
| 56 | |
| 57 | if (is_array($config_to_migrate) && !empty($config_to_migrate)) { |
| 58 | $this->settings = $config_to_migrate; |
| 59 | $this->save_settings(); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | public function initialize_defaults() { |
| 67 | // Load default and additional |
| 68 | if (defined('BMI_CONFIG_DEFAULT') && file_exists(BMI_CONFIG_DEFAULT)) { |
| 69 | $defaults = json_decode(file_get_contents(BMI_CONFIG_DEFAULT), true); |
| 70 | } else { |
| 71 | $defaults = []; |
| 72 | } |
| 73 | |
| 74 | if (!is_array($defaults)) { |
| 75 | $defaults = []; |
| 76 | } |
| 77 | |
| 78 | $this->settings = $defaults; |
| 79 | |
| 80 | $localStoragePath = $this->get('STORAGE::LOCAL::PATH'); |
| 81 | if ($localStoragePath == 'default') { |
| 82 | $localStoragePath = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'backup-migration-' . bmi_config_random_string(10); |
| 83 | $this->set('STORAGE::LOCAL::PATH', $localStoragePath); |
| 84 | } |
| 85 | |
| 86 | $bmiLogFilesSuffix = $this->get('STORAGE::LOCAL::LOGS::SUFFIX'); |
| 87 | if (empty($bmiLogFilesSuffix)) { |
| 88 | $bmiLogFilesSuffix = bmi_config_random_string(10); |
| 89 | $this->set('STORAGE::LOCAL::LOGS::SUFFIX', $bmiLogFilesSuffix); |
| 90 | } |
| 91 | |
| 92 | if (!$this->get('REQUEST:SECRET')) { |
| 93 | $this->set('REQUEST:SECRET', bmi_config_random_string(16)); |
| 94 | } |
| 95 | |
| 96 | $this->save_settings(); |
| 97 | } |
| 98 | |
| 99 | public function get($setting) { |
| 100 | if (isset($this->settings[$setting])) { |
| 101 | return $this->settings[$setting]; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | public function normalizePathSeparators($path) { |
| 107 | return str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); |
| 108 | } |
| 109 | |
| 110 | public function set($setting, $value) { |
| 111 | // Allow empty |
| 112 | $allow_empty = ['OTHER:CLI:PATH']; |
| 113 | |
| 114 | // Check if setting is not empty |
| 115 | if (isset($value) && (!is_string($value) || (in_array($setting, $allow_empty) || strlen(trim($value)) > 0))) { |
| 116 | |
| 117 | // If the value is the same, no need to update DB, just return true |
| 118 | if (isset($this->settings[$setting]) && $this->settings[$setting] === $value) { |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | $this->settings[$setting] = $value; |
| 123 | $this->save_settings(); |
| 124 | return true; |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | public function get_all() { |
| 130 | return $this->settings; |
| 131 | } |
| 132 | |
| 133 | private function save_settings() { |
| 134 | update_option(self::OPTION_NAME, $this->settings); |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 |