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.

170 lines
5.2 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use HTMLPurifier, HTMLPurifier_Config;
  4. use DOMDocument, DOMXPath;
  5. use p3k\XRay\Formats;
  6. class HTML extends Format {
  7. public static function matches_host($url) { return true; }
  8. public static function matches($url) { return true; }
  9. public static function parse($http, $html, $url, $opts=[]) {
  10. $result = [
  11. 'data' => [
  12. 'type' => 'unknown',
  13. ],
  14. 'url' => $url,
  15. ];
  16. // attempt to parse the page as HTML
  17. $doc = new DOMDocument();
  18. @$doc->loadHTML(self::toHtmlEntities($html));
  19. if(!$doc) {
  20. return [
  21. 'error' => 'invalid_content',
  22. 'error_description' => 'The document could not be parsed as HTML'
  23. ];
  24. }
  25. $xpath = new DOMXPath($doc);
  26. // Check for meta http equiv and replace the status code if present
  27. foreach($xpath->query('//meta[translate(@http-equiv,\'STATUS\',\'status\')=\'status\']') as $el) {
  28. $equivStatus = ''.$el->getAttribute('content');
  29. if($equivStatus && is_string($equivStatus)) {
  30. if(preg_match('/^(\d+)/', $equivStatus, $match)) {
  31. $result['code'] = (int)$match[1];
  32. }
  33. }
  34. }
  35. // If a target parameter was provided, make sure a link to it exists on the page
  36. if(isset($opts['target'])) {
  37. $target = $opts['target'];
  38. $found = [];
  39. if($target) {
  40. $found = self::findLinksInDocument($xpath, $target);
  41. }
  42. if(!$found) {
  43. return [
  44. 'error' => 'no_link_found',
  45. 'error_description' => 'The source document does not have a link to the target URL',
  46. 'code' => isset($result['code']) ? $result['code'] : 200,
  47. 'url' => $url
  48. ];
  49. }
  50. }
  51. // If the URL has a fragment ID, find the DOM starting at that node and parse it instead
  52. $fragment = parse_url($url, PHP_URL_FRAGMENT);
  53. if($fragment) {
  54. $fragElement = self::xPathGetElementById($xpath, $fragment);
  55. if($fragElement) {
  56. $html = $doc->saveHTML($fragElement);
  57. $foundFragment = true;
  58. } else {
  59. $foundFragment = false;
  60. }
  61. }
  62. $mf2 = \mf2\Parse($html, $url);
  63. // Check for a rel=alternate link to a Microformats JSON representation, and use that instead
  64. if(isset($mf2['rel-urls'])) {
  65. $alternates = [
  66. 'mf2' => [],
  67. 'as2' => [],
  68. ];
  69. foreach($mf2['rel-urls'] as $relurl => $reltype) {
  70. if(isset($reltype['type'])) {
  71. if(in_array('alternate', $reltype['rels']) && $reltype['type'] == 'application/mf2+json') {
  72. $alternates['mf2'][] = $relurl;
  73. }
  74. if(in_array('alternate', $reltype['rels']) && $reltype['type'] == 'application/activity+json') {
  75. $alternates['as2'][] = $relurl;
  76. }
  77. }
  78. }
  79. if(count($alternates['mf2'])) {
  80. // Fetch and parse the MF2 JSON link
  81. $relurl = $alternates['mf2'][0];
  82. $jsonpage = $http->get($relurl, [
  83. 'Accept' => 'application/mf2+json,application/json'
  84. ]);
  85. // Skip and fall back to parsing the HTML if anything about this request fails
  86. if(!$jsonpage['error'] && $jsonpage['body']) {
  87. $jsondata = json_decode($jsonpage['body'],true);
  88. if($jsondata) {
  89. $data = Formats\Mf2::parse($jsondata, $url, $http, $opts);
  90. if($data && is_array($data) && isset($data['data']['type'])) {
  91. $data['url'] = $relurl;
  92. $data['source-format'] = 'mf2+json';
  93. return $data;
  94. }
  95. }
  96. }
  97. }
  98. if(count($alternates['as2'])) {
  99. $relurl = $alternates['as2'][0];
  100. // Fetch and parse the ActivityStreams JSON link
  101. $jsonpage = $http->get($relurl, [
  102. 'Accept' => 'application/activity+json,application/json'
  103. ]);
  104. // Skip and fall back to parsing the HTML if anything about this request fails
  105. if(!$jsonpage['error'] && $jsonpage['body']) {
  106. $jsondata = json_decode($jsonpage['body'],true);
  107. if($jsondata) {
  108. $data = Formats\ActivityStreams::parse($jsondata, $url, $http, $opts);
  109. if($data && is_array($data) && isset($data['data']['type'])) {
  110. $data['url'] = $relurl;
  111. $data['source-format'] = 'activity+json';
  112. return $data;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. // Now start pulling in the data from the page. Start by looking for microformats2
  119. if($mf2 && count($mf2['items']) > 0) {
  120. $data = Formats\Mf2::parse($mf2, $url, $http, $opts);
  121. if($data) {
  122. $result = array_merge($result, $data);
  123. if($fragment) {
  124. $result['info'] = [
  125. 'found_fragment' => $foundFragment
  126. ];
  127. }
  128. $result['original'] = $html;
  129. $result['url'] = $url; // this will be the effective URL after following redirects
  130. $result['source-format'] = 'mf2+html';
  131. }
  132. }
  133. return $result;
  134. }
  135. private static function toHtmlEntities($input) {
  136. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  137. }
  138. private static function xPathGetElementById($xpath, $id) {
  139. $element = null;
  140. foreach($xpath->query("//*[@id='$id']") as $el) {
  141. $element = $el;
  142. }
  143. return $element;
  144. }
  145. }