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.

125 lines
3.5 KiB

  1. <?php
  2. namespace p3k\XRay;
  3. use p3k\XRay\Formats;
  4. class Feeds {
  5. private $http;
  6. public function __construct($http) {
  7. $this->http = $http;
  8. }
  9. public function find($url, $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. $scheme = parse_url($url, PHP_URL_SCHEME);
  15. if(!in_array($scheme, ['http','https'])) {
  16. return [
  17. 'error' => 'invalid_url',
  18. 'error_description' => 'Only http and https URLs are supported'
  19. ];
  20. }
  21. $host = parse_url($url, PHP_URL_HOST);
  22. if(!$host) {
  23. return [
  24. 'error' => 'invalid_url',
  25. 'error_description' => 'The URL provided was not valid'
  26. ];
  27. }
  28. $url = normalize_url($url);
  29. $result = $this->http->get($url);
  30. if(isset($result['error']) && $result['error']) {
  31. return [
  32. 'error' => $result['error'],
  33. 'error_description' => $result['error_description']
  34. ];
  35. }
  36. $body = $result['body'];
  37. $feeds = [];
  38. // First check the content type of the response
  39. $contentType = isset($result['headers']['Content-Type']) ? $result['headers']['Content-Type'] : '';
  40. if(is_array($contentType))
  41. $contentType = $contentType[count($contentType)-1];
  42. if(strpos($contentType, 'application/atom+xml') !== false) {
  43. $feeds[] = [
  44. 'url' => $result['url'],
  45. 'type' => 'atom'
  46. ];
  47. } elseif(strpos($contentType, 'application/rss+xml') !== false || strpos($contentType, 'text/xml') !== false) {
  48. $feeds[] = [
  49. 'url' => $result['url'],
  50. 'type' => 'rss'
  51. ];
  52. } elseif(strpos($contentType, 'application/json') !== false && substr($body, 0, 1) == '{') {
  53. $feeddata = json_decode($body, true);
  54. if($feeddata && isset($feeddata['version']) && $feeddata['version'] == 'https://jsonfeed.org/version/1') {
  55. $feeds[] = [
  56. 'url' => $result['url'],
  57. 'type' => 'jsonfeed'
  58. ];
  59. }
  60. } else {
  61. // Some other document was returned, parse the HTML and look for rel alternates and Microformats
  62. $mf2 = \mf2\Parse($body, $result['url']);
  63. if(isset($mf2['alternates'])) {
  64. foreach($mf2['alternates'] as $alt) {
  65. if(isset($alt['type'])) {
  66. if(strpos($alt['type'], 'application/json') !== false) {
  67. $feeds[] = [
  68. 'url' => $alt['url'],
  69. 'type' => 'jsonfeed'
  70. ];
  71. }
  72. if(strpos($alt['type'], 'application/atom+xml') !== false) {
  73. $feeds[] = [
  74. 'url' => $alt['url'],
  75. 'type' => 'atom'
  76. ];
  77. }
  78. if(strpos($alt['type'], 'application/rss+xml') !== false) {
  79. $feeds[] = [
  80. 'url' => $alt['url'],
  81. 'type' => 'rss'
  82. ];
  83. }
  84. }
  85. }
  86. }
  87. $parsed = Formats\HTML::parse($this->http, $body, $result['url'], array_merge($opts, ['expect'=>'feed']));
  88. if($parsed && isset($parsed['data']['type']) && $parsed['data']['type'] == 'feed') {
  89. $feeds[] = [
  90. 'url' => $result['url'],
  91. 'type' => 'microformats'
  92. ];
  93. }
  94. }
  95. // Sort feeds by priority
  96. $rank = ['microformats'=>0,'jsonfeed'=>1,'atom'=>2,'rss'=>3];
  97. usort($feeds, function($a, $b) use($rank) {
  98. return $rank[$a['type']] > $rank[$b['type']];
  99. });
  100. return [
  101. 'url' => $result['url'],
  102. 'code' => $result['code'],
  103. 'feeds' => $feeds,
  104. ];
  105. }
  106. }