6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This file provides the framework for a plugin architecture.
11 * Documentation on how to write plugins might show up some time.
14 * @package squirrelmail
17 /** Everything needs global.. */
18 require_once(SM_PATH
. 'functions/global.php');
19 require_once(SM_PATH
. 'functions/prefs.php');
21 global $squirrelmail_plugin_hooks;
22 $squirrelmail_plugin_hooks = array();
25 * This function adds a plugin.
26 * @param string $name Internal plugin name (ie. delete_move_next)
29 function use_plugin ($name) {
30 if (file_exists(SM_PATH
. "plugins/$name/setup.php")) {
31 include_once(SM_PATH
. "plugins/$name/setup.php");
32 $function = "squirrelmail_plugin_init_$name";
33 if (function_exists($function)) {
40 * This function executes a hook.
41 * @param string $name Name of hook to fire
44 function do_hook ($name) {
45 global $squirrelmail_plugin_hooks, $currentHookName;
46 $data = func_get_args();
48 $currentHookName = $name;
50 if (isset($squirrelmail_plugin_hooks[$name])
51 && is_array($squirrelmail_plugin_hooks[$name])) {
52 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
53 /* Add something to set correct gettext domain for plugin. */
54 if (function_exists($function)) {
60 $currentHookName = '';
62 /* Variable-length argument lists have a slight problem when */
63 /* passing values by reference. Pity. This is a workaround. */
68 * This function executes a hook and allows for parameters to be passed.
70 * @param string name the name of the hook
71 * @param mixed param the parameters to pass to the hook function
72 * @return mixed the return value of the hook function
74 function do_hook_function($name,$parm=NULL) {
75 global $squirrelmail_plugin_hooks, $currentHookName;
77 $currentHookName = $name;
79 if (isset($squirrelmail_plugin_hooks[$name])
80 && is_array($squirrelmail_plugin_hooks[$name])) {
81 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
82 /* Add something to set correct gettext domain for plugin. */
83 if (function_exists($function)) {
84 $ret = $function($parm);
89 $currentHookName = '';
91 /* Variable-length argument lists have a slight problem when */
92 /* passing values by reference. Pity. This is a workaround. */
97 * This function executes a hook, concatenating the results of each
98 * plugin that has the hook defined.
100 * @param string name the name of the hook
101 * @param mixed parm optional hook function parameters
102 * @return string a concatenation of the results of each plugin function
104 function concat_hook_function($name,$parm=NULL) {
105 global $squirrelmail_plugin_hooks, $currentHookName;
107 $currentHookName = $name;
109 if (isset($squirrelmail_plugin_hooks[$name])
110 && is_array($squirrelmail_plugin_hooks[$name])) {
111 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
112 /* Concatenate results from hook. */
113 if (function_exists($function)) {
114 $ret .= $function($parm);
119 $currentHookName = '';
121 /* Variable-length argument lists have a slight problem when */
122 /* passing values by reference. Pity. This is a workaround. */
127 * This function is used for hooks which are to return true or
128 * false. If $priority is > 0, any one or more trues will override
129 * any falses. If $priority < 0, then one or more falses will
130 * override any trues.
131 * Priority 0 means majority rules. Ties will be broken with $tie
133 * @param string name the hook name
134 * @param mixed parm the parameters for the hook function
135 * @param int priority
137 * @return bool the result of the function
139 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
140 global $squirrelmail_plugin_hooks, $currentHookName;
145 if (isset($squirrelmail_plugin_hooks[$name]) &&
146 is_array($squirrelmail_plugin_hooks[$name])) {
148 /* Loop over the plugins that registered the hook */
149 $currentHookName = $name;
150 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
151 if (function_exists($function)) {
152 $ret = $function($parm);
160 $currentHookName = '';
162 /* Examine the aftermath and assign the return value appropriately */
163 if (($priority > 0) && ($yea)) {
165 } elseif (($priority < 0) && ($nay)) {
167 } elseif ($yea > $nay) {
169 } elseif ($nay > $yea) {
172 // There's a tie, no action needed.
176 // If the code gets here, there was a problem - no hooks, etc.
181 * This function checks whether the user's USER_AGENT is known to
182 * be broken. If so, returns true and the plugin is invisible to the
184 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
185 * FIXME: This function needs to have its name changed!
187 * @return bool whether this browser properly supports JavaScript
190 return !checkForJavascript();
192 /*************************************/
193 /*** MAIN PLUGIN LOADING CODE HERE ***/
194 /*************************************/
196 /* On startup, register all plugins configured for use. */
197 if (isset($plugins) && is_array($plugins)) {
198 foreach ($plugins as $name) {