update Smarty::_realpath()

This commit is contained in:
Uwe Tews
2015-07-07 17:56:50 +02:00
parent 7fa6c4fd47
commit 1e4f28da54
5 changed files with 29 additions and 21 deletions

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.28-dev/24'; const SMARTY_VERSION = '3.1.28-dev/25';
/** /**
* define variable scopes * define variable scopes
@@ -989,7 +989,7 @@ class Smarty extends Smarty_Internal_TemplateBase
} }
if ($this->_flags[$type] == false) { if ($this->_flags[$type] == false) {
foreach ($this->{$type} as $k => $v) { foreach ($this->{$type} as $k => $v) {
$this->{$type}[$k] = $this->_realpath($v . DS, $this->use_include_path); $this->{$type}[$k] = $this->_realpath($v . DS, true);
} }
$this->_flags[$type] = true; $this->_flags[$type] = true;
} }
@@ -1085,7 +1085,7 @@ class Smarty extends Smarty_Internal_TemplateBase
$plugins_dir = (array) $this->plugins_dir; $plugins_dir = (array) $this->plugins_dir;
$this->plugins_dir = array(); $this->plugins_dir = array();
foreach ($plugins_dir as $v) { foreach ($plugins_dir as $v) {
$this->plugins_dir[] = $this->_realpath($v . DS, $this->use_include_path); $this->plugins_dir[] = $this->_realpath($v . DS, true);
} }
$this->plugins_dir = array_unique($this->plugins_dir); $this->plugins_dir = array_unique($this->plugins_dir);
} }
@@ -1104,7 +1104,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public function setCompileDir($compile_dir) public function setCompileDir($compile_dir)
{ {
$this->compile_dir = $this->_realpath($compile_dir . DS); $this->compile_dir = $this->_realpath($compile_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->compile_dir])) { if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
Smarty::$_muted_directories[$this->compile_dir] = null; Smarty::$_muted_directories[$this->compile_dir] = null;
} }
@@ -1120,7 +1120,7 @@ class Smarty extends Smarty_Internal_TemplateBase
public function getCompileDir() public function getCompileDir()
{ {
if (!isset($this->_flags['compile_dir'])) { if (!isset($this->_flags['compile_dir'])) {
$this->compile_dir = $this->_realpath($this->compile_dir . DS); $this->compile_dir = $this->_realpath($this->compile_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->compile_dir])) { if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
Smarty::$_muted_directories[$this->compile_dir] = null; Smarty::$_muted_directories[$this->compile_dir] = null;
} }
@@ -1138,7 +1138,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public function setCacheDir($cache_dir) public function setCacheDir($cache_dir)
{ {
$this->cache_dir = $this->_realpath($cache_dir . DS); $this->cache_dir = $this->_realpath($cache_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->cache_dir])) { if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
Smarty::$_muted_directories[$this->cache_dir] = null; Smarty::$_muted_directories[$this->cache_dir] = null;
} }
@@ -1154,7 +1154,7 @@ class Smarty extends Smarty_Internal_TemplateBase
public function getCacheDir() public function getCacheDir()
{ {
if (!isset($this->_flags['cache_dir'])) { if (!isset($this->_flags['cache_dir'])) {
$this->cache_dir = $this->_realpath($this->cache_dir . DS); $this->cache_dir = $this->_realpath($this->cache_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->cache_dir])) { if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
Smarty::$_muted_directories[$this->cache_dir] = null; Smarty::$_muted_directories[$this->cache_dir] = null;
} }
@@ -1175,7 +1175,7 @@ class Smarty extends Smarty_Internal_TemplateBase
$rp = $this->_flags[$dirName]; $rp = $this->_flags[$dirName];
if (is_array($dir)) { if (is_array($dir)) {
foreach ($dir as $k => $v) { foreach ($dir as $k => $v) {
$path = $rp ? $this->_realpath($v . DS, $this->use_include_path) : $v; $path = $rp ? $this->_realpath($v . DS, true) : $v;
if (is_int($k)) { if (is_int($k)) {
// indexes are not merged but appended // indexes are not merged but appended
$this->{$dirName}[] = $path; $this->{$dirName}[] = $path;
@@ -1185,7 +1185,7 @@ class Smarty extends Smarty_Internal_TemplateBase
} }
} }
} else { } else {
$path = $rp ? $this->_realpath($dir . DS, $this->use_include_path) : $dir; $path = $rp ? $this->_realpath($dir . DS, true) : $dir;
if ($key !== null) { if ($key !== null) {
// override directory at specified index // override directory at specified index
$this->{$dirName}[$key] = $path; $this->{$dirName}[$key] = $path;
@@ -1382,23 +1382,26 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* Normalize path * Normalize path
* - remove /./ and /../ * - remove /./ and /../
* - make it absolute * - make it absolute if required
* *
* @param string $path file path * @param string $path file path
* @param bool $relative leave $path relative * @param bool $realpath leave $path relative
* *
* @return string * @return string
*/ */
public function _realpath($path, $relative = false) public function _realpath($path, $realpath = null)
{ {
static $pattern = null; static $pattern = null;
static $pattern2 = null; static $pattern2 = null;
if (!$relative && $path[0] !== '/' && $path[1] !== ':') { if ($realpath !== null && $path[0] !== '/' && $path[1] !== ':') {
$path = getcwd() . DS . $path; $path = getcwd() . DS . $path;
} }
while (preg_match(isset($pattern) ? $pattern : $pattern = '#([\\\/][.]+[\\\/])|[' . (DS == '/' ? '\\\\' : '/') . ']|[\\\/]{2,}#', $path)) { while (preg_match(isset($pattern) ? $pattern : $pattern = '#([\\\/][.]+[\\\/])|[' . (DS == '/' ? '\\\\' : '/') . ']|[\\\/]{2,}#', $path)) {
$path = preg_replace(isset($pattern2) ? $pattern2 : $pattern2 = '#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[\\\/]{2,}|[' . (DS == '/' ? '\\\\' : '/') . ']+#', DS, $path); $path = preg_replace(isset($pattern2) ? $pattern2 : $pattern2 = '#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[\\\/]{2,}|[' . (DS == '/' ? '\\\\' : '/') . ']+#', DS, $path);
} }
if ($realpath === false && ($path[0] == '/' || $path[1] == ':')) {
$path = str_ireplace(getcwd(), '.', $path);
}
return $path; return $path;
} }

