plugin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * Get a plugin's version.
  251. *
  252. * Determines and returns a plugin's version.
  253. *
  254. * By default, the desired plugin must be currently
  255. * activated, and if it is not, this function will
  256. * return FALSE. By overriding the default value
  257. * of $force_inclusion, this function will attempt
  258. * to grab versioning information from the given
  259. * plugin even if it is not activated (plugin still
  260. * has to be unpackaged and set in place in the
  261. * plugins directory). Use with care - some plugins
  262. * might break SquirrelMail when this is used.
  263. *
  264. * By turning on the $do_parse argument, the version
  265. * string will be parsed by SquirrelMail into a
  266. * SquirrelMail-compatible version string (such as
  267. * "1.2.3") if it is not already.
  268. *
  269. * Note that this assumes plugin versioning is
  270. * consistently applied in the same fashion that
  271. * SquirrelMail versions are, with the exception that
  272. * an applicable SquirrelMail version may be appended
  273. * to the version number (which will be ignored herein).
  274. * That is, plugin version number schemes are expected
  275. * in the following format: 1.2.3, or 1.2.3-1.4.0.
  276. *
  277. * Any characters after the third version number
  278. * indicating things such as beta or release candidate
  279. * versions are discarded, so formats such as the
  280. * following will also work, although extra information
  281. * about beta versions can possibly confuse the desired
  282. * results of the version check: 1.2.3-beta4, 1.2.3.RC2,
  283. * and so forth.
  284. *
  285. * @since 1.5.2
  286. *
  287. * @param string plugin_name name of the plugin to
  288. * check; must precisely
  289. * match the plugin
  290. * directory name
  291. * @param bool force_inclusion try to get version info
  292. * for plugins not activated?
  293. * (default FALSE)
  294. * @param bool do_parse return the plugin version
  295. * in SquirrelMail-compatible
  296. * format (default FALSE)
  297. *
  298. * @return mixed The plugin version string if found, otherwise,
  299. * boolean FALSE is returned indicating that no
  300. * version information could be found for the plugin.
  301. *
  302. */
  303. function get_plugin_version($plugin_name, $force_inclusion = FALSE, $do_parse = FALSE)
  304. {
  305. $info_function = $plugin_name . '_info';
  306. $version_function = $plugin_name . '_version';
  307. $plugin_info = array();
  308. $plugin_version = FALSE;
  309. // first attempt to find the plugin info function, wherein
  310. // the plugin version should be available
  311. //
  312. if (function_exists($info_function))
  313. $plugin_info = $info_function();
  314. else if ($force_inclusion
  315. && file_exists(SM_PATH . 'plugins/' . $plugin_name . '/setup.php'))
  316. {
  317. include_once(SM_PATH . 'plugins/' . $plugin_name . '/setup.php');
  318. if (function_exists($info_function))
  319. $plugin_info = $info_function();
  320. }
  321. if (!empty($plugin_info['version']))
  322. $plugin_version = $plugin_info['version'];
  323. // otherwise, look for older version function
  324. //
  325. if (!$plugin_version && function_exists($version_function))
  326. $plugin_version = $version_function();
  327. if ($plugin_version && $do_parse)
  328. {
  329. // massage version number into something we understand
  330. //
  331. // the first regexp strips everything and anything that follows
  332. // the first occurance of a non-digit (or non decimal point), so
  333. // beware that putting letters in the middle of a version string
  334. // will effectively truncate the version string right there (but
  335. // this also just helps remove the SquirrelMail version part off
  336. // of versions such as "1.2.3-1.4.4")
  337. //
  338. // the second regexp just strips out non-digits/non-decimal points
  339. // (and might be redundant(?))
  340. //
  341. // the regexps are wrapped in a trim that makes sure the version
  342. // does not start or end with a decimal point
  343. //
  344. $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'),
  345. '', $plugin_version),
  346. '.');
  347. }
  348. return $plugin_version;
  349. }
  350. /**
  351. * Check a plugin's version.
  352. *
  353. * Returns TRUE if the given plugin is installed,
  354. * activated and is at minimum version $a.$b.$c.
  355. * If any one of those conditions fails, FALSE
  356. * will be returned (careful of plugins that are
  357. * sufficiently versioned but are not activated).
  358. *
  359. * By overriding the default value of $force_inclusion,
  360. * this function will attempt to grab versioning
  361. * information from the given plugin even if it
  362. * is not activated (the plugin still has to be
  363. * unpackaged and set in place in the plugins
  364. * directory). Use with care - some plugins
  365. * might break SquirrelMail when this is used.
  366. *
  367. * Note that this function assumes plugin
  368. * versioning is consistently applied in the same
  369. * fashion that SquirrelMail versions are, with the
  370. * exception that an applicable SquirrelMail
  371. * version may be appended to the version number
  372. * (which will be ignored herein). That is, plugin
  373. * version number schemes are expected in the following
  374. * format: 1.2.3, or 1.2.3-1.4.0.
  375. *
  376. * Any characters after the third number indicating
  377. * things such as beta or release candidate versions
  378. * are discarded, so formats such as the following
  379. * will also work, although extra information about
  380. * beta versions can possibly confuse the desired results
  381. * of the version check: 1.2.3-beta4, 1.2.3.RC2, and so forth.
  382. *
  383. * @since 1.5.2
  384. *
  385. * @param string plugin_name name of the plugin to
  386. * check; must precisely
  387. * match the plugin
  388. * directory name
  389. * @param int a major version number
  390. * @param int b minor version number
  391. * @param int c release number
  392. * @param bool force_inclusion try to get version info
  393. * for plugins not activated?
  394. * (default FALSE)
  395. *
  396. * @return bool
  397. *
  398. */
  399. function check_plugin_version($plugin_name,
  400. $a = 0, $b = 0, $c = 0,
  401. $force_inclusion = FALSE)
  402. {
  403. $plugin_version = get_plugin_version($plugin_name, $force_inclusion, TRUE);
  404. if (!$plugin_version) return FALSE;
  405. // split the version string into sections delimited by
  406. // decimal points, and make sure we have three sections
  407. //
  408. $plugin_version = explode('.', $plugin_version);
  409. if (!isset($plugin_version[0])) $plugin_version[0] = 0;
  410. if (!isset($plugin_version[1])) $plugin_version[1] = 0;
  411. if (!isset($plugin_version[2])) $plugin_version[2] = 0;
  412. // sm_print_r($plugin_version);
  413. // now test the version number
  414. //
  415. if ($plugin_version[0] < $a ||
  416. ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
  417. ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))
  418. return FALSE;
  419. return TRUE;
  420. }