]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
remove extraneous <dl> and <dt> tags
[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         $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         if (!empty($this->application->icon)) {
159             $this->element('img', array('src' => $this->application->icon,
160                                         'class' => 'photo logo entity_depiction'));
161         }
162
163         $this->element('a', array('href' =>  $this->application->source_url,
164                                   'class' => 'url fn entity_fn'),
165                             $this->application->name);
166
167         $this->element('a', array('href' =>  $this->application->homepage,
168                                   'class' => 'url entity_org'),
169                             $this->application->organization);
170
171         $this->element('div',
172                        'note entity_note',
173                        $this->application->description);
174
175         $this->elementStart('div', 'entity_statistics');
176         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
177             ? 'read-write' : 'read-only';
178         $profile = Profile::staticGet($this->application->owner);
179
180         $appUsers = new Oauth_application_user();
181         $appUsers->application_id = $this->application->id;
182         $userCnt = $appUsers->count();
183
184         $this->raw(sprintf(
185             _('Created by %1$s - %2$s access by default - %3$d users'),
186               $profile->getBestName(),
187               $defaultAccess,
188               $userCnt
189             ));
190         $this->elementEnd('div');
191
192         $this->elementEnd('div');
193
194         $this->elementStart('div', 'entity_actions');
195         $this->element('h2', null, _('Application actions'));
196         $this->elementStart('ul');
197         $this->elementStart('li', 'entity_edit');
198         $this->element('a',
199                        array('href' => common_local_url('editapplication',
200                                                         array('id' => $this->application->id))),
201                        'Edit');
202         $this->elementEnd('li');
203
204         $this->elementStart('li', 'entity_reset_keysecret');
205         $this->elementStart('form', array(
206             'id' => 'form_reset_key',
207             'class' => 'form_reset_key',
208             'method' => 'POST',
209             'action' => common_local_url('showapplication',
210                                          array('id' => $this->application->id))));
211         $this->elementStart('fieldset');
212         $this->hidden('token', common_session_token());
213
214         $this->element('input', array('type' => 'submit',
215                                       'id' => 'reset',
216                                       'name' => 'reset',
217                                       'class' => 'submit',
218                                       'value' => _('Reset key & secret'),
219                                       'onClick' => 'return confirmReset()'));
220         $this->elementEnd('fieldset');
221         $this->elementEnd('form');
222         $this->elementEnd('li');
223
224         $this->elementStart('li', 'entity_delete');
225         $this->elementStart('form', array(
226                                           'id' => 'form_delete_application',
227                                           'class' => 'form_delete_application',
228                                           'method' => 'POST',
229                                           'action' => common_local_url('deleteapplication',
230                                                                        array('id' => $this->application->id))));
231
232         $this->elementStart('fieldset');
233         $this->hidden('token', common_session_token());
234         $this->submit('delete', _('Delete'));
235         $this->elementEnd('fieldset');
236         $this->elementEnd('form');
237         $this->elementEnd('li');
238
239         $this->elementEnd('ul');
240         $this->elementEnd('div');
241
242         $this->elementStart('div', 'entity_data');
243         $this->element('h2', null, _('Application info'));
244         $this->element('div',
245                        'entity_consumer_key',
246                        $consumer->consumer_key);
247
248         $this->element('div',
249                        'entity_consumer_secret',
250                        $consumer->consumer_secret);
251
252         $this->element('div',
253                        'entity_request_token_url',
254                        common_local_url('ApiOauthRequestToken'));
255
256         $this->element('div', 'entity_access_token_url', common_local_url('ApiOauthAccessToken'));
257
258         $this->element('div', 'entity_authorize_url', common_local_url('ApiOauthAuthorize'));
259
260         $this->element('p', 'note',
261             _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
262         $this->elementEnd('div');
263
264         $this->elementStart('p', array('id' => 'application_action'));
265         $this->element('a',
266             array('href' => common_local_url('oauthappssettings'),
267                   'class' => 'more'),
268                   'View your applications');
269         $this->elementEnd('p');
270     }
271
272     /**
273      * Add a confirm script for Consumer key/secret reset
274      *
275      * @return void
276      */
277
278     function showScripts()
279     {
280         parent::showScripts();
281
282         $msg = _('Are you sure you want to reset your consumer key and secret?');
283
284         $js  = 'function confirmReset() { ';
285         $js .= '    var agree = confirm("' . $msg . '"); ';
286         $js .= '    return agree;';
287         $js .= '}';
288
289         $this->inlineScript($js);
290     }
291
292     /**
293      * Reset an application's Consumer key and secret
294      *
295      * XXX: Should this be moved to its own page with a confirm?
296      *
297      */
298
299     function resetKey()
300     {
301         $this->application->query('BEGIN');
302
303         $oauser = new Oauth_application_user();
304         $oauser->application_id = $this->application->id;
305         $result = $oauser->delete();
306
307         if ($result === false) {
308             common_log_db_error($oauser, 'DELETE', __FILE__);
309             $this->success = false;
310             $this->msg = ('Unable to reset consumer key and secret.');
311             $this->showPage();
312             return;
313         }
314
315         $consumer = $this->application->getConsumer();
316         $result = $consumer->delete();
317
318         if ($result === false) {
319             common_log_db_error($consumer, 'DELETE', __FILE__);
320             $this->success = false;
321             $this->msg = ('Unable to reset consumer key and secret.');
322             $this->showPage();
323             return;
324         }
325
326         $consumer = Consumer::generateNew();
327
328         $result = $consumer->insert();
329
330         if (empty($result)) {
331             common_log_db_error($consumer, 'INSERT', __FILE__);
332             $this->application->query('ROLLBACK');
333             $this->success = false;
334             $this->msg = ('Unable to reset consumer key and secret.');
335             $this->showPage();
336             return;
337         }
338
339         $orig = clone($this->application);
340         $this->application->consumer_key = $consumer->consumer_key;
341         $result = $this->application->update($orig);
342
343         if ($result === false) {
344             common_log_db_error($application, 'UPDATE', __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         $this->application->query('COMMIT');
353
354         $this->success = true;
355         $this->msg = ('Consumer key and secret reset.');
356         $this->showPage();
357     }
358
359 }