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-2005 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
16 /** Everything needs global.. */
17 require_once(SM_PATH
. 'functions/global.php');
18 require_once(SM_PATH
. 'functions/prefs.php');
20 global $squirrelmail_plugin_hooks;
21 $squirrelmail_plugin_hooks = array();
24 * This function adds a plugin.
25 * @param string $name Internal plugin name (ie. delete_move_next)
28 function use_plugin ($name) {
29 if (file_exists(SM_PATH
. "plugins/$name/setup.php")) {
30 include_once(SM_PATH
. "plugins/$name/setup.php");
31 $function = "squirrelmail_plugin_init_$name";
32 if (function_exists($function)) {
39 * This function executes a hook.
40 * @param string $name Name of hook to fire
43 function do_hook ($name) {
44 global $squirrelmail_plugin_hooks, $currentHookName;
45 $data = func_get_args();
46 $currentHookName = $name;
48 if (isset($squirrelmail_plugin_hooks[$name])
49 && is_array($squirrelmail_plugin_hooks[$name])) {
50 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
51 /* Add something to set correct gettext domain for plugin. */
52 if (function_exists($function)) {
58 $currentHookName = '';
60 /* Variable-length argument lists have a slight problem when */
61 /* passing values by reference. Pity. This is a workaround. */
66 * This function executes a hook and allows for parameters to be passed.
68 * @param string name the name of the hook
69 * @param mixed param the parameters to pass to the hook function
70 * @return mixed the return value of the hook function
72 function do_hook_function($name,$parm=NULL) {
73 global $squirrelmail_plugin_hooks, $currentHookName;
75 $currentHookName = $name;
77 if (isset($squirrelmail_plugin_hooks[$name])
78 && is_array($squirrelmail_plugin_hooks[$name])) {
79 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
80 /* Add something to set correct gettext domain for plugin. */
81 if (function_exists($function)) {
82 $ret = $function($parm);
87 $currentHookName = '';
89 /* Variable-length argument lists have a slight problem when */
90 /* passing values by reference. Pity. This is a workaround. */
95 * This function executes a hook, concatenating the results of each
96 * plugin that has the hook defined.
98 * @param string name the name of the hook
99 * @param mixed parm optional hook function parameters
100 * @return string a concatenation of the results of each plugin function
102 function concat_hook_function($name,$parm=NULL) {
103 global $squirrelmail_plugin_hooks, $currentHookName;
105 $currentHookName = $name;
107 if (isset($squirrelmail_plugin_hooks[$name])
108 && is_array($squirrelmail_plugin_hooks[$name])) {
109 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
110 /* Concatenate results from hook. */
111 if (function_exists($function)) {
112 $ret .= $function($parm);
117 $currentHookName = '';
119 /* Variable-length argument lists have a slight problem when */
120 /* passing values by reference. Pity. This is a workaround. */
125 * This function is used for hooks which are to return true or
126 * false. If $priority is > 0, any one or more trues will override
127 * any falses. If $priority < 0, then one or more falses will
128 * override any trues.
129 * Priority 0 means majority rules. Ties will be broken with $tie
131 * @param string name the hook name
132 * @param mixed parm the parameters for the hook function
133 * @param int priority
135 * @return bool the result of the function
137 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
138 global $squirrelmail_plugin_hooks, $currentHookName;
143 if (isset($squirrelmail_plugin_hooks[$name]) &&
144 is_array($squirrelmail_plugin_hooks[$name])) {
146 /* Loop over the plugins that registered the hook */
147 $currentHookName = $name;
148 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
149 if (function_exists($function)) {
150 $ret = $function($parm);
158 $currentHookName = '';
160 /* Examine the aftermath and assign the return value appropriately */
161 if (($priority > 0) && ($yea)) {
163 } elseif (($priority < 0) && ($nay)) {
165 } elseif ($yea > $nay) {
167 } elseif ($nay > $yea) {
170 // There's a tie, no action needed.
174 // If the code gets here, there was a problem - no hooks, etc.
179 * This function checks whether the user's USER_AGENT is known to
180 * be broken. If so, returns true and the plugin is invisible to the
182 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
183 * FIXME: This function needs to have its name changed!
185 * @return bool whether this browser properly supports JavaScript
186 * @deprecated use checkForJavascript() since 1.5.1
189 return !checkForJavascript();
193 * Check if plugin is enabled
194 * @param string $plugin_name plugin name
198 function is_plugin_enabled($plugin_name) {
202 * check if variable is empty. if var is not set, php empty
203 * returns true without error notice.
205 * then check if it is an array
207 if (empty($plugins) ||
! is_array($plugins))
210 if ( in_array($plugin_name,$plugins) ) {
217 /*************************************/
218 /*** MAIN PLUGIN LOADING CODE HERE ***/
219 /*************************************/
221 /* On startup, register all plugins configured for use. */
222 if (isset($plugins) && is_array($plugins)) {
223 foreach ($plugins as $name) {