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.

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