1: <?php
2:
3: /**
4: * Smarty Extension Loadplugin
5: *
6: * $smarty->loadPlugin() method
7: *
8: * @package Smarty
9: * @subpackage PluginsInternal
10: * @author Uwe Tews
11: */
12: class Smarty_Internal_Method_LoadPlugin
13: {
14: /**
15: * Cache of searched plugin files
16: *
17: * @var array
18: */
19: public $plugin_files = array();
20:
21: /**
22: * Takes unknown classes and loads plugin files for them
23: * class name format: Smarty_PluginType_PluginName
24: * plugin filename format: plugintype.pluginname.php
25: *
26: * @param \Smarty $smarty
27: * @param string $plugin_name class plugin name to load
28: * @param bool $check check if already loaded
29: *
30: * @return bool|string
31: * @throws \SmartyException
32: */
33: public function loadPlugin(Smarty $smarty, $plugin_name, $check)
34: {
35: // if function or class exists, exit silently (already loaded)
36: if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
37: return true;
38: }
39: if (!preg_match('#^smarty_((internal)|([^_]+))_(.+)$#i', $plugin_name, $match)) {
40: throw new SmartyException("plugin {$plugin_name} is not a valid name format");
41: }
42: if (!empty($match[ 2 ])) {
43: $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
44: if (isset($this->plugin_files[ $file ])) {
45: if ($this->plugin_files[ $file ] !== false) {
46: return $this->plugin_files[ $file ];
47: } else {
48: return false;
49: }
50: } else {
51: if (is_file($file)) {
52: $this->plugin_files[ $file ] = $file;
53: include_once $file;
54: return $file;
55: } else {
56: $this->plugin_files[ $file ] = false;
57: return false;
58: }
59: }
60: }
61: // plugin filename is expected to be: [type].[name].php
62: $_plugin_filename = "{$match[1]}.{$match[4]}.php";
63: $_lower_filename = strtolower($_plugin_filename);
64: if (isset($this->plugin_files)) {
65: if (isset($this->plugin_files[ 'plugins_dir' ][ $_lower_filename ])) {
66: if (!$smarty->use_include_path || $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ] !== false) {
67: return $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ];
68: }
69: }
70: if (!$smarty->use_include_path || $smarty->ext->_getIncludePath->isNewIncludePath($smarty)) {
71: unset($this->plugin_files[ 'include_path' ]);
72: } else {
73: if (isset($this->plugin_files[ 'include_path' ][ $_lower_filename ])) {
74: return $this->plugin_files[ 'include_path' ][ $_lower_filename ];
75: }
76: }
77: }
78: $_file_names = array($_plugin_filename);
79: if ($_lower_filename !== $_plugin_filename) {
80: $_file_names[] = $_lower_filename;
81: }
82: $_p_dirs = $smarty->getPluginsDir();
83: if (!isset($this->plugin_files[ 'plugins_dir' ][ $_lower_filename ])) {
84: // loop through plugin dirs and find the plugin
85: foreach ($_p_dirs as $_plugin_dir) {
86: foreach ($_file_names as $name) {
87: $file = $_plugin_dir . $name;
88: if (is_file($file)) {
89: $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ] = $file;
90: include_once $file;
91: return $file;
92: }
93: $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ] = false;
94: }
95: }
96: }
97: if ($smarty->use_include_path) {
98: foreach ($_file_names as $_file_name) {
99: // try PHP include_path
100: $file = $smarty->ext->_getIncludePath->getIncludePath($_p_dirs, $_file_name, $smarty);
101: $this->plugin_files[ 'include_path' ][ $_lower_filename ] = $file;
102: if ($file !== false) {
103: include_once $file;
104: return $file;
105: }
106: }
107: }
108: // no plugin loaded
109: return false;
110: }
111: }
112: