plugin.php 12 KB

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