Removed support for PHP include path, and removed the ::loadPlugin method.

This commit is contained in:
Simon Wisselink
2023-01-05 21:58:29 +01:00
parent e89a3dda49
commit 576bd4cc01
45 changed files with 12 additions and 634 deletions

View File

@@ -41,7 +41,6 @@ them directly, or use the corresponding setter/getter methods.
- [$smarty_debug_id](./api-variables/variable-smarty-debug-id.md)
- [$template_dir](./api-variables/variable-template-dir.md)
- [$trusted_dir](./api-variables/variable-trusted-dir.md)
- [$use_include_path](./api-variables/variable-use-include-path.md)
- [$use_sub_dirs](./api-variables/variable-use-sub-dirs.md)
> **Note**

View File

@@ -15,22 +15,12 @@ found.
> document root.
> **Note**
>
> If the directories known to `$template_dir` are relative to
> directories known to the
> [include\_path](&url.php-manual;ini.core.php#ini.include-path) you
> need to activate the [`$use_include_path`](#variable.use.include.path)
> option.
> **Note**
>
> As of Smarty 3.1 the attribute \$template\_dir is no longer accessible
> directly. Use [`getTemplateDir()`](#api.get.template.dir),
> [`setTemplateDir()`](#api.set.template.dir) and
> [`addTemplateDir()`](#api.add.template.dir) instead.
See also [`Template Resources`](#resources),
[`$use_include_path`](#variable.use.include.path),
[`getTemplateDir()`](#api.get.template.dir),
[`setTemplateDir()`](#api.set.template.dir) and
[`addTemplateDir()`](#api.add.template.dir).

View File

@@ -1,49 +0,0 @@
\$use\_include\_path {#variable.use.include.path}
====================
This tells smarty to respect the
[include\_path](&url.php-manual;ini.core.php#ini.include-path) within
the [`File Template Resource`](#resources.file) handler and the plugin
loader to resolve the directories known to
[`$template_dir`](#variable.template.dir). The flag also makes the
plugin loader check the include\_path for
[`$plugins_dir`](#variable.plugins.dir).
> **Note**
>
> You should not design your applications to rely on the include\_path,
> as this may - depending on your implementation - slow down your system
> (and Smarty) considerably.
If use\_include\_path is enabled, file discovery for
[`$template_dir`](#variable.template.dir) and
[`$plugins_dir`](#variable.plugins.dir) work as follows.
- For each element `$directory` in array (\$template\_dir or
\$plugins\_dir) do
- Test if requested file is in `$directory` relative to the [current
working directory](&url.php-manual;function.getcwd.php). If file
found, return it.
- For each `$path` in include\_path do
- Test if requested file is in `$directory` relative to the `$path`
(possibly relative to the [current working
directory](&url.php-manual;function.getcwd.php)). If file found,
return it.
- Try default\_handler or fail.
This means that whenever a directory/file relative to the current
working directory is encountered, it is preferred over anything
potentially accessible through the include\_path.
> **Note**
>
> Smarty does not filter elements of the include\_path. That means a
> \".:\" within your include path will trigger the current working
> directory lookup twice.
See also [`Template Resources`](#resources) and
[`$template_dir`](#variable.template.dir)

View File

@@ -11,12 +11,6 @@ If the file resource cannot find the requested template, the
[`$default_template_handler_func`](#variable.default.template.handler.func)
is invoked.
> **Note**
>
> As of Smarty 3.1 the file resource no longer walks through the
> [include\_path](&url.php-manual;ini.core.php#ini.include-path) unless
> [`$use_include_path` is activated](#variable.use.include.path)
Templates from \$template\_dir {#templates.from.template.dir}
------------------------------

View File

@@ -170,10 +170,6 @@ class FilePlugin extends BasePlugin {
return $path;
}
}
// Use include path ?
if ($source->smarty->use_include_path) {
return $source->smarty->getIncludePath($_directories, $file);
}
return false;
}
}

View File

@@ -230,13 +230,6 @@ class Security {
*/
protected $_trusted_dir = null;
/**
* Cache for include path status
*
* @var bool
*/
protected $_include_path_status = false;
/**
* Cache for $_include_array lookup
*
@@ -481,15 +474,6 @@ class Security {
* @throws \Smarty\Exception if directory is not trusted
*/
public function isTrustedResourceDir($filepath, $isConfig = null) {
if ($this->_include_path_status !== $this->smarty->use_include_path) {
$_dir =
$this->smarty->use_include_path ? $this->smarty->getIncludePathDirs() : [];
if ($this->_include_dir !== $_dir) {
$this->_updateResourceDir($this->_include_dir, $_dir);
$this->_include_dir = $_dir;
}
$this->_include_path_status = $this->smarty->use_include_path;
}
$_dir = $this->smarty->getTemplateDir();
if ($this->_template_dir !== $_dir) {
$this->_updateResourceDir($this->_template_dir, $_dir);

View File

@@ -136,13 +136,6 @@ class Smarty extends \Smarty\TemplateBase
*/
public $error_unassigned = false;
/**
* look up relative file path in include_path
*
* @var boolean
*/
public $use_include_path = false;
/**
* flag if template_dir is normalized
*
@@ -1819,162 +1812,6 @@ class Smarty extends \Smarty\TemplateBase
return $content;
}
/**
* include path cache
*
* @var string
*/
private $_include_path = '';
/**
* include path directory cache
*
* @var array
*/
private $_include_dirs = array();
/**
* include path directory cache
*
* @var array
*/
private $_user_dirs = array();
/**
* stream cache
*
* @var string[][]
*/
private $isFile = array();
/**
* stream cache
*
* @var string[]
*/
private $isPath = array();
/**
* stream cache
*
* @var int[]
*/
private $number = array();
/**
* status cache
*
* @var bool
*/
private $_has_stream_include = null;
/**
* Number for array index
*
* @var int
*/
private $counter = 0;
/**
* Check if include path was updated
*
* @return bool
*/
private function isNewIncludePath()
{
$_i_path = get_include_path();
if ($this->_include_path !== $_i_path) {
$this->_include_dirs = array();
$this->_include_path = $_i_path;
$_dirs = (array)explode(PATH_SEPARATOR, $_i_path);
foreach ($_dirs as $_path) {
if (is_dir($_path)) {
$this->_include_dirs[] = $this->_realpath($_path . DIRECTORY_SEPARATOR, true);
}
}
return true;
}
return false;
}
/**
* return array with include path directories
*
* @return array
*/
public function getIncludePathDirs()
{
$this->isNewIncludePath();
return $this->_include_dirs;
}
/**
* Return full file path from PHP include_path
*
* @param string[] $dirs
* @param string $file
*
* @return bool|string full filepath or false
*/
public function getIncludePath($dirs, $file)
{
if (!($this->_has_stream_include ?? $this->_has_stream_include = function_exists('stream_resolve_include_path'))
) {
$this->isNewIncludePath();
}
// try PHP include_path
foreach ($dirs as $dir) {
$dir_n = $this->number[$dir] ?? $this->number[$dir] = $this->counter++;
if (isset($this->isFile[ $dir_n ][ $file ])) {
if ($this->isFile[ $dir_n ][ $file ]) {
return $this->isFile[ $dir_n ][ $file ];
} else {
continue;
}
}
if (isset($this->_user_dirs[ $dir_n ])) {
if (false === $this->_user_dirs[ $dir_n ]) {
continue;
} else {
$dir = $this->_user_dirs[ $dir_n ];
}
} else {
if ($dir[ 0 ] === '/' || $dir[ 1 ] === ':') {
$dir = str_ireplace(getcwd(), '.', $dir);
if ($dir[ 0 ] === '/' || $dir[ 1 ] === ':') {
$this->_user_dirs[ $dir_n ] = false;
continue;
}
}
$dir = substr($dir, 2);
$this->_user_dirs[ $dir_n ] = $dir;
}
if ($this->_has_stream_include) {
$path = stream_resolve_include_path($dir . ($file ?? ''));
if ($path) {
return $this->isFile[ $dir_n ][ $file ] = $path;
}
} else {
foreach ($this->_include_dirs as $key => $_i_path) {
$path = $this->isPath[$key][$dir_n] ?? $this->isPath[$key][$dir_n] = is_dir($_dir_path = $_i_path . $dir) ? $_dir_path : false;
if ($path === false) {
continue;
}
if (isset($file)) {
$_file = $this->isFile[ $dir_n ][ $file ] = (is_file($path . $file)) ? $path . $file : false;
if ($_file) {
return $_file;
}
} else {
// no file was given return directory path
return $path;
}
}
}
}
return false;
}
/**
* Writes file in a safe way to disk
*

View File

@@ -43,39 +43,14 @@ class TestInstall
$template_dir = realpath($template_dir);
// resolve include_path or fail existence
if (!$template_dir) {
if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_template_dir)) {
// try PHP include_path
if ($_stream_resolve_include_path) {
$template_dir = stream_resolve_include_path($_template_dir);
} else {
$template_dir = $smarty->getIncludePath($_template_dir, null);
}
if ($template_dir !== false) {
if ($errors === null) {
echo "$template_dir is OK.\n";
}
continue;
} else {
$status = false;
$message =
"FAILED: $_template_dir does not exist (and couldn't be found in include_path either)";
if ($errors === null) {
echo $message . ".\n";
} else {
$errors[ 'template_dir' ] = $message;
}
continue;
}
$status = false;
$message = "FAILED: $_template_dir does not exist";
if ($errors === null) {
echo $message . ".\n";
} else {
$status = false;
$message = "FAILED: $_template_dir does not exist";
if ($errors === null) {
echo $message . ".\n";
} else {
$errors[ 'template_dir' ] = $message;
}
continue;
$errors[ 'template_dir' ] = $message;
}
continue;
}
if (!is_dir($template_dir)) {
$status = false;
@@ -196,38 +171,14 @@ class TestInstall
$_config_dir = $config_dir;
// resolve include_path or fail existence
if (!$config_dir) {
if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_config_dir)) {
// try PHP include_path
if ($_stream_resolve_include_path) {
$config_dir = stream_resolve_include_path($_config_dir);
} else {
$config_dir = $smarty->getIncludePath($_config_dir, null);
}
if ($config_dir !== false) {
if ($errors === null) {
echo "$config_dir is OK.\n";
}
continue;
} else {
$status = false;
$message = "FAILED: $_config_dir does not exist (and couldn't be found in include_path either)";
if ($errors === null) {
echo $message . ".\n";
} else {
$errors[ 'config_dir' ] = $message;
}
continue;
}
$status = false;
$message = "FAILED: $_config_dir does not exist";
if ($errors === null) {
echo $message . ".\n";
} else {
$status = false;
$message = "FAILED: $_config_dir does not exist";
if ($errors === null) {
echo $message . ".\n";
} else {
$errors[ 'config_dir' ] = $message;
}
continue;
$errors[ 'config_dir' ] = $message;
}
continue;
}
if (!is_dir($config_dir)) {
$status = false;

View File

@@ -1,5 +0,0 @@
<?php
function smarty_function_plugin1($params, $template)
{
return 'plugin1';
}

View File

@@ -1,5 +0,0 @@
<?php
function smarty_function_plugin2($params, $template)
{
return 'plugin2';
}

View File

@@ -1 +0,0 @@
include_test2

View File

@@ -1 +0,0 @@
include_test4

View File

@@ -1 +0,0 @@
{include 'include_test4.tpl'}

View File

@@ -1 +0,0 @@
include path root

View File

@@ -1,51 +0,0 @@
<?php
/**
* Smarty PHPunit tests for File resources
*
* @package PHPunit
* @author Rodney Rehm
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class IncludePathTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->use_include_path = true;
$this->smarty->setPluginsDir(array('./include','./include1'));
$this->smarty->enableSecurity();
$ds = DIRECTORY_SEPARATOR;
set_include_path($this->smarty->_realpath(__DIR__ . "{$ds}..{$ds}..{$ds}..{$ds}Include_Path{$ds}Plugins{$ds}", true) . PATH_SEPARATOR . get_include_path());
}
/**
* Tears down the fixture
* This method is called after a test is executed.
*
*/
protected function tearDown(): void
{
ini_restore('include_path');
$this->smarty->disableSecurity();
parent::tearDown();
}
public function testInit()
{
$this->cleanDirs();
}
public function testInclude1()
{
$this->assertStringContainsString('plugin1', $this->smarty->fetch('test_include_path1.tpl'));
}
public function testInclude2()
{
$this->assertStringContainsString('plugin2', $this->smarty->fetch('test_include_path2.tpl'));
}
public function testInclude3()
{
$this->assertStringContainsString('plugin3', $this->smarty->fetch('test_include_path3.tpl'));
}
}

View File

@@ -1,54 +0,0 @@
<?php
/**
* Smarty PHPunit basic core function tests
*
* @package PHPunit
* @author Uwe Tews
*/
/**
* class core function tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class LoadPluginTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* loadPlugin test unkown plugin
*/
public function testLoadPluginErrorReturn()
{
$this->assertFalse($this->smarty->loadPlugin('\\Smarty\\Not\\Known'));
}
/**
* loadPlugin test \Smarty\Debug exists
*/
public function testLoadPluginSmartyInternalDebug()
{
$this->assertTrue($this->smarty->loadPlugin(\Smarty\Debug::class) == true);
}
/**
* loadPlugin test \Smarty\Template exists
*/
public function testLoadPluginSmartyTemplateClass()
{
$this->assertTrue($this->smarty->loadPlugin(\Smarty\Template) == true);
}
/**
* loadPlugin test loaging from plugins_dir
*/
public function testLoadPluginSmartyPluginCounter()
{
$this->assertTrue($this->smarty->loadPlugin('smarty_function_counter') == true);
}
}

View File

@@ -1,5 +0,0 @@
<?php
function smarty_function_plugin3($params, $template)
{
return 'plugin3';
}

View File

@@ -1,7 +0,0 @@
<?php
function smarty_function_chain1($params, $tpl)
{
$tpl->smarty->loadPlugin('smarty_function_chain2');
return smarty_function_chain2($params, $tpl);
}

View File

@@ -1,7 +0,0 @@
<?php
function smarty_function_chain2($params, $tpl)
{
$tpl->smarty->loadPlugin('smarty_function_chain3');
return smarty_function_chain3($params, $tpl);
}

View File

@@ -1,5 +0,0 @@
<?php
function smarty_function_chain3($params, $tpl)
{
return 'from chain3';
}

View File

@@ -1,33 +0,0 @@
<?php
/**
* Smarty PHPunit tests chained loading of dependend pluglind
*
* @package PHPunit
* @author Rodney Rehm
*/
/**
* class for modifier tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class PluginChainedLoadTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testInit()
{
$this->cleanDirs();
}
public function testPluginChainedLoad()
{
$this->smarty->addPluginsDir(__DIR__ . "/PHPunitplugins/");
$this->assertStringContainsString('from chain3', $this->smarty->fetch('test_plugin_chained_load.tpl'));
}
}

View File

@@ -1,27 +0,0 @@
<?php
/**
* Smarty PHPunit tests of shared plugin functions
*
* @package PHPunit
* @author Rodney Rehm
*/
/**
* class for function tests
*/
class SharedFunctionsTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* test smarty_function_escape_special_chars()
*/
public function testEscapeSpecialChars()
{
$this->assertEquals('hello&lt;world &copy;', smarty_function_escape_special_chars('hello<world &copy;'));
$this->assertEquals('ö€', smarty_function_escape_special_chars('ö€'));
}
}

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -1,26 +0,0 @@
<?php
class _object_toString
{
protected $string = null;
public function __construct($string)
{
$this->string = (string) $string;
}
public function __toString()
{
return $this->string;
}
}
class _object_noString
{
protected $string = null;
public function __construct($string)
{
$this->string = (string) $string;
}
}

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -29,8 +29,6 @@ class CacheResourceCustomPDOGzipTest extends CacheResourceTestCommon
$this->setUpSmarty(__DIR__);
parent::setUp();
$this->smarty->setCachingType('pdo');
$this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdo_Gziptest'),
'loadPlugin() could not load PDOGzip cache resource');
$this->smarty->registerCacheResource('pdo', new Smarty_CacheResource_Pdo_Gziptest($this->getPDO(),
'output_cache'));
}

View File

@@ -1,67 +0,0 @@
<?php
/**
* Smarty PHPunit tests for File resources
*
* @package PHPunit
* @author Rodney Rehm
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class FileIncludePathTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->use_include_path = true;
$this->smarty->setTemplateDir(array('./templates', './templates_2', './include'));
$this->smarty->enableSecurity();
$ds = DIRECTORY_SEPARATOR;
set_include_path($this->smarty->_realpath(__DIR__ . "{$ds}..{$ds}..{$ds}..{$ds}Include_Path{$ds}Tpl{$ds}", true) . PATH_SEPARATOR . get_include_path());
}
/**
* Tears down the fixture
* This method is called after a test is executed.
*
*/
protected function tearDown(): void
{
ini_restore('include_path');
$this->smarty->disableSecurity();
parent::tearDown();
}
public function testInit()
{
$this->cleanDirs();
}
public function testInclude1()
{
$this->assertStringContainsString('include_test1', $this->smarty->fetch('test1.tpl'));
}
public function testInclude2()
{
$this->assertStringContainsString('include_test2', $this->smarty->fetch('test2.tpl'));
}
public function testInclude3()
{
$this->assertStringContainsString('include_test3', $this->smarty->fetch('test3.tpl'));
}
public function testInclude31()
{
$this->smarty->use_include_path = false;
$this->smarty->security_policy->secure_dir = getcwd();
$this->assertStringContainsString('include_test3', $this->smarty->fetch('test3.tpl'));
}
public function testInclude4()
{
$this->assertStringContainsString('include_test4', $this->smarty->fetch('test4.tpl'));
}
public function testInclude5()
{
$this->smarty->setTemplateDir(array('./'));
$this->assertStringContainsString('include path root', $this->smarty->fetch('test5.tpl'));
}
}

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -1 +0,0 @@
include_test3

View File

@@ -1 +0,0 @@
{include 'include_test1.tpl'}

View File

@@ -1 +0,0 @@
{include 'include_test2.tpl'}

View File

@@ -1 +0,0 @@
{include 'include_test3.tpl'}

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -49,7 +49,6 @@ class ResourcePluginTest extends PHPUnit_Smarty
public function testResourcePluginRegisteredInstance()
{
$this->smarty->addPluginsDir("./PHPunitplugins/");
$this->smarty->loadPlugin('_Db2Plugin');
$this->smarty->registerResource('db2a', new _Db2Plugin('db2a'));
$this->assertEquals('hello world', $this->smarty->fetch('db2a:test'));
}