- optimization of template/config file normalization

This commit is contained in:
Uwe Tews
2015-06-21 12:42:53 +02:00
parent 2d0f457abd
commit 4e471b19e2
3 changed files with 20 additions and 7 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.28-dev===== (xx.xx.2015)
21.06.2015
- optimization of template/config file normalization
19.06.2015
- improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.28-dev/1';
const SMARTY_VERSION = '3.1.28-dev/2';
/**
* define variable scopes

View File

@@ -17,6 +17,13 @@
*/
class Smarty_Internal_Resource_File extends Smarty_Resource
{
/**
* Inverse DS
*
* @var string
*/
private $invDS = '';
/**
* build template filepath by traversing the template_dir array
*
@@ -29,7 +36,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$file = $source->name;
preg_match('#^(?P<absolute>[\\\/]|[a-zA-Z]:[\\\/])|(\[(?P<index>[^\]]+)\])|(?P<rel>\.[\\\/])#', $file, $fileMatch);
$this->invDS = (DS == '/') ? '\\' : '/';
preg_match('#(^(?P<absolute>[\\\/]|[a-zA-Z]:[\\\/])|(\[(?P<index>[^\]]+)\])|(?P<rel>\.[\\\/]))|((?P<rel2>\.[\\\/])|(?P<ds>[' . $this->invDS . ']))#', $file, $fileMatch);
// save basename
if (!empty($fileMatch['absolute'])) {
$file = $this->normalizePath($file);
@@ -83,10 +91,11 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
}
}
$normalize = !empty($fileMatch['rel']) || !empty($fileMatch['rel2']) || !empty($fileMatch['ds']);
// relative file name?
foreach ($_directories as $_directory) {
$_filepath = $_directory . $file;
$path = $this->normalizePath($_filepath);
$path = $normalize ? $this->normalizePath($_filepath) : $_filepath;
if (is_file($path)) {
return $path;
}
@@ -124,9 +133,10 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
if ($path[0] == '.') {
$path = getcwd() . DS . $path;
}
$path = preg_replace('#[\\\/]+([.][\\\/]+)*#', DS, $path);
while (strrpos($path, '.' . DS) !== false) {
$path = preg_replace('#([\\\/]([^\\\/]+[\\\/]){2}([.][.][\\\/]){2})|([\\\/][^\\\/]+[\\\/][.][.][\\\/])#', DS, $path);
$first = true;
while (strrpos($path, '.' . DS) !== false || ($first && strrpos($path, $this->invDS) !== false)) {
$path = preg_replace('#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[' . $this->invDS . ']+#', DS, $path);
$first = false;
}
return $path;
}
@@ -196,7 +206,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
if ($source->timestamp) {
return file_get_contents($source->filepath);
}
if ($source instanceof Smarty_Config_Source) {
if ($source instanceof Smarty_Template_Config) {
throw new SmartyException("Unable to read config {$source->type} '{$source->name}'");
}
throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");