wysiwyg_filter.module 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @file
  4. * Provides an input filter that allows site administrators configure which
  5. * HTML elements, attributes and style properties are allowed.
  6. */
  7. // todo: more intelligent include policy
  8. // beware: it filter process function in ...page.inc is not included,
  9. // the whole filter is silently ignored. see http://drupal.org/node/1151506
  10. module_load_include('inc', 'wysiwyg_filter', 'wysiwyg_filter.admin');
  11. module_load_include('inc', 'wysiwyg_filter', 'wysiwyg_filter.pages');
  12. /**
  13. * Implements hook_filter_info().
  14. */
  15. function wysiwyg_filter_filter_info() {
  16. // Load common functions.
  17. module_load_include('inc', 'wysiwyg_filter');
  18. $filters = array();
  19. global $base_url;
  20. $parts = parse_url($base_url);
  21. $defaults = array(
  22. 'valid_elements' => wysiwyg_filter_default_valid_elements(),
  23. 'allow_comments' => 0,
  24. 'nofollow_policy' => 'whitelist',
  25. 'nofollow_domains' => array($parts['host']),
  26. );
  27. foreach(wysiwyg_filter_get_style_property_groups() as $group => $stuff):
  28. $defaults["style_$group"] = array();
  29. endforeach;
  30. foreach(wysiwyg_filter_get_advanced_rules() as $rule => $stuff):
  31. $defaults["rule_$rule"] = array();
  32. endforeach;
  33. $filters['wysiwyg'] = array(
  34. 'title' => t('WYSIWYG Filter'),
  35. 'description' => t('Allows you to restrict whether users can post HTML and which tags and attributes per HTML tag to filter out.'),
  36. 'process callback' => 'wysiwyg_filter_filter_wysiwyg_process',
  37. 'settings callback' => 'wysiwyg_filter_filter_wysiwyg_settings',
  38. 'tips callback' => 'wysiwyg_filter_filter_wysiwyg_tips',
  39. 'default settings' => $defaults
  40. );
  41. return $filters;
  42. }
  43. /**
  44. * Implements hook_filter_FILTER_tips().
  45. */
  46. function wysiwyg_filter_filter_wysiwyg_tips($filter, $format, $long = FALSE) {
  47. // Load common functions.
  48. module_load_include('inc', 'wysiwyg_filter');
  49. $filter_options = wysiwyg_filter_get_filter_options($format->format, $filter->settings);
  50. $tips = array();
  51. if (!empty($filter_options['valid_elements'])) {
  52. $tags = array();
  53. foreach (array_keys($filter_options['valid_elements']) as $tag) {
  54. $tags[] = '<' . $tag . '>';
  55. }
  56. $tips[] = t('Allowed HTML tags: @tags', array('@tags' => implode(' ', $tags)));
  57. }
  58. if (!empty($filter_options['style_properties'])) {
  59. $tips[] = t('Allowed Style properties: @properties', array('@properties' => implode(', ', array_keys($filter_options['style_properties']))));
  60. }
  61. if (!empty($tips)) {
  62. return implode('<br />', $tips);
  63. }
  64. }
  65. /**
  66. * Implementation of wysiwyg_editor_settings_alter().
  67. */
  68. function wysiwyg_filter_wysiwyg_editor_settings_alter(&$editor_settings, $context) {
  69. // Provide the valid_elements option to TinyMCE editors, only if the WYSIWYG
  70. // Filter is enabled in the input format related to the current given context.
  71. if ($context['profile']->editor == 'tinymce'):
  72. // first get the filters and their settings
  73. if (isset($context['profile']->format)):
  74. $format_name = $context['profile']->format;
  75. $filters = filter_list_format($format_name);
  76. if($filters && array_key_exists('wysiwyg', $filters)):
  77. $filter = $filters['wysiwyg'];
  78. if($filter->status != 0) {
  79. $settings = $filter->settings;
  80. $editor_settings['valid_elements'] = preg_replace('#\s+#', '', $settings['valid_elements']);
  81. }
  82. endif;
  83. endif;
  84. endif;
  85. }