extra quote removed
[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;
46 $data = func_get_args();
47 $ret = '';
48
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)) {
54 $function($data);
55 }
56 }
57 }
58
59 /* Variable-length argument lists have a slight problem when */
60 /* passing values by reference. Pity. This is a workaround. */
61 return $data;
62 }
63
64 /**
65 * This function executes a hook and allows for parameters to be passed.
66 *
67 * @param string name the name of the hook
68 * @param mixed param the parameters to pass to the hook function
69 * @return mixed the return value of the hook function
70 */
71 function do_hook_function($name,$parm=NULL) {
72 global $squirrelmail_plugin_hooks;
73 $ret = '';
74
75 if (isset($squirrelmail_plugin_hooks[$name])
76 && is_array($squirrelmail_plugin_hooks[$name])) {
77 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
78 /* Add something to set correct gettext domain for plugin. */
79 if (function_exists($function)) {
80 $ret = $function($parm);
81 }
82 }
83 }
84
85 /* Variable-length argument lists have a slight problem when */
86 /* passing values by reference. Pity. This is a workaround. */
87 return $ret;
88 }
89
90 /**
91 * This function executes a hook, concatenating the results of each
92 * plugin that has the hook defined.
93 *
94 * @param string name the name of the hook
95 * @param mixed parm optional hook function parameters
96 * @return string a concatenation of the results of each plugin function
97 */
98 function concat_hook_function($name,$parm=NULL) {
99 global $squirrelmail_plugin_hooks;
100 $ret = '';
101
102 if (isset($squirrelmail_plugin_hooks[$name])
103 && is_array($squirrelmail_plugin_hooks[$name])) {
104 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
105 /* Concatenate results from hook. */
106 if (function_exists($function)) {
107 $ret .= $function($parm);
108 }
109 }
110 }
111
112 /* Variable-length argument lists have a slight problem when */
113 /* passing values by reference. Pity. This is a workaround. */
114 return $ret;
115 }
116
117 /**
118 * This function is used for hooks which are to return true or
119 * false. If $priority is > 0, any one or more trues will override
120 * any falses. If $priority < 0, then one or more falses will
121 * override any trues.
122 * Priority 0 means majority rules. Ties will be broken with $tie
123 *
124 * @param string name the hook name
125 * @param mixed parm the parameters for the hook function
126 * @param int priority
127 * @param bool tie
128 * @return bool the result of the function
129 */
130 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
131 global $squirrelmail_plugin_hooks;
132 $yea = 0;
133 $nay = 0;
134 $ret = $tie;
135
136 if (isset($squirrelmail_plugin_hooks[$name]) &&
137 is_array($squirrelmail_plugin_hooks[$name])) {
138
139 /* Loop over the plugins that registered the hook */
140 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
141 if (function_exists($function)) {
142 $ret = $function($parm);
143 if ($ret) {
144 $yea++;
145 } else {
146 $nay++;
147 }
148 }
149 }
150
151 /* Examine the aftermath and assign the return value appropriately */
152 if (($priority > 0) && ($yea)) {
153 $ret = true;
154 } elseif (($priority < 0) && ($nay)) {
155 $ret = false;
156 } elseif ($yea > $nay) {
157 $ret = true;
158 } elseif ($nay > $yea) {
159 $ret = false;
160 } else {
161 // There's a tie, no action needed.
162 }
163 return $ret;
164 }
165 // If the code gets here, there was a problem - no hooks, etc.
166 return NULL;
167 }
168
169 /**
170 * This function checks whether the user's USER_AGENT is known to
171 * be broken. If so, returns true and the plugin is invisible to the
172 * offending browser.
173 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
174 * FIXME: This function needs to have its name changed!
175 *
176 * @return bool whether this browser properly supports JavaScript
177 */
178 function soupNazi(){
179 return !checkForJavascript();
180 }
181 /*************************************/
182 /*** MAIN PLUGIN LOADING CODE HERE ***/
183 /*************************************/
184
185 /* On startup, register all plugins configured for use. */
186 if (isset($plugins) && is_array($plugins)) {
187 foreach ($plugins as $name) {
188 use_plugin($name);
189 }
190 }
191
192 ?>