.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
189 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\JetBackupLinux\JetBackupLinux; |
| 13 | use JetBackup\License\License; |
| 14 | use JetBackup\Wordpress\Wordpress; |
| 15 | use SleekDB\Exceptions\InvalidArgumentException; |
| 16 | use SleekDB\Exceptions\IOException; |
| 17 | |
| 18 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 19 | |
| 20 | class General extends Settings { |
| 21 | |
| 22 | const SECTION = 'general'; |
| 23 | |
| 24 | const TIMEZONE = 'TIMEZONE'; |
| 25 | const JETBACKUP_INTEGRATION = 'JETBACKUP_INTEGRATION'; |
| 26 | const JETBACKUP_SOCKET_API_STATUS = 'JETBACKUP_SOCKET_API_STATUS'; |
| 27 | const ADMIN_TOP_MENU_INTEGRATION = 'ADMIN_TOP_MENU_INTEGRATION'; |
| 28 | const COMMUNITY_LANGUAGES = 'COMMUNITY_LANGUAGES'; |
| 29 | const MANUAL_BACKUPS_RETENTION = 'MANUAL_BACKUPS_RETENTION'; |
| 30 | const PHP_CLI_LOCATION = 'PHP_CLI_LOCATION'; |
| 31 | const MYSQL_DEFAULT_PORT = 'MYSQL_DEFAULT_PORT'; |
| 32 | const PHP_DEFAULT_BINARY = 'php'; |
| 33 | const DEFAULT_TIMEZONE = 'UTC'; |
| 34 | const WORDPRESS_TIMEZONE = 'WORDPRESS_TIMEZONE'; |
| 35 | const LICENSE_STATUS = 'LICENSE_STATUS'; |
| 36 | |
| 37 | private ?bool $socketApiStatus = null; |
| 38 | /** |
| 39 | * @throws InvalidArgumentException |
| 40 | * @throws DBException |
| 41 | * @throws IOException |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | parent::__construct(self::SECTION); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return string |
| 49 | */ |
| 50 | public function getTimeZone():string { |
| 51 | $timezone = $this->get(self::TIMEZONE, self::WORDPRESS_TIMEZONE); |
| 52 | if ($timezone == self::WORDPRESS_TIMEZONE) return Wordpress::getOption('timezone_string') ?: self::DEFAULT_TIMEZONE; |
| 53 | return $timezone; |
| 54 | } |
| 55 | |
| 56 | private function getTimeZoneDisplay() {return $this->get(self::TIMEZONE, self::WORDPRESS_TIMEZONE);} |
| 57 | /** |
| 58 | * @param $value |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function setTimeZone($value):void { $this->set(self::TIMEZONE, $value); } |
| 63 | |
| 64 | /** |
| 65 | * @return bool |
| 66 | */ |
| 67 | public function isJBIntegrationEnabled():bool { return (bool) $this->get(self::JETBACKUP_INTEGRATION, false); } |
| 68 | private function _getSocketApiStatus():bool { |
| 69 | |
| 70 | if ($this->socketApiStatus === null) { |
| 71 | try { |
| 72 | JetBackupLinux::getAccountInfo(); |
| 73 | $this->socketApiStatus = true; |
| 74 | } catch (\Exception $e) { |
| 75 | $this->socketApiStatus = false; |
| 76 | } |
| 77 | } |
| 78 | return $this->socketApiStatus; |
| 79 | |
| 80 | } |
| 81 | public function isAdminTopMenuIntegrationEnabled():bool { return (bool) $this->get(self::ADMIN_TOP_MENU_INTEGRATION, false); } |
| 82 | |
| 83 | /** |
| 84 | * @param bool $value |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function setJBIntegrationEnabled(bool $value):void { $this->set(self::JETBACKUP_INTEGRATION, $value); } |
| 89 | public function setAdminTopMenuIntegration(bool $value):void { $this->set(self::ADMIN_TOP_MENU_INTEGRATION, $value); } |
| 90 | |
| 91 | /** |
| 92 | * @return bool |
| 93 | */ |
| 94 | public function isCommunityLanguages():bool { return (bool) $this->get(self::COMMUNITY_LANGUAGES, true); } |
| 95 | |
| 96 | /** |
| 97 | * @param bool $value |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public function setCommunityLanguages(bool $value):void { $this->set(self::COMMUNITY_LANGUAGES, $value); } |
| 102 | |
| 103 | /** |
| 104 | * @return int |
| 105 | */ |
| 106 | public function getManualBackupsRetention():int { return (int) $this->get(self::MANUAL_BACKUPS_RETENTION, 0); } |
| 107 | |
| 108 | /** |
| 109 | * @param int $value |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public function setManualBackupsRetention(int $value):void { $this->set(self::MANUAL_BACKUPS_RETENTION, $value); } |
| 114 | |
| 115 | /** |
| 116 | * @return string |
| 117 | */ |
| 118 | public function getPHPCLILocation():string { return $this->get(self::PHP_CLI_LOCATION, self::PHP_DEFAULT_BINARY); } |
| 119 | public function getMysqlDefaultPort():int { return $this->get(self::MYSQL_DEFAULT_PORT, 3306); } |
| 120 | |
| 121 | /** |
| 122 | * @param $value |
| 123 | * |
| 124 | * @return void |
| 125 | */ |
| 126 | public function setPHPCLILocation($value):void { $this->set(self::PHP_CLI_LOCATION, $value); } |
| 127 | public function setMysqlDefaultPort($value):void { $this->set(self::MYSQL_DEFAULT_PORT, $value); } |
| 128 | |
| 129 | /** |
| 130 | * @return array |
| 131 | * @throws Exception |
| 132 | */ |
| 133 | public function getDisplay():array { |
| 134 | |
| 135 | return [ |
| 136 | Config::LICENSE_KEY => str_repeat('*', max(0, strlen(Factory::getConfig()->getLicenseKey()) - 6)) . substr(Factory::getConfig()->getLicenseKey(), -4), |
| 137 | self::LICENSE_STATUS => License::getLicenseStatus(), |
| 138 | self::TIMEZONE => $this->getTimeZoneDisplay(), |
| 139 | self::COMMUNITY_LANGUAGES => $this->isCommunityLanguages() ? 1 : 0, |
| 140 | self::JETBACKUP_INTEGRATION => $this->_getSocketApiStatus() ? ($this->isJBIntegrationEnabled() ? 1 : 0) : 0, |
| 141 | self::JETBACKUP_SOCKET_API_STATUS => $this->_getSocketApiStatus() ? 1 : 0, |
| 142 | self::ADMIN_TOP_MENU_INTEGRATION => $this->isAdminTopMenuIntegrationEnabled() ? 1 : 0, |
| 143 | self::MANUAL_BACKUPS_RETENTION => $this->getManualBackupsRetention(), |
| 144 | self::PHP_CLI_LOCATION => $this->getPHPCLILocation(), |
| 145 | self::MYSQL_DEFAULT_PORT => $this->getMySQLDefaultPort(), |
| 146 | ]; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return array |
| 151 | * @throws Exception |
| 152 | */ |
| 153 | public function getDisplayCLI():array { |
| 154 | |
| 155 | return [ |
| 156 | 'License Key' => str_repeat('*', max(0, strlen(Factory::getConfig()->getLicenseKey()) - 6)) . substr(Factory::getConfig()->getLicenseKey(), -4), |
| 157 | 'License Status' => License::getLicenseStatus(), |
| 158 | 'Timezone' => $this->getTimeZoneDisplay(), |
| 159 | 'Community Languages' => $this->isCommunityLanguages() ? "Yes" : "No", |
| 160 | 'JetBackup Integration' => $this->_getSocketApiStatus() ? ($this->isJBIntegrationEnabled() ? "Yes" : "No") : "No", |
| 161 | 'JetBackup Socket API Enabled' => $this->_getSocketApiStatus() ? "Yes" : "No", |
| 162 | 'Admin bar top menu integration' => $this->isAdminTopMenuIntegrationEnabled() ? "Yes" : "No", |
| 163 | 'Manual Backup Retain' => $this->getManualBackupsRetention(), |
| 164 | 'PHP CLI Location' => $this->getPHPCLILocation(), |
| 165 | 'MySQL Default Port' => $this->getMysqlDefaultPort(), |
| 166 | ]; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @return void |
| 171 | * @throws FieldsValidationException |
| 172 | */ |
| 173 | public function validateFields():void { |
| 174 | |
| 175 | if((!$this->getTimeZone() || $this->getTimeZone() != (self::DEFAULT_TIMEZONE || self::WORDPRESS_TIMEZONE)) && !isset(Util::generateTimeZoneList()[$this->getTimeZone()])) |
| 176 | throw new FieldsValidationException("Timezone " . $this->getTimeZone(). " is not valid"); |
| 177 | |
| 178 | if((!$this->getPHPCLILocation() || strtolower($this->getPHPCLILocation()) != self::PHP_DEFAULT_BINARY) && !is_executable(trim($this->getPHPCLILocation()))) |
| 179 | throw new FieldsValidationException("PHP CLI location ".$this->getPHPCLILocation()." is not executable"); |
| 180 | |
| 181 | if($this->isJBIntegrationEnabled()) { |
| 182 | try { |
| 183 | JetBackupLinux::checkRequirements(); |
| 184 | } catch(JetBackupLinuxException $e) { |
| 185 | throw new FieldsValidationException($e->getMessage()); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |