]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/WebFingerPlugin.php
Merge commit 'refs/merge-requests/30' of https://gitorious.org/social/mainline into...
[quix0rs-gnu-social.git] / plugins / WebFinger / WebFingerPlugin.php
1 <?php
2 /*
3  * GNU Social - a federating social network
4  * Copyright (C) 2013, Free Software Foundation, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * Implements WebFinger for GNU Social, as well as support for the
22  * '.well-known/host-meta' resource.
23  *
24  * Depends on: LRDD plugin
25  *
26  * @package GNUsocial
27  * @author  Mikael Nordfeldth <mmn@hethane.se>
28  */
29
30 if (!defined('GNUSOCIAL')) { exit(1); }
31
32 class WebFingerPlugin extends Plugin
33 {
34     public function onRouterInitialized(URLMapper $m)
35     {
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'));
46         return true;
47     }
48
49     public function onLoginAction($action, &$login)
50     {
51         switch ($action) {
52         case 'hostmeta':
53         case 'webfinger':
54             $login = true;
55             return false;
56         }
57         
58         return true;
59     }
60
61     public function onStartGetProfileAcctUri(Profile $profile, &$acct)
62     {
63         $wfr = new WebFingerResource_Profile($profile);
64         try {
65             $acct = $wfr->reconstructAcct();
66         } catch (Exception $e) {
67             return true;
68         }
69
70         return false;
71     }
72
73     public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
74     {
75         $profile = null;
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));
85                     }
86                     $profile = $user->getProfile();
87                 } else {
88                     throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
89                 }
90             }
91         } else {
92             $user = User::getKV('uri', $resource);
93             if ($user instanceof User) {
94                 $profile = $user->getProfile();
95             } else {
96                 // try and get it by profile url
97                 $profile = Profile::getKV('profileurl', $resource);
98             }
99         }
100
101         if ($profile instanceof Profile) {
102             $target = new WebFingerResource_Profile($profile);
103             return false;   // We got our target, stop handler execution
104         }
105
106         $notice = Notice::getKV('uri', $resource);
107         if ($notice instanceof Notice) {
108             $target = new WebFingerResource_Notice($notice);
109             return false;
110         }
111
112         return true;
113     }
114
115     public function onStartHostMetaLinks(array &$links)
116     {
117         foreach (Discovery::supportedMimeTypes() as $type) {
118             $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
119                             common_local_url('webfinger') . '?resource={uri}',
120                             $type,
121                             true);    // isTemplate
122         }
123     }
124
125     /**
126      * Add a link header for LRDD Discovery
127      */
128     public function onStartShowHTML($action)
129     {
130         if ($action instanceof ShowstreamAction) {
131             $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
132             $url = common_local_url('webfinger') . '?resource='.$acct;
133
134             foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
135                 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"');
136             }
137         }
138     }
139
140     public function onPluginVersion(array &$versions)
141     {
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'));
148
149         return true;
150     }
151 }