plugin.php 12 KB

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