plugin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /**
  23. * As of SM 1.5.2, plugin hook registration is statically
  24. * accomplished using the configuration utility (config/conf.pl).
  25. * And this code is deprecated (but let's keep it until
  26. * the new registration system is proven).
  27. *
  28. */
  29. //$function = "squirrelmail_plugin_init_$name";
  30. //if (function_exists($function)) {
  31. // $function();
  32. //}
  33. }
  34. }
  35. /**
  36. * This function executes a plugin hook.
  37. *
  38. * It includes an arbitrary return value that is managed by
  39. * all plugins on the same hook and returned to the core hook
  40. * location.
  41. *
  42. * The desired format of the return value should be defined
  43. * by the context in which the hook is called.
  44. *
  45. * Note that the master return value for this hook is passed
  46. * to each plugin after the main argument(s) value/array as a
  47. * convenience only - to show what the current return value is
  48. * even though it is liable to be changed by other plugins.
  49. *
  50. * If any plugin on this hook wants to modify the $args
  51. * plugin parameter, it simply has to use call-by-reference
  52. * syntax in the hook function that it has registered for the
  53. * current hook. Note that this is in addition to (entirely
  54. * independent of) the return value for this hook.
  55. *
  56. * @param string $name Name of hook being executed
  57. * @param mixed $args A single value or an array of arguments
  58. * that are to be passed to all plugins
  59. * operating off the hook being called.
  60. * Note that this argument is passed by
  61. * reference thus it is liable to be
  62. * changed after the hook completes.
  63. *
  64. * @return mixed The return value that is managed by the plugins
  65. * on the current hook.
  66. *
  67. */
  68. function do_hook($name, &$args) {
  69. global $squirrelmail_plugin_hooks, $currentHookName;
  70. $currentHookName = $name;
  71. $ret = NULL;
  72. if (isset($squirrelmail_plugin_hooks[$name])
  73. && is_array($squirrelmail_plugin_hooks[$name])) {
  74. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  75. use_plugin($plugin_name);
  76. if (function_exists($function)) {
  77. $ret = $function($args, $ret);
  78. }
  79. }
  80. }
  81. $currentHookName = '';
  82. return $ret;
  83. }
  84. /**
  85. * This function executes a hook that allows for an arbitrary
  86. * return value from each plugin that will be merged into one
  87. * array (or one string if all return values are strings) and
  88. * returned to the core hook location.
  89. *
  90. * Note that unlike PHP's array_merge function, matching array keys
  91. * will not overwrite each other, instead, values under such keys
  92. * will be concatenated if they are both strings, or merged if they
  93. * are arrays (in the same (non-overwrite) manner recursively).
  94. *
  95. * Plugins returning non-arrays (strings, objects, etc) will have
  96. * their output added to the end of the ultimate return array,
  97. * unless ALL values returned are strings, in which case one string
  98. * with all returned strings concatenated together is returned.
  99. *
  100. * If any plugin on this hook wants to modify the $args
  101. * plugin parameter, it simply has to use call-by-reference
  102. * syntax in the hook function that it has registered for the
  103. * current hook. Note that this is in addition to (entirely
  104. * independent of) the return value for this hook.
  105. *
  106. * @param string $name Name of hook being executed
  107. * @param mixed $args A single value or an array of arguments
  108. * that are to be passed to all plugins
  109. * operating off the hook being called.
  110. * Note that this argument is passed by
  111. * reference thus it is liable to be
  112. * changed after the hook completes.
  113. *
  114. * @return mixed the merged return arrays or strings of each
  115. * plugin on this hook.
  116. *
  117. */
  118. function concat_hook_function($name, &$args) {
  119. global $squirrelmail_plugin_hooks, $currentHookName;
  120. $currentHookName = $name;
  121. $ret = '';
  122. if (isset($squirrelmail_plugin_hooks[$name])
  123. && is_array($squirrelmail_plugin_hooks[$name])) {
  124. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  125. use_plugin($plugin_name);
  126. if (function_exists($function)) {
  127. $plugin_ret = $function($args);
  128. if (!empty($plugin_ret)) {
  129. $ret = sqm_array_merge($ret, $plugin_ret);
  130. }
  131. }
  132. }
  133. }
  134. $currentHookName = '';
  135. return $ret;
  136. }
  137. /**
  138. * This function is used for hooks which are to return true or
  139. * false. If $priority is > 0, any one or more trues will override
  140. * any falses. If $priority < 0, then one or more falses will
  141. * override any trues.
  142. * Priority 0 means majority rules. Ties will be broken with $tie
  143. *
  144. * If any plugin on this hook wants to modify the $args
  145. * plugin parameter, it simply has to use call-by-reference
  146. * syntax in the hook function that it has registered for the
  147. * current hook. Note that this is in addition to (entirely
  148. * independent of) the return value for this hook.
  149. *
  150. * @param string $name The hook name
  151. * @param mixed $args A single value or an array of arguments
  152. * that are to be passed to all plugins
  153. * operating off the hook being called.
  154. * Note that this argument is passed by
  155. * reference thus it is liable to be
  156. * changed after the hook completes.
  157. * @param int $priority See explanation above
  158. * @param boolean $tie See explanation above
  159. *
  160. * @return boolean The result of the function
  161. *
  162. */
  163. function boolean_hook_function($name, &$args, $priority=0, $tie=false) {
  164. global $squirrelmail_plugin_hooks, $currentHookName;
  165. $yea = 0;
  166. $nay = 0;
  167. $ret = $tie;
  168. if (isset($squirrelmail_plugin_hooks[$name]) &&
  169. is_array($squirrelmail_plugin_hooks[$name])) {
  170. /* Loop over the plugins that registered the hook */
  171. $currentHookName = $name;
  172. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  173. use_plugin($plugin_name);
  174. if (function_exists($function)) {
  175. $ret = $function($args);
  176. if ($ret) {
  177. $yea++;
  178. } else {
  179. $nay++;
  180. }
  181. }
  182. }
  183. $currentHookName = '';
  184. /* Examine the aftermath and assign the return value appropriately */
  185. if (($priority > 0) && ($yea)) {
  186. $ret = true;
  187. } elseif (($priority < 0) && ($nay)) {
  188. $ret = false;
  189. } elseif ($yea > $nay) {
  190. $ret = true;
  191. } elseif ($nay > $yea) {
  192. $ret = false;
  193. } else {
  194. // There's a tie, no action needed.
  195. }
  196. return $ret;
  197. }
  198. // If the code gets here, there was a problem - no hooks, etc.
  199. return NULL;
  200. }
  201. /**
  202. * Do not use, use checkForJavascript() instead.
  203. *
  204. * This function checks whether the user's USER_AGENT is known to
  205. * be broken. If so, returns true and the plugin is invisible to the
  206. * offending browser.
  207. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  208. *
  209. * @return bool whether this browser properly supports JavaScript
  210. * @deprecated use checkForJavascript() since 1.5.1
  211. */
  212. function soupNazi(){
  213. return !checkForJavascript();
  214. }
  215. /**
  216. * Check if plugin is enabled
  217. * @param string $plugin_name plugin name
  218. * @since 1.5.1
  219. * @return boolean
  220. */
  221. function is_plugin_enabled($plugin_name) {
  222. global $plugins;
  223. /**
  224. * check if variable is empty. if var is not set, php empty
  225. * returns true without error notice.
  226. *
  227. * then check if it is an array
  228. */
  229. if (empty($plugins) || ! is_array($plugins))
  230. return false;
  231. if ( in_array($plugin_name,$plugins) ) {
  232. return true;
  233. } else {
  234. return false;
  235. }
  236. }
  237. /**
  238. * Check a plugin's version.
  239. *
  240. * Returns TRUE if the given plugin is installed,
  241. * activated and is at minimum version $a.$b.$c.
  242. * If any one of those conditions fails, FALSE
  243. * will be returned (careful of plugins that are
  244. * sufficiently versioned but are not activated).
  245. *
  246. * By overriding the default value of $force_inclusion,
  247. * this function will attempt to grab versioning
  248. * information from the given plugin even if it
  249. * is not activated (plugin still has to be
  250. * unpackaged and set in place in the plugins
  251. * directory). Use with care - some plugins
  252. * might break SquirrelMail when this is used.
  253. *
  254. * Note that this function assumes plugin
  255. * versioning is consistently applied in the same
  256. * fashion that SquirrelMail versions are, with the
  257. * exception that an applicable SquirrelMail
  258. * version may be appended to the version number
  259. * (which will be ignored herein). That is, plugin
  260. * version number schemes are expected in the following
  261. * format: 1.2.3, or 1.2.3-1.4.0.
  262. *
  263. * Any characters after the third number are discarded,
  264. * so formats such as the following will also work,
  265. * although extra information about beta versions can
  266. * possibly confuse the desired results of the version
  267. * check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
  268. *
  269. * @since 1.5.2
  270. *
  271. * @param string plugin_name name of the plugin to
  272. * check; must precisely
  273. * match the plugin
  274. * directory name
  275. * @param int a major version number
  276. * @param int b minor version number
  277. * @param int c release number
  278. * @param bool force_inclusion try to get version info
  279. * for plugins not activated?
  280. * (default FALSE)
  281. *
  282. * @return bool
  283. *
  284. */
  285. function check_plugin_version($plugin_name,
  286. $a = 0, $b = 0, $c = 0,
  287. $force_inclusion = FALSE)
  288. {
  289. $info_function = $plugin_name . '_info';
  290. $version_function = $plugin_name . '_version';
  291. $plugin_info = array();
  292. $plugin_version = FALSE;
  293. // first attempt to find the plugin info function, wherein
  294. // the plugin version should be available
  295. //
  296. if (function_exists($info_function))
  297. $plugin_info = $info_function();
  298. else if ($force_inclusion
  299. && file_exists(SM_PATH . 'plugins/' . $plugin_name . '/setup.php'))
  300. {
  301. include_once(SM_PATH . 'plugins/' . $plugin_name . '/setup.php');
  302. if (function_exists($info_function))
  303. $plugin_info = $info_function();
  304. }
  305. if (!empty($plugin_info['version']))
  306. $plugin_version = $plugin_info['version'];
  307. // otherwise, look for older version function
  308. //
  309. if (!$plugin_version && function_exists($version_function))
  310. $plugin_version = $version_function();
  311. if (!$plugin_version) return FALSE;
  312. // now massage version number into something we understand
  313. //
  314. $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
  315. '', $plugin_version),
  316. '.');
  317. $plugin_version = explode('.', $plugin_version);
  318. if (!isset($plugin_version[0])) $plugin_version[0] = 0;
  319. if (!isset($plugin_version[1])) $plugin_version[1] = 0;
  320. if (!isset($plugin_version[2])) $plugin_version[2] = 0;
  321. // sm_print_r($plugin_version);
  322. // now test the version number
  323. //
  324. if ($plugin_version[0] < $a ||
  325. ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
  326. ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))
  327. return FALSE;
  328. return TRUE;
  329. }