mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 02:44:27 +02:00
- fix $smarty->use_include_path option and optimize file path code
This commit is contained in:
@@ -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->{$type}[$k] = $this->_realpath($v . DS, $this->use_include_path);
|
||||||
}
|
}
|
||||||
$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->plugins_dir[] = $this->_realpath($v . DS, $this->use_include_path);
|
||||||
}
|
}
|
||||||
$this->plugins_dir = array_unique($this->plugins_dir);
|
$this->plugins_dir = array_unique($this->plugins_dir);
|
||||||
}
|
}
|
||||||
@@ -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) : $v;
|
$path = $rp ? $this->_realpath($v . DS, $this->use_include_path) : $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) : $dir;
|
$path = $rp ? $this->_realpath($dir . DS, $this->use_include_path) : $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;
|
||||||
@@ -1385,17 +1385,18 @@ class Smarty extends Smarty_Internal_TemplateBase
|
|||||||
* - make it absolute
|
* - make it absolute
|
||||||
*
|
*
|
||||||
* @param string $path file path
|
* @param string $path file path
|
||||||
|
* @param bool $relative leave $path relative
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function _realpath($path)
|
public function _realpath($path, $relative = false)
|
||||||
{
|
{
|
||||||
static $pattern = null;
|
static $pattern = null;
|
||||||
static $pattern2 = null;
|
static $pattern2 = null;
|
||||||
if ($path[0] !== '/' && $path[1] !== ':') {
|
if (!$relative && $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);
|
||||||
}
|
}
|
||||||
return $path;
|
return $path;
|
||||||
|
@@ -29,42 +29,61 @@ class Smarty_Internal_Extension_LoadPlugin
|
|||||||
if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
|
if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Plugin name is expected to be: Smarty_[Type]_[Name]
|
if (!preg_match('#^smarty_((internal)|([^_]+))_(.+)$#i', $plugin_name, $match)) {
|
||||||
$_name_parts = explode('_', $plugin_name, 3);
|
|
||||||
// class name must have three parts to be valid plugin
|
|
||||||
// count($_name_parts) < 3 === !isset($_name_parts[2])
|
|
||||||
if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') {
|
|
||||||
throw new SmartyException("plugin {$plugin_name} is not a valid name format");
|
throw new SmartyException("plugin {$plugin_name} is not a valid name format");
|
||||||
}
|
}
|
||||||
// if type is "internal", get plugin from sysplugins
|
if (!empty($match[2])) {
|
||||||
if (strtolower($_name_parts[1]) == 'internal') {
|
|
||||||
$file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
|
$file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
|
||||||
if (isset($smarty->_is_file_cache[$file]) ? $smarty->_is_file_cache[$file] : $smarty->_is_file_cache[$file] = is_file($file)) {
|
if (isset($smarty->_is_file_cache[$file])) {
|
||||||
require_once($file);
|
if ($smarty->_is_file_cache[$file] !== false) {
|
||||||
return $file;
|
return $smarty->_is_file_cache[$file];
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (is_file($file)) {
|
||||||
|
$smarty->_is_file_cache[$file] = $file;
|
||||||
|
require_once($file);
|
||||||
|
return $file;
|
||||||
|
} else {
|
||||||
|
$smarty->_is_file_cache[$file] = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// plugin filename is expected to be: [type].[name].php
|
// plugin filename is expected to be: [type].[name].php
|
||||||
$_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
|
$_plugin_filename = "{$match[1]}.{$match[4]}.php";
|
||||||
|
$_lower_filename = strtolower($_plugin_filename);
|
||||||
|
$_different = $_lower_filename != $_plugin_filename;
|
||||||
// loop through plugin dirs and find the plugin
|
// loop through plugin dirs and find the plugin
|
||||||
|
$names = array();
|
||||||
foreach ($smarty->getPluginsDir() as $_plugin_dir) {
|
foreach ($smarty->getPluginsDir() as $_plugin_dir) {
|
||||||
$names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename),);
|
$names[] = $_plugin_dir . $_plugin_filename;
|
||||||
foreach ($names as $file) {
|
if ($_different) {
|
||||||
if (isset($smarty->_is_file_cache[$file]) ? $smarty->_is_file_cache[$file] : $smarty->_is_file_cache[$file] = is_file($file)) {
|
$names[] = $_plugin_dir . $_lower_filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($names as $path) {
|
||||||
|
$file = $smarty->use_include_path ? $smarty->_realpath($path) : $path;
|
||||||
|
if (isset($smarty->_is_file_cache[$file])) {
|
||||||
|
if ($smarty->_is_file_cache[$file] !== false) {
|
||||||
|
return $smarty->_is_file_cache[$file];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (is_file($file)) {
|
||||||
|
$smarty->_is_file_cache[$file] = $file;
|
||||||
require_once($file);
|
require_once($file);
|
||||||
return $file;
|
return $file;
|
||||||
}
|
}
|
||||||
if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
|
$smarty->_is_file_cache[$file] = $file;
|
||||||
|
}
|
||||||
|
if ($smarty->use_include_path) {
|
||||||
// try PHP include_path
|
// try PHP include_path
|
||||||
$file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
|
$path = Smarty_Internal_Get_Include_Path::getIncludePath($names, null, $smarty);
|
||||||
if ($file !== false) {
|
if ($path !== false) {
|
||||||
require_once($file);
|
$smarty->_is_file_cache[$path] = $path;
|
||||||
return $file;
|
require_once($path);
|
||||||
}
|
return $path;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// no plugin loaded
|
// no plugin loaded
|
||||||
|
@@ -18,29 +18,42 @@ class Smarty_Internal_Get_Include_Path
|
|||||||
/**
|
/**
|
||||||
* Return full file path from PHP include_path
|
* Return full file path from PHP include_path
|
||||||
*
|
*
|
||||||
* @param string $filepath filepath
|
* @param string[] $dirs
|
||||||
|
* @param string $file
|
||||||
|
* @param \Smarty $smarty
|
||||||
|
*
|
||||||
|
* @return bool|string full filepath or false
|
||||||
*
|
*
|
||||||
* @return string|boolean full filepath or false
|
|
||||||
*/
|
*/
|
||||||
public static function getIncludePath($filepath)
|
public static function getIncludePath($dirs, $file, Smarty $smarty)
|
||||||
{
|
{
|
||||||
static $_include_path = null;
|
static $_include_path = null;
|
||||||
|
static $_has_stream_include = null;
|
||||||
if (function_exists('stream_resolve_include_path')) {
|
|
||||||
// available since PHP 5.3.2
|
|
||||||
return stream_resolve_include_path($filepath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($_include_path === null) {
|
if ($_include_path === null) {
|
||||||
$_include_path = explode(PATH_SEPARATOR, get_include_path());
|
$_include_path = (array) explode(PATH_SEPARATOR, get_include_path());
|
||||||
|
foreach ($_include_path as $key => $_path) {
|
||||||
|
$_include_path[$key] = rtrim($_path, '/\\');
|
||||||
|
}
|
||||||
|
$_has_stream_include = function_exists('stream_resolve_include_path');
|
||||||
|
}
|
||||||
|
// try PHP include_path
|
||||||
|
foreach ($dirs as $dir) {
|
||||||
|
if ($dir[0] != '/' && $dir[1] != ':') {
|
||||||
|
$_d_path = $dir . isset($file) ? $file : '';
|
||||||
|
if ($_has_stream_include) {
|
||||||
|
// available since PHP 5.3.2
|
||||||
|
$path = stream_resolve_include_path($_d_path);
|
||||||
|
if ($path !== false && is_file($path)) {
|
||||||
|
return $smarty->_realpath($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($_include_path as $_i_path) {
|
||||||
|
if (is_file($_i_path . DS . $_d_path)) {
|
||||||
|
return $smarty->_realpath($_i_path . DS . $_d_path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($_include_path as $_path) {
|
|
||||||
if (file_exists($_path . DS . $filepath)) {
|
|
||||||
return $_path . DS . $filepath;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -46,11 +46,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
return is_file($path) ? $path : false;
|
return is_file($path) ? $path : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($source->isConfig) {
|
$_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
|
||||||
$_directories = $source->smarty->getConfigDir();
|
|
||||||
} else {
|
|
||||||
$_directories = $source->smarty->getTemplateDir();
|
|
||||||
}
|
|
||||||
// template_dir index?
|
// template_dir index?
|
||||||
if ($file[0] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
|
if ($file[0] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
|
||||||
$index = $fileMatch[1];
|
$index = $fileMatch[1];
|
||||||
@@ -83,25 +79,21 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
|
|||||||
|
|
||||||
// relative file name?
|
// relative file name?
|
||||||
foreach ($_directories as $_directory) {
|
foreach ($_directories as $_directory) {
|
||||||
$_filepath = $_directory . $file;
|
$path = $_directory . $file;
|
||||||
$path = $source->smarty->_realpath($_filepath);
|
|
||||||
if (is_file($path)) {
|
if (is_file($path)) {
|
||||||
return $path;
|
return $source->smarty->_realpath($path);
|
||||||
}
|
|
||||||
if ($source->smarty->use_include_path && !preg_match('/^([\\\/]|[a-zA-Z]:[\\\/])/', $_directory)) {
|
|
||||||
// try PHP include_path
|
|
||||||
$_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath);
|
|
||||||
if ($_filepath !== false) {
|
|
||||||
$path = $source->smarty->_realpath($_filepath);
|
|
||||||
if (is_file($path)) {
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Could be relative to cwd
|
// Could be relative to cwd
|
||||||
$path = $source->smarty->_realpath($file);
|
$path = $source->smarty->_realpath($file);
|
||||||
return is_file($path) ? $path : false;
|
if (is_file($path)) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
// Use include path ?
|
||||||
|
if ($source->smarty->use_include_path) {
|
||||||
|
return Smarty_Internal_Get_Include_Path::getIncludePath($_directories, $file, $source->smarty);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -490,6 +490,9 @@ class Smarty_Security
|
|||||||
unset($this->_resource_dir[$directory]);
|
unset($this->_resource_dir[$directory]);
|
||||||
}
|
}
|
||||||
foreach ($_dir as $directory) {
|
foreach ($_dir as $directory) {
|
||||||
|
if ($this->smarty->use_include_path) {
|
||||||
|
$directory = $this->smarty->_realpath($directory);
|
||||||
|
}
|
||||||
$this->_resource_dir[$directory] = true;
|
$this->_resource_dir[$directory] = true;
|
||||||
}
|
}
|
||||||
$this->_template_dir = $_dir;
|
$this->_template_dir = $_dir;
|
||||||
@@ -502,6 +505,9 @@ class Smarty_Security
|
|||||||
unset($this->_resource_dir[$directory]);
|
unset($this->_resource_dir[$directory]);
|
||||||
}
|
}
|
||||||
foreach ($_dir as $directory) {
|
foreach ($_dir as $directory) {
|
||||||
|
if ($this->smarty->use_include_path) {
|
||||||
|
$directory = $this->smarty->_realpath($directory);
|
||||||
|
}
|
||||||
$this->_resource_dir[$directory] = true;
|
$this->_resource_dir[$directory] = true;
|
||||||
}
|
}
|
||||||
$this->_config_dir = $_dir;
|
$this->_config_dir = $_dir;
|
||||||
|
Reference in New Issue
Block a user