Fixed SM version detection bug - wasn't going to hit us until v2.0, but anyway...
[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 *
31841a9e 13 * @version $Id$
d6c32258 14 * @package squirrelmail
35586184 15 */
16
d6c32258 17/** Everything needs global.. */
cd21d1aa 18require_once(SM_PATH . 'functions/global.php');
7349fa12 19require_once(SM_PATH . 'functions/prefs.php');
cd21d1aa 20
35586184 21global $squirrelmail_plugin_hooks;
0a17ec32 22$squirrelmail_plugin_hooks = array();
2d367c68 23
d6c32258 24/**
25 * This function adds a plugin.
26 * @param string $name Internal plugin name (ie. delete_move_next)
27 * @return void
28 */
0606ca1f 29function use_plugin ($name) {
bd9c880b 30 if (file_exists(SM_PATH . "plugins/$name/setup.php")) {
31 include_once(SM_PATH . "plugins/$name/setup.php");
0606ca1f 32 $function = "squirrelmail_plugin_init_$name";
33 if (function_exists($function)) {
34 $function();
2d367c68 35 }
2d367c68 36 }
0606ca1f 37}
2d367c68 38
d6c32258 39/**
40 * This function executes a hook.
41 * @param string $name Name of hook to fire
42 * @return mixed $data
43 */
31524bcd 44function do_hook ($name) {
0606ca1f 45 global $squirrelmail_plugin_hooks;
46 $data = func_get_args();
2586d588 47 $ret = '';
a3439b27 48
0606ca1f 49 if (isset($squirrelmail_plugin_hooks[$name])
50 && is_array($squirrelmail_plugin_hooks[$name])) {
51 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
52 /* Add something to set correct gettext domain for plugin. */
53 if (function_exists($function)) {
31524bcd 54 $function($data);
55 }
56 }
57 }
58
59 /* Variable-length argument lists have a slight problem when */
60 /* passing values by reference. Pity. This is a workaround. */
61 return $data;
62}
63
8b096f0a 64/**
65 * This function executes a hook and allows for parameters to be passed.
66 *
67 * @param string name the name of the hook
68 * @param mixed param the parameters to pass to the hook function
69 * @return mixed the return value of the hook function
70 */
31524bcd 71function do_hook_function($name,$parm=NULL) {
72 global $squirrelmail_plugin_hooks;
73 $ret = '';
74
75 if (isset($squirrelmail_plugin_hooks[$name])
76 && is_array($squirrelmail_plugin_hooks[$name])) {
77 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
78 /* Add something to set correct gettext domain for plugin. */
79 if (function_exists($function)) {
80 $ret = $function($parm);
dd389be5 81 }
2d367c68 82 }
2d367c68 83 }
7b086a80 84
0606ca1f 85 /* Variable-length argument lists have a slight problem when */
86 /* passing values by reference. Pity. This is a workaround. */
2586d588 87 return $ret;
0606ca1f 88}
7b086a80 89
8b096f0a 90/**
91 * This function executes a hook, concatenating the results of each
92 * plugin that has the hook defined.
93 *
94 * @param string name the name of the hook
95 * @param mixed parm optional hook function parameters
96 * @return string a concatenation of the results of each plugin function
97 */
09ac2863 98function concat_hook_function($name,$parm=NULL) {
99 global $squirrelmail_plugin_hooks;
100 $ret = '';
31524bcd 101
09ac2863 102 if (isset($squirrelmail_plugin_hooks[$name])
103 && is_array($squirrelmail_plugin_hooks[$name])) {
104 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
105 /* Concatenate results from hook. */
106 if (function_exists($function)) {
107 $ret .= $function($parm);
108 }
109 }
110 }
111
112 /* Variable-length argument lists have a slight problem when */
113 /* passing values by reference. Pity. This is a workaround. */
114 return $ret;
115}
cd7fc9e6 116
5576644b 117/**
118 * This function is used for hooks which are to return true or
119 * false. If $priority is > 0, any one or more trues will override
120 * any falses. If $priority < 0, then one or more falses will
121 * override any trues.
8b096f0a 122 * Priority 0 means majority rules. Ties will be broken with $tie
123 *
124 * @param string name the hook name
125 * @param mixed parm the parameters for the hook function
126 * @param int priority
127 * @param bool tie
128 * @return bool the result of the function
129 */
5576644b 130function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
131 global $squirrelmail_plugin_hooks;
132 $yea = 0;
133 $nay = 0;
134 $ret = $tie;
135
136 if (isset($squirrelmail_plugin_hooks[$name]) &&
137 is_array($squirrelmail_plugin_hooks[$name])) {
138
139 /* Loop over the plugins that registered the hook */
140 foreach ($squirrelmail_plugin_hooks[$name] as $function) {
141 if (function_exists($function)) {
142 $ret = $function($parm);
143 if ($ret) {
144 $yea++;
145 } else {
146 $nay++;
147 }
148 }
149 }
150
151 /* Examine the aftermath and assign the return value appropriately */
152 if (($priority > 0) && ($yea)) {
153 $ret = true;
154 } elseif (($priority < 0) && ($nay)) {
155 $ret = false;
156 } elseif ($yea > $nay) {
157 $ret = true;
158 } elseif ($nay > $yea) {
159 $ret = false;
160 } else {
161 // There's a tie, no action needed.
162 }
163 return $ret;
164 }
165 // If the code gets here, there was a problem - no hooks, etc.
166 return NULL;
167}
168
cd7fc9e6 169/**
170 * This function checks whether the user's USER_AGENT is known to
171 * be broken. If so, returns true and the plugin is invisible to the
172 * offending browser.
7349fa12 173 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
94ba79ce 174 * FIXME: This function needs to have its name changed!
8b096f0a 175 *
176 * @return bool whether this browser properly supports JavaScript
cd7fc9e6 177 */
178function soupNazi(){
7349fa12 179 return !checkForJavascript();
cd7fc9e6 180}
0606ca1f 181/*************************************/
182/*** MAIN PLUGIN LOADING CODE HERE ***/
183/*************************************/
184
185/* On startup, register all plugins configured for use. */
186if (isset($plugins) && is_array($plugins)) {
187 foreach ($plugins as $name) {
188 use_plugin($name);
2d367c68 189 }
0606ca1f 190}
076c01d7 191
94ba79ce 192?>