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.

63 lines
1.4 KiB

  1. <?php
  2. namespace p3k\XRay;
  3. class Rels {
  4. private $http;
  5. public function __construct($http) {
  6. $this->http = $http;
  7. }
  8. public function parse($url, $opts=[]) {
  9. if(isset($opts['timeout']))
  10. $this->http->set_timeout($opts['timeout']);
  11. if(isset($opts['max_redirects']))
  12. $this->http->set_max_redirects($opts['max_redirects']);
  13. $scheme = parse_url($url, PHP_URL_SCHEME);
  14. if(!in_array($scheme, ['http','https'])) {
  15. return [
  16. 'error' => 'invalid_url',
  17. 'error_description' => 'Only http and https URLs are supported'
  18. ];
  19. }
  20. $host = parse_url($url, PHP_URL_HOST);
  21. if(!$host) {
  22. return [
  23. 'error' => 'invalid_url',
  24. 'error_description' => 'The URL provided was not valid'
  25. ];
  26. }
  27. $url = normalize_url($url);
  28. $result = $this->http->get($url);
  29. $html = $result['body'];
  30. $mf2 = \mf2\Parse($html, $result['url']);
  31. $rels = $result['rels'];
  32. if(isset($mf2['rels'])) {
  33. $rels = array_merge($rels, $mf2['rels']);
  34. }
  35. // Resolve all relative URLs
  36. foreach($rels as $rel=>$values) {
  37. foreach($values as $i=>$value) {
  38. $value = \mf2\resolveUrl($result['url'], $value);
  39. $rels[$rel][$i] = $value;
  40. }
  41. }
  42. if(count($rels) == 0)
  43. $rels = new \StdClass;
  44. return [
  45. 'url' => $result['url'],
  46. 'code' => $result['code'],
  47. 'rels' => $rels
  48. ];
  49. }
  50. }