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.

93 lines
3.0 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($http_response, $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. $url = $http_response['url'];
  16. if(Formats\Instagram::matches($url)) {
  17. return Formats\Instagram::parse($this->http, $http_response, $opts);
  18. }
  19. if(Formats\GitHub::matches($url)) {
  20. return Formats\GitHub::parse($http_response);
  21. }
  22. if(Formats\Twitter::matches($url)) {
  23. return Formats\Twitter::parse($http_response);
  24. }
  25. if(Formats\Facebook::matches($url)) {
  26. return Formats\Facebook::parse($http_response);
  27. }
  28. if(Formats\XKCD::matches($url)) {
  29. return Formats\XKCD::parse($http_response);
  30. }
  31. if(Formats\Hackernews::matches($url)) {
  32. return Formats\Hackernews::parse($http_response);
  33. }
  34. $body = $http_response['body'];
  35. // Check if an mf2 JSON object was passed in
  36. if(is_array($body) && isset($body['items'][0]['type']) && isset($body['items'][0]['properties'])) {
  37. $data = Formats\Mf2::parse($http_response, $this->http, $opts);
  38. $data['source-format'] = 'mf2+json';
  39. return $data;
  40. }
  41. // Check if an ActivityStreams JSON object was passed in
  42. if(Formats\ActivityStreams::is_as2_json($body)) {
  43. $data = Formats\ActivityStreams::parse($http_response, $this->http, $opts);
  44. $data['source-format'] = 'activity+json';
  45. return $data;
  46. }
  47. if(substr($body, 0, 5) == '<?xml') {
  48. return Formats\XML::parse($http_response);
  49. }
  50. if(substr($body, 0, 1) == '{') {
  51. $parsed = json_decode($body, true);
  52. if($parsed && isset($parsed['version']) && $parsed['version'] == 'https://jsonfeed.org/version/1') {
  53. $http_response['body'] = $parsed;
  54. return Formats\JSONFeed::parse($http_response);
  55. } elseif($parsed && isset($parsed['items'][0]['type']) && isset($parsed['items'][0]['properties'])) {
  56. // Check if an mf2 JSON string was passed in
  57. $http_response['body'] = $parsed;
  58. $data = Formats\Mf2::parse($http_response, $this->http, $opts);
  59. $data['source-format'] = 'mf2+json';
  60. return $data;
  61. } elseif($parsed && Formats\ActivityStreams::is_as2_json($parsed)) {
  62. // Check if an ActivityStreams JSON string was passed in
  63. $http_response['body'] = $parsed;
  64. $data = Formats\ActivityStreams::parse($http_response, $this->http, $opts);
  65. $data['source-format'] = 'activity+json';
  66. return $data;
  67. }
  68. }
  69. // No special parsers matched, parse for Microformats now
  70. $data = Formats\HTML::parse($this->http, $http_response, $opts);
  71. if(!isset($data['source-format']) && isset($data['type']) && $data['type'] != 'unknown')
  72. $data['source-format'] = 'mf2+html';
  73. return $data;
  74. }
  75. }