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.

92 lines
3.1 KiB

  1. <?php
  2. namespace p3k;
  3. class XRay {
  4. public $http;
  5. private $defaultOptions = [];
  6. public function __construct($options=[]) {
  7. $this->http = new HTTP('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 p3k/XRay');
  8. if (is_array($options)) {
  9. $this->defaultOptions = $options;
  10. }
  11. }
  12. public function rels($url, $opts=[]) {
  13. $rels = new XRay\Rels($this->http);
  14. // Merge provided options with default options, allowing provided options to override defaults.
  15. $opts = array_merge($this->defaultOptions, $opts);
  16. return $rels->parse($url, $opts);
  17. }
  18. public function feeds($url, $opts=[]) {
  19. $feeds = new XRay\Feeds($this->http);
  20. // Merge provided options with default options, allowing provided options to override defaults.
  21. $opts = array_merge($this->defaultOptions, $opts);
  22. return $feeds->find($url, $opts);
  23. }
  24. public function parse($url, $opts_or_body=false, $opts_for_body=[]) {
  25. if(!$opts_or_body || is_array($opts_or_body)) {
  26. $fetch = new XRay\Fetcher($this->http);
  27. if(is_array($opts_or_body)) {
  28. $fetch_opts = array_merge($this->defaultOptions, $opts_or_body);
  29. } else {
  30. $fetch_opts = $this->defaultOptions;
  31. }
  32. if(is_array($fetch_opts) && isset($fetch_opts['httpsig'])) {
  33. $fetch->httpsig($fetch_opts['httpsig']);
  34. }
  35. $response = $fetch->fetch($url, $fetch_opts);
  36. if(!empty($response['error']))
  37. return $response;
  38. $body = $response['body'];
  39. $url = $response['url'];
  40. $code = $response['code'];
  41. $opts = is_array($opts_or_body) ? $opts_or_body : $opts_for_body;
  42. } else {
  43. $body = $opts_or_body;
  44. $opts = $opts_for_body;
  45. $code = null;
  46. }
  47. $parser = new XRay\Parser($this->http, $fetch);
  48. // Merge provided options with default options, allowing provided options to override defaults.
  49. $opts = array_merge($this->defaultOptions, $opts);
  50. $result = $parser->parse([
  51. 'body' => $body,
  52. 'url' => $url,
  53. 'code' => $code,
  54. ], $opts);
  55. if(!isset($opts['include_original']) || !$opts['include_original'])
  56. unset($result['original']);
  57. if(!isset($result['url'])) $result['url'] = $url;
  58. if(!isset($result['code'])) $result['code'] = $code;
  59. if(!isset($result['source-format'])) $result['source-format'] = null;
  60. return $result;
  61. }
  62. public function process($url, $mf2json, $opts=[]) {
  63. $parser = new XRay\Parser($this->http);
  64. // Merge provided options with default options, allowing provided options to override defaults.
  65. $opts = array_merge($this->defaultOptions, $opts);
  66. $result = $parser->parse([
  67. 'body' => $mf2json,
  68. 'url' => $url,
  69. 'code' => null,
  70. ], $opts);
  71. if(!isset($opts['include_original']) || !$opts['include_original'])
  72. unset($result['original']);
  73. if(!isset($result['url'])) $result['url'] = $url;
  74. if(!isset($result['source-format'])) $result['source-format'] = null;
  75. return $result;
  76. }
  77. public function httpsig($opts) {
  78. $this->defaultOptions = array_merge($this->defaultOptions, ['httpsig' => $opts]);
  79. }
  80. }