discovery.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This class performs lookups based on methods implemented in separate
  7. * classes, where a resource uri is given. Examples are WebFinger (RFC7033)
  8. * and the LRDD (Link-based Resource Descriptor Discovery) in RFC6415.
  9. *
  10. * PHP version 5
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. * @category Discovery
  26. * @package GNUsocial
  27. * @author James Walker <james@status.net>
  28. * @author Mikael Nordfeldth <mmn@hethane.se>
  29. * @copyright 2010 StatusNet, Inc.
  30. * @copyright 2013 Free Software Foundation, Inc.
  31. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  32. * @link http://www.gnu.org/software/social/
  33. */
  34. if (!defined('GNUSOCIAL')) { exit(1); }
  35. class Discovery
  36. {
  37. const LRDD_REL = 'lrdd';
  38. const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
  39. const HCARD = 'http://microformats.org/profile/hcard';
  40. const MF2_HCARD = 'http://microformats.org/profile/h-card'; // microformats2 h-card
  41. const JRD_MIMETYPE_OLD = 'application/json'; // RFC6415 uses this
  42. const JRD_MIMETYPE = 'application/jrd+json';
  43. const XRD_MIMETYPE = 'application/xrd+xml';
  44. public $methods = array();
  45. /**
  46. * Constructor for a discovery object
  47. *
  48. * Registers different discovery methods.
  49. *
  50. * @return Discovery this
  51. */
  52. public function __construct()
  53. {
  54. if (Event::handle('StartDiscoveryMethodRegistration', array($this))) {
  55. Event::handle('EndDiscoveryMethodRegistration', array($this));
  56. }
  57. }
  58. public static function supportedMimeTypes()
  59. {
  60. return array('json'=>self::JRD_MIMETYPE,
  61. 'jsonold'=>self::JRD_MIMETYPE_OLD,
  62. 'xml'=>self::XRD_MIMETYPE);
  63. }
  64. /**
  65. * Register a discovery class
  66. *
  67. * @param string $class Class name
  68. *
  69. * @return void
  70. */
  71. public function registerMethod($class)
  72. {
  73. $this->methods[] = $class;
  74. }
  75. /**
  76. * Given a user ID, return the first available resource descriptor
  77. *
  78. * @param string $id User ID URI
  79. *
  80. * @return XML_XRD object for the resource descriptor of the id
  81. */
  82. public function lookup($id)
  83. {
  84. // Normalize the incoming $id to make sure we have a uri
  85. $uri = self::normalize($id);
  86. foreach ($this->methods as $class) {
  87. try {
  88. $xrd = new XML_XRD();
  89. common_debug("LRDD discovery method for '$uri': {$class}");
  90. $lrdd = new $class;
  91. $links = call_user_func(array($lrdd, 'discover'), $uri);
  92. $link = Discovery::getService($links, Discovery::LRDD_REL);
  93. // Load the LRDD XRD
  94. if (!empty($link->template)) {
  95. $xrd_uri = Discovery::applyTemplate($link->template, $uri);
  96. } elseif (!empty($link->href)) {
  97. $xrd_uri = $link->href;
  98. } else {
  99. throw new Exception('No resource descriptor URI in link.');
  100. }
  101. $client = new HTTPClient();
  102. $headers = array();
  103. if (!is_null($link->type)) {
  104. $headers[] = "Accept: {$link->type}";
  105. }
  106. $response = $client->get($xrd_uri, $headers);
  107. if ($response->getStatus() != 200) {
  108. throw new Exception('Unexpected HTTP status code.');
  109. }
  110. $xrd->loadString($response->getBody());
  111. return $xrd;
  112. } catch (Exception $e) {
  113. continue;
  114. }
  115. }
  116. // TRANS: Exception. %s is an ID.
  117. throw new Exception(sprintf(_('Unable to find services for %s.'), $id));
  118. }
  119. /**
  120. * Given an array of links, returns the matching service
  121. *
  122. * @param array $links Links to check (as instances of XML_XRD_Element_Link)
  123. * @param string $service Service to find
  124. *
  125. * @return array $link assoc array representing the link
  126. */
  127. public static function getService(array $links, $service)
  128. {
  129. foreach ($links as $link) {
  130. if ($link->rel === $service) {
  131. return $link;
  132. }
  133. common_debug('LINK: rel '.$link->rel.' !== '.$service);
  134. }
  135. throw new Exception('No service link found');
  136. }
  137. /**
  138. * Given a "user id" make sure it's normalized to an acct: uri
  139. *
  140. * @param string $user_id User ID to normalize
  141. *
  142. * @return string normalized acct: URI
  143. */
  144. public static function normalize($uri)
  145. {
  146. if (is_null($uri) || $uri==='') {
  147. throw new Exception(_('No resource given.'));
  148. }
  149. $parts = parse_url($uri);
  150. // If we don't have a scheme, but the path implies user@host,
  151. // though this is far from a perfect matching procedure...
  152. if (!isset($parts['scheme']) && isset($parts['path'])
  153. && preg_match('/[\w@\w]/u', $parts['path'])) {
  154. return 'acct:' . $uri;
  155. }
  156. return $uri;
  157. }
  158. public static function isAcct($uri)
  159. {
  160. return (mb_strtolower(mb_substr($uri, 0, 5)) == 'acct:');
  161. }
  162. /**
  163. * Apply a template using an ID
  164. *
  165. * Replaces {uri} in template string with the ID given.
  166. *
  167. * @param string $template Template to match
  168. * @param string $uri URI to replace with
  169. *
  170. * @return string replaced values
  171. */
  172. public static function applyTemplate($template, $uri)
  173. {
  174. $template = str_replace('{uri}', urlencode($uri), $template);
  175. return $template;
  176. }
  177. }