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.

162 lines
4.4 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. // Transform the HTML GitHub URL into an GitHub API request and fetch the API response
  38. if(Formats\GitHub::matches_host($url)) {
  39. return $this->_fetch_github($url, $opts);
  40. }
  41. // Check if this is a Hackernews URL and use the API
  42. if(Formats\Hackernews::matches($url)) {
  43. return Formats\Hackernews::fetch($this->http, $url, $opts);
  44. }
  45. // All other URLs are fetched normally
  46. // Special-case appspot.com URLs to not follow redirects.
  47. // https://cloud.google.com/appengine/docs/php/urlfetch/
  48. if(!should_follow_redirects($url)) {
  49. $this->http->set_max_redirects(0);
  50. $this->http->set_transport(new \p3k\HTTP\Stream());
  51. } else {
  52. $this->http->set_transport(new \p3k\HTTP\Curl());
  53. }
  54. $headers = [];
  55. if(isset($opts['token']))
  56. $headers[] = 'Authorization: Bearer ' . $opts['token'];
  57. $result = $this->http->get($url, $headers);
  58. if($result['error']) {
  59. return [
  60. 'error' => $result['error'],
  61. 'error_description' => $result['error_description'],
  62. 'url' => $result['url'],
  63. 'code' => $result['code'],
  64. ];
  65. }
  66. if(trim($result['body']) == '') {
  67. if($result['code'] == 410) {
  68. // 410 Gone responses are valid and should not return an error
  69. return $this->respond($response, 200, [
  70. 'data' => [
  71. 'type' => 'unknown'
  72. ],
  73. 'url' => $result['url'],
  74. 'code' => $result['code']
  75. ]);
  76. }
  77. return [
  78. 'error' => 'no_content',
  79. 'error_description' => 'We did not get a response body when fetching the URL',
  80. 'url' => $result['url'],
  81. 'code' => $result['code']
  82. ];
  83. }
  84. // Check for HTTP 401/403
  85. if($result['code'] == 401) {
  86. return [
  87. 'error' => 'unauthorized',
  88. 'error_description' => 'The URL returned "HTTP 401 Unauthorized"',
  89. 'url' => $result['url'],
  90. 'code' => $result['code']
  91. ];
  92. }
  93. if($result['code'] == 403) {
  94. return [
  95. 'error' => 'forbidden',
  96. 'error_description' => 'The URL returned "HTTP 403 Forbidden"',
  97. 'url' => $result['url'],
  98. 'code' => $result['code']
  99. ];
  100. }
  101. // If the original URL had a fragment, include it in the final URL
  102. if(($fragment=parse_url($url, PHP_URL_FRAGMENT)) && !parse_url($result['url'], PHP_URL_FRAGMENT)) {
  103. $result['url'] .= '#'.$fragment;
  104. }
  105. return [
  106. 'url' => $result['url'],
  107. 'body' => $result['body'],
  108. 'code' => $result['code'],
  109. ];
  110. }
  111. private function _fetch_tweet($url, $opts) {
  112. $fields = ['twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret'];
  113. $creds = [];
  114. foreach($fields as $f) {
  115. if(isset($opts[$f]))
  116. $creds[$f] = $opts[$f];
  117. }
  118. if(count($creds) < 4) {
  119. return [
  120. 'error_code' => 400,
  121. 'error' => 'missing_parameters',
  122. 'error_description' => 'All 4 Twitter credentials must be included in the request'
  123. ];
  124. }
  125. return Formats\Twitter::fetch($url, $creds);
  126. }
  127. private function _fetch_github($url, $opts) {
  128. $fields = ['github_access_token'];
  129. $creds = [];
  130. foreach($fields as $f) {
  131. if(isset($opts[$f]))
  132. $creds[$f] = $opts[$f];
  133. }
  134. return Formats\GitHub::fetch($this->http, $url, $creds);
  135. }
  136. }