LrddMethodLinkHeader.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\FreeNetwork\Util\LrddMethod;
  4. use App\Core\Log;
  5. use Component\FreeNetwork\Util\LinkHeader;
  6. use Component\FreeNetwork\Util\LrddMethod;
  7. use Exception;
  8. use XML_XRD_Element_Link;
  9. /**
  10. * Implementation of discovery using HTTP Link header
  11. *
  12. * Discovers XRD file for a user by fetching the URL and reading any
  13. * Link: headers in the HTTP response.
  14. *
  15. * @category Discovery
  16. * @package StatusNet
  17. *
  18. * @author James Walker <james@status.net>
  19. * @copyright 2010 StatusNet, Inc.
  20. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  21. *
  22. * @see http://status.net/
  23. */
  24. class LrddMethodLinkHeader extends LRDDMethod
  25. {
  26. /**
  27. * For HTTP IDs fetch the URL and look for Link headers.
  28. *
  29. * @todo fail out of WebFinger URIs faster
  30. */
  31. public function discover($uri)
  32. {
  33. $response = self::fetchUrl($uri, 'head');
  34. $link_header = $response->getHeaders()['link'][0];
  35. if (empty($link_header)) {
  36. throw new Exception('No Link header found');
  37. }
  38. Log::debug('LRDD LinkHeader found: ' . var_export($link_header, true));
  39. return self::parseHeader($link_header);
  40. }
  41. /**
  42. * Given a string or array of headers, returns JRD-like assoc array
  43. *
  44. * @param array|string $header string or array of strings for headers
  45. *
  46. * @return array of associative arrays in JRD-like array format
  47. */
  48. protected static function parseHeader($header)
  49. {
  50. $lh = new LinkHeader($header);
  51. $link = new XML_XRD_Element_Link($lh->rel, $lh->href, $lh->type);
  52. return [$link];
  53. }
  54. }