- bugfix sha1() calculations at extends resource and some general improvments on sha1() handling

This commit is contained in:
Uwe.Tews
2010-01-05 21:10:25 +00:00
parent a6c2f5f8d4
commit d287c6b1d9
8 changed files with 122 additions and 117 deletions

View File

@@ -1,3 +1,7 @@
01/05/2009
- bugfix sha1() calculations at extends resource and some general improvments on sha1() handling
01/03/2009
- internal change on building cache files

View File

@@ -22,56 +22,82 @@ class Smarty_Internal_CacheResource_File {
/**
* Returns the filepath of the cached template output
*
* @param object $template current template
* @param object $_template current template
* @return string the cache filepath
*/
public function getCachedFilepath($template)
public function getCachedFilepath($_template)
{
return $this->buildCachedFilepath ($template->getTemplateFilepath(), $template->cache_id, $template->compile_id);
$_source_file_path = str_replace(':', '.', $_template->getTemplateFilepath());
$_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
$_filepath = $_template->templateUid;
// if use_sub_dirs, break file into directories
if ($this->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS
. substr($_filepath, 2, 2) . DS
. substr($_filepath, 4, 2) . DS
. $_filepath;
}
$_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
if (isset($_cache_id)) {
$_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
} else {
$_cache_id = '';
}
if (isset($_compile_id)) {
$_compile_id = $_compile_id . $_compile_dir_sep;
} else {
$_compile_id = '';
}
$_cache_dir = $this->smarty->cache_dir;
if (strpos('/\\', substr($_cache_dir, -1)) === false) {
$_cache_dir .= DS;
}
return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
}
/**
* Returns the timpestamp of the cached template output
*
* @param object $template current template
* @param object $_template current template
* @return integer |booelan the template timestamp or false if the file does not exist
*/
public function getCachedTimestamp($template)
public function getCachedTimestamp($_template)
{
// return @filemtime ($template->getCachedFilepath());
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 ;
}
/**
* Returns the cached template output
*
* @param object $template current template
* @param object $_template current template
* @return string |booelan the template content or false if the file does not exist
*/
public function getCachedContents($template)
public function getCachedContents($_template)
{
ob_start();
$_smarty_tpl = $template;
include $template->getCachedFilepath();
$_smarty_tpl = $_template;
include $_template->getCachedFilepath();
return ob_get_clean();
}
/**
* Writes the rendered template output to cache file
*
* @param object $template current template
* @param object $_template current template
* @return boolean status
*/
public function writeCachedContent($template, $content)
public function writeCachedContent($_template, $content)
{
if (!$template->resource_object->isEvaluated) {
if (Smarty_Internal_Write_File::writeFile($template->getCachedFilepath(), $content, $this->smarty) === true) {
$template->cached_timestamp = filemtime($template->getCachedFilepath());
if (!$_template->resource_object->isEvaluated) {
if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
$_template->cached_timestamp = filemtime($_template->getCachedFilepath());
return true;
}
}
}
}
return false;
}
}
/**
* Empty cache folder
@@ -128,7 +154,7 @@ class Smarty_Internal_CacheResource_File {
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;
@@ -153,44 +179,6 @@ class Smarty_Internal_CacheResource_File {
}
return $_count;
}
/**
* Get system filepath to cached file
*
* @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 ($source_file_path, $cache_id, $compile_id)
{
$_source_file_path = str_replace(':', '.', $source_file_path);
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
$_filepath = sha1($_source_file_path);
// if use_sub_dirs, break file into directories
if ($this->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS
. substr($_filepath, 2, 2) . DS
. substr($_filepath, 4, 2) . DS
. $_filepath;
}
$_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
if (isset($_cache_id)) {
$_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
} else {
$_cache_id = '';
}
if (isset($_compile_id)) {
$_compile_id = $_compile_id . $_compile_dir_sep;
} else {
$_compile_id = '';
}
$_cache_dir = $this->smarty->cache_dir;
if (strpos('/\\', substr($_cache_dir, -1)) === false) {
$_cache_dir .= DS;
}
return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
}
}
?>

View File

@@ -97,7 +97,11 @@ class Smarty_Internal_Debug extends Smarty_Internal_Data {
*/
static function get_key($template)
{
$key = sha1($template->getTemplateFilepath());
// calculate Uid if not already done
if ($template->templateUid == '') {
$template->getTemplateFilepath();
}
$key = $template->templateUid;
if (isset(self::$template_data[$key])) {
return $key;
} else {

View File

@@ -20,20 +20,21 @@ class Smarty_Internal_Resource_Extends {
// classes used for compiling Smarty templates from file resource
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
public $template_parser_class = 'Smarty_Internal_Templateparser';
public $template_parser_class = 'Smarty_Internal_Templateparser';
// properties
public $usesCompiler = true;
public $isEvaluated = false;
public $allFilepaths = array();
/**
* Return flag if template source is existing
*
* @param object $template template object
* @param object $_template template object
* @return boolean result
*/
public function isExisting($template)
public function isExisting($_template)
{
if ($template->getTemplateFilepath() === false) {
if ($_template->getTemplateFilepath() === false) {
return false;
} else {
return true;
@@ -42,56 +43,59 @@ class Smarty_Internal_Resource_Extends {
/**
* Get filepath to template source
*
* @param object $template template object
* @param object $_template template object
* @return string filepath to template source file
*/
public function getTemplateFilepath($template)
public function getTemplateFilepath($_template)
{
$_files = explode('|', $template->resource_name);
$_filepath = $template->buildTemplateFilepath ($_files[count($_files)-1]);
if ($_filepath !== false) {
if ($template->security) {
$template->smarty->security_handler->isTrustedResourceDir($_filepath);
$sha1String = '';
$_files = explode('|', $_template->resource_name);
foreach ($_files as $_file) {
$_filepath = $_template->buildTemplateFilepath ($_file);
if ($_filepath !== false) {
if ($_template->security) {
$_template->smarty->security_handler->isTrustedResourceDir($_filepath);
}
}
$sha1String .= $_filepath;
$this->allFilepaths[] = $_filepath;
}
$_template->templateUid = sha1($sha1String);
return $_filepath;
}
/**
* Get timestamp to template source
*
* @param object $template template object
* @param object $_template template object
* @return integer timestamp of template source file
*/
public function getTemplateTimestamp($template)
public function getTemplateTimestamp($_template)
{
return filemtime($template->getTemplateFilepath());
return filemtime($_template->getTemplateFilepath());
}
/**
* Read template source from file
*
* @param object $template template object
* @param object $_template template object
* @return string content of template source file
*/
public function getTemplateSource($template)
public function getTemplateSource($_template)
{
$this->template = $template;
// $saved_filepath = $template->getTemplateFilepath();
$_files = explode('|', $template->resource_name);
$_files = array_reverse($_files);
foreach ($_files as $_file) {
$_filepath = $template->buildTemplateFilepath ($_file);
$this->template = $_template;
$_files = array_reverse($this->allFilepaths);
foreach ($_files as $_filepath) {
// read template file
if ($_filepath === false) {
throw new Exception("Unable to load template \"file : {$_file}\"");
}
if ($_file != $_files[0]) {
$template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath));
if ($_filepath != $_files[0]) {
$_template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath));
}
$template->template_filepath = $_filepath;
$_template->template_filepath = $_filepath;
$_content = file_get_contents($_filepath);
if ($_file != $_files[count($_files)-1]) {
if ($_filepath != $_files[count($_files)-1]) {
if (preg_match_all('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')/', $_content, $_open, PREG_OFFSET_CAPTURE) !=
preg_match_all('/(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/', $_content, $_close, PREG_OFFSET_CAPTURE)) {
$this->smarty->trigger_error(" unmatched {block} {/block} pairs");
@@ -103,11 +107,11 @@ class Smarty_Internal_Resource_Extends {
$this->saveBlockData($_block_content, $_open[0][$_i][0], $_filepath);
}
} else {
$template->template_source = $_content;
$_template->template_source = $_content;
return true;
}
}
// $template->template_filepath = $saved_filepath;
// $_template->template_filepath = $saved_filepath;
}
protected function saveBlockData($block_content, $block_tag, $_filepath)
{
@@ -141,35 +145,39 @@ class Smarty_Internal_Resource_Extends {
/**
* Get filepath to compiled template
*
* @param object $template template object
* @param object $_template template object
* @return string return path to compiled template
*/
public function getCompiledFilepath($template)
public function getCompiledFilepath($_template)
{
$_compile_id = isset($template->compile_id) ? preg_replace('![^\w\|]+!', '_', $template->compile_id) : null;
$_files = explode('|', $template->resource_name);
$_filepath = sha1($template->getTemplateFilepath());
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
$_files = explode('|', $_template->resource_name);
// calculate Uid if not already done
if ($_template->templateUid == '') {
$_template->getTemplateFilepath();
}
$_filepath = $_template->templateUid;
// if use_sub_dirs, break file into directories
if ($template->smarty->use_sub_dirs) {
if ($_template->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS
. substr($_filepath, 2, 2) . DS
. substr($_filepath, 4, 2) . DS
. $_filepath;
}
$_compile_dir_sep = $template->smarty->use_sub_dirs ? DS : '^';
$_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
if (isset($_compile_id)) {
$_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
}
if ($template->caching) {
if ($_template->caching) {
$_cache = '.cache';
} else {
$_cache = '';
}
$_compile_dir = $template->smarty->compile_dir;
$_compile_dir = $_template->smarty->compile_dir;
if (substr($_compile_dir, -1) != DS) {
$_compile_dir .= DS;
}
return $_compile_dir . $_filepath . '.' . $template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
}
}

View File

@@ -20,7 +20,7 @@ class Smarty_Internal_Resource_File {
// classes used for compiling Smarty templates from file resource
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
public $template_parser_class = 'Smarty_Internal_Templateparser';
public $template_parser_class = 'Smarty_Internal_Templateparser';
// properties
public $usesCompiler = true;
public $isEvaluated = false;
@@ -54,6 +54,7 @@ class Smarty_Internal_Resource_File {
$_template->smarty->security_handler->isTrustedResourceDir($_filepath);
}
}
$_template->templateUid = sha1($_filepath);
return $_filepath;
}
@@ -92,9 +93,13 @@ class Smarty_Internal_Resource_File {
* @return string return path to compiled template
*/
public function getCompiledFilepath($_template)
{
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!','_',$_template->compile_id) : null;
$_filepath = sha1($_template->getTemplateFilepath());
{
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
// calculate Uid if not already done
if ($_template->templateUid == '') {
$_template->getTemplateFilepath();
}
$_filepath = $_template->templateUid;
// if use_sub_dirs, break file into directories
if ($_template->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS
@@ -115,7 +120,7 @@ class Smarty_Internal_Resource_File {
if (strpos('/\\', substr($_compile_dir, -1)) === false) {
$_compile_dir .= DS;
}
return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_template->resource_name). $_cache . '.php';
return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_template->resource_name) . $_cache . '.php';
}
}

View File

@@ -52,7 +52,7 @@ class Smarty_Internal_Resource_PHP {
if ($_template->security) {
$_template->smarty->security_handler->isTrustedResourceDir($_filepath);
}
$_template->templateUid = sha1($_filepath);
return $_filepath;
}

View File

@@ -50,7 +50,7 @@ class Smarty_Internal_Resource_Registered {
// no filepath for strings
// return "string" for compiler error messages
$_filepath = $_template->resource_type .':'.$_template->resource_name;
$_template->templateUid = sha1($_filepath);
return $_filepath;
}
@@ -105,7 +105,11 @@ class Smarty_Internal_Resource_Registered {
public function getCompiledFilepath($_template)
{
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!','_',$_template->compile_id) : null;
$_filepath = sha1($_template->template_resource);
// calculate Uid if not already done
if ($_template->templateUid == '') {
$_template->getTemplateFilepath();
}
$_filepath = $_template->$templateUid;
// if use_sub_dirs, break file into directories
if ($_template->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS

View File

@@ -31,7 +31,8 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
public $resource_type = null;
public $resource_name = null;
public $resource_object = null;
private $isExisting = null;
private $isExisting = null;
public $templateUid = '';
// Template source
public $template_filepath = null;
public $template_source = null;
@@ -243,7 +244,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
{
if (!$this->resource_object->isEvaluated) {
$this->properties['file_dependency'] = array();
$this->properties['file_dependency'][sha1($this->getTemplateFilepath())] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
$this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
}
if ($this->smarty->debugging) {
Smarty_Internal_Debug::start_compile($this);
@@ -260,15 +261,6 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
if (!$this->resource_object->isEvaluated) {
// write compiled template
Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty);
// make template and compiled file timestamp match
/**
* $this->compiled_timestamp = null;
* touch($this->getCompiledFilepath(), $this->getTemplateTimestamp());
* // daylight saving time problem on windows
* if ($this->template_timestamp != $this->getCompiledTimestamp()) {
* touch($this->getCompiledFilepath(), 2 * $this->template_timestamp - $this->compiled_timestamp);
* }
*/
}
} else {
// error compiling template
@@ -452,7 +444,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
}
$this->rendered_content = ob_get_clean();
if (!$this->resource_object->isEvaluated) {
$this->properties['file_dependency'][sha1($this->getTemplateFilepath())] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
$this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
}
if ($this->parent instanceof Smarty_Template or $this->parent instanceof Smarty_Internal_Template) {
$this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']);