]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
2e6e0077765c29670926b37a7d94cd64ba6acd80
[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) {
82                         $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
83                         return;
84                 }
85                 
86                 $omb = $this->getOmb($yadis);
87                 
88                 if (!$omb) {
89                         $this->show_form(_t('Not a valid profile URL (incorrect services).'));
90                         return;
91                 }
92                 
93                 list($token, $secret) = $this->request_token($omb);
94                 
95                 if (!$token || !$secret) {
96                         $this->show_form(_t('Couldn\'t get a request token.'));
97                         return;
98                 }
99                 
100                 $this->request_authorization($user, $omb, $token, $secret);
101         }
102         
103         function get_user() {
104                 $user = NULL;
105                 $nickname = $this->trimmed('nickname');
106                 if ($nickname) {
107                         $user = User::staticGet('nickname', $nickname);
108                 }
109                 return $user;
110         }
111
112         function getOmb($yadis) {
113             static $endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE,
114                                                                   OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
115                                                                   OAUTH_ENDPOINT_ACCESS);
116                 $omb = array();
117                 $services = $yadis->services(); # ordered by priority
118                 if (!$services) {
119                         return NULL;
120                 }
121                 
122                 foreach ($services as $service) {
123                         $types = $service->matchTypes($endpoints);
124                         foreach ($types as $type) {
125                                 # We take the first one, since it's the highest priority
126                                 if (!array_key_exists($type, $omb)) {
127                                         # URIs is an array, priority-ordered
128                                         $omb[$type] = $service->getURIs();
129                                         # Special handling for request
130                                         if ($type == OAUTH_ENDPOINT_REQUEST) {
131                                                 $nodes = $service->getElements('LocalID');
132                                                 if (!$nodes) {
133                                                         # error
134                                                         return NULL;
135                                                 }
136                                                 $omb['listener'] = $service->parser->content($nodes[0]);
137                                         }
138                                 }
139                         }
140                 }
141                 foreach ($endpoints as $ep) {
142                         if (!array_key_exists($ep, $omb)) {
143                                 return NULL;
144                         }
145                 }
146                 if (!array_key_exists('listener', $omb)) {
147                         return NULL;
148                 }
149                 return $omb;
150         }
151         
152         function request_token($omb) {
153                 $con = omb_oauth_consumer();
154
155                 $url = $omb[OAUTH_ENDPOINT_REQUEST][0];
156                 
157                 # XXX: Is this the right thing to do? Strip off GET params and make them
158                 # POST params? Seems wrong to me.
159                 
160                 $parsed = parse_url($url);
161                 $params = array();
162                 parse_str($parsed['query'], $params);
163
164                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
165                 
166                 $req->set_parameter('omb_listener', $omb['listener']);
167                 $req->set_parameter('omb_version', OMB_VERSION_01);
168                 
169                 # XXX: test to see if endpoint accepts this signature method
170
171                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
172                 
173                 # We re-use this tool's fetcher, since it's pretty good
174                 
175                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
176                 $result = $fetcher->post($req->get_normalized_http_url(),
177                                                                  $req->to_postdata());
178                 
179                 if ($result->status != 200) {
180                         return NULL;
181                 }
182
183                 parse_str($result->body, $return);
184                 
185                 return array($return['oauth_token'], $return['oauth_token_secret']);
186         }
187         
188         function request_authorization($user, $omb, $token, $secret) {
189                 global $config; # for license URL
190                 
191                 $con = omb_oauth_consumer();
192                 $tok = new OAuthToken($token, $secret);
193                 
194                 $url = $omb[OAUTH_ENDPOINT_AUTHORIZE][0];
195                 
196                 # XXX: Is this the right thing to do? Strip off GET params and make them
197                 # POST params? Seems wrong to me.
198                 
199                 $parsed = parse_url($url);
200                 $params = array();
201                 parse_str($parsed['query'], $params);
202
203                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
204                 
205                 # We send over a ton of information. This lets the other
206                 # server store info about our user, and it lets the current
207                 # user decide if they really want to authorize the subscription.
208                 
209                 $req->set_parameter('omb_version', OMB_VERSION_01);
210                 $req->set_parameter('omb_listener', $omb['listener']);
211                 $req->set_parameter('omb_listenee', $user->uri);
212                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
213                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
214                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
215                 $profile = $user->getProfile();
216                 if ($profile->fullname) {
217                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
218                 }
219                 if ($profile->homepage) {
220                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
221                 }
222                 if ($profile->bio) {
223                         $req->set_parameter('omb_listenee_bio', $profile->bio);
224                 }
225                 if ($profile->location) {
226                         $req->set_parameter('omb_listenee_location', $profile->location);
227                 }
228                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
229                 if ($avatar) {
230                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
231                 }
232
233                 $nonce = $this->make_nonce();
234                 
235                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe',
236                                                                                                                            array('nonce' => $nonce)));
237                                                         
238                 # XXX: test to see if endpoint accepts this signature method
239
240                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
241                 
242                 # store all our info here
243
244                 $omb['listenee'] = $user->nickname;
245                 $omb['token'] = $token;
246                 $omb['secret'] = $secret;
247                 
248                 $_SESSION[$nonce] = $omb;
249                 
250                 # Redirect to authorization service
251                 
252                 common_redirect($req->to_url());
253                 return;
254         }
255 }