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.

87 lines
2.8 KiB

  1. <?php
  2. namespace p3k\XRay;
  3. use p3k\XRay\Formats;
  4. class Parser {
  5. private $http;
  6. public function __construct($http) {
  7. $this->http = $http;
  8. }
  9. public function parse($body, $url, $opts=[]) {
  10. if(isset($opts['timeout']))
  11. $this->http->set_timeout($opts['timeout']);
  12. if(isset($opts['max_redirects']))
  13. $this->http->set_max_redirects($opts['max_redirects']);
  14. // Check if the URL matches a special parser
  15. if(Formats\Instagram::matches($url)) {
  16. return Formats\Instagram::parse($this->http, $body, $url, $opts);
  17. }
  18. if(Formats\GitHub::matches($url)) {
  19. return Formats\GitHub::parse($body, $url);
  20. }
  21. if(Formats\Twitter::matches($url)) {
  22. return Formats\Twitter::parse($body, $url);
  23. }
  24. if(Formats\Facebook::matches($url)) {
  25. return Formats\Facebook::parse($body, $url);
  26. }
  27. if(Formats\XKCD::matches($url)) {
  28. return Formats\XKCD::parse($body, $url);
  29. }
  30. if(Formats\Hackernews::matches($url)) {
  31. return Formats\Hackernews::parse($body, $url);
  32. }
  33. // Check if an mf2 JSON object was passed in
  34. if(is_array($body) && isset($body['items'][0]['type']) && isset($body['items'][0]['properties'])) {
  35. $data = Formats\Mf2::parse($body, $url, $this->http, $opts);
  36. $data['source-format'] = 'mf2+json';
  37. return $data;
  38. }
  39. // Check if an ActivityStreams JSON object was passed in
  40. if(Formats\ActivityStreams::is_as2_json($body)) {
  41. $data = Formats\ActivityStreams::parse($body, $url, $this->http, $opts);
  42. $data['source-format'] = 'activity+json';
  43. return $data;
  44. }
  45. if(substr($body, 0, 5) == '<?xml') {
  46. return Formats\XML::parse($body, $url);
  47. }
  48. if(substr($body, 0, 1) == '{') {
  49. $parsed = json_decode($body, true);
  50. if($parsed && isset($parsed['version']) && $parsed['version'] == 'https://jsonfeed.org/version/1') {
  51. return Formats\JSONFeed::parse($parsed, $url);
  52. } elseif($parsed && isset($parsed['items'][0]['type']) && isset($parsed['items'][0]['properties'])) {
  53. // Check if an mf2 JSON string was passed in
  54. $data = Formats\Mf2::parse($parsed, $url, $this->http, $opts);
  55. $data['source-format'] = 'mf2+json';
  56. return $data;
  57. } elseif($parsed && Formats\ActivityStreams::is_as2_json($parsed)) {
  58. // Check if an ActivityStreams JSON string was passed in
  59. $data = Formats\ActivityStreams::parse($parsed, $url, $this->http, $opts);
  60. $data['source-format'] = 'activity+json';
  61. return $data;
  62. }
  63. }
  64. // No special parsers matched, parse for Microformats now
  65. $data = Formats\HTML::parse($this->http, $body, $url, $opts);
  66. if(!isset($data['source-format']) && isset($data['type']) && $data['type'] != 'unknown')
  67. $data['source-format'] = 'mf2+html';
  68. return $data;
  69. }
  70. }