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