mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 02:44:27 +02:00
- optimize file path normalization
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
- move getStreamVariable() into extension
|
- move getStreamVariable() into extension
|
||||||
- move $smarty->append() and $smarty->appendByRef() into extension
|
- move $smarty->append() and $smarty->appendByRef() into extension
|
||||||
- optimize autoloader
|
- optimize autoloader
|
||||||
|
- optimize file path normalization
|
||||||
|
|
||||||
27.06.2015
|
27.06.2015
|
||||||
- bugfix resolve naming conflict between custom Smarty delimiter '<%' and PHP ASP tags https://github.com/smarty-php/smarty/issues/64
|
- bugfix resolve naming conflict between custom Smarty delimiter '<%' and PHP ASP tags https://github.com/smarty-php/smarty/issues/64
|
||||||
|
@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
|||||||
/**
|
/**
|
||||||
* smarty version
|
* smarty version
|
||||||
*/
|
*/
|
||||||
const SMARTY_VERSION = '3.1.28-dev/17';
|
const SMARTY_VERSION = '3.1.28-dev/18';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* define variable scopes
|
* define variable scopes
|
||||||
@@ -1390,14 +1390,13 @@ class Smarty extends Smarty_Internal_TemplateBase
|
|||||||
*/
|
*/
|
||||||
public function _realpath($path)
|
public function _realpath($path)
|
||||||
{
|
{
|
||||||
|
static $pattern = null;
|
||||||
|
static $pattern2 = null;
|
||||||
if ($path[0] !== '/' && $path[1] !== ':') {
|
if ($path[0] !== '/' && $path[1] !== ':') {
|
||||||
$path = getcwd() . DS . $path;
|
$path = getcwd() . DS . $path;
|
||||||
}
|
}
|
||||||
//return realpath($path) . DS;
|
while (preg_match(isset($pattern) ? $pattern : $pattern = '#([.][\\\/])|[' . (DS == '/' ? '\\\\' : '/') . ']|[\\\/]{2,}#', $path)) {
|
||||||
$first = true;
|
$path = preg_replace(isset($pattern2) ? $pattern2 : $pattern2 = '#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[\\\/]{2,}|[' . (DS == '/' ? '\\\\' : '/') . ']+#', DS, $path);
|
||||||
while ($first || strrpos($path, '.' . DS) !== false) {
|
|
||||||
$path = preg_replace('#([\\\/]([^\\\/]+[\\\/]){2}([.][.][\\\/]){2})|([\\\/][^\\\/]+[\\\/][.][.][\\\/])|([\\\/]+([.][\\\/]+)*)#', DS, $path);
|
|
||||||
$first = false;
|
|
||||||
}
|
}
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
@@ -17,13 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
class Smarty_Internal_Resource_File extends Smarty_Resource
|
class Smarty_Internal_Resource_File extends Smarty_Resource
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Inverse DS
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $invDS = '';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* build template filepath by traversing the template_dir array
|
* build template filepath by traversing the template_dir array
|
||||||
*
|
*
|
||||||
@@ -36,21 +29,19 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
|
protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
|
||||||
{
|
{
|
||||||
$file = $source->name;
|
$file = $source->name;
|
||||||
$this->invDS = (DS == '/') ? '\\\\' : '/';
|
// absolute file ?
|
||||||
preg_match('#(^(?P<absolute>[\\\/]|[a-zA-Z]:[\\\/])|(\[(?P<index>[^\]]+)\])|(?P<rel>\.[\\\/]))|((?P<rel2>\.[\\\/])|(?P<ds>[' . $this->invDS . ']))#', $file, $fileMatch);
|
if ($file[0] == '/' || $file[1] == ':' ) {
|
||||||
// save basename
|
$file = $source->smarty->_realpath($file);
|
||||||
if (!empty($fileMatch['absolute'])) {
|
|
||||||
$file = $this->normalizePath($file);
|
|
||||||
return is_file($file) ? $file : false;
|
return is_file($file) ? $file : false;
|
||||||
}
|
}
|
||||||
// go relative to a given template?
|
// go relative to a given template?
|
||||||
if (!empty($fileMatch['rel']) && $_template && $_template->parent instanceof Smarty_Internal_Template) {
|
if ($file[0] == '.' && $_template && $_template->parent instanceof Smarty_Internal_Template && preg_match('#^[.]{1,2}[\\\/]#', $file)) {
|
||||||
if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) {
|
if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) {
|
||||||
throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
|
throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
|
||||||
}
|
}
|
||||||
$path = dirname($_template->parent->source->filepath) . DS . $file;
|
$path = dirname($_template->parent->source->filepath) . DS . $file;
|
||||||
// normalize path
|
// normalize path
|
||||||
$path = $this->normalizePath($path);
|
$path = $source->smarty->_realpath($path);
|
||||||
// files relative to a template only get one shot
|
// files relative to a template only get one shot
|
||||||
return is_file($path) ? $path : false;
|
return is_file($path) ? $path : false;
|
||||||
}
|
}
|
||||||
@@ -61,8 +52,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
$_directories = $source->smarty->getTemplateDir();
|
$_directories = $source->smarty->getTemplateDir();
|
||||||
}
|
}
|
||||||
// template_dir index?
|
// template_dir index?
|
||||||
if (!empty($fileMatch['index'])) {
|
if ($file[0] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
|
||||||
$index = $fileMatch['index'];
|
$index = $fileMatch[1];
|
||||||
$_directory = null;
|
$_directory = null;
|
||||||
// try string indexes
|
// try string indexes
|
||||||
if (isset($_directories[$index])) {
|
if (isset($_directories[$index])) {
|
||||||
@@ -79,9 +70,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($_directory) {
|
if ($_directory) {
|
||||||
preg_match('#\](.+)$#', $file, $fileMatch);
|
$path = $_directory . $fileMatch[2];
|
||||||
$path = $_directory . $fileMatch[1];
|
$path = $source->smarty->_realpath($path);
|
||||||
$path = $this->normalizePath($path);
|
|
||||||
if (is_file($path)) {
|
if (is_file($path)) {
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
@@ -91,11 +81,10 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$normalize = !empty($fileMatch['rel']) || !empty($fileMatch['rel2']) || !empty($fileMatch['ds']);
|
|
||||||
// relative file name?
|
// relative file name?
|
||||||
foreach ($_directories as $_directory) {
|
foreach ($_directories as $_directory) {
|
||||||
$_filepath = $_directory . $file;
|
$_filepath = $_directory . $file;
|
||||||
$path = $normalize ? $this->normalizePath($_filepath) : $_filepath;
|
$path = $source->smarty->_realpath($_filepath);
|
||||||
if (is_file($path)) {
|
if (is_file($path)) {
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
@@ -107,7 +96,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
$_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath);
|
$_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath);
|
||||||
}
|
}
|
||||||
if ($_filepath !== false) {
|
if ($_filepath !== false) {
|
||||||
$path = $this->normalizePath($_filepath);
|
$path = $source->smarty->_realpath($_filepath);
|
||||||
if (is_file($path)) {
|
if (is_file($path)) {
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
@@ -115,28 +104,10 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Could be relative to cwd
|
// Could be relative to cwd
|
||||||
$path = $this->normalizePath(getcwd() . DS . $file);
|
$path = $source->smarty->_realpath($file);
|
||||||
return is_file($path) ? $path : false;
|
return is_file($path) ? $path : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize path
|
|
||||||
* - remove /./ and /../
|
|
||||||
* - make it absolute
|
|
||||||
*
|
|
||||||
* @param string $path file path
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function normalizePath($path)
|
|
||||||
{
|
|
||||||
$first = true;
|
|
||||||
while (strrpos($path, '.' . DS) !== false || ($first && strrpos($path, $this->invDS[0]) !== false)) {
|
|
||||||
$path = preg_replace('#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[' . $this->invDS . ']+#', DS, $path);
|
|
||||||
$first = false;
|
|
||||||
}
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test is file exists and save timestamp
|
* test is file exists and save timestamp
|
||||||
|
Reference in New Issue
Block a user