You are here

OAuthHTTPFetcher.inc in Feeds OAuth 6

Same filename and directory in other branches
  1. 7 OAuthHTTPFetcher.inc

File

OAuthHTTPFetcher.inc
View source
<?php

/**
 * Definition of the import batch object created on the fetching stage by
 * OAuthHTTPFetcher.
 */
class OAuthHTTPBatch extends FeedsImportBatch {
  protected $url;
  protected $authenticator;
  protected $consumer_key;
  protected $consumer_secret;
  protected $site_id;
  protected $uid;

  /**
   * Constructor.
   */
  public function __construct($url, $authenticator, $consumer_key, $consumer_secret, $site_id, $uid) {
    $this->url = $url;
    $this->authenticator = $authenticator;
    $this->consumer_key = $consumer_key;
    $this->consumer_secret = $consumer_secret;
    $this->site_id = $site_id;
    $this->uid = $uid;
    parent::__construct();
  }

  /**
   * Implementation of FeedsImportBatch::getRaw();
   */
  public function getRaw() {
    $access_token = call_user_func($this->authenticator, $this->uid, $this->site_id);
    $oauth = new OAuth($this->consumer_key, $this->consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
    $oauth
      ->setToken($access_token['oauth_token'], $access_token['oauth_token_secret']);
    if ($oauth
      ->fetch($this->url)) {
      return $oauth
        ->getLastResponse();
    }
  }

}

/**
 * Support OAuth authentication.
 */
class OAuthHTTPFetcher extends FeedsHTTPFetcher {

  /**
   * Use signed URL to fetch content.
   */
  public function fetch(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $source_node = node_load($source->feed_nid);
    global $user;
    return new OAuthHTTPBatch(trim($source_config['source']), $this->config['authenticator'], $this->config['consumer_key'], $this->config['consumer_secret'], $this->config['site_id'], $source_node ? $source_node->uid : $user->uid);
  }

  /**
   * Declare defaults.
   */
  public function configDefaults() {
    return array(
      'authenticator' => 'feeds_oauth_get_tokens',
      'site_id' => '',
      'consumer_key' => '',
      'consumer_secret' => '',
      'request_token_url' => '',
      'access_token_url' => '',
      'authorize_url' => '',
    ) + parent::configDefaults();
  }

  /**
   * Add form options.
   */
  public function configForm(&$form_state) {
    $form = parent::configForm($form_state);
    $form['use_pubsubhubbub'] = array(
      '#type' => 'value',
      '#value' => FALSE,
    );
    $form['authenticator'] = array(
      '#type' => 'select',
      '#title' => t('OAuth authenticator'),
      '#default_value' => $this->config['authenticator'],
      '#options' => module_invoke_all('feeds_oauth_authenticator'),
      '#description' => t('Choose the authentication module that provides the needed OAuth tokens.'),
    );
    $form['site_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Site identifier'),
      '#default_value' => $this->config['site_id'],
      '#description' => t('Internal identifier for this connection. Callback URL on OAuth server should be suffixed with this identifier.
                           For the current configuration, callback URL will be: <code>%url</code>', array(
        '%url' => url('feeds/oauth/callback/' . $this->config['site_id'], array(
          'absolute' => TRUE,
        )),
      )),
      '#required' => TRUE,
    );
    $form['consumer_key'] = array(
      '#type' => 'textfield',
      '#title' => t('Consumer key'),
      '#default_value' => $this->config['consumer_key'],
      '#required' => TRUE,
    );
    $form['consumer_secret'] = array(
      '#type' => 'textfield',
      '#title' => t('Consumer secret'),
      '#default_value' => $this->config['consumer_secret'],
      '#required' => TRUE,
    );
    $form['request_token_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Request token URL'),
      '#default_value' => $this->config['request_token_url'],
      '#required' => TRUE,
    );
    $form['access_token_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Access token URL'),
      '#default_value' => $this->config['access_token_url'],
      '#required' => TRUE,
    );
    $form['authorize_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Authorize URL'),
      '#default_value' => $this->config['authorize_url'],
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * Expose source form.
   */
  public function sourceForm($source_config) {
    $form = parent::sourceForm($source_config);
    global $user;
    $access_tokens = call_user_func($this->config['authenticator'], $user->uid, $this->config['site_id']);
    if (empty($access_tokens) && !empty($this->config['site_id'])) {
      drupal_set_message(t('Could not find OAuth access tokens for site %site. You should probably <a href="@url">authenticate first</a> to access protected information.', array(
        '%site' => $this->config['site_id'],
        '@url' => url('feeds/oauth/authenticate/' . $this->id),
      )), 'warning', FALSE);
    }
    return $form;
  }

}

Classes

Namesort descending Description
OAuthHTTPBatch Definition of the import batch object created on the fetching stage by OAuthHTTPFetcher.
OAuthHTTPFetcher Support OAuth authentication.