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.

113 lines
3.0 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. // Override the author if the item contains author info
  44. if(isset($item['author']['name']))
  45. $entry['author']['name'] = $item['author']['name'];
  46. if(isset($item['author']['url']))
  47. $entry['author']['url'] = $item['author']['url'];
  48. if(isset($item['author']['avatar']))
  49. $entry['author']['photo'] = $item['author']['avatar'];
  50. if(isset($item['url']))
  51. $entry['url'] = $item['url'];
  52. if(isset($item['id']))
  53. $entry['uid'] = $item['id'];
  54. if(isset($item['title']) && trim($item['title']))
  55. $entry['name'] = trim($item['title']);
  56. $baseURL = isset($entry['url']) ? $entry['url'] : $feedurl;
  57. if(isset($item['content_html']) && isset($item['content_text'])) {
  58. $entry['content'] = [
  59. 'html' => self::sanitizeHTML($item['content_html'], true, $baseURL),
  60. 'text' => trim($item['content_text'])
  61. ];
  62. } elseif(isset($item['content_html'])) {
  63. $entry['content'] = [
  64. 'html' => self::sanitizeHTML($item['content_html'], true, $baseURL),
  65. 'text' => self::stripHTML($item['content_html'])
  66. ];
  67. } elseif(isset($item['content_text'])) {
  68. $entry['content'] = [
  69. 'text' => trim($item['content_text'])
  70. ];
  71. }
  72. if(isset($item['summary'])) {
  73. $entry['summary'] = $item['summary'];
  74. }
  75. if(isset($item['date_published'])) {
  76. $entry['published'] = $item['date_published'];
  77. }
  78. if(isset($item['date_modified'])) {
  79. $entry['updated'] = $item['date_modified'];
  80. }
  81. if(isset($item['image'])) {
  82. $entry['photo'] = [\Mf2\resolveUrl($baseURL, $item['image'])];
  83. }
  84. if(isset($item['tags'])) {
  85. $entry['category'] = $item['tags'];
  86. }
  87. $entry['post-type'] = \p3k\XRay\PostType::discover($entry);
  88. return $entry;
  89. }
  90. }