From f574528338f8a7c1136432dba05abadf156b6c47 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 1 Feb 2018 13:31:03 -0500 Subject: [PATCH 1/5] Add FriendicaSmarty and FriendicaSmartyEngine classes in src --- src/Render/FriendicaSmarty.php | 54 ++++++++++++++++++++++++++ src/Render/FriendicaSmartyEngine.php | 57 ++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/Render/FriendicaSmarty.php create mode 100644 src/Render/FriendicaSmartyEngine.php diff --git a/src/Render/FriendicaSmarty.php b/src/Render/FriendicaSmarty.php new file mode 100644 index 000000000..b86357612 --- /dev/null +++ b/src/Render/FriendicaSmarty.php @@ -0,0 +1,54 @@ + "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"]; + if (x($a->theme_info, "extends")) + $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"]; + $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"]; + $this->setTemplateDir($template_dirs); + + $this->setCompileDir('view/smarty3/compiled/'); + $this->setConfigDir('view/smarty3/config/'); + $this->setCacheDir('view/smarty3/cache/'); + + $this->left_delimiter = $a->get_template_ldelim('smarty3'); + $this->right_delimiter = $a->get_template_rdelim('smarty3'); + + // Don't report errors so verbosely + $this->error_reporting = E_ALL & ~E_NOTICE; + } + + function parsed($template = '') + { + if ($template) { + return $this->fetch('string:' . $template); + } + return $this->fetch('file:' . $this->filename); + } + +} \ No newline at end of file diff --git a/src/Render/FriendicaSmartyEngine.php b/src/Render/FriendicaSmartyEngine.php new file mode 100644 index 000000000..a565f3ad3 --- /dev/null +++ b/src/Render/FriendicaSmartyEngine.php @@ -0,0 +1,57 @@ +ERROR: folder view/smarty3/ must be writable by webserver."; + killme(); + } + } + + // ITemplateEngine interface + public function replaceMacros($s, $r) + { + $template = ''; + if (gettype($s) === 'string') { + $template = $s; + $s = new FriendicaSmarty(); + } + + $r['$APP'] = get_app(); + + // "middleware": inject variables into templates + $arr = [ + "template" => basename($s->filename), + "vars" => $r + ]; + Addon::callHooks("template_vars", $arr); + $r = $arr['vars']; + + foreach ($r as $key => $value) { + if ($key[0] === '$') { + $key = substr($key, 1); + } + $s->assign($key, $value); + } + return $s->parsed($template); + } + + public function getTemplateFile($file, $root = '') + { + $a = get_app(); + $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root); + $template = new FriendicaSmarty(); + $template->filename = $template_file; + return $template; + } +} From 42c97db7241c8393a7560a31f84a03325df00616 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 1 Feb 2018 13:33:04 -0500 Subject: [PATCH 2/5] Switch register_template_engine() to the only one available - get_declared_classes() can't be used with autoloading - Removed killme() to prevent infinite loop during App object instantiation --- src/App.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/App.php b/src/App.php index 7c87130b3..6eff22f54 100644 --- a/src/App.php +++ b/src/App.php @@ -288,12 +288,7 @@ class App $this->is_friendica_app = ($_SERVER['HTTP_USER_AGENT'] == 'Apache-HttpClient/UNAVAILABLE (java 1.4)'); // Register template engines - $dc = get_declared_classes(); - foreach ($dc as $k) { - if (in_array('Friendica\Render\ITemplateEngine', class_implements($k))) { - $this->register_template_engine($k); - } - } + $this->register_template_engine('Friendica\Render\FriendicaSmartyEngine'); self::$a = $this; } @@ -602,7 +597,7 @@ class App $this->template_engines[$name] = $class; } else { echo "template engine $class cannot be registered without a name.\n"; - killme(); + die(); } } From 65713f26f4656dfb3faeaa772154f4b891e30e9f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 1 Feb 2018 13:35:00 -0500 Subject: [PATCH 3/5] Remove references to include/friendica_smarty --- include/text.php | 1 - 1 file changed, 1 deletion(-) diff --git a/include/text.php b/include/text.php index ffa0ad0ed..3244d7bb9 100644 --- a/include/text.php +++ b/include/text.php @@ -16,7 +16,6 @@ use Friendica\Model\Profile; use Friendica\Model\Term; use Friendica\Util\Map; -require_once "include/friendica_smarty.php"; require_once "mod/proxy.php"; require_once "include/conversation.php"; From 99eb696386b7161a5135abe9287fca5d1042a693 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 1 Feb 2018 13:35:15 -0500 Subject: [PATCH 4/5] Remove include/friendica_smarty --- include/friendica_smarty.php | 96 ------------------------------------ 1 file changed, 96 deletions(-) delete mode 100644 include/friendica_smarty.php diff --git a/include/friendica_smarty.php b/include/friendica_smarty.php deleted file mode 100644 index a7d153e30..000000000 --- a/include/friendica_smarty.php +++ /dev/null @@ -1,96 +0,0 @@ - "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"]; - if (x($a->theme_info, "extends")) - $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"]; - $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"]; - $this->setTemplateDir($template_dirs); - - $this->setCompileDir('view/smarty3/compiled/'); - $this->setConfigDir('view/smarty3/config/'); - $this->setCacheDir('view/smarty3/cache/'); - - $this->left_delimiter = $a->get_template_ldelim('smarty3'); - $this->right_delimiter = $a->get_template_rdelim('smarty3'); - - // Don't report errors so verbosely - $this->error_reporting = E_ALL & ~E_NOTICE; - } - - function parsed($template = '') - { - if ($template) { - return $this->fetch('string:' . $template); - } - return $this->fetch('file:' . $this->filename); - } - -} - -class FriendicaSmartyEngine implements ITemplateEngine -{ - static $name = "smarty3"; - - public function __construct() - { - if (!is_writable('view/smarty3/')) { - echo "ERROR: folder view/smarty3/ must be writable by webserver."; - killme(); - } - } - - // ITemplateEngine interface - public function replaceMacros($s, $r) - { - $template = ''; - if (gettype($s) === 'string') { - $template = $s; - $s = new FriendicaSmarty(); - } - - $r['$APP'] = get_app(); - - // "middleware": inject variables into templates - $arr = [ - "template" => basename($s->filename), - "vars" => $r - ]; - Addon::callHooks("template_vars", $arr); - $r = $arr['vars']; - - foreach ($r as $key => $value) { - if ($key[0] === '$') { - $key = substr($key, 1); - } - $s->assign($key, $value); - } - return $s->parsed($template); - } - - public function getTemplateFile($file, $root = '') - { - $a = get_app(); - $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root); - $template = new FriendicaSmarty(); - $template->filename = $template_file; - return $template; - } -} From 03c1f2cbf8d2bc2f6deb53675aea16f56ab8c2e5 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 2 Feb 2018 07:47:42 -0500 Subject: [PATCH 5/5] Fix formatting in Render/FriendicaSmarty* --- src/Render/FriendicaSmarty.php | 4 +++- src/Render/FriendicaSmartyEngine.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Render/FriendicaSmarty.php b/src/Render/FriendicaSmarty.php index b86357612..b147d7e81 100644 --- a/src/Render/FriendicaSmarty.php +++ b/src/Render/FriendicaSmarty.php @@ -27,8 +27,10 @@ class FriendicaSmarty extends Smarty // setTemplateDir can be set to an array, which Smarty will parse in order. // The order is thus very important here $template_dirs = ['theme' => "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"]; - if (x($a->theme_info, "extends")) + if (x($a->theme_info, "extends")) { $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"]; + } + $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"]; $this->setTemplateDir($template_dirs); diff --git a/src/Render/FriendicaSmartyEngine.php b/src/Render/FriendicaSmartyEngine.php index a565f3ad3..2cfce760a 100644 --- a/src/Render/FriendicaSmartyEngine.php +++ b/src/Render/FriendicaSmartyEngine.php @@ -41,6 +41,7 @@ class FriendicaSmartyEngine implements ITemplateEngine if ($key[0] === '$') { $key = substr($key, 1); } + $s->assign($key, $value); } return $s->parsed($template); @@ -52,6 +53,7 @@ class FriendicaSmartyEngine implements ITemplateEngine $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root); $template = new FriendicaSmarty(); $template->filename = $template_file; + return $template; } }