From 291c06dbea75a618466d9cd719f1ca9672aad3ba Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Sat, 27 Jun 2015 20:56:27 +0200 Subject: [PATCH] - move $smarty->loadPlugin() into extension --- change_log.txt | 1 + libs/Autoloader.php | 1 + libs/Smarty.class.php | 55 +------------ .../smarty_internal_extension_loadplugin.php | 81 +++++++++++++++++++ 4 files changed, 85 insertions(+), 53 deletions(-) create mode 100644 libs/sysplugins/smarty_internal_extension_loadplugin.php diff --git a/change_log.txt b/change_log.txt index fea821ec..00c41a16 100644 --- a/change_log.txt +++ b/change_log.txt @@ -4,6 +4,7 @@ - update $smarty->_realpath for relative path not starting with './' - update Smarty security with new realpath handling - update {include_php} with new realpath handling + - move $smarty->loadPlugin() into extension 19.06.2015 - improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59 diff --git a/libs/Autoloader.php b/libs/Autoloader.php index 2dd1c747..7e4370d3 100644 --- a/libs/Autoloader.php +++ b/libs/Autoloader.php @@ -78,6 +78,7 @@ class Smarty_Autoloader 'smarty_internal_extension_config' => true, 'smarty_internal_extension_filter' => true, 'smarty_internal_extension_object' => true, + 'smarty_internal_extension_loadplugin' => true, 'smarty_internal_filter_handler' => true, 'smarty_internal_function_call_handler' => true, 'smarty_internal_cacheresource_file' => true, diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index fdc2b377..1d4e7113 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/8'; + const SMARTY_VERSION = '3.1.28-dev/9'; /** * define variable scopes @@ -1416,58 +1416,7 @@ class Smarty extends Smarty_Internal_TemplateBase */ public function loadPlugin($plugin_name, $check = true) { - // 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($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; + return Smarty_Internal_Extension_LoadPlugin::loadPlugin($this, $plugin_name, $check); } /** diff --git a/libs/sysplugins/smarty_internal_extension_loadplugin.php b/libs/sysplugins/smarty_internal_extension_loadplugin.php new file mode 100644 index 00000000..ff268568 --- /dev/null +++ b/libs/sysplugins/smarty_internal_extension_loadplugin.php @@ -0,0 +1,81 @@ +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; + } +} \ No newline at end of file