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.

187 lines
5.1 KiB

  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. if(isset($opts['token']))
  60. $headers[] = 'Authorization: Bearer ' . $opts['token'];
  61. $result = $this->http->get($url, $headers);
  62. if($result['error']) {
  63. return [
  64. 'error' => $result['error'],
  65. 'error_description' => $result['error_description'],
  66. 'url' => $result['url'],
  67. 'code' => $result['code'],
  68. ];
  69. }
  70. if(trim($result['body']) == '') {
  71. if($result['code'] == 410) {
  72. // 410 Gone responses are valid and should not return an error
  73. return $this->respond($response, 200, [
  74. 'data' => [
  75. 'type' => 'unknown'
  76. ],
  77. 'url' => $result['url'],
  78. 'code' => $result['code']
  79. ]);
  80. }
  81. return [
  82. 'error' => 'no_content',
  83. 'error_description' => 'We did not get a response body when fetching the URL',
  84. 'url' => $result['url'],
  85. 'code' => $result['code']
  86. ];
  87. }
  88. // Check for HTTP 401/403
  89. if($result['code'] == 401) {
  90. return [
  91. 'error' => 'unauthorized',
  92. 'error_description' => 'The URL returned "HTTP 401 Unauthorized"',
  93. 'url' => $result['url'],
  94. 'code' => $result['code']
  95. ];
  96. }
  97. if($result['code'] == 403) {
  98. return [
  99. 'error' => 'forbidden',
  100. 'error_description' => 'The URL returned "HTTP 403 Forbidden"',
  101. 'url' => $result['url'],
  102. 'code' => $result['code']
  103. ];
  104. }
  105. // If the original URL had a fragment, include it in the final URL
  106. if(($fragment=parse_url($url, PHP_URL_FRAGMENT)) && !parse_url($result['url'], PHP_URL_FRAGMENT)) {
  107. $result['url'] .= '#'.$fragment;
  108. }
  109. return [
  110. 'url' => $result['url'],
  111. 'body' => $result['body'],
  112. 'code' => $result['code'],
  113. ];
  114. }
  115. private function _fetch_tweet($url, $opts) {
  116. $fields = ['twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret'];
  117. $creds = [];
  118. foreach($fields as $f) {
  119. if(isset($opts[$f]))
  120. $creds[$f] = $opts[$f];
  121. }
  122. if(count($creds) < 4) {
  123. return [
  124. 'error_code' => 400,
  125. 'error' => 'missing_parameters',
  126. 'error_description' => 'All 4 Twitter credentials must be included in the request'
  127. ];
  128. }
  129. return Formats\Twitter::fetch($url, $creds);
  130. }
  131. private function _fetch_facebook($url, $opts) {
  132. $fields = ['facebook_app_id','facebook_app_secret'];
  133. $creds = [];
  134. foreach($fields as $f) {
  135. if(isset($opts[$f]))
  136. $creds[$f] = $opts[$f];
  137. }
  138. if(count($creds) < 2) {
  139. return [
  140. 'error_code' => 400,
  141. 'error' => 'missing_parameters',
  142. 'error_description' => 'Both Facebook credentials must be included in the request'
  143. ];
  144. }
  145. // TODO: Question, should I do this like Twitter or like Github?
  146. return Formats\Facebook::fetch($url, $creds);
  147. }
  148. private function _fetch_github($url, $opts) {
  149. $fields = ['github_access_token'];
  150. $creds = [];
  151. foreach($fields as $f) {
  152. if(isset($opts[$f]))
  153. $creds[$f] = $opts[$f];
  154. }
  155. return Formats\GitHub::fetch($this->http, $url, $creds);
  156. }
  157. }