3 * GNU Social - a federating social network
4 * Copyright (C) 2013, Free Software Foundation, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Implements WebFinger for GNU Social, as well as support for the
22 * '.well-known/host-meta' resource.
24 * Depends on: LRDD plugin
27 * @author Mikael Nordfeldth <mmn@hethane.se>
30 if (!defined('GNUSOCIAL')) { exit(1); }
32 class WebFingerPlugin extends Plugin
34 public function onRouterInitialized(URLMapper $m)
36 $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
37 $m->connect('.well-known/host-meta.:format',
38 array('action' => 'hostmeta',
39 'format' => '(xml|json)'));
40 // the resource GET parameter can be anywhere, so don't mention it here
41 $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
42 $m->connect('.well-known/webfinger.:format',
43 array('action' => 'webfinger',
44 'format' => '(xml|json)'));
45 $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
49 public function onLoginAction($action, &$login)
61 public function onStartGetProfileAcctUri(Profile $profile, &$acct)
63 $wfr = new WebFingerResource_Profile($profile);
65 $acct = $wfr->reconstructAcct();
66 } catch (Exception $e) {
73 public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
76 if (Discovery::isAcct($resource)) {
77 $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
78 if (count($parts) == 2) {
79 list($nick, $domain) = $parts;
80 if ($domain === common_config('site', 'server')) {
81 $nick = common_canonical_nickname($nick);
82 $user = User::getKV('nickname', $nick);
83 if (!($user instanceof User)) {
84 throw new NoSuchUserException(array('nickname'=>$nick));
86 $profile = $user->getProfile();
88 throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
92 $user = User::getKV('uri', $resource);
93 if ($user instanceof User) {
94 $profile = $user->getProfile();
96 // try and get it by profile url
97 $profile = Profile::getKV('profileurl', $resource);
101 if ($profile instanceof Profile) {
102 $target = new WebFingerResource_Profile($profile);
103 return false; // We got our target, stop handler execution
106 $notice = Notice::getKV('uri', $resource);
107 if ($notice instanceof Notice) {
108 $target = new WebFingerResource_Notice($notice);
115 public function onStartHostMetaLinks(array &$links)
117 foreach (Discovery::supportedMimeTypes() as $type) {
118 $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
119 common_local_url('webfinger') . '?resource={uri}',
126 * Add a link header for LRDD Discovery
128 public function onStartShowHTML($action)
130 if ($action instanceof ShowstreamAction) {
131 $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
132 $url = common_local_url('webfinger') . '?resource='.$acct;
134 foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
135 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"');
140 public function onPluginVersion(array &$versions)
142 $versions[] = array('name' => 'WebFinger',
143 'version' => GNUSOCIAL_VERSION,
144 'author' => 'Mikael Nordfeldth',
145 'homepage' => 'http://www.gnu.org/software/social/',
146 // TRANS: Plugin description.
147 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));