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 | * |
47ccfad4 |
10 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
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'); |
78153530 |
18 | require_once(SM_PATH . 'config/config.php'); |
7349fa12 |
19 | require_once(SM_PATH . 'functions/prefs.php'); |
cd21d1aa |
20 | |
35586184 |
21 | global $squirrelmail_plugin_hooks; |
0a17ec32 |
22 | $squirrelmail_plugin_hooks = array(); |
2d367c68 |
23 | |
d6c32258 |
24 | /** |
25 | * This function adds a plugin. |
26 | * @param string $name Internal plugin name (ie. delete_move_next) |
27 | * @return void |
28 | */ |
0606ca1f |
29 | function use_plugin ($name) { |
bd9c880b |
30 | if (file_exists(SM_PATH . "plugins/$name/setup.php")) { |
31 | include_once(SM_PATH . "plugins/$name/setup.php"); |
0606ca1f |
32 | $function = "squirrelmail_plugin_init_$name"; |
33 | if (function_exists($function)) { |
34 | $function(); |
2d367c68 |
35 | } |
2d367c68 |
36 | } |
0606ca1f |
37 | } |
2d367c68 |
38 | |
d6c32258 |
39 | /** |
40 | * This function executes a hook. |
41 | * @param string $name Name of hook to fire |
42 | * @return mixed $data |
43 | */ |
31524bcd |
44 | function do_hook ($name) { |
eb1f02bc |
45 | global $squirrelmail_plugin_hooks, $currentHookName; |
0606ca1f |
46 | $data = func_get_args(); |
eb1f02bc |
47 | $currentHookName = $name; |
a3439b27 |
48 | |
0606ca1f |
49 | if (isset($squirrelmail_plugin_hooks[$name]) |
50 | && is_array($squirrelmail_plugin_hooks[$name])) { |
51 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
52 | /* Add something to set correct gettext domain for plugin. */ |
53 | if (function_exists($function)) { |
31524bcd |
54 | $function($data); |
55 | } |
56 | } |
57 | } |
58 | |
eb1f02bc |
59 | $currentHookName = ''; |
60 | |
31524bcd |
61 | /* Variable-length argument lists have a slight problem when */ |
62 | /* passing values by reference. Pity. This is a workaround. */ |
63 | return $data; |
64 | } |
65 | |
8b096f0a |
66 | /** |
67 | * This function executes a hook and allows for parameters to be passed. |
68 | * |
69 | * @param string name the name of the hook |
70 | * @param mixed param the parameters to pass to the hook function |
71 | * @return mixed the return value of the hook function |
72 | */ |
31524bcd |
73 | function do_hook_function($name,$parm=NULL) { |
eb1f02bc |
74 | global $squirrelmail_plugin_hooks, $currentHookName; |
31524bcd |
75 | $ret = ''; |
eb1f02bc |
76 | $currentHookName = $name; |
31524bcd |
77 | |
78 | if (isset($squirrelmail_plugin_hooks[$name]) |
79 | && is_array($squirrelmail_plugin_hooks[$name])) { |
80 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
81 | /* Add something to set correct gettext domain for plugin. */ |
82 | if (function_exists($function)) { |
83 | $ret = $function($parm); |
dd389be5 |
84 | } |
2d367c68 |
85 | } |
2d367c68 |
86 | } |
7b086a80 |
87 | |
eb1f02bc |
88 | $currentHookName = ''; |
89 | |
0606ca1f |
90 | /* Variable-length argument lists have a slight problem when */ |
91 | /* passing values by reference. Pity. This is a workaround. */ |
2586d588 |
92 | return $ret; |
0606ca1f |
93 | } |
7b086a80 |
94 | |
8b096f0a |
95 | /** |
96 | * This function executes a hook, concatenating the results of each |
97 | * plugin that has the hook defined. |
98 | * |
99 | * @param string name the name of the hook |
100 | * @param mixed parm optional hook function parameters |
101 | * @return string a concatenation of the results of each plugin function |
102 | */ |
09ac2863 |
103 | function concat_hook_function($name,$parm=NULL) { |
eb1f02bc |
104 | global $squirrelmail_plugin_hooks, $currentHookName; |
09ac2863 |
105 | $ret = ''; |
eb1f02bc |
106 | $currentHookName = $name; |
31524bcd |
107 | |
09ac2863 |
108 | if (isset($squirrelmail_plugin_hooks[$name]) |
109 | && is_array($squirrelmail_plugin_hooks[$name])) { |
110 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
111 | /* Concatenate results from hook. */ |
112 | if (function_exists($function)) { |
113 | $ret .= $function($parm); |
114 | } |
115 | } |
116 | } |
117 | |
eb1f02bc |
118 | $currentHookName = ''; |
119 | |
09ac2863 |
120 | /* Variable-length argument lists have a slight problem when */ |
121 | /* passing values by reference. Pity. This is a workaround. */ |
122 | return $ret; |
123 | } |
cd7fc9e6 |
124 | |
5576644b |
125 | /** |
126 | * This function is used for hooks which are to return true or |
127 | * false. If $priority is > 0, any one or more trues will override |
128 | * any falses. If $priority < 0, then one or more falses will |
129 | * override any trues. |
8b096f0a |
130 | * Priority 0 means majority rules. Ties will be broken with $tie |
131 | * |
132 | * @param string name the hook name |
133 | * @param mixed parm the parameters for the hook function |
134 | * @param int priority |
135 | * @param bool tie |
136 | * @return bool the result of the function |
137 | */ |
5576644b |
138 | function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) { |
eb1f02bc |
139 | global $squirrelmail_plugin_hooks, $currentHookName; |
5576644b |
140 | $yea = 0; |
141 | $nay = 0; |
142 | $ret = $tie; |
143 | |
144 | if (isset($squirrelmail_plugin_hooks[$name]) && |
145 | is_array($squirrelmail_plugin_hooks[$name])) { |
146 | |
147 | /* Loop over the plugins that registered the hook */ |
eb1f02bc |
148 | $currentHookName = $name; |
5576644b |
149 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
150 | if (function_exists($function)) { |
151 | $ret = $function($parm); |
152 | if ($ret) { |
153 | $yea++; |
154 | } else { |
155 | $nay++; |
156 | } |
157 | } |
158 | } |
eb1f02bc |
159 | $currentHookName = ''; |
5576644b |
160 | |
161 | /* Examine the aftermath and assign the return value appropriately */ |
162 | if (($priority > 0) && ($yea)) { |
163 | $ret = true; |
164 | } elseif (($priority < 0) && ($nay)) { |
165 | $ret = false; |
166 | } elseif ($yea > $nay) { |
167 | $ret = true; |
168 | } elseif ($nay > $yea) { |
169 | $ret = false; |
170 | } else { |
171 | // There's a tie, no action needed. |
172 | } |
173 | return $ret; |
174 | } |
175 | // If the code gets here, there was a problem - no hooks, etc. |
176 | return NULL; |
177 | } |
178 | |
cd7fc9e6 |
179 | /** |
180 | * This function checks whether the user's USER_AGENT is known to |
181 | * be broken. If so, returns true and the plugin is invisible to the |
182 | * offending browser. |
7349fa12 |
183 | * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT *** |
94ba79ce |
184 | * FIXME: This function needs to have its name changed! |
8b096f0a |
185 | * |
186 | * @return bool whether this browser properly supports JavaScript |
ccacde36 |
187 | * @deprecated use checkForJavascript() since 1.5.1 |
cd7fc9e6 |
188 | */ |
189 | function soupNazi(){ |
7349fa12 |
190 | return !checkForJavascript(); |
cd7fc9e6 |
191 | } |
fe0aa536 |
192 | |
193 | /** |
194 | * Check if plugin is enabled |
195 | * @param string $plugin_name plugin name |
196 | * @since 1.5.1 |
197 | * @return boolean |
198 | */ |
199 | function is_plugin_enabled($plugin_name) { |
200 | global $plugins; |
201 | |
e83cfcef |
202 | /** |
e495bd68 |
203 | * check if variable is empty. if var is not set, php empty |
204 | * returns true without error notice. |
205 | * |
206 | * then check if it is an array |
e83cfcef |
207 | */ |
e495bd68 |
208 | if (empty($plugins) || ! is_array($plugins)) |
fe0aa536 |
209 | return false; |
210 | |
211 | if ( in_array($plugin_name,$plugins) ) { |
212 | return true; |
213 | } else { |
214 | return false; |
215 | } |
216 | } |
217 | |
0606ca1f |
218 | /*************************************/ |
219 | /*** MAIN PLUGIN LOADING CODE HERE ***/ |
220 | /*************************************/ |
221 | |
222 | /* On startup, register all plugins configured for use. */ |
223 | if (isset($plugins) && is_array($plugins)) { |
517f5099 |
224 | // turn on output buffering in order to prevent output of new lines |
225 | ob_start(); |
0606ca1f |
226 | foreach ($plugins as $name) { |
227 | use_plugin($name); |
2d367c68 |
228 | } |
517f5099 |
229 | // get output and remove whitespace |
230 | $output = trim(ob_get_contents()); |
231 | ob_end_clean(); |
232 | // if plugins output more than newlines and spacing, stop script execution. |
233 | if (!empty($output)) { |
234 | die($output); |
235 | } |
0606ca1f |
236 | } |
076c01d7 |
237 | |
78153530 |
238 | ?> |