- optimization replace internal Smarty::$ds property by DIRECTORY_SEPARATOR (reverted from commit f7a5316205) (reverted from commit beaa293eb3) (reverted from commit 86f36196e2)

This commit is contained in:
Uwe Tews
2017-10-26 04:35:56 +02:00
parent 86f36196e2
commit 36c6a72015
12 changed files with 323 additions and 320 deletions

View File

@@ -1,8 +1,4 @@
===== 3.1.32 - dev === ===== 3.1.32 - dev ===
22.10.2017 3.1.32-dev-28
- bugfix Smarty version was not filled in header comment of compiled and cached files
- optimization replace internal Smarty::$ds property by DIRECTORY_SEPARATOR
21.10.2017 21.10.2017
- bugfix custom delimiters could fail since modification of version 3.1.32-dev-23 - bugfix custom delimiters could fail since modification of version 3.1.32-dev-23
https://github.com/smarty-php/smarty/issues/394 https://github.com/smarty-php/smarty/issues/394

View File

@@ -100,7 +100,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.32-dev-28'; const SMARTY_VERSION = '3.1.32-dev-27';
/** /**
* define variable scopes * define variable scopes
*/ */
@@ -544,6 +544,12 @@ class Smarty extends Smarty_Internal_TemplateBase
* @var Smarty_Internal_Debug * @var Smarty_Internal_Debug
*/ */
public $_debug = null; public $_debug = null;
/**
* Directory separator
*
* @var string
*/
public $ds = DIRECTORY_SEPARATOR;
/** /**
* template directory * template directory
* *
@@ -936,7 +942,7 @@ class Smarty extends Smarty_Internal_TemplateBase
$this->plugins_dir = (array)$this->plugins_dir; $this->plugins_dir = (array)$this->plugins_dir;
} }
foreach ($this->plugins_dir as $k => $v) { foreach ($this->plugins_dir as $k => $v) {
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . DIRECTORY_SEPARATOR, true); $this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
} }
$this->_cache[ 'plugin_files' ] = array(); $this->_cache[ 'plugin_files' ] = array();
$this->_pluginsDirNormalized = true; $this->_pluginsDirNormalized = true;
@@ -1127,9 +1133,9 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public function _realpath($path, $realpath = null) public function _realpath($path, $realpath = null)
{ {
$nds = DIRECTORY_SEPARATOR === '/' ? '\\' : '/'; $nds = $this->ds == '/' ? '\\' : '/';
// normalize DIRECTORY_SEPARATOR // normalize $this->ds
$path = str_replace($nds, DIRECTORY_SEPARATOR, $path); $path = str_replace($nds, $this->ds, $path);
preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(.*))$%u', preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(.*))$%u',
$path, $path,
$parts); $parts);
@@ -1138,13 +1144,13 @@ class Smarty extends Smarty_Internal_TemplateBase
$parts[ 'root' ] = substr(getcwd(), 0, 2) . $parts[ 'root' ]; $parts[ 'root' ] = substr(getcwd(), 0, 2) . $parts[ 'root' ];
} else { } else {
if ($realpath !== null && !$parts[ 'root' ]) { if ($realpath !== null && !$parts[ 'root' ]) {
$path = getcwd() . DIRECTORY_SEPARATOR . $path; $path = getcwd() . $this->ds . $path;
} }
} }
// remove noop 'DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR' and 'DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR' patterns // remove noop 'DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR' and 'DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR' patterns
$path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#u', DIRECTORY_SEPARATOR, $path); $path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#u', $this->ds, $path);
// resolve '..DIRECTORY_SEPARATOR' pattern, smallest first // resolve '..DIRECTORY_SEPARATOR' pattern, smallest first
if (strpos($path, '..' . DIRECTORY_SEPARATOR) != false && if (strpos($path, '..' . $this->ds) != false &&
preg_match_all('#(([.]?[\\\\/])*([.][.])[\\\\/]([.]?[\\\\/])*)+#u', $path, $match) preg_match_all('#(([.]?[\\\\/])*([.][.])[\\\\/]([.]?[\\\\/])*)+#u', $path, $match)
) { ) {
$counts = array(); $counts = array();
@@ -1155,7 +1161,7 @@ class Smarty extends Smarty_Internal_TemplateBase
foreach ($counts as $count) { foreach ($counts as $count) {
$path = preg_replace('#(([\\\\/]([.]?[\\\\/])*[^\\\\/.]+){' . $count . $path = preg_replace('#(([\\\\/]([.]?[\\\\/])*[^\\\\/.]+){' . $count .
'}[\\\\/]([.]?[\\\\/])*([.][.][\\\\/]([.]?[\\\\/])*){' . $count . '})(?=[^.])#u', '}[\\\\/]([.]?[\\\\/])*([.][.][\\\\/]([.]?[\\\\/])*){' . $count . '})(?=[^.])#u',
DIRECTORY_SEPARATOR, $this->ds,
$path); $path);
} }
} }
@@ -1401,7 +1407,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
private function _normalizeDir($dirName, $dir) private function _normalizeDir($dirName, $dir)
{ {
$this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . DIRECTORY_SEPARATOR, true); $this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . $this->ds, true);
if (!isset(Smarty::$_muted_directories[ $this->{$dirName} ])) { if (!isset(Smarty::$_muted_directories[ $this->{$dirName} ])) {
Smarty::$_muted_directories[ $this->{$dirName} ] = null; Smarty::$_muted_directories[ $this->{$dirName} ] = null;
} }
@@ -1427,7 +1433,7 @@ class Smarty extends Smarty_Internal_TemplateBase
} }
foreach ($dir as $k => $v) { foreach ($dir as $k => $v) {
if (!isset($processed[ $k ])) { if (!isset($processed[ $k ])) {
$dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . DIRECTORY_SEPARATOR, true); $dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
$processed[ $k ] = true; $processed[ $k ] = true;
} }
} }

View File

@@ -29,14 +29,13 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
{ {
$source = &$_template->source; $source = &$_template->source;
$smarty = &$_template->smarty; $smarty = &$_template->smarty;
$_compile_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'; $_compile_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^';
$_filepath = sha1($source->uid . $smarty->_joined_template_dir); $_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir(); $cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) { if (isset($_template->cache_id)) {
$cached->filepath .= preg_replace(array('![^\w|]+!', $cached->filepath .= preg_replace(array('![^\w|]+!',
'![|]+!'), '![|]+!'), array('_',
array('_', $_compile_dir_sep),
$_compile_dir_sep),
$_template->cache_id) . $_compile_dir_sep; $_template->cache_id) . $_compile_dir_sep;
} }
if (isset($_template->compile_id)) { if (isset($_template->compile_id)) {
@@ -44,10 +43,9 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
} }
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) { if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] . $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] .
$_filepath[ 3 ] . $smarty->ds .
DIRECTORY_SEPARATOR . $_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
$_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR;
} }
$cached->filepath .= $_filepath; $cached->filepath .= $_filepath;
$basename = $source->handler->getBasename($source); $basename = $source->handler->getBasename($source);
@@ -88,8 +86,7 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
* *
* @return boolean true or false if the cached content does not exist * @return boolean true or false if the cached content does not exist
*/ */
public function process(Smarty_Internal_Template $_smarty_tpl, public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null,
Smarty_Template_Cached $cached = null,
$update = false) $update = false)
{ {
$_smarty_tpl->cached->valid = false; $_smarty_tpl->cached->valid = false;
@@ -111,15 +108,14 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
*/ */
public function writeCachedContent(Smarty_Internal_Template $_template, $content) public function writeCachedContent(Smarty_Internal_Template $_template, $content)
{ {
if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
$content,
$_template->smarty) === true $_template->smarty) === true
) { ) {
if (function_exists('opcache_invalidate') && if (function_exists('opcache_invalidate') &&
(!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1 (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1
) { ) {
opcache_invalidate($_template->cached->filepath, true); opcache_invalidate($_template->cached->filepath, true);
} else if (function_exists('apc_compile_file')) { } elseif (function_exists('apc_compile_file')) {
apc_compile_file($_template->cached->filepath); apc_compile_file($_template->cached->filepath);
} }
$cached = $_template->cached; $cached = $_template->cached;

View File

@@ -57,6 +57,7 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
} }
// check and get attributes // check and get attributes
$_attr = $this->getAttributes($compiler, $args); $_attr = $this->getAttributes($compiler, $args);
/** @var Smarty_Internal_Template $_smarty_tpl /** @var Smarty_Internal_Template $_smarty_tpl
* used in evaluated code * used in evaluated code
*/ */
@@ -73,8 +74,8 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
$_dir = $compiler->smarty->trusted_dir; $_dir = $compiler->smarty->trusted_dir;
} }
if (!empty($_dir)) { if (!empty($_dir)) {
foreach ((array)$_dir as $_script_dir) { foreach ((array) $_dir as $_script_dir) {
$_path = $compiler->smarty->_realpath($_script_dir . DIRECTORY_SEPARATOR . $_file, true); $_path = $compiler->smarty->_realpath($_script_dir . $compiler->smarty->ds . $_file, true);
if (file_exists($_path)) { if (file_exists($_path)) {
$_filepath = $_path; $_filepath = $_path;
break; break;
@@ -85,9 +86,11 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
if ($_filepath == false) { if ($_filepath == false) {
$compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", null, true); $compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", null, true);
} }
if (isset($compiler->smarty->security_policy)) { if (isset($compiler->smarty->security_policy)) {
$compiler->smarty->security_policy->isTrustedPHPDir($_filepath); $compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
} }
if (isset($_attr[ 'assign' ])) { if (isset($_attr[ 'assign' ])) {
// output will be stored in a smarty variable instead of being displayed // output will be stored in a smarty variable instead of being displayed
$_assign = $_attr[ 'assign' ]; $_assign = $_attr[ 'assign' ];
@@ -98,6 +101,7 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
$_once = ''; $_once = '';
} }
} }
if (isset($_assign)) { if (isset($_assign)) {
return "<?php ob_start();\ninclude{$_once} ('{$_filepath}');\n\$_smarty_tpl->assign({$_assign},ob_get_clean());\n?>"; return "<?php ob_start();\ninclude{$_once} ('{$_filepath}');\n\$_smarty_tpl->assign({$_assign},ob_get_clean());\n?>";
} else { } else {

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* Smarty Internal Plugin Compile Insert * Smarty Internal Plugin Compile Insert
* Compiles the {insert} tag * Compiles the {insert} tag
@@ -55,6 +56,7 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
$_attr = $this->getAttributes($compiler, $args); $_attr = $this->getAttributes($compiler, $args);
//Does tag create output //Does tag create output
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true; $compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
$nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache); $nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
if (!$nocacheParam) { if (!$nocacheParam) {
// do not compile as nocache code // do not compile as nocache code
@@ -64,6 +66,7 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
$_smarty_tpl = $compiler->template; $_smarty_tpl = $compiler->template;
$_name = null; $_name = null;
$_script = null; $_script = null;
$_output = '<?php '; $_output = '<?php ';
// save possible attributes // save possible attributes
eval('$_name = @' . $_attr[ 'name' ] . ';'); eval('$_name = @' . $_attr[ 'name' ] . ';');
@@ -93,8 +96,8 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
$_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null; $_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null;
} }
if (!empty($_dir)) { if (!empty($_dir)) {
foreach ((array)$_dir as $_script_dir) { foreach ((array) $_dir as $_script_dir) {
$_script_dir = rtrim($_script_dir, '/\\') . DIRECTORY_SEPARATOR; $_script_dir = rtrim($_script_dir, '/\\') . $compiler->smarty->ds;
if (file_exists($_script_dir . $_script)) { if (file_exists($_script_dir . $_script)) {
$_filepath = $_script_dir . $_script; $_filepath = $_script_dir . $_script;
break; break;
@@ -110,8 +113,7 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
require_once $_filepath; require_once $_filepath;
if (!is_callable($_function)) { if (!is_callable($_function)) {
$compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'",
null, null, true);
true);
} }
} else { } else {
$_filepath = 'null'; $_filepath = 'null';
@@ -120,8 +122,7 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
if (!is_callable($_function)) { if (!is_callable($_function)) {
// try plugin // try plugin
if (!$_function = $compiler->getPlugin($_name, 'insert')) { if (!$_function = $compiler->getPlugin($_name, 'insert')) {
$compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", null,
null,
true); true);
} }
} }
@@ -148,6 +149,7 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
$_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>"; $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
} }
} }
return $_output; return $_output;
} }
} }

