plugin.php 6.0 KB

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