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.

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