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.

107 lines
2.6 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);
  21. }
  22. }
  23. return $result;
  24. }
  25. private static function _hEntryFromFeedItem($item, $feed) {
  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. if(isset($item['content_html']) && isset($item['content_text'])) {
  55. $entry['content'] = [
  56. 'html' => self::sanitizeHTML($item['content_html']),
  57. 'text' => trim($item['content_text'])
  58. ];
  59. } elseif(isset($item['content_html'])) {
  60. $entry['content'] = [
  61. 'html' => self::sanitizeHTML($item['content_html']),
  62. 'text' => self::stripHTML($item['content_html'])
  63. ];
  64. } elseif(isset($item['content_text'])) {
  65. $entry['content'] = [
  66. 'text' => trim($item['content_text'])
  67. ];
  68. }
  69. if(isset($item['summary'])) {
  70. $entry['summary'] = $item['summary'];
  71. }
  72. if(isset($item['date_published'])) {
  73. $entry['published'] = $item['date_published'];
  74. }
  75. if(isset($item['date_modified'])) {
  76. $entry['updated'] = $item['date_modified'];
  77. }
  78. if(isset($item['image'])) {
  79. $entry['photo'] = $item['image'];
  80. }
  81. if(isset($item['tags'])) {
  82. $entry['category'] = $item['tags'];
  83. }
  84. $entry['post-type'] = \p3k\XRay\PostType::discover($entry);
  85. return $entry;
  86. }
  87. }