]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
correct order of params in get_token
[quix0rs-gnu-social.git] / actions / remotesubscribe.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23 require_once('Auth/Yadis/Yadis.php');
24
25 class RemotesubscribeAction extends Action {
26
27         function handle($args) {
28
29                 parent::handle($args);
30
31                 if (common_logged_in()) {
32                         common_user_error(_t('You can use the local subscription!'));
33                     return;
34                 }
35
36                 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
37                         $this->remote_subscription();
38                 } else {
39                         $this->show_form();
40                 }
41         }
42
43         function show_form($err=NULL) {
44                 $nickname = $this->trimmed('nickname');
45                 common_show_header(_t('Remote subscribe'));
46                 if ($err) {
47                         common_element('div', 'error', $err);
48                 }
49                 common_element_start('form', array('id' => 'remotesubscribe', 'method' => 'POST',
50                                                                                    'action' => common_local_url('remotesubscribe')));
51                 common_input('nickname', _t('User nickname'), $nickname);
52                 common_input('profile', _t('Profile URL'));
53                 common_submit('submit', _t('Subscribe'));
54                 common_element_end('form');
55                 common_show_footer();
56         }
57
58         function remote_subscription() {
59                 $user = $this->get_user();
60
61                 if (!$user) {
62                         $this->show_form(_t('No such user!'));
63                         return;
64                 }
65
66                 $profile = $this->trimmed('profile');
67
68                 if (!$profile) {
69                         $this->show_form(_t('No such user!'));
70                         return;
71                 }
72
73                 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
74                         $this->show_form(_t('Invalid profile URL (bad format)'));
75                         return;
76                 }
77
78                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
79                 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
80
81                 if (!$yadis || $yadis->failed) {
82                         $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
83                         return;
84                 }
85
86         $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
87
88                 if (!$xrds) {
89                         $this->show_form(_t('Not a valid profile URL (no XRDS defined).'));
90                         return;
91                 }
92
93                 $omb = $this->getOmb($xrds);
94
95                 if (!$omb) {
96                         $this->show_form(_t('Not a valid profile URL (incorrect services).'));
97                         return;
98                 }
99
100                 list($token, $secret) = $this->request_token($omb);
101
102                 if (!$token || !$secret) {
103                         $this->show_form(_t('Couldn\'t get a request token.'));
104                         return;
105                 }
106
107                 $this->request_authorization($user, $omb, $token, $secret);
108         }
109
110         function get_user() {
111                 $user = NULL;
112                 $nickname = $this->trimmed('nickname');
113                 if ($nickname) {
114                         $user = User::staticGet('nickname', $nickname);
115                 }
116                 return $user;
117         }
118
119         function getOmb($xrds) {
120
121             static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
122                 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
123                                                                                 OAUTH_ENDPOINT_ACCESS);
124                 $omb = array();
125
126                 # XXX: the following code could probably be refactored to eliminate dupes
127
128                 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
129
130                 if (!$oauth_services) {
131                         return NULL;
132                 }
133
134                 $oauth_service = $oauth_services[0];
135
136                 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
137
138                 if (!$oauth_xrd) {
139                         return NULL;
140                 }
141
142                 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
143                         return NULL;
144                 }
145
146                 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
147
148                 if (!$omb_services) {
149                         return NULL;
150                 }
151
152                 $omb_service = $omb_services[0];
153
154                 $omb_xrd = $this->getXRD($omb_service, $xrds);
155
156                 if (!$omb_xrd) {
157                         return NULL;
158                 }
159
160                 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
161                         return NULL;
162                 }
163
164                 # XXX: check that we got all the services we needed
165
166                 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
167                         if (!array_key_exists($type, $omb) || !$omb[$type]) {
168                                 return NULL;
169                         }
170                 }
171
172                 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
173                         return NULL;
174                 }
175
176                 return $omb;
177         }
178
179         function getXRD($main_service, $main_xrds) {
180                 $uri = omb_service_uri($main_service);
181                 if (strpos($uri, "#") !== 0) {
182                         # FIXME: more rigorous handling of external service definitions
183                         return NULL;
184                 }
185                 $id = substr($uri, 1);
186                 $nodes = $main_xrds->allXrdNodes;
187                 $parser = $main_xrds->parser;
188                 foreach ($nodes as $node) {
189                         $attrs = $parser->attributes($node);
190                         if (array_key_exists('xml:id', $attrs) &&
191                                 $attrs['xml:id'] == $id) {
192                                 # XXX: trick the constructor into thinking this is the only node
193                                 $bogus_nodes = array($node);
194                                 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
195                         }
196                 }
197                 return NULL;
198         }
199
200         function addServices($xrd, $types, &$omb) {
201                 foreach ($types as $type) {
202                         $matches = omb_get_services($xrd, $type);
203                         if ($matches) {
204                                 $omb[$type] = $matches[0];
205                         } else {
206                                 # no match for type
207                                 return false;
208                         }
209                 }
210                 return true;
211         }
212
213         function request_token($omb) {
214                 $con = omb_oauth_consumer();
215
216                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
217
218                 # XXX: Is this the right thing to do? Strip off GET params and make them
219                 # POST params? Seems wrong to me.
220
221                 $parsed = parse_url($url);
222                 $params = array();
223                 parse_str($parsed['query'], $params);
224
225                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
226
227                 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
228
229                 if (!$listener) {
230                         return NULL;
231                 }
232
233                 $req->set_parameter('omb_listener', $listener);
234                 $req->set_parameter('omb_version', OMB_VERSION_01);
235
236                 # XXX: test to see if endpoint accepts this signature method
237
238                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
239
240                 # We re-use this tool's fetcher, since it's pretty good
241
242                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
243                 
244                 $result = $fetcher->post($req->get_normalized_http_url(),
245                                                                  $req->to_postdata());
246
247                 if ($result->status != 200) {
248                         return NULL;
249                 }
250
251                 parse_str($result->body, $return);
252
253                 return array($return['oauth_token'], $return['oauth_token_secret']);
254         }
255
256         function request_authorization($user, $omb, $token, $secret) {
257                 global $config; # for license URL
258
259                 $con = omb_oauth_consumer();
260                 $tok = new OAuthToken($token, $secret);
261
262                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
263
264                 # XXX: Is this the right thing to do? Strip off GET params and make them
265                 # POST params? Seems wrong to me.
266
267                 $parsed = parse_url($url);
268                 $params = array();
269                 parse_str($parsed['query'], $params);
270
271                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
272
273                 # We send over a ton of information. This lets the other
274                 # server store info about our user, and it lets the current
275                 # user decide if they really want to authorize the subscription.
276
277                 $req->set_parameter('omb_version', OMB_VERSION_01);
278                 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
279                 $req->set_parameter('omb_listenee', $user->uri);
280                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
281                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
282                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
283                 $profile = $user->getProfile();
284                 if ($profile->fullname) {
285                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
286                 }
287                 if ($profile->homepage) {
288                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
289                 }
290                 if ($profile->bio) {
291                         $req->set_parameter('omb_listenee_bio', $profile->bio);
292                 }
293                 if ($profile->location) {
294                         $req->set_parameter('omb_listenee_location', $profile->location);
295                 }
296                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
297                 if ($avatar) {
298                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
299                 }
300
301                 # XXX: add a nonce to prevent replay attacks
302                 
303                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
304
305                 # XXX: test to see if endpoint accepts this signature method
306
307                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
308
309                 # store all our info here
310
311                 $omb['listenee'] = $user->nickname;
312                 $omb['token'] = $token;
313                 $omb['secret'] = $secret;
314
315                 $_SESSION['oauth_authorization_request'] = $omb;
316
317                 # Redirect to authorization service
318
319                 common_redirect($req->to_url());
320                 return;
321         }
322         
323         function make_nonce() {
324                 return common_good_rand(16);
325         }
326 }