Skip to content

Commit

Permalink
- bugfix preg_replace could fail on large content resulting in a blan…
Browse files Browse the repository at this point in the history
…k page smarty-php#417
  • Loading branch information
Uwe Tews committed Mar 23, 2018
1 parent d79af55 commit d646d46
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 55 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
===== 3.1.32 - dev ===
23.03.2018
- bugfix preg_replace could fail on large content resulting in a blank page https://github.com/smarty-php/smarty/issues/417

21.03.2018
- bugfix {$smarty.section...} used outside {section}{/section} showed incorrect values if {section}{/section} was called inside
another loop https://github.com/smarty-php/smarty/issues/422
Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.32-dev-42';
const SMARTY_VERSION = '3.1.32-dev-43';
/**
* define variable scopes
*/
Expand Down
25 changes: 24 additions & 1 deletion libs/sysplugins/smarty_internal_runtime_codeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ public function create(Smarty_Internal_Template $_template, $content = '', $func
$output .= $functions;
$output .= "<?php }\n";
// remove unneeded PHP tags
return preg_replace(array('/\s*\?>[\n]?<\?php\s*/', '/\?>\s*$/'), array("\n", ''), $output);
if (preg_match('/\s*\?>[\n]?<\?php\s*/', $output)) {
$curr_split = preg_split('/\s*\?>[\n]?<\?php\s*/',
$output);
var_dump($curr_split);
preg_match_all('/\s*\?>[\n]?<\?php\s*/',
$output,
$curr_parts);
$output = '';
foreach ($curr_split as $idx => $curr_output) {
$output .= $curr_output;
if (isset($curr_parts[ 0 ][ $idx ])) {
$output .= "\n";
}
}
}
if (preg_match('/\?>\s*$/', $output)) {
$curr_split = preg_split('/\?>\s*$/',
$output);
$output = '';
foreach ($curr_split as $idx => $curr_output) {
$output .= $curr_output;
}
}
return $output;
}
}
114 changes: 61 additions & 53 deletions libs/sysplugins/smarty_internal_runtime_updatecache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,6 @@ public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Intern
{
}

/**
* Sanitize content and write it to cache resource
*
* @param \Smarty_Template_Cached $cached
* @param Smarty_Internal_Template $_template
* @param bool $no_output_filter
*
* @throws \SmartyException
*/
public function removeNoCacheHash(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template,
$no_output_filter)
{
$content = ob_get_clean();
unset($cached->hashes[ $_template->compiled->nocache_hash ]);
if (!empty($cached->hashes)) {
$hash_array = array();
foreach ($cached->hashes as $hash => $foo) {
$hash_array[] = "/{$hash}/";
}
$content = preg_replace($hash_array, $_template->compiled->nocache_hash, $content);
}
$_template->cached->has_nocache_code = false;
// get text between non-cached items
$cache_split =
preg_split("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
$content);
// get non-cached items
preg_match_all("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
$content, $cache_parts);
$content = '';
// loop over items, stitch back together
foreach ($cache_split as $curr_idx => $curr_split) {
// escape PHP tags in template content
$content .= preg_replace('/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/',
"<?php echo '\$1'; ?>\n", $curr_split);
if (isset($cache_parts[ 0 ][ $curr_idx ])) {
$_template->cached->has_nocache_code = true;
$content .= $cache_parts[ 1 ][ $curr_idx ];
}
}
if (!$no_output_filter && !$_template->cached->has_nocache_code &&
(isset($_template->smarty->autoload_filters[ 'output' ]) ||
isset($_template->smarty->registered_filters[ 'output' ]))
) {
$content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
}
// write cache file content
$this->writeCachedContent($cached, $_template, $content);
}

/**
* Cache was invalid , so render from compiled and write to cache
*
Expand Down Expand Up @@ -106,6 +56,67 @@ public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Temp
}
}

/**
* Sanitize content and write it to cache resource
*
* @param \Smarty_Template_Cached $cached
* @param Smarty_Internal_Template $_template
* @param bool $no_output_filter
*
* @throws \SmartyException
*/
public function removeNoCacheHash(Smarty_Template_Cached $cached,
Smarty_Internal_Template $_template,
$no_output_filter)
{
$php_pattern = '/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/';
$content = ob_get_clean();
$hash_array = $cached->hashes;
$hash_array = array_keys($hash_array);
$nocache_hash = '(' . implode('|', $hash_array) . ')';
$_template->cached->has_nocache_code = false;
// get text between non-cached items
$cache_split =
preg_split("!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
$content);
// get non-cached items
preg_match_all("!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
$content,
$cache_parts);
$content = '';
// loop over items, stitch back together
foreach ($cache_split as $curr_idx => $curr_split) {
if (preg_match($php_pattern, $curr_split)) {
// escape PHP tags in template content
$php_split = preg_split($php_pattern,
$curr_split);
preg_match_all($php_pattern,
$curr_split,
$php_parts);
foreach ($php_split as $idx_php => $curr_php) {
$content .= $curr_php;
if (isset($php_parts[ 0 ][ $idx_php ])) {
$content .= "<?php echo '{$php_parts[ 1 ][ $idx_php ]}'; ?>\n";
}
}
} else {
$content .= $curr_split;
}
if (isset($cache_parts[ 0 ][ $curr_idx ])) {
$_template->cached->has_nocache_code = true;
$content .= $cache_parts[ 2 ][ $curr_idx ];
}
}
if (!$no_output_filter && !$_template->cached->has_nocache_code &&
(isset($_template->smarty->autoload_filters[ 'output' ]) ||
isset($_template->smarty->registered_filters[ 'output' ]))
) {
$content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
}
// write cache file content
$this->writeCachedContent($cached, $_template, $content);
}

/**
* Writes the content to cache resource
*
Expand Down Expand Up @@ -148,7 +159,6 @@ public function write(Smarty_Template_Cached $cached, Smarty_Internal_Template $
if ($_template->smarty->cache_locking) {
$cached->handler->releaseLock($_template->smarty, $cached);
}

return true;
}
$cached->content = null;
Expand All @@ -157,8 +167,6 @@ public function write(Smarty_Template_Cached $cached, Smarty_Internal_Template $
$cached->valid = false;
$cached->processed = false;
}

return false;
}

}

0 comments on commit d646d46

Please sign in to comment.