]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Merge branch 'master' into 0.9.x
[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 class ShowApplicationAction extends OwnerDesignAction
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             $this->clientError(_('You must be logged in to view an application.'));
79             return false;
80         }
81
82         if (empty($this->application)) {
83             $this->clientError(_('No such application.'), 404);
84             return false;
85         }
86
87         $cur = common_current_user();
88
89         if ($cur->id != $this->owner->id) {
90             $this->clientError(_('You are not the owner of this application.'), 401);
91             return false;
92         }
93
94         return true;
95     }
96
97     /**
98      * Handle the request
99      *
100      * Shows info about the app
101      *
102      * @return void
103      */
104     function handle($args)
105     {
106         parent::handle($args);
107
108         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
109
110             // CSRF protection
111             $token = $this->trimmed('token');
112             if (!$token || $token != common_session_token()) {
113                 $this->clientError(_('There was a problem with your session token.'));
114                 return;
115             }
116
117             if ($this->arg('reset')) {
118                 $this->resetKey();
119             }
120         } else {
121             $this->showPage();
122         }
123     }
124
125     /**
126      * Title of the page
127      *
128      * @return string title of the page
129      */
130     function title()
131     {
132         if (!empty($this->application->name)) {
133             return 'Application: ' . $this->application->name;
134         }
135     }
136
137     function showPageNotice()
138     {
139         if (!empty($this->msg)) {
140             $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
141         }
142     }
143
144     function showContent()
145     {
146         $cur = common_current_user();
147
148         $consumer = $this->application->getConsumer();
149
150         $this->elementStart('div', 'entity_profile vcard');
151         $this->element('h2', null, _('Application profile'));
152         $this->elementStart('dl', 'entity_depiction');
153         $this->element('dt', null, _('Icon'));
154         $this->elementStart('dd');
155         if (!empty($this->application->icon)) {
156             $this->element('img', array('src' => $this->application->icon,
157                                         'class' => 'photo logo'));
158         }
159         $this->elementEnd('dd');
160         $this->elementEnd('dl');
161
162         $this->elementStart('dl', 'entity_fn');
163         $this->element('dt', null, _('Name'));
164         $this->elementStart('dd');
165         $this->element('a', array('href' =>  $this->application->source_url,
166                                   'class' => 'url fn'),
167                             $this->application->name);
168         $this->elementEnd('dd');
169         $this->elementEnd('dl');
170
171         $this->elementStart('dl', 'entity_org');
172         $this->element('dt', null, _('Organization'));
173         $this->elementStart('dd');
174         $this->element('a', array('href' =>  $this->application->homepage,
175                                   'class' => 'url'),
176                             $this->application->organization);
177         $this->elementEnd('dd');
178         $this->elementEnd('dl');
179
180         $this->elementStart('dl', 'entity_note');
181         $this->element('dt', null, _('Description'));
182         $this->element('dd', 'note', $this->application->description);
183         $this->elementEnd('dl');
184
185         $this->elementStart('dl', 'entity_statistics');
186         $this->element('dt', null, _('Statistics'));
187         $this->elementStart('dd');
188         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
189             ? 'read-write' : 'read-only';
190         $profile = Profile::staticGet($this->application->owner);
191
192         $appUsers = new Oauth_application_user();
193         $appUsers->application_id = $this->application->id;
194         $userCnt = $appUsers->count();
195
196         $this->raw(sprintf(
197             _('Created by %1$s - %2$s access by default - %3$d users'),
198               $profile->getBestName(),
199               $defaultAccess,
200               $userCnt
201             ));
202         $this->elementEnd('dd');
203         $this->elementEnd('dl');
204         $this->elementEnd('div');
205
206         $this->elementStart('div', 'entity_actions');
207         $this->element('h2', null, _('Application actions'));
208         $this->elementStart('ul');
209         $this->elementStart('li', 'entity_edit');
210         $this->element('a',
211                        array('href' => common_local_url('editapplication',
212                                                         array('id' => $this->application->id))),
213                        'Edit');
214         $this->elementEnd('li');
215
216         $this->elementStart('li', 'entity_reset_keysecret');
217         $this->elementStart('form', array(
218             'id' => 'form_reset_key',
219             'class' => 'form_reset_key',
220             'method' => 'POST',
221             'action' => common_local_url('showapplication',
222                                          array('id' => $this->application->id))));
223         $this->elementStart('fieldset');
224         $this->hidden('token', common_session_token());
225
226         $this->element('input', array('type' => 'submit',
227                                       'id' => 'reset',
228                                       'name' => 'reset',
229                                       'class' => 'submit',
230                                       'value' => _('Reset key & secret'),
231                                       'onClick' => 'return confirmReset()'));
232         $this->elementEnd('fieldset');
233         $this->elementEnd('form');
234         $this->elementEnd('li');
235
236         $this->elementStart('li', 'entity_delete');
237         $this->elementStart('form', array(
238                                           'id' => 'form_delete_application',
239                                           'class' => 'form_delete_application',
240                                           'method' => 'POST',
241                                           'action' => common_local_url('deleteapplication',
242                                                                        array('id' => $this->application->id))));
243
244         $this->elementStart('fieldset');
245         $this->hidden('token', common_session_token());
246         $this->submit('delete', _('Delete'));
247         $this->elementEnd('fieldset');
248         $this->elementEnd('form');
249         $this->elementEnd('li');
250
251         $this->elementEnd('ul');
252         $this->elementEnd('div');
253
254         $this->elementStart('div', 'entity_data');
255         $this->element('h2', null, _('Application info'));
256         $this->elementStart('dl', 'entity_consumer_key');
257         $this->element('dt', null, _('Consumer key'));
258         $this->element('dd', null, $consumer->consumer_key);
259         $this->elementEnd('dl');
260
261         $this->elementStart('dl', 'entity_consumer_secret');
262         $this->element('dt', null, _('Consumer secret'));
263         $this->element('dd', null, $consumer->consumer_secret);
264         $this->elementEnd('dl');
265
266         $this->elementStart('dl', 'entity_request_token_url');
267         $this->element('dt', null, _('Request token URL'));
268         $this->element('dd', null, common_local_url('ApiOauthRequestToken'));
269         $this->elementEnd('dl');
270
271         $this->elementStart('dl', 'entity_access_token_url');
272         $this->element('dt', null, _('Access token URL'));
273         $this->element('dd', null, common_local_url('ApiOauthAccessToken'));
274         $this->elementEnd('dl');
275
276         $this->elementStart('dl', 'entity_authorize_url');
277         $this->element('dt', null, _('Authorize URL'));
278         $this->element('dd', null, common_local_url('ApiOauthAuthorize'));
279         $this->elementEnd('dl');
280
281         $this->element('p', 'note',
282             _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
283         $this->elementEnd('div');
284
285         $this->elementStart('p', array('id' => 'application_action'));
286         $this->element('a',
287             array('href' => common_local_url('oauthappssettings'),
288                   'class' => 'more'),
289                   'View your applications');
290         $this->elementEnd('p');
291     }
292
293     /**
294      * Add a confirm script for Consumer key/secret reset
295      *
296      * @return void
297      */
298     function showScripts()
299     {
300         parent::showScripts();
301
302         $msg = _('Are you sure you want to reset your consumer key and secret?');
303
304         $js  = 'function confirmReset() { ';
305         $js .= '    var agree = confirm("' . $msg . '"); ';
306         $js .= '    return agree;';
307         $js .= '}';
308
309         $this->inlineScript($js);
310     }
311
312     /**
313      * Reset an application's Consumer key and secret
314      *
315      * XXX: Should this be moved to its own page with a confirm?
316      *
317      */
318     function resetKey()
319     {
320         $this->application->query('BEGIN');
321
322         $oauser = new Oauth_application_user();
323         $oauser->application_id = $this->application->id;
324         $result = $oauser->delete();
325
326         if ($result === false) {
327             common_log_db_error($oauser, 'DELETE', __FILE__);
328             $this->success = false;
329             $this->msg = ('Unable to reset consumer key and secret.');
330             $this->showPage();
331             return;
332         }
333
334         $consumer = $this->application->getConsumer();
335         $result = $consumer->delete();
336
337         if ($result === false) {
338             common_log_db_error($consumer, 'DELETE', __FILE__);
339             $this->success = false;
340             $this->msg = ('Unable to reset consumer key and secret.');
341             $this->showPage();
342             return;
343         }
344
345         $consumer = Consumer::generateNew();
346
347         $result = $consumer->insert();
348
349         if (empty($result)) {
350             common_log_db_error($consumer, 'INSERT', __FILE__);
351             $this->application->query('ROLLBACK');
352             $this->success = false;
353             $this->msg = ('Unable to reset consumer key and secret.');
354             $this->showPage();
355             return;
356         }
357
358         $orig = clone($this->application);
359         $this->application->consumer_key = $consumer->consumer_key;
360         $result = $this->application->update($orig);
361
362         if ($result === false) {
363             common_log_db_error($application, 'UPDATE', __FILE__);
364             $this->application->query('ROLLBACK');
365             $this->success = false;
366             $this->msg = ('Unable to reset consumer key and secret.');
367             $this->showPage();
368             return;
369         }
370
371         $this->application->query('COMMIT');
372
373         $this->success = true;
374         $this->msg = ('Consumer key and secret reset.');
375         $this->showPage();
376     }
377 }