You are here

public function OAuthStoreSQL::addServerToken in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.2 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::addServerToken()
  2. 7.3 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::addServerToken()
  3. 7.4 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::addServerToken()
  4. 7.5 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::addServerToken()
  5. 7.6 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::addServerToken()

* Add a request token we obtained from a server. * * @todo remove old tokens for this user and this ocr_id *

Parameters

string consumer_key key of the server in the consumer registry: * @param string token_type one of 'request' or 'access' * @param string token * @param string token_secret * @param int user_id the user owning the token * @param array options extra options, name and token_ttl * @exception OAuthException2 when server is not known * @exception OAuthException2 when we received a duplicate token

Overrides OAuthStoreAbstract::addServerToken

File

lib/oauth-php/library/store/OAuthStoreSQL.php, line 295

Class

OAuthStoreSQL

Code

public function addServerToken($consumer_key, $token_type, $token, $token_secret, $user_id, $options = array()) {
  if ($token_type != 'request' && $token_type != 'access') {
    throw new OAuthException2('Unknown token type "' . $token_type . '", must be either "request" or "access"');
  }

  // Maximum time to live for this token
  if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
    $ttl = 'DATE_ADD(NOW(), INTERVAL ' . intval($options['token_ttl']) . ' SECOND)';
  }
  else {
    if ($token_type == 'request') {
      $ttl = 'DATE_ADD(NOW(), INTERVAL ' . $this->max_request_token_ttl . ' SECOND)';
    }
    else {
      $ttl = "'9999-12-31'";
    }
  }
  if (isset($options['server_uri'])) {
    $ocr_id = $this
      ->query_one('
						SELECT ocr_id
						FROM oauth_consumer_registry
						WHERE ocr_consumer_key = \'%s\'
						AND ocr_usa_id_ref = %d
						AND ocr_server_uri = \'%s\'
						', $consumer_key, $user_id, $options['server_uri']);
  }
  else {
    $ocr_id = $this
      ->query_one('
						SELECT ocr_id
						FROM oauth_consumer_registry
						WHERE ocr_consumer_key = \'%s\'
						AND ocr_usa_id_ref = %d
						', $consumer_key, $user_id);
  }
  if (empty($ocr_id)) {
    throw new OAuthException2('No server associated with consumer_key "' . $consumer_key . '"');
  }

  // Named tokens, unique per user/consumer key
  if (isset($options['name']) && $options['name'] != '') {
    $name = $options['name'];
  }
  else {
    $name = '';
  }

  // Delete any old tokens with the same type and name for this user/server combination
  $this
    ->query('
					DELETE FROM oauth_consumer_token
					WHERE oct_ocr_id_ref = %d
					  AND oct_usa_id_ref = %d
					  AND oct_token_type = LOWER(\'%s\')
					  AND oct_name       = \'%s\'
					', $ocr_id, $user_id, $token_type, $name);

  // Insert the new token
  $this
    ->query('
					INSERT IGNORE INTO oauth_consumer_token
					SET oct_ocr_id_ref	= %d,
						oct_usa_id_ref  = %d,
						oct_name		= \'%s\',
						oct_token		= \'%s\',
						oct_token_secret= \'%s\',
						oct_token_type	= LOWER(\'%s\'),
						oct_timestamp	= NOW(),
						oct_token_ttl	= ' . $ttl . '
					', $ocr_id, $user_id, $name, $token, $token_secret, $token_type);
  if (!$this
    ->query_affected_rows()) {
    throw new OAuthException2('Received duplicate token "' . $token . '" for the same consumer_key "' . $consumer_key . '"');
  }
}