]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/WebFingerPlugin.php
XSS vulnerability when remote-subscribing
[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     const OAUTH_ACCESS_TOKEN_REL    = 'http://apinamespace.org/oauth/access_token';
35     const OAUTH_REQUEST_TOKEN_REL   = 'http://apinamespace.org/oauth/request_token';
36     const OAUTH_AUTHORIZE_REL       = 'http://apinamespace.org/oauth/authorize';
37
38     public $http_alias = false;
39
40     public function initialize()
41     {
42         common_config_set('webfinger', 'http_alias', $this->http_alias);
43     }
44
45     public function onRouterInitialized($m)
46     {
47         $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
48         $m->connect('.well-known/host-meta.:format',
49                         array('action' => 'hostmeta',
50                               'format' => '(xml|json)'));
51         // the resource GET parameter can be anywhere, so don't mention it here
52         $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
53         $m->connect('.well-known/webfinger.:format',
54                         array('action' => 'webfinger',
55                               'format' => '(xml|json)'));
56         $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
57         return true;
58     }
59
60     public function onLoginAction($action, &$login)
61     {
62         switch ($action) {
63         case 'hostmeta':
64         case 'webfinger':
65             $login = true;
66             return false;
67         }
68         
69         return true;
70     }
71
72     public function onStartGetProfileAcctUri(Profile $profile, &$acct)
73     {
74         $wfr = new WebFingerResource_Profile($profile);
75         try {
76             $acct = $wfr->reconstructAcct();
77         } catch (Exception $e) {
78             return true;
79         }
80
81         return false;
82     }
83
84     public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
85     {
86         $profile = null;
87         if (Discovery::isAcct($resource)) {
88             $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
89             if (count($parts) == 2) {
90                 list($nick, $domain) = $parts;
91                 if ($domain === common_config('site', 'server')) {
92                     $nick = common_canonical_nickname($nick);
93                     $user = User::getKV('nickname', $nick);
94                     if (!($user instanceof User)) {
95                         throw new NoSuchUserException(array('nickname'=>$nick));
96                     }
97                     $profile = $user->getProfile();
98                 } else {
99                     throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
100                 }
101             }
102         } else {
103             $user = User::getKV('uri', $resource);
104             if ($user instanceof User) {
105                 $profile = $user->getProfile();
106             } else {
107                 // try and get it by profile url
108                 $profile = Profile::getKV('profileurl', $resource);
109             }
110         }
111
112         if ($profile instanceof Profile) {
113             $target = new WebFingerResource_Profile($profile);
114             return false;   // We got our target, stop handler execution
115         }
116
117         $notice = Notice::getKV('uri', $resource);
118         if ($notice instanceof Notice) {
119             $target = new WebFingerResource_Notice($notice);
120             return false;
121         }
122
123         return true;
124     }
125
126     public function onStartHostMetaLinks(array &$links)
127     {
128         foreach (Discovery::supportedMimeTypes() as $type) {
129             $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
130                             common_local_url('webfinger') . '?resource={uri}',
131                             $type,
132                             true);    // isTemplate
133         }
134
135         // OAuth connections
136         $links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL,  common_local_url('ApiOAuthAccessToken'));
137         $links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
138         $links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL,     common_local_url('ApiOAuthAuthorize'));
139     }
140
141     /**
142      * Add a link header for LRDD Discovery
143      */
144     public function onStartShowHTML($action)
145     {
146         if ($action instanceof ShowstreamAction) {
147             $acct = 'acct:'. $action->getTarget()->getNickname() .'@'. common_config('site', 'server');
148             $url = common_local_url('webfinger') . '?resource='.$acct;
149
150             foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
151                 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
152             }
153         }
154     }
155
156     public function onPluginVersion(array &$versions)
157     {
158         $versions[] = array('name' => 'WebFinger',
159                             'version' => GNUSOCIAL_VERSION,
160                             'author' => 'Mikael Nordfeldth',
161                             'homepage' => 'http://www.gnu.org/software/social/',
162                             // TRANS: Plugin description.
163                             'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));
164
165         return true;
166     }
167 }