plugin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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-2007 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. * (unless $force_array is TRUE).
  100. *
  101. * If any plugin on this hook wants to modify the $args
  102. * plugin parameter, it simply has to use call-by-reference
  103. * syntax in the hook function that it has registered for the
  104. * current hook. Note that this is in addition to (entirely
  105. * independent of) the return value for this hook.
  106. *
  107. * @param string $name Name of hook being executed
  108. * @param mixed $args A single value or an array of arguments
  109. * that are to be passed to all plugins
  110. * operating off the hook being called.
  111. * Note that this argument is passed by
  112. * reference thus it is liable to be
  113. * changed after the hook completes.
  114. * @param boolean $force_array When TRUE, guarantees the return
  115. * value will ALWAYS be an array,
  116. * (simple strings will be forced
  117. * into a one-element array).
  118. * When FALSE, behavior is as
  119. * described above (OPTIONAL;
  120. * default behavior is to return
  121. * mixed - array or string).
  122. *
  123. * @return mixed the merged return arrays or strings of each
  124. * plugin on this hook.
  125. *
  126. */
  127. function concat_hook_function($name, &$args, $force_array=FALSE) {
  128. global $squirrelmail_plugin_hooks, $currentHookName;
  129. $currentHookName = $name;
  130. $ret = '';
  131. if (isset($squirrelmail_plugin_hooks[$name])
  132. && is_array($squirrelmail_plugin_hooks[$name])) {
  133. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  134. use_plugin($plugin_name);
  135. if (function_exists($function)) {
  136. $plugin_ret = $function($args);
  137. if (!empty($plugin_ret)) {
  138. $ret = sqm_array_merge($ret, $plugin_ret);
  139. }
  140. }
  141. }
  142. }
  143. if ($force_array && is_string($ret)) {
  144. $ret = array($ret);
  145. }
  146. $currentHookName = '';
  147. return $ret;
  148. }
  149. /**
  150. * This function is used for hooks which are to return true or
  151. * false. If $priority is > 0, any one or more trues will override
  152. * any falses. If $priority < 0, then one or more falses will
  153. * override any trues.
  154. * Priority 0 means majority rules. Ties will be broken with $tie
  155. *
  156. * If any plugin on this hook wants to modify the $args
  157. * plugin parameter, it simply has to use call-by-reference
  158. * syntax in the hook function that it has registered for the
  159. * current hook. Note that this is in addition to (entirely
  160. * independent of) the return value for this hook.
  161. *
  162. * @param string $name The hook name
  163. * @param mixed $args A single value or an array of arguments
  164. * that are to be passed to all plugins
  165. * operating off the hook being called.
  166. * Note that this argument is passed by
  167. * reference thus it is liable to be
  168. * changed after the hook completes.
  169. * @param int $priority See explanation above
  170. * @param boolean $tie See explanation above
  171. *
  172. * @return boolean The result of the function
  173. *
  174. */
  175. function boolean_hook_function($name, &$args, $priority=0, $tie=false) {
  176. global $squirrelmail_plugin_hooks, $currentHookName;
  177. $yea = 0;
  178. $nay = 0;
  179. $ret = $tie;
  180. if (isset($squirrelmail_plugin_hooks[$name]) &&
  181. is_array($squirrelmail_plugin_hooks[$name])) {
  182. /* Loop over the plugins that registered the hook */
  183. $currentHookName = $name;
  184. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  185. use_plugin($plugin_name);
  186. if (function_exists($function)) {
  187. $ret = $function($args);
  188. if ($ret) {
  189. $yea++;
  190. } else {
  191. $nay++;
  192. }
  193. }
  194. }
  195. $currentHookName = '';
  196. /* Examine the aftermath and assign the return value appropriately */
  197. if (($priority > 0) && ($yea)) {
  198. $ret = true;
  199. } elseif (($priority < 0) && ($nay)) {
  200. $ret = false;
  201. } elseif ($yea > $nay) {
  202. $ret = true;
  203. } elseif ($nay > $yea) {
  204. $ret = false;
  205. } else {
  206. // There's a tie, no action needed.
  207. }
  208. return $ret;
  209. }
  210. // If the code gets here, there was a problem - no hooks, etc.
  211. return NULL;
  212. }
  213. /**
  214. * Do not use, use checkForJavascript() instead.
  215. *
  216. * This function checks whether the user's USER_AGENT is known to
  217. * be broken. If so, returns true and the plugin is invisible to the
  218. * offending browser.
  219. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  220. *
  221. * @return bool whether this browser properly supports JavaScript
  222. * @deprecated use checkForJavascript() since 1.5.1
  223. */
  224. function soupNazi(){
  225. return !checkForJavascript();
  226. }
  227. /**
  228. * Check if plugin is enabled
  229. * @param string $plugin_name plugin name
  230. * @since 1.5.1
  231. * @return boolean
  232. */
  233. function is_plugin_enabled($plugin_name) {
  234. global $plugins;
  235. /**
  236. * check if variable is empty. if var is not set, php empty
  237. * returns true without error notice.
  238. *
  239. * then check if it is an array
  240. */
  241. if (empty($plugins) || ! is_array($plugins))
  242. return false;
  243. if ( in_array($plugin_name,$plugins) ) {
  244. return true;
  245. } else {
  246. return false;
  247. }
  248. }
  249. /**
  250. * Check a plugin's version.
  251. *
  252. * Returns TRUE if the given plugin is installed,
  253. * activated and is at minimum version $a.$b.$c.
  254. * If any one of those conditions fails, FALSE
  255. * will be returned (careful of plugins that are
  256. * sufficiently versioned but are not activated).
  257. *
  258. * By overriding the default value of $force_inclusion,
  259. * this function will attempt to grab versioning
  260. * information from the given plugin even if it
  261. * is not activated (plugin still has to be
  262. * unpackaged and set in place in the plugins
  263. * directory). Use with care - some plugins
  264. * might break SquirrelMail when this is used.
  265. *
  266. * Note that this function assumes plugin
  267. * versioning is consistently applied in the same
  268. * fashion that SquirrelMail versions are, with the
  269. * exception that an applicable SquirrelMail
  270. * version may be appended to the version number
  271. * (which will be ignored herein). That is, plugin
  272. * version number schemes are expected in the following
  273. * format: 1.2.3, or 1.2.3-1.4.0.
  274. *
  275. * Any characters after the third number are discarded,
  276. * so formats such as the following will also work,
  277. * although extra information about beta versions can
  278. * possibly confuse the desired results of the version
  279. * check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
  280. *
  281. * @since 1.5.2
  282. *
  283. * @param string plugin_name name of the plugin to
  284. * check; must precisely
  285. * match the plugin
  286. * directory name
  287. * @param int a major version number
  288. * @param int b minor version number
  289. * @param int c release number
  290. * @param bool force_inclusion try to get version info
  291. * for plugins not activated?
  292. * (default FALSE)
  293. *
  294. * @return bool
  295. *
  296. */
  297. function check_plugin_version($plugin_name,
  298. $a = 0, $b = 0, $c = 0,
  299. $force_inclusion = FALSE)
  300. {
  301. $info_function = $plugin_name . '_info';
  302. $version_function = $plugin_name . '_version';
  303. $plugin_info = array();
  304. $plugin_version = FALSE;
  305. // first attempt to find the plugin info function, wherein
  306. // the plugin version should be available
  307. //
  308. if (function_exists($info_function))
  309. $plugin_info = $info_function();
  310. else if ($force_inclusion
  311. && file_exists(SM_PATH . 'plugins/' . $plugin_name . '/setup.php'))
  312. {
  313. include_once(SM_PATH . 'plugins/' . $plugin_name . '/setup.php');
  314. if (function_exists($info_function))
  315. $plugin_info = $info_function();
  316. }
  317. if (!empty($plugin_info['version']))
  318. $plugin_version = $plugin_info['version'];
  319. // otherwise, look for older version function
  320. //
  321. if (!$plugin_version && function_exists($version_function))
  322. $plugin_version = $version_function();
  323. if (!$plugin_version) return FALSE;
  324. // now massage version number into something we understand
  325. //
  326. $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
  327. '', $plugin_version),
  328. '.');
  329. $plugin_version = explode('.', $plugin_version);
  330. if (!isset($plugin_version[0])) $plugin_version[0] = 0;
  331. if (!isset($plugin_version[1])) $plugin_version[1] = 0;
  332. if (!isset($plugin_version[2])) $plugin_version[2] = 0;
  333. // sm_print_r($plugin_version);
  334. // now test the version number
  335. //
  336. if ($plugin_version[0] < $a ||
  337. ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
  338. ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))
  339. return FALSE;
  340. return TRUE;
  341. }