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

137 lines
4.5 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.
*/
namespace Configuration;
use Driver\SQL\Query\Insert;
use Objects\User;
class Settings {
2021-04-02 21:58:06 +02:00
//
private bool $installationComplete;
// settings
2020-06-25 16:54:58 +02:00
private string $siteName;
private string $baseUrl;
private string $jwtSecret;
private bool $registrationAllowed;
2020-06-26 23:32:45 +02:00
private bool $recaptchaEnabled;
2021-04-02 21:58:06 +02:00
private bool $mailEnabled;
2020-06-26 23:32:45 +02:00
private string $recaptchaPublicKey;
private string $recaptchaPrivateKey;
2022-02-20 16:53:26 +01:00
private string $mailSender;
private string $mailFooter;
private array $allowedExtensions;
2020-06-25 16:54:58 +02:00
public function getJwtSecret(): string {
return $this->jwtSecret;
}
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 {
2021-04-07 00:03:14 +02:00
$hostname = $_SERVER["SERVER_NAME"] ?? "localhost";
2020-06-25 16:54:58 +02:00
$protocol = getProtocol();
$jwt = generateRandomString(32);
$settings = new Settings();
$settings->siteName = "WebBase";
$settings->baseUrl = "$protocol://$hostname";
$settings->jwtSecret = $jwt;
$settings->installationComplete = false;
$settings->registrationAllowed = false;
2020-06-26 23:32:45 +02:00
$settings->recaptchaPublicKey = "";
$settings->recaptchaPrivateKey = "";
$settings->recaptchaEnabled = false;
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 = "";
$settings->allowedExtensions = ['png', 'jpg', 'jpeg', 'gif', 'htm', 'html'];
2022-02-20 16:53:26 +01:00
2020-06-25 16:54:58 +02:00
return $settings;
}
2021-04-02 21:58:06 +02:00
public function loadFromDatabase(User $user): bool {
2020-06-25 16:54:58 +02:00
$req = new \Api\Settings\Get($user);
$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;
$this->jwtSecret = $result["jwt_secret"] ?? $this->jwtSecret;
2020-06-26 23:32:45 +02:00
$this->recaptchaEnabled = $result["recaptcha_enabled"] ?? $this->recaptchaEnabled;
$this->recaptchaPublicKey = $result["recaptcha_public_key"] ?? $this->recaptchaPublicKey;
$this->recaptchaPrivateKey = $result["recaptcha_private_key"] ?? $this->recaptchaPrivateKey;
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;
$this->allowedExtensions = explode(",", $result["allowed_extensions"] ?? strtolower(implode(",", $this->allowedExtensions)));
2020-06-25 16:54:58 +02:00
if (!isset($result["jwt_secret"])) {
$req = new \Api\Settings\Set($user);
$req->execute(array("settings" => array(
"jwt_secret" => $this->jwtSecret
)));
}
}
return false;
}
public function addRows(Insert $query) {
2020-06-26 19:31:31 +02:00
$query->addRow("site_name", $this->siteName, false, false)
->addRow("base_url", $this->baseUrl, false, false)
->addRow("user_registration_enabled", $this->registrationAllowed ? "1" : "0", false, false)
->addRow("installation_completed", $this->installationComplete ? "1" : "0", true, true)
2020-06-26 23:32:45 +02:00
->addRow("jwt_secret", $this->jwtSecret, true, true)
->addRow("recaptcha_enabled", $this->recaptchaEnabled ? "1" : "0", false, false)
->addRow("recaptcha_public_key", $this->recaptchaPublicKey, false, false)
->addRow("recaptcha_private_key", $this->recaptchaPrivateKey, true, false)
->addRow("allowed_extensions", implode(",", $this->allowedExtensions), 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;
}
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
2021-04-02 21:58:06 +02:00
public function isRecaptchaEnabled(): bool {
2020-06-26 23:32:45 +02:00
return $this->recaptchaEnabled;
}
2021-04-02 21:58:06 +02:00
public function getRecaptchaSiteKey(): string {
2020-06-26 23:32:45 +02:00
return $this->recaptchaPublicKey;
}
2021-04-02 21:58:06 +02:00
public function getRecaptchaSecretKey(): string {
2020-06-26 23:32:45 +02:00
return $this->recaptchaPrivateKey;
}
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
public function getMailSender(): string {
return $this->mailSender;
}
public function isExtensionAllowed(string $ext): bool {
return empty($this->allowedExtensions) || in_array(strtolower(trim($ext)), $this->allowedExtensions);
}
2020-06-25 16:54:58 +02:00
}