3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Use Hammer discovery stack to find out interesting things about an URI
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author James Walker <james@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
32 * This class implements LRDD-based service discovery based on the "Hammer Draft"
33 * (including webfinger)
37 * @author James Walker <james@status.net>
38 * @copyright 2010 StatusNet, Inc.
39 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
40 * @link http://status.net/
42 * @see http://groups.google.com/group/webfinger/browse_thread/thread/9f3d93a479e91bbf
47 const LRDD_REL = 'lrdd';
48 const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
49 const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
50 const HCARD = 'http://microformats.org/profile/hcard';
52 public $methods = array();
55 * Constructor for a discovery object
57 * Registers different discovery methods.
59 * @return Discovery this
62 public function __construct()
64 $this->registerMethod('Discovery_LRDD_Host_Meta');
65 $this->registerMethod('Discovery_LRDD_Link_Header');
66 $this->registerMethod('Discovery_LRDD_Link_HTML');
70 * Register a discovery class
72 * @param string $class Class name
77 public function registerMethod($class)
79 $this->methods[] = $class;
83 * Given a "user id" make sure it's normalized to either a webfinger
84 * acct: uri or a profile HTTP URL.
86 * @param string $user_id User ID to normalize
88 * @return string normalized acct: or http(s)?: URI
91 public static function normalize($user_id)
93 if (substr($user_id, 0, 5) == 'http:' ||
94 substr($user_id, 0, 6) == 'https:' ||
95 substr($user_id, 0, 5) == 'acct:') {
99 if (strpos($user_id, '@') !== false) {
100 return 'acct:' . $user_id;
103 return 'http://' . $user_id;
107 * Determine if a string is a Webfinger ID
109 * Webfinger IDs look like foo@example.com or acct:foo@example.com
111 * @param string $user_id ID to check
113 * @return boolean true if $user_id is a Webfinger, else false
116 public static function isWebfinger($user_id)
118 $uri = Discovery::normalize($user_id);
120 return (substr($uri, 0, 5) == 'acct:');
124 * Given a user ID, return the first available XRD
126 * @param string $id User ID URI
128 * @return XRD XRD object for the user
131 public function lookup($id)
133 // Normalize the incoming $id to make sure we have a uri
134 $uri = $this->normalize($id);
136 foreach ($this->methods as $class) {
137 $links = call_user_func(array($class, 'discover'), $uri);
138 if ($link = Discovery::getService($links, Discovery::LRDD_REL)) {
140 if (!empty($link['template'])) {
141 $xrd_uri = Discovery::applyTemplate($link['template'], $uri);
143 $xrd_uri = $link['href'];
146 $xrd = $this->fetchXrd($xrd_uri);
154 throw new Exception(sprintf(_('Unable to find services for %s.'), $id));
158 * Given an array of links, returns the matching service
160 * @param array $links Links to check
161 * @param string $service Service to find
163 * @return array $link assoc array representing the link
166 public static function getService($links, $service)
168 if (!is_array($links)) {
172 foreach ($links as $link) {
173 if ($link['rel'] == $service) {
180 * Apply a template using an ID
182 * Replaces {uri} in template string with the ID given.
184 * @param string $template Template to match
185 * @param string $id User ID to replace with
187 * @return string replaced values
190 public static function applyTemplate($template, $id)
192 $template = str_replace('{uri}', urlencode($id), $template);
198 * Fetch an XRD file and parse
200 * @param string $url URL of the XRD
202 * @return XRD object representing the XRD file
205 public static function fetchXrd($url)
208 $client = new HTTPClient();
209 $response = $client->get($url);
210 } catch (HTTP_Request2_Exception $e) {
214 if ($response->getStatus() != 200) {
218 return XRD::parse($response->getBody());
223 * Abstract interface for discovery
225 * Objects that implement this interface can retrieve an array of
226 * XRD links for the URI.
228 * @category Discovery
230 * @author James Walker <james@status.net>
231 * @copyright 2010 StatusNet, Inc.
232 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
233 * @link http://status.net/
236 interface Discovery_LRDD
239 * Discover interesting info about the URI
241 * @param string $uri URI to inquire about
243 * @return array Links in the XRD file
246 public function discover($uri);
250 * Implementation of discovery using host-meta file
252 * Discovers XRD file for a user by going to the organization's
253 * host-meta file and trying to find a template for LRDD.
255 * @category Discovery
257 * @author James Walker <james@status.net>
258 * @copyright 2010 StatusNet, Inc.
259 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
260 * @link http://status.net/
263 class Discovery_LRDD_Host_Meta implements Discovery_LRDD
266 * Discovery core method
268 * For Webfinger and HTTP URIs, fetch the host-meta file
269 * and look for LRDD templates
271 * @param string $uri URI to inquire about
273 * @return array Links in the XRD file
276 public function discover($uri)
278 if (Discovery::isWebfinger($uri)) {
279 // We have a webfinger acct: - start with host-meta
280 list($name, $domain) = explode('@', $uri);
282 $domain = parse_url($uri, PHP_URL_HOST);
285 $url = 'http://'. $domain .'/.well-known/host-meta';
287 $xrd = Discovery::fetchXrd($url);
290 if ($xrd->host != $domain) {
300 * Implementation of discovery using HTTP Link header
302 * Discovers XRD file for a user by fetching the URL and reading any
303 * Link: headers in the HTTP response.
305 * @category Discovery
307 * @author James Walker <james@status.net>
308 * @copyright 2010 StatusNet, Inc.
309 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
310 * @link http://status.net/
313 class Discovery_LRDD_Link_Header implements Discovery_LRDD
316 * Discovery core method
318 * For HTTP IDs fetch the URL and look for Link headers.
320 * @param string $uri URI to inquire about
322 * @return array Links in the XRD file
324 * @todo fail out of Webfinger URIs faster
327 public function discover($uri)
330 $client = new HTTPClient();
331 $response = $client->get($uri);
332 } catch (HTTP_Request2_Exception $e) {
336 if ($response->getStatus() != 200) {
340 $link_header = $response->getHeader('Link');
345 return array(Discovery_LRDD_Link_Header::parseHeader($link_header));
349 * Given a string or array of headers, returns XRD-like assoc array
351 * @param string|array $header string or array of strings for headers
353 * @return array Link header in XRD-like format
356 protected static function parseHeader($header)
358 $lh = new LinkHeader($header);
360 return array('href' => $lh->href,
362 'type' => $lh->type);
367 * Implementation of discovery using HTML <link> element
369 * Discovers XRD file for a user by fetching the URL and reading any
370 * <link> elements in the HTML response.
372 * @category Discovery
374 * @author James Walker <james@status.net>
375 * @copyright 2010 StatusNet, Inc.
376 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
377 * @link http://status.net/
380 class Discovery_LRDD_Link_HTML implements Discovery_LRDD
383 * Discovery core method
385 * For HTTP IDs, fetch the URL and look for <link> elements
386 * in the HTML response.
388 * @param string $uri URI to inquire about
390 * @return array Links in XRD-ish assoc array
392 * @todo fail out of Webfinger URIs faster
395 public function discover($uri)
398 $client = new HTTPClient();
399 $response = $client->get($uri);
400 } catch (HTTP_Request2_Exception $e) {
404 if ($response->getStatus() != 200) {
408 return Discovery_LRDD_Link_HTML::parse($response->getBody());
412 * Parse HTML and return <link> elements
414 * Given an HTML string, scans the string for <link> elements
416 * @param string $html HTML to scan
418 * @return array array of associative arrays in XRD-ish format
421 public function parse($html)
425 preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
426 $head_html = $head_matches[2];
428 preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
430 foreach ($link_matches[0] as $link_html) {
435 preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
436 if ( isset($rel_matches[3]) ) {
437 $link_rel = $rel_matches[3];
438 } else if ( isset($rel_matches[1]) ) {
439 $link_rel = $rel_matches[1];
442 preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
443 if ( isset($href_matches[3]) ) {
444 $link_uri = $href_matches[3];
445 } else if ( isset($href_matches[1]) ) {
446 $link_uri = $href_matches[1];
449 preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
450 if ( isset($type_matches[3]) ) {
451 $link_type = $type_matches[3];
452 } else if ( isset($type_matches[1]) ) {
453 $link_type = $type_matches[1];
459 'type' => $link_type,