hostmeta.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Implementation of discovery using host-meta file
  4. *
  5. * Discovers resource descriptor file for a user by going to the
  6. * organization's host-meta file and trying to find a template for LRDD.
  7. *
  8. * @category Discovery
  9. * @package StatusNet
  10. * @author James Walker <james@status.net>
  11. * @copyright 2010 StatusNet, Inc.
  12. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  13. * @link http://status.net/
  14. */
  15. class LRDDMethod_HostMeta extends LRDDMethod
  16. {
  17. /**
  18. * For RFC6415 and HTTP URIs, fetch the host-meta file
  19. * and look for LRDD templates
  20. */
  21. public function discover($uri)
  22. {
  23. // This is allowed for RFC6415 but not the 'WebFinger' RFC7033.
  24. $try_schemes = array('https', 'http');
  25. $scheme = mb_strtolower(parse_url($uri, PHP_URL_SCHEME));
  26. switch ($scheme) {
  27. case 'acct':
  28. if (!Discovery::isAcct($uri)) {
  29. throw new Exception('Bad resource URI: '.$uri);
  30. }
  31. // We can't use parse_url data for this, since the 'host'
  32. // entry is only set if the scheme has '://' after it.
  33. list($user, $domain) = explode('@', parse_url($uri, PHP_URL_PATH));
  34. break;
  35. case 'http':
  36. case 'https':
  37. $domain = mb_strtolower(parse_url($uri, PHP_URL_HOST));
  38. $try_schemes = array($scheme);
  39. break;
  40. default:
  41. throw new Exception('Unable to discover resource descriptor endpoint.');
  42. }
  43. foreach ($try_schemes as $scheme) {
  44. $url = $scheme . '://' . $domain . '/.well-known/host-meta';
  45. try {
  46. $response = self::fetchUrl($url);
  47. $this->xrd->loadString($response->getBody());
  48. } catch (Exception $e) {
  49. common_debug('LRDD could not load resource descriptor: '.$url.' ('.$e->getMessage().')');
  50. continue;
  51. }
  52. return $this->xrd->links;
  53. }
  54. throw new Exception('Unable to retrieve resource descriptor links.');
  55. }
  56. }