]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/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-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         $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
198         $appUsers = new Oauth_application_user();
199         $appUsers->application_id = $this->application->id;
200         $userCnt = $appUsers->count();
201
202         $this->raw(sprintf(
203             _('Created by %1$s - %2$s access by default - %3$d users'),
204               $profile->getBestName(),
205               $defaultAccess,
206               $userCnt
207             ));
208         $this->elementEnd('dd');
209         $this->elementEnd('dl');
210         $this->elementEnd('div');
211
212         $this->elementStart('div', 'entity_actions');
213         $this->element('h2', null, _('Application actions'));
214         $this->elementStart('ul');
215         $this->elementStart('li', 'entity_edit');
216         $this->element('a',
217                        array('href' => common_local_url('editapplication',
218                                                         array('id' => $this->application->id))),
219                        'Edit');
220         $this->elementEnd('li');
221
222         $this->elementStart('li', 'entity_reset_keysecret');
223         $this->elementStart('form', array(
224             'id' => 'form_reset_key',
225             'class' => 'form_reset_key',
226             'method' => 'POST',
227             'action' => common_local_url('showapplication',
228                                          array('id' => $this->application->id))));
229         $this->elementStart('fieldset');
230         $this->hidden('token', common_session_token());
231
232         $this->element('input', array('type' => 'submit',
233                                       'id' => 'reset',
234                                       'name' => 'reset',
235                                       'class' => 'submit',
236                                       'value' => _('Reset key & secret'),
237                                       'onClick' => 'return confirmReset()'));
238         $this->elementEnd('fieldset');
239         $this->elementEnd('form');
240         $this->elementEnd('li');
241
242         $this->elementStart('li', 'entity_delete');
243         $this->elementStart('form', array(
244                                           'id' => 'form_delete_application',
245                                           'class' => 'form_delete_application',
246                                           'method' => 'POST',
247                                           'action' => common_local_url('deleteapplication',
248                                                                        array('id' => $this->application->id))));
249
250         $this->elementStart('fieldset');
251         $this->hidden('token', common_session_token());
252         $this->submit('delete', _('Delete'));
253         $this->elementEnd('fieldset');
254         $this->elementEnd('form');
255         $this->elementEnd('li');
256
257         $this->elementEnd('ul');
258         $this->elementEnd('div');
259
260         $this->elementStart('div', 'entity_data');
261         $this->element('h2', null, _('Application info'));
262         $this->elementStart('dl', 'entity_consumer_key');
263         $this->element('dt', null, _('Consumer key'));
264         $this->element('dd', null, $consumer->consumer_key);
265         $this->elementEnd('dl');
266
267         $this->elementStart('dl', 'entity_consumer_secret');
268         $this->element('dt', null, _('Consumer secret'));
269         $this->element('dd', null, $consumer->consumer_secret);
270         $this->elementEnd('dl');
271
272         $this->elementStart('dl', 'entity_request_token_url');
273         $this->element('dt', null, _('Request token URL'));
274         $this->element('dd', null, common_local_url('ApiOauthRequestToken'));
275         $this->elementEnd('dl');
276
277         $this->elementStart('dl', 'entity_access_token_url');
278         $this->element('dt', null, _('Access token URL'));
279         $this->element('dd', null, common_local_url('ApiOauthAccessToken'));
280         $this->elementEnd('dl');
281
282         $this->elementStart('dl', 'entity_authorize_url');
283         $this->element('dt', null, _('Authorize URL'));
284         $this->element('dd', null, common_local_url('ApiOauthAuthorize'));
285         $this->elementEnd('dl');
286
287         $this->element('p', 'note',
288             _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
289         $this->elementEnd('div');
290
291         $this->elementStart('p', array('id' => 'application_action'));
292         $this->element('a',
293             array('href' => common_local_url('oauthappssettings'),
294                   'class' => 'more'),
295                   'View your applications');
296         $this->elementEnd('p');
297     }
298
299     /**
300      * Add a confirm script for Consumer key/secret reset
301      *
302      * @return void
303      */
304
305     function showScripts()
306     {
307         parent::showScripts();
308
309         $msg = _('Are you sure you want to reset your consumer key and secret?');
310
311         $js  = 'function confirmReset() { ';
312         $js .= '    var agree = confirm("' . $msg . '"); ';
313         $js .= '    return agree;';
314         $js .= '}';
315
316         $this->inlineScript($js);
317     }
318
319     /**
320      * Reset an application's Consumer key and secret
321      *
322      * XXX: Should this be moved to its own page with a confirm?
323      *
324      */
325
326     function resetKey()
327     {
328         $this->application->query('BEGIN');
329
330         $oauser = new Oauth_application_user();
331         $oauser->application_id = $this->application->id;
332         $result = $oauser->delete();
333
334         if ($result === false) {
335             common_log_db_error($oauser, 'DELETE', __FILE__);
336             $this->success = false;
337             $this->msg = ('Unable to reset consumer key and secret.');
338             $this->showPage();
339             return;
340         }
341
342         $consumer = $this->application->getConsumer();
343         $result = $consumer->delete();
344
345         if ($result === false) {
346             common_log_db_error($consumer, 'DELETE', __FILE__);
347             $this->success = false;
348             $this->msg = ('Unable to reset consumer key and secret.');
349             $this->showPage();
350             return;
351         }
352
353         $consumer = Consumer::generateNew();
354
355         $result = $consumer->insert();
356
357         if (empty($result)) {
358             common_log_db_error($consumer, 'INSERT', __FILE__);
359             $this->application->query('ROLLBACK');
360             $this->success = false;
361             $this->msg = ('Unable to reset consumer key and secret.');
362             $this->showPage();
363             return;
364         }
365
366         $orig = clone($this->application);
367         $this->application->consumer_key = $consumer->consumer_key;
368         $result = $this->application->update($orig);
369
370         if ($result === false) {
371             common_log_db_error($application, 'UPDATE', __FILE__);
372             $this->application->query('ROLLBACK');
373             $this->success = false;
374             $this->msg = ('Unable to reset consumer key and secret.');
375             $this->showPage();
376             return;
377         }
378
379         $this->application->query('COMMIT');
380
381         $this->success = true;
382         $this->msg = ('Consumer key and secret reset.');
383         $this->showPage();
384     }
385
386 }