]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/userauthorization.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / 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, 2009, 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 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);
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                 $this->showForm($srv->getRemoteUser(), _('There was a problem ' .
54                                         'with your session token. Try again, ' .
55                                         'please.'));
56                 return;
57             }
58             /* We've shown the form, now post user's choice. */
59             $this->sendAuthorization();
60         } else {
61             if (!common_logged_in()) {
62                 /* Go log in, and then come back. */
63                 common_set_returnto($_SERVER['REQUEST_URI']);
64
65                 common_redirect(common_local_url('login'));
66                 return;
67             }
68
69             $user    = common_current_user();
70             $profile = $user->getProfile();
71             if (!$profile) {
72                 common_log_db_error($user, 'SELECT', __FILE__);
73                 // TRANS: Server error displayed when trying to authorise a remote subscription request
74                 // TRANS: while the user has no profile.
75                 $this->serverError(_('User without matching 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 "Auhtorize 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         $this->element('div', 'entity_nickname', _('Nickname'));
146
147         $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
148
149         // XXX: why are these raw() instead of escaped...?
150
151         $this->elementStart('a', array('href' => $profile,
152                                        'class' => 'url '.$hasFN));
153         $this->raw($nickname);
154         $this->elementEnd('a');
155
156         if (!is_null($fullname)) {
157             $this->elementStart('div', 'fn entity_fn');
158             $this->raw($fullname);
159             $this->elementEnd('div');
160         }
161
162         if (!is_null($location)) {
163             $this->elementStart('div', 'label entity_location');
164             $this->raw($location);
165         }
166
167         if (!is_null($homepage)) {
168             $this->elementStart('a', array('href' => $homepage,
169                                            'class' => 'url entity_url'));
170             $this->raw($homepage);
171             $this->elementEnd('a');
172         }
173
174         if (!is_null($bio)) {
175             $this->elementStart('div', 'note entity_note');
176             $this->raw($bio);
177             $this->elementEnd('dd');
178         }
179
180         if (!is_null($license)) {
181             $this->element('a', array('href' => $license,
182                                       'class' => 'license entity_license'),
183                            $license);
184         }
185
186         $this->elementEnd('div');
187
188         $this->elementStart('div', 'entity_actions');
189         $this->elementStart('ul');
190         $this->elementStart('li', 'entity_subscribe');
191         $this->elementStart('form', array('method' => 'post',
192                                           'id' => 'userauthorization',
193                                           'class' => 'form_user_authorization',
194                                           'name' => 'userauthorization',
195                                           'action' => common_local_url(
196                                                          'userauthorization')));
197         $this->hidden('token', common_session_token());
198
199         // TRANS: Button text on Authorise Subscription page.
200         $this->submit('accept', _m('BUTTON','Accept'), 'submit accept', null,
201                       // TRANS: Title for button on Authorise Subscription page.
202                       _('Subscribe to this user.'));
203         // TRANS: Button text on Authorise Subscription page.
204         $this->submit('reject', _m('BUTTON','Reject'), 'submit reject', null,
205                       // TRANS: Title for button on Authorise Subscription page.
206                       _('Reject this subscription.'));
207         $this->elementEnd('form');
208         $this->elementEnd('li');
209         $this->elementEnd('ul');
210         $this->elementEnd('div');
211     }
212
213     function sendAuthorization()
214     {
215         $srv = $this->getStoredParams();
216
217         if (is_null($srv)) {
218             // TRANS: Client error displayed for an empty authorisation request.
219             $this->clientError(_('No authorization request!'));
220             return;
221         }
222
223         $accepted = $this->arg('accept');
224         try {
225             list($val, $token) = $srv->continueUserAuth($accepted);
226         } catch (Exception $e) {
227             $this->clientError($e->getMessage());
228             return;
229         }
230         if ($val !== false) {
231             common_redirect($val, 303);
232         } elseif ($accepted) {
233             $this->showAcceptMessage($token);
234         } else {
235             $this->showRejectMessage();
236         }
237     }
238
239     function showAcceptMessage($tok)
240     {
241         // TRANS: Accept message header from Authorise subscription page.
242         common_show_header(_('Subscription authorized'));
243         // TRANS: Accept message text from Authorise subscription page.
244         $this->element('p', null,
245                        _('The subscription has been authorized, but no '.
246                          'callback URL was passed. Check with the site’s ' .
247                          'instructions for details on how to authorize the ' .
248                          'subscription. Your subscription token is:'));
249         $this->element('blockquote', 'token', $tok);
250         common_show_footer();
251     }
252
253     function showRejectMessage()
254     {
255         // TRANS: Reject message header from Authorise subscription page.
256         common_show_header(_('Subscription rejected'));
257         // TRANS: Reject message from Authorise subscription page.
258         $this->element('p', null,
259                        _('The subscription has been rejected, but no '.
260                          'callback URL was passed. Check with the site’s ' .
261                          'instructions for details on how to fully reject ' .
262                          'the subscription.'));
263         common_show_footer();
264     }
265
266     function storeParams($params)
267     {
268         common_ensure_session();
269         $_SESSION['userauthorizationparams'] = serialize($params);
270     }
271
272     function clearParams()
273     {
274         common_ensure_session();
275         unset($_SESSION['userauthorizationparams']);
276     }
277
278     function getStoredParams()
279     {
280         common_ensure_session();
281         $params = unserialize($_SESSION['userauthorizationparams']);
282         return $params;
283     }
284
285     function validateOmb()
286     {
287         $listener = $_GET['omb_listener'];
288         $listenee = $_GET['omb_listenee'];
289         $nickname = $_GET['omb_listenee_nickname'];
290         $profile  = $_GET['omb_listenee_profile'];
291
292         $user = User::staticGet('uri', $listener);
293         if (!$user) {
294             // TRANS: Exception thrown when no valid user is found for an authorisation request.
295             // TRANS: %s is a listener URI.
296             throw new Exception(sprintf(_('Listener URI "%s" not found here.'),
297                                         $listener));
298         }
299
300         if (strlen($listenee) > 255) {
301             // TRANS: Exception thrown when listenee URI is too long for an authorisation request.
302             // TRANS: %s is a listenee URI.
303             throw new Exception(sprintf(_('Listenee URI "%s" is too long.'),
304                                         $listenee));
305         }
306
307         $other = User::staticGet('uri', $listenee);
308         if ($other) {
309             // TRANS: Exception thrown when listenee URI is a local user for an authorisation request.
310             // TRANS: %s is a listenee URI.
311             throw new Exception(sprintf(_('Listenee URI "%s" is a local user.'),
312                                         $listenee));
313         }
314
315         $remote = Remote_profile::staticGet('uri', $listenee);
316         if ($remote) {
317             $sub             = new Subscription();
318             $sub->subscriber = $user->id;
319             $sub->subscribed = $remote->id;
320             if ($sub->find(true)) {
321                 // TRANS: Exception thrown when already subscribed.
322                 throw new Exception('You are already subscribed to this user.');
323             }
324         }
325
326         if ($profile == common_profile_url($nickname)) {
327             // TRANS: Exception thrown when profile URL is a local user for an authorisation request.
328             // TRANS: %s is a profile URL.
329             throw new Exception(sprintf(_('Profile URL "%s" is for a local user.'),
330                                         $profile));
331
332         }
333
334         $license      = $_GET['omb_listenee_license'];
335         $site_license = common_config('license', 'url');
336         if (!common_compatible_license($license, $site_license)) {
337             // TRANS: Exception thrown when licenses are not compatible for an authorisation request.
338             // TRANS: %1$s is the license for the listenee, %2$s is the license for "this" StatusNet site.
339             throw new Exception(sprintf(_('Listenee stream license "%1$s" is not ' .
340                                           'compatible with site license "%2$s".'),
341                                         $license, $site_license));
342         }
343
344         $avatar = $_GET['omb_listenee_avatar'];
345         if ($avatar) {
346             if (!common_valid_http_url($avatar) || strlen($avatar) > 255) {
347                 // TRANS: Exception thrown when avatar URL is invalid for an authorisation request.
348                 // TRANS: %s is an avatar URL.
349                 throw new Exception(sprintf(_('Avatar URL "%s" is not valid.'),
350                                             $avatar));
351             }
352             $size = @getimagesize($avatar);
353             if (!$size) {
354                 // TRANS: Exception thrown when avatar URL could not be read for an authorisation request.
355                 // TRANS: %s is an avatar URL.
356                 throw new Exception(sprintf(_('Cannot read avatar URL "%s".'),
357                                             $avatar));
358             }
359             if (!in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG,
360                                           IMAGETYPE_PNG))) {
361                 // TRANS: Exception thrown when avatar URL return an invalid image type for an authorisation request.
362                 // TRANS: %s is an avatar URL.
363                 throw new Exception(sprintf(_('Wrong image type for avatar URL '.
364                                               '"%s".'), $avatar));
365             }
366         }
367     }
368 }