]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
84bd63263bd0d7b21b869c7a07ddc7df55e6b865
[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                 common_debug('remotesubscribe.php: XRDS discovery failure? "'.$yadis->failed.'"');
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                 common_debug('remotesubscribe.php: XRDS is "'.print_r($xrds,TRUE).'"');
96
97                 $omb = $this->getOmb($xrds);
98                 
99                 if (!$omb) {
100                         $this->show_form(_t('Not a valid profile URL (incorrect services).'));
101                         return;
102                 }
103                 
104                 list($token, $secret) = $this->request_token($omb);
105                 
106                 if (!$token || !$secret) {
107                         $this->show_form(_t('Couldn\'t get a request token.'));
108                         return;
109                 }
110                 
111                 $this->request_authorization($user, $omb, $token, $secret);
112         }
113         
114         function get_user() {
115                 $user = NULL;
116                 $nickname = $this->trimmed('nickname');
117                 if ($nickname) {
118                         $user = User::staticGet('nickname', $nickname);
119                 }
120                 return $user;
121         }
122
123         function getOmb($xrds) {
124                 
125             static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
126                 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
127                                                                                 OAUTH_ENDPOINT_ACCESS);
128                 $omb = array();
129
130                 # XXX: the following code could probably be refactored to eliminate dupes
131                 
132                 $oauth_service = $xrds->services(array($this, 'matchOAuth'));
133                 
134                 if (!$oauth_service) {
135                         return NULL;
136                 }
137
138                 $xrd = $this->getXRD($oauth_service, $xrds);
139                 $this->addServices($xrd, $oauth_endpoints, $omb);
140
141                 $omb_service = $xrds->services(array($this, 'matchOMB'));
142
143                 if (!$omb_service) {
144                         return NULL;
145                 }
146
147                 $xrd = $this->getXRD($omb_service, $xrds);
148                 $this->addServices($xrd, $omb_endpoints, $omb);
149                 
150                 # XXX: check that we got all the services we needed
151                 
152                 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
153                         if (!array_key_exists($type, $omb)) {
154                                 return NULL;
155                         }
156                 }
157                 
158                 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
159                         return NULL;
160                 }
161                 
162                 return $omb;
163         }
164
165         function getXRD($main_service, $main_xrds) {
166                 $uri = omb_service_uri($main_service);
167                 if (strpos($uri, "#") !== 0) {
168                         # FIXME: more rigorous handling of external service definitions
169                         return NULL;
170                 }
171                 $id = substr($uri, 1);
172                 $nodes = $main_xrds->allXrdNodes;
173                 $parser = $main_xrds->parser;
174                 foreach ($nodes as $node) {
175                         $attrs = $parser->attributes($node);
176                         if (array_key_exists('xml:id', $attrs) &&
177                                 $attrs['xml:id'] == $id) {
178                                 return new Auth_Yadis_XRDS($parser, array($node));
179                         }
180                 }
181                 return NULL;
182         }
183
184         function addServices($xrd, $types, &$omb) {
185                 foreach ($types as $type) {
186                         $filter = create_function('$s', 
187                                                                           'return RemotesubscribeAction::matchService($s, \''.$type.'\'');
188                         $matches = $xrd->services($filter);
189                         if ($matches) {
190                                 $omb[$type] = $services[0];
191                         } else {
192                                 # no match for type
193                                 return false;
194                         }
195                 }
196                 return true;
197         }
198         
199         function matchOAuth($service) {
200                 return $this->matchService($service, OAUTH_DISCOVERY);
201         }
202
203         function matchOMB($service) {
204                 return $this->matchService($service, OMB_VERSION_01);
205         }
206
207         function matchService($service, $type) {
208                 if ($service && $service->matchTypes(array($type))) {
209                         return TRUE;
210                 } else {
211                         return FALSE;
212                 }
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                 $result = $fetcher->post($req->get_normalized_http_url(),
246                                                                  $req->to_postdata());
247                 
248                 if ($result->status != 200) {
249                         return NULL;
250                 }
251
252                 parse_str($result->body, $return);
253                 
254                 return array($return['oauth_token'], $return['oauth_token_secret']);
255         }
256         
257         function request_authorization($user, $omb, $token, $secret) {
258                 global $config; # for license URL
259                 
260                 $con = omb_oauth_consumer();
261                 $tok = new OAuthToken($token, $secret);
262                 
263                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
264                 
265                 # XXX: Is this the right thing to do? Strip off GET params and make them
266                 # POST params? Seems wrong to me.
267                 
268                 $parsed = parse_url($url);
269                 $params = array();
270                 parse_str($parsed['query'], $params);
271
272                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
273                 
274                 # We send over a ton of information. This lets the other
275                 # server store info about our user, and it lets the current
276                 # user decide if they really want to authorize the subscription.
277                 
278                 $req->set_parameter('omb_version', OMB_VERSION_01);
279                 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
280                 $req->set_parameter('omb_listenee', $user->uri);
281                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
282                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
283                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
284                 $profile = $user->getProfile();
285                 if ($profile->fullname) {
286                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
287                 }
288                 if ($profile->homepage) {
289                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
290                 }
291                 if ($profile->bio) {
292                         $req->set_parameter('omb_listenee_bio', $profile->bio);
293                 }
294                 if ($profile->location) {
295                         $req->set_parameter('omb_listenee_location', $profile->location);
296                 }
297                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
298                 if ($avatar) {
299                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
300                 }
301
302                 $nonce = $this->make_nonce();
303                 
304                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe',
305                                                                                                                            array('nonce' => $nonce)));
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['token'] = $token;
315                 $omb['secret'] = $secret;
316                 
317                 $_SESSION[$nonce] = $omb;
318                 
319                 # Redirect to authorization service
320                 
321                 common_redirect($req->to_url());
322                 return;
323         }
324 }