2018-02-03 11:11:00 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 16:18:46 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-02-03 11:11:00 +01:00
|
|
|
*/
|
2020-02-09 16:18:46 +01:00
|
|
|
|
2018-02-03 11:11:00 +01:00
|
|
|
namespace Friendica\Render;
|
|
|
|
|
2018-12-26 07:06:24 +01:00
|
|
|
use Friendica\Core\Hook;
|
2020-01-04 23:42:01 +01:00
|
|
|
use Friendica\DI;
|
2021-10-30 01:21:07 +02:00
|
|
|
use Friendica\Network\HTTPException\ServiceUnavailableException;
|
2020-05-18 07:18:41 +02:00
|
|
|
use Friendica\Util\Strings;
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2018-02-03 14:52:43 +01:00
|
|
|
/**
|
2020-05-18 07:18:41 +02:00
|
|
|
* Smarty implementation of the Friendica template abstraction
|
2018-02-03 14:52:43 +01:00
|
|
|
*/
|
2020-05-18 07:18:41 +02:00
|
|
|
final class FriendicaSmartyEngine extends TemplateEngine
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
|
|
|
static $name = "smarty3";
|
|
|
|
|
2020-05-18 07:18:41 +02: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
|
|
|
{
|
2020-05-18 07:18:41 +02:00
|
|
|
$this->theme = $theme;
|
|
|
|
$this->theme_info = $theme_info;
|
|
|
|
$this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
|
|
|
|
|
|
|
|
if (!is_writable(DI::basePath() . '/view/smarty3')) {
|
2020-05-19 00:10:21 +02:00
|
|
|
$admin_message = DI::l10n()->t('The folder view/smarty3/ must be writable by webserver.');
|
|
|
|
DI::logger()->critical($admin_message);
|
2021-11-04 21:29:59 +01:00
|
|
|
$message = DI::app()->isSiteAdmin() ?
|
2020-05-19 00:10:21 +02:00
|
|
|
$admin_message :
|
|
|
|
DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
|
2021-10-30 01:21:07 +02:00
|
|
|
throw new ServiceUnavailableException($message);
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 07:19:30 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function testInstall(array &$errors = null)
|
|
|
|
{
|
|
|
|
$this->smarty->testInstall($errors);
|
|
|
|
}
|
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function replaceMacros(string $template, array $vars)
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2020-05-18 07:18:41 +02: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 = [
|
2020-05-18 07:18:41 +02:00
|
|
|
'template' => basename($this->smarty->filename),
|
|
|
|
'vars' => $vars
|
2018-02-03 11:11:00 +01:00
|
|
|
];
|
2020-05-18 07:18:41 +02:00
|
|
|
Hook::callAll('template_vars', $arr);
|
|
|
|
$vars = $arr['vars'];
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2020-06-13 04:06:09 +02:00
|
|
|
$this->smarty->clearAllAssign();
|
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
foreach ($vars as $key => $value) {
|
2018-02-03 11:11:00 +01:00
|
|
|
if ($key[0] === '$') {
|
|
|
|
$key = substr($key, 1);
|
|
|
|
}
|
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
$this->smarty->assign($key, $value);
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
2020-05-18 07:18:41 +02:00
|
|
|
|
|
|
|
return $this->smarty->fetch($template);
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateFile(string $file, string $subDir = '')
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2018-02-03 14:52:43 +01: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 14:52:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-26 15:45:25 +02:00
|
|
|
$root = DI::basePath() . '/' . $subDir;
|
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
$filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
|
2018-02-03 14:52:43 +01:00
|
|
|
|
2020-05-18 07:18:41 +02: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 14:52:43 +01:00
|
|
|
} elseif (file_exists("{$root}/$filename")) {
|
|
|
|
$template_file = "{$root}/$filename";
|
|
|
|
} else {
|
|
|
|
$template_file = "{$root}view/$filename";
|
|
|
|
}
|
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
$this->smarty->filename = $template_file;
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2020-05-18 07:18:41 +02:00
|
|
|
return self::FILE_PREFIX . $template_file;
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
}
|