]> git.mxchange.org Git - friendica.git/blobdiff - src/Render/FriendicaSmartyEngine.php
spelling: one
[friendica.git] / src / Render / FriendicaSmartyEngine.php
index 668b91ea5b5745c409d0f8b3a7919a4e16d73d44..5d7ed6c0c94dbb353e01f07c8d16925c5543421c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,6 +23,7 @@ namespace Friendica\Render;
 
 use Friendica\Core\Hook;
 use Friendica\DI;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Util\Strings;
 
 /**
@@ -30,7 +31,7 @@ use Friendica\Util\Strings;
  */
 final class FriendicaSmartyEngine extends TemplateEngine
 {
-       static $name = "smarty3";
+       static $name = 'smarty3';
 
        const FILE_PREFIX = 'file:';
        const STRING_PREFIX = 'string:';
@@ -43,20 +44,36 @@ final class FriendicaSmartyEngine extends TemplateEngine
         */
        public function __construct(string $theme, array $theme_info)
        {
-               $this->theme = $theme;
+               $this->theme      = $theme;
                $this->theme_info = $theme_info;
-               $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
 
-               if (!is_writable(DI::basePath() . '/view/smarty3')) {
-                       echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
-                       exit();
+               $work_dir     = DI::config()->get('smarty3', 'config_dir');
+               $use_sub_dirs = DI::config()->get('smarty3', 'use_sub_dirs');
+
+               $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info, $work_dir, $use_sub_dirs);
+
+               if (!is_writable($work_dir)) {
+                       $admin_message = DI::l10n()->t('The folder %s must be writable by webserver.', $work_dir);
+                       DI::logger()->critical($admin_message);
+                       $message = DI::app()->isSiteAdmin() ?
+                               $admin_message :
+                               DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
+                       throw new ServiceUnavailableException($message);
                }
        }
 
        /**
         * @inheritDoc
         */
-       public function replaceMacros(string $template, array $vars)
+       public function testInstall(array &$errors = null)
+       {
+               $this->smarty->testInstall($errors);
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function replaceMacros(string $template, array $vars): string
        {
                if (!Strings::startsWith($template, self::FILE_PREFIX)) {
                        $template = self::STRING_PREFIX . $template;
@@ -64,12 +81,14 @@ final class FriendicaSmartyEngine extends TemplateEngine
 
                // "middleware": inject variables into templates
                $arr = [
-                       'template' => basename($this->smarty->filename),
+                       'template' => basename($this->smarty->filename ?? ''),
                        'vars' => $vars
                ];
                Hook::callAll('template_vars', $arr);
                $vars = $arr['vars'];
 
+               $this->smarty->clearAllAssign();
+
                foreach ($vars as $key => $value) {
                        if ($key[0] === '$') {
                                $key = substr($key, 1);