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.

86 lines
2.1 KiB

  1. <?php
  2. namespace feeds;
  3. use BarnabyWalters\Mf2;
  4. function parse_mf2(&$html, $base) {
  5. $parser = new \mf2\Parser($html, $base);
  6. return $parser->parse();
  7. }
  8. function get_rels(&$data) {
  9. if($data && array_key_exists('rels', $data)) {
  10. return $data['rels'];
  11. } else {
  12. return [];
  13. }
  14. }
  15. function get_alternates(&$data) {
  16. if($data && array_key_exists('alternates', $data)) {
  17. return $data['alternates'];
  18. } else {
  19. return [];
  20. }
  21. }
  22. // Compares name, summary and content values to determine if they are equal
  23. function content_is_equal($a, $b) {
  24. // remove html tags
  25. $a = strip_tags($a);
  26. $b = strip_tags($b);
  27. // remove encoded entities
  28. $a = preg_replace('/&#?[a-z0-9]{2,8};/i', '', $a);
  29. $b = preg_replace('/&#?[a-z0-9]{2,8};/i', '', $b);
  30. // remove all non-alphanumeric chars
  31. $a = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($a));
  32. $b = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($b));
  33. return $a == $b;
  34. }
  35. // Given a parsed microformat data structure, find the feed on the page.
  36. // This is meant to follow
  37. // * http://indiewebcamp.com/feed#How_To_Consume
  38. // * http://microformats.org/wiki/h-feed#Parsing
  39. // Returns an array:
  40. // [
  41. // 'properties' => [ list of mf2 properties of the h-feed ],
  42. // 'entries' => [ list of h-entry items of the feed ]
  43. // ]
  44. function find_feed_info(&$data) {
  45. // tantek.com : h-card => h-feed => h-entry
  46. // snarfed.org : h-feed => h-entry
  47. // aaronparecki.com : h-entry
  48. $properties = [];
  49. $entries = [];
  50. // Find the first h-feed
  51. $feeds = Mf2\findMicroformatsByType($data, 'h-feed');
  52. if(count($feeds)) {
  53. $feed = $feeds[0];
  54. $properties = $feed['properties'];
  55. $entries = Mf2\findMicroformatsByType($feed['children'], 'h-entry', false);
  56. return [
  57. 'properties' => $properties,
  58. 'entries' => $entries
  59. ];
  60. } else {
  61. // This is an implied feed if there are h-entry posts found at the top level
  62. $entries = Mf2\findMicroformatsByType($data['items'], 'h-entry', false);
  63. if(count($entries)) {
  64. return [
  65. 'properties' => [],
  66. 'entries' => $entries
  67. ];
  68. }
  69. }
  70. return false;
  71. }