View File

@@ -62,9 +62,10 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
*/ */
$_smarty_tpl = $compiler->template; $_smarty_tpl = $compiler->template;
$_filepath = false; $_filepath = false;
$_file = null;
eval('$_file = ' . $_attr['file'] . ';'); eval('$_file = ' . $_attr['file'] . ';');
if (!isset($compiler->smarty->security_policy) && file_exists($_file)) { if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
$_filepath = $compiler->smarty->_realpath($_file); $_filepath = $compiler->smarty->_realpath($_file, true);
} else { } else {
if (isset($compiler->smarty->security_policy)) { if (isset($compiler->smarty->security_policy)) {
$_dir = $compiler->smarty->security_policy->trusted_dir; $_dir = $compiler->smarty->security_policy->trusted_dir;
@@ -73,7 +74,7 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
} }
if (!empty($_dir)) { if (!empty($_dir)) {
foreach ((array) $_dir as $_script_dir) { foreach ((array) $_dir as $_script_dir) {
$_path = $compiler->smarty->_realpath($_script_dir . DS . $_file); $_path = $compiler->smarty->_realpath($_script_dir . DS . $_file, true);
if (file_exists($_path)) { if (file_exists($_path)) {
$_filepath = $_path; $_filepath = $_path;
break; break;

View File

@@ -64,7 +64,7 @@ class Smarty_Internal_Extension_LoadPlugin
} }
} }
foreach ($names as $path) { foreach ($names as $path) {
$file = $smarty->use_include_path ? $smarty->_realpath($path) : $path; $file = $smarty->use_include_path ? $smarty->_realpath($path, false) : $path;
if (isset($smarty->_is_file_cache[$file])) { if (isset($smarty->_is_file_cache[$file])) {
if ($smarty->_is_file_cache[$file] !== false) { if ($smarty->_is_file_cache[$file] !== false) {
return $smarty->_is_file_cache[$file]; return $smarty->_is_file_cache[$file];

View File

@@ -44,7 +44,7 @@ class Smarty_Internal_Resource_Extends extends Smarty_Resource
throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type"); throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
} }
$sources[$s->uid] = $s; $sources[$s->uid] = $s;
$uid .= $source->smarty->_realpath($s->filepath); $uid .= $source->smarty->_realpath($s->filepath, true);
if ($_template) { if ($_template) {
$exists = $exists && $s->exists; $exists = $exists && $s->exists;
} }

View File

@@ -31,12 +31,16 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
$file = $source->name; $file = $source->name;
// absolute file ? // absolute file ?
if ($file[0] == '/' || $file[1] == ':') { if ($file[0] == '/' || $file[1] == ':') {
$file = $source->smarty->_realpath($file); $file = $source->smarty->_realpath($file, true);
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 ($file[0] == '.' && $_template && $_template->parent instanceof Smarty_Internal_Template && preg_match('#^[.]{1,2}[\\\/]#', $file)) { if ($file[0] == '.' && $_template && $_template->parent instanceof Smarty_Internal_Template &&
if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { preg_match('#^[.]{1,2}[\\\/]#', $file)
) {
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;
@@ -81,7 +85,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
foreach ($_directories as $_directory) { foreach ($_directories as $_directory) {
$path = $_directory . $file; $path = $_directory . $file;
if (is_file($path)) { if (is_file($path)) {
return $source->smarty->_realpath($path); return $source->smarty->_realpath($path, true);
} }
} }
// Could be relative to cwd // Could be relative to cwd