stop loading functions in setup.php
[squirrelmail.git] / functions / plugin.php
CommitLineData
7b086a80 1<?php
2
35586184 3/**
4 * plugin.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 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 18require_once(SM_PATH . 'functions/global.php');
7349fa12 19require_once(SM_PATH . 'functions/prefs.php');
cd21d1aa 20
35586184 21global $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 29function 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 44function do_hook ($name) {
eb1f02bc 45 global $squirrelmail_plugin_hooks, $currentHookName;
0606ca1f 46 $data = func_get_args();
eb1f02bc 47 $currentHookName = $name;
a3439b27 48
0606ca1f 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)) {
31524bcd 54 $function($data);
55 }
56 }
57 }
58
eb1f02bc 59 $currentHookName = '';
60
31524bcd 61 /* Variable-length argument lists have a slight problem when */
62 /* passing values by reference. Pity. This is a workaround. */
63 return $data;
64}
65
8b096f0a 66/**
67 * This function executes a hook and allows for parameters to be passed.
68 *
69 * @param string name the name of the hook
70 * @param mixed param the parameters to pass to the hook function
71 * @return mixed the return value of the hook function
72 */
31524bcd 73function do_hook_function($name,$parm=NULL) {
eb1f02bc 74 global $squirrelmail_plugin_hooks, $currentHookName;
31524bcd 75 $ret = '';
eb1f02bc 76 $currentHookName = $name;
31524bcd 77
78 if (isset($squirrelmail_plugin_hooks[$name])
79 && is_array($squirrelmail_plugin_hooks[$name])) {
80 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
81 /* Add something to set correct gettext domain for plugin. */
82 if (function_exists($function)) {
83 $ret = $function($parm);
dd389be5 84 }
2d367c68 85 }
2d367c68 86 }
7b086a80 87
eb1f02bc 88 $currentHookName = '';
89
0606ca1f 90 /* Variable-length argument lists have a slight problem when */
91 /* passing values by reference. Pity. This is a workaround. */
2586d588 92 return $ret;
0606ca1f 93}
7b086a80 94
8b096f0a 95/**
96 * This function executes a hook, concatenating the results of each
97 * plugin that has the hook defined.
98 *
99 * @param string name the name of the hook
100 * @param mixed parm optional hook function parameters
101 * @return string a concatenation of the results of each plugin function
102 */
09ac2863 103function concat_hook_function($name,$parm=NULL) {
eb1f02bc 104 global $squirrelmail_plugin_hooks, $currentHookName;
09ac2863 105 $ret = '';
eb1f02bc 106 $currentHookName = $name;
31524bcd 107
09ac2863 108 if (isset($squirrelmail_plugin_hooks[$name])
109 && is_array($squirrelmail_plugin_hooks[$name])) {
110 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
111 /* Concatenate results from hook. */
112 if (function_exists($function)) {
113 $ret .= $function($parm);
114 }
115 }
116 }
117
eb1f02bc 118 $currentHookName = '';
119
09ac2863 120 /* Variable-length argument lists have a slight problem when */
121 /* passing values by reference. Pity. This is a workaround. */
122 return $ret;
123}
cd7fc9e6 124
125/**
5576644b 126 * This function is used for hooks which are to return true or
127 * false. If $priority is > 0, any one or more trues will override
128 * any falses. If $priority < 0, then one or more falses will
129 * override any trues.
8b096f0a 130 * Priority 0 means majority rules. Ties will be broken with $tie
131 *
132 * @param string name the hook name
133 * @param mixed parm the parameters for the hook function
134 * @param int priority
135 * @param bool tie
136 * @return bool the result of the function
137 */
5576644b 138function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
eb1f02bc 139 global $squirrelmail_plugin_hooks, $currentHookName;
5576644b 140 $yea = 0;
141 $nay = 0;
142 $ret = $tie;
143
144 if (isset($squirrelmail_plugin_hooks[$name]) &&
145 is_array($squirrelmail_plugin_hooks[$name])) {
146
147 /* Loop over the plugins that registered the hook */
eb1f02bc 148 $currentHookName = $name;
5576644b 149 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
150 if (function_exists($function)) {
151 $ret = $function($parm);
152 if ($ret) {
153 $yea++;
154 } else {
155 $nay++;
156 }
157 }
158 }
eb1f02bc 159 $currentHookName = '';
5576644b 160
161 /* Examine the aftermath and assign the return value appropriately */
162 if (($priority > 0) && ($yea)) {
163 $ret = true;
164 } elseif (($priority < 0) && ($nay)) {
165 $ret = false;
166 } elseif ($yea > $nay) {
167 $ret = true;
168 } elseif ($nay > $yea) {
169 $ret = false;
170 } else {
171 // There's a tie, no action needed.
172 }
173 return $ret;
174 }
175 // If the code gets here, there was a problem - no hooks, etc.
176 return NULL;
177}
178
179/**
cd7fc9e6 180 * This function checks whether the user's USER_AGENT is known to
181 * be broken. If so, returns true and the plugin is invisible to the
182 * offending browser.
7349fa12 183 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
94ba79ce 184 * FIXME: This function needs to have its name changed!
8b096f0a 185 *
186 * @return bool whether this browser properly supports JavaScript
ccacde36 187 * @deprecated use checkForJavascript() since 1.5.1
cd7fc9e6 188 */
189function 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. */
197if (isset($plugins) && is_array($plugins)) {
198 foreach ($plugins as $name) {
199 use_plugin($name);
2d367c68 200 }
0606ca1f 201}
076c01d7 202
91e0dccc 203?>