You are here

feeds_oauth.module in Feeds OAuth 6

Same filename and directory in other branches
  1. 7 feeds_oauth.module

File

feeds_oauth.module
View source
<?php

/**
 * Implementation of hook_feed_plugins().
 */
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',
      // This is the key name, not the class name.
      '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',
      // This is the key name, not the class name.
      'class' => 'OAuth2HTTPSFetcher',
      'file' => 'OAuth2HTTPSFetcher.inc',
      'path' => drupal_get_path('module', 'feeds_oauth'),
    ),
  );
  return $info;
}

/**
 *  Implementation of hook_menu().
 */
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 {

    // TODO:  Handle failure.
  }
  drupal_goto('import/' . $id);
}

/**
 * Implementation of hook_feeds_oauth_authenticator().
 */
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));
}

/**
 * On behalf implementation of hook_feeds_oauth_authenticator() for twitter.module.
 */
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));
}

Functions

Namesort descending Description
feeds_oauth2_authenticate
feeds_oauth2_callback
feeds_oauth_authenticate
feeds_oauth_callback
feeds_oauth_feeds_oauth_authenticator Implementation of hook_feeds_oauth_authenticator().
feeds_oauth_feeds_plugins Implementation of hook_feed_plugins().
feeds_oauth_get_tokens
feeds_oauth_menu Implementation of hook_menu().
twitter_feeds_oauth_authenticator On behalf implementation of hook_feeds_oauth_authenticator() for twitter.module.
twitter_get_tokens