2009-11-13 03:42:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet, the distributed open-source microblogging tool
|
|
|
|
*
|
|
|
|
* List a user's OAuth connected applications
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* LICENCE: This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Settings
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
2010-10-20 04:54:53 +01:00
|
|
|
* @copyright 2008-2010 StatusNet, Inc.
|
2009-11-13 03:42:18 +00:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET') && !defined('LACONICA')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-02-02 07:35:54 +00:00
|
|
|
require_once INSTALLDIR . '/lib/apioauthstore.php';
|
2009-11-13 03:42:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show connected OAuth applications
|
|
|
|
*
|
|
|
|
* @category Settings
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*
|
|
|
|
* @see SettingsAction
|
|
|
|
*/
|
2011-01-23 17:35:35 +00:00
|
|
|
class OauthconnectionssettingsAction extends SettingsAction
|
2009-11-13 03:42:18 +00:00
|
|
|
{
|
2010-10-20 04:54:53 +01:00
|
|
|
var $page = null;
|
|
|
|
var $oauth_token = null;
|
2010-01-11 22:11:43 +00:00
|
|
|
|
|
|
|
function prepare($args)
|
|
|
|
{
|
|
|
|
parent::prepare($args);
|
2010-10-20 04:54:53 +01:00
|
|
|
$this->oauth_token = $this->arg('oauth_token');
|
2010-01-11 22:11:43 +00:00
|
|
|
$this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-13 03:42:18 +00:00
|
|
|
/**
|
|
|
|
* Title of the page
|
|
|
|
*
|
|
|
|
* @return string Title of the page
|
|
|
|
*/
|
|
|
|
function title()
|
|
|
|
{
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Title for OAuth connection settings.
|
2010-01-14 22:36:13 +00:00
|
|
|
return _('Connected applications');
|
2009-11-13 03:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instructions for use
|
|
|
|
*
|
|
|
|
* @return instructions for use
|
|
|
|
*/
|
|
|
|
function getInstructions()
|
|
|
|
{
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Instructions for OAuth connection settings.
|
2010-10-20 04:54:53 +01:00
|
|
|
return _('The following connections exist for your account.');
|
2009-11-13 03:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Content area of the page
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
|
|
|
|
function showContent()
|
|
|
|
{
|
|
|
|
$user = common_current_user();
|
|
|
|
$profile = $user->getProfile();
|
|
|
|
|
|
|
|
$offset = ($this->page - 1) * APPS_PER_PAGE;
|
|
|
|
$limit = APPS_PER_PAGE + 1;
|
|
|
|
|
2010-12-13 01:37:42 +00:00
|
|
|
$connection = $user->getConnectedApps($offset, $limit);
|
2009-11-13 03:42:18 +00:00
|
|
|
|
2010-03-03 20:10:21 +00:00
|
|
|
$cnt = 0;
|
2010-01-11 22:11:43 +00:00
|
|
|
|
2010-10-20 04:54:53 +01:00
|
|
|
if (!empty($connection)) {
|
|
|
|
$cal = new ConnectedAppsList($connection, $user, $this);
|
|
|
|
$cnt = $cal->show();
|
2010-01-13 21:11:08 +00:00
|
|
|
}
|
2010-01-11 22:11:43 +00:00
|
|
|
|
2010-01-13 21:11:08 +00:00
|
|
|
if ($cnt == 0) {
|
|
|
|
$this->showEmptyListMessage();
|
|
|
|
}
|
2009-11-13 03:42:18 +00:00
|
|
|
|
2010-10-20 04:54:53 +01:00
|
|
|
$this->pagination(
|
|
|
|
$this->page > 1,
|
|
|
|
$cnt > APPS_PER_PAGE,
|
|
|
|
$this->page,
|
|
|
|
'connectionssettings',
|
|
|
|
array('nickname' => $user->nickname)
|
|
|
|
);
|
2009-11-13 03:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle posts to this form
|
|
|
|
*
|
|
|
|
* Based on the button that was pressed, muxes out to other functions
|
|
|
|
* to do the actual task requested.
|
|
|
|
*
|
|
|
|
* All sub-functions reload the form with a message -- success or failure.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handlePost()
|
|
|
|
{
|
|
|
|
// CSRF protection
|
|
|
|
|
|
|
|
$token = $this->trimmed('token');
|
|
|
|
if (!$token || $token != common_session_token()) {
|
|
|
|
$this->showForm(_('There was a problem with your session token. '.
|
|
|
|
'Try again, please.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:11:08 +00:00
|
|
|
if ($this->arg('revoke')) {
|
2010-10-20 04:54:53 +01:00
|
|
|
$this->revokeAccess($this->oauth_token);
|
2010-01-13 21:11:08 +00:00
|
|
|
} else {
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Client error when submitting a form with unexpected information.
|
2010-01-13 21:11:08 +00:00
|
|
|
$this->clientError(_('Unexpected form submission.'), 401);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-02 07:35:54 +00:00
|
|
|
/**
|
2010-10-20 04:54:53 +01:00
|
|
|
* Revoke an access token
|
|
|
|
*
|
|
|
|
* XXX: Confirm revoke before doing it
|
2010-02-02 07:35:54 +00:00
|
|
|
*
|
|
|
|
* @param int $appId the ID of the application
|
|
|
|
*
|
|
|
|
*/
|
2010-10-20 04:54:53 +01:00
|
|
|
function revokeAccess($token)
|
2010-01-13 21:11:08 +00:00
|
|
|
{
|
|
|
|
$cur = common_current_user();
|
|
|
|
|
2010-10-20 04:54:53 +01:00
|
|
|
$appUser = Oauth_application_user::getByUserAndToken($cur, $token);
|
2010-01-13 21:11:08 +00:00
|
|
|
|
|
|
|
if (empty($appUser)) {
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Client error when trying to revoke access for an application while not being a user of it.
|
2010-01-13 21:11:08 +00:00
|
|
|
$this->clientError(_('You are not a user of that application.'), 401);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-20 04:54:53 +01:00
|
|
|
$app = Oauth_application::staticGet('id', $appUser->application_id);
|
|
|
|
|
2010-02-02 07:35:54 +00:00
|
|
|
$datastore = new ApiStatusNetOAuthDataStore();
|
|
|
|
$datastore->revoke_token($appUser->token, 1);
|
|
|
|
|
|
|
|
$result = $appUser->delete();
|
2010-01-13 21:11:08 +00:00
|
|
|
|
|
|
|
if (!$result) {
|
2010-02-02 07:35:54 +00:00
|
|
|
common_log_db_error($orig, 'DELETE', __FILE__);
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Client error when revoking access has failed for some reason.
|
|
|
|
// TRANS: %s is the application ID revoking access failed for.
|
|
|
|
$this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id));
|
2010-01-13 21:11:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-20 04:54:53 +01:00
|
|
|
$msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
|
|
|
|
common_log(
|
|
|
|
LOG_INFO,
|
|
|
|
sprintf(
|
|
|
|
$msg,
|
|
|
|
$cur->nickname,
|
|
|
|
$cur->id,
|
|
|
|
$appUser->token,
|
|
|
|
$appUser->application_id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$msg = sprintf(
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Success message after revoking access for an application.
|
|
|
|
// TRANS: %1$s is the application name, %2$s is the first part of the user token.
|
|
|
|
_('You have successfully revoked access for %1$s and the access token starting with %2$s.'),
|
2010-10-20 04:54:53 +01:00
|
|
|
$app->name,
|
|
|
|
substr($appUser->token, 0, 7)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->showForm($msg, true);
|
2009-11-13 03:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showEmptyListMessage()
|
|
|
|
{
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Empty list message when no applications have been authorised yet.
|
2010-04-09 23:58:57 +01:00
|
|
|
$message = _('You have not authorized any applications to use your account.');
|
2009-11-13 03:42:18 +00:00
|
|
|
|
|
|
|
$this->elementStart('div', 'guide');
|
|
|
|
$this->raw(common_markup_to_html($message));
|
|
|
|
$this->elementEnd('div');
|
|
|
|
}
|
|
|
|
|
2009-11-17 00:58:49 +00:00
|
|
|
function showSections()
|
|
|
|
{
|
2010-10-20 04:54:53 +01:00
|
|
|
$cur = common_current_user();
|
|
|
|
|
2010-10-20 05:24:19 +01:00
|
|
|
$this->elementStart('div', array('id' => 'developer-help', 'class' => 'section'));
|
|
|
|
|
2010-10-20 04:54:53 +01:00
|
|
|
$this->element('h2', null, 'Developers');
|
|
|
|
$this->elementStart('p');
|
|
|
|
|
|
|
|
$devMsg = sprintf(
|
2010-10-20 18:34:27 +01:00
|
|
|
// TRANS: Note for developers in the OAuth connection settings form.
|
|
|
|
// TRANS: This message contains a Markdown link. Do not separate "](".
|
|
|
|
// TRANS: %s is the URL to the OAuth settings.
|
2010-10-20 04:54:53 +01:00
|
|
|
_('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'),
|
|
|
|
common_local_url('oauthappssettings')
|
|
|
|
);
|
|
|
|
|
|
|
|
$output = common_markup_to_html($devMsg);
|
|
|
|
|
|
|
|
$this->raw($output);
|
|
|
|
$this->elementEnd('p');
|
2010-10-20 05:24:19 +01:00
|
|
|
|
|
|
|
$this->elementEnd('section');
|
2009-11-17 00:58:49 +00:00
|
|
|
}
|
2009-11-13 03:42:18 +00:00
|
|
|
}
|