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.

178 lines
5.3 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, $http_response, $opts=[]) {
  10. $html = $http_response['body'];
  11. $url = $http_response['url'];
  12. $result = [
  13. 'data' => [
  14. 'type' => 'unknown',
  15. ],
  16. 'url' => $url,
  17. 'code' => $http_response['code'],
  18. 'html' => $html,
  19. ];
  20. // attempt to parse the page as HTML
  21. $doc = new DOMDocument();
  22. if (empty($html)) {
  23. $html=' '; // ugly hack to make DOMDocument happy
  24. };
  25. @$doc->loadHTML(self::toHtmlEntities($html));
  26. if(!$doc) {
  27. return [
  28. 'error' => 'invalid_content',
  29. 'error_description' => 'The document could not be parsed as HTML'
  30. ];
  31. }
  32. $xpath = new DOMXPath($doc);
  33. // Check for meta http equiv and replace the status code if present
  34. foreach($xpath->query('//meta[translate(@http-equiv,\'STATUS\',\'status\')=\'status\']') as $el) {
  35. $equivStatus = ''.$el->getAttribute('content');
  36. if($equivStatus && is_string($equivStatus)) {
  37. if(preg_match('/^(\d+)/', $equivStatus, $match)) {
  38. $result['code'] = (int)$match[1];
  39. }
  40. }
  41. }
  42. // If the URL has a fragment ID, find the DOM starting at that node and parse it instead
  43. $fragment = parse_url($url, PHP_URL_FRAGMENT);
  44. if($fragment) {
  45. $fragElement = self::xPathGetElementById($xpath, $fragment);
  46. if($fragElement) {
  47. $html = $doc->saveHTML($fragElement);
  48. $foundFragment = true;
  49. } else {
  50. $foundFragment = false;
  51. }
  52. }
  53. $includeMF1 = true;
  54. if(isset($opts['include-mf1']) && $opts['include-mf1'] == false)
  55. $includeMF1 = false;
  56. $mf2 = \Mf2\parse($html, $url, $includeMF1);
  57. $canonical = false;
  58. if(isset($mf2['rels']['canonical'][0]))
  59. $canonical = $mf2['rels']['canonical'][0];
  60. // Check for a rel=alternate link to a Microformats JSON representation, and use that instead
  61. if(isset($mf2['rel-urls'])) {
  62. $alternates = [
  63. 'mf2' => [],
  64. 'as2' => [],
  65. ];
  66. foreach($mf2['rel-urls'] as $relurl => $reltype) {
  67. if(isset($reltype['type'])) {
  68. if(in_array('alternate', $reltype['rels']) && $reltype['type'] == 'application/mf2+json') {
  69. $alternates['mf2'][] = $relurl;
  70. }
  71. if(in_array('alternate', $reltype['rels']) && $reltype['type'] == 'application/activity+json') {
  72. $alternates['as2'][] = $relurl;
  73. }
  74. }
  75. }
  76. if(count($alternates['mf2'])) {
  77. // Fetch and parse the MF2 JSON link
  78. $relurl = $alternates['mf2'][0];
  79. $jsonpage = $http->get($relurl, [
  80. 'Accept' => 'application/mf2+json,application/json'
  81. ]);
  82. // Skip and fall back to parsing the HTML if anything about this request fails
  83. if(!$jsonpage['error'] && $jsonpage['body']) {
  84. $jsondata = json_decode($jsonpage['body'], true);
  85. if($jsondata) {
  86. $jsonpage['body'] = $jsondata;
  87. $data = Formats\Mf2::parse($jsonpage, $http, $opts);
  88. if($data && is_array($data) && isset($data['data']['type'])) {
  89. $data['url'] = $relurl;
  90. $data['source-format'] = 'mf2+json';
  91. return $data;
  92. }
  93. }
  94. }
  95. }
  96. if(count($alternates['as2'])) {
  97. $relurl = $alternates['as2'][0];
  98. // Fetch and parse the ActivityStreams JSON link
  99. $jsonpage = $http->get($relurl, [
  100. 'Accept' => 'application/activity+json,application/json'
  101. ]);
  102. // Skip and fall back to parsing the HTML if anything about this request fails
  103. if(!$jsonpage['error'] && $jsonpage['body']) {
  104. $jsondata = json_decode($jsonpage['body'],true);
  105. if($jsondata) {
  106. $jsonpage['body'] = $jsondata;
  107. $data = Formats\ActivityStreams::parse($jsonpage, $http, $opts);
  108. if($data && is_array($data) && isset($data['data']['type'])) {
  109. $data['url'] = $relurl;
  110. $data['source-format'] = 'activity+json';
  111. return $data;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. // Now start pulling in the data from the page. Start by looking for microformats2
  118. if($mf2 && count($mf2['items']) > 0) {
  119. $http_response['body'] = $mf2;
  120. $data = Formats\Mf2::parse($http_response, $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. if($canonical) {
  134. $result['data']['rels'] = [
  135. 'canonical' => $canonical,
  136. ];
  137. }
  138. return $result;
  139. }
  140. private static function toHtmlEntities($input) {
  141. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  142. }
  143. private static function xPathGetElementById($xpath, $id) {
  144. $element = null;
  145. foreach($xpath->query("//*[@id='$id']") as $el) {
  146. $element = $el;
  147. }
  148. return $element;
  149. }
  150. }