]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Merge activity plugin into mainline
[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-2011 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 class ShowApplicationAction extends Action
44 {
45     /**
46      * Application to show
47      */
48     var $application = null;
49
50     /**
51      * User who owns the app
52      */
53     var $owner = null;
54
55     var $msg = null;
56
57     var $success = null;
58
59     /**
60      * Load attributes based on database arguments
61      *
62      * Loads all the DB stuff
63      *
64      * @param array $args $_REQUEST array
65      *
66      * @return success flag
67      */
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $id = (int)$this->arg('id');
73
74         $this->application  = Oauth_application::staticGet($id);
75         $this->owner        = User::staticGet($this->application->owner);
76
77         if (!common_logged_in()) {
78             // TRANS: Client error displayed trying to display an OAuth application while not logged in.
79             $this->clientError(_('You must be logged in to view an application.'));
80             return false;
81         }
82
83         if (empty($this->application)) {
84             // TRANS: Client error displayed trying to display a non-existing OAuth application.
85             $this->clientError(_('No such application.'), 404);
86             return false;
87         }
88
89         $cur = common_current_user();
90
91         if ($cur->id != $this->owner->id) {
92             // TRANS: Client error displayed trying to display an OAuth application for which the logged in user is not the owner.
93             $this->clientError(_('You are not the owner of this application.'), 401);
94             return false;
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     function handle($args)
108     {
109         parent::handle($args);
110
111         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
112
113             // CSRF protection
114             $token = $this->trimmed('token');
115             if (!$token || $token != common_session_token()) {
116                 // TRANS: Client error displayed when the session token does not match or is not given.
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     function title()
135     {
136         if (!empty($this->application->name)) {
137             return 'Application: ' . $this->application->name;
138         }
139     }
140
141     function showPageNotice()
142     {
143         if (!empty($this->msg)) {
144             $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
145         }
146     }
147
148     function showContent()
149     {
150         $cur = common_current_user();
151
152         $consumer = $this->application->getConsumer();
153
154         $this->elementStart('div', 'entity_profile vcard');
155         // TRANS: Header on the OAuth application page.
156         $this->element('h2', null, _('Application profile'));
157         if (!empty($this->application->icon)) {
158             $this->element('img', array('src' => $this->application->icon,
159                                         'class' => 'photo logo entity_depiction'));
160         }
161
162         $this->element('a', array('href' =>  $this->application->source_url,
163                                   'class' => 'url fn entity_fn'),
164                             $this->application->name);
165
166         $this->element('a', array('href' =>  $this->application->homepage,
167                                   'class' => 'url entity_org'),
168                             $this->application->organization);
169
170         $this->element('div',
171                        'note entity_note',
172                        $this->application->description);
173
174         $this->elementStart('div', 'entity_statistics');
175         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
176             ? 'read-write' : 'read-only';
177         $profile = Profile::staticGet($this->application->owner);
178
179         $appUsers = new Oauth_application_user();
180         $appUsers->application_id = $this->application->id;
181         $userCnt = $appUsers->count();
182
183         $this->raw(sprintf(
184             // TRANS: Information output on an OAuth application page.
185             // TRANS: %1$s is the application creator, %2$s is "read-only" or "read-write",
186             // TRANS: %3$d is the number of users using the OAuth application.
187             _m('Created by %1$s - %2$s access by default - %3$d user',
188                'Created by %1$s - %2$s access by default - %3$d users',
189                $userCnt),
190               $profile->getBestName(),
191               $defaultAccess,
192               $userCnt
193             ));
194         $this->elementEnd('div');
195
196         $this->elementEnd('div');
197
198         $this->elementStart('div', 'entity_actions');
199         // TRANS: Header on the OAuth application page.
200         $this->element('h2', null, _('Application actions'));
201         $this->elementStart('ul');
202         $this->elementStart('li', 'entity_edit');
203         $this->element('a',
204                        array('href' => common_local_url('editapplication',
205                                                         array('id' => $this->application->id))),
206                        // TRANS: Link text to edit application on the OAuth application page.
207                        _m('EDITAPP','Edit'));
208         $this->elementEnd('li');
209
210         $this->elementStart('li', 'entity_reset_keysecret');
211         $this->elementStart('form', array(
212             'id' => 'form_reset_key',
213             'class' => 'form_reset_key',
214             'method' => 'POST',
215             'action' => common_local_url('showapplication',
216                                          array('id' => $this->application->id))));
217         $this->elementStart('fieldset');
218         $this->hidden('token', common_session_token());
219
220         $this->element('input', array('type' => 'submit',
221                                       'id' => 'reset',
222                                       'name' => 'reset',
223                                       'class' => 'submit',
224                                       // TRANS: Button text on the OAuth application page.
225                                       // TRANS: Resets the OAuth consumer key and secret.
226                                       'value' => _('Reset key & secret'),
227                                       'onClick' => 'return confirmReset()'));
228         $this->elementEnd('fieldset');
229         $this->elementEnd('form');
230         $this->elementEnd('li');
231
232         $this->elementStart('li', 'entity_delete');
233         $this->elementStart('form', array(
234                                           'id' => 'form_delete_application',
235                                           'class' => 'form_delete_application',
236                                           'method' => 'POST',
237                                           'action' => common_local_url('deleteapplication',
238                                                                        array('id' => $this->application->id))));
239
240         $this->elementStart('fieldset');
241         $this->hidden('token', common_session_token());
242         // TRANS: Submit button text the OAuth application page to delete an application.
243         $this->submit('delete', _m('BUTTON','Delete'));
244         $this->elementEnd('fieldset');
245         $this->elementEnd('form');
246         $this->elementEnd('li');
247
248         $this->elementEnd('ul');
249         $this->elementEnd('div');
250
251         $this->elementStart('div', 'entity_data');
252         // TRANS: Header on the OAuth application page.
253         $this->element('h2', null, _('Application info'));
254
255         $this->elementStart('dl');
256         // TRANS: Field label on application page.
257         $this->element('dt', null, _('Consumer key'));
258         $this->element('dd', null, $consumer->consumer_key);
259         // TRANS: Field label on application page.
260         $this->element('dt', null, _('Consumer secret'));
261         $this->element('dd', null, $consumer->consumer_secret);
262         // TRANS: Field label on application page.
263         $this->element('dt', null, _('Request token URL'));
264         $this->element('dd', null, common_local_url('ApiOauthRequestToken'));
265         // TRANS: Field label on application page.
266         $this->element('dt', null, _('Access token URL'));
267         $this->element('dd', null, common_local_url('ApiOauthAccessToken'));
268         // TRANS: Field label on application page.
269         $this->element('dt', null, _('Authorize URL'));
270         $this->element('dd', null, common_local_url('ApiOauthAuthorize'));
271         $this->elementEnd('dl');
272
273         $this->element('p', 'note',
274             // TRANS: Note on the OAuth application page about signature support.
275             _('Note: HMAC-SHA1 signatures are supported. The plaintext signature method is not supported.'));
276         $this->elementEnd('div');
277
278         $this->elementStart('p', array('id' => 'application_action'));
279         $this->element('a',
280             array('href' => common_local_url('oauthappssettings'),
281                   'class' => 'more'),
282                   'View your applications');
283         $this->elementEnd('p');
284     }
285
286     /**
287      * Add a confirm script for Consumer key/secret reset
288      *
289      * @return void
290      */
291     function showScripts()
292     {
293         parent::showScripts();
294
295         // TRANS: Text in confirmation dialog to reset consumer key and secret for an OAuth application.
296         $msg = _('Are you sure you want to reset your consumer key and secret?');
297
298         $js  = 'function confirmReset() { ';
299         $js .= '    var agree = confirm("' . $msg . '"); ';
300         $js .= '    return agree;';
301         $js .= '}';
302
303         $this->inlineScript($js);
304     }
305
306     /**
307      * Reset an application's Consumer key and secret
308      *
309      * XXX: Should this be moved to its own page with a confirm?
310      *
311      */
312     function resetKey()
313     {
314         $this->application->query('BEGIN');
315
316         $oauser = new Oauth_application_user();
317         $oauser->application_id = $this->application->id;
318         $result = $oauser->delete();
319
320         if ($result === false) {
321             common_log_db_error($oauser, 'DELETE', __FILE__);
322             $this->success = false;
323             $this->msg = ('Unable to reset consumer key and secret.');
324             $this->showPage();
325             return;
326         }
327
328         $consumer = $this->application->getConsumer();
329         $result = $consumer->delete();
330
331         if ($result === false) {
332             common_log_db_error($consumer, 'DELETE', __FILE__);
333             $this->success = false;
334             $this->msg = ('Unable to reset consumer key and secret.');
335             $this->showPage();
336             return;
337         }
338
339         $consumer = Consumer::generateNew();
340
341         $result = $consumer->insert();
342
343         if (empty($result)) {
344             common_log_db_error($consumer, 'INSERT', __FILE__);
345             $this->application->query('ROLLBACK');
346             $this->success = false;
347             $this->msg = ('Unable to reset consumer key and secret.');
348             $this->showPage();
349             return;
350         }
351
352         $orig = clone($this->application);
353         $this->application->consumer_key = $consumer->consumer_key;
354         $result = $this->application->update($orig);
355
356         if ($result === false) {
357             common_log_db_error($application, 'UPDATE', __FILE__);
358             $this->application->query('ROLLBACK');
359             $this->success = false;
360             $this->msg = ('Unable to reset consumer key and secret.');
361             $this->showPage();
362             return;
363         }
364
365         $this->application->query('COMMIT');
366
367         $this->success = true;
368         $this->msg = ('Consumer key and secret reset.');
369         $this->showPage();
370     }
371 }