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.

109 lines
2.7 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($feed, $url) {
  10. $result = [
  11. 'data' => [
  12. 'type' => 'unknown',
  13. ],
  14. 'url' => $url,
  15. 'source-format' => 'feed+json',
  16. ];
  17. if($feed) {
  18. $result['data']['type'] = 'feed';
  19. foreach($feed['items'] as $item) {
  20. $result['data']['items'][] = self::_hEntryFromFeedItem($item, $feed, $url);
  21. }
  22. }
  23. return $result;
  24. }
  25. private static function _hEntryFromFeedItem($item, $feed, $feedurl) {
  26. $entry = [
  27. 'type' => 'entry',
  28. 'author' => [
  29. 'name' => null,
  30. 'url' => null,
  31. 'photo' => null
  32. ]
  33. ];
  34. if(isset($item['author']['name'])) {
  35. $entry['author']['name'] = $item['author']['name'];
  36. }
  37. if(isset($item['author']['url'])) {
  38. $entry['author']['url'] = $item['author']['url'];
  39. } elseif(isset($feed['home_page_url'])) {
  40. $entry['author']['url'] = $feed['home_page_url'];
  41. }
  42. if(isset($item['author']['avatar'])) {
  43. $entry['author']['photo'] = $item['author']['avatar'];
  44. }
  45. if(isset($item['url'])) {
  46. $entry['url'] = $item['url'];
  47. }
  48. if(isset($item['id'])) {
  49. $entry['uid'] = $item['id'];
  50. }
  51. if(isset($item['title']) && trim($item['title'])) {
  52. $entry['name'] = trim($item['title']);
  53. }
  54. $baseURL = isset($entry['url']) ? $entry['url'] : $feedurl;
  55. if(isset($item['content_html']) && isset($item['content_text'])) {
  56. $entry['content'] = [
  57. 'html' => self::sanitizeHTML($item['content_html'], true, $baseURL),
  58. 'text' => trim($item['content_text'])
  59. ];
  60. } elseif(isset($item['content_html'])) {
  61. $entry['content'] = [
  62. 'html' => self::sanitizeHTML($item['content_html'], true, $baseURL),
  63. 'text' => self::stripHTML($item['content_html'])
  64. ];
  65. } elseif(isset($item['content_text'])) {
  66. $entry['content'] = [
  67. 'text' => trim($item['content_text'])
  68. ];
  69. }
  70. if(isset($item['summary'])) {
  71. $entry['summary'] = $item['summary'];
  72. }
  73. if(isset($item['date_published'])) {
  74. $entry['published'] = $item['date_published'];
  75. }
  76. if(isset($item['date_modified'])) {
  77. $entry['updated'] = $item['date_modified'];
  78. }
  79. if(isset($item['image'])) {
  80. $entry['photo'] = \Mf2\resolveUrl($baseURL, $item['image']);
  81. }
  82. if(isset($item['tags'])) {
  83. $entry['category'] = $item['tags'];
  84. }
  85. $entry['post-type'] = \p3k\XRay\PostType::discover($entry);
  86. return $entry;
  87. }
  88. }