mirror of
https://github.com/smarty-php/smarty.git
synced 2026-04-28 18:02:07 +02:00
--- this is a major update with a couple of internal changes ---
- new config file lexer/parser (thanks to Thue Jnaus Kristensen)
- template lexer/parser fixes for PHP and {literal} handing (thanks to Thue Jnaus Kristensen)
- fix on registered plugins with different type but same name
- rewrite of plugin handling (optimized execution speed)
- closed a security hole regarding PHP code injection into cache files
- fixed bug in clear cache handling
- Renamed a couple of internal classes
- code cleanup for merging compiled templates
- couple of runtime optimizations (still not all done)
This commit is contained in:
@@ -38,7 +38,8 @@ class Smarty_Internal_CacheResource_File {
|
||||
*/
|
||||
public function getCachedTimestamp($template)
|
||||
{
|
||||
return ($template->getCachedFilepath() && file_exists($template->getCachedFilepath())) ? filemtime($template->getCachedFilepath()) : false ;
|
||||
return @filemtime($template->getCachedFilepath());
|
||||
// return ($template->getCachedFilepath() && file_exists($template->getCachedFilepath())) ? filemtime($template->getCachedFilepath()) : false ;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +64,7 @@ class Smarty_Internal_CacheResource_File {
|
||||
*/
|
||||
public function writeCachedContent($template, $content)
|
||||
{
|
||||
if (!$template->isEvaluated()) {
|
||||
if (!$template->resource_object->isEvaluated) {
|
||||
return Smarty_Internal_Write_File::writeFile($template->getCachedFilepath(), $content, $this->smarty);
|
||||
} else {
|
||||
return false;
|
||||
@@ -91,51 +92,61 @@ class Smarty_Internal_CacheResource_File {
|
||||
*/
|
||||
public function clear($resource_name, $cache_id, $compile_id, $exp_time)
|
||||
{
|
||||
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!','_',$cache_id) : null;
|
||||
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!','_',$compile_id) : null;
|
||||
$_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
|
||||
if (isset($resource_name)) {
|
||||
$_resource_part = (string)abs(crc32($resource_name)) . '.' . $resource_name . '.php';
|
||||
} else {
|
||||
$_resource_part = null;
|
||||
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
|
||||
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
|
||||
$_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^';
|
||||
$_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0;
|
||||
$_dir = rtrim($this->smarty->cache_dir, '/\\') . DS;
|
||||
$_dir_length = strlen($_dir);
|
||||
if (isset($_cache_id)) {
|
||||
$_cache_id_parts = explode('|', $_cache_id);
|
||||
$_cache_id_parts_count = count($_cache_id_parts);
|
||||
}
|
||||
$_dir = $this->smarty->cache_dir;
|
||||
if (strpos('/\\', substr($_dir, -1)) === false) {
|
||||
$_dir .= DS;
|
||||
}
|
||||
if ($this->smarty->use_sub_dirs && isset($_cache_id)) {
|
||||
$_dir .= str_replace('|', $_dir_sep, $_cache_id) . $_dir_sep;
|
||||
}
|
||||
$_compile_pos = $this->smarty->use_sub_dirs ? 5 : 2;
|
||||
$_count = 0;
|
||||
$_cacheDirs = new RecursiveDirectoryIterator($_dir);
|
||||
$_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
|
||||
foreach ($_cache as $_file) {
|
||||
if (strpos($_file, '.svn') !== false) continue;
|
||||
if (strpos($_file, '.svn') !== false) continue;
|
||||
// directory ?
|
||||
if ($_file->isDir()) {
|
||||
if (!$_cache->isDot()) {
|
||||
// delete folder if empty
|
||||
@rmdir($_file->getPathname());
|
||||
}
|
||||
} else {
|
||||
$_parts = explode($_dir_sep, $_file);
|
||||
$_parts_count = count($_parts);
|
||||
$_parts_compile_pos = $_parts_count - $_compile_pos;
|
||||
if ($_parts_compile_pos < 0) {
|
||||
$_parts_compile_pos = 0;
|
||||
}
|
||||
if ((substr_compare((string)$_file, $_dir, 0, strlen($_dir)) == 0 &&
|
||||
(!isset($resource_name) || $_parts[$_parts_count-1] == $_resource_part) &&
|
||||
(!isset($_compile_id) || $_parts[$_parts_compile_pos] == $_compile_id)) ||
|
||||
(isset($resource_name) && (string)$_file == $_dir . $_resource_part)) {
|
||||
if (isset($exp_time)) {
|
||||
if (time() - @filemtime($_file) >= $exp_time) {
|
||||
$_count += @unlink((string) $_file) ? 1 : 0;
|
||||
}
|
||||
} else {
|
||||
$_count += @unlink((string) $_file) ? 1 : 0;
|
||||
$_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
|
||||
$_parts_count = count($_parts);
|
||||
// check name
|
||||
if (isset($resource_name)) {
|
||||
$_filename_parts = explode('.', $_parts[$_parts_count-1]);
|
||||
$_resourcename_parts = explode('.', $resource_name . '.php');
|
||||
if (count($_filename_parts)-1 != count($_resourcename_parts)) {
|
||||
continue;
|
||||
}
|
||||
for ($i = 0; $i < count($_resourcename_parts); $i++) {
|
||||
if ($_filename_parts[$i + 1] != $_resourcename_parts[$i]) continue 2;
|
||||
}
|
||||
}
|
||||
// check compile id
|
||||
if (isset($_compile_id) && $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id) {
|
||||
continue;
|
||||
}
|
||||
// check cache id
|
||||
if (isset($_cache_id)) {
|
||||
// count of cache id parts
|
||||
$_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
|
||||
if ($_parts_count < $_cache_id_parts_count) {
|
||||
continue;
|
||||
}
|
||||
for ($i = 0; $i < $_cache_id_parts_count; $i++) {
|
||||
if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
|
||||
}
|
||||
}
|
||||
// expired ?
|
||||
if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
|
||||
continue;
|
||||
}
|
||||
$_count += @unlink((string) $_file) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
return $_count;
|
||||
@@ -143,17 +154,16 @@ class Smarty_Internal_CacheResource_File {
|
||||
/**
|
||||
* Get system filepath to cached file
|
||||
*
|
||||
* @param string $resource_name template name
|
||||
* @param string $source_file_path template source file path
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @return string filepath of cache file
|
||||
*/
|
||||
private function buildCachedFilepath ($resource_name, $cache_id, $compile_id)
|
||||
private function buildCachedFilepath ($source_file_path, $cache_id, $compile_id)
|
||||
{
|
||||
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!','_',$cache_id) : null;
|
||||
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!','_',$compile_id) : null;
|
||||
$_files = explode('|', $resource_name);
|
||||
$_filepath = (string)abs(crc32($resource_name));
|
||||
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
|
||||
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
|
||||
$_filepath = (string)abs(crc32($source_file_path));
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($this->smarty->use_sub_dirs) {
|
||||
$_filepath = substr($_filepath, 0, 2) . DS
|
||||
@@ -176,8 +186,7 @@ class Smarty_Internal_CacheResource_File {
|
||||
if (strpos('/\\', substr($_cache_dir, -1)) === false) {
|
||||
$_cache_dir .= DS;
|
||||
}
|
||||
|
||||
return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_files[count($_files)-1]) . '.php';
|
||||
return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($source_file_path) . '.php';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user