Редакция 2 | Только различия | Учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 58 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * Smarty plugin
|
3 | * Smarty plugin
|
4 | * @package Smarty
|
4 | * @package Smarty
|
5 | * @subpackage plugins
|
5 | * @subpackage plugins
|
6 | */
|
6 | */
|
7 | 7 | ||
8 | /**
|
8 | /**
|
9 | * Load requested plugins
|
9 | * Load requested plugins
|
10 | *
|
10 | *
|
11 | * @param array $plugins
|
11 | * @param array $plugins
|
12 | */
|
12 | */
|
13 | 13 | ||
14 | // $plugins
|
14 | // $plugins
|
15 | 15 | ||
16 | function smarty_core_load_plugins($params, &$smarty) |
16 | function smarty_core_load_plugins($params, &$smarty) |
17 | {
|
17 | {
|
18 | 18 | ||
19 | foreach ($params['plugins'] as $_plugin_info) { |
19 | foreach ($params['plugins'] as $_plugin_info) { |
20 | list($_type, $_name, $_tpl_file, $_tpl_line, $_delayed_loading) = $_plugin_info; |
20 | list($_type, $_name, $_tpl_file, $_tpl_line, $_delayed_loading) = $_plugin_info; |
21 | $_plugin = &$smarty->_plugins[$_type][$_name]; |
21 | $_plugin = &$smarty->_plugins[$_type][$_name]; |
22 | 22 | ||
23 | /*
|
23 | /*
|
24 | * We do not load plugin more than once for each instance of Smarty.
|
24 | * We do not load plugin more than once for each instance of Smarty.
|
25 | * The following code checks for that. The plugin can also be
|
25 | * The following code checks for that. The plugin can also be
|
26 | * registered dynamically at runtime, in which case template file
|
26 | * registered dynamically at runtime, in which case template file
|
27 | * and line number will be unknown, so we fill them in.
|
27 | * and line number will be unknown, so we fill them in.
|
28 | *
|
28 | *
|
29 | * The final element of the info array is a flag that indicates
|
29 | * The final element of the info array is a flag that indicates
|
30 | * whether the dynamically registered plugin function has been
|
30 | * whether the dynamically registered plugin function has been
|
31 | * checked for existence yet or not.
|
31 | * checked for existence yet or not.
|
32 | */
|
32 | */
|
33 | if (isset($_plugin)) { |
33 | if (isset($_plugin)) { |
34 | if (empty($_plugin[3])) { |
34 | if (empty($_plugin[3])) { |
35 | if (!is_callable($_plugin[0])) { |
35 | if (!is_callable($_plugin[0])) { |
36 | $smarty->_trigger_fatal_error("[plugin] $_type '$_name' is not implemented", $_tpl_file, $_tpl_line, __FILE__, __LINE__); |
36 | $smarty->_trigger_fatal_error("[plugin] $_type '$_name' is not implemented", $_tpl_file, $_tpl_line, __FILE__, __LINE__); |
37 | } else { |
37 | } else { |
38 | $_plugin[1] = $_tpl_file; |
38 | $_plugin[1] = $_tpl_file; |
39 | $_plugin[2] = $_tpl_line; |
39 | $_plugin[2] = $_tpl_line; |
40 | $_plugin[3] = true; |
40 | $_plugin[3] = true; |
41 | if (!isset($_plugin[4])) $_plugin[4] = true; /* cacheable */ |
41 | if (!isset($_plugin[4])) $_plugin[4] = true; /* cacheable */ |
42 | }
|
42 | }
|
43 | }
|
43 | }
|
44 | continue; |
44 | continue; |
45 | } else if ($_type == 'insert') { |
45 | } else if ($_type == 'insert') { |
46 | /*
|
46 | /*
|
47 | * For backwards compatibility, we check for insert functions in
|
47 | * For backwards compatibility, we check for insert functions in
|
48 | * the symbol table before trying to load them as a plugin.
|
48 | * the symbol table before trying to load them as a plugin.
|
49 | */
|
49 | */
|
50 | $_plugin_func = 'insert_' . $_name; |
50 | $_plugin_func = 'insert_' . $_name; |
51 | if (function_exists($_plugin_func)) { |
51 | if (function_exists($_plugin_func)) { |
52 | $_plugin = array($_plugin_func, $_tpl_file, $_tpl_line, true, false); |
52 | $_plugin = array($_plugin_func, $_tpl_file, $_tpl_line, true, false); |
53 | continue; |
53 | continue; |
54 | }
|
54 | }
|
55 | }
|
55 | }
|
56 | 56 | ||
57 | $_plugin_file = $smarty->_get_plugin_filepath($_type, $_name); |
57 | $_plugin_file = $smarty->_get_plugin_filepath($_type, $_name); |
58 | 58 | ||
59 | if (! $_found = ($_plugin_file != false)) { |
59 | if (! $_found = ($_plugin_file != false)) { |
60 | $_message = "could not load plugin file '$_type.$_name.php'\n"; |
60 | $_message = "could not load plugin file '$_type.$_name.php'\n"; |
61 | }
|
61 | }
|
62 | 62 | ||
63 | /*
|
63 | /*
|
64 | * If plugin file is found, it -must- provide the properly named
|
64 | * If plugin file is found, it -must- provide the properly named
|
65 | * plugin function. In case it doesn't, simply output the error and
|
65 | * plugin function. In case it doesn't, simply output the error and
|
66 | * do not fall back on any other method.
|
66 | * do not fall back on any other method.
|
67 | */
|
67 | */
|
68 | if ($_found) { |
68 | if ($_found) { |
69 | include_once $_plugin_file; |
69 | include_once $_plugin_file; |
70 | 70 | ||
71 | $_plugin_func = 'smarty_' . $_type . '_' . $_name; |
71 | $_plugin_func = 'smarty_' . $_type . '_' . $_name; |
72 | if (!function_exists($_plugin_func)) { |
72 | if (!function_exists($_plugin_func)) { |
73 | $smarty->_trigger_fatal_error("[plugin] function $_plugin_func() not found in $_plugin_file", $_tpl_file, $_tpl_line, __FILE__, __LINE__); |
73 | $smarty->_trigger_fatal_error("[plugin] function $_plugin_func() not found in $_plugin_file", $_tpl_file, $_tpl_line, __FILE__, __LINE__); |
74 | continue; |
74 | continue; |
75 | }
|
75 | }
|
76 | }
|
76 | }
|
77 | /*
|
77 | /*
|
78 | * In case of insert plugins, their code may be loaded later via
|
78 | * In case of insert plugins, their code may be loaded later via
|
79 | * 'script' attribute.
|
79 | * 'script' attribute.
|
80 | */
|
80 | */
|
81 | else if ($_type == 'insert' && $_delayed_loading) { |
81 | else if ($_type == 'insert' && $_delayed_loading) { |
82 | $_plugin_func = 'smarty_' . $_type . '_' . $_name; |
82 | $_plugin_func = 'smarty_' . $_type . '_' . $_name; |
83 | $_found = true; |
83 | $_found = true; |
84 | }
|
84 | }
|
85 | 85 | ||
86 | /*
|
86 | /*
|
87 | * Plugin specific processing and error checking.
|
87 | * Plugin specific processing and error checking.
|
88 | */
|
88 | */
|
89 | if (!$_found) { |
89 | if (!$_found) { |
90 | if ($_type == 'modifier') { |
90 | if ($_type == 'modifier') { |
91 | /*
|
91 | /*
|
92 | * In case modifier falls back on using PHP functions
|
92 | * In case modifier falls back on using PHP functions
|
93 | * directly, we only allow those specified in the security
|
93 | * directly, we only allow those specified in the security
|
94 | * context.
|
94 | * context.
|
95 | */
|
95 | */
|
96 | if ($smarty->security && !in_array($_name, $smarty->security_settings['MODIFIER_FUNCS'])) { |
96 | if ($smarty->security && !in_array($_name, $smarty->security_settings['MODIFIER_FUNCS'])) { |
97 | $_message = "(secure mode) modifier '$_name' is not allowed"; |
97 | $_message = "(secure mode) modifier '$_name' is not allowed"; |
98 | } else { |
98 | } else { |
99 | if (!function_exists($_name)) { |
99 | if (!function_exists($_name)) { |
100 | $_message = "modifier '$_name' is not implemented"; |
100 | $_message = "modifier '$_name' is not implemented"; |
101 | } else { |
101 | } else { |
102 | $_plugin_func = $_name; |
102 | $_plugin_func = $_name; |
103 | $_found = true; |
103 | $_found = true; |
104 | }
|
104 | }
|
105 | }
|
105 | }
|
106 | } else if ($_type == 'function') { |
106 | } else if ($_type == 'function') { |
107 | /*
|
107 | /*
|
108 | * This is a catch-all situation.
|
108 | * This is a catch-all situation.
|
109 | */
|
109 | */
|
110 | $_message = "unknown tag - '$_name'"; |
110 | $_message = "unknown tag - '$_name'"; |
111 | }
|
111 | }
|
112 | }
|
112 | }
|
113 | 113 | ||
114 | if ($_found) { |
114 | if ($_found) { |
115 | $smarty->_plugins[$_type][$_name] = array($_plugin_func, $_tpl_file, $_tpl_line, true, true); |
115 | $smarty->_plugins[$_type][$_name] = array($_plugin_func, $_tpl_file, $_tpl_line, true, true); |
116 | } else { |
116 | } else { |
117 | // output error
|
117 | // output error
|
118 | $smarty->_trigger_fatal_error('[plugin] ' . $_message, $_tpl_file, $_tpl_line, __FILE__, __LINE__); |
118 | $smarty->_trigger_fatal_error('[plugin] ' . $_message, $_tpl_file, $_tpl_line, __FILE__, __LINE__); |
119 | }
|
119 | }
|
120 | }
|
120 | }
|
121 | }
|
121 | }
|
122 | 122 | ||
123 | /* vim: set expandtab: */
|
123 | /* vim: set expandtab: */
|
124 | 124 | ||
125 | ?>
|
125 | ?>
|
126 | 126 |