View File

@@ -31,10 +31,7 @@ class Smarty_Internal_Method_CompileAllTemplates
* *
* @return integer number of template files recompiled * @return integer number of template files recompiled
*/ */
public function compileAllTemplates(Smarty $smarty, public function compileAllTemplates(Smarty $smarty, $extension = '.tpl', $force_compile = false, $time_limit = 0,
$extension = '.tpl',
$force_compile = false,
$time_limit = 0,
$max_errors = null) $max_errors = null)
{ {
return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors); return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
@@ -52,11 +49,7 @@ class Smarty_Internal_Method_CompileAllTemplates
* *
* @return int number of template files compiled * @return int number of template files compiled
*/ */
protected function compileAll(Smarty $smarty, protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_limit, $max_errors,
$extension,
$force_compile,
$time_limit,
$max_errors,
$isConfig = false) $isConfig = false)
{ {
// switch off time limit // switch off time limit
@@ -76,11 +69,11 @@ class Smarty_Internal_Method_CompileAllTemplates
if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
continue; continue;
} }
if (!substr_compare($_file, $extension, -strlen($extension)) == 0) { if (!substr_compare($_file, $extension, - strlen($extension)) == 0) {
continue; continue;
} }
if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) { if ($_fileinfo->getPath() !== substr($_dir, 0, - 1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file; $_file = substr($_fileinfo->getPath(), strlen($_dir)) . $smarty->ds . $_file;
} }
echo "\n<br>", $_dir, '---', $_file; echo "\n<br>", $_dir, '---', $_file;
flush(); flush();
@@ -99,7 +92,7 @@ class Smarty_Internal_Method_CompileAllTemplates
$isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl); $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
if ($_tpl->mustCompile()) { if ($_tpl->mustCompile()) {
$_tpl->compileTemplateSource(); $_tpl->compileTemplateSource();
$_count++; $_count ++;
echo ' compiled in ', microtime(true) - $_start_time, ' seconds'; echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
flush(); flush();
} else { } else {
@@ -109,7 +102,7 @@ class Smarty_Internal_Method_CompileAllTemplates
} }
catch (Exception $e) { catch (Exception $e) {
echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n"; echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
$_error_count++; $_error_count ++;
} }
// free memory // free memory
unset($_tpl); unset($_tpl);

