]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
spelling: preview
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Render;
23
24 use Friendica\Core\Hook;
25 use Friendica\DI;
26 use Friendica\Network\HTTPException\ServiceUnavailableException;
27 use Friendica\Util\Strings;
28
29 /**
30  * Smarty implementation of the Friendica template abstraction
31  */
32 final class FriendicaSmartyEngine extends TemplateEngine
33 {
34         static $name = 'smarty3';
35
36         const FILE_PREFIX = 'file:';
37         const STRING_PREFIX = 'string:';
38
39         /** @var FriendicaSmarty */
40         private $smarty;
41
42         /**
43          * @inheritDoc
44          */
45         public function __construct(string $theme, array $theme_info)
46         {
47                 $this->theme      = $theme;
48                 $this->theme_info = $theme_info;
49
50                 $work_dir     = DI::config()->get('smarty3', 'config_dir');
51                 $use_sub_dirs = DI::config()->get('smarty3', 'use_sub_dirs');
52
53                 $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info, $work_dir, $use_sub_dirs);
54
55                 if (!is_writable($work_dir)) {
56                         $admin_message = DI::l10n()->t('The folder %s must be writable by webserver.', $work_dir);
57                         DI::logger()->critical($admin_message);
58                         $message = DI::app()->isSiteAdmin() ?
59                                 $admin_message :
60                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
61                         throw new ServiceUnavailableException($message);
62                 }
63         }
64
65         /**
66          * @inheritDoc
67          */
68         public function testInstall(array &$errors = null)
69         {
70                 $this->smarty->testInstall($errors);
71         }
72
73         /**
74          * @inheritDoc
75          */
76         public function replaceMacros(string $template, array $vars): string
77         {
78                 if (!Strings::startsWith($template, self::FILE_PREFIX)) {
79                         $template = self::STRING_PREFIX . $template;
80                 }
81
82                 // "middleware": inject variables into templates
83                 $arr = [
84                         'template' => basename($this->smarty->filename ?? ''),
85                         'vars' => $vars
86                 ];
87                 Hook::callAll('template_vars', $arr);
88                 $vars = $arr['vars'];
89
90                 $this->smarty->clearAllAssign();
91
92                 foreach ($vars as $key => $value) {
93                         if ($key[0] === '$') {
94                                 $key = substr($key, 1);
95                         }
96
97                         $this->smarty->assign($key, $value);
98                 }
99
100                 return $this->smarty->fetch($template);
101         }
102
103         /**
104          * @inheritDoc
105          */
106         public function getTemplateFile(string $file, string $subDir = '')
107         {
108                 // Make sure $root ends with a slash /
109                 if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
110                         $subDir = $subDir . '/';
111                 }
112
113                 $root = DI::basePath() . '/' . $subDir;
114
115                 $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
116
117                 if (file_exists("{$root}view/theme/$this->theme/$filename")) {
118                         $template_file = "{$root}view/theme/$this->theme/$filename";
119                 } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
120                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
121                 } elseif (file_exists("{$root}/$filename")) {
122                         $template_file = "{$root}/$filename";
123                 } else {
124                         $template_file = "{$root}view/$filename";
125                 }
126
127                 $this->smarty->filename = $template_file;
128
129                 return self::FILE_PREFIX . $template_file;
130         }
131 }