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.

115 lines
2.9 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use HTMLPurifier, HTMLPurifier_Config;
  4. use DOMDocument, DOMXPath;
  5. use p3k\XRay\Formats;
  6. use PicoFeed\Reader\Reader;
  7. use PicoFeed\PicoFeedException;
  8. class XML extends Format {
  9. public static function matches_host($url) { return true; }
  10. public static function matches($url) { return true; }
  11. public static function parse($xml, $url) {
  12. $result = [
  13. 'data' => [
  14. 'type' => 'unknown',
  15. ],
  16. 'url' => $url,
  17. ];
  18. try {
  19. $reader = new Reader();
  20. $parser = $reader->getParser($url, $xml, '');
  21. $feed = $parser->execute();
  22. $result['data']['type'] = 'feed';
  23. $result['data']['items'] = [];
  24. foreach($feed->getItems() as $item) {
  25. $result['data']['items'][] = self::_hEntryFromFeedItem($item, $feed);
  26. }
  27. } catch(PicoFeedException $e) {
  28. }
  29. return $result;
  30. }
  31. private static function _hEntryFromFeedItem($item, $feed) {
  32. $entry = [
  33. 'type' => 'entry',
  34. 'author' => [
  35. 'name' => null,
  36. 'url' => null,
  37. 'photo' => null
  38. ]
  39. ];
  40. if(is_array($guid=$item->getTag('guid')) && count($guid))
  41. $entry['uid'] = $guid[0];
  42. elseif(is_array($guid=$item->getTag('id')) && count($guid))
  43. $entry['uid'] = $guid[0];
  44. if($item->getUrl())
  45. $entry['url'] = $item->getUrl();
  46. if($item->getPublishedDate())
  47. $entry['published'] = $item->getPublishedDate()->format('c');
  48. if($item->getContent())
  49. $entry['content'] = [
  50. 'html' => self::sanitizeHTML($item->getContent()),
  51. 'text' => self::stripHTML($item->getContent())
  52. ];
  53. if($item->getTitle() && $item->getTitle() != $item->getUrl()) {
  54. $title = $item->getTitle();
  55. $entry['name'] = $title;
  56. // Check if the title is a prefix of the content and drop if so
  57. if(isset($entry['content'])) {
  58. if(substr($title, -3) == '...' || substr($title, -1) == '…') {
  59. if(substr($title, -3) == '...') {
  60. $trimmedTitle = substr($title, 0, -3);
  61. } else {
  62. $trimmedTitle = substr($title, 0, -1);
  63. }
  64. if(substr($entry['content']['text'], 0, strlen($trimmedTitle)) == $trimmedTitle) {
  65. unset($entry['name']);
  66. }
  67. }
  68. }
  69. }
  70. if($item->getAuthor()) {
  71. $entry['author']['name'] = $item->getAuthor();
  72. }
  73. if($item->getAuthorUrl()) {
  74. $entry['author']['url'] = $item->getAuthorUrl();
  75. } else if($feed->siteUrl) {
  76. $entry['author']['url'] = $feed->siteUrl;
  77. }
  78. if($item->getEnclosureType()) {
  79. $prop = false;
  80. switch($item->getEnclosureType()) {
  81. case 'audio/mpeg':
  82. $prop = 'audio'; break;
  83. case 'image/jpeg':
  84. case 'image/png':
  85. case 'image/gif':
  86. $prop = 'photo'; break;
  87. }
  88. if($prop)
  89. $entry[$prop] = [$item->getEnclosureUrl()];
  90. }
  91. return $entry;
  92. }
  93. }