2009-11-17 00:58:49 +00:00
|
|
|
<?php
|
2020-06-08 10:25:01 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2009-11-17 00:58:49 +00:00
|
|
|
/**
|
|
|
|
* Show an OAuth application
|
|
|
|
*
|
|
|
|
* @category Application
|
2020-06-08 10:25:01 +01:00
|
|
|
* @package GNUsocial
|
2009-11-17 00:58:49 +00:00
|
|
|
* @author Zach Copley <zach@status.net>
|
2011-06-09 21:20:19 +01:00
|
|
|
* @copyright 2008-2011 StatusNet, Inc.
|
2020-06-08 10:25:01 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2009-11-17 00:58:49 +00:00
|
|
|
*/
|
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
defined('GNUSOCIAL') || die();
|
2009-11-17 00:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an OAuth application
|
|
|
|
*
|
2020-06-08 10:25:01 +01:00
|
|
|
* @category Application
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2009-11-17 00:58:49 +00:00
|
|
|
*/
|
2011-06-09 21:20:19 +01:00
|
|
|
class ShowApplicationAction extends Action
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Application to show
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public $application = null;
|
2009-11-17 00:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User who owns the app
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public $owner = null;
|
2009-11-17 00:58:49 +00:00
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
public $msg = null;
|
2009-11-17 00:58:49 +00:00
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
public $success = null;
|
2009-11-17 00:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load attributes based on database arguments
|
|
|
|
*
|
|
|
|
* Loads all the DB stuff
|
|
|
|
*
|
|
|
|
* @param array $args $_REQUEST array
|
|
|
|
*
|
|
|
|
* @return success flag
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public function prepare(array $args = [])
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
|
|
|
|
$id = (int)$this->arg('id');
|
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$this->application = Oauth_application::getKV($id);
|
|
|
|
$this->owner = User::getKV($this->application->owner);
|
2009-11-17 00:58:49 +00:00
|
|
|
|
|
|
|
if (!common_logged_in()) {
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Client error displayed trying to display an OAuth application while not logged in.
|
2009-11-17 00:58:49 +00:00
|
|
|
$this->clientError(_('You must be logged in to view an application.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->application)) {
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Client error displayed trying to display a non-existing OAuth application.
|
2009-11-17 00:58:49 +00:00
|
|
|
$this->clientError(_('No such application.'), 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cur = common_current_user();
|
|
|
|
|
|
|
|
if ($cur->id != $this->owner->id) {
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Client error displayed trying to display an OAuth application for which the logged in user is not the owner.
|
2009-11-17 00:58:49 +00:00
|
|
|
$this->clientError(_('You are not the owner of this application.'), 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the request
|
|
|
|
*
|
|
|
|
* Shows info about the app
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public function handle()
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
2016-06-01 03:05:11 +01:00
|
|
|
parent::handle();
|
2009-11-17 00:58:49 +00:00
|
|
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
|
|
|
|
|
|
// CSRF protection
|
|
|
|
$token = $this->trimmed('token');
|
|
|
|
if (!$token || $token != common_session_token()) {
|
2011-04-03 22:47:46 +01:00
|
|
|
// TRANS: Client error displayed when the session token does not match or is not given.
|
2009-11-17 00:58:49 +00:00
|
|
|
$this->clientError(_('There was a problem with your session token.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->arg('reset')) {
|
|
|
|
$this->resetKey();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->showPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Title of the page
|
|
|
|
*
|
|
|
|
* @return string title of the page
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public function title()
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
|
|
|
if (!empty($this->application->name)) {
|
|
|
|
return 'Application: ' . $this->application->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
public function showPageNotice()
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
|
|
|
if (!empty($this->msg)) {
|
|
|
|
$this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
public function showContent()
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
|
|
|
$cur = common_current_user();
|
|
|
|
|
|
|
|
$consumer = $this->application->getConsumer();
|
|
|
|
|
2014-06-21 20:01:17 +01:00
|
|
|
$this->elementStart('div', 'entity_profile h-card');
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Header on the OAuth application page.
|
2010-01-11 23:51:12 +00:00
|
|
|
$this->element('h2', null, _('Application profile'));
|
|
|
|
if (!empty($this->application->icon)) {
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->element(
|
|
|
|
'img',
|
|
|
|
[
|
|
|
|
'src' => $this->application->icon,
|
|
|
|
'class' => 'u-photo logo entity_depiction',
|
|
|
|
]
|
|
|
|
);
|
2010-01-11 23:51:12 +00:00
|
|
|
}
|
2010-01-07 09:55:57 +00:00
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->element(
|
|
|
|
'a',
|
|
|
|
[
|
|
|
|
'href' => $this->application->source_url,
|
|
|
|
'class' => 'u-url p-name entity_fn',
|
|
|
|
],
|
|
|
|
$this->application->name
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->element(
|
|
|
|
'a',
|
|
|
|
[
|
|
|
|
'href' => $this->application->homepage,
|
|
|
|
'class' => 'u-url entity_org',
|
|
|
|
],
|
|
|
|
$this->application->organization
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->element(
|
|
|
|
'div',
|
|
|
|
'note entity_note',
|
|
|
|
$this->application->description
|
|
|
|
);
|
2009-11-17 00:58:49 +00:00
|
|
|
|
2011-01-14 20:36:06 +00:00
|
|
|
$this->elementStart('div', 'entity_statistics');
|
2009-11-17 00:58:49 +00:00
|
|
|
$defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
|
|
|
|
? 'read-write' : 'read-only';
|
2013-08-18 12:04:58 +01:00
|
|
|
$profile = Profile::getKV($this->application->owner);
|
2010-01-13 17:52:25 +00:00
|
|
|
|
|
|
|
$appUsers = new Oauth_application_user();
|
|
|
|
$appUsers->application_id = $this->application->id;
|
|
|
|
$userCnt = $appUsers->count();
|
2009-11-17 00:58:49 +00:00
|
|
|
|
|
|
|
$this->raw(sprintf(
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Information output on an OAuth application page.
|
|
|
|
// TRANS: %1$s is the application creator, %2$s is "read-only" or "read-write",
|
|
|
|
// TRANS: %3$d is the number of users using the OAuth application.
|
|
|
|
_m('Created by %1$s - %2$s access by default - %3$d user',
|
|
|
|
'Created by %1$s - %2$s access by default - %3$d users',
|
|
|
|
$userCnt),
|
2020-06-08 10:25:01 +01:00
|
|
|
$profile->getBestName(),
|
|
|
|
$defaultAccess,
|
|
|
|
$userCnt
|
|
|
|
));
|
2011-01-14 20:36:06 +00:00
|
|
|
$this->elementEnd('div');
|
|
|
|
|
2010-01-11 23:51:12 +00:00
|
|
|
$this->elementEnd('div');
|
2009-11-17 00:58:49 +00:00
|
|
|
|
2010-01-12 01:02:25 +00:00
|
|
|
$this->elementStart('div', 'entity_actions');
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Header on the OAuth application page.
|
2010-01-12 01:02:25 +00:00
|
|
|
$this->element('h2', null, _('Application actions'));
|
|
|
|
$this->elementStart('ul');
|
|
|
|
$this->elementStart('li', 'entity_edit');
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->element(
|
|
|
|
'a',
|
|
|
|
[
|
|
|
|
'href' => common_local_url(
|
|
|
|
'editapplication',
|
|
|
|
['id' => $this->application->id]
|
|
|
|
)
|
|
|
|
],
|
|
|
|
// TRANS: Link text to edit application on the OAuth application page.
|
|
|
|
_m('EDITAPP', 'Edit')
|
|
|
|
);
|
2010-01-12 01:02:25 +00:00
|
|
|
$this->elementEnd('li');
|
|
|
|
|
|
|
|
$this->elementStart('li', 'entity_reset_keysecret');
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->elementStart(
|
|
|
|
'form',
|
|
|
|
[
|
|
|
|
'id' => 'form_reset_key',
|
|
|
|
'class' => 'form_reset_key',
|
|
|
|
'method' => 'POST',
|
|
|
|
'action' => common_local_url(
|
|
|
|
'showapplication',
|
|
|
|
['id' => $this->application->id]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
2010-01-12 01:02:25 +00:00
|
|
|
$this->elementStart('fieldset');
|
|
|
|
$this->hidden('token', common_session_token());
|
2010-02-03 01:43:59 +00:00
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->element(
|
|
|
|
'input',
|
|
|
|
[
|
|
|
|
'type' => 'submit',
|
|
|
|
'id' => 'reset',
|
|
|
|
'name' => 'reset',
|
|
|
|
'class' => 'submit',
|
|
|
|
// TRANS: Button text on the OAuth application page.
|
|
|
|
// TRANS: Resets the OAuth consumer key and secret.
|
|
|
|
'value' => _('Reset key & secret'),
|
|
|
|
'onClick' => 'return confirmReset()',
|
|
|
|
]
|
|
|
|
);
|
2010-01-12 01:02:25 +00:00
|
|
|
$this->elementEnd('fieldset');
|
|
|
|
$this->elementEnd('form');
|
|
|
|
$this->elementEnd('li');
|
2010-02-02 06:26:03 +00:00
|
|
|
|
|
|
|
$this->elementStart('li', 'entity_delete');
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->elementStart(
|
|
|
|
'form',
|
|
|
|
[
|
|
|
|
'id' => 'form_delete_application',
|
|
|
|
'class' => 'form_delete_application',
|
|
|
|
'method' => 'POST',
|
|
|
|
'action' => common_local_url(
|
|
|
|
'deleteapplication',
|
|
|
|
['id' => $this->application->id]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
2010-02-02 06:26:03 +00:00
|
|
|
|
|
|
|
$this->elementStart('fieldset');
|
|
|
|
$this->hidden('token', common_session_token());
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Submit button text the OAuth application page to delete an application.
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->submit('delete', _m('BUTTON', 'Delete'));
|
2010-02-02 06:26:03 +00:00
|
|
|
$this->elementEnd('fieldset');
|
|
|
|
$this->elementEnd('form');
|
|
|
|
$this->elementEnd('li');
|
|
|
|
|
2010-01-12 01:02:25 +00:00
|
|
|
$this->elementEnd('ul');
|
|
|
|
$this->elementEnd('div');
|
|
|
|
|
|
|
|
$this->elementStart('div', 'entity_data');
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Header on the OAuth application page.
|
2010-01-11 23:51:12 +00:00
|
|
|
$this->element('h2', null, _('Application info'));
|
2011-01-14 20:36:06 +00:00
|
|
|
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->elementStart('dl');
|
2011-08-20 19:33:16 +01:00
|
|
|
// TRANS: Field label on application page.
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->element('dt', null, _('Consumer key'));
|
|
|
|
$this->element('dd', null, $consumer->consumer_key);
|
2011-08-20 19:33:16 +01:00
|
|
|
// TRANS: Field label on application page.
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->element('dt', null, _('Consumer secret'));
|
|
|
|
$this->element('dd', null, $consumer->consumer_secret);
|
2011-08-20 19:33:16 +01:00
|
|
|
// TRANS: Field label on application page.
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->element('dt', null, _('Request token URL'));
|
2013-10-06 11:40:58 +01:00
|
|
|
$this->element('dd', null, common_local_url('ApiOAuthRequestToken'));
|
2011-08-20 19:33:16 +01:00
|
|
|
// TRANS: Field label on application page.
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->element('dt', null, _('Access token URL'));
|
2013-10-06 11:40:58 +01:00
|
|
|
$this->element('dd', null, common_local_url('ApiOAuthAccessToken'));
|
2011-08-20 19:33:16 +01:00
|
|
|
// TRANS: Field label on application page.
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->element('dt', null, _('Authorize URL'));
|
2013-10-06 11:40:58 +01:00
|
|
|
$this->element('dd', null, common_local_url('ApiOAuthAuthorize'));
|
2011-08-19 20:43:56 +01:00
|
|
|
$this->elementEnd('dl');
|
2009-11-17 00:58:49 +00:00
|
|
|
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->element(
|
|
|
|
'p',
|
|
|
|
'note',
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Note on the OAuth application page about signature support.
|
2020-06-08 10:25:01 +01:00
|
|
|
_('Note: HMAC-SHA1 signatures are supported. The plaintext signature method is not supported.')
|
|
|
|
);
|
2009-11-17 00:58:49 +00:00
|
|
|
$this->elementEnd('div');
|
|
|
|
|
2010-01-11 22:54:46 +00:00
|
|
|
$this->elementStart('p', array('id' => 'application_action'));
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->element(
|
|
|
|
'a',
|
|
|
|
[
|
|
|
|
'href' => common_local_url('oauthappssettings'),
|
|
|
|
'class' => 'more',
|
|
|
|
],
|
|
|
|
'View your applications'
|
|
|
|
);
|
2010-01-11 22:54:46 +00:00
|
|
|
$this->elementEnd('p');
|
2009-11-17 00:58:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 01:43:59 +00:00
|
|
|
/**
|
|
|
|
* Add a confirm script for Consumer key/secret reset
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public function showScripts()
|
2010-02-03 01:43:59 +00:00
|
|
|
{
|
|
|
|
parent::showScripts();
|
|
|
|
|
2011-03-24 10:47:57 +00:00
|
|
|
// TRANS: Text in confirmation dialog to reset consumer key and secret for an OAuth application.
|
2010-02-03 01:43:59 +00:00
|
|
|
$msg = _('Are you sure you want to reset your consumer key and secret?');
|
|
|
|
|
|
|
|
$js = 'function confirmReset() { ';
|
|
|
|
$js .= ' var agree = confirm("' . $msg . '"); ';
|
|
|
|
$js .= ' return agree;';
|
|
|
|
$js .= '}';
|
|
|
|
|
|
|
|
$this->inlineScript($js);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset an application's Consumer key and secret
|
|
|
|
*
|
|
|
|
* XXX: Should this be moved to its own page with a confirm?
|
|
|
|
*
|
|
|
|
*/
|
2020-06-08 10:25:01 +01:00
|
|
|
public function resetKey()
|
2009-11-17 00:58:49 +00:00
|
|
|
{
|
2020-06-08 10:25:01 +01:00
|
|
|
$this->application->query('START TRANSACTION');
|
2009-11-17 00:58:49 +00:00
|
|
|
|
2010-02-03 01:43:59 +00:00
|
|
|
$oauser = new Oauth_application_user();
|
|
|
|
$oauser->application_id = $this->application->id;
|
|
|
|
$result = $oauser->delete();
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
common_log_db_error($oauser, 'DELETE', __FILE__);
|
|
|
|
$this->success = false;
|
|
|
|
$this->msg = ('Unable to reset consumer key and secret.');
|
|
|
|
$this->showPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-17 00:58:49 +00:00
|
|
|
$consumer = $this->application->getConsumer();
|
|
|
|
$result = $consumer->delete();
|
|
|
|
|
2010-02-03 01:43:59 +00:00
|
|
|
if ($result === false) {
|
2009-11-17 00:58:49 +00:00
|
|
|
common_log_db_error($consumer, 'DELETE', __FILE__);
|
|
|
|
$this->success = false;
|
|
|
|
$this->msg = ('Unable to reset consumer key and secret.');
|
|
|
|
$this->showPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$consumer = Consumer::generateNew();
|
|
|
|
|
|
|
|
$result = $consumer->insert();
|
|
|
|
|
2010-02-03 01:43:59 +00:00
|
|
|
if (empty($result)) {
|
2009-11-17 00:58:49 +00:00
|
|
|
common_log_db_error($consumer, 'INSERT', __FILE__);
|
|
|
|
$this->application->query('ROLLBACK');
|
|
|
|
$this->success = false;
|
|
|
|
$this->msg = ('Unable to reset consumer key and secret.');
|
|
|
|
$this->showPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$orig = clone($this->application);
|
|
|
|
$this->application->consumer_key = $consumer->consumer_key;
|
|
|
|
$result = $this->application->update($orig);
|
|
|
|
|
2010-02-03 01:43:59 +00:00
|
|
|
if ($result === false) {
|
2009-11-17 00:58:49 +00:00
|
|
|
common_log_db_error($application, 'UPDATE', __FILE__);
|
|
|
|
$this->application->query('ROLLBACK');
|
|
|
|
$this->success = false;
|
|
|
|
$this->msg = ('Unable to reset consumer key and secret.');
|
|
|
|
$this->showPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->application->query('COMMIT');
|
|
|
|
|
|
|
|
$this->success = true;
|
|
|
|
$this->msg = ('Consumer key and secret reset.');
|
|
|
|
$this->showPage();
|
|
|
|
}
|
|
|
|
}
|