3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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.
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/>.
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/
35 if (!defined('GNUSOCIAL')) { exit(1); }
39 const LRDD_REL = 'lrdd';
40 const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
41 const HCARD = 'http://microformats.org/profile/hcard';
43 const JRD_MIMETYPE_OLD = 'application/json'; // RFC6415 uses this
44 const JRD_MIMETYPE = 'application/jrd+json';
45 const XRD_MIMETYPE = 'application/xrd+xml';
47 public $methods = array();
50 * Constructor for a discovery object
52 * Registers different discovery methods.
54 * @return Discovery this
57 public function __construct()
59 if (Event::handle('StartDiscoveryMethodRegistration', array($this))) {
60 Event::handle('EndDiscoveryMethodRegistration', array($this));
64 public static function supportedMimeTypes()
66 return array('json'=>self::JRD_MIMETYPE,
67 'jsonold'=>self::JRD_MIMETYPE_OLD,
68 'xml'=>self::XRD_MIMETYPE);
72 * Register a discovery class
74 * @param string $class Class name
78 public function registerMethod($class)
80 $this->methods[] = $class;
84 * Given a user ID, return the first available resource descriptor
86 * @param string $id User ID URI
88 * @return XML_XRD object for the resource descriptor of the id
90 public function lookup($id)
92 // Normalize the incoming $id to make sure we have a uri
93 $uri = self::normalize($id);
95 foreach ($this->methods as $class) {
99 common_debug("LRDD discovery method for '$uri': {$class}");
101 $links = call_user_func(array($lrdd, 'discover'), $uri);
102 $link = Discovery::getService($links, Discovery::LRDD_REL);
105 if (!empty($link->template)) {
106 $xrd_uri = Discovery::applyTemplate($link->template, $uri);
107 } elseif (!empty($link->href)) {
108 $xrd_uri = $link->href;
110 throw new Exception('No resource descriptor URI in link.');
113 $client = new HTTPClient();
115 if (!is_null($link->type)) {
116 $headers[] = "Accept: {$link->type}";
119 $response = $client->get($xrd_uri, $headers);
120 if ($response->getStatus() != 200) {
121 throw new Exception('Unexpected HTTP status code.');
124 $xrd->loadString($response->getBody());
126 } catch (Exception $e) {
131 // TRANS: Exception. %s is an ID.
132 throw new Exception(sprintf(_('Unable to find services for %s.'), $id));
136 * Given an array of links, returns the matching service
138 * @param array $links Links to check (as instances of XML_XRD_Element_Link)
139 * @param string $service Service to find
141 * @return array $link assoc array representing the link
143 public static function getService(array $links, $service)
145 foreach ($links as $link) {
146 if ($link->rel === $service) {
149 common_debug('LINK: rel '.$link->rel.' !== '.$service);
152 throw new Exception('No service link found');
156 * Given a "user id" make sure it's normalized to an acct: uri
158 * @param string $user_id User ID to normalize
160 * @return string normalized acct: URI
162 public static function normalize($uri)
164 if (is_null($uri) || $uri==='') {
165 throw new Exception(_('No resource given.'));
168 $parts = parse_url($uri);
169 // If we don't have a scheme, but the path implies user@host,
170 // though this is far from a perfect matching procedure...
171 if (!isset($parts['scheme']) && isset($parts['path'])
172 && preg_match('/[\w@\w]/u', $parts['path'])) {
173 return 'acct:' . $uri;
179 public static function isAcct($uri)
181 return (mb_strtolower(mb_substr($uri, 0, 5)) == 'acct:');
185 * Apply a template using an ID
187 * Replaces {uri} in template string with the ID given.
189 * @param string $template Template to match
190 * @param string $uri URI to replace with
192 * @return string replaced values
194 public static function applyTemplate($template, $uri)
196 $template = str_replace('{uri}', urlencode($uri), $template);