From 177b430c0867d5f352e34d97e7c2abf454124317 Mon Sep 17 00:00:00 2001 From: Stefano Cipriani Date: Fri, 7 Oct 2022 12:03:35 +0200 Subject: [PATCH 1/3] add variable use_only_compiled --- docs/programmers/api-variables.md | 1 + .../api-variables/variable-use-only-compiled.md | 9 +++++++++ libs/Smarty.class.php | 15 +++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 docs/programmers/api-variables/variable-use-only-compiled.md diff --git a/docs/programmers/api-variables.md b/docs/programmers/api-variables.md index 2fcf6e217..244f9b092 100644 --- a/docs/programmers/api-variables.md +++ b/docs/programmers/api-variables.md @@ -46,6 +46,7 @@ them directly, or use the corresponding setter/getter methods. - [$template_dir](./api-variables/variable-template-dir.md) - [$trusted_dir](./api-variables/variable-trusted-dir.md) - [$use_include_path](./api-variables/variable-use-include-path.md) +- [$use_only_compiled](./api-variables/variable-use-only-compiled.md) - [$use_sub_dirs](./api-variables/variable-use-sub-dirs.md) > **Note** diff --git a/docs/programmers/api-variables/variable-use-only-compiled.md b/docs/programmers/api-variables/variable-use-only-compiled.md new file mode 100644 index 000000000..1c376e7e5 --- /dev/null +++ b/docs/programmers/api-variables/variable-use-only-compiled.md @@ -0,0 +1,9 @@ +\$use\_only\_compiled {#variable.use.only.compiled} +================ + +If set to TRUE, Smarty will use only compiled templates, alsoù +ignoring the (in)existence of base templates. Compiled filenames +will be constant and relative to the template basename. +Useful to use pre-compiled templates and distribute them already +compiled on production websites. Overrides and disables +[`$merge_compiled_includes`](#variable.merge.compiled.includes) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 21beafdf0..0ab83b3a6 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -569,6 +569,13 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $_debug = null; + /** + * force using of only compiled templates, ignoring source + * + * @var boolean + */ + public $use_only_compiled = false; + /** * template directory * @@ -1270,6 +1277,14 @@ public function setCachingType($caching_type) $this->caching_type = $caching_type; } + /** + * @param boolean $use_only_compiled + */ + public function setUseOnlyCompiled($use_only_compiled) + { + $this->use_only_compiled = $use_only_compiled; + } + /** * Test install * From 1a3728b88ea607a89044f06a70d0ba25fa569dc6 Mon Sep 17 00:00:00 2001 From: Stefano Cipriani Date: Fri, 7 Oct 2022 12:05:04 +0200 Subject: [PATCH 2/3] ignore merge_compiled_includes if use_only_compiled --- libs/sysplugins/smarty_internal_compile_include.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/sysplugins/smarty_internal_compile_include.php b/libs/sysplugins/smarty_internal_compile_include.php index bf62461bc..77f1ba629 100644 --- a/libs/sysplugins/smarty_internal_compile_include.php +++ b/libs/sysplugins/smarty_internal_compile_include.php @@ -145,6 +145,10 @@ public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler) if (isset($_attr[ 'compile_id' ]) && $compiler->isVariable($_attr[ 'compile_id' ])) { $merge_compiled_includes = false; } + // compile names without uid + if ($compiler->smarty->use_only_compiled) { + $merge_compiled_includes = false; + } } /* * if the {include} tag provides individual parameter for caching or compile_id From c281ca9c362c26f0e9aee683df00cb4d43ffd27f Mon Sep 17 00:00:00 2001 From: Stefano Cipriani Date: Fri, 7 Oct 2022 12:06:34 +0200 Subject: [PATCH 3/3] properly handle use_only_compiled --- libs/sysplugins/smarty_internal_template.php | 2 +- libs/sysplugins/smarty_template_compiled.php | 55 +++++++++++++------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index bae22a7d5..192b155ee 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -191,7 +191,7 @@ public function render($no_output_filter = true, $display = null) $this->smarty->_debug->start_template($this, $display); } // checks if template exists - if (!$this->source->exists) { + if (!$this->source->exists && !$this->smarty->use_only_compiled) { throw new SmartyException( "Unable to load template '{$this->source->type}:{$this->source->name}'" . ($this->_isSubTpl() ? " in '{$this->parent->template_resource}'" : '') diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index 37d8f0a9e..fb5e5e5a8 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -50,23 +50,37 @@ public function populateCompiledFilepath(Smarty_Internal_Template $_template) $this->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . ($smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'); } - // if use_sub_dirs, break file into directories - if ($smarty->use_sub_dirs) { - $this->filepath .= $source->uid[ 0 ] . $source->uid[ 1 ] . DIRECTORY_SEPARATOR . $source->uid[ 2 ] . - $source->uid[ 3 ] . DIRECTORY_SEPARATOR . $source->uid[ 4 ] . $source->uid[ 5 ] . - DIRECTORY_SEPARATOR; - } - $this->filepath .= $source->uid . '_'; - if ($source->isConfig) { - $this->filepath .= (int)$smarty->config_read_hidden + (int)$smarty->config_booleanize * 2 + - (int)$smarty->config_overwrite * 4; + $basename = $smarty->use_only_compiled ? basename($source->name) : $source->handler->getBasename($source); + if ($smarty->use_only_compiled) { + if (empty($basename)) { + throw new SmartyException("Unable to get basename"); + } + // if use_sub_dirs, break file into directories + if ($smarty->use_sub_dirs) { + $padded_basename = str_pad(str_replace('.','',$basename), 6, '_'); + $this->filepath .= $padded_basename[ 0 ] . $padded_basename[ 1 ] . DIRECTORY_SEPARATOR . + $padded_basename[ 2 ] . $padded_basename[ 3 ] . DIRECTORY_SEPARATOR . + $padded_basename[ 4 ] . $padded_basename[ 5 ] . DIRECTORY_SEPARATOR; + } } else { - $this->filepath .= (int)$smarty->merge_compiled_includes + (int)$smarty->escape_html * 2 + - (($smarty->merge_compiled_includes && $source->type === 'extends') ? - (int)$smarty->extends_recursion * 4 : 0); + // if use_sub_dirs, break file into directories + if ($smarty->use_sub_dirs) { + $this->filepath .= $source->uid[ 0 ] . $source->uid[ 1 ] . DIRECTORY_SEPARATOR . $source->uid[ 2 ] . + $source->uid[ 3 ] . DIRECTORY_SEPARATOR . $source->uid[ 4 ] . $source->uid[ 5 ] . + DIRECTORY_SEPARATOR; + } + $this->filepath .= $source->uid . '_'; + if ($source->isConfig) { + $this->filepath .= (int)$smarty->config_read_hidden + (int)$smarty->config_booleanize * 2 + + (int)$smarty->config_overwrite * 4; + } else { + $this->filepath .= (int)$smarty->merge_compiled_includes + (int)$smarty->escape_html * 2 + + (($smarty->merge_compiled_includes && $source->type === 'extends') ? + (int)$smarty->extends_recursion * 4 : 0); + } + $this->filepath .= '.'; } - $this->filepath .= '.' . $source->type; - $basename = $source->handler->getBasename($source); + $this->filepath .= $source->type; if (!empty($basename)) { $this->filepath .= '.' . $basename; } @@ -91,7 +105,7 @@ public function populateCompiledFilepath(Smarty_Internal_Template $_template) public function render(Smarty_Internal_Template $_template) { // checks if template exists - if (!$_template->source->exists) { + if (!$_template->source->exists && !is_file($_template->source->filepath) && !$_template->smarty->use_only_compiled) { $type = $_template->source->isConfig ? 'config' : 'template'; throw new SmartyException("Unable to load {$type} '{$_template->source->type}:{$_template->source->name}'"); } @@ -135,8 +149,9 @@ public function process(Smarty_Internal_Template $_smarty_tpl) if ($source->handler->recompiled) { $source->handler->process($_smarty_tpl); } elseif (!$source->handler->uncompiled) { - if (!$this->exists || $smarty->force_compile - || ($_smarty_tpl->compile_check && $source->getTimeStamp() > $this->getTimeStamp()) + if ( !$smarty->use_only_compiled && + (!$this->exists || $smarty->force_compile + || ($_smarty_tpl->compile_check && $source->getTimeStamp() > $this->getTimeStamp())) ) { $this->compileTemplateSource($_smarty_tpl); $compileCheck = $_smarty_tpl->compile_check; @@ -147,7 +162,9 @@ public function process(Smarty_Internal_Template $_smarty_tpl) $_smarty_tpl->mustCompile = true; @include $this->filepath; if ($_smarty_tpl->mustCompile) { - $this->compileTemplateSource($_smarty_tpl); + if (!$smarty->use_only_compiled) { + $this->compileTemplateSource($_smarty_tpl); + } $compileCheck = $_smarty_tpl->compile_check; $_smarty_tpl->compile_check = Smarty::COMPILECHECK_OFF; $this->loadCompiledTemplate($_smarty_tpl);