7b086a80 |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * plugin.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 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 | * |
13 | * $Id$ |
d6c32258 |
14 | * @package squirrelmail |
35586184 |
15 | */ |
16 | |
d6c32258 |
17 | /** Everything needs global.. */ |
cd21d1aa |
18 | require_once(SM_PATH . 'functions/global.php'); |
19 | |
35586184 |
20 | global $squirrelmail_plugin_hooks; |
0a17ec32 |
21 | $squirrelmail_plugin_hooks = array(); |
2d367c68 |
22 | |
d6c32258 |
23 | /** |
24 | * This function adds a plugin. |
25 | * @param string $name Internal plugin name (ie. delete_move_next) |
26 | * @return void |
27 | */ |
0606ca1f |
28 | function use_plugin ($name) { |
bd9c880b |
29 | if (file_exists(SM_PATH . "plugins/$name/setup.php")) { |
30 | include_once(SM_PATH . "plugins/$name/setup.php"); |
0606ca1f |
31 | $function = "squirrelmail_plugin_init_$name"; |
32 | if (function_exists($function)) { |
33 | $function(); |
2d367c68 |
34 | } |
2d367c68 |
35 | } |
0606ca1f |
36 | } |
2d367c68 |
37 | |
d6c32258 |
38 | /** |
39 | * This function executes a hook. |
40 | * @param string $name Name of hook to fire |
41 | * @return mixed $data |
42 | */ |
31524bcd |
43 | function do_hook ($name) { |
0606ca1f |
44 | global $squirrelmail_plugin_hooks; |
45 | $data = func_get_args(); |
2586d588 |
46 | $ret = ''; |
a3439b27 |
47 | |
0606ca1f |
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)) { |
31524bcd |
53 | $function($data); |
54 | } |
55 | } |
56 | } |
57 | |
58 | /* Variable-length argument lists have a slight problem when */ |
59 | /* passing values by reference. Pity. This is a workaround. */ |
60 | return $data; |
61 | } |
62 | |
63 | /* This function executes a hook. */ |
64 | function do_hook_function($name,$parm=NULL) { |
65 | global $squirrelmail_plugin_hooks; |
66 | $ret = ''; |
67 | |
68 | if (isset($squirrelmail_plugin_hooks[$name]) |
69 | && is_array($squirrelmail_plugin_hooks[$name])) { |
70 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
71 | /* Add something to set correct gettext domain for plugin. */ |
72 | if (function_exists($function)) { |
73 | $ret = $function($parm); |
dd389be5 |
74 | } |
2d367c68 |
75 | } |
2d367c68 |
76 | } |
7b086a80 |
77 | |
0606ca1f |
78 | /* Variable-length argument lists have a slight problem when */ |
79 | /* passing values by reference. Pity. This is a workaround. */ |
2586d588 |
80 | return $ret; |
0606ca1f |
81 | } |
7b086a80 |
82 | |
09ac2863 |
83 | /* This function executes a hook. */ |
84 | function concat_hook_function($name,$parm=NULL) { |
85 | global $squirrelmail_plugin_hooks; |
86 | $ret = ''; |
31524bcd |
87 | |
09ac2863 |
88 | if (isset($squirrelmail_plugin_hooks[$name]) |
89 | && is_array($squirrelmail_plugin_hooks[$name])) { |
90 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
91 | /* Concatenate results from hook. */ |
92 | if (function_exists($function)) { |
93 | $ret .= $function($parm); |
94 | } |
95 | } |
96 | } |
97 | |
98 | /* Variable-length argument lists have a slight problem when */ |
99 | /* passing values by reference. Pity. This is a workaround. */ |
100 | return $ret; |
101 | } |
cd7fc9e6 |
102 | |
5576644b |
103 | /** |
104 | * This function is used for hooks which are to return true or |
105 | * false. If $priority is > 0, any one or more trues will override |
106 | * any falses. If $priority < 0, then one or more falses will |
107 | * override any trues. |
108 | * Priority 0 means majority rules. Ties will be broken with $tie */ |
109 | function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) { |
110 | global $squirrelmail_plugin_hooks; |
111 | $yea = 0; |
112 | $nay = 0; |
113 | $ret = $tie; |
114 | |
115 | if (isset($squirrelmail_plugin_hooks[$name]) && |
116 | is_array($squirrelmail_plugin_hooks[$name])) { |
117 | |
118 | /* Loop over the plugins that registered the hook */ |
119 | foreach ($squirrelmail_plugin_hooks[$name] as $function) { |
120 | if (function_exists($function)) { |
121 | $ret = $function($parm); |
122 | if ($ret) { |
123 | $yea++; |
124 | } else { |
125 | $nay++; |
126 | } |
127 | } |
128 | } |
129 | |
130 | /* Examine the aftermath and assign the return value appropriately */ |
131 | if (($priority > 0) && ($yea)) { |
132 | $ret = true; |
133 | } elseif (($priority < 0) && ($nay)) { |
134 | $ret = false; |
135 | } elseif ($yea > $nay) { |
136 | $ret = true; |
137 | } elseif ($nay > $yea) { |
138 | $ret = false; |
139 | } else { |
140 | // There's a tie, no action needed. |
141 | } |
142 | return $ret; |
143 | } |
144 | // If the code gets here, there was a problem - no hooks, etc. |
145 | return NULL; |
146 | } |
147 | |
cd7fc9e6 |
148 | /** |
149 | * This function checks whether the user's USER_AGENT is known to |
150 | * be broken. If so, returns true and the plugin is invisible to the |
151 | * offending browser. |
152 | */ |
153 | function soupNazi(){ |
154 | |
cd7fc9e6 |
155 | $soup_menu = array('Mozilla/3','Mozilla/2','Mozilla/1', 'Opera 4', |
156 | 'Opera/4', 'OmniWeb', 'Lynx'); |
961ca3d8 |
157 | sqgetGlobalVar('HTTP_USER_AGENT', $user_agent, SQ_SERVER); |
cd7fc9e6 |
158 | foreach($soup_menu as $browser) { |
961ca3d8 |
159 | if(stristr($user_agent, $browser)) { |
cd7fc9e6 |
160 | return 1; |
161 | } |
162 | } |
163 | return 0; |
164 | } |
0606ca1f |
165 | /*************************************/ |
166 | /*** MAIN PLUGIN LOADING CODE HERE ***/ |
167 | /*************************************/ |
168 | |
169 | /* On startup, register all plugins configured for use. */ |
170 | if (isset($plugins) && is_array($plugins)) { |
171 | foreach ($plugins as $name) { |
172 | use_plugin($name); |
2d367c68 |
173 | } |
0606ca1f |
174 | } |
076c01d7 |
175 | |
b68edc75 |
176 | ?> |