From 2549e5068e795986601d10b77ab0f5bc57108eb1 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 14 Aug 2005 17:50:35 +0000 Subject: - Fixed the filter module. --- modules/filter.module | 315 ++++++++++++++++++++++++++++++++++++++++++- modules/filter/filter.module | 315 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 628 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/filter.module b/modules/filter.module index 61da3f7e0..ce6128592 100644 --- a/modules/filter.module +++ b/modules/filter.module @@ -705,4 +705,317 @@ function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE) { } // Store in cache with a minimum expiration time of 1 day. - i \ No newline at end of file + if ($cache) { + cache_set($id, $text, time() + (60 * 60 * 24)); + } + } + else { + $text = message_na(); + } + + return $text; +} + +/** + * Generate a selector for choosing a format in a form. + * + * @param $name + * The internal name used to refer to the form element. + * @param $value + * The ID of the format that is currently selected. + * @return + * HTML for the form element. + */ +function filter_form($name = 'format', $value = FILTER_FORMAT_DEFAULT) { + if ($value == FILTER_FORMAT_DEFAULT) { + $value = variable_get('filter_default_format', 1); + } + $formats = filter_formats(); + + $extra = l(t('More information about formatting options'), 'filter/tips'); + + if (count($formats) > 1) { + // Multiple formats available: display radio buttons with tips. + $output = ''; + foreach ($formats as $format) { + $tips = _filter_tips($format->format, false); + + // TODO: get support for block-level radios so the
is not output? + $output .= '
'; + $output .= ''; + $output .= theme('filter_tips', $tips); + $output .= '
'; + } + $group = theme('form_element', NULL, $output, $extra, NULL, _form_get_error($name)); + return form_group_collapsible(t('Input format'), $group, TRUE); + } + else { + // Only one format available: use a hidden form item and only show tips. + $format = array_shift($formats); + $output = form_hidden($name, $format->format); + $tips = _filter_tips(variable_get('filter_default_format', 1), false); + $output .= form_item(t('Formatting guidelines'), theme('filter_tips', $tips, false, $extra), $extra); + return $output; + } +} + +/** + * Returns true if the user is allowed to access this format. + */ +function filter_access($format) { + if (user_access('administer filters') || ($format == FILTER_FORMAT_DEFAULT) || ($format == variable_get('filter_default_format', 1))) { + return true; + } + else { + $formats = filter_formats(); + return isset($formats[$format]); + } +} +/** + * @} End of "Filtering functions". + */ + +/** + * Menu callback; show a page with long filter tips. + */ +function filter_tips_long() { + $format = arg(2); + if ($format) { + $output = theme('filter_tips', _filter_tips($format, true), true); + } + else { + $output = theme('filter_tips', _filter_tips(-1, true), true); + } + return $output; +} + +/** + * Helper function for fetching filter tips. + */ +function _filter_tips($format, $long = false) { + if ($format == -1) { + $formats = filter_formats(); + } + else { + $formats = array(db_fetch_object(db_query("SELECT * FROM {filter_formats} WHERE format = %d", $format))); + } + + $tips = array(); + + foreach ($formats as $format) { + $filters = filter_list_format($format->format); + + $tips[$format->name] = array(); + foreach ($filters as $id => $filter) { + if ($tip = module_invoke($filter->module, 'filter_tips', $filter->delta, $format->format, $long)) { + $tips[$format->name][] = array('tip' => $tip, 'id' => $id); + } + } + } + + return $tips; +} + +/** + * Format a set of filter tips. + * + * @ingroup themeable + */ +function theme_filter_tips($tips, $long = false, $extra = '') { + $output = ''; + + $multiple = count($tips) > 1; + if ($multiple) { + $output = t('Input formats') .':'; + } + + if (count($tips)) { + if ($multiple) { + $output .= ''; + } + } + + return $output; +} + +/** + * @name Standard filters + * @{ + * Filters implemented by the filter.module. + */ + +/** + * Implementation of hook_filter(). Contains a basic set of essential filters. + * - HTML filter: + * Validates user-supplied HTML, transforming it as necessary. + * - PHP evaluator: + * Executes PHP code. + * - Line break converter: + * Converts newlines into paragraph and break tags. + */ +function filter_filter($op, $delta = 0, $format = -1, $text = '') { + switch ($op) { + case 'list': + return array(0 => t('HTML filter'), 1 => t('PHP evaluator'), 2 => t('Line break converter')); + + case 'no cache': + return $delta == 1; // No caching for the PHP evaluator. + + case 'description': + switch ($delta) { + case 0: + return t('Allows you to restrict if users can post HTML and which tags to filter out.'); + case 1: + return t('Runs a piece of PHP code. The usage of this filter should be restricted to administrators only!'); + case 2: + return t('Converts line breaks into HTML (i.e. <br> and <p> tags).'); + default: + return; + } + + case 'process': + switch ($delta) { + case 0: + return _filter_html($text, $format); + case 1: + return drupal_eval($text); + case 2: + return _filter_autop($text); + default: + return $text; + } + + case 'settings': + switch ($delta) { + case 0: + return _filter_html_settings($format); + default: + return; + } + + default: + return $text; + } +} + +/** + * Settings for the HTML filter. + */ +function _filter_html_settings($format) { + $group = form_radios(t('Filter HTML tags'), "filter_html_$format", variable_get("filter_html_$format", FILTER_HTML_STRIP), array(FILTER_HTML_STRIP => t('Strip tags'), FILTER_HTML_ESCAPE => t('Escape tags')), t('How to deal with HTML tags in user-contributed content. If set to "Strip tags", dangerous tags are removed (see below). If set to "Escape tags", all HTML is escaped and presented as it was typed.')); + $group .= form_textfield(t('Allowed HTML tags'), "allowed_html_$format", variable_get("allowed_html_$format", '