filter.api.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Filter module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Define content filters.
  12. *
  13. * User submitted content is passed through a group of filters before it is
  14. * output in HTML, in order to remove insecure or unwanted parts, correct or
  15. * enhance the formatting, transform special keywords, etc. A group of filters
  16. * is referred to as a "text format". Administrators can create as many text
  17. * formats as needed. Individual filters can be enabled and configured
  18. * differently for each text format.
  19. *
  20. * This hook is invoked by filter_get_filters() and allows modules to register
  21. * input filters they provide.
  22. *
  23. * Filtering is a two-step process. First, the content is 'prepared' by calling
  24. * the 'prepare callback' function for every filter. The purpose of the 'prepare
  25. * callback' is to escape HTML-like structures. For example, imagine a filter
  26. * which allows the user to paste entire chunks of programming code without
  27. * requiring manual escaping of special HTML characters like < or &. If the
  28. * programming code were left untouched, then other filters could think it was
  29. * HTML and change it. For many filters, the prepare step is not necessary.
  30. *
  31. * The second step is the actual processing step. The result from passing the
  32. * text through all the filters' prepare steps gets passed to all the filters
  33. * again, this time with the 'process callback' function. The process callbacks
  34. * should then actually change the content: transform URLs into hyperlinks,
  35. * convert smileys into images, etc.
  36. *
  37. * For performance reasons content is only filtered once; the result is stored
  38. * in the cache table and retrieved from the cache the next time the same piece
  39. * of content is displayed. If a filter's output is dynamic, it can override the
  40. * cache mechanism, but obviously this should be used with caution: having one
  41. * filter that does not support caching in a particular text format disables
  42. * caching for the entire format, not just for one filter.
  43. *
  44. * Beware of the filter cache when developing your module: it is advised to set
  45. * your filter to 'cache' => FALSE while developing, but be sure to remove that
  46. * setting if it's not needed, when you are no longer in development mode.
  47. *
  48. * @return
  49. * An associative array of filters, whose keys are internal filter names,
  50. * which should be unique and therefore prefixed with the name of the module.
  51. * Each value is an associative array describing the filter, with the
  52. * following elements (all are optional except as noted):
  53. * - title: (required) An administrative summary of what the filter does.
  54. * - description: Additional administrative information about the filter's
  55. * behavior, if needed for clarification.
  56. * - settings callback: The name of a function that returns configuration form
  57. * elements for the filter. See callback_filter_settings() for details.
  58. * - default settings: An associative array containing default settings for
  59. * the filter, to be applied when the filter has not been configured yet.
  60. * - prepare callback: The name of a function that escapes the content before
  61. * the actual filtering happens. See callback_filter_prepare() for
  62. * details.
  63. * - process callback: (required) The name the function that performs the
  64. * actual filtering. See callback_filter_process() for details.
  65. * - cache (default TRUE): Specifies whether the filtered text can be cached.
  66. * Note that setting this to FALSE makes the entire text format not
  67. * cacheable, which may have an impact on the site's overall performance.
  68. * See filter_format_allowcache() for details.
  69. * - tips callback: The name of a function that returns end-user-facing filter
  70. * usage guidelines for the filter. See callback_filter_tips() for
  71. * details.
  72. * - weight: A default weight for the filter in new text formats.
  73. *
  74. * @see filter_example.module
  75. * @see hook_filter_info_alter()
  76. */
  77. function hook_filter_info() {
  78. $filters['filter_html'] = array(
  79. 'title' => t('Limit allowed HTML tags'),
  80. 'description' => t('Allows you to restrict the HTML tags the user can use. It will also remove harmful content such as JavaScript events, JavaScript URLs and CSS styles from those tags that are not removed.'),
  81. 'process callback' => '_filter_html',
  82. 'settings callback' => '_filter_html_settings',
  83. 'default settings' => array(
  84. 'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>',
  85. 'filter_html_help' => 1,
  86. 'filter_html_nofollow' => 0,
  87. ),
  88. 'tips callback' => '_filter_html_tips',
  89. );
  90. $filters['filter_autop'] = array(
  91. 'title' => t('Convert line breaks'),
  92. 'description' => t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'),
  93. 'process callback' => '_filter_autop',
  94. 'tips callback' => '_filter_autop_tips',
  95. );
  96. return $filters;
  97. }
  98. /**
  99. * Perform alterations on filter definitions.
  100. *
  101. * @param $info
  102. * Array of information on filters exposed by hook_filter_info()
  103. * implementations.
  104. */
  105. function hook_filter_info_alter(&$info) {
  106. // Replace the PHP evaluator process callback with an improved
  107. // PHP evaluator provided by a module.
  108. $info['php_code']['process callback'] = 'my_module_php_evaluator';
  109. // Alter the default settings of the URL filter provided by core.
  110. $info['filter_url']['default settings'] = array(
  111. 'filter_url_length' => 100,
  112. );
  113. }
  114. /**
  115. * @} End of "addtogroup hooks".
  116. */
  117. /**
  118. * Provide a settings form for filter settings.
  119. *
  120. * Callback for hook_filter_info().
  121. *
  122. * This callback function is used to provide a settings form for filter
  123. * settings, for filters that need settings on a per-text-format basis. This
  124. * function should return the form elements for the settings; the filter
  125. * module will take care of saving the settings in the database.
  126. *
  127. * If the filter's behavior depends on an extensive list and/or external data
  128. * (e.g. a list of smileys, a list of glossary terms), then the filter module
  129. * can choose to provide a separate, global configuration page rather than
  130. * per-text-format settings. In that case, the settings callback function
  131. * should provide a link to the separate settings page.
  132. *
  133. * @param $form
  134. * The prepopulated form array of the filter administration form.
  135. * @param $form_state
  136. * The state of the (entire) configuration form.
  137. * @param $filter
  138. * The filter object containing the current settings for the given format,
  139. * in $filter->settings.
  140. * @param $format
  141. * The format object being configured.
  142. * @param $defaults
  143. * The default settings for the filter, as defined in 'default settings' in
  144. * hook_filter_info(). These should be combined with $filter->settings to
  145. * define the form element defaults.
  146. * @param $filters
  147. * The complete list of filter objects that are enabled for the given format.
  148. *
  149. * @return
  150. * An array of form elements defining settings for the filter. Array keys
  151. * should match the array keys in $filter->settings and $defaults.
  152. *
  153. * @ingroup callbacks
  154. */
  155. function callback_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
  156. $filter->settings += $defaults;
  157. $elements = array();
  158. $elements['nofollow'] = array(
  159. '#type' => 'checkbox',
  160. '#title' => t('Add rel="nofollow" to all links'),
  161. '#default_value' => $filter->settings['nofollow'],
  162. );
  163. return $elements;
  164. }
  165. /**
  166. * Provide prepared text with special characters escaped.
  167. *
  168. * Callback for hook_filter_info().
  169. *
  170. * See hook_filter_info() for a description of the filtering process. Filters
  171. * should not use the 'prepare callback' step for anything other than escaping,
  172. * because that would short-circuit the control the user has over the order in
  173. * which filters are applied.
  174. *
  175. * @param $text
  176. * The text string to be filtered.
  177. * @param $filter
  178. * The filter object containing settings for the given format.
  179. * @param $format
  180. * The text format object assigned to the text to be filtered.
  181. * @param $langcode
  182. * The language code of the text to be filtered.
  183. * @param $cache
  184. * A Boolean indicating whether the filtered text is going to be cached in
  185. * {cache_filter}.
  186. * @param $cache_id
  187. * The ID of the filtered text in {cache_filter}, if $cache is TRUE.
  188. *
  189. * @return
  190. * The prepared, escaped text.
  191. *
  192. * @ingroup callbacks
  193. */
  194. function callback_filter_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
  195. // Escape <code> and </code> tags.
  196. $text = preg_replace('|<code>(.+?)</code>|se', "[codefilter_code]$1[/codefilter_code]", $text);
  197. return $text;
  198. }
  199. /**
  200. * Provide text filtered to conform to the supplied format.
  201. *
  202. * Callback for hook_filter_info().
  203. *
  204. * See hook_filter_info() for a description of the filtering process. This step
  205. * is where the filter actually transforms the text.
  206. *
  207. * @param $text
  208. * The text string to be filtered.
  209. * @param $filter
  210. * The filter object containing settings for the given format.
  211. * @param $format
  212. * The text format object assigned to the text to be filtered.
  213. * @param $langcode
  214. * The language code of the text to be filtered.
  215. * @param $cache
  216. * A Boolean indicating whether the filtered text is going to be cached in
  217. * {cache_filter}.
  218. * @param $cache_id
  219. * The ID of the filtered text in {cache_filter}, if $cache is TRUE.
  220. *
  221. * @return
  222. * The filtered text.
  223. *
  224. * @ingroup callbacks
  225. */
  226. function callback_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  227. $text = preg_replace('|\[codefilter_code\](.+?)\[/codefilter_code\]|se', "<pre>$1</pre>", $text);
  228. return $text;
  229. }
  230. /**
  231. * Return help text for a filter.
  232. *
  233. * Callback for hook_filter_info().
  234. *
  235. * A filter's tips should be informative and to the point. Short tips are
  236. * preferably one-liners.
  237. *
  238. * @param $filter
  239. * An object representing the filter.
  240. * @param $format
  241. * An object representing the text format the filter is contained in.
  242. * @param $long
  243. * Whether this callback should return a short tip to display in a form
  244. * (FALSE), or whether a more elaborate filter tips should be returned for
  245. * theme_filter_tips() (TRUE).
  246. *
  247. * @return
  248. * Translated text to display as a tip.
  249. *
  250. * @ingroup callbacks
  251. */
  252. function callback_filter_tips($filter, $format, $long) {
  253. if ($long) {
  254. return t('Lines and paragraphs are automatically recognized. The &lt;br /&gt; line break, &lt;p&gt; paragraph and &lt;/p&gt; close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.');
  255. }
  256. else {
  257. return t('Lines and paragraphs break automatically.');
  258. }
  259. }
  260. /**
  261. * @addtogroup hooks
  262. * @{
  263. */
  264. /**
  265. * Perform actions when a new text format has been created.
  266. *
  267. * @param $format
  268. * The format object of the format being updated.
  269. *
  270. * @see hook_filter_format_update()
  271. * @see hook_filter_format_disable()
  272. */
  273. function hook_filter_format_insert($format) {
  274. mymodule_cache_rebuild();
  275. }
  276. /**
  277. * Perform actions when a text format has been updated.
  278. *
  279. * This hook allows modules to act when a text format has been updated in any
  280. * way. For example, when filters have been reconfigured, disabled, or
  281. * re-arranged in the text format.
  282. *
  283. * @param $format
  284. * The format object of the format being updated.
  285. *
  286. * @see hook_filter_format_insert()
  287. * @see hook_filter_format_disable()
  288. */
  289. function hook_filter_format_update($format) {
  290. mymodule_cache_rebuild();
  291. }
  292. /**
  293. * Perform actions when a text format has been disabled.
  294. *
  295. * @param $format
  296. * The format object of the format being disabled.
  297. *
  298. * @see hook_filter_format_insert()
  299. * @see hook_filter_format_update()
  300. */
  301. function hook_filter_format_disable($format) {
  302. mymodule_cache_rebuild();
  303. }
  304. /**
  305. * @} End of "addtogroup hooks".
  306. */