wysiwyg_filter.module 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $defaults["rule_bypass_$rule"] = 0;
  33. endforeach;
  34. $filters['wysiwyg'] = array(
  35. 'title' => t('WYSIWYG Filter'),
  36. 'description' => t('Allows you to restrict whether users can post HTML and which tags and attributes per HTML tag to filter out.'),
  37. 'process callback' => 'wysiwyg_filter_filter_wysiwyg_process',
  38. 'settings callback' => 'wysiwyg_filter_filter_wysiwyg_settings',
  39. 'tips callback' => 'wysiwyg_filter_filter_wysiwyg_tips',
  40. 'default settings' => $defaults
  41. );
  42. return $filters;
  43. }
  44. /**
  45. * Implements hook_filter_FILTER_tips().
  46. */
  47. function wysiwyg_filter_filter_wysiwyg_tips($filter, $format, $long = FALSE) {
  48. // Load common functions.
  49. module_load_include('inc', 'wysiwyg_filter');
  50. $filter_options = wysiwyg_filter_get_filter_options($format->format, $filter->settings);
  51. $tips = array();
  52. if (!empty($filter_options['valid_elements'])) {
  53. $tags = array();
  54. foreach (array_keys($filter_options['valid_elements']) as $tag) {
  55. $tags[] = '<' . $tag . '>';
  56. }
  57. $tips[] = t('Allowed HTML tags: @tags', array('@tags' => implode(' ', $tags)));
  58. }
  59. if (!empty($filter_options['style_properties'])) {
  60. $tips[] = t('Allowed Style properties: @properties', array('@properties' => implode(', ', array_keys($filter_options['style_properties']))));
  61. }
  62. if (!empty($tips)) {
  63. return implode('<br />', $tips);
  64. }
  65. }
  66. /**
  67. * Implementation of wysiwyg_editor_settings_alter().
  68. */
  69. function wysiwyg_filter_wysiwyg_editor_settings_alter(&$editor_settings, $context) {
  70. // Provide the valid_elements option to TinyMCE editors, only if the WYSIWYG
  71. // Filter is enabled in the input format related to the current given context.
  72. if ($context['profile']->editor == 'tinymce'):
  73. // first get the filters and their settings
  74. if (isset($context['profile']->format)):
  75. $format_name = $context['profile']->format;
  76. $filters = filter_list_format($format_name);
  77. if($filters && array_key_exists('wysiwyg', $filters)):
  78. $filter = $filters['wysiwyg'];
  79. if($filter->status != 0) {
  80. $settings = $filter->settings;
  81. $editor_settings['valid_elements'] = preg_replace('#\s+#', '', $settings['valid_elements']);
  82. }
  83. endif;
  84. endif;
  85. endif;
  86. }