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 $http_alias = false;
36 public function initialize()
38 common_config_set('webfinger', 'http_alias', $this->http_alias);
41 public function onRouterInitialized(URLMapper $m)
43 $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
44 $m->connect('.well-known/host-meta.:format',
45 array('action' => 'hostmeta',
46 'format' => '(xml|json)'));
47 // the resource GET parameter can be anywhere, so don't mention it here
48 $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
49 $m->connect('.well-known/webfinger.:format',
50 array('action' => 'webfinger',
51 'format' => '(xml|json)'));
52 $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
56 public function onLoginAction($action, &$login)
68 public function onStartGetProfileAcctUri(Profile $profile, &$acct)
70 $wfr = new WebFingerResource_Profile($profile);
72 $acct = $wfr->reconstructAcct();
73 } catch (Exception $e) {
80 public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
83 if (Discovery::isAcct($resource)) {
84 $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
85 if (count($parts) == 2) {
86 list($nick, $domain) = $parts;
87 if ($domain === common_config('site', 'server')) {
88 $nick = common_canonical_nickname($nick);
89 $user = User::getKV('nickname', $nick);
90 if (!($user instanceof User)) {
91 throw new NoSuchUserException(array('nickname'=>$nick));
93 $profile = $user->getProfile();
95 throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
99 $user = User::getKV('uri', $resource);
100 if ($user instanceof User) {
101 $profile = $user->getProfile();
103 // try and get it by profile url
104 $profile = Profile::getKV('profileurl', $resource);
108 if ($profile instanceof Profile) {
109 $target = new WebFingerResource_Profile($profile);
110 return false; // We got our target, stop handler execution
113 $notice = Notice::getKV('uri', $resource);
114 if ($notice instanceof Notice) {
115 $target = new WebFingerResource_Notice($notice);
122 public function onStartHostMetaLinks(array &$links)
124 foreach (Discovery::supportedMimeTypes() as $type) {
125 $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
126 common_local_url('webfinger') . '?resource={uri}',
133 * Add a link header for LRDD Discovery
135 public function onStartShowHTML(Action $action)
137 if ($action instanceof ShowstreamAction) {
138 $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
139 $url = common_local_url('webfinger') . '?resource='.$acct;
141 foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
142 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"');
147 public function onPluginVersion(array &$versions)
149 $versions[] = array('name' => 'WebFinger',
150 'version' => GNUSOCIAL_VERSION,
151 'author' => 'Mikael Nordfeldth',
152 'homepage' => 'http://www.gnu.org/software/social/',
153 // TRANS: Plugin description.
154 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));