View source
<?php
class DrupalOAuthToken extends OAuthToken {
public $tid = 0;
public $expires = 0;
public $type = OAUTH_COMMON_TOKEN_TYPE_REQUEST;
public $uid = 0;
public $created = 0;
public $changed = 0;
public $services = array();
public $authorized = 0;
public $in_database = FALSE;
public function __construct($key, $secret, $consumer, $params = array()) {
foreach ($params as $param_key => $value) {
if (isset($this->{$param_key})) {
$this->{$param_key} = $value;
}
}
if (empty($consumer) || is_array($consumer)) {
if (is_array($consumer)) {
$params = $consumer;
}
if (!empty($params['csid'])) {
$consumer = DrupalOAuthConsumer::loadById($params['csid'], isset($params['services']));
}
}
if (!is_object($consumer)) {
throw new OAuthException("Needs an associated consumer");
}
else {
$this->consumer = $consumer;
}
parent::__construct($key, $secret);
}
public function write() {
$update = !empty($this->tid);
$primary = $update ? array(
'tid',
) : array();
if ($this->consumer->provider_consumer) {
$this->changed = REQUEST_TIME;
$values = array(
'token_key' => $this->key,
'changed' => $this->changed,
'services' => json_encode($this->services),
'authorized' => $this->authorized,
);
if ($update) {
$values['tid'] = $this->tid;
}
else {
$this->created = REQUEST_TIME;
$values['created'] = $this->created;
}
$ready = drupal_write_record('oauth_common_provider_token', $values, $primary);
if (!$ready) {
throw new OAuthException("Couldn't save token");
}
}
$values = array(
'csid' => $this->consumer->csid,
'key_hash' => sha1($this->key),
'token_key' => $this->key,
'secret' => $this->secret,
'expires' => $this->expires,
'type' => $this->type,
'uid' => $this->uid,
);
if ($update) {
$values['tid'] = $this->tid;
}
drupal_write_record('oauth_common_token', $values, $primary);
$this->tid = $values['tid'];
$this->in_database = TRUE;
if (!$update) {
$values = array(
'tid' => $this->tid,
'token_key' => $this->key,
);
drupal_write_record('oauth_common_provider_token', $values, array(
'token_key',
));
}
}
public function delete() {
self::deleteToken($this->key, $this->consumer);
}
public static function deleteToken($key, $consumer) {
$condition = db_and()
->condition('key_hash', sha1($key))
->condition('csid', $consumer->csid);
db_delete('oauth_common_provider_token')
->condition('tid', db_select('oauth_common_token', 't')
->condition($condition)
->fields('t', array(
'tid',
)), 'IN')
->execute();
db_delete('oauth_common_token')
->condition($condition)
->execute();
}
public static function load($key, $provider_token = TRUE) {
return DrupalOAuthToken::loadByKey($key, !$provider_token, FALSE);
}
public static function loadByKey($key, $consumer = FALSE, $type = OAUTH_COMMON_TOKEN_TYPE_ACCESS) {
$query = db_select('oauth_common_token', 't');
$query
->condition('t.key_hash', sha1($key))
->fields('t');
if ($type !== FALSE) {
$query
->condition('t.type', $type);
}
if (!$consumer || is_object($consumer) && $consumer->provider_consumer) {
$query
->join('oauth_common_provider_token', 'pt', 'pt.tid = t.tid');
$query
->fields('pt', array(
'created',
'changed',
'services',
'authorized',
));
}
if ($consumer === TRUE) {
$query
->leftJoin('oauth_common_provider_token', 'pt', 'pt.tid = t.tid');
$query
->isNull('pt.tid');
}
else {
if ($consumer) {
$query
->condition('t.csid', $consumer->csid);
}
}
return self::fromResult($query
->execute(), $consumer);
}
public static function loadById($tid, $load_provider_data = TRUE) {
$query = db_select('oauth_common_token', 't');
$query
->condition('t.tid', $tid)
->fields('t');
if ($load_provider_data) {
$query
->join('oauth_common_provider_token', 'pt', 'pt.tid = t.tid');
$query
->fields('pt', array(
'created',
'changed',
'services',
'authorized',
));
}
return self::fromResult($query
->execute());
}
public static function fromResult($res, $consumer = FALSE) {
if ($data = $res
->fetchAssoc()) {
if (isset($data['services'])) {
$data['services'] = json_decode($data['services']);
}
$data['in_database'] = TRUE;
if (is_object($consumer) && $consumer->csid == $data['csid']) {
$token_consumer = $consumer;
}
else {
$token_consumer = DrupalOAuthConsumer::loadById($data['csid'], isset($data['services']));
}
return new DrupalOAuthToken($data['token_key'], $data['secret'], $token_consumer, $data);
}
return NULL;
}
}