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.

208 lines
5.9 KiB

5 years ago
  1. <?php
  2. namespace p3k\XRay;
  3. class Fetcher {
  4. private $http;
  5. public function __construct($http) {
  6. $this->http = $http;
  7. }
  8. public function fetch($url, $opts=[]) {
  9. if($opts == false) $opts = [];
  10. if(isset($opts['timeout']))
  11. $this->http->set_timeout($opts['timeout']);
  12. if(isset($opts['max_redirects']))
  13. $this->http->set_max_redirects($opts['max_redirects']);
  14. // Attempt some basic URL validation
  15. $scheme = parse_url($url, PHP_URL_SCHEME);
  16. if(!in_array($scheme, ['http','https'])) {
  17. return [
  18. 'error_code' => 400,
  19. 'error' => 'invalid_url',
  20. 'error_description' => 'Only http and https URLs are supported'
  21. ];
  22. }
  23. $host = parse_url($url, PHP_URL_HOST);
  24. if(!$host) {
  25. return [
  26. 'error_code' => 400,
  27. 'error' => 'invalid_url',
  28. 'error_description' => 'The URL provided was not valid'
  29. ];
  30. }
  31. $url = normalize_url($url);
  32. $host = parse_url($url, PHP_URL_HOST);
  33. // Check if this is a Twitter URL and use the API
  34. if(Formats\Twitter::matches_host($url)) {
  35. return $this->_fetch_tweet($url, $opts);
  36. }
  37. // Check if this is a Facebook URL and use the API
  38. if(Formats\Facebook::matches_host($url)) {
  39. return $this->_fetch_facebook($url, $opts);
  40. }
  41. // Transform the HTML GitHub URL into an GitHub API request and fetch the API response
  42. if(Formats\GitHub::matches_host($url)) {
  43. return $this->_fetch_github($url, $opts);
  44. }
  45. // Check if this is a Hackernews URL and use the API
  46. if(Formats\Hackernews::matches($url)) {
  47. return Formats\Hackernews::fetch($this->http, $url, $opts);
  48. }
  49. // All other URLs are fetched normally
  50. // Special-case appspot.com URLs to not follow redirects.
  51. // https://cloud.google.com/appengine/docs/php/urlfetch/
  52. if(!should_follow_redirects($url)) {
  53. $this->http->set_max_redirects(0);
  54. $this->http->set_transport(new \p3k\HTTP\Stream());
  55. } else {
  56. $this->http->set_transport(new \p3k\HTTP\Curl());
  57. }
  58. $headers = [];
  59. $headers[] = 'Accept: application/mf2+json, application/activity+json, text/html, application/json, application/xml, text/xml';
  60. if(isset($opts['token']))
  61. $headers[] = 'Authorization: Bearer ' . $opts['token'];
  62. $result = $this->http->get($url, $headers);
  63. if($result['error']) {
  64. return [
  65. 'error' => $result['error'],
  66. 'error_description' => $result['error_description'],
  67. 'url' => $result['url'],
  68. 'code' => $result['code'],
  69. ];
  70. }
  71. // Show an error if the content type returned is not a recognized type
  72. $format = null;
  73. if(isset($result['headers']['Content-Type']) && is_string($result['headers']['Content-Type'])) {
  74. $type = new MediaType($result['headers']['Content-Type']);
  75. $format = $type->format;
  76. }
  77. if(!$format ||
  78. !in_array($format, ['html', 'json', 'xml'])) {
  79. return [
  80. 'error' => 'invalid_content',
  81. 'error_description' => 'The server did not return a recognized content type',
  82. 'content_type' => isset($result['headers']['Content-Type']) ? $result['headers']['Content-Type'] : null,
  83. 'url' => $result['url'],
  84. 'code' => $result['code']
  85. ];
  86. }
  87. if(trim($result['body']) == '') {
  88. if($result['code'] == 410) {
  89. // 410 Gone responses are valid and should not return an error
  90. return [
  91. 'data' => [
  92. 'type' => 'deleted'
  93. ],
  94. 'url' => $result['url'],
  95. 'code' => $result['code']
  96. ];
  97. }
  98. return [
  99. 'error' => 'no_content',
  100. 'error_description' => 'We did not get a response body when fetching the URL',
  101. 'url' => $result['url'],
  102. 'code' => $result['code']
  103. ];
  104. }
  105. // Check for HTTP 401/403
  106. if($result['code'] == 401) {
  107. return [
  108. 'error' => 'unauthorized',
  109. 'error_description' => 'The URL returned "HTTP 401 Unauthorized"',
  110. 'url' => $result['url'],
  111. 'code' => $result['code']
  112. ];
  113. }
  114. if($result['code'] == 403) {
  115. return [
  116. 'error' => 'forbidden',
  117. 'error_description' => 'The URL returned "HTTP 403 Forbidden"',
  118. 'url' => $result['url'],
  119. 'code' => $result['code']
  120. ];
  121. }
  122. // If the original URL had a fragment, include it in the final URL
  123. if(($fragment=parse_url($url, PHP_URL_FRAGMENT)) && !parse_url($result['url'], PHP_URL_FRAGMENT)) {
  124. $result['url'] .= '#'.$fragment;
  125. }
  126. return [
  127. 'url' => $result['url'],
  128. 'body' => $result['body'],
  129. 'code' => $result['code'],
  130. ];
  131. }
  132. private function _fetch_tweet($url, $opts) {
  133. $fields = ['twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret'];
  134. $creds = [];
  135. foreach($fields as $f) {
  136. if(isset($opts[$f]))
  137. $creds[$f] = $opts[$f];
  138. }
  139. if(count($creds) < 4) {
  140. return [
  141. 'error_code' => 400,
  142. 'error' => 'missing_parameters',
  143. 'error_description' => 'All 4 Twitter credentials must be included in the request'
  144. ];
  145. }
  146. return Formats\Twitter::fetch($url, $creds);
  147. }
  148. private function _fetch_facebook($url, $opts) {
  149. $fields = ['facebook_app_id','facebook_app_secret'];
  150. $creds = [];
  151. foreach($fields as $f) {
  152. if(isset($opts[$f]))
  153. $creds[$f] = $opts[$f];
  154. }
  155. if(count($creds) < 2) {
  156. return [
  157. 'error_code' => 400,
  158. 'error' => 'missing_parameters',
  159. 'error_description' => 'Both Facebook credentials must be included in the request'
  160. ];
  161. }
  162. // TODO: Question, should I do this like Twitter or like Github?
  163. return Formats\Facebook::fetch($url, $creds);
  164. }
  165. private function _fetch_github($url, $opts) {
  166. $fields = ['github_access_token'];
  167. $creds = [];
  168. foreach($fields as $f) {
  169. if(isset($opts[$f]))
  170. $creds[$f] = $opts[$f];
  171. }
  172. return Formats\GitHub::fetch($this->http, $url, $creds);
  173. }
  174. }