start.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Elgg htmLawed tag filtering.
  4. *
  5. * http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/
  6. *
  7. * @package ElgghtmLawed
  8. */
  9. elgg_register_event_handler('init', 'system', 'htmlawed_init');
  10. /**
  11. * Initialize the htmlawed plugin
  12. */
  13. function htmlawed_init() {
  14. elgg_register_plugin_hook_handler('validate', 'input', 'htmlawed_filter_tags', 1);
  15. $lib = elgg_get_plugins_path() . "htmlawed/vendors/htmLawed/htmLawed.php";
  16. elgg_register_library('htmlawed', $lib);
  17. elgg_register_plugin_hook_handler('unit_test', 'system', 'htmlawed_test');
  18. }
  19. /**
  20. * htmLawed filtering of data
  21. *
  22. * Called on the 'validate', 'input' plugin hook
  23. *
  24. * Triggers the 'config', 'htmlawed' plugin hook so that plugins can change
  25. * htmlawed's configuration. For information on configuraton options, see
  26. * http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/htmLawed_README.htm#s2.2
  27. *
  28. * @param string $hook Hook name
  29. * @param string $type The type of hook
  30. * @param mixed $result Data to filter
  31. * @param array $params Not used
  32. * @return mixed
  33. */
  34. function htmlawed_filter_tags($hook, $type, $result, $params = null) {
  35. $var = $result;
  36. elgg_load_library('htmlawed');
  37. $htmlawed_config = array(
  38. // seems to handle about everything we need.
  39. 'safe' => true,
  40. // remove comments/CDATA instead of converting to text
  41. 'comment' => 1,
  42. 'cdata' => 1,
  43. 'deny_attribute' => 'class, on*',
  44. 'hook_tag' => 'htmlawed_tag_post_processor',
  45. 'schemes' => '*:http,https,ftp,news,mailto,rtsp,teamspeak,gopher,mms,callto',
  46. // apparent this doesn't work.
  47. // 'style:color,cursor,text-align,font-size,font-weight,font-style,border,margin,padding,float'
  48. );
  49. // add nofollow to all links on output
  50. if (!elgg_in_context('input')) {
  51. $htmlawed_config['anti_link_spam'] = array('/./', '');
  52. }
  53. $htmlawed_config = elgg_trigger_plugin_hook('config', 'htmlawed', null, $htmlawed_config);
  54. if (!is_array($var)) {
  55. $result = htmLawed($var, $htmlawed_config);
  56. } else {
  57. array_walk_recursive($var, 'htmLawedArray', $htmlawed_config);
  58. $result = $var;
  59. }
  60. return $result;
  61. }
  62. /**
  63. * wrapper function for htmlawed for handling arrays
  64. */
  65. function htmLawedArray(&$v, $k, $htmlawed_config) {
  66. $v = htmLawed($v, $htmlawed_config);
  67. }
  68. /**
  69. * Post processor for tags in htmlawed
  70. *
  71. * This runs after htmlawed has filtered. It runs for each tag and filters out
  72. * style attributes we don't want.
  73. *
  74. * This function triggers the 'allowed_styles', 'htmlawed' plugin hook.
  75. *
  76. * @param string $element The tag element name
  77. * @param array $attributes An array of attributes
  78. * @return string
  79. */
  80. function htmlawed_tag_post_processor($element, $attributes = false) {
  81. if ($attributes === false) {
  82. // This is a closing tag. Prevent further processing to avoid inserting a duplicate tag
  83. return "</${element}>";
  84. }
  85. // this list should be coordinated with the WYSIWYG editor used (tinymce, ckeditor, etc.)
  86. $allowed_styles = array(
  87. 'color', 'cursor', 'text-align', 'vertical-align', 'font-size',
  88. 'font-weight', 'font-style', 'border', 'border-top', 'background-color',
  89. 'border-bottom', 'border-left', 'border-right',
  90. 'margin', 'margin-top', 'margin-bottom', 'margin-left',
  91. 'margin-right', 'padding', 'float', 'text-decoration'
  92. );
  93. $params = array('tag' => $element);
  94. $allowed_styles = elgg_trigger_plugin_hook('allowed_styles', 'htmlawed', $params, $allowed_styles);
  95. // must return something.
  96. $string = '';
  97. foreach ($attributes as $attr => $value) {
  98. if ($attr == 'style') {
  99. $styles = explode(';', $value);
  100. $style_str = '';
  101. foreach ($styles as $style) {
  102. if (!trim($style)) {
  103. continue;
  104. }
  105. list($style_attr, $style_value) = explode(':', trim($style));
  106. $style_attr = trim($style_attr);
  107. $style_value = trim($style_value);
  108. if (in_array($style_attr, $allowed_styles)) {
  109. $style_str .= "$style_attr: $style_value; ";
  110. }
  111. }
  112. if ($style_str) {
  113. $style_str = trim($style_str);
  114. $string .= " style=\"$style_str\"";
  115. }
  116. } else {
  117. $string .= " $attr=\"$value\"";
  118. }
  119. }
  120. // Some WYSIWYG editors do not like tags like <p > so only add a space if needed.
  121. if ($string = trim($string)) {
  122. $string = " $string";
  123. }
  124. $r = "<$element$string>";
  125. return $r;
  126. }
  127. /**
  128. * Runs unit tests for htmlawed
  129. *
  130. * @return array
  131. */
  132. function htmlawed_test($hook, $type, $value, $params) {
  133. $value[] = dirname(__FILE__) . '/tests/tags.php';
  134. return $value;
  135. }