Makes managing decoding easier. Mac decoding moved to separate cvs module
[squirrelmail.git] / functions / plugin.php
1 <?php
2
3 /**
4 * plugin.php
5 *
6 * Copyright (c) 1999-2003 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 * $Id$
14 * @package squirrelmail
15 */
16
17 /** Everything needs global.. */
18 require_once(SM_PATH . 'functions/global.php');
19
20 global $squirrelmail_plugin_hooks;
21 $squirrelmail_plugin_hooks = array();
22
23 /**
24 * This function adds a plugin.
25 * @param string $name Internal plugin name (ie. delete_move_next)
26 * @return void
27 */
28 function use_plugin ($name) {
29 if (file_exists(SM_PATH . "plugins/$name/setup.php")) {
30 include_once(SM_PATH . "plugins/$name/setup.php");
31 $function = "squirrelmail_plugin_init_$name";
32 if (function_exists($function)) {
33 $function();
34 }
35 }
36 }
37
38 /**
39 * This function executes a hook.
40 * @param string $name Name of hook to fire
41 * @return mixed $data
42 */
43 function do_hook ($name) {
44 global $squirrelmail_plugin_hooks;
45 $data = func_get_args();
46 $ret = '';
47
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)) {
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 /**
64 * This function executes a hook and allows for parameters to be passed.
65 *
66 * @param string name the name of the hook
67 * @param mixed param the parameters to pass to the hook function
68 * @return mixed the return value of the hook function
69 */
70 function do_hook_function($name,$parm=NULL) {
71 global $squirrelmail_plugin_hooks;
72 $ret = '';
73
74 if (isset($squirrelmail_plugin_hooks[$name])
75 && is_array($squirrelmail_plugin_hooks[$name])) {
76 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
77 /* Add something to set correct gettext domain for plugin. */
78 if (function_exists($function)) {
79 $ret = $function($parm);
80 }
81 }
82 }
83
84 /* Variable-length argument lists have a slight problem when */
85 /* passing values by reference. Pity. This is a workaround. */
86 return $ret;
87 }
88
89 /**
90 * This function executes a hook, concatenating the results of each
91 * plugin that has the hook defined.
92 *
93 * @param string name the name of the hook
94 * @param mixed parm optional hook function parameters
95 * @return string a concatenation of the results of each plugin function
96 */
97 function concat_hook_function($name,$parm=NULL) {
98 global $squirrelmail_plugin_hooks;
99 $ret = '';
100
101 if (isset($squirrelmail_plugin_hooks[$name])
102 && is_array($squirrelmail_plugin_hooks[$name])) {
103 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
104 /* Concatenate results from hook. */
105 if (function_exists($function)) {
106 $ret .= $function($parm);
107 }
108 }
109 }
110
111 /* Variable-length argument lists have a slight problem when */
112 /* passing values by reference. Pity. This is a workaround. */
113 return $ret;
114 }
115
116 /**
117 * This function is used for hooks which are to return true or
118 * false. If $priority is > 0, any one or more trues will override
119 * any falses. If $priority < 0, then one or more falses will
120 * override any trues.
121 * Priority 0 means majority rules. Ties will be broken with $tie
122 *
123 * @param string name the hook name
124 * @param mixed parm the parameters for the hook function
125 * @param int priority
126 * @param bool tie
127 * @return bool the result of the function
128 */
129 function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
130 global $squirrelmail_plugin_hooks;
131 $yea = 0;
132 $nay = 0;
133 $ret = $tie;
134
135 if (isset($squirrelmail_plugin_hooks[$name]) &&
136 is_array($squirrelmail_plugin_hooks[$name])) {
137
138 /* Loop over the plugins that registered the hook */
139 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
140 if (function_exists($function)) {
141 $ret = $function($parm);
142 if ($ret) {
143 $yea++;
144 } else {
145 $nay++;
146 }
147 }
148 }
149
150 /* Examine the aftermath and assign the return value appropriately */
151 if (($priority > 0) && ($yea)) {
152 $ret = true;
153 } elseif (($priority < 0) && ($nay)) {
154 $ret = false;
155 } elseif ($yea > $nay) {
156 $ret = true;
157 } elseif ($nay > $yea) {
158 $ret = false;
159 } else {
160 // There's a tie, no action needed.
161 }
162 return $ret;
163 }
164 // If the code gets here, there was a problem - no hooks, etc.
165 return NULL;
166 }
167
168 /**
169 * This function checks whether the user's USER_AGENT is known to
170 * be broken. If so, returns true and the plugin is invisible to the
171 * offending browser.
172 * This function needs to have its name changed!
173 *
174 * @return bool whether this browser properly supports JavaScript
175 */
176 function soupNazi(){
177
178 $soup_menu = array('Mozilla/3','Mozilla/2','Mozilla/1', 'Opera 4',
179 'Opera/4', 'OmniWeb', 'Lynx');
180 sqgetGlobalVar('HTTP_USER_AGENT', $user_agent, SQ_SERVER);
181 foreach($soup_menu as $browser) {
182 if(stristr($user_agent, $browser)) {
183 return 1;
184 }
185 }
186 return 0;
187 }
188 /*************************************/
189 /*** MAIN PLUGIN LOADING CODE HERE ***/
190 /*************************************/
191
192 /* On startup, register all plugins configured for use. */
193 if (isset($plugins) && is_array($plugins)) {
194 foreach ($plugins as $name) {
195 use_plugin($name);
196 }
197 }
198
199 ?>