mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-06 03:14:27 +02:00
- use sha1() for filepath encoding
- updates on nocache_hash handling
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
12/29/2009
|
||||
- use sha1() for filepath encoding
|
||||
- updates on nocache_hash handling
|
||||
|
||||
12/28/2009
|
||||
- update for security fixes
|
||||
- make modifier plugins always trusted
|
||||
|
@@ -177,7 +177,7 @@ class Smarty extends Smarty_Internal_Data {
|
||||
// dummy parent object
|
||||
public $parent = null;
|
||||
// global template functions
|
||||
public $template_functions = null;
|
||||
public $template_functions = array();
|
||||
// resource type used if none given
|
||||
public $default_resource_type = 'file';
|
||||
// caching type
|
||||
|
@@ -163,7 +163,7 @@ class Smarty_Internal_CacheResource_File {
|
||||
{
|
||||
$_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));
|
||||
$_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
|
||||
|
@@ -33,7 +33,7 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
|
||||
// create template object
|
||||
$_template = new $compiler->smarty->template_class($include_file, $this->smarty, $compiler->template);
|
||||
// save file dependency
|
||||
$compiler->template->properties['file_dependency']['F' . abs(crc32($_template->getTemplateFilepath()))] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
|
||||
$compiler->template->properties['file_dependency'][sha1($_template->getTemplateFilepath())] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
|
||||
$_old_source = $compiler->template->template_source;
|
||||
if (preg_match_all('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')/', $_old_source, $s, PREG_OFFSET_CAPTURE) !=
|
||||
preg_match_all('/(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/', $_old_source, $c, PREG_OFFSET_CAPTURE)) {
|
||||
|
@@ -26,7 +26,7 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
|
||||
$this->optional_attributes = array('_any');
|
||||
// check and get attributes
|
||||
$_attr = $this->_get_attributes($args);
|
||||
$save = array($_attr, $compiler->template->extracted_compiled_code, $compiler->template->extract_code);
|
||||
$save = array($_attr, $compiler->template->extracted_compiled_code, $compiler->template->extract_code, $compiler->template->has_nocache_code);
|
||||
$this->_open_tag('function', $save);
|
||||
$_name = trim($_attr['name'], "'");
|
||||
unset($_attr['name']);
|
||||
@@ -38,6 +38,7 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
|
||||
$compiler->template->extract_code = true;
|
||||
$compiler->template->extracted_compiled_code = '';
|
||||
$compiler->template->has_code = false;
|
||||
$compiler->template->has_nocache_code = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -56,16 +57,21 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
public function compile($args, $compiler)
|
||||
{
|
||||
$this->compiler = $compiler;
|
||||
$this->compiler->has_code = false;
|
||||
$this->compiler->has_code = false;
|
||||
$_attr = $this->_get_attributes($args);
|
||||
$saved_data = $this->_close_tag(array('function'));
|
||||
$saved_data = $this->_close_tag(array('function'));
|
||||
$_name = trim($saved_data[0]['name'], "'");
|
||||
$compiler->template->properties['function'][$_name]['compiled'] = $compiler->template->extracted_compiled_code;
|
||||
$compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
|
||||
$compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
|
||||
$this->compiler->smarty->template_functions[$_name]['compiled'] = $compiler->template->extracted_compiled_code;
|
||||
$this->compiler->smarty->template_functions[$_name]['parameter'] = $compiler->template->properties['function'][$_name]['parameter'];
|
||||
$this->compiler->smarty->template_functions[$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
|
||||
$this->compiler->smarty->template_functions[$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
|
||||
// restore old code extraction status
|
||||
$compiler->template->extracted_compiled_code = $saved_data[1];
|
||||
$compiler->template->extract_code = $saved_data[2];
|
||||
$compiler->template->has_nocache_code = $saved_data[3];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -43,12 +43,17 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
|
||||
if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
|
||||
// make sure that template is up to date and merge template properties
|
||||
$tpl->renderTemplate();
|
||||
// compiled code for {function} tags
|
||||
$compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
|
||||
// get compiled code
|
||||
$compiled_tpl = $tpl->getCompiledTemplate();
|
||||
// remove header code
|
||||
$compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
|
||||
// replace nocache_hash
|
||||
$compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
|
||||
$compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
|
||||
if ($tpl->has_nocache_code) {
|
||||
// replace nocache_hash
|
||||
$compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
|
||||
$compiler->template->has_nocache_code = true;
|
||||
}
|
||||
$has_compiled_template = true;
|
||||
}
|
||||
}
|
||||
|
@@ -34,7 +34,13 @@ class Smarty_Internal_Compile_Private_Function_Call extends Smarty_Internal_Comp
|
||||
}
|
||||
$_name = trim($_attr['name'], "'");
|
||||
// create template object
|
||||
$_output = "<?php \$_template = new {$compiler->smarty->template_class} ('string:', \$_smarty_tpl->smarty, \$_smarty_tpl);\n";
|
||||
$_output = "<?php \$_template = new {$compiler->smarty->template_class} ('string:', \$_smarty_tpl->smarty, \$_smarty_tpl);\n";
|
||||
$_output .= "\$_template->properties['nocache_hash'] = \$_smarty_tpl->smarty->template_functions['$_name']['nocache_hash'];\n";
|
||||
// set flag (compiled code of {function} must be included in cache file
|
||||
if ($this->compiler->nocache || $this->compiler->tag_nocache) {
|
||||
$compiler->smarty->template_functions[$_name]['called_nocache'] = true;
|
||||
$compiler->template->properties['function'][$_name]['called_nocache'] = true;
|
||||
}
|
||||
// assign default paramter
|
||||
if (isset($this->smarty->template_functions[$_name]['parameter'])) {
|
||||
// function is already compiled
|
||||
|
@@ -134,7 +134,7 @@ class Smarty_Internal_Config {
|
||||
{
|
||||
$_flag = (int)$this->smarty->config_read_hidden + (int)$this->smarty->config_booleanize * 2 +
|
||||
(int)$this->smarty->config_overwrite * 4;
|
||||
$_filepath = (string)abs(crc32($this->config_resource_name . $_flag));
|
||||
$_filepath = sha1($this->config_resource_name . $_flag);
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($this->smarty->use_sub_dirs) {
|
||||
$_filepath = substr($_filepath, 0, 2) . DS
|
||||
@@ -225,9 +225,9 @@ class Smarty_Internal_Config {
|
||||
public function loadConfigVars ($sections = null, $scope)
|
||||
{
|
||||
if (isset($this->template)) {
|
||||
$this->template->properties['file_dependency']['F' . abs(crc32($this->getConfigFilepath()))] = array($this->getConfigFilepath(), $this->getTimestamp());
|
||||
$this->template->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
|
||||
} else {
|
||||
$this->smarty->properties['file_dependency']['F' . abs(crc32($this->getConfigFilepath()))] = array($this->getConfigFilepath(), $this->getTimestamp());
|
||||
$this->smarty->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
|
||||
}
|
||||
$config_data = unserialize($this->getCompiledConfig());
|
||||
// var_dump($config_data);
|
||||
|
@@ -97,7 +97,7 @@ class Smarty_Internal_Debug extends Smarty_Internal_Data {
|
||||
*/
|
||||
static function get_key($template)
|
||||
{
|
||||
$key = 'F' . abs(crc32($template->getTemplateFilepath()));
|
||||
$key = sha1($template->getTemplateFilepath());
|
||||
if (isset(self::$template_data[$key])) {
|
||||
return $key;
|
||||
} else {
|
||||
|
@@ -77,7 +77,7 @@ class Smarty_Internal_Resource_Extends {
|
||||
public function getTemplateSource($template)
|
||||
{
|
||||
$this->template = $template;
|
||||
$saved_filepath = $template->getTemplateFilepath();
|
||||
// $saved_filepath = $template->getTemplateFilepath();
|
||||
$_files = explode('|', $template->resource_name);
|
||||
$_files = array_reverse($_files);
|
||||
foreach ($_files as $_file) {
|
||||
@@ -87,7 +87,7 @@ class Smarty_Internal_Resource_Extends {
|
||||
throw new Exception("Unable to load template \"file : {$_file}\"");
|
||||
}
|
||||
if ($_file != $_files[0]) {
|
||||
$template->properties['file_dependency']['F' . abs(crc32($_filepath))] = array($_filepath, filemtime($_filepath));
|
||||
$template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath));
|
||||
}
|
||||
$template->template_filepath = $_filepath;
|
||||
$_content = file_get_contents($_filepath);
|
||||
@@ -107,7 +107,7 @@ class Smarty_Internal_Resource_Extends {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$template->template_filepath = $saved_filepath;
|
||||
// $template->template_filepath = $saved_filepath;
|
||||
}
|
||||
protected function saveBlockData($block_content, $block_tag, $_filepath)
|
||||
{
|
||||
@@ -148,7 +148,7 @@ class Smarty_Internal_Resource_Extends {
|
||||
{
|
||||
$_compile_id = isset($template->compile_id) ? preg_replace('![^\w\|]+!', '_', $template->compile_id) : null;
|
||||
$_files = explode('|', $template->resource_name);
|
||||
$_filepath = (string)abs(crc32($template->getTemplateFilepath()));
|
||||
$_filepath = sha1($template->getTemplateFilepath());
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($template->smarty->use_sub_dirs) {
|
||||
$_filepath = substr($_filepath, 0, 2) . DS
|
||||
|
@@ -94,8 +94,7 @@ class Smarty_Internal_Resource_File {
|
||||
public function getCompiledFilepath($_template)
|
||||
{
|
||||
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!','_',$_template->compile_id) : null;
|
||||
// $_filepath = md5($_template->resource_name);
|
||||
$_filepath = (string)abs(crc32($_template->getTemplateFilepath()));
|
||||
$_filepath = sha1($_template->getTemplateFilepath());
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($_template->smarty->use_sub_dirs) {
|
||||
$_filepath = substr($_filepath, 0, 2) . DS
|
||||
|
@@ -105,7 +105,7 @@ class Smarty_Internal_Resource_Registered {
|
||||
public function getCompiledFilepath($_template)
|
||||
{
|
||||
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!','_',$_template->compile_id) : null;
|
||||
$_filepath = (string)abs(crc32($_template->template_resource));
|
||||
$_filepath = sha1($_template->template_resource);
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($_template->smarty->use_sub_dirs) {
|
||||
$_filepath = substr($_filepath, 0, 2) . DS
|
||||
|
@@ -44,7 +44,8 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
public $suppressHeader = false;
|
||||
public $suppressFileDependency = false;
|
||||
public $extract_code = false;
|
||||
public $extracted_compiled_code = '';
|
||||
public $extracted_compiled_code = '';
|
||||
public $has_nocache_code = false;
|
||||
// Rendered content
|
||||
public $rendered_content = null;
|
||||
// Cache file
|
||||
@@ -61,7 +62,9 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
// storage for plugin
|
||||
public $plugin_data = array();
|
||||
// special properties
|
||||
public $properties = null;
|
||||
public $properties = array ('file_dependency' => array(),
|
||||
'nocache_hash' => '',
|
||||
'function' => array());
|
||||
// storage for block data
|
||||
public $block_data = array();
|
||||
// required plugins
|
||||
@@ -80,7 +83,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
*/
|
||||
public function __construct($template_resource, $smarty, $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
|
||||
{
|
||||
$this->smarty = $smarty;
|
||||
$this->smarty = &$smarty;
|
||||
// Smarty parameter
|
||||
$this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
|
||||
$this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
|
||||
@@ -90,10 +93,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
$this->cache_lifetime = $_cache_lifetime === null ?$this->smarty->cache_lifetime : $_cache_lifetime;
|
||||
$this->force_cache = $this->smarty->force_cache;
|
||||
$this->security = $this->smarty->security;
|
||||
$this->parent = $_parent;
|
||||
$this->properties['file_dependency'] = array();
|
||||
$this->properties['nocache_hash'] = '';
|
||||
$this->properties['function'] = array();;
|
||||
$this->parent = $_parent;
|
||||
// dummy local smarty variable
|
||||
$this->tpl_vars['smarty'] = new Smarty_Variable;
|
||||
// Template resource
|
||||
@@ -244,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']['F' . abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
|
||||
$this->properties['file_dependency'][sha1($this->getTemplateFilepath())] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
|
||||
}
|
||||
if ($this->smarty->debugging) {
|
||||
Smarty_Internal_Debug::start_compile($this);
|
||||
@@ -330,8 +330,8 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
// don't write cache file
|
||||
return false;
|
||||
}
|
||||
// build file dependency string
|
||||
$this->properties['cache_lifetime'] = $this->cache_lifetime;
|
||||
$this->properties['cache_lifetime'] = $this->cache_lifetime;
|
||||
$this->properties['has_nocache_code'] = false;
|
||||
// get text between non-cached items
|
||||
$cache_split = preg_split("!/\*%%SmartyNocache:{$this->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$this->properties['nocache_hash']}%%\*/!s", $this->rendered_content);
|
||||
// get non-cached items
|
||||
@@ -342,6 +342,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
// escape PHP tags in template content
|
||||
$output .= preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php echo \'$1\'; ?>', $curr_split);
|
||||
if (isset($cache_parts[0][$curr_idx])) {
|
||||
$this->properties['has_nocache_code'] = true;
|
||||
// remove nocache tags from cache output
|
||||
$output .= preg_replace("!/\*/?%%SmartyNocache:{$this->properties['nocache_hash']}%%\*/!", '', $cache_parts[0][$curr_idx]);
|
||||
}
|
||||
@@ -359,10 +360,10 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
public function isCached ()
|
||||
{
|
||||
if ($this->isCached === null) {
|
||||
$this->isCached = false;
|
||||
if (($this->caching == 1 || $this->caching == 2) && !$this->resource_object->isEvaluated && !$this->force_compile && !$this->force_cache) {
|
||||
$cachedTimestamp = $this->getCachedTimestamp();
|
||||
if ($cachedTimestamp === false) {
|
||||
$this->isCached = false;
|
||||
return $this->isCached;
|
||||
}
|
||||
if ($this->caching === SMARTY_CACHING_LIFETIME_SAVED || ($this->caching == SMARTY_CACHING_LIFETIME_CURRENT && (time() <= ($cachedTimestamp + $this->cache_lifetime) || $this->cache_lifetime < 0))) {
|
||||
@@ -373,6 +374,9 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
if ($this->smarty->debugging) {
|
||||
Smarty_Internal_Debug::end_cache($this);
|
||||
}
|
||||
if ($this->isCached === false) {
|
||||
return $this->isCached;
|
||||
}
|
||||
if ($this->cacheFileChecked) {
|
||||
$this->isCached = true;
|
||||
return $this->isCached;
|
||||
@@ -380,6 +384,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
$this->cacheFileChecked = true;
|
||||
if ($this->caching === SMARTY_CACHING_LIFETIME_SAVED && $this->properties['cache_lifetime'] > 0 && (time() > ($this->getCachedTimestamp() + $this->properties['cache_lifetime']))) {
|
||||
$this->rendered_content = null;
|
||||
$this->isCached = false;
|
||||
return $this->isCached;
|
||||
}
|
||||
if (!empty($this->properties['file_dependency']) && $this->smarty->compile_check) {
|
||||
@@ -394,6 +399,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
// If ($mtime > $this->getCachedTimestamp()) {
|
||||
If ($mtime > $_file_to_check[1]) {
|
||||
$this->rendered_content = null;
|
||||
$this->isCached = false;
|
||||
return $this->isCached;
|
||||
}
|
||||
}
|
||||
@@ -402,6 +408,9 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($this->isCached === null) {
|
||||
$this->isCached = false;
|
||||
}
|
||||
return $this->isCached;
|
||||
}
|
||||
|
||||
@@ -468,10 +477,9 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
}
|
||||
$this->rendered_content = ob_get_clean();
|
||||
if (!$this->resource_object->isEvaluated) {
|
||||
$this->properties['file_dependency']['F' . abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
|
||||
$this->properties['file_dependency'][sha1($this->getTemplateFilepath())] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
|
||||
}
|
||||
if ($this->parent instanceof Smarty_Template or $this->parent instanceof Smarty_Internal_Template) {
|
||||
// var_dump('merge ', $this->parent->getTemplateFilepath(), $this->parent->properties['file_dependency'], $this->getTemplateFilepath(), $this->properties['file_dependency']);
|
||||
$this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']);
|
||||
$this->parent->required_plugins = array_merge_recursive($this->parent->required_plugins, $this->required_plugins);
|
||||
}
|
||||
@@ -494,9 +502,9 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
Smarty_Internal_Debug::end_cache($this);
|
||||
}
|
||||
} else {
|
||||
if (!empty($this->properties['nocache_hash']) && !empty($this->parent->properties['nocache_hash'])) {
|
||||
// var_dump('renderTemplate',$this->has_nocache_code,$this->template_resource, $this->has_nocache_code, $this->properties['nocache_hash'], $this->parent->properties['nocache_hash'], $this->rendered_content);
|
||||
if ($this->has_nocache_code && !empty($this->properties['nocache_hash']) && !empty($this->parent->properties['nocache_hash'])) {
|
||||
// replace nocache_hash
|
||||
// var_dump($this->properties['nocache_hash'],$this->parent->properties['nocache_hash'],$this->rendered_content);
|
||||
$this->rendered_content = preg_replace("/{$this->properties['nocache_hash']}/", $this->parent->properties['nocache_hash'], $this->rendered_content);
|
||||
}
|
||||
}
|
||||
@@ -721,13 +729,8 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
*/
|
||||
public function createPropertyHeader ($cache = false)
|
||||
{
|
||||
$this->properties['fullpath'] = realpath($this->getTemplateFilepath());
|
||||
$properties_string = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/" ;
|
||||
if ($this->smarty->direct_access_security) {
|
||||
$properties_string .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
|
||||
}
|
||||
$properties_string .= "\$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . "); /*/%%SmartyHeaderCode%%*/?>\n";
|
||||
$plugins_string = '';
|
||||
$plugins_string = '';
|
||||
// include code for plugins
|
||||
if (!$cache) {
|
||||
if (!empty($this->required_plugins['compiled'])) {
|
||||
$plugins_string = '<?php ';
|
||||
@@ -738,6 +741,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
$plugins_string .= '?>';
|
||||
}
|
||||
if (!empty($this->required_plugins['cache'])) {
|
||||
$this->has_nocache_code = true;
|
||||
$plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php ";
|
||||
foreach($this->required_plugins['cache'] as $plugin_name => $data) {
|
||||
$plugin = 'smarty_' . $data['type'] . '_' . $plugin_name;
|
||||
@@ -746,6 +750,27 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
$plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
|
||||
}
|
||||
}
|
||||
// build property code
|
||||
$this->properties['fullpath'] = realpath($this->resource_object->getTemplateFilepath($this));
|
||||
$this->properties['has_nocache_code'] = $this->has_nocache_code;
|
||||
$properties_string = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/" ;
|
||||
if ($this->smarty->direct_access_security) {
|
||||
$properties_string .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
|
||||
}
|
||||
if ($cache) {
|
||||
// remove compiled code of{function} definition
|
||||
unset($this->properties['function']);
|
||||
if (!empty($this->smarty->template_functions)) {
|
||||
// copy code of {function} tags called in nocache mode
|
||||
foreach ($this->smarty->template_functions as $name => $function_data) {
|
||||
if (isset($function_data['called_nocache'])) {
|
||||
unset($function_data['called_nocache'], $this->smarty->template_functions[$name]['called_nocache']);
|
||||
$this->properties['function'][$name] = $function_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$properties_string .= "\$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . "); /*/%%SmartyHeaderCode%%*/?>\n";
|
||||
return $properties_string . $plugins_string;
|
||||
}
|
||||
|
||||
@@ -754,9 +779,11 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
*/
|
||||
public function decodeProperties ($properties)
|
||||
{
|
||||
// if ($properties['fullpath'] != realpath($this->getTemplateFilepath())) {
|
||||
// throw new Exception('CRC32 collision \'' . $properties['fullpath'] . '\'');
|
||||
// } ;
|
||||
if ($properties['fullpath'] != realpath($this->resource_object->getTemplateFilepath($this))) {
|
||||
$this->isCached = false;
|
||||
$this->mustCompile = true;
|
||||
} ;
|
||||
$this->has_nocache_code = $properties['has_nocache_code'];
|
||||
$this->properties['nocache_hash'] = $properties['nocache_hash'];
|
||||
if (isset($properties['cache_lifetime'])) {
|
||||
$this->properties['cache_lifetime'] = $properties['cache_lifetime'];
|
||||
@@ -765,10 +792,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
|
||||
$this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $properties['file_dependency']);
|
||||
}
|
||||
if (!empty($properties['function'])) {
|
||||
foreach ($properties['function'] as $_name => $_data) {
|
||||
$this->smarty->template_functions[$_name]['compiled'] = $_data['compiled'];
|
||||
$this->smarty->template_functions[$_name]['parameter'] = $_data['parameter'];
|
||||
}
|
||||
$this->smarty->template_functions = array_merge($this->smarty->template_functions, $properties['function']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,396 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty Internal Plugin Smarty Template Compiler Base
|
||||
*
|
||||
* This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
/**
|
||||
* Main compiler class
|
||||
*/
|
||||
class Smarty_Internal_TemplateCompilerBase {
|
||||
// compile tag objects
|
||||
static $_tag_objects = array();
|
||||
// tag stack
|
||||
public $_tag_stack = array();
|
||||
// current template
|
||||
public $template = null;
|
||||
// required plugins
|
||||
public $required_plugins_call = array();
|
||||
// hash for nocache sections
|
||||
private $nocache_hash = null;
|
||||
|
||||
/**
|
||||
* Initialize compiler
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->nocache_hash = str_replace('.','_',uniqid(rand(),true));
|
||||
}
|
||||
// abstract function doCompile($_content);
|
||||
/**
|
||||
* Methode to compile a Smarty template
|
||||
*
|
||||
* @param $template template object to compile
|
||||
* @return bool true if compiling succeeded, false if it failed
|
||||
*/
|
||||
public function compileTemplate($template)
|
||||
{
|
||||
$template->properties['nocache_hash'] = $this->nocache_hash;
|
||||
/* here is where the compiling takes place. Smarty
|
||||
tags in the templates are replaces with PHP code,
|
||||
then written to compiled files. */
|
||||
// flag for nochache sections
|
||||
$this->nocache = false;
|
||||
$this->tag_nocache = false;
|
||||
// assume successfull compiling
|
||||
$this->compile_error = false;
|
||||
// save template object in compiler class
|
||||
$this->template = $template;
|
||||
$this->smarty->_current_file = $this->template->getTemplateFilepath();
|
||||
// template header code
|
||||
$template_header = '';
|
||||
if (!$template->suppressHeader) {
|
||||
$template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
|
||||
$template_header .= " compiled from \"" . $this->template->getTemplateFilepath() . "\" */ ?>\n";
|
||||
}
|
||||
|
||||
do {
|
||||
// flag for aborting current and start recompile
|
||||
$this->abort_and_recompile = false;
|
||||
// get template source
|
||||
$_content = $template->getTemplateSource();
|
||||
// run prefilter if required
|
||||
if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
|
||||
$_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $this->smarty);
|
||||
}
|
||||
// on empty template just return header
|
||||
if ($_content == '') {
|
||||
if ($template->suppressFileDependency) {
|
||||
$template->compiled_template = '';
|
||||
} else {
|
||||
$template->compiled_template = $template_header . $template->createPropertyHeader();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// call compiler
|
||||
$_compiled_code = $this->doCompile($_content);
|
||||
} while ($this->abort_and_recompile);
|
||||
|
||||
if (!$this->compile_error) {
|
||||
// return compiled code to template object
|
||||
if ($template->suppressFileDependency) {
|
||||
$template->compiled_template = $_compiled_code;
|
||||
} else {
|
||||
$template->compiled_template = $template_header . $template->createPropertyHeader() . $_compiled_code;
|
||||
}
|
||||
// run postfilter if required
|
||||
if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
|
||||
$template->compiled_template = Smarty_Internal_Filter_Handler::runFilter('post', $template->compiled_template, $this->smarty);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
// compilation error
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile Tag
|
||||
*
|
||||
* This is a call back from the lexer/parser
|
||||
* It executes the required compile plugin for the Smarty tag
|
||||
*
|
||||
* @param string $tag tag name
|
||||
* @param array $args array with tag attributes
|
||||
* @return string compiled code
|
||||
*/
|
||||
public function compileTag($tag, $args)
|
||||
{
|
||||
// $args contains the attributes parsed and compiled by the lexer/parser
|
||||
// assume that tag does compile into code, but creates no HTML output
|
||||
$this->has_code = true;
|
||||
$this->has_output = false;
|
||||
// compile the smarty tag (required compile classes to compile the tag are autoloaded)
|
||||
if (($_output = $this->callTagCompiler($tag, $args)) === false) {
|
||||
if (isset($this->smarty->template_functions[$tag])) {
|
||||
// template defined by {template} tag
|
||||
$args['name'] = $tag;
|
||||
$_output = $this->callTagCompiler('private_function_call', $args);
|
||||
}
|
||||
}
|
||||
if ($_output !== false) {
|
||||
if ($_output !== true) {
|
||||
// did we get compiled code
|
||||
if ($this->has_code) {
|
||||
// Does it create output?
|
||||
if ($this->has_output) {
|
||||
$_output .= "\n";
|
||||
}
|
||||
// return compiled code
|
||||
return $_output;
|
||||
}
|
||||
}
|
||||
// tag did not produce compiled code
|
||||
return '';
|
||||
} else {
|
||||
// not an internal compiler tag
|
||||
if (strlen($tag) < 6 || substr_compare($tag, 'close', -5, 5) != 0) {
|
||||
// check if tag is a registered object
|
||||
if (isset($this->smarty->registered_objects[$tag]) && isset($args['object_methode'])) {
|
||||
$methode = $args['object_methode'];
|
||||
unset ($args['object_methode']);
|
||||
if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
|
||||
(empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
|
||||
return $this->callTagCompiler('private_object_function', $args, $tag, $methode);
|
||||
} elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
|
||||
return $this->callTagCompiler('private_object_block_function', $args, $tag, $methode);
|
||||
} else {
|
||||
return $this->trigger_template_error ('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
|
||||
}
|
||||
}
|
||||
// check if tag is registered
|
||||
foreach (array('compiler', 'function', 'block') as $type) {
|
||||
if (isset($this->smarty->registered_plugins[$type][$tag])) {
|
||||
// if compiler function plugin call it now
|
||||
if ($type == 'compiler') {
|
||||
if (!$this->smarty->registered_plugins[$type][$tag][1]) {
|
||||
$this->tag_nocache = true;
|
||||
}
|
||||
return call_user_func_array($this->smarty->registered_plugins[$type][$tag][0], array($args, $this));
|
||||
}
|
||||
// compile registered function or block function
|
||||
if ($type == 'function' || $type == 'block') {
|
||||
return $this->callTagCompiler('private_registered_' . $type, $args, $tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
// check plugins from plugins folder
|
||||
foreach ($this->smarty->plugin_search_order as $plugin_type) {
|
||||
if ($plugin_type == 'compiler' && $this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
|
||||
$plugin = 'smarty_compiler_' . $tag;
|
||||
if (class_exists($plugin, false)) {
|
||||
$plugin = array(new $plugin, 'compile');
|
||||
}
|
||||
if (is_callable($plugin)) {
|
||||
return call_user_func_array($plugin, array($args, $this));
|
||||
} else {
|
||||
throw new Exception("Plugin \"{$tag}\" not callable");
|
||||
}
|
||||
} else {
|
||||
if ($function = $this->getPlugin($tag, $plugin_type)) {
|
||||
return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $tag, $function);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// compile closing tag of block function
|
||||
$base_tag = substr($tag, 0, -5);
|
||||
// check if closing tag is a registered object
|
||||
if (isset($this->smarty->registered_objects[$base_tag]) && isset($args['object_methode'])) {
|
||||
$methode = $args['object_methode'];
|
||||
unset ($args['object_methode']);
|
||||
if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
|
||||
return $this->callTagCompiler('private_object_block_function', $args, $tag, $methode);
|
||||
} else {
|
||||
return $this->trigger_template_error ('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
|
||||
}
|
||||
}
|
||||
// registered block tag ?
|
||||
if (isset($this->smarty->registered_plugins['block'][$base_tag])) {
|
||||
return $this->callTagCompiler('private_registered_block', $args, $tag);
|
||||
}
|
||||
// block plugin?
|
||||
if ($function = $this->getPlugin($base_tag, 'block')) {
|
||||
return $this->callTagCompiler('private_block_plugin', $args, $tag, $function);
|
||||
}
|
||||
if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
|
||||
$plugin = 'smarty_compiler_' . $tag;
|
||||
if (class_exists($plugin, false)) {
|
||||
$plugin = array(new $plugin, 'compile');
|
||||
}
|
||||
if (is_callable($plugin)) {
|
||||
return call_user_func_array($plugin, array($args, $this));
|
||||
} else {
|
||||
throw new Exception("Plugin \"{$tag}\" not callable");
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* lazy loads internal compile plugin for tag and calls the compile methode
|
||||
*
|
||||
* compile objects cached for reuse.
|
||||
* class name format: Smarty_Internal_Compile_TagName
|
||||
* plugin filename format: Smarty_Internal_Tagname.php
|
||||
*
|
||||
* @param $tag string tag name
|
||||
* @param $args array with tag attributes
|
||||
* @param $param1 optional parameter
|
||||
* @param $param2 optional parameter
|
||||
* @param $param3 optional parameter
|
||||
* @return string compiled code
|
||||
*/
|
||||
public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
|
||||
{
|
||||
// re-use object if already exists
|
||||
if (isset(self::$_tag_objects[$tag])) {
|
||||
// compile this tag
|
||||
return call_user_func(array(self::$_tag_objects[$tag], 'compile'), $args, $this, $param1, $param2, $param3);
|
||||
}
|
||||
// lazy load internal compiler plugin
|
||||
$class_name = 'Smarty_Internal_Compile_' . $tag;
|
||||
if ($this->smarty->loadPlugin($class_name)) {
|
||||
// use plugin if found
|
||||
self::$_tag_objects[$tag] = new $class_name;
|
||||
// compile this tag
|
||||
return call_user_func(array(self::$_tag_objects[$tag], 'compile'), $args, $this, $param1, $param2, $param3);
|
||||
}
|
||||
// no internal compile plugin for this tag
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for plugins and return function name
|
||||
*
|
||||
* @param $pugin_name string name of plugin or function
|
||||
* @param $type string type of plugin
|
||||
* @return string call name of function
|
||||
*/
|
||||
public function getPlugin($plugin_name, $type)
|
||||
{
|
||||
if (isset($this->template->required_plugins_call[$plugin_name][$type])) {
|
||||
if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
|
||||
if (isset($this->template->required_plugins['compiled'][$plugin_name])) {
|
||||
$this->template->required_plugins['cache'][$plugin_name] = $this->template->required_plugins['compiled'][$plugin_name];
|
||||
}
|
||||
} else {
|
||||
if (isset($this->template->required_plugins['cache'][$plugin_name])) {
|
||||
$this->template->required_plugins['compiled'][$plugin_name] = $this->template->required_plugins['compiled'][$plugin_name];
|
||||
}
|
||||
}
|
||||
if ($type == 'modifier') {
|
||||
$this->template->saved_modifer[$plugin_name] = true;
|
||||
}
|
||||
return $this->template->required_plugins_call[$plugin_name][$type];
|
||||
}
|
||||
// loop through plugin dirs and find the plugin
|
||||
$plugin = 'smarty_' . $type . '_' . $plugin_name;
|
||||
$found = false;
|
||||
foreach((array)$this->smarty->plugins_dir as $_plugin_dir) {
|
||||
$file = rtrim($_plugin_dir, '/\\') . DS . $type . '.' . $plugin_name . '.php';
|
||||
if (file_exists($file)) {
|
||||
require_once($file);
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($found) {
|
||||
if (is_callable($plugin)) {
|
||||
$this->template->required_plugins_call[$plugin_name][$type] = $plugin;
|
||||
if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
|
||||
$this->template->required_plugins['cache'][$plugin_name]['file'] = $file;
|
||||
$this->template->required_plugins['cache'][$plugin_name]['type'] = $type;
|
||||
} else {
|
||||
$this->template->required_plugins['compiled'][$plugin_name]['file'] = $file;
|
||||
$this->template->required_plugins['compiled'][$plugin_name]['type'] = $type;
|
||||
}
|
||||
if ($type == 'modifier') {
|
||||
$this->template->saved_modifer[$plugin_name] = true;
|
||||
}
|
||||
|
||||
return $plugin;
|
||||
} else {
|
||||
throw new Exception("Plugin {$type} \"{$plugin_name}\" not callable");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Inject inline code for nocache template sections
|
||||
*
|
||||
* This method gets the content of each template element from the parser.
|
||||
* If the content is compiled code and it should be not cached the code is injected
|
||||
* into the rendered output.
|
||||
*
|
||||
* @param string $content content of template element
|
||||
* @param boolean $tag_nocache true if the parser detected a nocache situation
|
||||
* @param boolean $is_code true if content is compiled code
|
||||
* @return string content
|
||||
*/
|
||||
public function processNocacheCode ($content, $is_code)
|
||||
{
|
||||
// If the template is not evaluated and we have a nocache section and or a nocache tag
|
||||
if ($is_code) {
|
||||
// generate replacement code
|
||||
if ((!$this->template->resource_object->isEvaluated || $this->template->forceNocache) && $this->template->caching &&
|
||||
($this->nocache || $this->tag_nocache)) {
|
||||
$this->tag_nocache = false;
|
||||
$_output = str_replace("'", "\'", $content);
|
||||
$_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n";
|
||||
// make sure we include modifer plugins for nocache code
|
||||
if (isset($this->template->saved_modifer)) {
|
||||
foreach ($this->template->saved_modifer as $plugin_name => $dummy) {
|
||||
if (isset($this->template->required_plugins['compiled'][$plugin_name])) {
|
||||
$this->template->required_plugins['cache'][$plugin_name] = $this->template->required_plugins['compiled'][$plugin_name];
|
||||
}
|
||||
}
|
||||
unset($this->template->saved_modifer);
|
||||
}
|
||||
} else {
|
||||
$_output = $content;
|
||||
}
|
||||
} else {
|
||||
$_output = $content;
|
||||
}
|
||||
return $_output;
|
||||
}
|
||||
/**
|
||||
* display compiler error messages without dying
|
||||
*
|
||||
* If parameter $args is empty it is a parser detected syntax error.
|
||||
* In this case the parser is called to obtain information about expected tokens.
|
||||
*
|
||||
* If parameter $args contains a string this is used as error message
|
||||
*
|
||||
* @param $args string individual error message or null
|
||||
*/
|
||||
public function trigger_template_error($args = null, $line = null)
|
||||
{
|
||||
// get template source line which has error
|
||||
if (!isset($line)) {
|
||||
$line = $this->lex->line;
|
||||
}
|
||||
$match = preg_split("/\n/", $this->lex->data);
|
||||
$error_text = 'Syntax Error in template "' . $this->template->getTemplateFilepath() . '" on line ' . $line . ' "' . $match[$line-1] . '" ';
|
||||
if (isset($args)) {
|
||||
// individual error message
|
||||
$error_text .= $args;
|
||||
} else {
|
||||
// expected token from parser
|
||||
foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
|
||||
$exp_token = $this->parser->yyTokenName[$token];
|
||||
if (isset($this->lex->smarty_token_names[$exp_token])) {
|
||||
// token type from lexer
|
||||
$expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
|
||||
} else {
|
||||
// otherwise internal token name
|
||||
$expect[] = $this->parser->yyTokenName[$token];
|
||||
}
|
||||
}
|
||||
// output parser error message
|
||||
$error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
|
||||
}
|
||||
throw new Exception($error_text);
|
||||
// set error flag
|
||||
$this->compile_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user