]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/oauthconnectionssettings.php
* i18n/L10n fixes.
[quix0rs-gnu-social.git] / actions / oauthconnectionssettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List a user's OAuth connected applications
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008-2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/connectsettingsaction.php';
35 require_once INSTALLDIR . '/lib/applicationlist.php';
36 require_once INSTALLDIR . '/lib/apioauthstore.php';
37
38 /**
39  * Show connected OAuth applications
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  *
47  * @see      SettingsAction
48  */
49 class OauthconnectionssettingsAction extends ConnectSettingsAction
50 {
51     var $page        = null;
52     var $oauth_token = null;
53
54     function prepare($args)
55     {
56         parent::prepare($args);
57         $this->oauth_token = $this->arg('oauth_token');
58         $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
59         return true;
60     }
61
62     /**
63      * Title of the page
64      *
65      * @return string Title of the page
66      */
67
68     function title()
69     {
70         // TRANS: Title for OAuth connection settings.
71         return _('Connected applications');
72     }
73
74     /**
75      * Instructions for use
76      *
77      * @return instructions for use
78      */
79
80     function getInstructions()
81     {
82         // TRANS: Instructions for OAuth connection settings.
83         return _('The following connections exist for your account.');
84     }
85
86     /**
87      * Content area of the page
88      *
89      * @return void
90      */
91
92     function showContent()
93     {
94         $user    = common_current_user();
95         $profile = $user->getProfile();
96
97         $offset = ($this->page - 1) * APPS_PER_PAGE;
98         $limit  =  APPS_PER_PAGE + 1;
99
100         $connection = $profile->getConnectedApps($offset, $limit);
101
102         $cnt = 0;
103
104         if (!empty($connection)) {
105             $cal = new ConnectedAppsList($connection, $user, $this);
106             $cnt = $cal->show();
107         }
108
109         if ($cnt == 0) {
110             $this->showEmptyListMessage();
111         }
112
113         $this->pagination(
114             $this->page > 1,
115             $cnt > APPS_PER_PAGE,
116             $this->page,
117             'connectionssettings',
118             array('nickname' => $user->nickname)
119         );
120     }
121
122     /**
123      * Handle posts to this form
124      *
125      * Based on the button that was pressed, muxes out to other functions
126      * to do the actual task requested.
127      *
128      * All sub-functions reload the form with a message -- success or failure.
129      *
130      * @return void
131      */
132     function handlePost()
133     {
134         // CSRF protection
135
136         $token = $this->trimmed('token');
137         if (!$token || $token != common_session_token()) {
138             $this->showForm(_('There was a problem with your session token. '.
139                               'Try again, please.'));
140             return;
141         }
142
143         if ($this->arg('revoke')) {
144             $this->revokeAccess($this->oauth_token);
145         } else {
146             // TRANS: Client error when submitting a form with unexpected information.
147             $this->clientError(_('Unexpected form submission.'), 401);
148             return false;
149         }
150     }
151
152     /**
153      * Revoke an access token
154      *
155      * XXX: Confirm revoke before doing it
156      *
157      * @param int $appId the ID of the application
158      *
159      */
160     function revokeAccess($token)
161     {
162         $cur = common_current_user();
163
164         $appUser = Oauth_application_user::getByUserAndToken($cur, $token);
165
166         if (empty($appUser)) {
167             // TRANS: Client error when trying to revoke access for an application while not being a user of it.
168             $this->clientError(_('You are not a user of that application.'), 401);
169             return false;
170         }
171
172         $app = Oauth_application::staticGet('id', $appUser->application_id);
173
174         $datastore = new ApiStatusNetOAuthDataStore();
175         $datastore->revoke_token($appUser->token, 1);
176
177         $result = $appUser->delete();
178
179         if (!$result) {
180             common_log_db_error($orig, 'DELETE', __FILE__);
181             // TRANS: Client error when revoking access has failed for some reason.
182             // TRANS: %s is the application ID revoking access failed for.
183             $this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id));
184             return false;
185         }
186
187         $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
188         common_log(
189             LOG_INFO,
190             sprintf(
191                 $msg,
192                 $cur->nickname,
193                 $cur->id,
194                 $appUser->token,
195                 $appUser->application_id
196             )
197         );
198
199         $msg = sprintf(
200             // TRANS: Success message after revoking access for an application.
201             // TRANS: %1$s is the application name, %2$s is the first part of the user token.
202             _('You have successfully revoked access for %1$s and the access token starting with %2$s.'),
203              $app->name,
204              substr($appUser->token, 0, 7)
205         );
206
207         $this->showForm($msg, true);
208     }
209
210     function showEmptyListMessage()
211     {
212         // TRANS: Empty list message when no applications have been authorised yet.
213         $message = _('You have not authorized any applications to use your account.');
214
215         $this->elementStart('div', 'guide');
216         $this->raw(common_markup_to_html($message));
217         $this->elementEnd('div');
218     }
219
220     function showSections()
221     {
222         $cur = common_current_user();
223
224         $this->elementStart('div', array('id' => 'developer-help', 'class' => 'section'));
225
226         $this->element('h2', null, 'Developers');
227         $this->elementStart('p');
228
229         $devMsg = sprintf(
230             // TRANS: Note for developers in the OAuth connection settings form.
231             // TRANS: This message contains a Markdown link. Do not separate "](".
232             // TRANS: %s is the URL to the OAuth settings.
233             _('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'),
234             common_local_url('oauthappssettings')
235         );
236
237         $output = common_markup_to_html($devMsg);
238
239         $this->raw($output);
240         $this->elementEnd('p');
241
242         $this->elementEnd('section');
243     }
244 }