5576ab4d875550272b0ef171fbe7e5a76b561900
6 * This file provides the framework for a plugin architecture.
8 * Documentation on how to write plugins might show up some time.
10 * @copyright © 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
17 * This function adds a plugin.
18 * @param string $name Internal plugin name (ie. delete_move_next)
21 function use_plugin ($name) {
22 if (file_exists(SM_PATH
. "plugins/$name/setup.php")) {
23 include_once(SM_PATH
. "plugins/$name/setup.php");
24 $function = "squirrelmail_plugin_init_$name";
25 if (function_exists($function)) {
32 * This function executes a hook.
33 * @param string $name Name of hook to fire
36 function do_hook ($name) {
37 global $squirrelmail_plugin_hooks, $currentHookName;
38 $data = func_get_args();
39 $currentHookName = $name;
41 if (isset($squirrelmail_plugin_hooks[$name])
42 && is_array($squirrelmail_plugin_hooks[$name])) {
43 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
44 /* Add something to set correct gettext domain for plugin. */
45 if (function_exists($function)) {
51 $currentHookName = '';
53 /* Variable-length argument lists have a slight problem when */
54 /* passing values by reference. Pity. This is a workaround. */
59 * This function executes a hook and allows for parameters to be passed.
61 * @param string name the name of the hook
62 * @param mixed param the parameters to pass to the hook function
63 * @return mixed the return value of the hook function
65 function do_hook_function($name,$parm=NULL) {
66 global $squirrelmail_plugin_hooks, $currentHookName;
68 $currentHookName = $name;
70 if (isset($squirrelmail_plugin_hooks[$name])
71 && is_array($squirrelmail_plugin_hooks[$name])) {
72 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
73 /* Add something to set correct gettext domain for plugin. */
74 if (function_exists($function)) {
75 $ret = $function($parm);
80 $currentHookName = '';
82 /* Variable-length argument lists have a slight problem when */
83 /* passing values by reference. Pity. This is a workaround. */
88 * This function executes a hook, concatenating the results of each
89 * plugin that has the hook defined.
91 * @param string name the name of the hook
92 * @param mixed parm optional hook function parameters
93 * @return string a concatenation of the results of each plugin function
95 function concat_hook_function($name,$parm=NULL) {
96 global $squirrelmail_plugin_hooks, $currentHookName;
98 $currentHookName = $name;
100 if (isset($squirrelmail_plugin_hooks[$name])
101 && is_array($squirrelmail_plugin_hooks[$name])) {
102 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
103 /* Concatenate results from hook. */
104 if (function_exists($function)) {
105 $ret .= $function($parm);
110 $currentHookName = '';
112 /* Variable-length argument lists have a slight problem when */
113 /* passing values by reference. Pity. This is a workaround. */
118 * This function is used for hooks which are to return true or
119 * false. If $priority is > 0, any one or more trues will override
120 * any falses. If $priority < 0, then one or more falses will
121 * override any trues.
122 * Priority 0 means majority rules. Ties will be broken with $tie
124 * @param string name the hook name
125 * @param mixed parm the parameters for the hook function
126 * @param int priority
128 * @return bool the result of the function
130 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
131 global $squirrelmail_plugin_hooks, $currentHookName;
136 if (isset($squirrelmail_plugin_hooks[$name]) &&
137 is_array($squirrelmail_plugin_hooks[$name])) {
139 /* Loop over the plugins that registered the hook */
140 $currentHookName = $name;
141 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
142 if (function_exists($function)) {
143 $ret = $function($parm);
151 $currentHookName = '';
153 /* Examine the aftermath and assign the return value appropriately */
154 if (($priority > 0) && ($yea)) {
156 } elseif (($priority < 0) && ($nay)) {
158 } elseif ($yea > $nay) {
160 } elseif ($nay > $yea) {
163 // There's a tie, no action needed.
167 // If the code gets here, there was a problem - no hooks, etc.
172 * This function checks whether the user's USER_AGENT is known to
173 * be broken. If so, returns true and the plugin is invisible to the
175 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
176 * FIXME: This function needs to have its name changed!
178 * @return bool whether this browser properly supports JavaScript
179 * @deprecated use checkForJavascript() since 1.5.1
182 return !checkForJavascript();
186 * Check if plugin is enabled
187 * @param string $plugin_name plugin name
191 function is_plugin_enabled($plugin_name) {
195 * check if variable is empty. if var is not set, php empty
196 * returns true without error notice.
198 * then check if it is an array
200 if (empty($plugins) ||
! is_array($plugins))
203 if ( in_array($plugin_name,$plugins) ) {
211 * Check a plugin's version.
213 * Returns TRUE if the given plugin is installed,
214 * activated and is at minimum version $a.$b.$c.
215 * If any one of those conditions fails, FALSE
216 * will be returned (careful of plugins that are
217 * sufficiently versioned but are not activated).
219 * By overriding the default value of $force_inclusion,
220 * this function will attempt to grab versioning
221 * information from the given plugin even if it
222 * is not activated (plugin still has to be
223 * unpackaged and set in place in the plugins
224 * directory). Use with care - some plugins
225 * might break SquirrelMail when this is used.
227 * Note that this function assumes plugin
228 * versioning is consistently applied in the same
229 * fashion that SquirrelMail versions are, with the
230 * exception that an applicable SquirrelMail
231 * version may be appended to the version number
232 * (which will be ignored herein). That is, plugin
233 * version number schemes are expected in the following
234 * format: 1.2.3, or 1.2.3-1.4.0.
236 * Any characters after the third number are discarded,
237 * so formats such as the following will also work,
238 * although extra information about beta versions can
239 * possibly confuse the desired results of the version
240 * check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
244 * @param string plugin_name name of the plugin to
245 * check; must precisely
248 * @param int a major version number
249 * @param int b minor version number
250 * @param int c release number
251 * @param bool force_inclusion try to get version info
252 * for plugins not activated?
258 function check_plugin_version($plugin_name,
259 $a = 0, $b = 0, $c = 0,
260 $force_inclusion = FALSE)
263 $version_function = $plugin_name . '_version';
264 $plugin_version = FALSE;
267 // attempt to find version function and get version from plugin
269 if (function_exists($version_function))
270 $plugin_version = $version_function();
271 else if ($force_inclusion
272 && file_exists(SM_PATH
. 'plugins/' . $plugin_name . '/setup.php'))
274 include_once(SM_PATH
. 'plugins/' . $plugin_name . '/setup.php');
275 if (function_exists($version_function))
276 $plugin_version = $version_function();
279 if (!$plugin_version) return FALSE;
282 // now massage version number into something we understand
284 $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
285 '', $plugin_version),
287 $plugin_version = explode('.', $plugin_version);
288 if (!isset($plugin_version[0])) $plugin_version[0] = 0;
289 if (!isset($plugin_version[1])) $plugin_version[1] = 0;
290 if (!isset($plugin_version[2])) $plugin_version[2] = 0;
291 // sm_print_r($plugin_version);
294 // now test the version number
296 if ($plugin_version[0] < $a ||
297 ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
298 ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))