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