]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
parse the XRDS results
[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             static $endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE,
125                                                                   OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
126                                                                   OAUTH_ENDPOINT_ACCESS);
127                 $omb = array();
128                 $services = $xrds->services(); # ordered by priority
129                 if (!$services) {
130                         common_debug('remotesubscribe.php: Got no services back from XRDS.');
131                         return NULL;
132                 }
133                 
134                 common_debug('remotesubscribe.php: XRDS service count: '.count($services));
135                 
136                 foreach ($services as $service) {
137                         common_debug('remotesubscribe.php: XRDS service "'.print_r($service,TRUE).'"');
138                         $types = $service->matchTypes($endpoints);
139                         foreach ($types as $type) {
140                                 # We take the first one, since it's the highest priority
141                                 if (!array_key_exists($type, $omb)) {
142                                         # URIs is an array, priority-ordered
143                                         $omb[$type] = $service->getURIs();
144                                         # Special handling for request
145                                         if ($type == OAUTH_ENDPOINT_REQUEST) {
146                                                 $nodes = $service->getElements('LocalID');
147                                                 if (!$nodes) {
148                                                         # error
149                                                         return NULL;
150                                                 }
151                                                 $omb['listener'] = $service->parser->content($nodes[0]);
152                                         }
153                                 }
154                         }
155                 }
156                 foreach ($endpoints as $ep) {
157                         if (!array_key_exists($ep, $omb)) {
158                                 return NULL;
159                         }
160                 }
161                 if (!array_key_exists('listener', $omb)) {
162                         return NULL;
163                 }
164                 return $omb;
165         }
166         
167         function request_token($omb) {
168                 $con = omb_oauth_consumer();
169
170                 $url = $omb[OAUTH_ENDPOINT_REQUEST][0];
171                 
172                 # XXX: Is this the right thing to do? Strip off GET params and make them
173                 # POST params? Seems wrong to me.
174                 
175                 $parsed = parse_url($url);
176                 $params = array();
177                 parse_str($parsed['query'], $params);
178
179                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
180                 
181                 $req->set_parameter('omb_listener', $omb['listener']);
182                 $req->set_parameter('omb_version', OMB_VERSION_01);
183                 
184                 # XXX: test to see if endpoint accepts this signature method
185
186                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
187                 
188                 # We re-use this tool's fetcher, since it's pretty good
189                 
190                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
191                 $result = $fetcher->post($req->get_normalized_http_url(),
192                                                                  $req->to_postdata());
193                 
194                 if ($result->status != 200) {
195                         return NULL;
196                 }
197
198                 parse_str($result->body, $return);
199                 
200                 return array($return['oauth_token'], $return['oauth_token_secret']);
201         }
202         
203         function request_authorization($user, $omb, $token, $secret) {
204                 global $config; # for license URL
205                 
206                 $con = omb_oauth_consumer();
207                 $tok = new OAuthToken($token, $secret);
208                 
209                 $url = $omb[OAUTH_ENDPOINT_AUTHORIZE][0];
210                 
211                 # XXX: Is this the right thing to do? Strip off GET params and make them
212                 # POST params? Seems wrong to me.
213                 
214                 $parsed = parse_url($url);
215                 $params = array();
216                 parse_str($parsed['query'], $params);
217
218                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
219                 
220                 # We send over a ton of information. This lets the other
221                 # server store info about our user, and it lets the current
222                 # user decide if they really want to authorize the subscription.
223                 
224                 $req->set_parameter('omb_version', OMB_VERSION_01);
225                 $req->set_parameter('omb_listener', $omb['listener']);
226                 $req->set_parameter('omb_listenee', $user->uri);
227                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
228                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
229                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
230                 $profile = $user->getProfile();
231                 if ($profile->fullname) {
232                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
233                 }
234                 if ($profile->homepage) {
235                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
236                 }
237                 if ($profile->bio) {
238                         $req->set_parameter('omb_listenee_bio', $profile->bio);
239                 }
240                 if ($profile->location) {
241                         $req->set_parameter('omb_listenee_location', $profile->location);
242                 }
243                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
244                 if ($avatar) {
245                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
246                 }
247
248                 $nonce = $this->make_nonce();
249                 
250                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe',
251                                                                                                                            array('nonce' => $nonce)));
252                                                         
253                 # XXX: test to see if endpoint accepts this signature method
254
255                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
256                 
257                 # store all our info here
258
259                 $omb['listenee'] = $user->nickname;
260                 $omb['token'] = $token;
261                 $omb['secret'] = $secret;
262                 
263                 $_SESSION[$nonce] = $omb;
264                 
265                 # Redirect to authorization service
266                 
267                 common_redirect($req->to_url());
268                 return;
269         }
270 }