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