You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.0 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use DOMDocument, DOMXPath;
  4. use HTMLPurifier, HTMLPurifier_Config;
  5. interface iFormat {
  6. public static function matches_host($url);
  7. public static function matches($url);
  8. }
  9. abstract class Format implements iFormat {
  10. protected static function _unknown() {
  11. return [
  12. 'data' => [
  13. 'type' => 'unknown'
  14. ]
  15. ];
  16. }
  17. protected static function _loadHTML($html) {
  18. $doc = new DOMDocument();
  19. @$doc->loadHTML($html);
  20. if(!$doc) {
  21. return [null, null];
  22. }
  23. $xpath = new DOMXPath($doc);
  24. return [$doc, $xpath];
  25. }
  26. protected static function sanitizeHTML($html, $allowImg=true, $baseURL=false) {
  27. $allowed = [
  28. 'a',
  29. 'abbr',
  30. 'b',
  31. 'br',
  32. 'code',
  33. 'del',
  34. 'em',
  35. 'i',
  36. 'q',
  37. 'strike',
  38. 'strong',
  39. 'time',
  40. 'blockquote',
  41. 'pre',
  42. 'p',
  43. 'h1',
  44. 'h2',
  45. 'h3',
  46. 'h4',
  47. 'h5',
  48. 'h6',
  49. 'ul',
  50. 'li',
  51. 'ol',
  52. 'span',
  53. ];
  54. if($allowImg)
  55. $allowed[] = 'img';
  56. $config = HTMLPurifier_Config::createDefault();
  57. $config->set('Cache.DefinitionImpl', null);
  58. $config->set('HTML.AllowedElements', $allowed);
  59. if($baseURL) {
  60. $config->set('URI.MakeAbsolute', true);
  61. $config->set('URI.Base', $baseURL);
  62. }
  63. $def = $config->getHTMLDefinition(true);
  64. // add HTML <time> element
  65. $def->addElement(
  66. 'time',
  67. 'Inline',
  68. 'Inline',
  69. 'Common',
  70. [
  71. 'datetime' => 'Text'
  72. ]
  73. );
  74. /*
  75. // This isn't working right now, not sure why
  76. // http://developers.whatwg.org/the-video-element.html#the-video-element
  77. $def->addElement(
  78. 'video',
  79. 'Block',
  80. 'Optional: (source, Flow) | (Flow, source) | Flow',
  81. 'Common',
  82. [
  83. 'src' => 'URI',
  84. 'type' => 'Text',
  85. 'width' => 'Length',
  86. 'height' => 'Length',
  87. 'poster' => 'URI',
  88. 'preload' => 'Enum#auto,metadata,none',
  89. 'controls' => 'Bool',
  90. ]
  91. );
  92. $def->addElement(
  93. 'source',
  94. 'Block',
  95. 'Flow',
  96. 'Common',
  97. [
  98. 'src' => 'URI',
  99. 'type' => 'Text',
  100. ]
  101. );
  102. */
  103. // Override the allowed classes to only support Microformats2 classes
  104. $def->manager->attrTypes->set('Class', new HTMLPurifier_AttrDef_HTML_Microformats2());
  105. $purifier = new HTMLPurifier($config);
  106. $sanitized = $purifier->purify($html);
  107. $sanitized = str_replace("&#xD;","\r",$sanitized);
  108. return trim($sanitized);
  109. }
  110. // Return a plaintext version of the input HTML
  111. protected static function stripHTML($html) {
  112. $config = HTMLPurifier_Config::createDefault();
  113. $config->set('Cache.DefinitionImpl', null);
  114. $config->set('HTML.AllowedElements', ['br']);
  115. $purifier = new HTMLPurifier($config);
  116. $sanitized = $purifier->purify($html);
  117. $sanitized = str_replace("&#xD;","\r",$sanitized);
  118. $sanitized = html_entity_decode($sanitized);
  119. return trim(str_replace(['<br>','<br />'],"\n", $sanitized));
  120. }
  121. }