web-base/Core/API/TemplateAPI.class.php

90 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2021-12-08 16:53:43 +01:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2021-12-08 16:53:43 +01:00
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2021-12-08 16:53:43 +01:00
abstract class TemplateAPI extends Request {
2022-06-20 19:52:31 +02:00
function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
2021-12-08 16:53:43 +01:00
$this->isPublic = false; // internal API
}
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Template {
2021-12-08 16:53:43 +01:00
2022-11-18 18:06:46 +01:00
use Core\API\Parameter\ArrayType;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\API\TemplateAPI;
use Core\Objects\Context;
2021-12-08 16:53:43 +01:00
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
class Render extends TemplateAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, [
2021-12-08 16:53:43 +01:00
"file" => new StringType("file"),
"parameters" => new ArrayType("parameters", Parameter::TYPE_MIXED, false, true, [])
]);
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-12-08 16:53:43 +01:00
$templateFile = $this->getParam("file");
$parameters = $this->getParam("parameters");
$extension = pathinfo($templateFile, PATHINFO_EXTENSION);
$allowedExtensions = ["html", "twig"];
if (!in_array($extension, $allowedExtensions)) {
return $this->createError("Invalid template file extension. Allowed: " . implode(",", $allowedExtensions));
}
2022-12-04 13:21:40 +01:00
$templateCache = WEBROOT . "/Site/Cache/Templates/";
2022-11-19 01:15:34 +01:00
$baseDirs = ["Site", "Core"];
$valid = false;
foreach ($baseDirs as $baseDir) {
2022-11-27 15:58:44 +01:00
$templateDir = realpath(implode("/", [WEBROOT, $baseDir, "Templates"]));
if ($templateDir) {
$path = realpath(implode("/", [$templateDir, $templateFile]));
if ($path && is_file($path)) {
$valid = true;
break;
}
2022-11-19 01:15:34 +01:00
}
2021-12-08 16:53:43 +01:00
}
2022-11-19 01:15:34 +01:00
if (!$valid) {
return $this->createError("Template file not found or not inside template directory");
}
2022-11-27 15:58:44 +01:00
$twigLoader = new FilesystemLoader($templateDir);
2021-12-08 16:53:43 +01:00
$twigEnvironment = new Environment($twigLoader, [
'cache' => $templateCache,
'auto_reload' => true
]);
try {
$this->result["html"] = $twigEnvironment->render($templateFile, $parameters);
} catch (LoaderError | RuntimeError | SyntaxError | \RuntimeException $e) {
2021-12-08 16:53:43 +01:00
return $this->createError("Error rendering twig template: " . $e->getMessage());
}
2022-03-08 11:50:18 +01:00
return true;
2021-12-08 16:53:43 +01:00
}
2024-04-23 12:14:28 +02:00
public static function getDescription(): string {
return "Renders a given template with a set of parameters. This API is for internal use only";
}
2021-12-08 16:53:43 +01:00
2024-04-23 12:14:28 +02:00
public static function hasConfigurablePermissions(): bool {
return false;
}
}
2021-12-08 16:53:43 +01:00
}