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.

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