plugin.php 11 KB

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