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