]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Rework application registration workflow to be more private
[quix0rs-gnu-social.git] / actions / showapplication.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show an OAuth application
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  Application
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 /**
35  * Show an OAuth application
36  *
37  * @category Application
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class ShowApplicationAction extends OwnerDesignAction
45 {
46     /**
47      * Application to show
48      */
49
50     var $application = null;
51
52     /**
53      * User who owns the app
54      */
55
56     var $owner = null;
57
58     var $msg = null;
59
60     var $success = null;
61
62     /**
63      * Load attributes based on database arguments
64      *
65      * Loads all the DB stuff
66      *
67      * @param array $args $_REQUEST array
68      *
69      * @return success flag
70      */
71
72     function prepare($args)
73     {
74         parent::prepare($args);
75
76         $id = (int)$this->arg('id');
77
78         $this->application  = Oauth_application::staticGet($id);
79         $this->owner        = User::staticGet($this->application->owner);
80
81         if (!common_logged_in()) {
82             $this->clientError(_('You must be logged in to view an application.'));
83             return false;
84         }
85
86         if (empty($this->application)) {
87             $this->clientError(_('No such application.'), 404);
88             return false;
89         }
90
91         $cur = common_current_user();
92
93         if ($cur->id != $this->owner->id) {
94             $this->clientError(_('You are not the owner of this application.'), 401);
95         }
96
97         return true;
98     }
99
100     /**
101      * Handle the request
102      *
103      * Shows info about the app
104      *
105      * @return void
106      */
107
108     function handle($args)
109     {
110         parent::handle($args);
111
112         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
113
114             // CSRF protection
115             $token = $this->trimmed('token');
116             if (!$token || $token != common_session_token()) {
117                 $this->clientError(_('There was a problem with your session token.'));
118                 return;
119             }
120
121             if ($this->arg('reset')) {
122                 $this->resetKey();
123             }
124         } else {
125             $this->showPage();
126         }
127     }
128
129     /**
130      * Title of the page
131      *
132      * @return string title of the page
133      */
134
135     function title()
136     {
137         if (!empty($this->application->name)) {
138             return 'Application: ' . $this->application->name;
139         }
140     }
141
142     function showPageNotice()
143     {
144         if (!empty($this->msg)) {
145             $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
146         }
147     }
148
149     function showContent()
150     {
151
152         $cur = common_current_user();
153
154         $consumer = $this->application->getConsumer();
155
156         $this->elementStart('div', 'entity_profile vcard');
157         $this->element('h2', null, _('Application profile'));
158         $this->elementStart('dl', 'entity_depiction');
159         $this->element('dt', null, _('Icon'));
160         $this->elementStart('dd');
161         if (!empty($this->application->icon)) {
162             $this->element('img', array('src' => $this->application->icon,
163                                         'class' => 'photo logo'));
164         }
165         $this->elementEnd('dd');
166         $this->elementEnd('dl');
167
168         $this->elementStart('dl', 'entity_fn');
169         $this->element('dt', null, _('Name'));
170         $this->elementStart('dd');
171         $this->element('a', array('href' =>  $this->application->source_url,
172                                   'class' => 'url fn'),
173                             $this->application->name);
174         $this->elementEnd('dd');
175         $this->elementEnd('dl');
176
177         $this->elementStart('dl', 'entity_org');
178         $this->element('dt', null, _('Organization'));
179         $this->elementStart('dd');
180         $this->element('a', array('href' =>  $this->application->homepage,
181                                   'class' => 'url'),
182                             $this->application->organization);
183         $this->elementEnd('dd');
184         $this->elementEnd('dl');
185
186         $this->elementStart('dl', 'entity_note');
187         $this->element('dt', null, _('Description'));
188         $this->element('dd', 'note', $this->application->description);
189         $this->elementEnd('dl');
190
191         $this->elementStart('dl', 'entity_statistics');
192         $this->element('dt', null, _('Statistics'));
193         $this->elementStart('dd');
194         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
195             ? 'read-write' : 'read-only';
196         $profile = Profile::staticGet($this->application->owner);
197         $userCnt = 0; // XXX: count how many users use the app
198
199         $this->raw(sprintf(
200             _('Created by %1$s - %2$s access by default - %3$d users.'),
201               $profile->getBestName(),
202               $defaultAccess,
203               $userCnt
204             ));
205         $this->elementEnd('dd');
206         $this->elementEnd('dl');
207         $this->elementEnd('div');
208
209         $this->elementStart('div', 'entity_actions');
210         $this->element('h2', null, _('Application actions'));
211         $this->elementStart('ul');
212         $this->elementStart('li', 'entity_edit');
213         $this->element('a',
214                        array('href' => common_local_url('editapplication',
215                                                         array('id' => $this->application->id))),
216                        'Edit');
217         $this->elementEnd('li');
218
219         $this->elementStart('li', 'entity_reset_keysecret');
220         $this->elementStart('form', array(
221             'id' => 'forma_reset_key',
222             'class' => 'form_reset_key',
223             'method' => 'POST',
224             'action' => common_local_url('showapplication',
225                 array('id' => $this->application->id))));
226
227         $this->elementStart('fieldset');
228         $this->hidden('token', common_session_token());
229         $this->submit('reset', _('Reset key & secret'));
230         $this->elementEnd('fieldset');
231         $this->elementEnd('form');
232         $this->elementEnd('li');
233         $this->elementEnd('ul');
234         $this->elementEnd('div');
235
236         $this->elementStart('div', 'entity_data');
237         $this->element('h2', null, _('Application info'));
238         $this->elementStart('dl', 'entity_consumer_key');
239         $this->element('dt', null, _('Consumer key'));
240         $this->element('dd', null, $consumer->consumer_key);
241         $this->elementEnd('dl');
242
243         $this->elementStart('dl', 'entity_consumer_secret');
244         $this->element('dt', null, _('Consumer secret'));
245         $this->element('dd', null, $consumer->consumer_secret);
246         $this->elementEnd('dl');
247
248         $this->elementStart('dl', 'entity_request_token_url');
249         $this->element('dt', null, _('Request token URL'));
250         $this->element('dd', null, common_local_url('apioauthrequesttoken'));
251         $this->elementEnd('dl');
252
253         $this->elementStart('dl', 'entity_access_token_url');
254         $this->element('dt', null, _('Access token URL'));
255         $this->element('dd', null, common_local_url('apioauthaccesstoken'));
256         $this->elementEnd('dl');
257
258         $this->elementStart('dl', 'entity_authorize_url');
259         $this->element('dt', null, _('Authorize URL'));
260         $this->element('dd', null, common_local_url('apioauthauthorize'));
261         $this->elementEnd('dl');
262
263         $this->element('p', 'note',
264             _('Note: We support hmac-sha1 signatures. We do not support the plaintext signature method.'));
265         $this->elementEnd('div');
266
267         $this->elementStart('p', array('id' => 'application_action'));
268         $this->element('a',
269             array('href' => common_local_url('oauthappssettings'),
270                   'class' => 'more'),
271                   'View your applications');
272         $this->elementEnd('p');
273     }
274
275     function resetKey()
276     {
277         $this->application->query('BEGIN');
278
279         $consumer = $this->application->getConsumer();
280         $result = $consumer->delete();
281
282         if (!$result) {
283             common_log_db_error($consumer, 'DELETE', __FILE__);
284             $this->success = false;
285             $this->msg = ('Unable to reset consumer key and secret.');
286             $this->showPage();
287             return;
288         }
289
290         $consumer = Consumer::generateNew();
291
292         $result = $consumer->insert();
293
294         if (!$result) {
295             common_log_db_error($consumer, 'INSERT', __FILE__);
296             $this->application->query('ROLLBACK');
297             $this->success = false;
298             $this->msg = ('Unable to reset consumer key and secret.');
299             $this->showPage();
300             return;
301         }
302
303         $orig = clone($this->application);
304         $this->application->consumer_key = $consumer->consumer_key;
305         $result = $this->application->update($orig);
306
307         if (!$result) {
308             common_log_db_error($application, 'UPDATE', __FILE__);
309             $this->application->query('ROLLBACK');
310             $this->success = false;
311             $this->msg = ('Unable to reset consumer key and secret.');
312             $this->showPage();
313             return;
314         }
315
316         $this->application->query('COMMIT');
317
318         $this->success = true;
319         $this->msg = ('Consumer key and secret reset.');
320         $this->showPage();
321     }
322
323 }
324