3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A sample module to show best practices for StatusNet plugins
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/>.
24 * @author James Walker <james@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27 * @link http://status.net/
31 * This class implements LRDD-based service discovery based on the "Hammer Draft"
32 * (including webfinger)
34 * @see http://groups.google.com/group/webfinger/browse_thread/thread/9f3d93a479e91bbf
39 const LRDD_REL = 'lrdd';
40 const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
41 const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
42 const HCARD = 'http://microformats.org/profile/hcard';
44 public $methods = array();
46 public function __construct()
48 $this->registerMethod('Discovery_LRDD_Host_Meta');
49 $this->registerMethod('Discovery_LRDD_Link_Header');
50 $this->registerMethod('Discovery_LRDD_Link_HTML');
53 public function registerMethod($class)
55 $this->methods[] = $class;
59 * Given a "user id" make sure it's normalized to either a webfinger
60 * acct: uri or a profile HTTP URL.
62 public static function normalize($user_id)
64 if (substr($user_id, 0, 5) == 'http:' ||
65 substr($user_id, 0, 6) == 'https:' ||
66 substr($user_id, 0, 5) == 'acct:') {
70 if (strpos($user_id, '@') !== FALSE) {
71 return 'acct:' . $user_id;
74 return 'http://' . $user_id;
77 public static function isWebfinger($user_id)
79 $uri = Discovery::normalize($user_id);
81 return (substr($uri, 0, 5) == 'acct:');
85 * This implements the actual lookup procedure
87 public function lookup($id)
89 // Normalize the incoming $id to make sure we have a uri
90 $uri = $this->normalize($id);
92 foreach ($this->methods as $class) {
93 $links = call_user_func(array($class, 'discover'), $uri);
94 if ($link = Discovery::getService($links, Discovery::LRDD_REL)) {
96 if (!empty($link['template'])) {
97 $xrd_uri = Discovery::applyTemplate($link['template'], $uri);
99 $xrd_uri = $link['href'];
102 $xrd = $this->fetchXrd($xrd_uri);
110 throw new Exception('Unable to find services for '. $id . '.');
113 public static function getService($links, $service) {
114 if (!is_array($links)) {
118 foreach ($links as $link) {
119 if ($link['rel'] == $service) {
125 public static function applyTemplate($template, $id)
127 $template = str_replace('{uri}', urlencode($id), $template);
132 public static function fetchXrd($url)
135 $client = new HTTPClient();
136 $response = $client->get($url);
137 } catch (HTTP_Request2_Exception $e) {
141 if ($response->getStatus() != 200) {
145 return XRD::parse($response->getBody());
149 interface Discovery_LRDD
151 public function discover($uri);
154 class Discovery_LRDD_Host_Meta implements Discovery_LRDD
156 public function discover($uri)
158 if (Discovery::isWebfinger($uri)) {
159 // We have a webfinger acct: - start with host-meta
160 list($name, $domain) = explode('@', $uri);
162 $domain = parse_url($uri, PHP_URL_HOST);
165 $url = 'http://'. $domain .'/.well-known/host-meta';
167 $xrd = Discovery::fetchXrd($url);
170 if ($xrd->host != $domain) {
179 class Discovery_LRDD_Link_Header implements Discovery_LRDD
181 public function discover($uri)
184 $client = new HTTPClient();
185 $response = $client->get($uri);
186 } catch (HTTP_Request2_Exception $e) {
190 if ($response->getStatus() != 200) {
194 $link_header = $response->getHeader('Link');
199 return array(Discovery_LRDD_Link_Header::parseHeader($link_header));
202 protected static function parseHeader($header)
204 $lh = new LinkHeader($header);
206 return array('href' => $lh->href,
208 'type' => $lh->type);
212 class Discovery_LRDD_Link_HTML implements Discovery_LRDD
214 public function discover($uri)
217 $client = new HTTPClient();
218 $response = $client->get($uri);
219 } catch (HTTP_Request2_Exception $e) {
223 if ($response->getStatus() != 200) {
227 return Discovery_LRDD_Link_HTML::parse($response->getBody());
230 public function parse($html)
234 preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
235 $head_html = $head_matches[2];
237 preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
239 foreach ($link_matches[0] as $link_html) {
244 preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
245 if ( isset($rel_matches[3]) ) {
246 $link_rel = $rel_matches[3];
247 } else if ( isset($rel_matches[1]) ) {
248 $link_rel = $rel_matches[1];
251 preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
252 if ( isset($href_matches[3]) ) {
253 $link_uri = $href_matches[3];
254 } else if ( isset($href_matches[1]) ) {
255 $link_uri = $href_matches[1];
258 preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
259 if ( isset($type_matches[3]) ) {
260 $link_type = $type_matches[3];
261 } else if ( isset($type_matches[1]) ) {
262 $link_type = $type_matches[1];
268 'type' => $link_type,