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/
31 if (!defined('STATUSNET')) {
36 * This class implements LRDD-based service discovery based on the "Hammer Draft"
37 * (including webfinger)
41 * @author James Walker <james@status.net>
42 * @copyright 2010 StatusNet, Inc.
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44 * @link http://status.net/
46 * @see http://groups.google.com/group/webfinger/browse_thread/thread/9f3d93a479e91bbf
51 const LRDD_REL = 'lrdd';
52 const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
53 const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
54 const HCARD = 'http://microformats.org/profile/hcard';
56 public $methods = array();
59 * Constructor for a discovery object
61 * Registers different discovery methods.
63 * @return Discovery this
66 public function __construct()
68 $this->registerMethod('Discovery_LRDD_Host_Meta');
69 $this->registerMethod('Discovery_LRDD_Link_Header');
70 $this->registerMethod('Discovery_LRDD_Link_HTML');
74 * Register a discovery class
76 * @param string $class Class name
81 public function registerMethod($class)
83 $this->methods[] = $class;
87 * Given a "user id" make sure it's normalized to either a webfinger
88 * acct: uri or a profile HTTP URL.
90 * @param string $user_id User ID to normalize
92 * @return string normalized acct: or http(s)?: URI
95 public static function normalize($user_id)
97 if (substr($user_id, 0, 5) == 'http:' ||
98 substr($user_id, 0, 6) == 'https:' ||
99 substr($user_id, 0, 5) == 'acct:') {
103 if (strpos($user_id, '@') !== false) {
104 return 'acct:' . $user_id;
107 return 'http://' . $user_id;
111 * Determine if a string is a Webfinger ID
113 * Webfinger IDs look like foo@example.com or acct:foo@example.com
115 * @param string $user_id ID to check
117 * @return boolean true if $user_id is a Webfinger, else false
120 public static function isWebfinger($user_id)
122 $uri = Discovery::normalize($user_id);
124 return (substr($uri, 0, 5) == 'acct:');
128 * Given a user ID, return the first available XRD
130 * @param string $id User ID URI
132 * @return XRD XRD object for the user
135 public function lookup($id)
137 // Normalize the incoming $id to make sure we have a uri
138 $uri = $this->normalize($id);
140 foreach ($this->methods as $class) {
141 $links = call_user_func(array($class, 'discover'), $uri);
142 if ($link = Discovery::getService($links, Discovery::LRDD_REL)) {
144 if (!empty($link['template'])) {
145 $xrd_uri = Discovery::applyTemplate($link['template'], $uri);
147 $xrd_uri = $link['href'];
150 $xrd = $this->fetchXrd($xrd_uri);
158 throw new Exception(sprintf(_('Unable to find services for %s.'), $id));
162 * Given an array of links, returns the matching service
164 * @param array $links Links to check
165 * @param string $service Service to find
167 * @return array $link assoc array representing the link
170 public static function getService($links, $service)
172 if (!is_array($links)) {
176 foreach ($links as $link) {
177 if ($link['rel'] == $service) {
184 * Apply a template using an ID
186 * Replaces {uri} in template string with the ID given.
188 * @param string $template Template to match
189 * @param string $id User ID to replace with
191 * @return string replaced values
194 public static function applyTemplate($template, $id)
196 $template = str_replace('{uri}', urlencode($id), $template);
202 * Fetch an XRD file and parse
204 * @param string $url URL of the XRD
206 * @return XRD object representing the XRD file
209 public static function fetchXrd($url)
212 $client = new HTTPClient();
213 $response = $client->get($url);
214 } catch (HTTP_Request2_Exception $e) {
218 if ($response->getStatus() != 200) {
222 return XRD::parse($response->getBody());
227 * Abstract interface for discovery
229 * Objects that implement this interface can retrieve an array of
230 * XRD links for the URI.
232 * @category Discovery
234 * @author James Walker <james@status.net>
235 * @copyright 2010 StatusNet, Inc.
236 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
237 * @link http://status.net/
240 interface Discovery_LRDD
243 * Discover interesting info about the URI
245 * @param string $uri URI to inquire about
247 * @return array Links in the XRD file
250 public function discover($uri);
254 * Implementation of discovery using host-meta file
256 * Discovers XRD file for a user by going to the organization's
257 * host-meta file and trying to find a template for LRDD.
259 * @category Discovery
261 * @author James Walker <james@status.net>
262 * @copyright 2010 StatusNet, Inc.
263 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
264 * @link http://status.net/
267 class Discovery_LRDD_Host_Meta implements Discovery_LRDD
270 * Discovery core method
272 * For Webfinger and HTTP URIs, fetch the host-meta file
273 * and look for LRDD templates
275 * @param string $uri URI to inquire about
277 * @return array Links in the XRD file
280 public function discover($uri)
282 if (Discovery::isWebfinger($uri)) {
283 // We have a webfinger acct: - start with host-meta
284 list($name, $domain) = explode('@', $uri);
286 $domain = parse_url($uri, PHP_URL_HOST);
289 $url = 'http://'. $domain .'/.well-known/host-meta';
291 $xrd = Discovery::fetchXrd($url);
294 if ($xrd->host != $domain) {
304 * Implementation of discovery using HTTP Link header
306 * Discovers XRD file for a user by fetching the URL and reading any
307 * Link: headers in the HTTP response.
309 * @category Discovery
311 * @author James Walker <james@status.net>
312 * @copyright 2010 StatusNet, Inc.
313 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
314 * @link http://status.net/
317 class Discovery_LRDD_Link_Header implements Discovery_LRDD
320 * Discovery core method
322 * For HTTP IDs fetch the URL and look for Link headers.
324 * @param string $uri URI to inquire about
326 * @return array Links in the XRD file
328 * @todo fail out of Webfinger URIs faster
331 public function discover($uri)
334 $client = new HTTPClient();
335 $response = $client->get($uri);
336 } catch (HTTP_Request2_Exception $e) {
340 if ($response->getStatus() != 200) {
344 $link_header = $response->getHeader('Link');
349 return array(Discovery_LRDD_Link_Header::parseHeader($link_header));
353 * Given a string or array of headers, returns XRD-like assoc array
355 * @param string|array $header string or array of strings for headers
357 * @return array Link header in XRD-like format
360 protected static function parseHeader($header)
362 $lh = new LinkHeader($header);
364 return array('href' => $lh->href,
366 'type' => $lh->type);
371 * Implementation of discovery using HTML <link> element
373 * Discovers XRD file for a user by fetching the URL and reading any
374 * <link> elements in the HTML response.
376 * @category Discovery
378 * @author James Walker <james@status.net>
379 * @copyright 2010 StatusNet, Inc.
380 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
381 * @link http://status.net/
384 class Discovery_LRDD_Link_HTML implements Discovery_LRDD
387 * Discovery core method
389 * For HTTP IDs, fetch the URL and look for <link> elements
390 * in the HTML response.
392 * @param string $uri URI to inquire about
394 * @return array Links in XRD-ish assoc array
396 * @todo fail out of Webfinger URIs faster
399 public function discover($uri)
402 $client = new HTTPClient();
403 $response = $client->get($uri);
404 } catch (HTTP_Request2_Exception $e) {
408 if ($response->getStatus() != 200) {
412 return Discovery_LRDD_Link_HTML::parse($response->getBody());
416 * Parse HTML and return <link> elements
418 * Given an HTML string, scans the string for <link> elements
420 * @param string $html HTML to scan
422 * @return array array of associative arrays in XRD-ish format
425 public function parse($html)
429 preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
430 $head_html = $head_matches[2];
432 preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
434 foreach ($link_matches[0] as $link_html) {
439 preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
440 if ( isset($rel_matches[3]) ) {
441 $link_rel = $rel_matches[3];
442 } else if ( isset($rel_matches[1]) ) {
443 $link_rel = $rel_matches[1];
446 preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
447 if ( isset($href_matches[3]) ) {
448 $link_uri = $href_matches[3];
449 } else if ( isset($href_matches[1]) ) {
450 $link_uri = $href_matches[1];
453 preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
454 if ( isset($type_matches[3]) ) {
455 $link_type = $type_matches[3];
456 } else if ( isset($type_matches[1]) ) {
457 $link_type = $type_matches[1];
463 'type' => $link_type,