Skip to content

Commit

Permalink
- optimize filepath normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Oct 18, 2015
1 parent 757d66a commit ca9ccfc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 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.28-dev===== (xx.xx.2015)
18.10.2015
- optimize filepath normalization

18.09.2015
- bugfix {if $foo instanceof $bar} failed to compile if 2nd value is a variable https://github.com/smarty-php/smarty/issues/92

Expand Down
17 changes: 11 additions & 6 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,17 +1146,22 @@ public function _getTemplateId($template_name, $cache_id = null, $compile_id = n
public function _realpath($path, $realpath = null)
{
static $pattern = null;
static $pattern2 = null;
static $nds = null;
if ($pattern == null) {
$pattern = '#[' . (DS == '/' ? '\\\\' : '/') . ']|([\\\/]([\\\/]|([.]+[\\\/])))#';
$pattern2 = '#([\\\/]+[^\\\/]+[\\\/]+[.]([\\\/]+[.])*[.][\\\/]+([.][\\\/]+)*)|([\\\/]+([.][\\\/]+)+)|[\\\/]{2,}|[' .
(DS == '/' ? '\\\\' : '/') . ']+#';
$nds = DS == '/' ? '\\' : '/';
$ds = '\\' . DS;
$pattern = "#([{$ds}]+[^{$ds}]+[{$ds}]+[.]([{$ds}]+[.])*[.][{$ds}]+([.][{$ds}]+)*)|([{$ds}]+([.][{$ds}]+)+)|[{$ds}]{2,}#";
}
// normalize DS
if (strpos($path, $nds) !== false) {
$path = str_replace($nds, DS, $path);
}

if ($realpath === true && $path[0] !== '/' && $path[1] !== ':') {
$path = getcwd() . DS . $path;
}
while (preg_match($pattern, $path)) {
$path = preg_replace($pattern2, DS, $path);
while ((strpos($path, '.' . DS) !== false) || (strpos($path, DS . DS) !== false)) {
$path = preg_replace($pattern, DS, $path);
}
if ($realpath === false && ($path[0] == '/' || $path[1] == ':')) {
$path = str_ireplace(getcwd(), '.', $path);
Expand Down
6 changes: 5 additions & 1 deletion libs/sysplugins/smarty_internal_resource_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal
// files relative to a template only get one shot
return is_file($path) ? $path : false;
}
// normalize DS
if (strpos($file, DS == '/' ? '\\' : '/') !== false) {
$file = str_replace(DS == '/' ? '\\' : '/', DS, $file);
}

$_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
// template_dir index?
Expand Down Expand Up @@ -87,7 +91,7 @@ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal
foreach ($_directories as $_directory) {
$path = $_directory . $file;
if (is_file($path)) {
return $source->smarty->_realpath($path);
return (strpos($path, '.' . DS) !== false) ? $source->smarty->_realpath($path) : $path;
}
}
if (!isset($_index_dirs)) {
Expand Down

0 comments on commit ca9ccfc

Please sign in to comment.