plugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * plugin.php
  4. *
  5. * Copyright (c) 1999-2003 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. * $Id$
  13. * @package squirrelmail
  14. */
  15. /** Everything needs global.. */
  16. require_once(SM_PATH . 'functions/global.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;
  40. $data = func_get_args();
  41. $ret = '';
  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. /* Variable-length argument lists have a slight problem when */
  52. /* passing values by reference. Pity. This is a workaround. */
  53. return $data;
  54. }
  55. /**
  56. * This function executes a hook and allows for parameters to be passed.
  57. *
  58. * @param string name the name of the hook
  59. * @param mixed param the parameters to pass to the hook function
  60. * @return mixed the return value of the hook function
  61. */
  62. function do_hook_function($name,$parm=NULL) {
  63. global $squirrelmail_plugin_hooks;
  64. $ret = '';
  65. if (isset($squirrelmail_plugin_hooks[$name])
  66. && is_array($squirrelmail_plugin_hooks[$name])) {
  67. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  68. /* Add something to set correct gettext domain for plugin. */
  69. if (function_exists($function)) {
  70. $ret = $function($parm);
  71. }
  72. }
  73. }
  74. /* Variable-length argument lists have a slight problem when */
  75. /* passing values by reference. Pity. This is a workaround. */
  76. return $ret;
  77. }
  78. /**
  79. * This function executes a hook, concatenating the results of each
  80. * plugin that has the hook defined.
  81. *
  82. * @param string name the name of the hook
  83. * @param mixed parm optional hook function parameters
  84. * @return string a concatenation of the results of each plugin function
  85. */
  86. function concat_hook_function($name,$parm=NULL) {
  87. global $squirrelmail_plugin_hooks;
  88. $ret = '';
  89. if (isset($squirrelmail_plugin_hooks[$name])
  90. && is_array($squirrelmail_plugin_hooks[$name])) {
  91. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  92. /* Concatenate results from hook. */
  93. if (function_exists($function)) {
  94. $ret .= $function($parm);
  95. }
  96. }
  97. }
  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;
  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. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  124. if (function_exists($function)) {
  125. $ret = $function($parm);
  126. if ($ret) {
  127. $yea++;
  128. } else {
  129. $nay++;
  130. }
  131. }
  132. }
  133. /* Examine the aftermath and assign the return value appropriately */
  134. if (($priority > 0) && ($yea)) {
  135. $ret = true;
  136. } elseif (($priority < 0) && ($nay)) {
  137. $ret = false;
  138. } elseif ($yea > $nay) {
  139. $ret = true;
  140. } elseif ($nay > $yea) {
  141. $ret = false;
  142. } else {
  143. // There's a tie, no action needed.
  144. }
  145. return $ret;
  146. }
  147. // If the code gets here, there was a problem - no hooks, etc.
  148. return NULL;
  149. }
  150. /**
  151. * This function checks whether the user's USER_AGENT is known to
  152. * be broken. If so, returns true and the plugin is invisible to the
  153. * offending browser.
  154. * This function needs to have its name changed!
  155. *
  156. * @return bool whether this browser properly supports JavaScript
  157. */
  158. function soupNazi(){
  159. $soup_menu = array('Mozilla/3','Mozilla/2','Mozilla/1', 'Opera 4',
  160. 'Opera/4', 'OmniWeb', 'Lynx');
  161. sqgetGlobalVar('HTTP_USER_AGENT', $user_agent, SQ_SERVER);
  162. foreach($soup_menu as $browser) {
  163. if(stristr($user_agent, $browser)) {
  164. return 1;
  165. }
  166. }
  167. return 0;
  168. }
  169. /*************************************/
  170. /*** MAIN PLUGIN LOADING CODE HERE ***/
  171. /*************************************/
  172. /* On startup, register all plugins configured for use. */
  173. if (isset($plugins) && is_array($plugins)) {
  174. foreach ($plugins as $name) {
  175. use_plugin($name);
  176. }
  177. }
  178. ?>