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.

148 lines
4.2 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use HTMLPurifier, HTMLPurifier_Config;
  4. use DOMDocument, DOMXPath;
  5. use p3k\XRay\Formats;
  6. class JSONFeed extends Format {
  7. public static function matches_host($url) { return true; }
  8. public static function matches($url) { return true; }
  9. public static function parse($http_response) {
  10. $feed = $http_response['body'];
  11. $url = $http_response['url'];
  12. $result = [
  13. 'data' => [
  14. 'type' => 'unknown',
  15. ],
  16. 'url' => $url,
  17. 'source-format' => 'feed+json',
  18. ];
  19. if($feed) {
  20. $result['data']['type'] = 'feed';
  21. foreach($feed['items'] as $item) {
  22. $result['data']['items'][] = self::_hEntryFromFeedItem($item, $feed, $url);
  23. }
  24. }
  25. return $result;
  26. }
  27. private static function _hEntryFromFeedItem($item, $feed, $feedurl) {
  28. $entry = [
  29. 'type' => 'entry',
  30. 'author' => [
  31. 'name' => null,
  32. 'url' => null,
  33. 'photo' => null
  34. ]
  35. ];
  36. // First use the feed title/icon/url as author info
  37. if(isset($feed['home_page_url']))
  38. $entry['author']['url'] = $feed['home_page_url'];
  39. if(isset($feed['title']))
  40. $entry['author']['name'] = $feed['title'];
  41. if(isset($feed['icon']))
  42. $entry['author']['photo'] = $feed['icon'];
  43. // If the feed has an author, use that instead
  44. if(isset($feed['author'])) {
  45. if(isset($feed['author']['url']))
  46. $entry['author']['url'] = $feed['author']['url'];
  47. if(isset($feed['author']['name']))
  48. $entry['author']['name'] = $feed['author']['name'];
  49. if(isset($feed['author']['avatar']))
  50. $entry['author']['photo'] = $feed['author']['avatar'];
  51. }
  52. // Override the author if the item contains author info
  53. if(isset($item['author'])) {
  54. if(isset($item['author']['name']))
  55. $entry['author']['name'] = $item['author']['name'];
  56. if(isset($item['author']['url']))
  57. $entry['author']['url'] = $item['author']['url'];
  58. if(isset($item['author']['avatar']))
  59. $entry['author']['photo'] = $item['author']['avatar'];
  60. }
  61. // JSONFeed 1.1 uses "authors" instead
  62. if(isset($feed['authors'][0])) {
  63. // We only support 1 author
  64. $author = $feed['authors'][0];
  65. if(isset($author['url']))
  66. $entry['author']['url'] = $author['url'];
  67. if(isset($author['name']))
  68. $entry['author']['name'] = $author['name'];
  69. if(isset($author['avatar']))
  70. $entry['author']['photo'] = $author['avatar'];
  71. }
  72. if(isset($item['authors'][0])) {
  73. $author = $item['authors'][0];
  74. if(isset($author['url']))
  75. $entry['author']['url'] = $author['url'];
  76. if(isset($author['name']))
  77. $entry['author']['name'] = $author['name'];
  78. if(isset($author['avatar']))
  79. $entry['author']['photo'] = $author['avatar'];
  80. }
  81. if(isset($item['url']))
  82. $entry['url'] = $item['url'];
  83. if(isset($item['id']))
  84. $entry['uid'] = $item['id'];
  85. if(isset($item['title']) && trim($item['title']))
  86. $entry['name'] = trim($item['title']);
  87. $baseURL = isset($entry['url']) ? $entry['url'] : $feedurl;
  88. if(isset($item['content_html']) && isset($item['content_text'])) {
  89. $entry['content'] = [
  90. 'html' => self::sanitizeHTML($item['content_html'], true, $baseURL),
  91. 'text' => trim($item['content_text'])
  92. ];
  93. } elseif(isset($item['content_html'])) {
  94. $entry['content'] = [
  95. 'html' => self::sanitizeHTML($item['content_html'], true, $baseURL),
  96. 'text' => self::stripHTML($item['content_html'])
  97. ];
  98. } elseif(isset($item['content_text'])) {
  99. $entry['content'] = [
  100. 'text' => trim($item['content_text'])
  101. ];
  102. }
  103. if(isset($item['summary'])) {
  104. $entry['summary'] = $item['summary'];
  105. }
  106. if(isset($item['date_published'])) {
  107. $entry['published'] = $item['date_published'];
  108. }
  109. if(isset($item['date_modified'])) {
  110. $entry['updated'] = $item['date_modified'];
  111. }
  112. if(isset($item['image'])) {
  113. $entry['photo'] = [\Mf2\resolveUrl($baseURL, $item['image'])];
  114. }
  115. if(isset($item['tags'])) {
  116. $entry['category'] = $item['tags'];
  117. }
  118. $entry['post-type'] = \p3k\XRay\PostType::discover($entry);
  119. return $entry;
  120. }
  121. }