.htaccess
9 months ago
Config.php
9 months ago
Locations.php
9 months ago
System.php
9 months ago
index.html
9 months ago
web.config
9 months ago
Config.php
251 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Config; |
| 4 | |
| 5 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 6 | |
| 7 | use JetBackup\Alert\Alert; |
| 8 | use JetBackup\Data\ReflectionObject; |
| 9 | use JetBackup\Encryption\Crypt; |
| 10 | use JetBackup\Entities\Util; |
| 11 | use JetBackup\Exception\FieldsValidationException; |
| 12 | use JetBackup\Exception\IOException; |
| 13 | use JetBackup\Exception\LicenseException; |
| 14 | use JetBackup\Factory; |
| 15 | use JetBackup\JetBackup; |
| 16 | use JetBackup\License\License; |
| 17 | use JetBackup\License\LicenseLocalKey; |
| 18 | use JetBackup\Wordpress\Helper; |
| 19 | use JetBackup\Wordpress\Wordpress; |
| 20 | |
| 21 | class Config extends ReflectionObject { |
| 22 | |
| 23 | const CONFIG_FILE = JetBackup::CONFIG_PATH . JetBackup::SEP . 'config.php'; |
| 24 | const MAIN_DATA_FOLDER = 'MAIN_DATA_FOLDER'; |
| 25 | const DB_MAIN_FOLDER = 'DB_MAIN_FOLDER'; |
| 26 | const BACKUPS_TEMP_FOLDER = 'BACKUPS_TEMP_FOLDER'; |
| 27 | const CRON_TOKEN = 'CRON_TOKEN'; |
| 28 | const LOGS_FOLDER = 'LOGS_FOLDER'; |
| 29 | const BACKUPS_FOLDER = 'BACKUPS_FOLDER'; |
| 30 | const DOWNLOADS_FOLDER = 'DOWNLOADS_FOLDER'; |
| 31 | const SYSTEM_CRON_DAILY_LAST_RUN = 'SYSTEM_CRON_DAILY_LAST_RUN'; |
| 32 | const SYSTEM_CRON_HOURLY_LAST_RUN = 'SYSTEM_CRON_HOURLY_LAST_RUN'; |
| 33 | const ENCRYPTION_KEY = 'ENCRYPTION_KEY'; |
| 34 | const UNIQUE_ID = 'UNIQUE_ID'; |
| 35 | const SGB_LEGACY_CONVERTED = 'SGB_LEGACY_CONVERTED'; |
| 36 | const CURRENT_VERSION = 'CURRENT_VERSION'; |
| 37 | const ALTERNATE_DATA_FOLDER = 'ALTERNATE_DATA_FOLDER'; |
| 38 | const LICENSE_KEY = 'LICENSE_KEY'; |
| 39 | const LICENSE_LOCAL_KEY = 'LICENSE_LOCAL_KEY'; |
| 40 | const LICENSE_NEXT_CHECK = 'LICENSE_NEXT_CHECK'; |
| 41 | const LICENSE_LAST_CHECK = 'LICENSE_LAST_CHECK'; |
| 42 | const LICENSE_NOTIFY_DATE = 'LICENSE_NOTIFY_DATE'; |
| 43 | const SUPPORT_USERNAME = 'SUPPORT_USERNAME'; |
| 44 | |
| 45 | public function __construct() { |
| 46 | parent::__construct(self::CONFIG_FILE, 'JetConfigDefines'); |
| 47 | |
| 48 | if(!$this->getDataDirectory()) $this->setDataDirectory( 'jetbackup-' . Util::generateRandomString()); |
| 49 | if(!$this->getBackupsDirectory()) $this->setBackupsDirectory( 'backups-' . Util::generateRandomString()); |
| 50 | if(!$this->getTempDirectory()) $this->setTempDirectory( 'temp-' . Util::generateRandomString()); |
| 51 | if(!$this->getDatabaseDirectory()) $this->setDatabaseDirectory( 'db-' . Util::generateRandomString()); |
| 52 | if(!$this->getLogsDirectory()) $this->setLogsDirectory( 'logs-' . Util::generateRandomString()); |
| 53 | if(!$this->getDownloadsDirectory()) $this->setDownloadsDirectory( 'downloads-' . Util::generateRandomString()); |
| 54 | if(!$this->getEncryptionKey()) $this->setEncryptionKey(Util::generateRandomString()); |
| 55 | if(!$this->getUniqueID()) $this->setUniqueID(Util::generateRandomString()); |
| 56 | if(!$this->getCronToken()) $this->setCronToken(Util::generateRandomString()); |
| 57 | |
| 58 | if($this->getDiff()) $this->save(); |
| 59 | } |
| 60 | |
| 61 | public function set($key, $value) { |
| 62 | if(is_string($value)) $value = htmlspecialchars($value); |
| 63 | parent::set($key, $value); |
| 64 | } |
| 65 | |
| 66 | public function getDataDirectory():string { return $this->get(self::MAIN_DATA_FOLDER); } |
| 67 | public function setDataDirectory($value):void { $this->set(self::MAIN_DATA_FOLDER, $value); } |
| 68 | public function getDatabaseDirectory():string { return $this->get(self::DB_MAIN_FOLDER); } |
| 69 | public function setDatabaseDirectory($value):void { $this->set(self::DB_MAIN_FOLDER, $value); } |
| 70 | public function getLogsDirectory():string { return $this->get(self::LOGS_FOLDER); } |
| 71 | public function setLogsDirectory($value):void { $this->set(self::LOGS_FOLDER, $value); } |
| 72 | public function getBackupsDirectory():string { return $this->get(self::BACKUPS_FOLDER); } |
| 73 | public function setBackupsDirectory($value):void { $this->set(self::BACKUPS_FOLDER, $value); } |
| 74 | public function getDownloadsDirectory():string { return $this->get(self::DOWNLOADS_FOLDER); } |
| 75 | public function setDownloadsDirectory($value):void { $this->set(self::DOWNLOADS_FOLDER, $value); } |
| 76 | public function getTempDirectory():string { return $this->get(self::BACKUPS_TEMP_FOLDER); } |
| 77 | public function setTempDirectory($value):void { $this->set(self::BACKUPS_TEMP_FOLDER, $value); } |
| 78 | public function getSystemCronDailyLastRun():int { return (int) $this->get(self::SYSTEM_CRON_DAILY_LAST_RUN, 0); } |
| 79 | public function setSystemCronDailyLastRun(?int $value=null):void { $this->set(self::SYSTEM_CRON_DAILY_LAST_RUN, $value ?? time()); } |
| 80 | public function getSystemCronHourlyLastRun():int { return (int) $this->get(self::SYSTEM_CRON_HOURLY_LAST_RUN, 0); } |
| 81 | public function setSystemCronHourlyLastRun(?int $value=null):void { $this->set(self::SYSTEM_CRON_HOURLY_LAST_RUN, $value ?? time()); } |
| 82 | public function getEncryptionKey():string { return $this->get(self::ENCRYPTION_KEY); } |
| 83 | public function setEncryptionKey($value):void { $this->set(self::ENCRYPTION_KEY, $value); } |
| 84 | public function getUniqueID():string { return $this->get(self::UNIQUE_ID); } |
| 85 | public function setUniqueID($value):void { $this->set(self::UNIQUE_ID, $value); } |
| 86 | public function getCronToken():string { return $this->get(self::CRON_TOKEN); } |
| 87 | public function setCronToken($value):void { $this->set(self::CRON_TOKEN, $value); } |
| 88 | public function isSGBLegacyConverted():bool { return !!$this->get(self::SGB_LEGACY_CONVERTED, false); } |
| 89 | public function setSGBLegacyConverted(bool $value):void { $this->set(self::SGB_LEGACY_CONVERTED, !!$value); } |
| 90 | public function getCurrentVersion():string { return $this->get(self::CURRENT_VERSION); } |
| 91 | public function setCurrentVersion($value):void { $this->set(self::CURRENT_VERSION, $value); } |
| 92 | |
| 93 | /** |
| 94 | * @return string |
| 95 | */ |
| 96 | public function getAlternateDataFolder():string { return $this->get(self::ALTERNATE_DATA_FOLDER); } |
| 97 | |
| 98 | /** |
| 99 | * @param $value |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | public function setAlternateDataFolder($value):void { $this->set(self::ALTERNATE_DATA_FOLDER, $value); } |
| 104 | |
| 105 | /** |
| 106 | * @return string |
| 107 | */ |
| 108 | public function getLicenseKey():string { return $this->get(self::LICENSE_KEY); } |
| 109 | |
| 110 | /** |
| 111 | * @param $value |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | public function setLicenseKey($value):void { $this->set(self::LICENSE_KEY, $value); } |
| 116 | |
| 117 | /** |
| 118 | * @return string |
| 119 | */ |
| 120 | public function getLicenseLocalKey():string { return $this->get(self::LICENSE_LOCAL_KEY); } |
| 121 | |
| 122 | /** |
| 123 | * @param $value |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function setLicenseLocalKey($value):void { $this->set(self::LICENSE_LOCAL_KEY, $value); } |
| 128 | |
| 129 | /** |
| 130 | * @param int $next_check |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function setLicenseNextCheck(int $next_check=0):void { $this->set(self::LICENSE_NEXT_CHECK, $next_check); } |
| 135 | |
| 136 | /** |
| 137 | * @return int |
| 138 | */ |
| 139 | public function getLicenseNextCheck():int { return (int) $this->get(self::LICENSE_NEXT_CHECK, 0); } |
| 140 | |
| 141 | /** |
| 142 | * @param int $last_check |
| 143 | * |
| 144 | * @return void |
| 145 | */ |
| 146 | public function setLicenseLastCheck(int $last_check=0):void { $this->set(self::LICENSE_LAST_CHECK, $last_check); } |
| 147 | |
| 148 | /** |
| 149 | * @return int |
| 150 | */ |
| 151 | public function getLicenseLastCheck():int { return (int) $this->get(self::LICENSE_LAST_CHECK, 0); } |
| 152 | |
| 153 | public function getSupportUsername():string { return $this->get(self::SUPPORT_USERNAME, ''); } |
| 154 | public function setSupportUsername($username):void { $this->set(self::SUPPORT_USERNAME, $username); } |
| 155 | |
| 156 | /** |
| 157 | * @param int $notify_date |
| 158 | * |
| 159 | * @return void |
| 160 | */ |
| 161 | public function setLicenseNotifyDate(int $notify_date=0):void { $this->set(self::LICENSE_NOTIFY_DATE, $notify_date); } |
| 162 | |
| 163 | /** |
| 164 | * @return int |
| 165 | */ |
| 166 | public function getLicenseNotifyDate():int { return (int) $this->get(self::LICENSE_NOTIFY_DATE, 0); } |
| 167 | |
| 168 | /** |
| 169 | * @param string $message |
| 170 | * @param LicenseLocalKey $localKey |
| 171 | * |
| 172 | * @return void |
| 173 | */ |
| 174 | public function setLocalKeyInvalid(string $message, LicenseLocalKey $localKey):void { |
| 175 | $this->setLicenseLocalKey("{$localKey->getSigned()}|{$localKey->getSignedStatus()}|Invalid|$message"); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @return void |
| 180 | */ |
| 181 | public function loadFromDatabase():void { |
| 182 | parent::loadFromDatabase(); |
| 183 | $jetbackup_config = Wordpress::getOption('jetbackup_config'); |
| 184 | $data = $jetbackup_config ? unserialize(Crypt::decrypt($jetbackup_config, DB_PASSWORD)) : []; |
| 185 | $new_data = []; |
| 186 | |
| 187 | foreach($data as $key => $value) { |
| 188 | if(str_starts_with($key, 'JET_') && $key != 'JET_2FA_ENABLED') $key = substr($key, 4); |
| 189 | $new_data[$key] = $value; |
| 190 | } |
| 191 | |
| 192 | $this->setData($new_data); |
| 193 | |
| 194 | // in case we migrate wordpress and there are config left overs in the db, the alternate folder might |
| 195 | // be in non-accessible location, we need to reset it |
| 196 | if($this->getAlternateDataFolder() && !is_writable($this->getAlternateDataFolder())) { |
| 197 | $this->setAlternateDataFolder(''); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @return void |
| 204 | */ |
| 205 | private function _saveToDatabase():void { |
| 206 | Wordpress::setOption('jetbackup_config', Crypt::encrypt(serialize($this->getData()), DB_PASSWORD)); |
| 207 | } |
| 208 | |
| 209 | public function save():void { |
| 210 | $this->_saveToDatabase(); |
| 211 | parent::save(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @return void |
| 216 | * @throws LicenseException |
| 217 | * @throws IOException |
| 218 | */ |
| 219 | public function validateLicense():void { |
| 220 | License::retrieveLocalKey($this->getLicenseKey()); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @return void |
| 225 | * @throws FieldsValidationException |
| 226 | */ |
| 227 | public function validateAltDataDir():void { |
| 228 | $alternate_folder = trim($this->getAlternateDataFolder()) ?? null; |
| 229 | if($alternate_folder) { |
| 230 | $alternate_folder = rtrim(trim($alternate_folder), JetBackup::SEP); |
| 231 | if (!System::isAlternateFolderSecured($alternate_folder)) { |
| 232 | $homedir = Helper::getUserHomedir() ?? dirname(ABSPATH); |
| 233 | throw new FieldsValidationException( "Data folder $alternate_folder cannot be outside of " . $homedir); |
| 234 | } |
| 235 | if(!@is_writable(dirname($alternate_folder))) throw new FieldsValidationException(sprintf("Data folder location '%s' is not writable.", dirname($alternate_folder))); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @throws FieldsValidationException |
| 241 | */ |
| 242 | public function moveDataDir() : void { |
| 243 | if (Factory::getLocations()->getDataDir() == self::getAlternateDataFolder()) return; |
| 244 | if (file_exists(self::getAlternateDataFolder())) throw new FieldsValidationException("Target folder " . self::getAlternateDataFolder() . " already exists."); |
| 245 | if (!@rename(Factory::getLocations()->getDataDir(), self::getAlternateDataFolder())) { |
| 246 | throw new FieldsValidationException("Failed moving " . Factory::getLocations()->getDataDir() . " to " . self::getAlternateDataFolder()); |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | } |