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;
|
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;
|
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;
|
|
|
|
$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;
|
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);
|
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;
|
|
|
|
}
|
2020-06-25 16:54:58 +02:00
|
|
|
}
|