View source
<?php
class OAuth2HTTPSBatch extends FeedsImportBatch {
protected $url;
protected $authenticator;
protected $consumer_key;
protected $consumer_secret;
protected $site_id;
protected $uid;
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();
}
public function getRaw() {
require_once drupal_get_path('module', 'feeds_oauth') . '/php-proauth-read-only/lib/oauth/OAuth2Client.php';
$access_token = call_user_func($this->authenticator, $this->uid, $this->site_id);
$oauth = new OAuth2CurlClient();
$oauth
->_setAccessToken(new OAuth2AccessToken($access_token['oauth_token']));
$request = $oauth
->createGetRequest($this->url, array(
'access_token' => $oauth
->getAccessToken()
->getToken(),
));
$response = $oauth
->executeRequest($request);
if ($response
->getStatusCode() == 200) {
return $response
->getBody();
}
else {
watchdog('feeds_oauth', print_r($response
->getBody(), TRUE), array());
}
}
}
class OAuth2HTTPSFetcher extends FeedsHTTPFetcher {
public function fetch(FeedsSource $source) {
$source_config = $source
->getConfigFor($this);
$source_node = node_load($source->feed_nid);
global $user;
return new OAuth2HTTPSBatch(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);
}
public function configDefaults() {
return array(
'authenticator' => 'feeds_oauth_get_tokens',
'site_id' => '',
'consumer_key' => '',
'consumer_secret' => '',
'access_token_url' => '',
'authorize_url' => '',
'scope' => '',
) + parent::configDefaults();
}
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.'),
'#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['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,
);
$form['scope'] = array(
'#type' => 'textarea',
'#title' => t('Scope'),
'#description' => t('Scope of the authorization request, one per line or comma-separated.'),
'#default_value' => $this->config['scope'],
);
return $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/oauth2/authenticate/' . $this->id),
)), 'warning', FALSE);
}
return $form;
}
}