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