Skip to content

Commit

Permalink
feat: set template base directory upon initialisation and allow custo…
Browse files Browse the repository at this point in the history
…m smarty security directories
  • Loading branch information
samerton committed Jan 12, 2025
1 parent 5d7274a commit 38ff084
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
8 changes: 4 additions & 4 deletions core/classes/Templates/SmartyTemplateBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ abstract class SmartyTemplateBase extends TemplateBase
* @param string $version
* @param string $nameless_version
* @param string $author
* @param bool $panelTemplate
* @param string $dir
*/
public function __construct(string $name, string $version, string $nameless_version, string $author, bool $panelTemplate = false)
public function __construct(string $name, string $version, string $nameless_version, string $author, string $dir)
{
parent::__construct($name, $version, $nameless_version, $author);
$this->_engine = new SmartyTemplateEngine($dir);

$this->_engine = new SmartyTemplateEngine($name, $panelTemplate);
parent::__construct($name, $version, $nameless_version, $author);
}
}
27 changes: 18 additions & 9 deletions core/classes/Templates/SmartyTemplateEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*/
class SmartyTemplateEngine extends TemplateEngine
{
protected Smarty_Security $_securityPolicy;
private Smarty $_smarty;

/**
* @param string $template Template name to load
* @param bool $panelTemplate Whether this is a panel template or not
* @param string $dir Path to template directory
* @throws SmartyException
*/
public function __construct(string $template, bool $panelTemplate = false)
public function __construct(string $dir)
{
$smarty = new Smarty();

Expand Down Expand Up @@ -50,17 +50,13 @@ public function __construct(string $template, bool $panelTemplate = false)
$smarty->enableSecurity($securityPolicy);

$smarty->setCompileDir(ROOT_PATH . '/cache/templates_c');

if ($panelTemplate) {
$smarty->setTemplateDir(ROOT_PATH . '/custom/panel_templates/' . $template);
} else {
$smarty->setTemplateDir(ROOT_PATH . '/custom/templates/' . $template);
}
$smarty->setTemplateDir($dir);

if (defined('PHPDEBUGBAR')) {
DebugBarHelper::getInstance()->addSmartyCollector($smarty);
}

$this->_securityPolicy = $securityPolicy;
$this->_smarty = $smarty;

parent::__construct();
Expand All @@ -84,4 +80,17 @@ public function clearCache(): void
{
$this->_smarty->clearAllCache();
}


/**
* Add an extra directory to the Smarty security policy
*
* @param string $dir Directory to add to policy
* @return void
*/
public function addSecurityPolicyDirectory(string $dir): void
{
$this->_securityPolicy->secure_dir = [...$this->_securityPolicy->secure_dir, $dir];
$this->_smarty->enableSecurity($this->_securityPolicy);
}
}
8 changes: 8 additions & 0 deletions core/classes/Templates/TemplateBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function __construct(string $name, string $version, string $nameless_vers
$this->_version = $version;
$this->_nameless_version = $nameless_version;
$this->_author = $author;

/*
* Temporary assignment to Smarty template engine for backwards compatibility for templates which extend TemplateBase
* This will be removed in 2.3.0 - breaking change!
*/
if (!isset($this->_engine)) {
$this->_engine = new SmartyTemplateEngine(ROOT_PATH . '/custom/templates/' . $name);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/classes/Templates/TwigTemplateBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
abstract class TwigTemplateBase extends TemplateBase
{
public function __construct(string $name, string $version, string $nameless_version, string $author)
public function __construct(string $name, string $version, string $nameless_version, string $author, string $dir)
{
parent::__construct($name, $version, $nameless_version, $author);

$this->_engine = new TwigTemplateEngine($name);
$this->_engine = new TwigTemplateEngine($dir);
}
}
6 changes: 3 additions & 3 deletions core/classes/Templates/TwigTemplateEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class TwigTemplateEngine extends TemplateEngine
private Environment $_twig;

/**
* @param string $template Template name to load.
* @param string $dir Path to template directory
*/
public function __construct(string $template)
public function __construct(string $dir)
{
$loader = new FilesystemLoader(ROOT_PATH . '/custom/templates/' . $template);
$loader = new FilesystemLoader($dir);
$twig = new Environment($loader, [
'cache' => ROOT_PATH . '/cache/twig',
]);
Expand Down
2 changes: 1 addition & 1 deletion custom/panel_templates/Default/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(Language $language)
'2.2.0', // Template version
'2.2.0', // Nameless version template is made for
'<a href="https://coldfiredzn.com" target="_blank">Coldfire</a>', // Author, you can use HTML here
true, // Set to true for panel templates
__DIR__, // Specify the path to the template
);

$this->assets()->include([
Expand Down
2 changes: 1 addition & 1 deletion custom/templates/DefaultRevamp/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(Cache $cache, Language $language, User $user, Pages

$template['path'] = (defined('CONFIG_PATH') ? CONFIG_PATH : '') . '/custom/templates/' . $template['name'] . '/';

parent::__construct($template['name'], $template['version'], $template['nl_version'], $template['author']);
parent::__construct($template['name'], $template['version'], $template['nl_version'], $template['author'], __DIR__);

$this->_settings = ROOT_PATH . '/custom/templates/DefaultRevamp/template_settings/settings.php';

Expand Down

0 comments on commit 38ff084

Please sign in to comment.