/html/html.php

https://github.com/mmakaay/Modules · PHP · 114 lines · 41 code · 12 blank · 61 comment · 5 complexity · f0e1555ff84b0ade68e85f58947bb70e MD5 · raw file

  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2009 Phorum Development Team //
  5. // http://www.phorum.org //
  6. // //
  7. // This program is free software. You can redistribute it and/or modify //
  8. // it under the terms of either the current Phorum License (viewable at //
  9. // phorum.org) or the Phorum License that was distributed with this file //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  14. // //
  15. // You should have received a copy of the Phorum License //
  16. // along with this program. //
  17. // //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. if(!defined("PHORUM")) return;
  20. // The path to the HTML Purifier stand alone distribution.
  21. define('HTMLPURIFIER_PATH',dirname(__FILE__).'/htmlpurifier-4.0.0-standalone');
  22. // Load HTMLPurifier.
  23. ini_set(
  24. 'include_path',
  25. ini_get('include_path') . PATH_SEPARATOR .
  26. HTMLPURIFIER_PATH.'/standalone'
  27. );
  28. require HTMLPURIFIER_PATH.'/HTMLPurifier.standalone.php';
  29. // HTML Phorum Mod
  30. function phorum_mod_html_format($data)
  31. {
  32. global $PHORUM;
  33. static $purifier;
  34. static $config;
  35. // Setup the HTML Purifier object.
  36. if (!$purifier)
  37. {
  38. $cache = $PHORUM['cache'] . '/html_purifier';
  39. if (!file_exists($cache) && !mkdir($cache)) trigger_error(
  40. "The HTML module is unable to create the HTML Purifier " .
  41. "cache directory \"$cache\". Fix the cause of this problem " .
  42. "or disable the HTML module in the Phorum admin interface.",
  43. E_USER_ERROR
  44. );
  45. // Determine the doctype to use.
  46. $doctype = isset($PHORUM['mod_html']['doctype'])
  47. ? $PHORUM['mod_html']['doctype']
  48. : 'XHTML 1.0 Transitional';
  49. // Bootstrap the HTML Purifier.
  50. $config = HTMLPurifier_Config::createDefault();
  51. $config->set('Core.Encoding', $PHORUM['DATA']['CHARSET']);
  52. $config->set('HTML.Doctype', $doctype);
  53. $config->set('Cache.SerializerPath', $cache);
  54. $purifier = new HTMLPurifier($config);
  55. }
  56. $PHORUM = $GLOBALS["PHORUM"];
  57. foreach($data as $message_id => $message)
  58. {
  59. if(isset($message["body"]))
  60. {
  61. $body = $message["body"];
  62. // pull out the phorum breaks
  63. $body = str_replace("<phorum break>", "", $body);
  64. // Protect against poisoned null byte XSS attacks
  65. // (MSIE does not protect itself against these, so we have
  66. // to take care of that).
  67. str_replace("\0", "", $body);
  68. // restore tags where Phorum has killed them
  69. $body = preg_replace("!&lt;(\/*[a-z].*?)&gt;!si", "<$1>", $body);
  70. // restore escaped & and "
  71. $body = str_replace("&amp;", "&", $body);
  72. $body = str_replace("&quot;", '"', $body);
  73. // run the message through HTML Purifier for stripping out
  74. // possible XSS risks.
  75. $body = $purifier->purify($body);
  76. // put the phorum breaks back
  77. $body = str_replace("\n", "<phorum break>\n", $body);
  78. // strip any <phorum break> tags that got inside certain
  79. // blocks like tables (to prevent <table><br/><tr> like
  80. // code) and pre/xmp (newlines are shown, even without
  81. // <br/> tags).
  82. $block_tags="table|pre|xmp";
  83. preg_match_all("!(<($block_tags).*?>).+?(</($block_tags).*?>)!ims", $body, $matches);
  84. foreach($matches[0] as $block){
  85. $newblock=str_replace("<phorum break>", "", $block);
  86. $body=str_replace($block, $newblock, $body);
  87. }
  88. $data[$message_id]["body"] = $body;
  89. }
  90. }
  91. return $data;
  92. }
  93. ?>