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