]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/actions/userauthorization.php
848cc77a71561e49f73aa27d87a88f94240f28d9
[quix0rs-gnu-social.git] / plugins / OMB / actions / userauthorization.php
1 <?php
2 /**
3  * Let the user authorize a remote subscription request
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
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/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2008-2011, StatusNet, Inc.
16  *
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.
21  *
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.
26  *
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/>.
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
32
33 require_once dirname(__FILE__) . '/../lib/omb.php';
34 require_once dirname(__FILE__) . '/../extlib/libomb/service_provider.php';
35 require_once dirname(__FILE__) . '/../extlib/libomb/profile.php';
36 define('TIMESTAMP_THRESHOLD', 300);
37
38 // @todo FIXME: Missing documentation.
39 class UserauthorizationAction extends Action
40 {
41     var $error;
42     var $params;
43
44     function handle($args)
45     {
46         parent::handle($args);
47
48         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
49             /* Use a session token for CSRF protection. */
50             $token = $this->trimmed('token');
51             if (!$token || $token != common_session_token()) {
52                 $srv = $this->getStoredParams();
53                 // TRANS: Client error displayed when the session token does not match or is not given.
54                 $this->showForm($srv->getRemoteUser(), _('There was a problem ' .
55                                         'with your session token. Try again, ' .
56                                         'please.'));
57                 return;
58             }
59             /* We've shown the form, now post user's choice. */
60             $this->sendAuthorization();
61         } else {
62             if (!common_logged_in()) {
63                 /* Go log in, and then come back. */
64                 common_set_returnto($_SERVER['REQUEST_URI']);
65
66                 common_redirect(common_local_url('login'));
67                 return;
68             }
69
70             $user    = common_current_user();
71             $profile = $user->getProfile();
72             if (!$profile) {
73                 common_log_db_error($user, 'SELECT', __FILE__);
74                 // TRANS: Error message displayed when referring to a user without a profile.
75                 $this->serverError(_('User has no profile.'));
76                 return;
77             }
78
79             /* TODO: If no token is passed the user should get a prompt to enter
80                it according to OAuth Core 1.0. */
81             try {
82                 $this->validateOmb();
83                 $srv = new OMB_Service_Provider(
84                         profile_to_omb_profile($user->uri, $profile),
85                         omb_oauth_datastore());
86
87                 $remote_user = $srv->handleUserAuth();
88             } catch (Exception $e) {
89                 $this->clearParams();
90                 $this->clientError($e->getMessage());
91                 return;
92             }
93
94             $this->storeParams($srv);
95             $this->showForm($remote_user);
96         }
97     }
98
99     function showForm($params, $error=null)
100     {
101         $this->params = $params;
102         $this->error  = $error;
103         $this->showPage();
104     }
105
106     function title()
107     {
108         // TRANS: Page title.
109         return _('Authorize subscription');
110     }
111
112     function showPageNotice()
113     {
114         // TRANS: Page notice on "Authorize subscription" page.
115         $this->element('p', null, _('Please check these details to make sure '.
116                                     'that you want to subscribe to this ' .
117                                     'user’s notices. If you didn’t just ask ' .
118                                     'to subscribe to someone’s notices, '.
119                                     'click "Reject".'));
120     }
121
122     function showContent()
123     {
124         $params = $this->params;
125
126         $nickname = $params->getNickname();
127         $profile  = $params->getProfileURL();
128         $license  = $params->getLicenseURL();
129         $fullname = $params->getFullname();
130         $homepage = $params->getHomepage();
131         $bio      = $params->getBio();
132         $location = $params->getLocation();
133         $avatar   = $params->getAvatarURL();
134
135         $this->elementStart('div', 'entity_profile vcard');
136
137         if ($avatar) {
138             $this->element('img', array('src' => $avatar,
139                                         'class' => 'photo avatar entity_depiction',
140                                         'width' => AVATAR_PROFILE_SIZE,
141                                         'height' => AVATAR_PROFILE_SIZE,
142                                         'alt' => $nickname));
143         }
144
145         // TRANS: Label for nickname on user authorisation page.
146         $this->element('div', 'entity_nickname', _('Nickname'));
147
148         $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
149
150         // XXX: why are these raw() instead of escaped...?
151
152         $this->elementStart('a', array('href' => $profile,
153                                        'class' => 'url '.$hasFN));
154         $this->raw($nickname);
155         $this->elementEnd('a');
156
157         if (!is_null($fullname)) {
158             $this->elementStart('div', 'fn entity_fn');
159             $this->raw($fullname);
160             $this->elementEnd('div');
161         }
162
163         if (!is_null($location)) {
164             $this->elementStart('div', 'label entity_location');
165             $this->raw($location);
166         }
167
168         if (!is_null($homepage)) {
169             $this->elementStart('a', array('href' => $homepage,
170                                            'class' => 'url entity_url'));
171             $this->raw($homepage);
172             $this->elementEnd('a');
173         }
174
175         if (!is_null($bio)) {
176             $this->elementStart('div', 'note entity_note');
177             $this->raw($bio);
178             $this->elementEnd('dd');
179         }
180
181         if (!is_null($license)) {
182             $this->element('a', array('href' => $license,
183                                       'class' => 'license entity_license'),
184                            $license);
185         }
186
187         $this->elementEnd('div');
188
189         $this->elementStart('div', 'entity_actions');
190         $this->elementStart('ul');
191         $this->elementStart('li', 'entity_subscribe');
192         $this->elementStart('form', array('method' => 'post',
193                                           'id' => 'userauthorization',
194                                           'class' => 'form_user_authorization',
195                                           'name' => 'userauthorization',
196                                           'action' => common_local_url(
197                                                          'userauthorization')));
198         $this->hidden('token', common_session_token());
199
200         $this->submit('accept',
201                       // TRANS: Button text on Authorise Subscription page.
202                       _m('BUTTON','Accept'), 'submit accept', null,
203                       // TRANS: Title for button on Authorise Subscription page.
204                       _('Subscribe to this user.'));
205         $this->submit('reject',
206                       // TRANS: Button text on Authorise Subscription page.
207                       _m('BUTTON','Reject'), 'submit reject', null,
208                       // TRANS: Title for button on Authorise Subscription page.
209                       _('Reject this subscription.'));
210         $this->elementEnd('form');
211         $this->elementEnd('li');
212         $this->elementEnd('ul');
213         $this->elementEnd('div');
214     }
215
216     function sendAuthorization()
217     {
218         $srv = $this->getStoredParams();
219
220         if (is_null($srv)) {
221             // TRANS: Client error displayed for an empty authorisation request.
222             $this->clientError(_('No authorization request!'));
223             return;
224         }
225
226         $accepted = $this->arg('accept');
227         try {
228             list($val, $token) = $srv->continueUserAuth($accepted);
229         } catch (Exception $e) {
230             $this->clientError($e->getMessage());
231             return;
232         }
233         if ($val !== false) {
234             common_redirect($val, 303);
235         } elseif ($accepted) {
236             $this->showAcceptMessage($token);
237         } else {
238             $this->showRejectMessage();
239         }
240     }
241
242     function showAcceptMessage($tok)
243     {
244         // TRANS: Accept message header from Authorise subscription page.
245         common_show_header(_('Subscription authorized'));
246         $this->element('p', null,
247                        // TRANS: Accept message text from Authorise subscription page.
248                        _('The subscription has been authorized, but no '.
249                          'callback URL was passed. Check with the site\'s ' .
250                          'instructions for details on how to authorize the ' .
251                          'subscription. Your subscription token is:'));
252         $this->element('blockquote', 'token', $tok);
253         common_show_footer();
254     }
255
256     function showRejectMessage()
257     {
258         // TRANS: Reject message header from Authorise subscription page.
259         common_show_header(_('Subscription rejected'));
260         $this->element('p', null,
261                        // TRANS: Reject message from Authorise subscription page.
262                        _('The subscription has been rejected, but no '.
263                          'callback URL was passed. Check with the site\'s ' .
264                          'instructions for details on how to fully reject ' .
265                          'the subscription.'));
266         common_show_footer();
267     }
268
269     function storeParams($params)
270     {
271         common_ensure_session();
272         $_SESSION['userauthorizationparams'] = serialize($params);
273     }
274
275     function clearParams()
276     {
277         common_ensure_session();
278         unset($_SESSION['userauthorizationparams']);
279     }
280
281     function getStoredParams()
282     {
283         common_ensure_session();
284         $params = unserialize($_SESSION['userauthorizationparams']);
285         return $params;
286     }
287
288     function validateOmb()
289     {
290         $listener = $_GET['omb_listener'];
291         $listenee = $_GET['omb_listenee'];
292         $nickname = $_GET['omb_listenee_nickname'];
293         $profile  = $_GET['omb_listenee_profile'];
294
295         $user = User::getKV('uri', $listener);
296         if (!$user) {
297             // TRANS: Exception thrown when no valid user is found for an authorisation request.
298             // TRANS: %s is a listener URI.
299             throw new Exception(sprintf(_('Listener URI "%s" not found here.'),
300                                         $listener));
301         }
302
303         if (strlen($listenee) > 255) {
304             // TRANS: Exception thrown when listenee URI is too long for an authorisation request.
305             // TRANS: %s is a listenee URI.
306             throw new Exception(sprintf(_('Listenee URI "%s" is too long.'),
307                                         $listenee));
308         }
309
310         $other = User::getKV('uri', $listenee);
311         if ($other) {
312             // TRANS: Exception thrown when listenee URI is a local user for an authorisation request.
313             // TRANS: %s is a listenee URI.
314             throw new Exception(sprintf(_('Listenee URI "%s" is a local user.'),
315                                         $listenee));
316         }
317
318         $remote = Remote_profile::getKV('uri', $listenee);
319         if ($remote) {
320             $sub             = new Subscription();
321             $sub->subscriber = $user->id;
322             $sub->subscribed = $remote->id;
323             if ($sub->find(true)) {
324                 // TRANS: Exception thrown when already subscribed.
325                 throw new Exception('You are already subscribed to this user.');
326             }
327         }
328
329         if ($profile == common_profile_url($nickname)) {
330             // TRANS: Exception thrown when profile URL is a local user for an authorisation request.
331             // TRANS: %s is a profile URL.
332             throw new Exception(sprintf(_('Profile URL "%s" is for a local user.'),
333                                         $profile));
334
335         }
336
337         $license      = $_GET['omb_listenee_license'];
338         $site_license = common_config('license', 'url');
339         if (!common_compatible_license($license, $site_license)) {
340             // TRANS: Exception thrown when licenses are not compatible for an authorisation request.
341             // TRANS: %1$s is the license for the listenee, %2$s is the license for "this" StatusNet site.
342             throw new Exception(sprintf(_('Listenee stream license "%1$s" is not ' .
343                                           'compatible with site license "%2$s".'),
344                                         $license, $site_license));
345         }
346
347         $avatar = $_GET['omb_listenee_avatar'];
348         if ($avatar) {
349             if (!common_valid_http_url($avatar) || strlen($avatar) > 255) {
350                 // TRANS: Exception thrown when avatar URL is invalid for an authorisation request.
351                 // TRANS: %s is an avatar URL.
352                 throw new Exception(sprintf(_('Avatar URL "%s" is not valid.'),
353                                             $avatar));
354             }
355             $size = @getimagesize($avatar);
356             if (!$size) {
357                 // TRANS: Exception thrown when avatar URL could not be read for an authorisation request.
358                 // TRANS: %s is an avatar URL.
359                 throw new Exception(sprintf(_('Cannot read avatar URL "%s".'),
360                                             $avatar));
361             }
362             if (!in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG,
363                                           IMAGETYPE_PNG))) {
364                 // TRANS: Exception thrown when avatar URL return an invalid image type for an authorisation request.
365                 // TRANS: %s is an avatar URL.
366                 throw new Exception(sprintf(_('Wrong image type for avatar URL '.
367                                               '"%s".'), $avatar));
368             }
369         }
370     }
371 }