]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishremotesubscribe.php
client side of distributed subscription almost complete
[quix0rs-gnu-social.git] / actions / finishremotesubscribe.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 FinishremotesubscribeAction 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                 $nonce = $this->trimmed('nonce');
37                 
38                 if (!$omb) {
39                         common_user_error(_t('No nonce returned!'));
40                         return;
41                 }
42                 
43                 $omb = $_SESSION[$nonce];
44                 
45                 if (!$omb) {
46                         common_user_error(_t('Not expecting this response!'));
47                         return;
48                 }
49
50                 $req = OAuthRequest::from_request();
51
52                 $token = $req->get_parameter('oauth_token');
53
54                 # I think this is the success metric
55                 
56                 if ($token != $omb['token']) {
57                         common_user_error(_t('Not authorized.'));
58                         return;
59                 }
60                 
61                 $version = $req->get_parameter('omb_version');
62                 
63                 if ($version != OMB_VERSION_01) {
64                         common_user_error(_t('Unknown version of OMB protocol.'));
65                         return;
66                 }
67                 
68                 $nickname = $req->get_parameter('omb_listener_nickname');
69                 
70                 if (!$nickname) {
71                         common_user_error(_t('No nickname provided by remote server.'));
72                         return;
73                 }
74
75                 $profile_url = $req->get_parameter('omb_listener_profile');
76                 
77                 if (!$profile_url) {
78                         common_user_error(_t('No profile URL returned by server.'));
79                         return;
80                 }
81
82                 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
83                         common_user_error(_t('Invalid profile URL returned by server.'));
84                         return;
85                 }
86
87                 $user = User::staticGet('uri', $omb['listenee']);
88                 
89                 if (!$user) {
90                         common_user_error(_t('User being listened to doesn\'t exist.'));
91                         return;
92                 }
93                 
94                 $fullname = $req->get_parameter('omb_listener_fullname');
95                 $homepage = $req->get_parameter('omb_listener_homepage');
96                 $bio = $req->get_parameter('omb_listener_bio');
97                 $location = $req->get_parameter('omb_listener_location');
98                 $avatar_url = $req->get_parameter('omb_listener_avatar');
99
100                 list($newtok, $newsecret) = $this->access_token($omb);
101                 
102                 if (!$newtok || !$newsecret) {
103                         common_user_error(_t('Couldn\'t convert request tokens to access tokens.'));
104                         return;
105                 }
106                 
107                 # XXX: possible attack point; subscribe and return someone else's profile URI
108                 
109                 $remote = Remote_profile::staticGet('uri', $omb['listener']);
110                 
111                 if ($remote) {
112                         $exists = true;
113                         $profile = Profile::staticGet($remote->id);
114                         $orig_remote = clone($remote);
115                         $orig_profile = clone($profile);
116                         # XXX: compare current postNotice and updateProfile URLs to the ones
117                         # stored in the DB to avoid (possibly...) above attack
118                 } else {
119                         $exists = false;
120                         $remote = new Remote_profile();
121                         $remote->uri = $omb['listener'];
122                         $profile = new Profile();
123                 }
124
125                 $profile->nickname = $nickname;
126                 $profile->profileurl = $profile_url;
127                 
128                 if ($fullname) {
129                         $profile->fullname = $fullname;
130                 }
131                 if ($homepage) {
132                         $profile->homepage = $homepage;
133                 }
134                 if ($bio) {
135                         $profile->bio = $bio;
136                 }
137                 if ($location) {
138                         $profile->location = $location;
139                 }
140                 
141                 if ($exists) {
142                         $profile->update($orig_profile);
143                 } else {
144                         $profile->created = DB_DataObject_Cast::dateTime(); # current time
145                         $id = $profile->insert();
146                         $remote->id = $id;
147                 }
148
149                 if ($avatar_url) {
150                         $this->add_avatar($avatar_url);
151                 }
152
153                 $remote->postnoticeurl = $omb[OMB_ENDPOINT_POSTNOTICE];
154                 $remote->updateprofileurl = $omb[OMB_ENDPOINT_UPDATEPROFILE];
155
156                 if ($exists) {
157                         $remote->update($orig_remote);
158                 } else {
159                         $remote->created = DB_DataObject_Cast::dateTime(); # current time
160                         $remote->insert;
161                 }
162                 
163                 $sub = new Subscription();
164                 $sub->subscriber = $remote->id;
165                 $sub->subscribed = $user->id;
166                 $sub->token = $newtok;
167                 $sub->secret = $newsecret;
168                 $sub->created = DB_DataObject_Cast::dateTime(); # current time
169                 
170                 if (!$sub->insert()) {
171                         common_user_error(_t('Couldn\'t insert new subscription.'));
172                         return;
173                 }
174
175                 # Clear the data
176                 unset($_SESSION[$nonce]);
177                 
178                 # If we show subscriptions in reverse chron order, this should
179                 # show up close to the top of the page
180                 
181                 common_redirect(common_local_url('subscribed', array('nickname' =>
182                                                                                                                          $user->nickname)));
183         }
184         
185         function access_token($omb) {
186                 
187                 $con = omb_oauth_consumer();
188                 $tok = new OAuthToken($omb['token'], $omb['secret']);
189
190                 $url = $omb[OAUTH_ENDPOINT_ACCESS][0];
191                 
192                 # XXX: Is this the right thing to do? Strip off GET params and make them
193                 # POST params? Seems wrong to me.
194                 
195                 $parsed = parse_url($url);
196                 $params = array();
197                 parse_str($parsed['query'], $params);
198
199                 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
200                 
201                 $req->set_parameter('omb_version', OMB_VERSION_01);
202                 
203                 # XXX: test to see if endpoint accepts this signature method
204
205                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
206                 
207                 # We re-use this tool's fetcher, since it's pretty good
208                 
209                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
210                 $result = $fetcher->post($req->get_normalized_http_url(),
211                                                                  $req->to_postdata());
212                 
213                 if ($result->status != 200) {
214                         return NULL;
215                 }
216
217                 parse_str($result->body, $return);
218                 
219                 return array($return['oauth_token'], $return['oauth_token_secret']);
220         }
221 }