3 * Let the user authorize a remote subscription request
9 * @author Evan Prodromou <evan@status.net>
10 * @author Robin Millette <millette@status.net>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://status.net/
14 * StatusNet - the distributed open-source microblogging tool
15 * Copyright (C) 2008, 2009, StatusNet, Inc.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Affero General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
33 require_once INSTALLDIR.'/lib/omb.php';
34 require_once INSTALLDIR.'/extlib/libomb/service_provider.php';
35 require_once INSTALLDIR.'/extlib/libomb/profile.php';
36 define('TIMESTAMP_THRESHOLD', 300);
38 class UserauthorizationAction extends Action
43 function handle($args)
45 parent::handle($args);
47 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
48 /* Use a session token for CSRF protection. */
49 $token = $this->trimmed('token');
50 if (!$token || $token != common_session_token()) {
51 $srv = $this->getStoredParams();
52 $this->showForm($srv->getRemoteUser(), _('There was a problem ' .
53 'with your session token. Try again, ' .
57 /* We've shown the form, now post user's choice. */
58 $this->sendAuthorization();
60 if (!common_logged_in()) {
61 /* Go log in, and then come back. */
62 common_set_returnto($_SERVER['REQUEST_URI']);
64 common_redirect(common_local_url('login'));
68 $user = common_current_user();
69 $profile = $user->getProfile();
71 common_log_db_error($user, 'SELECT', __FILE__);
72 $this->serverError(_('User without matching profile'));
76 /* TODO: If no token is passed the user should get a prompt to enter
77 it according to OAuth Core 1.0. */
80 $srv = new OMB_Service_Provider(
81 profile_to_omb_profile($user->uri, $profile),
82 omb_oauth_datastore());
84 $remote_user = $srv->handleUserAuth();
85 } catch (Exception $e) {
87 $this->clientError($e->getMessage());
91 $this->storeParams($srv);
92 $this->showForm($remote_user);
96 function showForm($params, $error=null)
98 $this->params = $params;
99 $this->error = $error;
105 return _('Authorize subscription');
108 function showPageNotice()
110 $this->element('p', null, _('Please check these details to make sure '.
111 'that you want to subscribe to this ' .
112 'user’s notices. If you didn’t just ask ' .
113 'to subscribe to someone’s notices, '.
117 function showContent()
119 $params = $this->params;
121 $nickname = $params->getNickname();
122 $profile = $params->getProfileURL();
123 $license = $params->getLicenseURL();
124 $fullname = $params->getFullname();
125 $homepage = $params->getHomepage();
126 $bio = $params->getBio();
127 $location = $params->getLocation();
128 $avatar = $params->getAvatarURL();
130 $this->elementStart('div', array('class' => 'profile'));
131 $this->elementStart('div', 'entity_profile vcard');
132 $this->elementStart('a', array('href' => $profile,
135 $this->element('img', array('src' => $avatar,
136 'class' => 'photo avatar',
137 'width' => AVATAR_PROFILE_SIZE,
138 'height' => AVATAR_PROFILE_SIZE,
139 'alt' => $nickname));
141 $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
142 $this->elementStart('span', $hasFN);
143 $this->raw($nickname);
144 $this->elementEnd('span');
145 $this->elementEnd('a');
147 if (!is_null($fullname)) {
148 $this->elementStart('dl', 'entity_fn');
149 $this->elementStart('dd');
150 $this->elementStart('span', 'fn');
151 $this->raw($fullname);
152 $this->elementEnd('span');
153 $this->elementEnd('dd');
154 $this->elementEnd('dl');
156 if (!is_null($location)) {
157 $this->elementStart('dl', 'entity_location');
158 $this->element('dt', null, _('Location'));
159 $this->elementStart('dd', 'label');
160 $this->raw($location);
161 $this->elementEnd('dd');
162 $this->elementEnd('dl');
165 if (!is_null($homepage)) {
166 $this->elementStart('dl', 'entity_url');
167 $this->element('dt', null, _('URL'));
168 $this->elementStart('dd');
169 $this->elementStart('a', array('href' => $homepage,
171 $this->raw($homepage);
172 $this->elementEnd('a');
173 $this->elementEnd('dd');
174 $this->elementEnd('dl');
177 if (!is_null($bio)) {
178 $this->elementStart('dl', 'entity_note');
179 $this->element('dt', null, _('Note'));
180 $this->elementStart('dd', 'note');
182 $this->elementEnd('dd');
183 $this->elementEnd('dl');
186 if (!is_null($license)) {
187 $this->elementStart('dl', 'entity_license');
188 $this->element('dt', null, _('License'));
189 $this->elementStart('dd', 'license');
190 $this->element('a', array('href' => $license,
191 'class' => 'license'),
193 $this->elementEnd('dd');
194 $this->elementEnd('dl');
196 $this->elementEnd('div');
198 $this->elementStart('div', 'entity_actions');
199 $this->elementStart('ul');
200 $this->elementStart('li', 'entity_subscribe');
201 $this->elementStart('form', array('method' => 'post',
202 'id' => 'userauthorization',
203 'class' => 'form_user_authorization',
204 'name' => 'userauthorization',
205 'action' => common_local_url(
206 'userauthorization')));
207 $this->hidden('token', common_session_token());
209 $this->submit('accept', _('Accept'), 'submit accept', null,
210 _('Subscribe to this user'));
211 $this->submit('reject', _('Reject'), 'submit reject', null,
212 _('Reject this subscription'));
213 $this->elementEnd('form');
214 $this->elementEnd('li');
215 $this->elementEnd('ul');
216 $this->elementEnd('div');
217 $this->elementEnd('div');
220 function sendAuthorization()
222 $srv = $this->getStoredParams();
225 $this->clientError(_('No authorization request!'));
229 $accepted = $this->arg('accept');
231 list($val, $token) = $srv->continueUserAuth($accepted);
232 } catch (Exception $e) {
233 $this->clientError($e->getMessage());
236 if ($val !== false) {
237 common_redirect($val, 303);
238 } elseif ($accepted) {
239 $this->showAcceptMessage($token);
241 $this->showRejectMessage();
245 function showAcceptMessage($tok)
247 common_show_header(_('Subscription authorized'));
248 $this->element('p', null,
249 _('The subscription has been authorized, but no '.
250 'callback URL was passed. Check with the site’s ' .
251 'instructions for details on how to authorize the ' .
252 'subscription. Your subscription token is:'));
253 $this->element('blockquote', 'token', $tok);
254 common_show_footer();
257 function showRejectMessage()
259 common_show_header(_('Subscription rejected'));
260 $this->element('p', null,
261 _('The subscription has been rejected, but no '.
262 'callback URL was passed. Check with the site’s ' .
263 'instructions for details on how to fully reject ' .
264 'the subscription.'));
265 common_show_footer();
268 function storeParams($params)
270 common_ensure_session();
271 $_SESSION['userauthorizationparams'] = serialize($params);
274 function clearParams()
276 common_ensure_session();
277 unset($_SESSION['userauthorizationparams']);
280 function getStoredParams()
282 common_ensure_session();
283 $params = unserialize($_SESSION['userauthorizationparams']);
287 function validateOmb()
289 $listener = $_GET['omb_listener'];
290 $listenee = $_GET['omb_listenee'];
291 $nickname = $_GET['omb_listenee_nickname'];
292 $profile = $_GET['omb_listenee_profile'];
294 $user = User::staticGet('uri', $listener);
296 throw new Exception(sprintf(_('Listener URI ‘%s’ not found here'),
300 if (strlen($listenee) > 255) {
301 throw new Exception(sprintf(_('Listenee URI ‘%s’ is too long.'),
305 $other = User::staticGet('uri', $listenee);
307 throw new Exception(sprintf(_('Listenee URI ‘%s’ is a local user.'),
311 $remote = Remote_profile::staticGet('uri', $listenee);
313 $sub = new Subscription();
314 $sub->subscriber = $user->id;
315 $sub->subscribed = $remote->id;
316 if ($sub->find(true)) {
317 throw new Exception('You are already subscribed to this user.');
321 if ($profile == common_profile_url($nickname)) {
322 throw new Exception(sprintf(_('Profile URL ‘%s’ is for a local user.'),
327 $license = $_GET['omb_listenee_license'];
328 $site_license = common_config('license', 'url');
329 if (!common_compatible_license($license, $site_license)) {
330 throw new Exception(sprintf(_('Listenee stream license ‘%s’ is not ' .
331 'compatible with site license ‘%s’.'),
332 $license, $site_license));
335 $avatar = $_GET['omb_listenee_avatar'];
337 if (!common_valid_http_url($avatar) || strlen($avatar) > 255) {
338 throw new Exception(sprintf(_('Avatar URL ‘%s’ is not valid.'),
341 $size = @getimagesize($avatar);
343 throw new Exception(sprintf(_('Can’t read avatar URL ‘%s’.'),
346 if (!in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG,
348 throw new Exception(sprintf(_('Wrong image type for avatar URL '.