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