PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1_5_1/squirrelmail/functions/plugin.php

#
PHP | 238 lines | 118 code | 27 blank | 93 comment | 26 complexity | 48601db33e940776b396432a02ade962 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  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: plugin.php 10594 2006-01-28 19:12:19Z kink $
  12. * @package squirrelmail
  13. */
  14. /** Everything needs global.. */
  15. require_once(SM_PATH . 'functions/global.php');
  16. require_once(SM_PATH . 'config/config.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. $currentHookName = $name;
  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. $currentHookName = '';
  53. /* Variable-length argument lists have a slight problem when */
  54. /* passing values by reference. Pity. This is a workaround. */
  55. return $data;
  56. }
  57. /**
  58. * This function executes a hook and allows for parameters to be passed.
  59. *
  60. * @param string name the name of the hook
  61. * @param mixed param the parameters to pass to the hook function
  62. * @return mixed the return value of the hook function
  63. */
  64. function do_hook_function($name,$parm=NULL) {
  65. global $squirrelmail_plugin_hooks, $currentHookName;
  66. $ret = '';
  67. $currentHookName = $name;
  68. if (isset($squirrelmail_plugin_hooks[$name])
  69. && is_array($squirrelmail_plugin_hooks[$name])) {
  70. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  71. /* Add something to set correct gettext domain for plugin. */
  72. if (function_exists($function)) {
  73. $ret = $function($parm);
  74. }
  75. }
  76. }
  77. $currentHookName = '';
  78. /* Variable-length argument lists have a slight problem when */
  79. /* passing values by reference. Pity. This is a workaround. */
  80. return $ret;
  81. }
  82. /**
  83. * This function executes a hook, concatenating the results of each
  84. * plugin that has the hook defined.
  85. *
  86. * @param string name the name of the hook
  87. * @param mixed parm optional hook function parameters
  88. * @return string a concatenation of the results of each plugin function
  89. */
  90. function concat_hook_function($name,$parm=NULL) {
  91. global $squirrelmail_plugin_hooks, $currentHookName;
  92. $ret = '';
  93. $currentHookName = $name;
  94. if (isset($squirrelmail_plugin_hooks[$name])
  95. && is_array($squirrelmail_plugin_hooks[$name])) {
  96. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  97. /* Concatenate results from hook. */
  98. if (function_exists($function)) {
  99. $ret .= $function($parm);
  100. }
  101. }
  102. }
  103. $currentHookName = '';
  104. /* Variable-length argument lists have a slight problem when */
  105. /* passing values by reference. Pity. This is a workaround. */
  106. return $ret;
  107. }
  108. /**
  109. * This function is used for hooks which are to return true or
  110. * false. If $priority is > 0, any one or more trues will override
  111. * any falses. If $priority < 0, then one or more falses will
  112. * override any trues.
  113. * Priority 0 means majority rules. Ties will be broken with $tie
  114. *
  115. * @param string name the hook name
  116. * @param mixed parm the parameters for the hook function
  117. * @param int priority
  118. * @param bool tie
  119. * @return bool the result of the function
  120. */
  121. function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
  122. global $squirrelmail_plugin_hooks, $currentHookName;
  123. $yea = 0;
  124. $nay = 0;
  125. $ret = $tie;
  126. if (isset($squirrelmail_plugin_hooks[$name]) &&
  127. is_array($squirrelmail_plugin_hooks[$name])) {
  128. /* Loop over the plugins that registered the hook */
  129. $currentHookName = $name;
  130. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  131. if (function_exists($function)) {
  132. $ret = $function($parm);
  133. if ($ret) {
  134. $yea++;
  135. } else {
  136. $nay++;
  137. }
  138. }
  139. }
  140. $currentHookName = '';
  141. /* Examine the aftermath and assign the return value appropriately */
  142. if (($priority > 0) && ($yea)) {
  143. $ret = true;
  144. } elseif (($priority < 0) && ($nay)) {
  145. $ret = false;
  146. } elseif ($yea > $nay) {
  147. $ret = true;
  148. } elseif ($nay > $yea) {
  149. $ret = false;
  150. } else {
  151. // There's a tie, no action needed.
  152. }
  153. return $ret;
  154. }
  155. // If the code gets here, there was a problem - no hooks, etc.
  156. return NULL;
  157. }
  158. /**
  159. * This function checks whether the user's USER_AGENT is known to
  160. * be broken. If so, returns true and the plugin is invisible to the
  161. * offending browser.
  162. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  163. * FIXME: This function needs to have its name changed!
  164. *
  165. * @return bool whether this browser properly supports JavaScript
  166. * @deprecated use checkForJavascript() since 1.5.1
  167. */
  168. function soupNazi(){
  169. return !checkForJavascript();
  170. }
  171. /**
  172. * Check if plugin is enabled
  173. * @param string $plugin_name plugin name
  174. * @since 1.5.1
  175. * @return boolean
  176. */
  177. function is_plugin_enabled($plugin_name) {
  178. global $plugins;
  179. /**
  180. * check if variable is empty. if var is not set, php empty
  181. * returns true without error notice.
  182. *
  183. * then check if it is an array
  184. */
  185. if (empty($plugins) || ! is_array($plugins))
  186. return false;
  187. if ( in_array($plugin_name,$plugins) ) {
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. /*************************************/
  194. /*** MAIN PLUGIN LOADING CODE HERE ***/
  195. /*************************************/
  196. /* On startup, register all plugins configured for use. */
  197. if (isset($plugins) && is_array($plugins)) {
  198. // turn on output buffering in order to prevent output of new lines
  199. ob_start();
  200. foreach ($plugins as $name) {
  201. use_plugin($name);
  202. }
  203. // get output and remove whitespace
  204. $output = trim(ob_get_contents());
  205. ob_end_clean();
  206. // if plugins output more than newlines and spacing, stop script execution.
  207. if (!empty($output)) {
  208. die($output);
  209. }
  210. }
  211. ?>