View source
<?php
function feeds_oauth_feeds_plugins() {
$info = array();
$info['OAuthHTTPFetcher'] = array(
'name' => 'HTTP OAuth Fetcher',
'description' => 'Download content with OAuth authentication.',
'help' => 'Uses OAuth to authenticate requests to remote resources.',
'handler' => array(
'parent' => 'FeedsHTTPFetcher',
'class' => 'OAuthHTTPFetcher',
'file' => 'OAuthHTTPFetcher.inc',
'path' => drupal_get_path('module', 'feeds_oauth'),
),
);
$info['OAuth2HTTPSFetcher'] = array(
'name' => 'HTTPS OAuth 2.0 Fetcher',
'description' => 'Download content with OAuth 2.0 authentication.',
'help' => 'Uses OAuth 2.0 to authenticate requests to remote resources.',
'handler' => array(
'parent' => 'FeedsHTTPFetcher',
'class' => 'OAuth2HTTPSFetcher',
'file' => 'OAuth2HTTPSFetcher.inc',
'path' => drupal_get_path('module', 'feeds_oauth'),
),
);
return $info;
}
function feeds_oauth_menu() {
$items['feeds/oauth/authenticate'] = array(
'title' => 'OAuth authentication',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_oauth_authenticate',
'access callback' => TRUE,
);
$items['feeds/oauth/callback'] = array(
'title' => 'OAuth callback',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_oauth_callback',
'access callback' => TRUE,
);
$items['feeds/oauth2/authenticate'] = array(
'title' => 'OAuth 2.0 authentication',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_oauth2_authenticate',
'access callback' => TRUE,
);
$items['feeds/oauth2/callback'] = array(
'title' => 'OAuth 2.0 callback',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_oauth2_callback',
'access callback' => TRUE,
);
return $items;
}
function feeds_oauth_authenticate($id) {
$fetcher = feeds_importer($id)->fetcher;
$config = $fetcher
->getConfig();
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$_SESSION['feeds']['request_token_info'] = $oauth
->getRequestToken($config['request_token_url']);
$_SESSION['feeds']['id'] = $id;
drupal_goto(url($config['authorize_url'], array(
'absolute' => TRUE,
'query' => array(
'oauth_token' => $_SESSION['feeds']['request_token_info']['oauth_token'],
),
)));
}
function feeds_oauth_callback($site_id) {
$fetcher = feeds_importer($_SESSION['feeds']['id'])->fetcher;
$config = $fetcher
->getConfig();
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$request_token_secret = $_SESSION['feeds']['request_token_info']['oauth_token_secret'];
if (!empty($_GET['oauth_token'])) {
$oauth
->setToken($_GET['oauth_token'], $request_token_secret);
$access_token_info = $oauth
->getAccessToken($config['access_token_url']);
global $user;
db_query("DELETE FROM {feeds_oauth_access_tokens} WHERE uid = %d AND site_id = '%s'", $user->uid, db_escape_string($site_id));
db_query("INSERT INTO {feeds_oauth_access_tokens} (uid, site_id, oauth_token, oauth_token_secret) VALUES(%d, '%s', '%s', '%s')", $user->uid, db_escape_string($site_id), $access_token_info['oauth_token'], $access_token_info['oauth_token_secret']);
}
drupal_goto('import/' . $id);
}
function feeds_oauth2_authenticate($id) {
require_once drupal_get_path('module', 'feeds_oauth') . '/php-proauth-read-only/lib/oauth/OAuth2Client.php';
$fetcher = feeds_importer($id)->fetcher;
$config = $fetcher
->getConfig();
$oauth = new OAuth2CurlClient();
$oauth
->setEndpoints($config['authorize_url'], $config['access_token_url']);
$oauth
->setClientId($config['consumer_key'], $config['consumer_secret']);
$obt = $oauth
->getAccessTokenObtainer('web_server');
$obt
->setRedirectUrl(url("feeds/oauth2/callback/{$config['site_id']}/{$id}", array(
'absolute' => TRUE,
)));
$params = array();
if (!empty($config['scope'])) {
$params['scope'] = preg_replace('/\\r?\\n/', ',', $config['scope']);
}
$obt
->webFlowRedirect($params);
}
function feeds_oauth2_callback($site_id, $id) {
require_once drupal_get_path('module', 'feeds_oauth') . '/php-proauth-read-only/lib/oauth/OAuth2Client.php';
$fetcher = feeds_importer($id)->fetcher;
$config = $fetcher
->getConfig();
$oauth = new OAuth2CurlClient();
$oauth
->setEndpoints($config['authorize_url'], $config['access_token_url']);
$oauth
->setClientId($config['consumer_key'], $config['consumer_secret']);
$obt = $oauth
->getAccessTokenObtainer('web_server');
$obt
->setRedirectUrl(url("feeds/oauth2/callback/{$config['site_id']}/{$id}", array(
'absolute' => TRUE,
)));
if ($obt
->webServerDidUserAuthorize()) {
global $user;
db_query("DELETE FROM {feeds_oauth_access_tokens} WHERE uid = %d AND site_id = '%s'", $user->uid, db_escape_string($site_id));
db_query("INSERT INTO {feeds_oauth_access_tokens} (uid, site_id, oauth_token, oauth_token_secret) VALUES(%d, '%s', '%s', '%s')", $user->uid, db_escape_string($site_id), $oauth
->getAccessToken()
->getToken(), 'dummy');
}
else {
}
drupal_goto('import/' . $id);
}
function feeds_oauth_feeds_oauth_authenticator() {
return array(
'feeds_oauth_get_tokens' => 'Feeds OAuth',
);
}
function feeds_oauth_get_tokens($uid, $site_id) {
return db_fetch_array(db_query("SELECT oauth_token, oauth_token_secret FROM {feeds_oauth_access_tokens} WHERE uid = %d AND site_id = '%s'", $uid, $site_id));
}
function twitter_feeds_oauth_authenticator() {
return array(
'twitter_get_tokens' => 'Twitter',
);
}
function twitter_get_tokens($uid) {
return db_fetch_array(db_query("SELECT oauth_token, oauth_token_secret FROM {twitter_account} WHERE uid = %d", $uid));
}