mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-10-30 20:01:37 +01:00 
			
		
		
		
	
		
			
	
	
		
			81 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			81 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
|   | <?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; | ||
|  |     } | ||
|  | } |