- move $smarty->loadPlugin() into extension

This commit is contained in:
Uwe Tews
2015-06-27 20:56:27 +02:00
parent 75ccd86c8a
commit 291c06dbea
4 changed files with 85 additions and 53 deletions

View File

@@ -4,6 +4,7 @@
- update $smarty->_realpath for relative path not starting with './' - update $smarty->_realpath for relative path not starting with './'
- update Smarty security with new realpath handling - update Smarty security with new realpath handling
- update {include_php} with new realpath handling - update {include_php} with new realpath handling
- move $smarty->loadPlugin() into extension
19.06.2015 19.06.2015
- improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59 - improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59

View File

@@ -78,6 +78,7 @@ class Smarty_Autoloader
'smarty_internal_extension_config' => true, 'smarty_internal_extension_config' => true,
'smarty_internal_extension_filter' => true, 'smarty_internal_extension_filter' => true,
'smarty_internal_extension_object' => true, 'smarty_internal_extension_object' => true,
'smarty_internal_extension_loadplugin' => true,
'smarty_internal_filter_handler' => true, 'smarty_internal_filter_handler' => true,
'smarty_internal_function_call_handler' => true, 'smarty_internal_function_call_handler' => true,
'smarty_internal_cacheresource_file' => true, 'smarty_internal_cacheresource_file' => true,

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.28-dev/8'; const SMARTY_VERSION = '3.1.28-dev/9';
/** /**
* define variable scopes * define variable scopes
@@ -1416,58 +1416,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public function loadPlugin($plugin_name, $check = true) public function loadPlugin($plugin_name, $check = true)
{ {
// if function or class exists, exit silently (already loaded) return Smarty_Internal_Extension_LoadPlugin::loadPlugin($this, $plugin_name, $check);
if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
return true;
}
// Plugin name is expected to be: Smarty_[Type]_[Name]
$_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");
}
// if type is "internal", get plugin from sysplugins
if (strtolower($_name_parts[1]) == 'internal') {
$file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
if (isset($this->_is_file_cache[$file]) ? $this->_is_file_cache[$file] : $this->_is_file_cache[$file] = is_file($file)) {
require_once($file);
return $file;
} else {
return false;
}
}
// plugin filename is expected to be: [type].[name].php
$_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
$_stream_resolve_include_path = function_exists('stream_resolve_include_path');
// loop through plugin dirs and find the plugin
foreach ($this->getPluginsDir() as $_plugin_dir) {
$names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename),);
foreach ($names as $file) {
if (isset($this->_is_file_cache[$file]) ? $this->_is_file_cache[$file] : $this->_is_file_cache[$file] = is_file($file)) {
require_once($file);
return $file;
}
if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
// try PHP include_path
if ($_stream_resolve_include_path) {
$file = stream_resolve_include_path($file);
} else {
$file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
}
if ($file !== false) {
require_once($file);
return $file;
}
}
}
}
// no plugin loaded
return false;
} }
/** /**

View File

@@ -0,0 +1,81 @@
<?php
/**
* Smarty Extension Loadplugin
*
* $smarty->loadPlugin() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Extension_LoadPlugin
{
/**
* Takes unknown classes and loads plugin files for them
* class name format: Smarty_PluginType_PluginName
* plugin filename format: plugintype.pluginname.php
*
* @param \Smarty $smarty
* @param string $plugin_name class plugin name to load
* @param bool $check check if already loaded
*
* @return bool|string
* @throws \SmartyException
*/
public static function loadPlugin(Smarty $smarty, $plugin_name, $check)
{
// if function or class exists, exit silently (already loaded)
if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
return true;
}
// Plugin name is expected to be: Smarty_[Type]_[Name]
$_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");
}
// if type is "internal", get plugin from sysplugins
if (strtolower($_name_parts[1]) == 'internal') {
$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)) {
require_once($file);
return $file;
} else {
return false;
}
}
// plugin filename is expected to be: [type].[name].php
$_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
$_stream_resolve_include_path = function_exists('stream_resolve_include_path');
// loop through plugin dirs and find the plugin
foreach ($smarty->getPluginsDir() as $_plugin_dir) {
$names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename),);
foreach ($names as $file) {
if (isset($smarty->_is_file_cache[$file]) ? $smarty->_is_file_cache[$file] : $smarty->_is_file_cache[$file] = is_file($file)) {
require_once($file);
return $file;
}
if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
// try PHP include_path
if ($_stream_resolve_include_path) {
$file = stream_resolve_include_path($file);
} else {
$file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
}
if ($file !== false) {
require_once($file);
return $file;
}
}
}
}
// no plugin loaded
return false;
}
}