plugin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, allows for parameters to be passed,
  78. * and looks for an array returned from each plugin: each array is
  79. * then merged into one and returned to the core hook location.
  80. *
  81. * Note that unlike PHP's array_merge function, matching array keys
  82. * will not overwrite each other, instead, values under such keys
  83. * will be concatenated if they are both strings, or merged if they
  84. * are arrays (in the same (non-overwrite) manner recursively).
  85. *
  86. * Plugins returning non-arrays (strings, objects, etc) will have
  87. * their output added to the end of the ultimate return array,
  88. * unless ALL values returned are strings, in which case one string
  89. * with all returned strings concatenated together is returned.
  90. *
  91. * @param string name the name of the hook
  92. * @param mixed param the parameters to pass to the hook function
  93. *
  94. * @return mixed the merged return arrays or strings of each
  95. * plugin on this hook
  96. *
  97. */
  98. function concat_hook_function($name,$parm=NULL) {
  99. global $squirrelmail_plugin_hooks, $currentHookName;
  100. // $ret = '';
  101. $ret = array();
  102. $currentHookName = $name;
  103. if (isset($squirrelmail_plugin_hooks[$name])
  104. && is_array($squirrelmail_plugin_hooks[$name])) {
  105. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  106. /* Add something to set correct gettext domain for plugin. */
  107. if (function_exists($function)) {
  108. // $ret .= $function($parm);
  109. $ret = sqm_array_merge($ret, $function($parm));
  110. }
  111. }
  112. }
  113. $currentHookName = '';
  114. /* Variable-length argument lists have a slight problem when */
  115. /* passing values by reference. Pity. This is a workaround. */
  116. return $ret;
  117. }
  118. /**
  119. * This function is used for hooks which are to return true or
  120. * false. If $priority is > 0, any one or more trues will override
  121. * any falses. If $priority < 0, then one or more falses will
  122. * override any trues.
  123. * Priority 0 means majority rules. Ties will be broken with $tie
  124. *
  125. * @param string name the hook name
  126. * @param mixed parm the parameters for the hook function
  127. * @param int priority
  128. * @param bool tie
  129. * @return bool the result of the function
  130. */
  131. function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
  132. global $squirrelmail_plugin_hooks, $currentHookName;
  133. $yea = 0;
  134. $nay = 0;
  135. $ret = $tie;
  136. if (isset($squirrelmail_plugin_hooks[$name]) &&
  137. is_array($squirrelmail_plugin_hooks[$name])) {
  138. /* Loop over the plugins that registered the hook */
  139. $currentHookName = $name;
  140. foreach ($squirrelmail_plugin_hooks[$name] as $function) {
  141. if (function_exists($function)) {
  142. $ret = $function($parm);
  143. if ($ret) {
  144. $yea++;
  145. } else {
  146. $nay++;
  147. }
  148. }
  149. }
  150. $currentHookName = '';
  151. /* Examine the aftermath and assign the return value appropriately */
  152. if (($priority > 0) && ($yea)) {
  153. $ret = true;
  154. } elseif (($priority < 0) && ($nay)) {
  155. $ret = false;
  156. } elseif ($yea > $nay) {
  157. $ret = true;
  158. } elseif ($nay > $yea) {
  159. $ret = false;
  160. } else {
  161. // There's a tie, no action needed.
  162. }
  163. return $ret;
  164. }
  165. // If the code gets here, there was a problem - no hooks, etc.
  166. return NULL;
  167. }
  168. /**
  169. * This function checks whether the user's USER_AGENT is known to
  170. * be broken. If so, returns true and the plugin is invisible to the
  171. * offending browser.
  172. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  173. * FIXME: This function needs to have its name changed!
  174. *
  175. * @return bool whether this browser properly supports JavaScript
  176. * @deprecated use checkForJavascript() since 1.5.1
  177. */
  178. function soupNazi(){
  179. return !checkForJavascript();
  180. }
  181. /**
  182. * Check if plugin is enabled
  183. * @param string $plugin_name plugin name
  184. * @since 1.5.1
  185. * @return boolean
  186. */
  187. function is_plugin_enabled($plugin_name) {
  188. global $plugins;
  189. /**
  190. * check if variable is empty. if var is not set, php empty
  191. * returns true without error notice.
  192. *
  193. * then check if it is an array
  194. */
  195. if (empty($plugins) || ! is_array($plugins))
  196. return false;
  197. if ( in_array($plugin_name,$plugins) ) {
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. /**
  204. * Check a plugin's version.
  205. *
  206. * Returns TRUE if the given plugin is installed,
  207. * activated and is at minimum version $a.$b.$c.
  208. * If any one of those conditions fails, FALSE
  209. * will be returned (careful of plugins that are
  210. * sufficiently versioned but are not activated).
  211. *
  212. * By overriding the default value of $force_inclusion,
  213. * this function will attempt to grab versioning
  214. * information from the given plugin even if it
  215. * is not activated (plugin still has to be
  216. * unpackaged and set in place in the plugins
  217. * directory). Use with care - some plugins
  218. * might break SquirrelMail when this is used.
  219. *
  220. * Note that this function assumes plugin
  221. * versioning is consistently applied in the same
  222. * fashion that SquirrelMail versions are, with the
  223. * exception that an applicable SquirrelMail
  224. * version may be appended to the version number
  225. * (which will be ignored herein). That is, plugin
  226. * version number schemes are expected in the following
  227. * format: 1.2.3, or 1.2.3-1.4.0.
  228. *
  229. * Any characters after the third number are discarded,
  230. * so formats such as the following will also work,
  231. * although extra information about beta versions can
  232. * possibly confuse the desired results of the version
  233. * check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
  234. *
  235. * @since 1.5.2
  236. *
  237. * @param string plugin_name name of the plugin to
  238. * check; must precisely
  239. * match the plugin
  240. * directory name
  241. * @param int a major version number
  242. * @param int b minor version number
  243. * @param int c release number
  244. * @param bool force_inclusion try to get version info
  245. * for plugins not activated?
  246. * (default FALSE)
  247. *
  248. * @return bool
  249. *
  250. */
  251. function check_plugin_version($plugin_name,
  252. $a = 0, $b = 0, $c = 0,
  253. $force_inclusion = FALSE)
  254. {
  255. $info_function = $plugin_name . '_info';
  256. $version_function = $plugin_name . '_version';
  257. $plugin_info = array();
  258. $plugin_version = FALSE;
  259. // first attempt to find the plugin info function, wherein
  260. // the plugin version should be available
  261. //
  262. if (function_exists($info_function))
  263. $plugin_info = $info_function();
  264. else if ($force_inclusion
  265. && file_exists(SM_PATH . 'plugins/' . $plugin_name . '/setup.php'))
  266. {
  267. include_once(SM_PATH . 'plugins/' . $plugin_name . '/setup.php');
  268. if (function_exists($info_function))
  269. $plugin_info = $info_function();
  270. }
  271. if (!empty($plugin_info['version']))
  272. $plugin_version = $plugin_info['version'];
  273. // otherwise, look for older version function
  274. //
  275. if (!$plugin_version && function_exists($version_function))
  276. $plugin_version = $version_function();
  277. if (!$plugin_version) return FALSE;
  278. // now massage version number into something we understand
  279. //
  280. $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
  281. '', $plugin_version),
  282. '.');
  283. $plugin_version = explode('.', $plugin_version);
  284. if (!isset($plugin_version[0])) $plugin_version[0] = 0;
  285. if (!isset($plugin_version[1])) $plugin_version[1] = 0;
  286. if (!isset($plugin_version[2])) $plugin_version[2] = 0;
  287. // sm_print_r($plugin_version);
  288. // now test the version number
  289. //
  290. if ($plugin_version[0] < $a ||
  291. ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
  292. ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))
  293. return FALSE;
  294. return TRUE;
  295. }