Minor cleanup in comments
[squirrelmail.git] / functions / plugin.php
1 <?php
2
3 /**
4 * plugin.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
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 *
13 * @version $Id$
14 * @package squirrelmail
15 */
16
17 /** Everything needs global.. */
18 require_once(SM_PATH . 'functions/global.php');
19 require_once(SM_PATH . 'functions/prefs.php');
20
21 global $squirrelmail_plugin_hooks;
22 $squirrelmail_plugin_hooks = array();
23
24 /**
25 * This function adds a plugin.
26 * @param string $name Internal plugin name (ie. delete_move_next)
27 * @return void
28 */
29 function use_plugin ($name) {
30 if (file_exists(SM_PATH . "plugins/$name/setup.php")) {
31 include_once(SM_PATH . "plugins/$name/setup.php");
32 $function = "squirrelmail_plugin_init_$name";
33 if (function_exists($function)) {
34 $function();
35 }
36 }
37 }
38
39 /**
40 * This function executes a hook.
41 * @param string $name Name of hook to fire
42 * @return mixed $data
43 */
44 function do_hook ($name) {
45 global $squirrelmail_plugin_hooks, $currentHookName;
46 $data = func_get_args();
47 $ret = '';
48 $currentHookName = $name;
49
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)) {
55 $function($data);
56 }
57 }
58 }
59
60 $currentHookName = '';
61
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
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 */
74 function do_hook_function($name,$parm=NULL) {
75 global $squirrelmail_plugin_hooks, $currentHookName;
76 $ret = '';
77 $currentHookName = $name;
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);
85 }
86 }
87 }
88
89 $currentHookName = '';
90
91 /* Variable-length argument lists have a slight problem when */
92 /* passing values by reference. Pity. This is a workaround. */
93 return $ret;
94 }
95
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 */
104 function concat_hook_function($name,$parm=NULL) {
105 global $squirrelmail_plugin_hooks, $currentHookName;
106 $ret = '';
107 $currentHookName = $name;
108
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
119 $currentHookName = '';
120
121 /* Variable-length argument lists have a slight problem when */
122 /* passing values by reference. Pity. This is a workaround. */
123 return $ret;
124 }
125
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.
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 */
139 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
140 global $squirrelmail_plugin_hooks, $currentHookName;
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 */
149 $currentHookName = $name;
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 }
160 $currentHookName = '';
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
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.
184 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
185 * FIXME: This function needs to have its name changed!
186 *
187 * @return bool whether this browser properly supports JavaScript
188 */
189 function soupNazi(){
190 return !checkForJavascript();
191 }
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);
200 }
201 }
202
203 ?>