plugin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. // each plugin can call additional hooks, so need
  79. // to make sure the current hook name is accurate
  80. // again after each plugin has finished
  81. //
  82. $currentHookName = $name;
  83. }
  84. }
  85. }
  86. $currentHookName = '';
  87. return $ret;
  88. }
  89. /**
  90. * This function executes a hook that allows for an arbitrary
  91. * return value from each plugin that will be merged into one
  92. * array (or one string if all return values are strings) and
  93. * returned to the core hook location.
  94. *
  95. * Note that unlike PHP's array_merge function, matching array keys
  96. * will not overwrite each other, instead, values under such keys
  97. * will be concatenated if they are both strings, or merged if they
  98. * are arrays (in the same (non-overwrite) manner recursively).
  99. *
  100. * Plugins returning non-arrays (strings, objects, etc) will have
  101. * their output added to the end of the ultimate return array,
  102. * unless ALL values returned are strings, in which case one string
  103. * with all returned strings concatenated together is returned
  104. * (unless $force_array is TRUE).
  105. *
  106. * If any plugin on this hook wants to modify the $args
  107. * plugin parameter, it simply has to use call-by-reference
  108. * syntax in the hook function that it has registered for the
  109. * current hook. Note that this is in addition to (entirely
  110. * independent of) the return value for this hook.
  111. *
  112. * @param string $name Name of hook being executed
  113. * @param mixed $args A single value or an array of arguments
  114. * that are to be passed to all plugins
  115. * operating off the hook being called.
  116. * Note that this argument is passed by
  117. * reference thus it is liable to be
  118. * changed after the hook completes.
  119. * @param boolean $force_array When TRUE, guarantees the return
  120. * value will ALWAYS be an array,
  121. * (simple strings will be forced
  122. * into a one-element array).
  123. * When FALSE, behavior is as
  124. * described above (OPTIONAL;
  125. * default behavior is to return
  126. * mixed - array or string).
  127. *
  128. * @return mixed the merged return arrays or strings of each
  129. * plugin on this hook.
  130. *
  131. */
  132. function concat_hook_function($name, &$args, $force_array=FALSE) {
  133. global $squirrelmail_plugin_hooks, $currentHookName;
  134. $currentHookName = $name;
  135. $ret = '';
  136. if (isset($squirrelmail_plugin_hooks[$name])
  137. && is_array($squirrelmail_plugin_hooks[$name])) {
  138. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  139. use_plugin($plugin_name);
  140. if (function_exists($function)) {
  141. $plugin_ret = $function($args);
  142. if (!empty($plugin_ret)) {
  143. $ret = sqm_array_merge($ret, $plugin_ret);
  144. }
  145. // each plugin can call additional hooks, so need
  146. // to make sure the current hook name is accurate
  147. // again after each plugin has finished
  148. //
  149. $currentHookName = $name;
  150. }
  151. }
  152. }
  153. if ($force_array && is_string($ret)) {
  154. $ret = array($ret);
  155. }
  156. $currentHookName = '';
  157. return $ret;
  158. }
  159. /**
  160. * This function is used for hooks which are to return true or
  161. * false. If $priority is > 0, any one or more trues will override
  162. * any falses. If $priority < 0, then one or more falses will
  163. * override any trues.
  164. * Priority 0 means majority rules. Ties will be broken with $tie
  165. *
  166. * If any plugin on this hook wants to modify the $args
  167. * plugin parameter, it simply has to use call-by-reference
  168. * syntax in the hook function that it has registered for the
  169. * current hook. Note that this is in addition to (entirely
  170. * independent of) the return value for this hook.
  171. *
  172. * @param string $name The hook name
  173. * @param mixed $args A single value or an array of arguments
  174. * that are to be passed to all plugins
  175. * operating off the hook being called.
  176. * Note that this argument is passed by
  177. * reference thus it is liable to be
  178. * changed after the hook completes.
  179. * @param int $priority See explanation above
  180. * @param boolean $tie See explanation above
  181. *
  182. * @return boolean The result of the function
  183. *
  184. */
  185. function boolean_hook_function($name, &$args, $priority=0, $tie=false) {
  186. global $squirrelmail_plugin_hooks, $currentHookName;
  187. $yea = 0;
  188. $nay = 0;
  189. $ret = $tie;
  190. if (isset($squirrelmail_plugin_hooks[$name]) &&
  191. is_array($squirrelmail_plugin_hooks[$name])) {
  192. /* Loop over the plugins that registered the hook */
  193. $currentHookName = $name;
  194. foreach ($squirrelmail_plugin_hooks[$name] as $plugin_name => $function) {
  195. use_plugin($plugin_name);
  196. if (function_exists($function)) {
  197. $ret = $function($args);
  198. if ($ret) {
  199. $yea++;
  200. } else {
  201. $nay++;
  202. }
  203. // each plugin can call additional hooks, so need
  204. // to make sure the current hook name is accurate
  205. // again after each plugin has finished
  206. //
  207. $currentHookName = $name;
  208. }
  209. }
  210. $currentHookName = '';
  211. /* Examine the aftermath and assign the return value appropriately */
  212. if (($priority > 0) && ($yea)) {
  213. $ret = true;
  214. } elseif (($priority < 0) && ($nay)) {
  215. $ret = false;
  216. } elseif ($yea > $nay) {
  217. $ret = true;
  218. } elseif ($nay > $yea) {
  219. $ret = false;
  220. } else {
  221. // There's a tie, no action needed.
  222. }
  223. return $ret;
  224. }
  225. // If the code gets here, there was a problem - no hooks, etc.
  226. return NULL;
  227. }
  228. /**
  229. * Do not use, use checkForJavascript() instead.
  230. *
  231. * This function checks whether the user's USER_AGENT is known to
  232. * be broken. If so, returns true and the plugin is invisible to the
  233. * offending browser.
  234. * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  235. *
  236. * @return bool whether this browser properly supports JavaScript
  237. * @deprecated use checkForJavascript() since 1.5.1
  238. */
  239. function soupNazi(){
  240. return !checkForJavascript();
  241. }
  242. /**
  243. * Check if plugin is enabled
  244. * @param string $plugin_name plugin name
  245. * @since 1.5.1
  246. * @return boolean
  247. */
  248. function is_plugin_enabled($plugin_name) {
  249. global $plugins;
  250. /**
  251. * check if variable is empty. if var is not set, php empty
  252. * returns true without error notice.
  253. *
  254. * then check if it is an array
  255. */
  256. if (empty($plugins) || ! is_array($plugins))
  257. return false;
  258. if ( in_array($plugin_name,$plugins) ) {
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. }
  264. /**
  265. * Get a plugin's version.
  266. *
  267. * Determines and returns a plugin's version.
  268. *
  269. * By default, the desired plugin must be currently
  270. * activated, and if it is not, this function will
  271. * return FALSE. By overriding the default value
  272. * of $force_inclusion, this function will attempt
  273. * to grab versioning information from the given
  274. * plugin even if it is not activated (plugin still
  275. * has to be unpackaged and set in place in the
  276. * plugins directory). Use with care - some plugins
  277. * might break SquirrelMail when this is used.
  278. *
  279. * By turning on the $do_parse argument, the version
  280. * string will be parsed by SquirrelMail into a
  281. * SquirrelMail-compatible version string (such as
  282. * "1.2.3") if it is not already.
  283. *
  284. * Note that this assumes plugin versioning is
  285. * consistently applied in the same fashion that
  286. * SquirrelMail versions are, with the exception that
  287. * an applicable SquirrelMail version may be appended
  288. * to the version number (which will be ignored herein).
  289. * That is, plugin version number schemes are expected
  290. * in the following format: 1.2.3, or 1.2.3-1.4.0.
  291. *
  292. * Any characters after the third version number
  293. * indicating things such as beta or release candidate
  294. * versions are discarded, so formats such as the
  295. * following will also work, although extra information
  296. * about beta versions can possibly confuse the desired
  297. * results of the version check: 1.2.3-beta4, 1.2.3.RC2,
  298. * and so forth.
  299. *
  300. * @since 1.5.2
  301. *
  302. * @param string plugin_name name of the plugin to
  303. * check; must precisely
  304. * match the plugin
  305. * directory name
  306. * @param bool force_inclusion try to get version info
  307. * for plugins not activated?
  308. * (default FALSE)
  309. * @param bool do_parse return the plugin version
  310. * in SquirrelMail-compatible
  311. * format (default FALSE)
  312. *
  313. * @return mixed The plugin version string if found, otherwise,
  314. * boolean FALSE is returned indicating that no
  315. * version information could be found for the plugin.
  316. *
  317. */
  318. function get_plugin_version($plugin_name, $force_inclusion = FALSE, $do_parse = FALSE)
  319. {
  320. $info_function = $plugin_name . '_info';
  321. $version_function = $plugin_name . '_version';
  322. $plugin_info = array();
  323. $plugin_version = FALSE;
  324. // first attempt to find the plugin info function, wherein
  325. // the plugin version should be available
  326. //
  327. if (function_exists($info_function))
  328. $plugin_info = $info_function();
  329. else if ($force_inclusion
  330. && file_exists(SM_PATH . 'plugins/' . $plugin_name . '/setup.php'))
  331. {
  332. include_once(SM_PATH . 'plugins/' . $plugin_name . '/setup.php');
  333. if (function_exists($info_function))
  334. $plugin_info = $info_function();
  335. }
  336. if (!empty($plugin_info['version']))
  337. $plugin_version = $plugin_info['version'];
  338. // otherwise, look for older version function
  339. //
  340. if (!$plugin_version && function_exists($version_function))
  341. $plugin_version = $version_function();
  342. if ($plugin_version && $do_parse)
  343. {
  344. // massage version number into something we understand
  345. //
  346. // the first regexp strips everything and anything that follows
  347. // the first occurance of a non-digit (or non decimal point), so
  348. // beware that putting letters in the middle of a version string
  349. // will effectively truncate the version string right there (but
  350. // this also just helps remove the SquirrelMail version part off
  351. // of versions such as "1.2.3-1.4.4")
  352. //
  353. // the second regexp just strips out non-digits/non-decimal points
  354. // (and might be redundant(?))
  355. //
  356. // the regexps are wrapped in a trim that makes sure the version
  357. // does not start or end with a decimal point
  358. //
  359. $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
  360. '', $plugin_version),
  361. '.');
  362. }
  363. return $plugin_version;
  364. }
  365. /**
  366. * Check a plugin's version.
  367. *
  368. * Returns TRUE if the given plugin is installed,
  369. * activated and is at minimum version $a.$b.$c.
  370. * If any one of those conditions fails, FALSE
  371. * will be returned (careful of plugins that are
  372. * sufficiently versioned but are not activated).
  373. *
  374. * By overriding the default value of $force_inclusion,
  375. * this function will attempt to grab versioning
  376. * information from the given plugin even if it
  377. * is not activated (the plugin still has to be
  378. * unpackaged and set in place in the plugins
  379. * directory). Use with care - some plugins
  380. * might break SquirrelMail when this is used.
  381. *
  382. * Note that this function assumes plugin
  383. * versioning is consistently applied in the same
  384. * fashion that SquirrelMail versions are, with the
  385. * exception that an applicable SquirrelMail
  386. * version may be appended to the version number
  387. * (which will be ignored herein). That is, plugin
  388. * version number schemes are expected in the following
  389. * format: 1.2.3, or 1.2.3-1.4.0.
  390. *
  391. * Any characters after the third number indicating
  392. * things such as beta or release candidate versions
  393. * are discarded, so formats such as the following
  394. * will also work, although extra information about
  395. * beta versions can possibly confuse the desired results
  396. * of the version check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
  397. *
  398. * @since 1.5.2
  399. *
  400. * @param string plugin_name name of the plugin to
  401. * check; must precisely
  402. * match the plugin
  403. * directory name
  404. * @param int a major version number
  405. * @param int b minor version number
  406. * @param int c release number
  407. * @param bool force_inclusion try to get version info
  408. * for plugins not activated?
  409. * (default FALSE)
  410. *
  411. * @return bool
  412. *
  413. */
  414. function check_plugin_version($plugin_name,
  415. $a = 0, $b = 0, $c = 0,
  416. $force_inclusion = FALSE)
  417. {
  418. $plugin_version = get_plugin_version($plugin_name, $force_inclusion, TRUE);
  419. if (!$plugin_version) return FALSE;
  420. // split the version string into sections delimited by
  421. // decimal points, and make sure we have three sections
  422. //
  423. $plugin_version = explode('.', $plugin_version);
  424. if (!isset($plugin_version[0])) $plugin_version[0] = 0;
  425. if (!isset($plugin_version[1])) $plugin_version[1] = 0;
  426. if (!isset($plugin_version[2])) $plugin_version[2] = 0;
  427. // sm_print_r($plugin_version);
  428. // now test the version number
  429. //
  430. if ($plugin_version[0] < $a ||
  431. ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
  432. ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))
  433. return FALSE;
  434. return TRUE;
  435. }