Bugfix, Document constructor, docker

This commit is contained in:
2022-06-01 12:28:50 +02:00
parent 366dbbd6cf
commit ce3aa574ea
19 changed files with 159 additions and 83 deletions

View File

@@ -2,33 +2,39 @@
namespace Elements;
use Configuration\Settings;
use Driver\SQL\SQL;
use Objects\Router\Router;
use Objects\User;
abstract class Document {
protected User $user;
protected Router $router;
protected bool $databaseRequired;
private bool $cspEnabled;
private ?string $cspNonce;
private array $cspWhitelist;
private string $domain;
public function __construct(User $user) {
$this->user = $user;
public function __construct(Router $router) {
$this->router = $router;
$this->cspEnabled = false;
$this->cspNonce = null;
$this->databaseRequired = true;
$this->cspWhitelist = [];
$this->domain = $user->getConfiguration()->getSettings()->getBaseUrl();
}
public function getSQL(): ?SQL {
return $this->user->getSQL();
$this->domain = $this->getSettings()->getBaseUrl();
}
public function getUser(): User {
return $this->user;
return $this->router->getUser();
}
public function getSQL(): ?SQL {
return $this->getUser()->getSQL();
}
public function getSettings(): Settings {
return $this->getUser()->getConfiguration()->getSettings();
}
public function getCSPNonce(): ?string {
@@ -44,13 +50,17 @@ abstract class Document {
$this->cspNonce = generateRandomString(16, "base62");
}
public function getRouter(): Router {
return $this->router;
}
protected function addCSPWhitelist(string $path) {
$this->cspWhitelist[] = $this->domain . $path;
}
public function getCode(array $params = []): string {
if ($this->databaseRequired) {
$sql = $this->user->getSQL();
$sql = $this->getSQL();
if (is_null($sql)) {
die("Database is not configured yet.");
} else if (!$sql->isConnected()) {
@@ -70,7 +80,7 @@ abstract class Document {
"img-src 'self' data:",
"script-src $cspWhiteList 'nonce-$this->cspNonce'"
];
if ($this->user->getConfiguration()->getSettings()->isRecaptchaEnabled()) {
if ($this->getSettings()->isRecaptchaEnabled()) {
$csp[] = "frame-src https://www.google.com/ 'self'";
}

View File

@@ -2,7 +2,7 @@
namespace Elements;
use Objects\User;
use Objects\Router\Router;
class HtmlDocument extends Document {
@@ -10,8 +10,8 @@ class HtmlDocument extends Document {
protected Body $body;
private ?string $activeView;
public function __construct(User $user, $headClass, $bodyClass, ?string $view = NULL) {
parent::__construct($user);
public function __construct(Router $router, $headClass, $bodyClass, ?string $view = NULL) {
parent::__construct($router);
$this->head = $headClass ? new $headClass($this) : null;
$this->body = $bodyClass ? new $bodyClass($this) : null;
$this->activeView = $view;
@@ -49,7 +49,7 @@ class HtmlDocument extends Document {
return $this->activeView;
}
function getCode(): string {
function getCode(array $params = []): string {
parent::getCode();
@@ -65,7 +65,7 @@ class HtmlDocument extends Document {
}
$head = $this->head->getCode();
$lang = $this->user->getLanguage()->getShortCode();
$lang = $this->getUser()->getLanguage()->getShortCode();
$html = "<!DOCTYPE html>";
$html .= "<html lang=\"$lang\">";

View File

@@ -2,7 +2,7 @@
namespace Elements;
use Objects\User;
use Objects\Router\Router;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
@@ -17,8 +17,8 @@ class TemplateDocument extends Document {
private FilesystemLoader $twigLoader;
protected string $title;
public function __construct(User $user, string $templateName, array $params = []) {
parent::__construct($user);
public function __construct(Router $router, string $templateName, array $params = []) {
parent::__construct($router);
$this->title = "";
$this->templateName = $templateName;
$this->parameters = $params;
@@ -46,15 +46,16 @@ class TemplateDocument extends Document {
public function renderTemplate(string $name, array $params = []): string {
try {
$user = $this->getUser();
$params["user"] = [
"lang" => $this->user->getLanguage()->getShortCode(),
"loggedIn" => $this->user->isLoggedIn(),
"session" => (!$this->user->isLoggedIn() ? null : [
"csrfToken" => $this->user->getSession()->getCsrfToken()
"lang" => $user->getLanguage()->getShortCode(),
"loggedIn" => $user->isLoggedIn(),
"session" => (!$user->isLoggedIn() ? null : [
"csrfToken" => $user->getSession()->getCsrfToken()
])
];
$settings = $this->user->getConfiguration()->getSettings();
$settings = $this->getSettings();
$params["site"] = [
"name" => $settings->getSiteName(),
"baseUrl" => $settings->getBaseUrl(),