]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/oauthconnectionssettings.php
Merge branch 'testing' into 0.9.x
[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-2009 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
37 /**
38  * Show connected OAuth applications
39  *
40  * @category Settings
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  *
46  * @see      SettingsAction
47  */
48
49 class OauthconnectionssettingsAction extends ConnectSettingsAction
50 {
51
52     var $page = null;
53     var $id   = null;
54
55     function prepare($args)
56     {
57         parent::prepare($args);
58         $this->id = (int)$this->arg('id');
59         $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
60         return true;
61     }
62
63     /**
64      * Title of the page
65      *
66      * @return string Title of the page
67      */
68
69     function title()
70     {
71         return _('Connected applications');
72     }
73
74     function isReadOnly($args)
75     {
76         return true;
77     }
78
79     /**
80      * Instructions for use
81      *
82      * @return instructions for use
83      */
84
85     function getInstructions()
86     {
87         return _('You have allowed the following applications to access you account.');
88     }
89
90     /**
91      * Content area of the page
92      *
93      * @return void
94      */
95
96     function showContent()
97     {
98         $user    = common_current_user();
99         $profile = $user->getProfile();
100
101         $offset = ($this->page - 1) * APPS_PER_PAGE;
102         $limit  =  APPS_PER_PAGE + 1;
103
104         $application = $profile->getApplications($offset, $limit);
105
106         $cnt == 0;
107
108         if (!empty($application)) {
109             $al = new ApplicationList($application, $user, $this, true);
110             $cnt = $al->show();
111         }
112
113         if ($cnt == 0) {
114             $this->showEmptyListMessage();
115         }
116
117         $this->pagination($this->page > 1, $cnt > APPS_PER_PAGE,
118                           $this->page, 'connectionssettings',
119                           array('nickname' => $this->user->nickname));
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
133     function handlePost()
134     {
135         // CSRF protection
136
137         $token = $this->trimmed('token');
138         if (!$token || $token != common_session_token()) {
139             $this->showForm(_('There was a problem with your session token. '.
140                               'Try again, please.'));
141             return;
142         }
143
144         if ($this->arg('revoke')) {
145             $this->revokeAccess($this->id);
146
147             // XXX: Show some indicator to the user of what's been done.
148
149             $this->showPage();
150         } else {
151             $this->clientError(_('Unexpected form submission.'), 401);
152             return false;
153         }
154     }
155
156     function revokeAccess($appId)
157     {
158         $cur = common_current_user();
159
160         $app = Oauth_application::staticGet('id', $appId);
161
162         if (empty($app)) {
163             $this->clientError(_('No such application.'), 404);
164             return false;
165         }
166
167         $appUser = Oauth_application_user::getByKeys($cur, $app);
168
169         if (empty($appUser)) {
170             $this->clientError(_('You are not a user of that application.'), 401);
171             return false;
172         }
173
174         $orig = clone($appUser);
175         $appUser->access_type = 0;  // No access
176         $result = $appUser->update();
177
178         if (!$result) {
179             common_log_db_error($orig, 'UPDATE', __FILE__);
180             $this->clientError(_('Unable to revoke access for app: ' . $app->id));
181             return false;
182         }
183
184         $msg = 'User %s (id: %d) revoked access to app %s (id: %d)';
185         common_log(LOG_INFO, sprintf($msg, $cur->nickname,
186                                      $cur->id, $app->name, $app->id));
187
188     }
189
190     function showEmptyListMessage()
191     {
192         $message = sprintf(_('You have not authorized any applications to use your account.'));
193
194         $this->elementStart('div', 'guide');
195         $this->raw(common_markup_to_html($message));
196         $this->elementEnd('div');
197     }
198
199     function showSections()
200     {
201        $cur = common_current_user();
202
203        $this->element('h2', null, 'Developers');
204        $this->elementStart('p');
205        $this->raw(_('Developers can edit the registration settings for their applications '));
206        $this->element('a',
207            array('href' => common_local_url('oauthappssettings')),
208                'here.');
209        $this->elementEnd('p');
210     }
211
212 }