-<?php\r
-/**\r
- * @file src/Render/FriendicaSmartyEngine.php\r
- */\r
-namespace Friendica\Render;\r
-\r
-use Friendica\Core\Addon;\r
-\r
-class FriendicaSmartyEngine implements ITemplateEngine\r
-{\r
- static $name = "smarty3";\r
-\r
- public function __construct()\r
- {\r
- if (!is_writable('view/smarty3/')) {\r
- echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";\r
- killme();\r
- }\r
- }\r
-\r
- // ITemplateEngine interface\r
- public function replaceMacros($s, $r)\r
- {\r
- $template = '';\r
- if (gettype($s) === 'string') {\r
- $template = $s;\r
- $s = new FriendicaSmarty();\r
- }\r
-\r
- $r['$APP'] = get_app();\r
-\r
- // "middleware": inject variables into templates\r
- $arr = [\r
- "template" => basename($s->filename),\r
- "vars" => $r\r
- ];\r
- Addon::callHooks("template_vars", $arr);\r
- $r = $arr['vars'];\r
-\r
- foreach ($r as $key => $value) {\r
- if ($key[0] === '$') {\r
- $key = substr($key, 1);\r
- }\r
-\r
- $s->assign($key, $value);\r
- }\r
- return $s->parsed($template);\r
- }\r
-\r
- public function getTemplateFile($file, $root = '')\r
- {\r
- $a = get_app();\r
- $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root);\r
- $template = new FriendicaSmarty();\r
- $template->filename = $template_file;\r
-\r
- return $template;\r
- }\r
-}\r
+<?php
+/**
+ * @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/>.
+ *
+ */
+
+namespace Friendica\Render;
+
+use Friendica\Core\Hook;
+use Friendica\DI;
+use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Util\Strings;
+
+/**
+ * Smarty implementation of the Friendica template abstraction
+ */
+final class FriendicaSmartyEngine extends TemplateEngine
+{
+ static $name = "smarty3";
+
+ const FILE_PREFIX = 'file:';
+ const STRING_PREFIX = 'string:';
+
+ /** @var FriendicaSmarty */
+ private $smarty;
+
+ /**
+ * @inheritDoc
+ */
+ public function __construct(string $theme, array $theme_info)
+ {
+ $this->theme = $theme;
+ $this->theme_info = $theme_info;
+ $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
+
+ if (!is_writable(DI::basePath() . '/view/smarty3')) {
+ $admin_message = DI::l10n()->t('The folder view/smarty3/ must be writable by webserver.');
+ DI::logger()->critical($admin_message);
+ $message = is_site_admin() ?
+ $admin_message :
+ DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
+ throw new InternalServerErrorException($message);
+ }
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function testInstall(array &$errors = null)
+ {
+ $this->smarty->testInstall($errors);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function replaceMacros(string $template, array $vars)
+ {
+ if (!Strings::startsWith($template, self::FILE_PREFIX)) {
+ $template = self::STRING_PREFIX . $template;
+ }
+
+ // "middleware": inject variables into templates
+ $arr = [
+ 'template' => basename($this->smarty->filename),
+ 'vars' => $vars
+ ];
+ Hook::callAll('template_vars', $arr);
+ $vars = $arr['vars'];
+
+ $this->smarty->clearAllAssign();
+
+ foreach ($vars as $key => $value) {
+ if ($key[0] === '$') {
+ $key = substr($key, 1);
+ }
+
+ $this->smarty->assign($key, $value);
+ }
+
+ return $this->smarty->fetch($template);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getTemplateFile(string $file, string $subDir = '')
+ {
+ // Make sure $root ends with a slash /
+ if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
+ $subDir = $subDir . '/';
+ }
+
+ $root = DI::basePath() . '/' . $subDir;
+
+ $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
+
+ 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);
+ } elseif (file_exists("{$root}/$filename")) {
+ $template_file = "{$root}/$filename";
+ } else {
+ $template_file = "{$root}view/$filename";
+ }
+
+ $this->smarty->filename = $template_file;
+
+ return self::FILE_PREFIX . $template_file;
+ }
+}