View File

@@ -17,30 +17,6 @@
*/ */
class Smarty_Internal_Resource_File extends Smarty_Resource class Smarty_Internal_Resource_File extends Smarty_Resource
{ {
/**
* populate Source Object with meta data from Resource
*
* @param Smarty_Template_Source $source source object
* @param Smarty_Internal_Template $_template template object
*
* @throws \SmartyException
*/
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$source->filepath = $this->buildFilepath($source, $_template);
if ($source->filepath !== false) {
if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
$source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
}
$source->exists = true;
$source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
$source->smarty->_joined_template_dir));
$source->timestamp = filemtime($source->filepath);
} else {
$source->timestamp = $source->exists = false;
}
}
/** /**
* build template filepath by traversing the template_dir array * build template filepath by traversing the template_dir array
* *
@@ -68,15 +44,15 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
} }
// normalize path // normalize path
$path = $path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . $source->smarty->ds . $file);
$source->smarty->_realpath(dirname($_template->parent->source->filepath) . DIRECTORY_SEPARATOR . $file);
// files relative to a template only get one shot // files relative to a template only get one shot
return is_file($path) ? $path : false; return is_file($path) ? $path : false;
} }
// normalize DIRECTORY_SEPARATOR // normalize $source->smarty->ds
if (strpos($file, DIRECTORY_SEPARATOR === '/' ? '\\' : '/') !== false) { if (strpos($file, $source->smarty->ds == '/' ? '\\' : '/') !== false) {
$file = str_replace(DIRECTORY_SEPARATOR === '/' ? '\\' : '/', DIRECTORY_SEPARATOR, $file); $file = str_replace($source->smarty->ds == '/' ? '\\' : '/', $source->smarty->ds, $file);
} }
$_directories = $source->smarty->getTemplateDir(null, $source->isConfig); $_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
// template_dir index? // template_dir index?
if ($file[ 0 ] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) { if ($file[ 0 ] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
@@ -88,9 +64,9 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
// try string indexes // try string indexes
if (isset($_directories[ $index ])) { if (isset($_directories[ $index ])) {
$_index_dirs[] = $_directories[ $index ]; $_index_dirs[] = $_directories[ $index ];
} else if (is_numeric($index)) { } elseif (is_numeric($index)) {
// try numeric index // try numeric index
$index = (int)$index; $index = (int) $index;
if (isset($_directories[ $index ])) { if (isset($_directories[ $index ])) {
$_index_dirs[] = $_directories[ $index ]; $_index_dirs[] = $_directories[ $index ];
} else { } else {
@@ -109,11 +85,12 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
$_directories = $_index_dirs; $_directories = $_index_dirs;
} }
} }
// relative file name? // relative file name?
foreach ($_directories as $_directory) { foreach ($_directories as $_directory) {
$path = $_directory . $file; $path = $_directory . $file;
if (is_file($path)) { if (is_file($path)) {
return (strpos($path, '.' . DIRECTORY_SEPARATOR) !== false) ? $source->smarty->_realpath($path) : $path; return (strpos($path, '.' . $source->smarty->ds) !== false) ? $source->smarty->_realpath($path) : $path;
} }
} }
if (!isset($_index_dirs)) { if (!isset($_index_dirs)) {
@@ -130,6 +107,29 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
return false; return false;
} }
/**
* populate Source Object with meta data from Resource
*
* @param Smarty_Template_Source $source source object
* @param Smarty_Internal_Template $_template template object
*/
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$source->filepath = $this->buildFilepath($source, $_template);
if ($source->filepath !== false) {
if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
$source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
}
$source->exists = true;
$source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
$source->smarty->_joined_template_dir));
$source->timestamp = filemtime($source->filepath);
} else {
$source->timestamp = $source->exists = false;
}
}
/** /**
* populate Source Object with timestamp and exists from Resource * populate Source Object with timestamp and exists from Resource
* *

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* Smarty cache resource file clear method * Smarty cache resource file clear method
* *
@@ -42,7 +43,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
$_cache_id_parts_count = count($_cache_id_parts); $_cache_id_parts_count = count($_cache_id_parts);
if ($smarty->use_sub_dirs) { if ($smarty->use_sub_dirs) {
foreach ($_cache_id_parts as $id_part) { foreach ($_cache_id_parts as $id_part) {
$_dir .= $id_part . DIRECTORY_SEPARATOR; $_dir .= $id_part . $smarty->ds;
} }
} }
} }
@@ -51,8 +52,10 @@ class Smarty_Internal_Runtime_CacheResourceFile
$smarty->caching = true; $smarty->caching = true;
$tpl = new $smarty->template_class($resource_name, $smarty); $tpl = new $smarty->template_class($resource_name, $smarty);
$smarty->caching = $_save_stat; $smarty->caching = $_save_stat;
// remove from template cache // remove from template cache
$tpl->source; // have the template registered before unset() $tpl->source; // have the template registered before unset()
if ($tpl->source->exists) { if ($tpl->source->exists) {
$_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
} else { } else {
@@ -68,7 +71,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
if (substr(basename($_file->getPathname()), 0, 1) == '.') { if (substr(basename($_file->getPathname()), 0, 1) == '.') {
continue; continue;
} }
$_filepath = (string)$_file; $_filepath = (string) $_file;
// directory ? // directory ?
if ($_file->isDir()) { if ($_file->isDir()) {
if (!$_cache->isDot()) { if (!$_cache->isDot()) {
@@ -77,7 +80,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
} }
} else { } else {
// delete only php files // delete only php files
if (substr($_filepath, -4) !== '.php') { if (substr($_filepath, - 4) !== '.php') {
continue; continue;
} }
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length))); $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
@@ -102,7 +105,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
if ($_parts_count < $_cache_id_parts_count) { if ($_parts_count < $_cache_id_parts_count) {
continue; continue;
} }
for ($i = 0; $i < $_cache_id_parts_count; $i++) { for ($i = 0; $i < $_cache_id_parts_count; $i ++) {
if ($_parts[ $i ] != $_cache_id_parts[ $i ]) { if ($_parts[ $i ] != $_cache_id_parts[ $i ]) {
continue 2; continue 2;
} }
@@ -127,7 +130,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1) && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) { ) {
opcache_invalidate($_filepath, true); opcache_invalidate($_filepath, true);
} else if (function_exists('apc_delete_file')) { } elseif (function_exists('apc_delete_file')) {
apc_delete_file($_filepath); apc_delete_file($_filepath);
} }
} }

View File

@@ -87,7 +87,7 @@ class Smarty_Internal_Runtime_GetIncludePath
$_dirs = (array) explode(PATH_SEPARATOR, $_i_path); $_dirs = (array) explode(PATH_SEPARATOR, $_i_path);
foreach ($_dirs as $_path) { foreach ($_dirs as $_path) {
if (is_dir($_path)) { if (is_dir($_path)) {
$this->_include_dirs[] = $smarty->_realpath($_path . DIRECTORY_SEPARATOR, true); $this->_include_dirs[] = $smarty->_realpath($_path . $smarty->ds, true);
} }
} }
return true; return true;

View File

@@ -35,30 +35,34 @@ class Smarty_Internal_Runtime_WriteFile
if ($_file_perms !== null) { if ($_file_perms !== null) {
$old_umask = umask(0); $old_umask = umask(0);
} }
$_dirpath = dirname($_filepath); $_dirpath = dirname($_filepath);
// if subdirs, create dir structure
// if subdirs, create dir structure
if ($_dirpath !== '.') { if ($_dirpath !== '.') {
$i = 0; $i=0;
// loop if concurrency problem occurs // loop if concurrency problem occurs
// see https://bugs.php.net/bug.php?id=35326 // see https://bugs.php.net/bug.php?id=35326
while (!is_dir($_dirpath)) { while (!is_dir($_dirpath)) {
if (@mkdir($_dirpath, $_dir_perms, true)) { if (@mkdir($_dirpath, $_dir_perms, true)) {
break; break;
} }
clearstatcache(); clearstatcache();
if (++$i === 3) { if (++$i === 3) {
error_reporting($_error_reporting); error_reporting($_error_reporting);
throw new SmartyException("unable to create directory {$_dirpath}"); throw new SmartyException("unable to create directory {$_dirpath}");
} }
sleep(1); sleep(1);
} }
} }
// write to tmp file, then move to overt file lock race condition // write to tmp file, then move to overt file lock race condition
$_tmp_file = $_dirpath . DIRECTORY_SEPARATOR . str_replace(array('.', ','), '_', uniqid('wrt', true)); $_tmp_file = $_dirpath . $smarty->ds . str_replace(array('.', ','), '_', uniqid('wrt', true));
if (!file_put_contents($_tmp_file, $_contents)) { if (!file_put_contents($_tmp_file, $_contents)) {
error_reporting($_error_reporting); error_reporting($_error_reporting);
throw new SmartyException("unable to write file {$_tmp_file}"); throw new SmartyException("unable to write file {$_tmp_file}");
} }
/* /*
* Windows' rename() fails if the destination exists, * Windows' rename() fails if the destination exists,
* Linux' rename() properly handles the overwrite. * Linux' rename() properly handles the overwrite.
@@ -95,6 +99,7 @@ class Smarty_Internal_Runtime_WriteFile
umask($old_umask); umask($old_umask);
} }
error_reporting($_error_reporting); error_reporting($_error_reporting);
return true; return true;
} }
} }

View File

@@ -14,22 +14,12 @@
* @package Smarty * @package Smarty
* @subpackage TemplateResources * @subpackage TemplateResources
* *
* @method renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
* @method populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
* @method process(Smarty_Internal_Template $_smarty_tpl) * @method process(Smarty_Internal_Template $_smarty_tpl)
*/ */
abstract class Smarty_Resource abstract class Smarty_Resource
{ {
/**
* resource types provided by the core
*
* @var array
*/
public static $sysplugins = array('file' => 'smarty_internal_resource_file.php',
'string' => 'smarty_internal_resource_string.php',
'extends' => 'smarty_internal_resource_extends.php',
'stream' => 'smarty_internal_resource_stream.php',
'eval' => 'smarty_internal_resource_eval.php',
'php' => 'smarty_internal_resource_php.php');
/** /**
* Source is bypassing compiler * Source is bypassing compiler
* *
@@ -44,6 +34,18 @@ abstract class Smarty_Resource
*/ */
public $recompiled = false; public $recompiled = false;
/**
* resource types provided by the core
*
* @var array
*/
public static $sysplugins = array('file' => 'smarty_internal_resource_file.php',
'string' => 'smarty_internal_resource_string.php',
'extends' => 'smarty_internal_resource_extends.php',
'stream' => 'smarty_internal_resource_stream.php',
'eval' => 'smarty_internal_resource_eval.php',
'php' => 'smarty_internal_resource_php.php');
/** /**
* Flag if resource does implement populateCompiledFilepath() method * Flag if resource does implement populateCompiledFilepath() method
* *
@@ -51,152 +53,6 @@ abstract class Smarty_Resource
*/ */
public $hasCompiledHandler = false; public $hasCompiledHandler = false;
/**
* modify template_resource according to resource handlers specifications
*
* @param \Smarty_Internal_Template|\Smarty $obj Smarty instance
* @param string $template_resource template_resource to extract resource handler and name of
*
* @return string unique resource name
* @throws \SmartyException
*/
public static function getUniqueTemplateName($obj, $template_resource)
{
$smarty = $obj->_getSmartyObj();
list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
// TODO: optimize for Smarty's internal resource types
$resource = Smarty_Resource::load($smarty, $type);
// go relative to a given template?
$_file_is_dotted = $name[ 0 ] == '.' && ($name[ 1 ] == '.' || $name[ 1 ] == '/');
if ($obj->_isTplObj() && $_file_is_dotted &&
($obj->source->type == 'file' || $obj->parent->source->type == 'extends')
) {
$name = $smarty->_realpath(dirname($obj->parent->source->filepath) . DIRECTORY_SEPARATOR . $name);
}
return $resource->buildUniqueResourceName($smarty, $name);
}
/**
* extract resource_type and resource_name from template_resource and config_resource
* @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
*
* @param string $resource_name template_resource or config_resource to parse
* @param string $default_resource the default resource_type defined in $smarty
*
* @return array with parsed resource name and type
*/
public static function parseResourceName($resource_name, $default_resource)
{
if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
$type = $match[ 1 ];
$name = substr($resource_name, strlen($match[ 0 ]));
} else {
// no resource given, use default
// or single character before the colon is not a resource type, but part of the filepath
$type = $default_resource;
$name = $resource_name;
}
return array($name, $type);
}
/**
* Load Resource Handler
*
* @param Smarty $smarty smarty object
* @param string $type name of the resource
*
* @throws SmartyException
* @return Smarty_Resource Resource Handler
*/
public static function load(Smarty $smarty, $type)
{
/* @var array \Smarty_Internal_Resource_File|\Smarty_Internal_Resource_Php|\Smarty_Internal_Resource_Extends|\Smarty_Internal_Resource_Eval|\Smarty_Internal_Resource_String|\Smarty_Internal_Resource_Stream|\Smarty_Internal_Resource_Registered */
static $handlerCache = array();
// try smarty's cache
if (isset($handlerCache[ $type ])) {
return $handlerCache[ $type ];
}
// try registered resource
if (isset($smarty->registered_resources[ $type ])) {
return $handlerCache[ $type ] =
$smarty->registered_resources[ $type ] instanceof Smarty_Resource ?
$smarty->registered_resources[ $type ] : new Smarty_Internal_Resource_Registered();
}
// try sysplugins dir
if (isset(self::$sysplugins[ $type ])) {
$_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
return $handlerCache[ $type ] = new $_resource_class();
}
// try plugins dir
$_resource_class = 'Smarty_Resource_' . ucfirst($type);
if ($smarty->loadPlugin($_resource_class)) {
if (class_exists($_resource_class, false)) {
return $handlerCache[ $type ] = new $_resource_class();
} else {
$smarty->registerResource($type,
array("smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp",
"smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted"));
// give it another try, now that the resource is registered properly
return self::load($smarty, $type);
}
}
// try streams
$_known_stream = stream_get_wrappers();
if (in_array($type, $_known_stream)) {
// is known stream
if (is_object($smarty->security_policy)) {
$smarty->security_policy->isTrustedStream($type);
}
return $handlerCache[ $type ] = new Smarty_Internal_Resource_Stream();
}
// TODO: try default_(template|config)_handler
// give up
throw new SmartyException("Unknown resource type '{$type}'");
}
/**
* modify resource_name according to resource handlers specifications
*
* @param Smarty $smarty Smarty instance
* @param string $resource_name resource_name to make unique
* @param boolean $isConfig flag for config resource
*
* @return string unique resource name
*/
public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
{
if ($isConfig) {
if (!isset($smarty->_joined_config_dir)) {
$smarty->getTemplateDir(null, true);
}
return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
} else {
if (!isset($smarty->_joined_template_dir)) {
$smarty->getTemplateDir();
}
return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
}
}
/**
* initialize Source Object for given resource
* wrapper for backward compatibility to versions < 3.1.22
* Either [$_template] or [$smarty, $template_resource] must be specified
*
* @param Smarty_Internal_Template $_template template object
* @param Smarty $smarty smarty object
* @param string $template_resource resource identifier
*
* @return \Smarty_Template_Source Source Object
* @throws \SmartyException
*/
public static function source(Smarty_Internal_Template $_template = null,
Smarty $smarty = null,
$template_resource = null)
{
return Smarty_Template_Source::load($_template, $smarty, $template_resource);
}
/** /**
* Load template's source into current template object * Load template's source into current template object
* *
@@ -225,12 +81,30 @@ abstract class Smarty_Resource
// intentionally left blank // intentionally left blank
} }
/* /**
* Check if resource must check time stamps when when loading complied or cached templates. * modify resource_name according to resource handlers specifications
* Resources like 'extends' which use source components my disable timestamp checks on own resource.
* *
* @return bool * @param Smarty $smarty Smarty instance
* @param string $resource_name resource_name to make unique
* @param boolean $isConfig flag for config resource
*
* @return string unique resource name
*/ */
public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
{
if ($isConfig) {
if (!isset($smarty->_joined_config_dir)) {
$smarty->getTemplateDir(null, true);
}
return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
} else {
if (!isset($smarty->_joined_template_dir)) {
$smarty->getTemplateDir();
}
return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
}
}
/** /**
* Determine basename for compiled filename * Determine basename for compiled filename
* *
@@ -243,9 +117,138 @@ abstract class Smarty_Resource
return basename(preg_replace('![^\w]+!', '_', $source->name)); return basename(preg_replace('![^\w]+!', '_', $source->name));
} }
/**
* Load Resource Handler
*
* @param Smarty $smarty smarty object
* @param string $type name of the resource
*
* @throws SmartyException
* @return Smarty_Resource Resource Handler
*/
public static function load(Smarty $smarty, $type)
{
// try smarty's cache
if (isset($smarty->_cache[ 'resource_handlers' ][ $type ])) {
return $smarty->_cache[ 'resource_handlers' ][ $type ];
}
// try registered resource
if (isset($smarty->registered_resources[ $type ])) {
return $smarty->_cache[ 'resource_handlers' ][ $type ] =
$smarty->registered_resources[ $type ] instanceof Smarty_Resource ?
$smarty->registered_resources[ $type ] : new Smarty_Internal_Resource_Registered();
}
// try sysplugins dir
if (isset(self::$sysplugins[ $type ])) {
$_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
}
// try plugins dir
$_resource_class = 'Smarty_Resource_' . ucfirst($type);
if ($smarty->loadPlugin($_resource_class)) {
if (class_exists($_resource_class, false)) {
return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
} else {
$smarty->registerResource($type,
array("smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp",
"smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted"));
// give it another try, now that the resource is registered properly
return self::load($smarty, $type);
}
}
// try streams
$_known_stream = stream_get_wrappers();
if (in_array($type, $_known_stream)) {
// is known stream
if (is_object($smarty->security_policy)) {
$smarty->security_policy->isTrustedStream($type);
}
return $smarty->_cache[ 'resource_handlers' ][ $type ] = new Smarty_Internal_Resource_Stream();
}
// TODO: try default_(template|config)_handler
// give up
throw new SmartyException("Unknown resource type '{$type}'");
}
/**
* extract resource_type and resource_name from template_resource and config_resource
* @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
*
* @param string $resource_name template_resource or config_resource to parse
* @param string $default_resource the default resource_type defined in $smarty
*
* @return array with parsed resource name and type
*/
public static function parseResourceName($resource_name, $default_resource)
{
if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
$type = $match[ 1 ];
$name = substr($resource_name, strlen($match[ 0 ]));
} else {
// no resource given, use default
// or single character before the colon is not a resource type, but part of the filepath
$type = $default_resource;
$name = $resource_name;
}
return array($name, $type);
}
/**
* modify template_resource according to resource handlers specifications
*
* @param \Smarty_Internal_Template|\Smarty $obj Smarty instance
* @param string $template_resource template_resource to extract resource handler and name of
*
* @return string unique resource name
*/
public static function getUniqueTemplateName($obj, $template_resource)
{
$smarty = $obj->_getSmartyObj();
list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
// TODO: optimize for Smarty's internal resource types
$resource = Smarty_Resource::load($smarty, $type);
// go relative to a given template?
$_file_is_dotted = $name[ 0 ] == '.' && ($name[ 1 ] == '.' || $name[ 1 ] == '/');
if ($obj->_isTplObj() && $_file_is_dotted &&
($obj->source->type == 'file' || $obj->parent->source->type == 'extends')
) {
$name = $smarty->_realpath(dirname($obj->parent->source->filepath) . $smarty->ds . $name);
}
return $resource->buildUniqueResourceName($smarty, $name);
}
/*
* Check if resource must check time stamps when when loading complied or cached templates.
* Resources like 'extends' which use source components my disable timestamp checks on own resource.
*
* @return bool
*/
public function checkTimestamps() public function checkTimestamps()
{ {
return true; return true;
} }
/**
* initialize Source Object for given resource
* wrapper for backward compatibility to versions < 3.1.22
* Either [$_template] or [$smarty, $template_resource] must be specified
*
* @param Smarty_Internal_Template $_template template object
* @param Smarty $smarty smarty object
* @param string $template_resource resource identifier
*
* @return Smarty_Template_Source Source Object
*/
public static function source(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
$template_resource = null)
{
return Smarty_Template_Source::load($_template, $smarty, $template_resource);
}
} }

