Slight adjustment. Plugins create their own table row for login_form hook; core...
[squirrelmail.git] / functions / plugin.php
CommitLineData
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 &copy; 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/**
17 * This function adds a plugin.
18 * @param string $name Internal plugin name (ie. delete_move_next)
19 * @return void
20 */
0606ca1f 21function use_plugin ($name) {
bd9c880b 22 if (file_exists(SM_PATH . "plugins/$name/setup.php")) {
23 include_once(SM_PATH . "plugins/$name/setup.php");
0606ca1f 24 $function = "squirrelmail_plugin_init_$name";
25 if (function_exists($function)) {
26 $function();
2d367c68 27 }
2d367c68 28 }
0606ca1f 29}
2d367c68 30
d6c32258 31/**
32 * This function executes a hook.
33 * @param string $name Name of hook to fire
34 * @return mixed $data
35 */
31524bcd 36function do_hook ($name) {
eb1f02bc 37 global $squirrelmail_plugin_hooks, $currentHookName;
0606ca1f 38 $data = func_get_args();
eb1f02bc 39 $currentHookName = $name;
a3439b27 40
0606ca1f 41 if (isset($squirrelmail_plugin_hooks[$name])
42 && is_array($squirrelmail_plugin_hooks[$name])) {
43 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
44 /* Add something to set correct gettext domain for plugin. */
45 if (function_exists($function)) {
31524bcd 46 $function($data);
47 }
48 }
49 }
50
eb1f02bc 51 $currentHookName = '';
52
31524bcd 53 /* Variable-length argument lists have a slight problem when */
54 /* passing values by reference. Pity. This is a workaround. */
55 return $data;
56}
57
8b096f0a 58/**
59 * This function executes a hook and allows for parameters to be passed.
60 *
61 * @param string name the name of the hook
62 * @param mixed param the parameters to pass to the hook function
63 * @return mixed the return value of the hook function
64 */
31524bcd 65function do_hook_function($name,$parm=NULL) {
eb1f02bc 66 global $squirrelmail_plugin_hooks, $currentHookName;
31524bcd 67 $ret = '';
eb1f02bc 68 $currentHookName = $name;
31524bcd 69
70 if (isset($squirrelmail_plugin_hooks[$name])
71 && is_array($squirrelmail_plugin_hooks[$name])) {
72 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
73 /* Add something to set correct gettext domain for plugin. */
74 if (function_exists($function)) {
75 $ret = $function($parm);
dd389be5 76 }
2d367c68 77 }
2d367c68 78 }
7b086a80 79
eb1f02bc 80 $currentHookName = '';
81
0606ca1f 82 /* Variable-length argument lists have a slight problem when */
83 /* passing values by reference. Pity. This is a workaround. */
2586d588 84 return $ret;
0606ca1f 85}
7b086a80 86
8b096f0a 87/**
88 * This function executes a hook, concatenating the results of each
89 * plugin that has the hook defined.
90 *
91 * @param string name the name of the hook
92 * @param mixed parm optional hook function parameters
93 * @return string a concatenation of the results of each plugin function
94 */
09ac2863 95function concat_hook_function($name,$parm=NULL) {
eb1f02bc 96 global $squirrelmail_plugin_hooks, $currentHookName;
09ac2863 97 $ret = '';
eb1f02bc 98 $currentHookName = $name;
31524bcd 99
09ac2863 100 if (isset($squirrelmail_plugin_hooks[$name])
101 && is_array($squirrelmail_plugin_hooks[$name])) {
102 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
103 /* Concatenate results from hook. */
104 if (function_exists($function)) {
105 $ret .= $function($parm);
106 }
107 }
108 }
109
eb1f02bc 110 $currentHookName = '';
111
09ac2863 112 /* Variable-length argument lists have a slight problem when */
113 /* passing values by reference. Pity. This is a workaround. */
114 return $ret;
115}
cd7fc9e6 116
117/**
5576644b 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.
8b096f0a 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 */
5576644b 130function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
eb1f02bc 131 global $squirrelmail_plugin_hooks, $currentHookName;
5576644b 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 */
eb1f02bc 140 $currentHookName = $name;
5576644b 141 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
142 if (function_exists($function)) {
143 $ret = $function($parm);
144 if ($ret) {
145 $yea++;
146 } else {
147 $nay++;
148 }
149 }
150 }
eb1f02bc 151 $currentHookName = '';
5576644b 152
153 /* Examine the aftermath and assign the return value appropriately */
154 if (($priority > 0) && ($yea)) {
155 $ret = true;
156 } elseif (($priority < 0) && ($nay)) {
157 $ret = false;
158 } elseif ($yea > $nay) {
159 $ret = true;
160 } elseif ($nay > $yea) {
161 $ret = false;
162 } else {
163 // There's a tie, no action needed.
164 }
165 return $ret;
166 }
167 // If the code gets here, there was a problem - no hooks, etc.
168 return NULL;
169}
170
171/**
cd7fc9e6 172 * This function checks whether the user's USER_AGENT is known to
173 * be broken. If so, returns true and the plugin is invisible to the
174 * offending browser.
7349fa12 175 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
94ba79ce 176 * FIXME: This function needs to have its name changed!
8b096f0a 177 *
178 * @return bool whether this browser properly supports JavaScript
ccacde36 179 * @deprecated use checkForJavascript() since 1.5.1
cd7fc9e6 180 */
181function soupNazi(){
7349fa12 182 return !checkForJavascript();
cd7fc9e6 183}
fe0aa536 184
185/**
186 * Check if plugin is enabled
187 * @param string $plugin_name plugin name
188 * @since 1.5.1
189 * @return boolean
190 */
191function is_plugin_enabled($plugin_name) {
192 global $plugins;
193
e83cfcef 194 /**
202bcbcc 195 * check if variable is empty. if var is not set, php empty
e495bd68 196 * returns true without error notice.
197 *
198 * then check if it is an array
e83cfcef 199 */
e495bd68 200 if (empty($plugins) || ! is_array($plugins))
fe0aa536 201 return false;
202
203 if ( in_array($plugin_name,$plugins) ) {
204 return true;
205 } else {
206 return false;
207 }
208}