banner
3 months ago
bodies
3 months ago
check
3 months ago
cli
3 months ago
cron
3 months ago
dashboard
3 months ago
database
3 months ago
external
3 months ago
extracter
3 months ago
htaccess
3 months ago
notices
3 months ago
progress
3 months ago
scanner
3 months ago
staging
3 months ago
traits
3 months ago
uploader
3 months ago
vendor
3 months ago
zipper
3 months ago
.htaccess
3 months ago
activation.php
3 months ago
ajax.php
3 months ago
ajax_offline.php
3 months ago
analyst.php
3 months ago
backup-process.php
3 months ago
class-backup-method-mananger.php
3 months ago
cli-handler.php
3 months ago
compatibility.php
3 months ago
config.php
3 months ago
constants.php
3 months ago
file-explorer.php
3 months ago
initializer.php
3 months ago
logger.php
3 months ago
offline.php
3 months ago
class-backup-method-mananger.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace BMI\Plugin; |
| 5 | use BMI\Plugin\Traits\LoggerTrait; |
| 6 | use BMI\Plugin\Dashboard as Dashboard; |
| 7 | use BMI\Plugin\Checker\System_Info as SI; |
| 8 | |
| 9 | |
| 10 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'traits' . DIRECTORY_SEPARATOR . 'logger-trait.php'; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Class BMI_BackupMethodManager |
| 17 | * |
| 18 | * This class is responsible for managing the backup method used by the plugin. |
| 19 | * It able to change the backup method during runtime or globally by changing the plugin settings. |
| 20 | * |
| 21 | * @package BMI\Plugin |
| 22 | * @since 1.4.6 |
| 23 | * |
| 24 | */ |
| 25 | class BMI_BackupMethodManager { |
| 26 | |
| 27 | use LoggerTrait; |
| 28 | |
| 29 | private $method; |
| 30 | private $error; |
| 31 | private $oldMethod; |
| 32 | |
| 33 | /** |
| 34 | * BMI_BackupMethodManager constructor. |
| 35 | * |
| 36 | * It determines the current backup method used by the plugin. |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | $this->method = $this->determineBackupMethod(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Determine the current backup method used by the plugin. |
| 44 | * |
| 45 | * @return string|null The current backup method used by the plugin. |
| 46 | */ |
| 47 | private function determineBackupMethod() { |
| 48 | if (Dashboard\bmi_get_config('OTHER:EXPERIMENT:TIMEOUT')) { |
| 49 | return BMI_METHOD_CURL; |
| 50 | } elseif (Dashboard\bmi_get_config('OTHER:USE:TIMEOUT:NORMAL')) { |
| 51 | return BMI_METHOD_DEFAULT; |
| 52 | } elseif (Dashboard\bmi_get_config('OTHER:EXPERIMENT:TIMEOUT:HARD')) { |
| 53 | return BMI_METHOD_BROWSER; |
| 54 | } |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Change the backup method used by the plugin. |
| 60 | * |
| 61 | * @param string $method The new backup method to use. |
| 62 | * @param bool $global Whether to change the backup method globally or not. (Will force change the method globally if method is cURL or browser-method) |
| 63 | * @return bool True if the backup method was changed successfully, false otherwise. |
| 64 | */ |
| 65 | public function changeBackupMethod($method, $global = true) { |
| 66 | if (!in_array($method, [BMI_METHOD_CURL, BMI_METHOD_DEFAULT, BMI_METHOD_BROWSER])) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if ($this->method === $method) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | if (false === $this->isMethodChangable($method)) { |
| 75 | // $this->log("Changing backup to {$method} method is not possible"); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if ($method === BMI_METHOD_BROWSER || $method === BMI_METHOD_CURL || $global) { |
| 80 | // $this->log("Changing backup to {$method} method with global settings"); |
| 81 | if (!$this->applyMethodSettings($method, true)) { |
| 82 | // $this->log("Changing backup to {$method} method failed"); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if ($this->applyMethodSettings($method)) { |
| 88 | $this->log("Backup method changed to {$method}"); |
| 89 | $this->oldMethod = $this->method; |
| 90 | $this->method = $method; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check if the backup method can be changed to the specified method. |
| 99 | * |
| 100 | * @param string $method The backup method to check. |
| 101 | * @return bool |
| 102 | */ |
| 103 | public function isMethodChangable($method) { |
| 104 | switch ($method) { |
| 105 | case BMI_METHOD_CURL: |
| 106 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'check' . DIRECTORY_SEPARATOR . 'system_info.php'; |
| 107 | return SI::is_curl_work(); |
| 108 | case BMI_METHOD_DEFAULT: |
| 109 | case BMI_METHOD_BROWSER: |
| 110 | return true; |
| 111 | default: |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Apply the settings for the specified backup method. |
| 118 | * |
| 119 | * @param string $method The backup method to apply settings for. |
| 120 | * @param bool $global Whether to apply the settings globally or not. |
| 121 | * @return bool True if the settings were applied successfully, false otherwise. |
| 122 | */ |
| 123 | private function applyMethodSettings($method, $global = false) { |
| 124 | if ($global) { |
| 125 | $settings = [ |
| 126 | BMI_METHOD_CURL => ['OTHER:EXPERIMENT:TIMEOUT' => true, 'OTHER:USE:TIMEOUT:NORMAL' => false, 'OTHER:EXPERIMENT:TIMEOUT:HARD' => false], |
| 127 | BMI_METHOD_DEFAULT => ['OTHER:EXPERIMENT:TIMEOUT' => false, 'OTHER:USE:TIMEOUT:NORMAL' => true, 'OTHER:EXPERIMENT:TIMEOUT:HARD' => false], |
| 128 | BMI_METHOD_BROWSER => ['OTHER:EXPERIMENT:TIMEOUT' => false, 'OTHER:USE:TIMEOUT:NORMAL' => false, 'OTHER:EXPERIMENT:TIMEOUT:HARD' => true], |
| 129 | ]; |
| 130 | |
| 131 | } else { |
| 132 | $settings = [ |
| 133 | BMI_METHOD_CURL => ['bmi_legacy_version' => false, 'bmi_function_normal' => false, 'bmi_legacy_hard_version' => true], |
| 134 | BMI_METHOD_DEFAULT => ['bmi_legacy_version' => true, 'bmi_function_normal' => true, 'bmi_legacy_hard_version' => true], |
| 135 | BMI_METHOD_BROWSER => ['bmi_legacy_version' => true, 'bmi_function_normal' => false, 'bmi_legacy_hard_version' => false], |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | return $this->configureTimeouts($settings[$method], $global); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Configure the settings for the specified backup method. |
| 144 | * |
| 145 | * @param array $settings The settings to configure. |
| 146 | * @param bool $global Whether to configure the settings globally or not. |
| 147 | * @return bool |
| 148 | */ |
| 149 | private function configureTimeouts($settings, $global) { |
| 150 | $this->configureConstantFilters($settings); |
| 151 | return $global ? $this->configurePluginSettings($settings) : true; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Configure the plugin settings. |
| 156 | * |
| 157 | * @param array $settings The settings to configure. |
| 158 | * @return bool |
| 159 | */ |
| 160 | private function configurePluginSettings($settings) { |
| 161 | $result = true; |
| 162 | foreach ($settings as $key => $value) { |
| 163 | if (!Dashboard\bmi_set_config($key, $value)) { |
| 164 | $result = false; |
| 165 | } |
| 166 | } |
| 167 | return $result; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Configure the constant filters. |
| 172 | * |
| 173 | * @param array $settings The settings to configure. |
| 174 | * @return bool always return true. |
| 175 | */ |
| 176 | private function configureConstantFilters($settings) { |
| 177 | foreach ($settings as $filterName => $returnValue) { |
| 178 | add_filter($filterName, '__return_' . ($returnValue ? 'true' : 'false')); |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get the current backup method used by the plugin. |
| 185 | * |
| 186 | * @return string The current backup method used by the plugin. |
| 187 | */ |
| 188 | public function currentMethod() { |
| 189 | return $this->method; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the old backup method used by the plugin. |
| 194 | * |
| 195 | * @return string|null The old backup method used by the plugin. |
| 196 | */ |
| 197 | public function oldMethod() { |
| 198 | return $this->oldMethod; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Temporary disable cli during current request. |
| 203 | * |
| 204 | * @param bool $global Whether to disable the cli globally or not. |
| 205 | * @return bool |
| 206 | */ |
| 207 | public function disableCLI($global = true) { |
| 208 | add_filter('bmi_cli_enabled', '__return_false'); |
| 209 | if ($global) { |
| 210 | return Dashboard\bmi_set_config('OTHER:CLI:DISABLE', true); |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | } |