plugin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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;
  41. $data = func_get_args();
  42. $ret = '';
  43. if (isset($squirrelmail_plugin_hooks[$name])
  44. && is_array($squirrelmail_plugin_hooks[$name])) {
  45. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  46. /* Add something to set correct gettext domain for plugin. */
  47. if (function_exists($function)) {
  48. $function($data);
  49. }
  50. }
  51. }
  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;
  65. $ret = '';
  66. if (isset($squirrelmail_plugin_hooks[$name])
  67. && is_array($squirrelmail_plugin_hooks[$name])) {
  68. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  69. /* Add something to set correct gettext domain for plugin. */
  70. if (function_exists($function)) {
  71. $ret = $function($parm);
  72. }
  73. }
  74. }
  75. /* Variable-length argument lists have a slight problem when */
  76. /* passing values by reference. Pity. This is a workaround. */
  77. return $ret;
  78. }
  79. /**
  80. * This function executes a hook, concatenating the results of each
  81. * plugin that has the hook defined.
  82. *
  83. * @param string name the name of the hook
  84. * @param mixed parm optional hook function parameters
  85. * @return string a concatenation of the results of each plugin function
  86. */
  87. function concat_hook_function($name,$parm=NULL) {
  88. global $squirrelmail_plugin_hooks;
  89. $ret = '';
  90. if (isset($squirrelmail_plugin_hooks[$name])
  91. && is_array($squirrelmail_plugin_hooks[$name])) {
  92. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  93. /* Concatenate results from hook. */
  94. if (function_exists($function)) {
  95. $ret .= $function($parm);
  96. }
  97. }
  98. }
  99. /* Variable-length argument lists have a slight problem when */
  100. /* passing values by reference. Pity. This is a workaround. */
  101. return $ret;
  102. }
  103. /**
  104. * This function is used for hooks which are to return true or
  105. * false. If $priority is > 0, any one or more trues will override
  106. * any falses. If $priority < 0, then one or more falses will
  107. * override any trues.
  108. * Priority 0 means majority rules. Ties will be broken with $tie
  109. *
  110. * @param string name the hook name
  111. * @param mixed parm the parameters for the hook function
  112. * @param int priority
  113. * @param bool tie
  114. * @return bool the result of the function
  115. */
  116. function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
  117. global $squirrelmail_plugin_hooks;
  118. $yea = 0;
  119. $nay = 0;
  120. $ret = $tie;
  121. if (isset($squirrelmail_plugin_hooks[$name]) &&
  122. is_array($squirrelmail_plugin_hooks[$name])) {
  123. /* Loop over the plugins that registered the hook */
  124. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  125. if (function_exists($function)) {
  126. $ret = $function($parm);
  127. if ($ret) {
  128. $yea++;
  129. } else {
  130. $nay++;
  131. }
  132. }
  133. }
  134. /* Examine the aftermath and assign the return value appropriately */
  135. if (($priority > 0) && ($yea)) {
  136. $ret = true;
  137. } elseif (($priority < 0) && ($nay)) {
  138. $ret = false;
  139. } elseif ($yea > $nay) {
  140. $ret = true;
  141. } elseif ($nay > $yea) {
  142. $ret = false;
  143. } else {
  144. // There's a tie, no action needed.
  145. }
  146. return $ret;
  147. }
  148. // If the code gets here, there was a problem - no hooks, etc.
  149. return NULL;
  150. }
  151. /**
  152. * This function checks whether the user's USER_AGENT is known to
  153. * be broken. If so, returns true and the plugin is invisible to the
  154. * offending browser.
  155. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  156. * This function needs to have its name changed!
  157. *
  158. * @return bool whether this browser properly supports JavaScript
  159. */
  160. function soupNazi(){
  161. return !checkForJavascript();
  162. }
  163. /*************************************/
  164. /*** MAIN PLUGIN LOADING CODE HERE ***/
  165. /*************************************/
  166. /* On startup, register all plugins configured for use. */
  167. if (isset($plugins) && is_array($plugins)) {
  168. foreach ($plugins as $name) {
  169. use_plugin($name);
  170. }
  171. }
  172. ?>