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.

175 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, $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. $includeMF1 = true;
  51. if(isset($opts['include-mf1']) && $opts['include-mf1'] == false)
  52. $includeMF1 = false;
  53. $mf2 = \Mf2\parse($html, $url, $includeMF1);
  54. $canonical = false;
  55. if(isset($mf2['rels']['canonical'][0]))
  56. $canonical = $mf2['rels']['canonical'][0];
  57. // Check for a rel=alternate link to a Microformats JSON representation, and use that instead
  58. if(isset($mf2['rel-urls'])) {
  59. $alternates = [
  60. 'mf2' => [],
  61. 'as2' => [],
  62. ];
  63. foreach($mf2['rel-urls'] as $relurl => $reltype) {
  64. if(isset($reltype['type'])) {
  65. if(in_array('alternate', $reltype['rels']) && $reltype['type'] == 'application/mf2+json') {
  66. $alternates['mf2'][] = $relurl;
  67. }
  68. if(in_array('alternate', $reltype['rels']) && $reltype['type'] == 'application/activity+json') {
  69. $alternates['as2'][] = $relurl;
  70. }
  71. }
  72. }
  73. if(count($alternates['mf2'])) {
  74. // Fetch and parse the MF2 JSON link
  75. $relurl = $alternates['mf2'][0];
  76. $jsonpage = $http->get($relurl, [
  77. 'Accept' => 'application/mf2+json,application/json'
  78. ]);
  79. // Skip and fall back to parsing the HTML if anything about this request fails
  80. if(!$jsonpage['error'] && $jsonpage['body']) {
  81. $jsondata = json_decode($jsonpage['body'], true);
  82. if($jsondata) {
  83. $jsonpage['body'] = $jsondata;
  84. $data = Formats\Mf2::parse($jsonpage, $http, $opts);
  85. if($data && is_array($data) && isset($data['data']['type'])) {
  86. $data['url'] = $relurl;
  87. $data['source-format'] = 'mf2+json';
  88. return $data;
  89. }
  90. }
  91. }
  92. }
  93. if(count($alternates['as2'])) {
  94. $relurl = $alternates['as2'][0];
  95. // Fetch and parse the ActivityStreams JSON link
  96. $jsonpage = $http->get($relurl, [
  97. 'Accept' => 'application/activity+json,application/json'
  98. ]);
  99. // Skip and fall back to parsing the HTML if anything about this request fails
  100. if(!$jsonpage['error'] && $jsonpage['body']) {
  101. $jsondata = json_decode($jsonpage['body'],true);
  102. if($jsondata) {
  103. $jsonpage['body'] = $jsondata;
  104. $data = Formats\ActivityStreams::parse($jsonpage, $http, $opts);
  105. if($data && is_array($data) && isset($data['data']['type'])) {
  106. $data['url'] = $relurl;
  107. $data['source-format'] = 'activity+json';
  108. return $data;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // Now start pulling in the data from the page. Start by looking for microformats2
  115. if($mf2 && count($mf2['items']) > 0) {
  116. $http_response['body'] = $mf2;
  117. $data = Formats\Mf2::parse($http_response, $http, $opts);
  118. if($data) {
  119. $result = array_merge($result, $data);
  120. if($fragment) {
  121. $result['info'] = [
  122. 'found_fragment' => $foundFragment
  123. ];
  124. }
  125. $result['original'] = $html;
  126. $result['url'] = $url; // this will be the effective URL after following redirects
  127. $result['source-format'] = 'mf2+html';
  128. }
  129. }
  130. if($canonical) {
  131. $result['data']['rels'] = [
  132. 'canonical' => $canonical,
  133. ];
  134. }
  135. return $result;
  136. }
  137. private static function toHtmlEntities($input) {
  138. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  139. }
  140. private static function xPathGetElementById($xpath, $id) {
  141. $element = null;
  142. foreach($xpath->query("//*[@id='$id']") as $el) {
  143. $element = $el;
  144. }
  145. return $element;
  146. }
  147. }