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