2bc6bff060551e711419a499b3b9cbf3f9723900
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
60 * passed, wherein each plugin can modify the parameters before
61 * they are passed to the next funciton. Whether or not the
62 * parameters are modified, plugins on this hook should always
63 * return the given parameters.
65 * @param string name the name of the hook
66 * @param mixed param the parameters to pass to the hook function
67 * @return mixed the possibly modified hook parameters
69 function filter_hook_function($name,$parm=NULL) {
70 global $squirrelmail_plugin_hooks, $currentHookName;
72 $currentHookName = $name;
74 if (isset($squirrelmail_plugin_hooks[$name])
75 && is_array($squirrelmail_plugin_hooks[$name])) {
76 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
77 /* Add something to set correct gettext domain for plugin. */
78 if (function_exists($function)) {
79 $parm = $function($parm);
84 $currentHookName = '';
86 /* Variable-length argument lists have a slight problem when */
87 /* passing values by reference. Pity. This is a workaround. */
92 * This function executes a hook and allows for parameters to be passed.
94 * @param string name the name of the hook
95 * @param mixed param the parameters to pass to the hook function
96 * @return mixed the return value of the hook function
98 function do_hook_function($name,$parm=NULL) {
99 global $squirrelmail_plugin_hooks, $currentHookName;
101 $currentHookName = $name;
103 if (isset($squirrelmail_plugin_hooks[$name])
104 && is_array($squirrelmail_plugin_hooks[$name])) {
105 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
106 /* Add something to set correct gettext domain for plugin. */
107 if (function_exists($function)) {
108 $ret = $function($parm);
113 $currentHookName = '';
115 /* Variable-length argument lists have a slight problem when */
116 /* passing values by reference. Pity. This is a workaround. */
121 * This function executes a hook, allows for parameters to be passed,
122 * and looks for an array returned from each plugin: each array is
123 * then merged into one and returned to the core hook location.
125 * Note that unlike PHP's array_merge function, matching array keys
126 * will not overwrite each other, instead, values under such keys
127 * will be concatenated if they are both strings, or merged if they
128 * are arrays (in the same (non-overwrite) manner recursively).
130 * Plugins returning non-arrays (strings, objects, etc) will have
131 * their output added to the end of the ultimate return array,
132 * unless ALL values returned are strings, in which case one string
133 * with all returned strings concatenated together is returned.
135 * @param string name the name of the hook
136 * @param mixed param the parameters to pass to the hook function
138 * @return mixed the merged return arrays or strings of each
139 * plugin on this hook
142 function concat_hook_function($name,$parm=NULL) {
143 global $squirrelmail_plugin_hooks, $currentHookName;
146 $currentHookName = $name;
148 if (isset($squirrelmail_plugin_hooks[$name])
149 && is_array($squirrelmail_plugin_hooks[$name])) {
150 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
151 /* Add something to set correct gettext domain for plugin. */
152 if (function_exists($function)) {
153 // $ret .= $function($parm);
154 $ret = sqm_array_merge($ret, $function($parm));
159 $currentHookName = '';
161 /* Variable-length argument lists have a slight problem when */
162 /* passing values by reference. Pity. This is a workaround. */
163 // FIXME: empty return array breaks legacy use of this hook, so
164 // as a workaround, return empty string (let's fix the
165 // hook calls so we can pull this out)
166 if (empty($ret)) return '';
171 * This function is used for hooks which are to return true or
172 * false. If $priority is > 0, any one or more trues will override
173 * any falses. If $priority < 0, then one or more falses will
174 * override any trues.
175 * Priority 0 means majority rules. Ties will be broken with $tie
177 * @param string name the hook name
178 * @param mixed parm the parameters for the hook function
179 * @param int priority
181 * @return bool the result of the function
183 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
184 global $squirrelmail_plugin_hooks, $currentHookName;
189 if (isset($squirrelmail_plugin_hooks[$name]) &&
190 is_array($squirrelmail_plugin_hooks[$name])) {
192 /* Loop over the plugins that registered the hook */
193 $currentHookName = $name;
194 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
195 if (function_exists($function)) {
196 $ret = $function($parm);
204 $currentHookName = '';
206 /* Examine the aftermath and assign the return value appropriately */
207 if (($priority > 0) && ($yea)) {
209 } elseif (($priority < 0) && ($nay)) {
211 } elseif ($yea > $nay) {
213 } elseif ($nay > $yea) {
216 // There's a tie, no action needed.
220 // If the code gets here, there was a problem - no hooks, etc.
225 * This function checks whether the user's USER_AGENT is known to
226 * be broken. If so, returns true and the plugin is invisible to the
228 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
229 * FIXME: This function needs to have its name changed!
231 * @return bool whether this browser properly supports JavaScript
232 * @deprecated use checkForJavascript() since 1.5.1
235 return !checkForJavascript();
239 * Check if plugin is enabled
240 * @param string $plugin_name plugin name
244 function is_plugin_enabled($plugin_name) {
248 * check if variable is empty. if var is not set, php empty
249 * returns true without error notice.
251 * then check if it is an array
253 if (empty($plugins) ||
! is_array($plugins))
256 if ( in_array($plugin_name,$plugins) ) {
264 * Check a plugin's version.
266 * Returns TRUE if the given plugin is installed,
267 * activated and is at minimum version $a.$b.$c.
268 * If any one of those conditions fails, FALSE
269 * will be returned (careful of plugins that are
270 * sufficiently versioned but are not activated).
272 * By overriding the default value of $force_inclusion,
273 * this function will attempt to grab versioning
274 * information from the given plugin even if it
275 * is not activated (plugin still has to be
276 * unpackaged and set in place in the plugins
277 * directory). Use with care - some plugins
278 * might break SquirrelMail when this is used.
280 * Note that this function assumes plugin
281 * versioning is consistently applied in the same
282 * fashion that SquirrelMail versions are, with the
283 * exception that an applicable SquirrelMail
284 * version may be appended to the version number
285 * (which will be ignored herein). That is, plugin
286 * version number schemes are expected in the following
287 * format: 1.2.3, or 1.2.3-1.4.0.
289 * Any characters after the third number are discarded,
290 * so formats such as the following will also work,
291 * although extra information about beta versions can
292 * possibly confuse the desired results of the version
293 * check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
297 * @param string plugin_name name of the plugin to
298 * check; must precisely
301 * @param int a major version number
302 * @param int b minor version number
303 * @param int c release number
304 * @param bool force_inclusion try to get version info
305 * for plugins not activated?
311 function check_plugin_version($plugin_name,
312 $a = 0, $b = 0, $c = 0,
313 $force_inclusion = FALSE)
316 $info_function = $plugin_name . '_info';
317 $version_function = $plugin_name . '_version';
318 $plugin_info = array();
319 $plugin_version = FALSE;
322 // first attempt to find the plugin info function, wherein
323 // the plugin version should be available
325 if (function_exists($info_function))
326 $plugin_info = $info_function();
327 else if ($force_inclusion
328 && file_exists(SM_PATH
. 'plugins/' . $plugin_name . '/setup.php'))
330 include_once(SM_PATH
. 'plugins/' . $plugin_name . '/setup.php');
331 if (function_exists($info_function))
332 $plugin_info = $info_function();
334 if (!empty($plugin_info['version']))
335 $plugin_version = $plugin_info['version'];
338 // otherwise, look for older version function
340 if (!$plugin_version && function_exists($version_function))
341 $plugin_version = $version_function();
344 if (!$plugin_version) return FALSE;
347 // now massage version number into something we understand
349 $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
350 '', $plugin_version),
352 $plugin_version = explode('.', $plugin_version);
353 if (!isset($plugin_version[0])) $plugin_version[0] = 0;
354 if (!isset($plugin_version[1])) $plugin_version[1] = 0;
355 if (!isset($plugin_version[2])) $plugin_version[2] = 0;
356 // sm_print_r($plugin_version);
359 // now test the version number
361 if ($plugin_version[0] < $a ||
362 ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
363 ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))