56315dc41d9cff76a791a4c8e5f0d01d0cc6105c
[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 /* 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);
74 }
75 }
76 }
77
78 /* Variable-length argument lists have a slight problem when */
79 /* passing values by reference. Pity. This is a workaround. */
80 return $ret;
81 }
82
83 /* This function executes a hook. */
84 function concat_hook_function($name,$parm=NULL) {
85 global $squirrelmail_plugin_hooks;
86 $ret = '';
87
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 }
102
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
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
155 $soup_menu = array('Mozilla/3','Mozilla/2','Mozilla/1', 'Opera 4',
156 'Opera/4', 'OmniWeb', 'Lynx');
157 sqgetGlobalVar('HTTP_USER_AGENT', $user_agent, SQ_SERVER);
158 foreach($soup_menu as $browser) {
159 if(stristr($user_agent, $browser)) {
160 return 1;
161 }
162 }
163 return 0;
164 }
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);
173 }
174 }
175
176 ?>