]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Fix casing for HMAC-SHA1.
[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             return false;
96         }
97
98         return true;
99     }
100
101     /**
102      * Handle the request
103      *
104      * Shows info about the app
105      *
106      * @return void
107      */
108
109     function handle($args)
110     {
111         parent::handle($args);
112
113         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
114
115             // CSRF protection
116             $token = $this->trimmed('token');
117             if (!$token || $token != common_session_token()) {
118                 $this->clientError(_('There was a problem with your session token.'));
119                 return;
120             }
121
122             if ($this->arg('reset')) {
123                 $this->resetKey();
124             }
125         } else {
126             $this->showPage();
127         }
128     }
129
130     /**
131      * Title of the page
132      *
133      * @return string title of the page
134      */
135
136     function title()
137     {
138         if (!empty($this->application->name)) {
139             return 'Application: ' . $this->application->name;
140         }
141     }
142
143     function showPageNotice()
144     {
145         if (!empty($this->msg)) {
146             $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
147         }
148     }
149
150     function showContent()
151     {
152
153         $cur = common_current_user();
154
155         $consumer = $this->application->getConsumer();
156
157         $this->elementStart('div', 'entity_profile vcard');
158         $this->element('h2', null, _('Application profile'));
159         $this->elementStart('dl', 'entity_depiction');
160         $this->element('dt', null, _('Icon'));
161         $this->elementStart('dd');
162         if (!empty($this->application->icon)) {
163             $this->element('img', array('src' => $this->application->icon,
164                                         'class' => 'photo logo'));
165         }
166         $this->elementEnd('dd');
167         $this->elementEnd('dl');
168
169         $this->elementStart('dl', 'entity_fn');
170         $this->element('dt', null, _('Name'));
171         $this->elementStart('dd');
172         $this->element('a', array('href' =>  $this->application->source_url,
173                                   'class' => 'url fn'),
174                             $this->application->name);
175         $this->elementEnd('dd');
176         $this->elementEnd('dl');
177
178         $this->elementStart('dl', 'entity_org');
179         $this->element('dt', null, _('Organization'));
180         $this->elementStart('dd');
181         $this->element('a', array('href' =>  $this->application->homepage,
182                                   'class' => 'url'),
183                             $this->application->organization);
184         $this->elementEnd('dd');
185         $this->elementEnd('dl');
186
187         $this->elementStart('dl', 'entity_note');
188         $this->element('dt', null, _('Description'));
189         $this->element('dd', 'note', $this->application->description);
190         $this->elementEnd('dl');
191
192         $this->elementStart('dl', 'entity_statistics');
193         $this->element('dt', null, _('Statistics'));
194         $this->elementStart('dd');
195         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
196             ? 'read-write' : 'read-only';
197         $profile = Profile::staticGet($this->application->owner);
198
199         $appUsers = new Oauth_application_user();
200         $appUsers->application_id = $this->application->id;
201         $userCnt = $appUsers->count();
202
203         $this->raw(sprintf(
204             _('created by %1$s - %2$s access by default - %3$d users'),
205               $profile->getBestName(),
206               $defaultAccess,
207               $userCnt
208             ));
209         $this->elementEnd('dd');
210         $this->elementEnd('dl');
211         $this->elementEnd('div');
212
213         $this->elementStart('div', 'entity_actions');
214         $this->element('h2', null, _('Application actions'));
215         $this->elementStart('ul');
216         $this->elementStart('li', 'entity_edit');
217         $this->element('a',
218                        array('href' => common_local_url('editapplication',
219                                                         array('id' => $this->application->id))),
220                        'Edit');
221         $this->elementEnd('li');
222
223         $this->elementStart('li', 'entity_reset_keysecret');
224         $this->elementStart('form', array(
225             'id' => 'forma_reset_key',
226             'class' => 'form_reset_key',
227             'method' => 'POST',
228             'action' => common_local_url('showapplication',
229                                          array('id' => $this->application->id))));
230
231         $this->elementStart('fieldset');
232         $this->hidden('token', common_session_token());
233         $this->submit('reset', _('Reset key & secret'));
234         $this->elementEnd('fieldset');
235         $this->elementEnd('form');
236         $this->elementEnd('li');
237         $this->elementEnd('ul');
238         $this->elementEnd('div');
239
240         $this->elementStart('div', 'entity_data');
241         $this->element('h2', null, _('Application info'));
242         $this->elementStart('dl', 'entity_consumer_key');
243         $this->element('dt', null, _('Consumer key'));
244         $this->element('dd', null, $consumer->consumer_key);
245         $this->elementEnd('dl');
246
247         $this->elementStart('dl', 'entity_consumer_secret');
248         $this->element('dt', null, _('Consumer secret'));
249         $this->element('dd', null, $consumer->consumer_secret);
250         $this->elementEnd('dl');
251
252         $this->elementStart('dl', 'entity_request_token_url');
253         $this->element('dt', null, _('Request token URL'));
254         $this->element('dd', null, common_local_url('apioauthrequesttoken'));
255         $this->elementEnd('dl');
256
257         $this->elementStart('dl', 'entity_access_token_url');
258         $this->element('dt', null, _('Access token URL'));
259         $this->element('dd', null, common_local_url('apioauthaccesstoken'));
260         $this->elementEnd('dl');
261
262         $this->elementStart('dl', 'entity_authorize_url');
263         $this->element('dt', null, _('Authorize URL'));
264         $this->element('dd', null, common_local_url('apioauthauthorize'));
265         $this->elementEnd('dl');
266
267         $this->element('p', 'note',
268             _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
269         $this->elementEnd('div');
270
271         $this->elementStart('p', array('id' => 'application_action'));
272         $this->element('a',
273             array('href' => common_local_url('oauthappssettings'),
274                   'class' => 'more'),
275                   'View your applications');
276         $this->elementEnd('p');
277     }
278
279     function resetKey()
280     {
281         $this->application->query('BEGIN');
282
283         $consumer = $this->application->getConsumer();
284         $result = $consumer->delete();
285
286         if (!$result) {
287             common_log_db_error($consumer, 'DELETE', __FILE__);
288             $this->success = false;
289             $this->msg = ('Unable to reset consumer key and secret.');
290             $this->showPage();
291             return;
292         }
293
294         $consumer = Consumer::generateNew();
295
296         $result = $consumer->insert();
297
298         if (!$result) {
299             common_log_db_error($consumer, 'INSERT', __FILE__);
300             $this->application->query('ROLLBACK');
301             $this->success = false;
302             $this->msg = ('Unable to reset consumer key and secret.');
303             $this->showPage();
304             return;
305         }
306
307         $orig = clone($this->application);
308         $this->application->consumer_key = $consumer->consumer_key;
309         $result = $this->application->update($orig);
310
311         if (!$result) {
312             common_log_db_error($application, 'UPDATE', __FILE__);
313             $this->application->query('ROLLBACK');
314             $this->success = false;
315             $this->msg = ('Unable to reset consumer key and secret.');
316             $this->showPage();
317             return;
318         }
319
320         $this->application->query('COMMIT');
321
322         $this->success = true;
323         $this->msg = ('Consumer key and secret reset.');
324         $this->showPage();
325     }
326
327 }