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.

117 lines
3.4 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. $body = $result['body'];
  31. $feeds = [];
  32. // First check the content type of the response
  33. $contentType = isset($result['headers']['Content-Type']) ? $result['headers']['Content-Type'] : '';
  34. if(is_array($contentType))
  35. $contentType = $contentType[count($contentType)-1];
  36. if(strpos($contentType, 'application/atom+xml') !== false) {
  37. $feeds[] = [
  38. 'url' => $result['url'],
  39. 'type' => 'atom'
  40. ];
  41. } elseif(strpos($contentType, 'application/rss+xml') !== false || strpos($contentType, 'text/xml') !== false) {
  42. $feeds[] = [
  43. 'url' => $result['url'],
  44. 'type' => 'rss'
  45. ];
  46. } elseif(strpos($contentType, 'application/json') !== false && substr($body, 0, 1) == '{') {
  47. $feeddata = json_decode($body, true);
  48. if($feeddata && isset($feeddata['version']) && $feeddata['version'] == 'https://jsonfeed.org/version/1') {
  49. $feeds[] = [
  50. 'url' => $result['url'],
  51. 'type' => 'jsonfeed'
  52. ];
  53. }
  54. } else {
  55. // Some other document was returned, parse the HTML and look for rel alternates and Microformats
  56. $mf2 = \mf2\Parse($body, $result['url']);
  57. if(isset($mf2['alternates'])) {
  58. foreach($mf2['alternates'] as $alt) {
  59. if(isset($alt['type'])) {
  60. if(strpos($alt['type'], 'application/json') !== false) {
  61. $feeds[] = [
  62. 'url' => $alt['url'],
  63. 'type' => 'jsonfeed'
  64. ];
  65. }
  66. if(strpos($alt['type'], 'application/atom+xml') !== false) {
  67. $feeds[] = [
  68. 'url' => $alt['url'],
  69. 'type' => 'atom'
  70. ];
  71. }
  72. if(strpos($alt['type'], 'application/rss+xml') !== false) {
  73. $feeds[] = [
  74. 'url' => $alt['url'],
  75. 'type' => 'rss'
  76. ];
  77. }
  78. }
  79. }
  80. }
  81. $parsed = Formats\HTML::parse($this->http, $body, $result['url'], array_merge($opts, ['expect'=>'feed']));
  82. if($parsed && isset($parsed['data']['type']) && $parsed['data']['type'] == 'feed') {
  83. $feeds[] = [
  84. 'url' => $result['url'],
  85. 'type' => 'microformats'
  86. ];
  87. }
  88. }
  89. // Sort feeds by priority
  90. $rank = ['microformats'=>0,'jsonfeed'=>1,'atom'=>2,'rss'=>3];
  91. usort($feeds, function($a, $b) use($rank) {
  92. return $rank[$a['type']] > $rank[$b['type']];
  93. });
  94. return [
  95. 'url' => $result['url'],
  96. 'code' => $result['code'],
  97. 'feeds' => $feeds
  98. ];
  99. }
  100. }