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.1 KiB

2 months ago
  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. $fetch = null;
  47. }
  48. $parser = new XRay\Parser($this->http, $fetch);
  49. // Merge provided options with default options, allowing provided options to override defaults.
  50. $opts = array_merge($this->defaultOptions, $opts);
  51. $result = $parser->parse([
  52. 'body' => $body,
  53. 'url' => $url,
  54. 'code' => $code,
  55. ], $opts);
  56. if(!isset($opts['include_original']) || !$opts['include_original'])
  57. unset($result['original']);
  58. if(!isset($result['url'])) $result['url'] = $url;
  59. if(!isset($result['code'])) $result['code'] = $code;
  60. if(!isset($result['source-format'])) $result['source-format'] = null;
  61. return $result;
  62. }
  63. public function process($url, $mf2json, $opts=[]) {
  64. $parser = new XRay\Parser($this->http);
  65. // Merge provided options with default options, allowing provided options to override defaults.
  66. $opts = array_merge($this->defaultOptions, $opts);
  67. $result = $parser->parse([
  68. 'body' => $mf2json,
  69. 'url' => $url,
  70. 'code' => null,
  71. ], $opts);
  72. if(!isset($opts['include_original']) || !$opts['include_original'])
  73. unset($result['original']);
  74. if(!isset($result['url'])) $result['url'] = $url;
  75. if(!isset($result['source-format'])) $result['source-format'] = null;
  76. return $result;
  77. }
  78. public function httpsig($opts) {
  79. $this->defaultOptions = array_merge($this->defaultOptions, ['httpsig' => $opts]);
  80. }
  81. }