]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git] / src / Core / Renderer.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\Core;
23
24 use Exception;
25 use Friendica\DI;
26 use Friendica\Network\HTTPException\InternalServerErrorException;
27 use Friendica\Render\TemplateEngine;
28
29 /**
30  * This class handles Renderer related functions.
31  */
32 class Renderer
33 {
34         /**
35          * An array of registered template engines ('name'=>'class name')
36          */
37         public static $template_engines = [];
38
39         /**
40          * An array of instanced template engines ('name'=>'instance')
41          */
42         public static $template_engine_instance = [];
43
44         /**
45          * An array for all theme-controllable parameters
46          *
47          * Mostly unimplemented yet. Only options 'template_engine' and
48          * beyond are used.
49          */
50         public static $theme = [
51                 'sourcename' => '',
52                 'videowidth' => 425,
53                 'videoheight' => 350,
54                 'stylesheet' => '',
55                 'template_engine' => 'smarty3',
56         ];
57
58         private static $ldelim = [
59                 'internal' => '',
60                 'smarty3' => '{{'
61         ];
62         private static $rdelim = [
63                 'internal' => '',
64                 'smarty3' => '}}'
65         ];
66
67         /**
68          * Returns the rendered template output from the template string and variables
69          *
70          * @param string $template
71          * @param array  $vars
72          * @return string
73          * @throws InternalServerErrorException
74          */
75         public static function replaceMacros(string $template, array $vars = [])
76         {
77                 $stamp1 = microtime(true);
78
79                 // pass $baseurl to all templates if it isn't set
80                 $vars = array_merge(['$baseurl' => DI::baseUrl()->get(), '$APP' => DI::app()], $vars);
81
82                 $t = self::getTemplateEngine();
83
84                 try {
85                         $output = $t->replaceMacros($template, $vars);
86                 } catch (Exception $e) {
87                         DI::logger()->critical($e->getMessage(), ['template' => $template, 'vars' => $vars]);
88                         $message = is_site_admin() ?
89                                 $e->getMessage() :
90                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
91                         throw new InternalServerErrorException($message);
92                 }
93
94                 DI::profiler()->saveTimestamp($stamp1, "rendering");
95
96                 return $output;
97         }
98
99         /**
100          * Load a given template $s
101          *
102          * @param string $file   Template to load.
103          * @param string $subDir Subdirectory (Optional)
104          *
105          * @return string template.
106          * @throws InternalServerErrorException
107          */
108         public static function getMarkupTemplate($file, $subDir = '')
109         {
110                 $stamp1 = microtime(true);
111                 $t = self::getTemplateEngine();
112
113                 try {
114                         $template = $t->getTemplateFile($file, $subDir);
115                 } catch (Exception $e) {
116                         DI::logger()->critical($e->getMessage(), ['file' => $file, 'subDir' => $subDir]);
117                         $message = is_site_admin() ?
118                                 $e->getMessage() :
119                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
120                         throw new InternalServerErrorException($message);
121                 }
122
123                 DI::profiler()->saveTimestamp($stamp1, "file");
124
125                 return $template;
126         }
127
128         /**
129          * Register template engine class
130          *
131          * @param string $class
132          * @throws InternalServerErrorException
133          */
134         public static function registerTemplateEngine($class)
135         {
136                 $v = get_class_vars($class);
137
138                 if (!empty($v['name'])) {
139                         $name = $v['name'];
140                         self::$template_engines[$name] = $class;
141                 } else {
142                         $admin_message = DI::l10n()->t('template engine cannot be registered without a name.');
143                         DI::logger()->critical($admin_message, ['class' => $class]);
144                         $message = is_site_admin() ?
145                                 $admin_message :
146                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
147                         throw new InternalServerErrorException($message);
148                 }
149         }
150
151         /**
152          * Return template engine instance.
153          *
154          * If $name is not defined, return engine defined by theme,
155          * or default
156          *
157          * @return TemplateEngine Template Engine instance
158          * @throws InternalServerErrorException
159          */
160         public static function getTemplateEngine()
161         {
162                 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
163
164                 if (isset(self::$template_engines[$template_engine])) {
165                         if (isset(self::$template_engine_instance[$template_engine])) {
166                                 return self::$template_engine_instance[$template_engine];
167                         } else {
168                                 $a = DI::app();
169                                 $class = self::$template_engines[$template_engine];
170                                 $obj = new $class($a->getCurrentTheme(), $a->theme_info);
171                                 self::$template_engine_instance[$template_engine] = $obj;
172                                 return $obj;
173                         }
174                 }
175
176                 $admin_message = DI::l10n()->t('template engine is not registered!');
177                 DI::logger()->critical($admin_message, ['template_engine' => $template_engine]);
178                 $message = is_site_admin() ?
179                         $admin_message :
180                         DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
181                 throw new InternalServerErrorException($message);
182         }
183
184         /**
185          * Returns the active template engine.
186          *
187          * @return string the active template engine
188          */
189         public static function getActiveTemplateEngine()
190         {
191                 return self::$theme['template_engine'];
192         }
193
194         /**
195          * sets the active template engine
196          *
197          * @param string $engine the template engine (default is Smarty3)
198          */
199         public static function setActiveTemplateEngine($engine = 'smarty3')
200         {
201                 self::$theme['template_engine'] = $engine;
202         }
203
204         /**
205          * Gets the right delimiter for a template engine
206          *
207          * Currently:
208          * Internal = ''
209          * Smarty3 = '{{'
210          *
211          * @param string $engine The template engine (default is Smarty3)
212          *
213          * @return string the right delimiter
214          */
215         public static function getTemplateLeftDelimiter($engine = 'smarty3')
216         {
217                 return self::$ldelim[$engine];
218         }
219
220         /**
221          * Gets the left delimiter for a template engine
222          *
223          * Currently:
224          * Internal = ''
225          * Smarty3 = '}}'
226          *
227          * @param string $engine The template engine (default is Smarty3)
228          *
229          * @return string the left delimiter
230          */
231         public static function getTemplateRightDelimiter($engine = 'smarty3')
232         {
233                 return self::$rdelim[$engine];
234         }
235 }