.htaccess
5 months ago
Automation.php
5 months ago
General.php
5 months ago
Integrations.php
5 months ago
Logging.php
5 months ago
Maintenance.php
5 months ago
Notifications.php
5 months ago
Performance.php
5 months ago
Restore.php
5 months ago
Security.php
5 months ago
Settings.php
5 months ago
Updates.php
5 months ago
index.html
5 months ago
web.config
5 months ago
General.php
250 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Settings; |
| 4 | |
| 5 | use Exception; |
| 6 | use JetBackup\Config\Config; |
| 7 | use JetBackup\Entities\Util; |
| 8 | use JetBackup\Exception\DBException; |
| 9 | use JetBackup\Exception\FieldsValidationException; |
| 10 | use JetBackup\Exception\JetBackupLinuxException; |
| 11 | use JetBackup\Factory; |
| 12 | use JetBackup\JetBackup; |
| 13 | use JetBackup\JetBackupLinux\JetBackupLinux; |
| 14 | use JetBackup\License\License; |
| 15 | use JetBackup\Wordpress\Helper; |
| 16 | use JetBackup\Wordpress\Wordpress; |
| 17 | use SleekDB\Exceptions\InvalidArgumentException; |
| 18 | use SleekDB\Exceptions\IOException; |
| 19 | |
| 20 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 21 | |
| 22 | class General extends Settings { |
| 23 | |
| 24 | const SECTION = 'general'; |
| 25 | |
| 26 | const TIMEZONE = 'TIMEZONE'; |
| 27 | const JETBACKUP_INTEGRATION = 'JETBACKUP_INTEGRATION'; |
| 28 | const JETBACKUP_SOCKET_API_STATUS = 'JETBACKUP_SOCKET_API_STATUS'; |
| 29 | const JETBACKUP_SOCKET_API_ERROR_MESSAGE = 'JETBACKUP_SOCKET_API_ERROR_MESSAGE'; |
| 30 | const ADMIN_TOP_MENU_INTEGRATION = 'ADMIN_TOP_MENU_INTEGRATION'; |
| 31 | const DISPLAY_LOCAL_FREE_DISK_SPACE = 'DISPLAY_LOCAL_FREE_DISK_SPACE'; |
| 32 | const COMMUNITY_LANGUAGES = 'COMMUNITY_LANGUAGES'; |
| 33 | const MANUAL_BACKUPS_RETENTION = 'MANUAL_BACKUPS_RETENTION'; |
| 34 | const PHP_CLI_LOCATION = 'PHP_CLI_LOCATION'; |
| 35 | const ALTERNATE_WP_CONFIG_LOCATION = 'ALTERNATE_WP_CONFIG_LOCATION'; |
| 36 | const MYSQL_DEFAULT_PORT = 'MYSQL_DEFAULT_PORT'; |
| 37 | const PHP_DEFAULT_BINARY = 'php'; |
| 38 | const DEFAULT_TIMEZONE = 'UTC'; |
| 39 | const WORDPRESS_TIMEZONE = 'WORDPRESS_TIMEZONE'; |
| 40 | const LICENSE_STATUS = 'LICENSE_STATUS'; |
| 41 | |
| 42 | private ?bool $socketApiStatus = null; |
| 43 | private ?string $socketApiErrMsg = null; |
| 44 | /** |
| 45 | * @throws InvalidArgumentException |
| 46 | * @throws DBException |
| 47 | * @throws IOException |
| 48 | */ |
| 49 | public function __construct() { |
| 50 | parent::__construct(self::SECTION); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return string |
| 55 | */ |
| 56 | public function getTimeZone():string { |
| 57 | $timezone = $this->get(self::TIMEZONE, self::WORDPRESS_TIMEZONE); |
| 58 | if ($timezone == self::WORDPRESS_TIMEZONE) return Wordpress::getOption('timezone_string') ?: self::DEFAULT_TIMEZONE; |
| 59 | return $timezone; |
| 60 | } |
| 61 | |
| 62 | private function getTimeZoneDisplay() {return $this->get(self::TIMEZONE, self::WORDPRESS_TIMEZONE);} |
| 63 | /** |
| 64 | * @param $value |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function setTimeZone($value):void { $this->set(self::TIMEZONE, $value); } |
| 69 | |
| 70 | /** |
| 71 | * @return bool |
| 72 | */ |
| 73 | public function isJBIntegrationEnabled():bool { return (bool) $this->get(self::JETBACKUP_INTEGRATION, false); } |
| 74 | private function _getSocketApiStatus():bool { |
| 75 | |
| 76 | if ($this->socketApiStatus === null) { |
| 77 | try { |
| 78 | JetBackupLinux::checkRequirements(); |
| 79 | JetBackupLinux::getAccountInfo(); |
| 80 | $this->socketApiStatus = true; |
| 81 | } catch (\Exception $e) { |
| 82 | $this->socketApiErrMsg = $e->getMessage(); |
| 83 | $this->socketApiStatus = false; |
| 84 | } |
| 85 | } |
| 86 | return $this->socketApiStatus; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | private function _getSocketApiErrorMessage():?string { |
| 91 | return $this->socketApiErrMsg ?? null; |
| 92 | } |
| 93 | |
| 94 | public function isAdminTopMenuIntegrationEnabled():bool { return (bool) $this->get(self::ADMIN_TOP_MENU_INTEGRATION, false); } |
| 95 | public function isDisplayLocalDiskSpaceEnabled():bool { return (bool) $this->get(self::DISPLAY_LOCAL_FREE_DISK_SPACE, false); } |
| 96 | |
| 97 | /** |
| 98 | * @param bool $value |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public function setJBIntegrationEnabled(bool $value):void { $this->set(self::JETBACKUP_INTEGRATION, $value); } |
| 103 | public function setAdminTopMenuIntegration(bool $value):void { $this->set(self::ADMIN_TOP_MENU_INTEGRATION, $value); } |
| 104 | public function setDisplayLocalDiskSpace(bool $value):void { $this->set(self::DISPLAY_LOCAL_FREE_DISK_SPACE, $value); } |
| 105 | |
| 106 | /** |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function isCommunityLanguages():bool { return (bool) $this->get(self::COMMUNITY_LANGUAGES, true); } |
| 110 | |
| 111 | /** |
| 112 | * @param bool $value |
| 113 | * |
| 114 | * @return void |
| 115 | */ |
| 116 | public function setCommunityLanguages(bool $value):void { $this->set(self::COMMUNITY_LANGUAGES, $value); } |
| 117 | |
| 118 | /** |
| 119 | * @return int |
| 120 | */ |
| 121 | public function getManualBackupsRetention():int { return (int) $this->get(self::MANUAL_BACKUPS_RETENTION, 0); } |
| 122 | |
| 123 | /** |
| 124 | * @param int $value |
| 125 | * |
| 126 | * @return void |
| 127 | */ |
| 128 | public function setManualBackupsRetention(int $value):void { $this->set(self::MANUAL_BACKUPS_RETENTION, $value); } |
| 129 | |
| 130 | /** |
| 131 | * @return string |
| 132 | */ |
| 133 | public function getPHPCLILocation():string { return $this->get(self::PHP_CLI_LOCATION, self::PHP_DEFAULT_BINARY); } |
| 134 | public function getAlternateWpConfigLocation():string { |
| 135 | $config = $this->get(trim(self::ALTERNATE_WP_CONFIG_LOCATION, '')); |
| 136 | if($config == "" || !$config) $config = JetBackup::WP_ROOT_PATH . JetBackup::SEP . 'wp-config.php'; // we cannot put this is the default response if I add to db and then remove |
| 137 | return $config; |
| 138 | } |
| 139 | |
| 140 | public function getMysqlDefaultPort():int { return $this->get(self::MYSQL_DEFAULT_PORT, 3306); } |
| 141 | |
| 142 | /** |
| 143 | * @param $value |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | public function setPHPCLILocation($value):void { $this->set(self::PHP_CLI_LOCATION, $value); } |
| 148 | public function setAlternateWpConfigLocation($value):void { $this->set(self::ALTERNATE_WP_CONFIG_LOCATION, $value); } |
| 149 | public function setMysqlDefaultPort($value):void { $this->set(self::MYSQL_DEFAULT_PORT, $value); } |
| 150 | |
| 151 | /** |
| 152 | * @return array |
| 153 | * @throws Exception |
| 154 | */ |
| 155 | public function getDisplay():array { |
| 156 | |
| 157 | return [ |
| 158 | Config::LICENSE_KEY => str_repeat('*', max(0, strlen(Factory::getConfig()->getLicenseKey()) - 6)) . substr(Factory::getConfig()->getLicenseKey(), -4), |
| 159 | self::LICENSE_STATUS => License::getLicenseStatus(), |
| 160 | self::TIMEZONE => $this->getTimeZoneDisplay(), |
| 161 | self::COMMUNITY_LANGUAGES => $this->isCommunityLanguages() ? 1 : 0, |
| 162 | self::JETBACKUP_INTEGRATION => $this->_getSocketApiStatus() ? ($this->isJBIntegrationEnabled() ? 1 : 0) : 0, |
| 163 | self::JETBACKUP_SOCKET_API_STATUS => $this->_getSocketApiStatus() ? 1 : 0, |
| 164 | self::JETBACKUP_SOCKET_API_ERROR_MESSAGE => $this->_getSocketApiErrorMessage(), |
| 165 | self::ADMIN_TOP_MENU_INTEGRATION => $this->isAdminTopMenuIntegrationEnabled() ? 1 : 0, |
| 166 | self::DISPLAY_LOCAL_FREE_DISK_SPACE => $this->isDisplayLocalDiskSpaceEnabled() ? 1 : 0, |
| 167 | self::MANUAL_BACKUPS_RETENTION => $this->getManualBackupsRetention(), |
| 168 | self::PHP_CLI_LOCATION => $this->getPHPCLILocation(), |
| 169 | self::ALTERNATE_WP_CONFIG_LOCATION => $this->getAlternateWpConfigLocation(), |
| 170 | self::MYSQL_DEFAULT_PORT => $this->getMySQLDefaultPort(), |
| 171 | ]; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @return array |
| 176 | * @throws Exception |
| 177 | */ |
| 178 | public function getDisplayCLI():array { |
| 179 | |
| 180 | return [ |
| 181 | 'License Key' => str_repeat('*', max(0, strlen(Factory::getConfig()->getLicenseKey()) - 6)) . substr(Factory::getConfig()->getLicenseKey(), -4), |
| 182 | 'License Status' => License::getLicenseStatus(), |
| 183 | 'Timezone' => $this->getTimeZoneDisplay(), |
| 184 | 'Community Languages' => $this->isCommunityLanguages() ? "Yes" : "No", |
| 185 | 'JetBackup Integration' => $this->_getSocketApiStatus() ? ($this->isJBIntegrationEnabled() ? "Yes" : "No") : "No", |
| 186 | 'JetBackup Socket API Enabled' => $this->_getSocketApiStatus() ? "Yes" : "No", |
| 187 | 'JetBackup Socket API Error' => $this->_getSocketApiErrorMessage(), |
| 188 | 'Admin bar top menu integration' => $this->isAdminTopMenuIntegrationEnabled() ? "Yes" : "No", |
| 189 | 'Display Local Disk Space' => $this->isDisplayLocalDiskSpaceEnabled() ? "Yes" : "No", |
| 190 | 'Manual Backup Retain' => $this->getManualBackupsRetention(), |
| 191 | 'PHP CLI Location' => $this->getPHPCLILocation(), |
| 192 | 'Alternate wp-config.php Location' => $this->getAlternateWpConfigLocation(), |
| 193 | 'MySQL Default Port' => $this->getMysqlDefaultPort(), |
| 194 | ]; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @return void |
| 199 | * @throws FieldsValidationException |
| 200 | * @throws Exception |
| 201 | */ |
| 202 | public function validateFields():void { |
| 203 | |
| 204 | if($this->getAlternateWpConfigLocation()) { |
| 205 | |
| 206 | $alternateLocation = JetBackup::SEP. trim($this->getAlternateWpConfigLocation(), JetBackup::SEP); |
| 207 | $homedir = JetBackup::SEP . trim(Helper::getUserHomedir() ?? dirname(Wordpress::getAbsPath()), JetBackup::SEP) . JetBackup::SEP; |
| 208 | |
| 209 | if(!str_starts_with($alternateLocation, $homedir)) throw new FieldsValidationException("Alternate location '$alternateLocation' cannot be outside of " . $homedir); |
| 210 | if(!is_file($alternateLocation)) throw new FieldsValidationException("Alternate location '$alternateLocation' is not a file or does not exist"); |
| 211 | if(!is_readable($alternateLocation)) throw new FieldsValidationException("Alternate location '$alternateLocation' is not readable or does not exist"); |
| 212 | |
| 213 | try { |
| 214 | |
| 215 | $config = Factory::getWPHelper()::parseWpConfig(); |
| 216 | |
| 217 | if(!isset($config->db_name)) throw new FieldsValidationException("Couldn't find database name in configuration"); |
| 218 | if(!isset($config->db_user)) throw new FieldsValidationException("Couldn't find database user in configuration"); |
| 219 | if(!isset($config->db_password)) throw new FieldsValidationException("Couldn't find database password in configuration"); |
| 220 | if(!isset($config->db_host)) throw new FieldsValidationException("Couldn't find database host in configuration"); |
| 221 | if(!isset($config->db_port)) throw new FieldsValidationException("Couldn't find database port in configuration"); |
| 222 | if(!isset($config->table_prefix)) throw new FieldsValidationException("Couldn't find database table prefix in configuration"); |
| 223 | |
| 224 | |
| 225 | if($config->db_name != DB_NAME) throw new FieldsValidationException("Database name from alternate config doesn't match runtime database name"); |
| 226 | if($config->db_user != DB_USER) throw new FieldsValidationException("Database user from alternate config doesn't match runtime database user"); |
| 227 | if($config->db_password != DB_PASSWORD) throw new FieldsValidationException("Database password from alternate config doesn't match runtime database password"); |
| 228 | |
| 229 | } catch (Exception $e) { |
| 230 | throw new FieldsValidationException($e->getMessage()); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | } |
| 235 | |
| 236 | if((!$this->getTimeZone() || $this->getTimeZone() != (self::DEFAULT_TIMEZONE || self::WORDPRESS_TIMEZONE)) && !isset(Util::generateTimeZoneList()[$this->getTimeZone()])) |
| 237 | throw new FieldsValidationException("Timezone " . $this->getTimeZone(). " is not valid"); |
| 238 | |
| 239 | if((!$this->getPHPCLILocation() || strtolower($this->getPHPCLILocation()) != self::PHP_DEFAULT_BINARY) && !is_executable(trim($this->getPHPCLILocation()))) |
| 240 | throw new FieldsValidationException("PHP CLI location ".$this->getPHPCLILocation()." is not executable"); |
| 241 | |
| 242 | if($this->isJBIntegrationEnabled()) { |
| 243 | try { |
| 244 | JetBackupLinux::checkRequirements(); |
| 245 | } catch(JetBackupLinuxException $e) { |
| 246 | throw new FieldsValidationException($e->getMessage()); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |