]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/oauthconnectionssettings.php
OAuth widgets separated into their own files
[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('GNUSOCIAL')) { exit(1); }
31
32 /**
33  * Show connected OAuth applications
34  *
35  * @category Settings
36  * @package  StatusNet
37  * @author   Zach Copley <zach@status.net>
38  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39  * @link     http://status.net/
40  *
41  * @see      SettingsAction
42  */
43 class OauthconnectionssettingsAction extends SettingsAction
44 {
45     var $page        = null;
46     var $oauth_token = null;
47
48     function prepare($args)
49     {
50         parent::prepare($args);
51         $this->oauth_token = $this->arg('oauth_token');
52         $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
53         return true;
54     }
55
56     /**
57      * Title of the page
58      *
59      * @return string Title of the page
60      */
61     function title()
62     {
63         // TRANS: Title for OAuth connection settings.
64         return _('Connected applications');
65     }
66
67     /**
68      * Instructions for use
69      *
70      * @return instructions for use
71      */
72     function getInstructions()
73     {
74         // TRANS: Instructions for OAuth connection settings.
75         return _('The following connections exist for your account.');
76     }
77
78     /**
79      * Content area of the page
80      *
81      * @return void
82      */
83
84     function showContent()
85     {
86         $user    = common_current_user();
87         $profile = $user->getProfile();
88
89         $offset = ($this->page - 1) * APPS_PER_PAGE;
90         $limit  =  APPS_PER_PAGE + 1;
91
92         $connection = $user->getConnectedApps($offset, $limit);
93
94         $cnt = 0;
95
96         if (!empty($connection)) {
97             $cal = new ConnectedAppsList($connection, $user, $this);
98             $cnt = $cal->show();
99         }
100
101         if ($cnt == 0) {
102             $this->showEmptyListMessage();
103         }
104
105         $this->pagination(
106             $this->page > 1,
107             $cnt > APPS_PER_PAGE,
108             $this->page,
109             'connectionssettings',
110             array('nickname' => $user->nickname)
111         );
112     }
113
114     /**
115      * Handle posts to this form
116      *
117      * Based on the button that was pressed, muxes out to other functions
118      * to do the actual task requested.
119      *
120      * All sub-functions reload the form with a message -- success or failure.
121      *
122      * @return void
123      */
124     function handlePost()
125     {
126         // CSRF protection
127
128         $token = $this->trimmed('token');
129         if (!$token || $token != common_session_token()) {
130             // TRANS: Client error displayed when the session token does not match or is not given.
131             $this->showForm(_('There was a problem with your session token. '.
132                               'Try again, please.'));
133             return;
134         }
135
136         if ($this->arg('revoke')) {
137             $this->revokeAccess($this->oauth_token);
138         } else {
139             // TRANS: Client error when submitting a form with unexpected information.
140             $this->clientError(_('Unexpected form submission.'), 401);
141         }
142     }
143
144     /**
145      * Revoke an access token
146      *
147      * XXX: Confirm revoke before doing it
148      *
149      * @param int $appId the ID of the application
150      *
151      */
152     function revokeAccess($token)
153     {
154         $cur = common_current_user();
155
156         $appUser = Oauth_application_user::getByUserAndToken($cur, $token);
157
158         if (empty($appUser)) {
159             // TRANS: Client error when trying to revoke access for an application while not being a user of it.
160             $this->clientError(_('You are not a user of that application.'), 401);
161         }
162
163         $app = Oauth_application::getKV('id', $appUser->application_id);
164
165         $datastore = new ApiGNUsocialOAuthDataStore();
166         $datastore->revoke_token($appUser->token, 1);
167
168         $result = $appUser->delete();
169
170         if (!$result) {
171             common_log_db_error($orig, 'DELETE', __FILE__);
172             // TRANS: Client error when revoking access has failed for some reason.
173             // TRANS: %s is the application ID revoking access failed for.
174             $this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id));
175         }
176
177         $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
178         common_log(
179             LOG_INFO,
180             sprintf(
181                 $msg,
182                 $cur->nickname,
183                 $cur->id,
184                 $appUser->token,
185                 $appUser->application_id
186             )
187         );
188
189         $msg = sprintf(
190             // TRANS: Success message after revoking access for an application.
191             // TRANS: %1$s is the application name, %2$s is the first part of the user token.
192             _('You have successfully revoked access for %1$s and the access token starting with %2$s.'),
193              $app->name,
194              substr($appUser->token, 0, 7)
195         );
196
197         $this->showForm($msg, true);
198     }
199
200     function showEmptyListMessage()
201     {
202         // TRANS: Empty list message when no applications have been authorised yet.
203         $message = _('You have not authorized any applications to use your account.');
204
205         $this->elementStart('div', 'guide');
206         $this->raw(common_markup_to_html($message));
207         $this->elementEnd('div');
208     }
209
210     function showSections()
211     {
212         $cur = common_current_user();
213
214         $this->elementStart('div', array('id' => 'developer-help', 'class' => 'section'));
215
216         $this->element('h2', null, 'Developers');
217         $this->elementStart('p');
218
219         $devMsg = sprintf(
220             // TRANS: Note for developers in the OAuth connection settings form.
221             // TRANS: This message contains a Markdown link. Do not separate "](".
222             // TRANS: %s is the URL to the OAuth settings.
223             _('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'),
224             common_local_url('oauthappssettings')
225         );
226
227         $output = common_markup_to_html($devMsg);
228
229         $this->raw($output);
230         $this->elementEnd('p');
231
232         $this->elementEnd('section');
233     }
234 }