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