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.

76 lines
2.2 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. if(substr($body, 0, 5) == '<?xml') {
  40. return Formats\XML::parse($body, $url);
  41. }
  42. if(substr($body, 0, 1) == '{') {
  43. $parsed = json_decode($body, true);
  44. if($parsed && isset($parsed['version']) && $parsed['version'] == 'https://jsonfeed.org/version/1') {
  45. return Formats\JSONFeed::parse($parsed, $url);
  46. // TODO: check for an activitystreams object too
  47. } elseif($parsed && isset($parsed['items'][0]['type']) && isset($parsed['items'][0]['properties'])) {
  48. // Check if an mf2 JSON string was passed in
  49. $data = Formats\Mf2::parse($parsed, $url, $this->http, $opts);
  50. $data['source-format'] = 'mf2+json';
  51. return $data;
  52. }
  53. }
  54. // No special parsers matched, parse for Microformats now
  55. $data = Formats\HTML::parse($this->http, $body, $url, $opts);
  56. if(!isset($data['source-format']))
  57. $data['source-format'] = 'mf2+html';
  58. return $data;
  59. }
  60. }