plugin.php 5.9 KB

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