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