2018-02-03 11:11:00 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-02-09 16:18:46 +01:00
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @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;
|
2018-02-03 11:11:00 +01:00
|
|
|
|
2018-02-03 14:52:43 +01:00
|
|
|
/**
|
|
|
|
* Smarty implementation of the Friendica template engine interface
|
|
|
|
*/
|
2018-02-03 11:11:00 +01:00
|
|
|
class FriendicaSmartyEngine implements ITemplateEngine
|
|
|
|
{
|
|
|
|
static $name = "smarty3";
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-03-10 05:21:19 +01:00
|
|
|
if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
|
2018-02-03 11:11:00 +01:00
|
|
|
echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
|
2018-12-26 06:40:12 +01:00
|
|
|
exit();
|
2018-02-03 11:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ITemplateEngine interface
|
|
|
|
public function replaceMacros($s, $r)
|
|
|
|
{
|
|
|
|
$template = '';
|
|
|
|
if (gettype($s) === 'string') {
|
|
|
|
$template = $s;
|
|
|
|
$s = new FriendicaSmarty();
|
|
|
|
}
|
|
|
|
|
2020-01-04 23:42:01 +01:00
|
|
|
$r['$APP'] = DI::app();
|
2018-02-03 11:11:00 +01:00
|
|
|
|
|
|
|
// "middleware": inject variables into templates
|
|
|
|
$arr = [
|
|
|
|
"template" => basename($s->filename),
|
|
|
|
"vars" => $r
|
|
|
|
];
|
2018-12-26 07:06:24 +01:00
|
|
|
Hook::callAll("template_vars", $arr);
|
2018-02-03 11:11:00 +01:00
|
|
|
$r = $arr['vars'];
|
|
|
|
|
|
|
|
foreach ($r as $key => $value) {
|
|
|
|
if ($key[0] === '$') {
|
|
|
|
$key = substr($key, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$s->assign($key, $value);
|
|
|
|
}
|
|
|
|
return $s->parsed($template);
|
|
|
|
}
|
|
|
|
|
2020-04-26 15:45:25 +02:00
|
|
|
public function getTemplateFile($file, $subDir = '')
|
2018-02-03 11:11:00 +01:00
|
|
|
{
|
2020-01-04 23:42:01 +01:00
|
|
|
$a = DI::app();
|
2018-02-03 11:11:00 +01:00
|
|
|
$template = new FriendicaSmarty();
|
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;
|
|
|
|
|
2018-04-29 00:37:04 +02:00
|
|
|
$theme = $a->getCurrentTheme();
|
2018-02-03 14:52:43 +01:00
|
|
|
$filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
|
|
|
|
|
|
|
|
if (file_exists("{$root}view/theme/$theme/$filename")) {
|
|
|
|
$template_file = "{$root}view/theme/$theme/$filename";
|
2018-11-30 15:06:22 +01:00
|
|
|
} elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
|
2018-02-03 14:52:43 +01:00
|
|
|
$template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
|
|
|
|
} elseif (file_exists("{$root}/$filename")) {
|
|
|
|
$template_file = "{$root}/$filename";
|
|
|
|
} else {
|
|
|
|
$template_file = "{$root}view/$filename";
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:11:00 +01:00
|
|
|
$template->filename = $template_file;
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
}
|