]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/WebFingerPlugin.php
Only serve tagprofile HTML if we aren't POSTing via ajax
[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 $http_alias = false;
35
36     public function initialize()
37     {
38         common_config_set('webfinger', 'http_alias', $this->http_alias);
39     }
40
41     public function onRouterInitialized($m)
42     {
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'));
53         return true;
54     }
55
56     public function onLoginAction($action, &$login)
57     {
58         switch ($action) {
59         case 'hostmeta':
60         case 'webfinger':
61             $login = true;
62             return false;
63         }
64         
65         return true;
66     }
67
68     public function onStartGetProfileAcctUri(Profile $profile, &$acct)
69     {
70         $wfr = new WebFingerResource_Profile($profile);
71         try {
72             $acct = $wfr->reconstructAcct();
73         } catch (Exception $e) {
74             return true;
75         }
76
77         return false;
78     }
79
80     public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
81     {
82         $profile = null;
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));
92                     }
93                     $profile = $user->getProfile();
94                 } else {
95                     throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
96                 }
97             }
98         } else {
99             $user = User::getKV('uri', $resource);
100             if ($user instanceof User) {
101                 $profile = $user->getProfile();
102             } else {
103                 // try and get it by profile url
104                 $profile = Profile::getKV('profileurl', $resource);
105             }
106         }
107
108         if ($profile instanceof Profile) {
109             $target = new WebFingerResource_Profile($profile);
110             return false;   // We got our target, stop handler execution
111         }
112
113         $notice = Notice::getKV('uri', $resource);
114         if ($notice instanceof Notice) {
115             $target = new WebFingerResource_Notice($notice);
116             return false;
117         }
118
119         return true;
120     }
121
122     public function onStartHostMetaLinks(array &$links)
123     {
124         foreach (Discovery::supportedMimeTypes() as $type) {
125             $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
126                             common_local_url('webfinger') . '?resource={uri}',
127                             $type,
128                             true);    // isTemplate
129         }
130     }
131
132     /**
133      * Add a link header for LRDD Discovery
134      */
135     public function onStartShowHTML($action)
136     {
137         if ($action instanceof ShowstreamAction) {
138             $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
139             $url = common_local_url('webfinger') . '?resource='.$acct;
140
141             foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
142                 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"');
143             }
144         }
145     }
146
147     public function onPluginVersion(&$versions)
148     {
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'));
155
156         return true;
157     }
158 }