7b086a80 |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * plugin.php |
5 | * |
35586184 |
6 | * This file provides the framework for a plugin architecture. |
7 | * |
8 | * Documentation on how to write plugins might show up some time. |
9 | * |
4b4abf93 |
10 | * @copyright © 1999-2005 The SquirrelMail Project Team |
11 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
12 | * @version $Id$ |
d6c32258 |
13 | * @package squirrelmail |
35586184 |
14 | */ |
15 | |
d6c32258 |
16 | /** Everything needs global.. */ |
cd21d1aa |
17 | require_once(SM_PATH . 'functions/global.php'); |
7349fa12 |
18 | require_once(SM_PATH . 'functions/prefs.php'); |
cd21d1aa |
19 | |
35586184 |
20 | global $squirrelmail_plugin_hooks; |
0a17ec32 |
21 | $squirrelmail_plugin_hooks = array(); |
2d367c68 |
22 | |
d6c32258 |
23 | /** |
24 | * This function adds a plugin. |
25 | * @param string $name Internal plugin name (ie. delete_move_next) |
26 | * @return void |
27 | */ |
0606ca1f |
28 | function use_plugin ($name) { |
bd9c880b |
29 | if (file_exists(SM_PATH . "plugins/$name/setup.php")) { |
30 | include_once(SM_PATH . "plugins/$name/setup.php"); |
0606ca1f |
31 | $function = "squirrelmail_plugin_init_$name"; |
32 | if (function_exists($function)) { |
33 | $function(); |
2d367c68 |
34 | } |
2d367c68 |
35 | } |
0606ca1f |
36 | } |
2d367c68 |
37 | |
d6c32258 |
38 | /** |
39 | * This function executes a hook. |
40 | * @param string $name Name of hook to fire |
41 | * @return mixed $data |
42 | */ |
31524bcd |
43 | function do_hook ($name) { |
eb1f02bc |
44 | global $squirrelmail_plugin_hooks, $currentHookName; |
0606ca1f |
45 | $data = func_get_args(); |
eb1f02bc |
46 | $currentHookName = $name; |
a3439b27 |
47 | |
0606ca1f |
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)) { |
31524bcd |
53 | $function($data); |
54 | } |
55 | } |
56 | } |
57 | |
eb1f02bc |
58 | $currentHookName = ''; |
59 | |
31524bcd |
60 | /* Variable-length argument lists have a slight problem when */ |
61 | /* passing values by reference. Pity. This is a workaround. */ |
62 | return $data; |
63 | } |
64 | |
8b096f0a |
65 | /** |
66 | * This function executes a hook and allows for parameters to be passed. |
67 | * |
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 |
71 | */ |
31524bcd |
72 | function do_hook_function($name,$parm=NULL) { |
eb1f02bc |
73 | global $squirrelmail_plugin_hooks, $currentHookName; |
31524bcd |
74 | $ret = ''; |
eb1f02bc |
75 | $currentHookName = $name; |
31524bcd |
76 | |
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); |
dd389be5 |
83 | } |
2d367c68 |
84 | } |
2d367c68 |
85 | } |
7b086a80 |
86 | |
eb1f02bc |
87 | $currentHookName = ''; |
88 | |
0606ca1f |
89 | /* Variable-length argument lists have a slight problem when */ |
90 | /* passing values by reference. Pity. This is a workaround. */ |
2586d588 |
91 | return $ret; |
0606ca1f |
92 | } |
7b086a80 |
93 | |
8b096f0a |
94 | /** |
95 | * This function executes a hook, concatenating the results of each |
96 | * plugin that has the hook defined. |
97 | * |
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 |
101 | */ |
09ac2863 |
102 | function concat_hook_function($name,$parm=NULL) { |
eb1f02bc |
103 | global $squirrelmail_plugin_hooks, $currentHookName; |
09ac2863 |
104 | $ret = ''; |
eb1f02bc |
105 | $currentHookName = $name; |
31524bcd |
106 | |
09ac2863 |
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); |
113 | } |
114 | } |
115 | } |
116 | |
eb1f02bc |
117 | $currentHookName = ''; |
118 | |
09ac2863 |
119 | /* Variable-length argument lists have a slight problem when */ |
120 | /* passing values by reference. Pity. This is a workaround. */ |
121 | return $ret; |
122 | } |
cd7fc9e6 |
123 | |
5576644b |
124 | /** |
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. |
8b096f0a |
129 | * Priority 0 means majority rules. Ties will be broken with $tie |
130 | * |
131 | * @param string name the hook name |
132 | * @param mixed parm the parameters for the hook function |
133 | * @param int priority |
134 | * @param bool tie |
135 | * @return bool the result of the function |
136 | */ |
5576644b |
137 | function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) { |
eb1f02bc |
138 | global $squirrelmail_plugin_hooks, $currentHookName; |
5576644b |
139 | $yea = 0; |
140 | $nay = 0; |
141 | $ret = $tie; |
142 | |
143 | if (isset($squirrelmail_plugin_hooks[$name]) && |
144 | is_array($squirrelmail_plugin_hooks[$name])) { |
145 | |
146 | /* Loop over the plugins that registered the hook */ |
eb1f02bc |
147 | $currentHookName = $name; |
5576644b |
148 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
149 | if (function_exists($function)) { |
150 | $ret = $function($parm); |
151 | if ($ret) { |
152 | $yea++; |
153 | } else { |
154 | $nay++; |
155 | } |
156 | } |
157 | } |
eb1f02bc |
158 | $currentHookName = ''; |
5576644b |
159 | |
160 | /* Examine the aftermath and assign the return value appropriately */ |
161 | if (($priority > 0) && ($yea)) { |
162 | $ret = true; |
163 | } elseif (($priority < 0) && ($nay)) { |
164 | $ret = false; |
165 | } elseif ($yea > $nay) { |
166 | $ret = true; |
167 | } elseif ($nay > $yea) { |
168 | $ret = false; |
169 | } else { |
170 | // There's a tie, no action needed. |
171 | } |
172 | return $ret; |
173 | } |
174 | // If the code gets here, there was a problem - no hooks, etc. |
175 | return NULL; |
176 | } |
177 | |
cd7fc9e6 |
178 | /** |
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 |
181 | * offending browser. |
7349fa12 |
182 | * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT *** |
94ba79ce |
183 | * FIXME: This function needs to have its name changed! |
8b096f0a |
184 | * |
185 | * @return bool whether this browser properly supports JavaScript |
ccacde36 |
186 | * @deprecated use checkForJavascript() since 1.5.1 |
cd7fc9e6 |
187 | */ |
188 | function soupNazi(){ |
7349fa12 |
189 | return !checkForJavascript(); |
cd7fc9e6 |
190 | } |
fe0aa536 |
191 | |
192 | /** |
193 | * Check if plugin is enabled |
194 | * @param string $plugin_name plugin name |
195 | * @since 1.5.1 |
196 | * @return boolean |
197 | */ |
198 | function is_plugin_enabled($plugin_name) { |
199 | global $plugins; |
200 | |
201 | if (! isset($plugins) || ! is_array($plugins) || empty($plugins)) |
202 | return false; |
203 | |
204 | if ( in_array($plugin_name,$plugins) ) { |
205 | return true; |
206 | } else { |
207 | return false; |
208 | } |
209 | } |
210 | |
0606ca1f |
211 | /*************************************/ |
212 | /*** MAIN PLUGIN LOADING CODE HERE ***/ |
213 | /*************************************/ |
214 | |
215 | /* On startup, register all plugins configured for use. */ |
216 | if (isset($plugins) && is_array($plugins)) { |
217 | foreach ($plugins as $name) { |
218 | use_plugin($name); |
2d367c68 |
219 | } |
0606ca1f |
220 | } |
076c01d7 |
221 | |
91e0dccc |
222 | ?> |