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