We're living in 2004 now... perl is your friend for these kinds of things :)
[squirrelmail.git] / functions / plugin.php
CommitLineData
7b086a80 1<?php
2
35586184 3/**
4 * plugin.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 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 18require_once(SM_PATH . 'functions/global.php');
19
35586184 20global $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 28function 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 43function 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
8b096f0a 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 */
31524bcd 70function 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);
dd389be5 80 }
2d367c68 81 }
2d367c68 82 }
7b086a80 83
0606ca1f 84 /* Variable-length argument lists have a slight problem when */
85 /* passing values by reference. Pity. This is a workaround. */
2586d588 86 return $ret;
0606ca1f 87}
7b086a80 88
8b096f0a 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 */
09ac2863 97function concat_hook_function($name,$parm=NULL) {
98 global $squirrelmail_plugin_hooks;
99 $ret = '';
31524bcd 100
09ac2863 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}
cd7fc9e6 115
5576644b 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.
8b096f0a 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 */
5576644b 129function 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
cd7fc9e6 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.
8b096f0a 172 * This function needs to have its name changed!
173 *
174 * @return bool whether this browser properly supports JavaScript
cd7fc9e6 175 */
176function soupNazi(){
177
cd7fc9e6 178 $soup_menu = array('Mozilla/3','Mozilla/2','Mozilla/1', 'Opera 4',
179 'Opera/4', 'OmniWeb', 'Lynx');
961ca3d8 180 sqgetGlobalVar('HTTP_USER_AGENT', $user_agent, SQ_SERVER);
cd7fc9e6 181 foreach($soup_menu as $browser) {
961ca3d8 182 if(stristr($user_agent, $browser)) {
cd7fc9e6 183 return 1;
184 }
185 }
186 return 0;
187}
0606ca1f 188/*************************************/
189/*** MAIN PLUGIN LOADING CODE HERE ***/
190/*************************************/
191
192/* On startup, register all plugins configured for use. */
193if (isset($plugins) && is_array($plugins)) {
194 foreach ($plugins as $name) {
195 use_plugin($name);
2d367c68 196 }
0606ca1f 197}
076c01d7 198
b68edc75 199?>