]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
Merge pull request #8263 from annando/remote-follow
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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
27 /**
28  * Smarty implementation of the Friendica template engine interface
29  */
30 class FriendicaSmartyEngine implements ITemplateEngine
31 {
32         static $name = "smarty3";
33
34         public function __construct()
35         {
36                 if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
37                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
38                         exit();
39                 }
40         }
41
42         // ITemplateEngine interface
43         public function replaceMacros($s, $r)
44         {
45                 $template = '';
46                 if (gettype($s) === 'string') {
47                         $template = $s;
48                         $s = new FriendicaSmarty();
49                 }
50
51                 $r['$APP'] = DI::app();
52
53                 // "middleware": inject variables into templates
54                 $arr = [
55                         "template" => basename($s->filename),
56                         "vars" => $r
57                 ];
58                 Hook::callAll("template_vars", $arr);
59                 $r = $arr['vars'];
60
61                 foreach ($r as $key => $value) {
62                         if ($key[0] === '$') {
63                                 $key = substr($key, 1);
64                         }
65
66                         $s->assign($key, $value);
67                 }
68                 return $s->parsed($template);
69         }
70
71         public function getTemplateFile($file, $root = '')
72         {
73                 $a = DI::app();
74                 $template = new FriendicaSmarty();
75
76                 // Make sure $root ends with a slash /
77                 if ($root !== '' && substr($root, -1, 1) !== '/') {
78                         $root = $root . '/';
79                 }
80
81                 $theme = $a->getCurrentTheme();
82                 $filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
83
84                 if (file_exists("{$root}view/theme/$theme/$filename")) {
85                         $template_file = "{$root}view/theme/$theme/$filename";
86                 } elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
87                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
88                 } elseif (file_exists("{$root}/$filename")) {
89                         $template_file = "{$root}/$filename";
90                 } else {
91                         $template_file = "{$root}view/$filename";
92                 }
93
94                 $template->filename = $template_file;
95
96                 return $template;
97         }
98 }