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.

128 lines
3.8 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 || strpos(substr($body, 0, 50), '<feed ') !== 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. || strpos(substr($body, 0, 50), '<rss ') !== false) {
  49. $feeds[] = [
  50. 'url' => $result['url'],
  51. 'type' => 'rss'
  52. ];
  53. } elseif(strpos($contentType, 'application/json') !== false && substr($body, 0, 1) == '{') {
  54. $feeddata = json_decode($body, true);
  55. if($feeddata && isset($feeddata['version']) && $feeddata['version'] == 'https://jsonfeed.org/version/1') {
  56. $feeds[] = [
  57. 'url' => $result['url'],
  58. 'type' => 'jsonfeed'
  59. ];
  60. }
  61. } else {
  62. // Some other document was returned, parse the HTML and look for rel alternates and Microformats
  63. $mf2 = \mf2\Parse($body, $result['url']);
  64. if(isset($mf2['rel-urls'])) {
  65. foreach($mf2['rel-urls'] as $rel=>$info) {
  66. if(isset($info['rels']) && in_array('alternate', $info['rels'])) {
  67. if(isset($info['type'])) {
  68. if(strpos($info['type'], 'application/json') !== false) {
  69. $feeds[] = [
  70. 'url' => $rel,
  71. 'type' => 'jsonfeed'
  72. ];
  73. }
  74. if(strpos($info['type'], 'application/atom+xml') !== false) {
  75. $feeds[] = [
  76. 'url' => $rel,
  77. 'type' => 'atom'
  78. ];
  79. }
  80. if(strpos($info['type'], 'application/rss+xml') !== false) {
  81. $feeds[] = [
  82. 'url' => $rel,
  83. 'type' => 'rss'
  84. ];
  85. }
  86. }
  87. }
  88. }
  89. }
  90. $parsed = Formats\HTML::parse($this->http, $body, $result['url'], array_merge($opts, ['expect'=>'feed']));
  91. if($parsed && isset($parsed['data']['type']) && $parsed['data']['type'] == 'feed') {
  92. $feeds[] = [
  93. 'url' => $result['url'],
  94. 'type' => 'microformats'
  95. ];
  96. }
  97. }
  98. // Sort feeds by priority
  99. $rank = ['microformats'=>0,'jsonfeed'=>1,'atom'=>2,'rss'=>3];
  100. usort($feeds, function($a, $b) use($rank) {
  101. return $rank[$a['type']] > $rank[$b['type']];
  102. });
  103. return [
  104. 'url' => $result['url'],
  105. 'code' => $result['code'],
  106. 'feeds' => $feeds,
  107. ];
  108. }
  109. }