View source
<?php
class DrupalOAuthConsumer extends OAuthConsumer {
public $csid = 0;
public $uid = 0;
public $name = '';
public $context = '';
public $created = 0;
public $changed = 0;
public $callback_url = 'oob';
public $configuration = array();
public $provider_consumer = FALSE;
public $in_database = FALSE;
function __construct($key, $secret, $params = array()) {
if (is_string($params)) {
$callback_url = $params;
if (func_num_args() > 4) {
$params = func_get_arg(4);
}
else {
$params = array();
}
$params['callback_url'] = $callback_url;
}
foreach ($params as $param_key => $value) {
if (isset($this->{$param_key})) {
$this->{$param_key} = $value;
}
}
if (!empty($this->created)) {
$this->provider_consumer = TRUE;
}
parent::__construct($key, $secret, $this->callback_url);
}
public function write() {
$update = !empty($this->csid);
$primary = $update ? array(
'csid',
) : array();
if ($this->provider_consumer) {
$this->changed = REQUEST_TIME;
$values = array(
'consumer_key' => $this->key,
'created' => $this->created,
'changed' => $this->changed,
'uid' => $this->uid,
'name' => $this->name,
'context' => $this->context,
'callback_url' => $this->callback_url,
);
if ($update) {
$values['csid'] = $this->csid;
}
else {
$this->created = REQUEST_TIME;
$values['created'] = $this->created;
}
$ready = drupal_write_record('oauth_common_provider_consumer', $values, $primary);
if (!$ready) {
throw new OAuthException("Couldn't save consumer");
}
}
$values = array(
'key_hash' => sha1($this->key),
'consumer_key' => $this->key,
'secret' => $this->secret,
'configuration' => serialize(empty($this->configuration) ? array() : $this->configuration),
);
if ($update) {
$values['csid'] = $this->csid;
}
drupal_write_record('oauth_common_consumer', $values, $primary);
$this->csid = $values['csid'];
$this->in_database = TRUE;
if (!$update) {
$values = array(
'csid' => $this->csid,
'consumer_key' => $this->key,
);
drupal_write_record('oauth_common_provider_consumer', $values, array(
'consumer_key',
));
}
}
public function delete() {
self::deleteConsumer($this->csid);
}
public static function deleteConsumer($csid) {
$condition = db_and()
->condition('csid', $csid);
db_delete('oauth_common_provider_token')
->condition('tid', db_select('oauth_common_token', 't')
->condition($condition)
->fields('t', array(
'tid',
)), 'IN')
->execute();
foreach (array(
'oauth_common_token',
'oauth_common_provider_consumer',
'oauth_common_consumer',
) as $table) {
db_delete($table)
->condition($condition)
->execute();
}
}
public static function load($key, $provider_consumer = TRUE) {
return DrupalOAuthConsumer::loadProviderByKey($key, $provider_consumer);
}
public static function loadById($csid, $load_provider_data = TRUE) {
$query = db_select('oauth_common_consumer', 'c');
if (is_numeric($csid)) {
$query
->condition('c.csid', $csid)
->fields('c', array(
'csid',
'consumer_key',
'secret',
'configuration',
));
if ($load_provider_data) {
$query
->leftJoin('oauth_common_provider_consumer', 'pc', 'pc.csid = c.csid');
$query
->fields('pc', array(
'created',
'changed',
'uid',
'name',
'context',
'callback_url',
));
}
return self::fromResult($query
->execute());
}
else {
return FALSE;
}
}
public static function loadProviderByKey($key, $provider = TRUE) {
$query = db_select('oauth_common_consumer', 'c');
$query
->condition('c.key_hash', sha1($key))
->fields('c', array(
'secret',
'configuration',
));
if ($provider) {
$query
->join('oauth_common_provider_consumer', 'pc', 'pc.csid = c.csid');
$query
->fields('pc');
}
else {
$query
->leftJoin('oauth_common_provider_consumer', 'pc', 'pc.csid = c.csid');
$query
->fields('c', array(
'csid',
'consumer_key',
))
->fields('pc', array(
'created',
'changed',
'uid',
'name',
'context',
'callback_url',
))
->isNull('pc.csid');
}
return self::fromResult($query
->execute());
}
public static function fromResult($res) {
if ($data = $res
->fetchAssoc()) {
if (!empty($data['configuration'])) {
$data['configuration'] = unserialize($data['configuration']);
}
$data['in_database'] = TRUE;
return new DrupalOAuthConsumer($data['consumer_key'], $data['secret'], $data);
}
return NULL;
}
}