mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 18:34:27 +02:00
update Smarty::_realpath()
This commit is contained in:
@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
/**
|
||||
* smarty version
|
||||
*/
|
||||
const SMARTY_VERSION = '3.1.28-dev/24';
|
||||
const SMARTY_VERSION = '3.1.28-dev/25';
|
||||
|
||||
/**
|
||||
* define variable scopes
|
||||
@@ -989,7 +989,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
}
|
||||
if ($this->_flags[$type] == false) {
|
||||
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;
|
||||
}
|
||||
@@ -1085,7 +1085,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
$plugins_dir = (array) $this->plugins_dir;
|
||||
$this->plugins_dir = array();
|
||||
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);
|
||||
}
|
||||
@@ -1104,7 +1104,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
*/
|
||||
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])) {
|
||||
Smarty::$_muted_directories[$this->compile_dir] = null;
|
||||
}
|
||||
@@ -1120,7 +1120,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
public function getCompileDir()
|
||||
{
|
||||
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])) {
|
||||
Smarty::$_muted_directories[$this->compile_dir] = null;
|
||||
}
|
||||
@@ -1138,7 +1138,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
*/
|
||||
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])) {
|
||||
Smarty::$_muted_directories[$this->cache_dir] = null;
|
||||
}
|
||||
@@ -1154,7 +1154,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
public function getCacheDir()
|
||||
{
|
||||
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])) {
|
||||
Smarty::$_muted_directories[$this->cache_dir] = null;
|
||||
}
|
||||
@@ -1175,7 +1175,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
$rp = $this->_flags[$dirName];
|
||||
if (is_array($dir)) {
|
||||
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)) {
|
||||
// indexes are not merged but appended
|
||||
$this->{$dirName}[] = $path;
|
||||
@@ -1185,7 +1185,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$path = $rp ? $this->_realpath($dir . DS, $this->use_include_path) : $dir;
|
||||
$path = $rp ? $this->_realpath($dir . DS, true) : $dir;
|
||||
if ($key !== null) {
|
||||
// override directory at specified index
|
||||
$this->{$dirName}[$key] = $path;
|
||||
@@ -1382,23 +1382,26 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
/**
|
||||
* Normalize path
|
||||
* - remove /./ and /../
|
||||
* - make it absolute
|
||||
* - make it absolute if required
|
||||
*
|
||||
* @param string $path file path
|
||||
* @param bool $relative leave $path relative
|
||||
* @param bool $realpath leave $path relative
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function _realpath($path, $relative = false)
|
||||
public function _realpath($path, $realpath = null)
|
||||
{
|
||||
static $pattern = null;
|
||||
static $pattern2 = null;
|
||||
if (!$relative && $path[0] !== '/' && $path[1] !== ':') {
|
||||
if ($realpath !== null && $path[0] !== '/' && $path[1] !== ':') {
|
||||
$path = getcwd() . DS . $path;
|
||||
}
|
||||
while (preg_match(isset($pattern) ? $pattern : $pattern = '#([\\\/][.]+[\\\/])|[' . (DS == '/' ? '\\\\' : '/') . ']|[\\\/]{2,}#', $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;
|
||||
}
|
||||
|
||||
|
@@ -62,9 +62,10 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
|
||||
*/
|
||||
$_smarty_tpl = $compiler->template;
|
||||
$_filepath = false;
|
||||
$_file = null;
|
||||
eval('$_file = ' . $_attr['file'] . ';');
|
||||
if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
|
||||
$_filepath = $compiler->smarty->_realpath($_file);
|
||||
$_filepath = $compiler->smarty->_realpath($_file, true);
|
||||
} else {
|
||||
if (isset($compiler->smarty->security_policy)) {
|
||||
$_dir = $compiler->smarty->security_policy->trusted_dir;
|
||||
@@ -73,7 +74,7 @@ class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
|
||||
}
|
||||
if (!empty($_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)) {
|
||||
$_filepath = $_path;
|
||||
break;
|
||||
|
@@ -64,7 +64,7 @@ class Smarty_Internal_Extension_LoadPlugin
|
||||
}
|
||||
}
|
||||
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 ($smarty->_is_file_cache[$file] !== false) {
|
||||
return $smarty->_is_file_cache[$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");
|
||||
}
|
||||
$sources[$s->uid] = $s;
|
||||
$uid .= $source->smarty->_realpath($s->filepath);
|
||||
$uid .= $source->smarty->_realpath($s->filepath, true);
|
||||
if ($_template) {
|
||||
$exists = $exists && $s->exists;
|
||||
}
|
||||
|
@@ -31,12 +31,16 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
||||
$file = $source->name;
|
||||
// absolute file ?
|
||||
if ($file[0] == '/' || $file[1] == ':') {
|
||||
$file = $source->smarty->_realpath($file);
|
||||
$file = $source->smarty->_realpath($file, true);
|
||||
return is_file($file) ? $file : false;
|
||||
}
|
||||
// go relative to a given 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 ($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
|
||||
) {
|
||||
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;
|
||||
@@ -81,7 +85,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 $source->smarty->_realpath($path, true);
|
||||
}
|
||||
}
|
||||
// Could be relative to cwd
|
||||
|
Reference in New Issue
Block a user