7b086a80 |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * plugin.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This file provides the framework for a plugin architecture. |
10 | * |
11 | * Documentation on how to write plugins might show up some time. |
12 | * |
31841a9e |
13 | * @version $Id$ |
d6c32258 |
14 | * @package squirrelmail |
35586184 |
15 | */ |
16 | |
d6c32258 |
17 | /** Everything needs global.. */ |
cd21d1aa |
18 | require_once(SM_PATH . 'functions/global.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(); |
2586d588 |
47 | $ret = ''; |
eb1f02bc |
48 | $currentHookName = $name; |
a3439b27 |
49 | |
0606ca1f |
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)) { |
31524bcd |
55 | $function($data); |
56 | } |
57 | } |
58 | } |
59 | |
eb1f02bc |
60 | $currentHookName = ''; |
61 | |
31524bcd |
62 | /* Variable-length argument lists have a slight problem when */ |
63 | /* passing values by reference. Pity. This is a workaround. */ |
64 | return $data; |
65 | } |
66 | |
8b096f0a |
67 | /** |
68 | * This function executes a hook and allows for parameters to be passed. |
69 | * |
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 |
73 | */ |
31524bcd |
74 | function do_hook_function($name,$parm=NULL) { |
eb1f02bc |
75 | global $squirrelmail_plugin_hooks, $currentHookName; |
31524bcd |
76 | $ret = ''; |
eb1f02bc |
77 | $currentHookName = $name; |
31524bcd |
78 | |
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); |
dd389be5 |
85 | } |
2d367c68 |
86 | } |
2d367c68 |
87 | } |
7b086a80 |
88 | |
eb1f02bc |
89 | $currentHookName = ''; |
90 | |
0606ca1f |
91 | /* Variable-length argument lists have a slight problem when */ |
92 | /* passing values by reference. Pity. This is a workaround. */ |
2586d588 |
93 | return $ret; |
0606ca1f |
94 | } |
7b086a80 |
95 | |
8b096f0a |
96 | /** |
97 | * This function executes a hook, concatenating the results of each |
98 | * plugin that has the hook defined. |
99 | * |
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 |
103 | */ |
09ac2863 |
104 | function concat_hook_function($name,$parm=NULL) { |
eb1f02bc |
105 | global $squirrelmail_plugin_hooks, $currentHookName; |
09ac2863 |
106 | $ret = ''; |
eb1f02bc |
107 | $currentHookName = $name; |
31524bcd |
108 | |
09ac2863 |
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); |
115 | } |
116 | } |
117 | } |
118 | |
eb1f02bc |
119 | $currentHookName = ''; |
120 | |
09ac2863 |
121 | /* Variable-length argument lists have a slight problem when */ |
122 | /* passing values by reference. Pity. This is a workaround. */ |
123 | return $ret; |
124 | } |
cd7fc9e6 |
125 | |
5576644b |
126 | /** |
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. |
8b096f0a |
131 | * Priority 0 means majority rules. Ties will be broken with $tie |
132 | * |
133 | * @param string name the hook name |
134 | * @param mixed parm the parameters for the hook function |
135 | * @param int priority |
136 | * @param bool tie |
137 | * @return bool the result of the function |
138 | */ |
5576644b |
139 | function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) { |
eb1f02bc |
140 | global $squirrelmail_plugin_hooks, $currentHookName; |
5576644b |
141 | $yea = 0; |
142 | $nay = 0; |
143 | $ret = $tie; |
144 | |
145 | if (isset($squirrelmail_plugin_hooks[$name]) && |
146 | is_array($squirrelmail_plugin_hooks[$name])) { |
147 | |
148 | /* Loop over the plugins that registered the hook */ |
eb1f02bc |
149 | $currentHookName = $name; |
5576644b |
150 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
151 | if (function_exists($function)) { |
152 | $ret = $function($parm); |
153 | if ($ret) { |
154 | $yea++; |
155 | } else { |
156 | $nay++; |
157 | } |
158 | } |
159 | } |
eb1f02bc |
160 | $currentHookName = ''; |
5576644b |
161 | |
162 | /* Examine the aftermath and assign the return value appropriately */ |
163 | if (($priority > 0) && ($yea)) { |
164 | $ret = true; |
165 | } elseif (($priority < 0) && ($nay)) { |
166 | $ret = false; |
167 | } elseif ($yea > $nay) { |
168 | $ret = true; |
169 | } elseif ($nay > $yea) { |
170 | $ret = false; |
171 | } else { |
172 | // There's a tie, no action needed. |
173 | } |
174 | return $ret; |
175 | } |
176 | // If the code gets here, there was a problem - no hooks, etc. |
177 | return NULL; |
178 | } |
179 | |
cd7fc9e6 |
180 | /** |
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 |
183 | * offending browser. |
7349fa12 |
184 | * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT *** |
94ba79ce |
185 | * FIXME: This function needs to have its name changed! |
8b096f0a |
186 | * |
187 | * @return bool whether this browser properly supports JavaScript |
cd7fc9e6 |
188 | */ |
189 | function soupNazi(){ |
7349fa12 |
190 | return !checkForJavascript(); |
cd7fc9e6 |
191 | } |
0606ca1f |
192 | /*************************************/ |
193 | /*** MAIN PLUGIN LOADING CODE HERE ***/ |
194 | /*************************************/ |
195 | |
196 | /* On startup, register all plugins configured for use. */ |
197 | if (isset($plugins) && is_array($plugins)) { |
198 | foreach ($plugins as $name) { |
199 | use_plugin($name); |
2d367c68 |
200 | } |
0606ca1f |
201 | } |
076c01d7 |
202 | |
eb1f02bc |
203 | ?> |