]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
Improved http error handling
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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                 $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
50
51                 if (!is_writable(DI::basePath() . '/view/smarty3')) {
52                         $admin_message = DI::l10n()->t('The folder view/smarty3/ must be writable by webserver.');
53                         DI::logger()->critical($admin_message);
54                         $message = is_site_admin() ?
55                                 $admin_message :
56                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
57                         throw new ServiceUnavailableException($message);
58                 }
59         }
60
61         /**
62          * @inheritDoc
63          */
64         public function testInstall(array &$errors = null)
65         {
66                 $this->smarty->testInstall($errors);
67         }
68
69         /**
70          * @inheritDoc
71          */
72         public function replaceMacros(string $template, array $vars)
73         {
74                 if (!Strings::startsWith($template, self::FILE_PREFIX)) {
75                         $template = self::STRING_PREFIX . $template;
76                 }
77
78                 // "middleware": inject variables into templates
79                 $arr = [
80                         'template' => basename($this->smarty->filename),
81                         'vars' => $vars
82                 ];
83                 Hook::callAll('template_vars', $arr);
84                 $vars = $arr['vars'];
85
86                 $this->smarty->clearAllAssign();
87
88                 foreach ($vars as $key => $value) {
89                         if ($key[0] === '$') {
90                                 $key = substr($key, 1);
91                         }
92
93                         $this->smarty->assign($key, $value);
94                 }
95
96                 return $this->smarty->fetch($template);
97         }
98
99         /**
100          * @inheritDoc
101          */
102         public function getTemplateFile(string $file, string $subDir = '')
103         {
104                 // Make sure $root ends with a slash /
105                 if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
106                         $subDir = $subDir . '/';
107                 }
108
109                 $root = DI::basePath() . '/' . $subDir;
110
111                 $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
112
113                 if (file_exists("{$root}view/theme/$this->theme/$filename")) {
114                         $template_file = "{$root}view/theme/$this->theme/$filename";
115                 } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
116                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
117                 } elseif (file_exists("{$root}/$filename")) {
118                         $template_file = "{$root}/$filename";
119                 } else {
120                         $template_file = "{$root}view/$filename";
121                 }
122
123                 $this->smarty->filename = $template_file;
124
125                 return self::FILE_PREFIX . $template_file;
126         }
127 }