plugin.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * plugin.php
  4. *
  5. * This file provides the framework for a plugin architecture.
  6. *
  7. * Documentation on how to write plugins might show up some time.
  8. *
  9. * @copyright &copy; 1999-2005 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package squirrelmail
  13. */
  14. /** Everything needs global.. */
  15. require_once(SM_PATH . 'functions/global.php');
  16. require_once(SM_PATH . 'functions/prefs.php');
  17. global $squirrelmail_plugin_hooks;
  18. $squirrelmail_plugin_hooks = array();
  19. /**
  20. * This function adds a plugin.
  21. * @param string $name Internal plugin name (ie. delete_move_next)
  22. * @return void
  23. */
  24. function use_plugin ($name) {
  25. if (file_exists(SM_PATH . "plugins/$name/setup.php")) {
  26. include_once(SM_PATH . "plugins/$name/setup.php");
  27. $function = "squirrelmail_plugin_init_$name";
  28. if (function_exists($function)) {
  29. $function();
  30. }
  31. }
  32. }
  33. /**
  34. * This function executes a hook.
  35. * @param string $name Name of hook to fire
  36. * @return mixed $data
  37. */
  38. function do_hook ($name) {
  39. global $squirrelmail_plugin_hooks, $currentHookName;
  40. $data = func_get_args();
  41. $currentHookName = $name;
  42. if (isset($squirrelmail_plugin_hooks[$name])
  43. && is_array($squirrelmail_plugin_hooks[$name])) {
  44. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  45. /* Add something to set correct gettext domain for plugin. */
  46. if (function_exists($function)) {
  47. $function($data);
  48. }
  49. }
  50. }
  51. $currentHookName = '';
  52. /* Variable-length argument lists have a slight problem when */
  53. /* passing values by reference. Pity. This is a workaround. */
  54. return $data;
  55. }
  56. /**
  57. * This function executes a hook and allows for parameters to be passed.
  58. *
  59. * @param string name the name of the hook
  60. * @param mixed param the parameters to pass to the hook function
  61. * @return mixed the return value of the hook function
  62. */
  63. function do_hook_function($name,$parm=NULL) {
  64. global $squirrelmail_plugin_hooks, $currentHookName;
  65. $ret = '';
  66. $currentHookName = $name;
  67. if (isset($squirrelmail_plugin_hooks[$name])
  68. && is_array($squirrelmail_plugin_hooks[$name])) {
  69. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  70. /* Add something to set correct gettext domain for plugin. */
  71. if (function_exists($function)) {
  72. $ret = $function($parm);
  73. }
  74. }
  75. }
  76. $currentHookName = '';
  77. /* Variable-length argument lists have a slight problem when */
  78. /* passing values by reference. Pity. This is a workaround. */
  79. return $ret;
  80. }
  81. /**
  82. * This function executes a hook, concatenating the results of each
  83. * plugin that has the hook defined.
  84. *
  85. * @param string name the name of the hook
  86. * @param mixed parm optional hook function parameters
  87. * @return string a concatenation of the results of each plugin function
  88. */
  89. function concat_hook_function($name,$parm=NULL) {
  90. global $squirrelmail_plugin_hooks, $currentHookName;
  91. $ret = '';
  92. $currentHookName = $name;
  93. if (isset($squirrelmail_plugin_hooks[$name])
  94. && is_array($squirrelmail_plugin_hooks[$name])) {
  95. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  96. /* Concatenate results from hook. */
  97. if (function_exists($function)) {
  98. $ret .= $function($parm);
  99. }
  100. }
  101. }
  102. $currentHookName = '';
  103. /* Variable-length argument lists have a slight problem when */
  104. /* passing values by reference. Pity. This is a workaround. */
  105. return $ret;
  106. }
  107. /**
  108. * This function is used for hooks which are to return true or
  109. * false. If $priority is > 0, any one or more trues will override
  110. * any falses. If $priority < 0, then one or more falses will
  111. * override any trues.
  112. * Priority 0 means majority rules. Ties will be broken with $tie
  113. *
  114. * @param string name the hook name
  115. * @param mixed parm the parameters for the hook function
  116. * @param int priority
  117. * @param bool tie
  118. * @return bool the result of the function
  119. */
  120. function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
  121. global $squirrelmail_plugin_hooks, $currentHookName;
  122. $yea = 0;
  123. $nay = 0;
  124. $ret = $tie;
  125. if (isset($squirrelmail_plugin_hooks[$name]) &&
  126. is_array($squirrelmail_plugin_hooks[$name])) {
  127. /* Loop over the plugins that registered the hook */
  128. $currentHookName = $name;
  129. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  130. if (function_exists($function)) {
  131. $ret = $function($parm);
  132. if ($ret) {
  133. $yea++;
  134. } else {
  135. $nay++;
  136. }
  137. }
  138. }
  139. $currentHookName = '';
  140. /* Examine the aftermath and assign the return value appropriately */
  141. if (($priority > 0) && ($yea)) {
  142. $ret = true;
  143. } elseif (($priority < 0) && ($nay)) {
  144. $ret = false;
  145. } elseif ($yea > $nay) {
  146. $ret = true;
  147. } elseif ($nay > $yea) {
  148. $ret = false;
  149. } else {
  150. // There's a tie, no action needed.
  151. }
  152. return $ret;
  153. }
  154. // If the code gets here, there was a problem - no hooks, etc.
  155. return NULL;
  156. }
  157. /**
  158. * This function checks whether the user's USER_AGENT is known to
  159. * be broken. If so, returns true and the plugin is invisible to the
  160. * offending browser.
  161. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  162. * FIXME: This function needs to have its name changed!
  163. *
  164. * @return bool whether this browser properly supports JavaScript
  165. * @deprecated use checkForJavascript() since 1.5.1
  166. */
  167. function soupNazi(){
  168. return !checkForJavascript();
  169. }
  170. /**
  171. * Check if plugin is enabled
  172. * @param string $plugin_name plugin name
  173. * @since 1.5.1
  174. * @return boolean
  175. */
  176. function is_plugin_enabled($plugin_name) {
  177. global $plugins;
  178. /**
  179. * check if variable is set. can't do is_array(), if variable is not set
  180. * check if it is an array
  181. * there is no need to call in_array() if $plugins array is empty
  182. */
  183. if (! isset($plugins) || ! is_array($plugins) || empty($plugins))
  184. return false;
  185. if ( in_array($plugin_name,$plugins) ) {
  186. return true;
  187. } else {
  188. return false;
  189. }
  190. }
  191. /*************************************/
  192. /*** MAIN PLUGIN LOADING CODE HERE ***/
  193. /*************************************/
  194. /* On startup, register all plugins configured for use. */
  195. if (isset($plugins) && is_array($plugins)) {
  196. foreach ($plugins as $name) {
  197. use_plugin($name);
  198. }
  199. }
  200. ?>