- optimize filepath normalization

This commit is contained in:
uwetews
2015-10-18 04:40:45 +02:00
parent 757d66a731
commit ca9ccfc919
3 changed files with 19 additions and 7 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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)) {