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