web-base/Core/Configuration/Settings.class.php

282 lines
8.9 KiB
PHP
Raw Normal View History

2020-06-25 16:54:58 +02:00
<?php
/**
* Do not change settings here, they are dynamically loaded from database.
*/
2022-11-18 18:06:46 +01:00
namespace Core\Configuration;
2020-06-25 16:54:58 +02:00
2022-11-18 18:06:46 +01:00
use Core\Driver\Logger\Logger;
2023-01-11 22:05:02 +01:00
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\Condition\CondNot;
use Core\Driver\SQL\Condition\CondRegex;
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\Query\Insert;
2023-01-11 22:05:02 +01:00
use Core\Driver\SQL\SQL;
2024-04-23 14:05:29 +02:00
use Core\Objects\Captcha\CaptchaProvider;
use Core\Objects\Captcha\GoogleRecaptchaProvider;
use Core\Objects\Captcha\HCaptchaProvider;
2024-04-23 20:14:32 +02:00
use Core\Objects\ConnectionData;
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2020-06-25 16:54:58 +02:00
class Settings {
2021-04-02 21:58:06 +02:00
//
private bool $installationComplete;
2022-11-19 01:15:34 +01:00
// general settings
2020-06-25 16:54:58 +02:00
private string $siteName;
private string $baseUrl;
2024-04-11 17:51:50 +02:00
private array $trustedDomains;
2022-11-19 01:15:34 +01:00
private bool $registrationAllowed;
private array $allowedExtensions;
private string $timeZone;
2024-04-23 14:05:29 +02:00
// captcha
private string $captchaProvider;
private string $captchaSiteKey;
private string $captchaSecretKey;
2022-11-19 01:15:34 +01:00
// mail
private bool $mailEnabled;
2022-02-20 16:53:26 +01:00
private string $mailSender;
private string $mailFooter;
2022-11-19 01:15:34 +01:00
private bool $mailAsync;
2020-06-25 16:54:58 +02:00
2024-04-23 20:14:32 +02:00
// rate limiting
private bool $rateLimitingEnabled;
private string $redisHost;
private int $redisPort;
private string $redisPassword;
2022-10-23 21:26:27 +02:00
//
private Logger $logger;
public function __construct() {
$this->logger = new Logger("Settings");
}
2023-01-11 22:05:02 +01:00
public static function getAll(?SQL $sql, ?string $pattern = null, bool $external = false): ?array {
2024-04-11 17:51:50 +02:00
$query = $sql->select("name", "value")->from("Settings");
2023-01-11 22:05:02 +01:00
if ($pattern) {
$query->where(new CondRegex(new Column("name"), $pattern));
}
if ($external) {
$query->where(new CondNot("private"));
}
$res = $query->execute();
if ($res !== false && $res !== null) {
$settings = array();
foreach($res as $row) {
$settings[$row["name"]] = json_decode($row["value"], true);
2023-01-11 22:05:02 +01:00
}
return $settings;
} else {
return null;
}
}
public static function get(?SQL $sql, string $key, mixed $defaultValue): mixed {
$res = $sql->select("value") ->from("Settings")
->whereEq("name", $key)
->execute();
if ($res === false || $res === null) {
return null;
} else {
return (empty($res)) ? $defaultValue : json_decode($res[0]["value"], true);
2023-01-11 22:05:02 +01:00
}
}
2021-04-02 21:58:06 +02:00
public function isInstalled(): bool {
2020-06-25 16:54:58 +02:00
return $this->installationComplete;
}
2021-04-02 21:58:06 +02:00
public static function loadDefaults(): Settings {
2020-06-25 16:54:58 +02:00
$protocol = getProtocol();
2024-04-10 19:04:37 +02:00
$hostname = getCurrentHostName();
2020-06-25 16:54:58 +02:00
$settings = new Settings();
2022-10-23 21:26:27 +02:00
// General
2020-06-25 16:54:58 +02:00
$settings->siteName = "WebBase";
$settings->baseUrl = "$protocol://$hostname";
2024-04-11 17:51:50 +02:00
$settings->trustedDomains = [$hostname];
2022-10-23 21:26:27 +02:00
$settings->allowedExtensions = ['png', 'jpg', 'jpeg', 'gif', 'htm', 'html'];
2020-06-25 16:54:58 +02:00
$settings->installationComplete = false;
$settings->registrationAllowed = false;
2022-11-19 01:15:34 +01:00
$settings->timeZone = date_default_timezone_get();
2022-10-23 21:26:27 +02:00
2024-04-23 14:05:29 +02:00
// captcha
2024-04-24 16:02:16 +02:00
$settings->captchaProvider = "disabled";
2024-04-23 14:05:29 +02:00
$settings->captchaSiteKey = "";
$settings->captchaSecretKey = "";
2022-10-23 21:26:27 +02:00
// Mail
2021-04-02 21:58:06 +02:00
$settings->mailEnabled = false;
2022-02-20 16:53:26 +01:00
$settings->mailSender = "webmaster@localhost";
$settings->mailFooter = "";
2022-11-19 01:15:34 +01:00
$settings->mailAsync = false;
2022-02-20 16:53:26 +01:00
2024-04-23 20:14:32 +02:00
// rate limiting
$settings->redisPort = 6379;
2024-04-23 21:51:07 +02:00
$settings->redisPassword = "";
2024-04-23 20:14:32 +02:00
if (isDocker()) {
$settings->rateLimitingEnabled = true;
$settings->redisHost = "webbase-redis";
} else {
$settings->rateLimitingEnabled = false;
$settings->redisHost = "";
}
2020-06-25 16:54:58 +02:00
return $settings;
}
2022-06-20 19:52:31 +02:00
public function loadFromDatabase(Context $context): bool {
2022-10-23 21:26:27 +02:00
$this->logger = new Logger("Settings", $context->getSQL());
2022-11-18 18:06:46 +01:00
$req = new \Core\API\Settings\Get($context);
2020-06-25 16:54:58 +02:00
$success = $req->execute();
if ($success) {
$result = $req->getResult()["settings"];
$this->siteName = $result["site_name"] ?? $this->siteName;
2021-12-08 16:53:43 +01:00
$this->baseUrl = $result["base_url"] ?? $this->baseUrl;
2020-06-25 16:54:58 +02:00
$this->registrationAllowed = $result["user_registration_enabled"] ?? $this->registrationAllowed;
$this->installationComplete = $result["installation_completed"] ?? $this->installationComplete;
2022-11-19 01:15:34 +01:00
$this->timeZone = $result["time_zone"] ?? $this->timeZone;
2024-04-23 14:05:29 +02:00
$this->captchaProvider = $result["captcha_provider"] ?? $this->captchaProvider;
$this->captchaSiteKey = $result["captcha_site_key"] ?? $this->captchaSiteKey;
$this->captchaSecretKey = $result["captcha_secret_key"] ?? $this->captchaSecretKey;
2021-04-02 21:58:06 +02:00
$this->mailEnabled = $result["mail_enabled"] ?? $this->mailEnabled;
2022-02-20 16:53:26 +01:00
$this->mailSender = $result["mail_from"] ?? $this->mailSender;
$this->mailFooter = $result["mail_footer"] ?? $this->mailFooter;
2022-11-19 01:15:34 +01:00
$this->mailAsync = $result["mail_async"] ?? $this->mailAsync;
$this->allowedExtensions = $result["allowed_extensions"] ?? $this->allowedExtensions;
$this->trustedDomains = $result["trusted_domains"] ?? $this->trustedDomains;
2024-04-23 20:14:32 +02:00
$this->rateLimitingEnabled = $result["rate_limiting_enabled"] ?? $this->rateLimitingEnabled;
$this->redisHost = $result["redis_host"] ?? $this->redisHost;
$this->redisPort = $result["redis_port"] ?? $this->redisPort;
$this->redisPassword = $result["redis_password"] ?? $this->redisPassword;
2022-11-19 01:15:34 +01:00
date_default_timezone_set($this->timeZone);
2020-06-25 16:54:58 +02:00
}
return false;
}
2022-11-19 01:15:34 +01:00
public function addRows(Insert $query): void {
$query->addRow("site_name", json_encode($this->siteName), false, false)
->addRow("base_url", json_encode($this->baseUrl), false, false)
->addRow("trusted_domains", json_encode($this->trustedDomains), false, false)
->addRow("user_registration_enabled", json_encode($this->registrationAllowed), false, false)
->addRow("installation_completed", json_encode($this->installationComplete), true, true)
->addRow("time_zone", json_encode($this->timeZone), false, false)
2024-04-23 14:05:29 +02:00
->addRow("captcha_provider", json_encode($this->captchaProvider), false, false)
->addRow("captcha_site_key", json_encode($this->captchaSiteKey), false, false)
->addRow("captcha_secret_key", json_encode($this->captchaSecretKey), true, false)
->addRow("allowed_extensions", json_encode($this->allowedExtensions), false, false)
->addRow("mail_host", '""', false, false)
->addRow("mail_port", '587', false, false)
->addRow("mail_username", '""', false, false)
->addRow("mail_password", '""', true, false)
->addRow("mail_from", '""', false, false)
2024-04-23 20:35:58 +02:00
->addRow("mail_footer", '""', false, false)
2024-04-23 20:14:32 +02:00
->addRow("mail_async", false, false, false)
->addRow("rate_limiting_enabled", json_encode($this->allowedExtensions), false, false)
->addRow("redis_host", json_encode($this->redisHost), false, false)
->addRow("redis_port", json_encode($this->redisPort), false, false)
->addRow("redis_password", json_encode($this->redisPassword), true, false)
;
2020-06-25 16:54:58 +02:00
}
2020-06-26 18:24:23 +02:00
2021-04-02 21:58:06 +02:00
public function getSiteName(): string {
2020-06-26 18:24:23 +02:00
return $this->siteName;
}
2022-11-19 01:15:34 +01:00
public function getTimeZone(): string {
return $this->timeZone;
}
public function setTimeZone(string $tz) {
$this->timeZone = $tz;
}
2021-04-02 21:58:06 +02:00
public function getBaseUrl(): string {
2020-06-26 18:24:23 +02:00
return $this->baseUrl;
}
2020-06-26 23:32:45 +02:00
2024-04-23 14:05:29 +02:00
public function isCaptchaEnabled(): bool {
return CaptchaProvider::isValid($this->captchaProvider);
2020-06-26 23:32:45 +02:00
}
2024-04-23 14:05:29 +02:00
public function getCaptchaProvider(): ?CaptchaProvider {
return match ($this->captchaProvider) {
CaptchaProvider::RECAPTCHA => new GoogleRecaptchaProvider($this->captchaSiteKey, $this->captchaSecretKey),
CaptchaProvider::HCAPTCHA => new HCaptchaProvider($this->captchaSiteKey, $this->captchaSecretKey),
default => null,
};
2020-06-26 23:32:45 +02:00
}
2020-06-27 22:47:12 +02:00
2021-04-02 21:58:06 +02:00
public function isRegistrationAllowed(): bool {
2020-06-27 22:47:12 +02:00
return $this->registrationAllowed;
}
2021-04-09 16:05:36 +02:00
public function isMailEnabled(): bool {
return $this->mailEnabled;
}
2022-02-20 16:53:26 +01:00
2022-11-19 01:15:34 +01:00
public function isMailAsync(): bool {
return $this->mailAsync;
}
2022-02-20 16:53:26 +01:00
public function getMailSender(): string {
return $this->mailSender;
}
public function isExtensionAllowed(string $ext): bool {
return empty($this->allowedExtensions) || in_array(strtolower(trim($ext)), $this->allowedExtensions);
}
2022-06-20 19:52:31 +02:00
public function getDomain(): string {
return parse_url($this->getBaseUrl(), PHP_URL_HOST);
}
2022-11-18 18:06:46 +01:00
public function getLogger(): Logger {
return $this->logger;
}
2024-04-11 17:51:50 +02:00
public function isTrustedDomain(string $domain): bool {
$domain = strtolower($domain);
foreach ($this->trustedDomains as $trustedDomain) {
$trustedDomain = trim(strtolower($trustedDomain));
if ($trustedDomain === $domain) {
return true;
}
// *.def.com <-> abc.def.com
if (startsWith($trustedDomain, "*.") && endsWith($domain, substr($trustedDomain, 1))) {
return true;
}
}
return false;
}
public function getTrustedDomains(): array {
return $this->trustedDomains;
}
2024-04-23 20:14:32 +02:00
public function isRateLimitingEnabled(): bool {
return $this->rateLimitingEnabled;
}
public function getRedisConfiguration(): ConnectionData {
return new ConnectionData(
$this->redisHost,
$this->redisPort,
"",
$this->redisPassword
);
}
2020-06-25 16:54:58 +02:00
}