diff --git a/change_log.txt b/change_log.txt index f8460831..87406797 100644 --- a/change_log.txt +++ b/change_log.txt @@ -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 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 3574dcf0..037cbabf 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -1146,17 +1146,22 @@ class Smarty extends Smarty_Internal_TemplateBase 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); diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index d41b0fc8..0b7055a7 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -49,6 +49,10 @@ class Smarty_Internal_Resource_File extends Smarty_Resource // 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? @@ -87,7 +91,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource 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)) {