2018-02-03 11:11:00 +01:00
|
|
|
<?php
|
2024-08-24 15:27:00 +02:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-02-09 16:18:46 +01:00
|
|
|
|
2018-02-03 11:11:00 +01:00
|
|
|
namespace Friendica\Render;
|
|
|
|
|
2018-12-26 01:06:24 -05:00
|
|
|
use Friendica\Core\Hook;
|
2020-01-04 23:42:01 +01:00
|
|
|
use Friendica\DI;
|
2021-10-29 23:21:07 +00:00
|
|
|
use Friendica\Network\HTTPException\ServiceUnavailableException;
|
2020-05-18 01:18:41 -04:00
|
|
|
use Friendica\Util\Strings;
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2018-02-03 08:52:43 -05:00
|
|
|
/**
|
2020-05-18 01:18:41 -04:00
|
|
|
* Smarty implementation of the Friendica template abstraction
|
2018-02-03 08:52:43 -05:00
|
|
|
*/
|
2020-05-18 01:18:41 -04:00
|
|
|
final class FriendicaSmartyEngine extends TemplateEngine
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2022-08-13 23:24:03 +02:00
|
|
|
static $name = 'smarty3';
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
const FILE_PREFIX = 'file:';
|
|
|
|
const STRING_PREFIX = 'string:';
|
|
|
|
|
|
|
|
/** @var FriendicaSmarty */
|
|
|
|
private $smarty;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function __construct(string $theme, array $theme_info)
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2022-09-08 06:21:16 +00:00
|
|
|
$this->theme = $theme;
|
2020-05-18 01:18:41 -04:00
|
|
|
$this->theme_info = $theme_info;
|
2022-09-08 06:21:16 +00:00
|
|
|
|
|
|
|
$work_dir = DI::config()->get('smarty3', 'config_dir');
|
|
|
|
$use_sub_dirs = DI::config()->get('smarty3', 'use_sub_dirs');
|
|
|
|
|
|
|
|
$this->smarty = new FriendicaSmarty($this->theme, $this->theme_info, $work_dir, $use_sub_dirs);
|
2020-05-18 01:18:41 -04:00
|
|
|
|
2022-08-05 15:12:22 +02:00
|
|
|
if (!is_writable($work_dir)) {
|
|
|
|
$admin_message = DI::l10n()->t('The folder %s must be writable by webserver.', $work_dir);
|
2020-05-18 18:10:21 -04:00
|
|
|
DI::logger()->critical($admin_message);
|
2024-05-14 08:37:10 +00:00
|
|
|
$message = DI::userSession()->isSiteAdmin() ?
|
2020-05-18 18:10:21 -04:00
|
|
|
$admin_message :
|
|
|
|
DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
|
2021-10-29 23:21:07 +00:00
|
|
|
throw new ServiceUnavailableException($message);
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 01:19:30 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function testInstall(array &$errors = null)
|
|
|
|
{
|
|
|
|
$this->smarty->testInstall($errors);
|
|
|
|
}
|
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2022-06-20 00:51:59 +02:00
|
|
|
public function replaceMacros(string $template, array $vars): string
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2020-05-18 01:18:41 -04:00
|
|
|
if (!Strings::startsWith($template, self::FILE_PREFIX)) {
|
|
|
|
$template = self::STRING_PREFIX . $template;
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// "middleware": inject variables into templates
|
|
|
|
$arr = [
|
2022-11-19 19:10:02 -05:00
|
|
|
'template' => basename($this->smarty->filename ?? ''),
|
2020-05-18 01:18:41 -04:00
|
|
|
'vars' => $vars
|
2018-02-03 11:11:00 +01:00
|
|
|
];
|
2020-05-18 01:18:41 -04:00
|
|
|
Hook::callAll('template_vars', $arr);
|
|
|
|
$vars = $arr['vars'];
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2020-06-12 22:06:09 -04:00
|
|
|
$this->smarty->clearAllAssign();
|
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
foreach ($vars as $key => $value) {
|
2018-02-03 11:11:00 +01:00
|
|
|
if ($key[0] === '$') {
|
|
|
|
$key = substr($key, 1);
|
|
|
|
}
|
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
$this->smarty->assign($key, $value);
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
2020-05-18 01:18:41 -04:00
|
|
|
|
|
|
|
return $this->smarty->fetch($template);
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateFile(string $file, string $subDir = '')
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2018-02-03 08:52:43 -05:00
|
|
|
// Make sure $root ends with a slash /
|
2020-04-26 15:45:25 +02:00
|
|
|
if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
|
|
|
|
$subDir = $subDir . '/';
|
2018-02-03 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
2020-04-26 15:45:25 +02:00
|
|
|
$root = DI::basePath() . '/' . $subDir;
|
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
$filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
|
2018-02-03 08:52:43 -05:00
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
if (file_exists("{$root}view/theme/$this->theme/$filename")) {
|
|
|
|
$template_file = "{$root}view/theme/$this->theme/$filename";
|
|
|
|
} elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
|
|
|
|
$template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
|
2018-02-03 08:52:43 -05:00
|
|
|
} elseif (file_exists("{$root}/$filename")) {
|
|
|
|
$template_file = "{$root}/$filename";
|
|
|
|
} else {
|
|
|
|
$template_file = "{$root}view/$filename";
|
|
|
|
}
|
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
$this->smarty->filename = $template_file;
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2020-05-18 01:18:41 -04:00
|
|
|
return self::FILE_PREFIX . $template_file;
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
}
|