View File

@@ -7,9 +7,11 @@
* @package Smarty * @package Smarty
* @subpackage TemplateResources * @subpackage TemplateResources
* @author Rodney Rehm * @author Rodney Rehm
* @property string $content compiled content
*/ */
class Smarty_Template_Compiled extends Smarty_Template_Resource_Base class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
{ {
/** /**
* nocache hash * nocache hash
* *
@@ -20,11 +22,11 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
/** /**
* get a Compiled Object of this source * get a Compiled Object of this source
* *
* @param \Smarty_Internal_Template $_template template object * @param Smarty_Internal_Template $_template template object
* *
* @return \Smarty_Template_Compiled compiled object * @return Smarty_Template_Compiled compiled object
*/ */
static function load(\Smarty_Internal_Template $_template) static function load($_template)
{ {
$compiled = new Smarty_Template_Compiled(); $compiled = new Smarty_Template_Compiled();
if ($_template->source->handler->hasCompiledHandler) { if ($_template->source->handler->hasCompiledHandler) {
@@ -38,31 +40,30 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
/** /**
* populate Compiled Object with compiled filepath * populate Compiled Object with compiled filepath
* *
* @param \Smarty_Internal_Template $_template template object * @param Smarty_Internal_Template $_template template object
**/ **/
public function populateCompiledFilepath(\Smarty_Internal_Template $_template) public function populateCompiledFilepath(Smarty_Internal_Template $_template)
{ {
$source = &$_template->source; $source = &$_template->source;
$smarty = &$_template->smarty; $smarty = &$_template->smarty;
$this->filepath = $smarty->getCompileDir(); $this->filepath = $smarty->getCompileDir();
if (isset($_template->compile_id)) { if (isset($_template->compile_id)) {
$this->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $this->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) .
($smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'); ($smarty->use_sub_dirs ? $smarty->ds : '^');
} }
$uid = $source->uid;
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) { if ($smarty->use_sub_dirs) {
$this->filepath .= $uid[ 0 ] . $uid[ 1 ] . DIRECTORY_SEPARATOR . $uid[ 2 ] . $this->filepath .= $source->uid[ 0 ] . $source->uid[ 1 ] . $smarty->ds . $source->uid[ 2 ] .
$uid[ 3 ] . DIRECTORY_SEPARATOR . $uid[ 4 ] . $uid[ 5 ] . DIRECTORY_SEPARATOR; $source->uid[ 3 ] . $smarty->ds . $source->uid[ 4 ] . $source->uid[ 5 ] . $smarty->ds;
} }
$this->filepath .= $uid . '_'; $this->filepath .= $source->uid . '_';
if ($source->isConfig) { if ($source->isConfig) {
$this->filepath .= (int)$smarty->config_read_hidden + (int)$smarty->config_booleanize * 2 + $this->filepath .= (int) $smarty->config_read_hidden + (int) $smarty->config_booleanize * 2 +
(int)$smarty->config_overwrite * 4; (int) $smarty->config_overwrite * 4;
} else { } else {
$this->filepath .= (int)$smarty->merge_compiled_includes + (int)$smarty->escape_html * 2 + $this->filepath .= (int) $smarty->merge_compiled_includes + (int) $smarty->escape_html * 2 +
(($smarty->merge_compiled_includes && $source->type === 'extends') ? (($smarty->merge_compiled_includes && $source->type === 'extends') ?
(int)$smarty->extends_recursion * 4 : 0); (int) $smarty->extends_recursion * 4 : 0);
} }
$this->filepath .= '.' . $source->type; $this->filepath .= '.' . $source->type;
$basename = $source->handler->getBasename($source); $basename = $source->handler->getBasename($source);
@@ -73,26 +74,21 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$this->filepath .= '.cache'; $this->filepath .= '.cache';
} }
$this->filepath .= '.php'; $this->filepath .= '.php';
if ($smarty->force_compile) { $this->timestamp = $this->exists = is_file($this->filepath);
// no need to obtain file status if ($this->exists) {
$this->timestamp = $this->exists = false; $this->timestamp = filemtime($this->filepath);
} else {
$this->timestamp = $this->exists = is_file($this->filepath);
if ($this->exists) {
$this->timestamp = filemtime($this->filepath);
}
} }
} }
/** /**
* render compiled template code * render compiled template code
* *
* @param \Smarty_Internal_Template $_template * @param Smarty_Internal_Template $_template
* *
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function render(\Smarty_Internal_Template $_template) public function render(Smarty_Internal_Template $_template)
{ {
// checks if template exists // checks if template exists
if (!$_template->source->exists) { if (!$_template->source->exists) {
@@ -128,17 +124,17 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
/** /**
* load compiled template or compile from source * load compiled template or compile from source
* *
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template * @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
* *
* @throws Exception * @throws Exception
*/ */
public function process(\Smarty_Internal_Template $_smarty_tpl) public function process(Smarty_Internal_Template $_smarty_tpl)
{ {
$source = &$_smarty_tpl->source; $source = &$_smarty_tpl->source;
$smarty = &$_smarty_tpl->smarty; $smarty = &$_smarty_tpl->smarty;
if ($source->handler->recompiled) { if ($source->handler->recompiled) {
$source->handler->process($_smarty_tpl); $source->handler->process($_smarty_tpl);
} else if (!$source->handler->uncompiled) { } elseif (!$source->handler->uncompiled) {
if (!$this->exists || $smarty->force_compile || if (!$this->exists || $smarty->force_compile ||
($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp()) ($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp())
) { ) {
@@ -166,11 +162,11 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
/** /**
* compile template from source * compile template from source
* *
* @param \Smarty_Internal_Template $_template * @param Smarty_Internal_Template $_template
* *
* @throws Exception * @throws Exception
*/ */
public function compileTemplateSource(\Smarty_Internal_Template $_template) public function compileTemplateSource(Smarty_Internal_Template $_template)
{ {
$this->file_dependency = array(); $this->file_dependency = array();
$this->includes = array(); $this->includes = array();
@@ -199,38 +195,15 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
unset($_template->compiler); unset($_template->compiler);
} }
/**
* Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*/
private function loadCompiledTemplate(\Smarty_Internal_Template $_smarty_tpl)
{
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($this->filepath, true);
} else if (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath);
}
if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath));
} else {
include($this->filepath);
}
}
/** /**
* Write compiled code by handler * Write compiled code by handler
* *
* @param \Smarty_Internal_Template $_template template object * @param Smarty_Internal_Template $_template template object
* @param string $code compiled code * @param string $code compiled code
* *
* @return bool success * @return boolean success
* @throws \SmartyException
*/ */
public function write(\Smarty_Internal_Template $_template, $code) public function write(Smarty_Internal_Template $_template, $code)
{ {
if (!$_template->source->handler->recompiled) { if (!$_template->source->handler->recompiled) {
if ($_template->smarty->ext->_writeFile->writeFile($this->filepath, $code, $_template->smarty) === true) { if ($_template->smarty->ext->_writeFile->writeFile($this->filepath, $code, $_template->smarty) === true) {
@@ -245,14 +218,36 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
return true; return true;
} }
/**
* Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*/
private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl)
{
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($this->filepath, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath);
}
if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath));
} else {
include($this->filepath);
}
}
/** /**
* Read compiled content from handler * Read compiled content from handler
* *
* @param \Smarty_Internal_Template $_template template object * @param Smarty_Internal_Template $_template template object
* *
* @return string content * @return string content
*/ */
public function read(\Smarty_Internal_Template $_template) public function read(Smarty_Internal_Template $_template)
{ {
if (!$_template->source->handler->recompiled) { if (!$_template->source->handler->recompiled) {
return file_get_contents($this->filepath); return file_get_contents($this->filepath);