5be3a953b8bd85f7ff7ebb419175e8c7a8d24a45
[squirrelmail.git] / functions / plugin.php
1 <?php
2
3 /**
4 * plugin.php
5 *
6 * This file provides the framework for a plugin architecture.
7 *
8 * Documentation on how to write plugins might show up some time.
9 *
10 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
14 */
15
16 /** Everything needs global.. */
17 require_once(SM_PATH . 'functions/global.php');
18 require_once(SM_PATH . 'functions/prefs.php');
19
20 global $squirrelmail_plugin_hooks;
21 $squirrelmail_plugin_hooks = array();
22
23 /**
24 * This function adds a plugin.
25 * @param string $name Internal plugin name (ie. delete_move_next)
26 * @return void
27 */
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)) {
33 $function();
34 }
35 }
36 }
37
38 /**
39 * This function executes a hook.
40 * @param string $name Name of hook to fire
41 * @return mixed $data
42 */
43 function do_hook ($name) {
44 global $squirrelmail_plugin_hooks, $currentHookName;
45 $data = func_get_args();
46 $currentHookName = $name;
47
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)) {
53 $function($data);
54 }
55 }
56 }
57
58 $currentHookName = '';
59
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
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 */
72 function do_hook_function($name,$parm=NULL) {
73 global $squirrelmail_plugin_hooks, $currentHookName;
74 $ret = '';
75 $currentHookName = $name;
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);
83 }
84 }
85 }
86
87 $currentHookName = '';
88
89 /* Variable-length argument lists have a slight problem when */
90 /* passing values by reference. Pity. This is a workaround. */
91 return $ret;
92 }
93
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 */
102 function concat_hook_function($name,$parm=NULL) {
103 global $squirrelmail_plugin_hooks, $currentHookName;
104 $ret = '';
105 $currentHookName = $name;
106
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
117 $currentHookName = '';
118
119 /* Variable-length argument lists have a slight problem when */
120 /* passing values by reference. Pity. This is a workaround. */
121 return $ret;
122 }
123
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.
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 */
137 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
138 global $squirrelmail_plugin_hooks, $currentHookName;
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 */
147 $currentHookName = $name;
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 }
158 $currentHookName = '';
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
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.
182 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
183 * FIXME: This function needs to have its name changed!
184 *
185 * @return bool whether this browser properly supports JavaScript
186 * @deprecated use checkForJavascript() since 1.5.1
187 */
188 function soupNazi(){
189 return !checkForJavascript();
190 }
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 /**
202 * check if variable is empty. if var is not set, php empty
203 * returns true without error notice.
204 *
205 * then check if it is an array
206 */
207 if (empty($plugins) || ! is_array($plugins))
208 return false;
209
210 if ( in_array($plugin_name,$plugins) ) {
211 return true;
212 } else {
213 return false;
214 }
215 }
216
217 /*************************************/
218 /*** MAIN PLUGIN LOADING CODE HERE ***/
219 /*************************************/
220
221 /* On startup, register all plugins configured for use. */
222 if (isset($plugins) && is_array($plugins)) {
223 // turn on output buffering in order to prevent output of new lines
224 ob_start();
225 foreach ($plugins as $name) {
226 use_plugin($name);
227 }
228 // get output and remove whitespace
229 $output = trim(ob_get_contents());
230 ob_end_clean();
231 // if plugins output more than newlines and spacing, stop script execution.
232 if (!empty($output)) {
233 die($output);
234 }
235 }
236
237 ?>