This commit is contained in:
2022-08-20 22:17:17 +02:00
parent 58c905acf5
commit 35d7e4000a
29 changed files with 619 additions and 131 deletions

View File

@@ -6,8 +6,13 @@ use Configuration\Settings;
use Driver\Logger\Logger;
use Driver\SQL\SQL;
use Objects\Context;
use Objects\lang\LanguageModule;
use Objects\Router\DocumentRoute;
use Objects\Router\Router;
use Objects\DatabaseEntity\User;
use Objects\Search\Searchable;
use Objects\Search\SearchQuery;
use Objects\Search\SearchResult;
abstract class Document {
@@ -18,6 +23,7 @@ abstract class Document {
private ?string $cspNonce;
private array $cspWhitelist;
private string $domain;
protected bool $searchable;
public function __construct(Router $router) {
$this->router = $router;
@@ -27,6 +33,13 @@ abstract class Document {
$this->cspWhitelist = [];
$this->domain = $this->getSettings()->getBaseUrl();
$this->logger = new Logger("Document", $this->getSQL());
$this->searchable = false;
}
public abstract function getTitle(): string;
public function isSearchable(): bool {
return $this->searchable;
}
public function getLogger(): Logger {
@@ -66,30 +79,29 @@ abstract class Document {
return $this->router;
}
protected function addCSPWhitelist(string $path) {
$this->cspWhitelist[] = $this->domain . $path;
public function addCSPWhitelist(string $path) {
$urlParts = parse_url($path);
if (!$urlParts || !isset($urlParts["host"])) {
$this->cspWhitelist[] = $this->domain . $path;
} else {
$this->cspWhitelist[] = $path;
}
}
public function getCode(array $params = []): string {
if ($this->databaseRequired) {
$sql = $this->getSQL();
if (is_null($sql)) {
die("Database is not configured yet.");
} else if (!$sql->isConnected()) {
die("Database is not connected: " . $sql->getLastError());
}
}
public function loadLanguageModule(LanguageModule|string $module) {
$language = $this->getContext()->getLanguage();
$language->loadModule($module);
}
public function sendHeaders() {
if ($this->cspEnabled) {
$cspWhiteList = implode(" ", $this->cspWhitelist);
$csp = [
"default-src 'self'",
"default-src $cspWhiteList 'self'",
"object-src 'none'",
"base-uri 'self'",
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data:",
"img-src 'self' 'unsafe-inline' data: https:;",
"script-src $cspWhiteList 'nonce-$this->cspNonce'"
];
if ($this->getSettings()->isRecaptchaEnabled()) {
@@ -99,7 +111,42 @@ abstract class Document {
$compiledCSP = implode("; ", $csp);
header("Content-Security-Policy: $compiledCSP;");
}
}
return "";
public abstract function getCode(array $params = []);
public function load(array $params = []): string {
if ($this->databaseRequired) {
$sql = $this->getSQL();
if (is_null($sql)) {
return "Database is not configured yet.";
} else if (!$sql->isConnected()) {
return "Database is not connected: " . $sql->getLastError();
}
}
$code = $this->getCode($params);
$this->sendHeaders();
return $code;
}
public function doSearch(SearchQuery $query, DocumentRoute $route): array {
$code = $this->getCode();
$results = Searchable::searchHtml($code, $query);
return array_map(function ($res) use ($route) {
return new SearchResult($route->getUrl(), $this->getTitle(), $res["text"]);
}, $results);
}
public function createScript($type, $src, $content = ""): Script {
$script = new Script($type, $src, $content);
if ($this->isCSPEnabled()) {
$script->setNonce($this->getCSPNonce());
}
return $script;
}
}

View File

@@ -6,8 +6,8 @@ use Objects\Router\Router;
class HtmlDocument extends Document {
protected Head $head;
protected Body $body;
protected ?Head $head;
protected ?Body $body;
private ?string $activeView;
public function __construct(Router $router, $headClass, $bodyClass, ?string $view = NULL) {
@@ -17,8 +17,8 @@ class HtmlDocument extends Document {
$this->activeView = $view;
}
public function getHead(): Head { return $this->head; }
public function getBody(): Body { return $this->body; }
public function getHead(): ?Head { return $this->head; }
public function getBody(): ?Body { return $this->body; }
public function getView() : ?View {
@@ -35,24 +35,11 @@ class HtmlDocument extends Document {
return new $view($this);
}
public function createScript($type, $src, $content = ""): Script {
$script = new Script($type, $src, $content);
if ($this->isCSPEnabled()) {
$script->setNonce($this->getCSPNonce());
}
return $script;
}
public function getRequestedView(): string {
return $this->activeView;
}
function getCode(array $params = []): string {
parent::getCode();
// generate body first, so we can modify head
$body = $this->body->getCode();
@@ -74,4 +61,11 @@ class HtmlDocument extends Document {
}
public function getTitle(): string {
if ($this->head !== null) {
return $this->head->getTitle();
}
return "Untitled Document";
}
}

View File

@@ -2,7 +2,12 @@
namespace Elements;
use Objects\CustomTwigFunctions;
use Objects\Router\DocumentRoute;
use Objects\Router\Router;
use Objects\Search\Searchable;
use Objects\Search\SearchQuery;
use Objects\Search\SearchResult;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
@@ -11,6 +16,8 @@ use Twig\Loader\FilesystemLoader;
class TemplateDocument extends Document {
const TEMPLATE_PATH = WEBROOT . '/core/Templates';
private string $templateName;
protected array $parameters;
private Environment $twigEnvironment;
@@ -19,14 +26,19 @@ class TemplateDocument extends Document {
public function __construct(Router $router, string $templateName, array $params = []) {
parent::__construct($router);
$this->title = "";
$this->title = "Untitled Document";
$this->templateName = $templateName;
$this->parameters = $params;
$this->twigLoader = new FilesystemLoader(WEBROOT . '/core/Templates');
$this->twigLoader = new FilesystemLoader(self::TEMPLATE_PATH);
$this->twigEnvironment = new Environment($this->twigLoader, [
'cache' => WEBROOT . '/core/Cache/Templates/',
'auto_reload' => true
]);
$this->twigEnvironment->addExtension(new CustomTwigFunctions());
}
public function getTitle(): string {
return $this->title;
}
protected function getTemplateName(): string {
@@ -37,8 +49,11 @@ class TemplateDocument extends Document {
}
protected function setTemplate(string $file) {
$this->templateName = $file;
}
public function getCode(array $params = []): string {
parent::getCode($params);
$this->loadParameters();
return $this->renderTemplate($this->templateName, $this->parameters);
}
@@ -48,33 +63,62 @@ class TemplateDocument extends Document {
$context = $this->getContext();
$session = $context->getSession();
$params["user"] = [
"lang" => $context->getLanguage()->getShortCode(),
"loggedIn" => $session !== null,
"session" => ($session ? [
"csrfToken" => $session->getCsrfToken()
] : null)
];
$settings = $this->getSettings();
$params["site"] = [
"name" => $settings->getSiteName(),
"baseUrl" => $settings->getBaseUrl(),
"registrationEnabled" => $settings->isRegistrationAllowed(),
"title" => $this->title,
"recaptcha" => [
"key" => $settings->isRecaptchaEnabled() ? $settings->getRecaptchaSiteKey() : null,
"enabled" => $settings->isRecaptchaEnabled(),
],
"csp" => [
"nonce" => $this->getCSPNonce(),
"enabled" => $this->isCSPEnabled()
]
];
$language = $context->getLanguage();
$urlParts = parse_url($this->getRouter()->getRequestedUri());
$params = array_replace_recursive([
"user" => [
"lang" => $language->getShortCode(),
"loggedIn" => $session !== null,
"session" => ($session ? [
"csrfToken" => $session->getCsrfToken()
] : null)
],
"site" => [
"name" => $settings->getSiteName(),
"url" => [
"base" => $settings->getBaseUrl(),
"path" => $urlParts["path"],
"query" => $urlParts["query"] ?? "",
"fragment" => $urlParts["fragment"] ?? ""
],
"lastModified" => date(L('Y-m-d H:i:s'), @filemtime(implode(DIRECTORY_SEPARATOR, [self::TEMPLATE_PATH, $name]))),
"registrationEnabled" => $settings->isRegistrationAllowed(),
"title" => $this->title,
"recaptcha" => [
"key" => $settings->isRecaptchaEnabled() ? $settings->getRecaptchaSiteKey() : null,
"enabled" => $settings->isRecaptchaEnabled(),
],
"csp" => [
"nonce" => $this->getCSPNonce(),
"enabled" => $this->isCSPEnabled()
]
]
], $params);
return $this->twigEnvironment->render($name, $params);
} catch (LoaderError | RuntimeError | SyntaxError $e) {
return "<b>Error rendering twig template: " . htmlspecialchars($e->getMessage()) . "</b>";
}
}
protected function loadView(string $class): array {
$view = new $class($this);
$view->loadParameters($this->parameters);
if ($view->getTitle()) {
$this->title = $view->getTitle();
}
return $this->parameters;
}
public function doSearch(SearchQuery $query, DocumentRoute $route): array {
$this->loadParameters();
$viewParams = $this->parameters["view"] ?? [];
$siteTitle = $this->parameters["site"]["title"] ?? $this->title;
$results = Searchable::searchArray($viewParams, $query);
return array_map(function ($res) use ($siteTitle, $route) {
return new SearchResult($route->getUrl(), $siteTitle, $res["text"]);
}, $results);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Elements;
abstract class TemplateView extends View {
protected array $keywords = [];
protected string $description = "";
public function __construct(TemplateDocument $document) {
parent::__construct($document);
$this->title = "";
}
protected function getParameters(): array {
return [];
}
public function loadParameters(array &$parameters) {
$siteParameters = [
"title" => $this->title,
"description" => $this->description,
"keywords" => $this->keywords
];
foreach ($siteParameters as $key => $value) {
if ($value) {
$parameters["site"][$key] = $value;
}
}
$parameters["view"] = $this->getParameters();
}
public function getCode(): string {
return $this->getDocument()->getCode();
}
}

View File

@@ -6,13 +6,11 @@ abstract class View extends StaticView {
private Document $document;
private bool $loadView;
protected bool $searchable;
protected string $title;
protected array $langModules;
public function __construct(Document $document, bool $loadView = true) {
$this->document = $document;
$this->searchable = false;
$this->title = "Untitled View";
$this->langModules = array();
$this->loadView = $loadView;
@@ -20,7 +18,6 @@ abstract class View extends StaticView {
public function getTitle(): string { return $this->title; }
public function getDocument(): Document { return $this->document; }
public function isSearchable(): bool { return $this->searchable; }
public function getSiteName(): string {
return $this->getDocument()->getSettings()->getSiteName();