You are here

public function OAuthStoreSQL::updateServer in Lingotek Translation 7.6

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

* Register or update a server for our site (we will be the consumer) * * (This is the registry at the consumers, registering servers ;-) ) * *

Parameters

array server: * @param int user_id user registering this server * @param boolean user_is_admin * @exception OAuthException2 when fields are missing or on duplicate consumer_key * @return consumer_key

Overrides OAuthStoreAbstract::updateServer

File

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

Class

OAuthStoreSQL

Code

public function updateServer($server, $user_id, $user_is_admin = false) {
  foreach (array(
    'consumer_key',
    'server_uri',
  ) as $f) {
    if (empty($server[$f])) {
      throw new OAuthException2('The field "' . $f . '" must be set and non empty');
    }
  }
  if (!empty($server['id'])) {
    $exists = $this
      ->query_one('
						SELECT ocr_id
						FROM oauth_consumer_registry
						WHERE ocr_consumer_key = \'%s\'
						  AND ocr_id <> %d
						  AND (ocr_usa_id_ref = %d OR ocr_usa_id_ref IS NULL)
						', $server['consumer_key'], $server['id'], $user_id);
  }
  else {
    $exists = $this
      ->query_one('
						SELECT ocr_id
						FROM oauth_consumer_registry
						WHERE ocr_consumer_key = \'%s\'
						  AND (ocr_usa_id_ref = %d OR ocr_usa_id_ref IS NULL)
						', $server['consumer_key'], $user_id);
  }
  if ($exists) {
    throw new OAuthException2('The server with key "' . $server['consumer_key'] . '" has already been registered');
  }
  $parts = parse_url($server['server_uri']);
  $host = isset($parts['host']) ? $parts['host'] : 'localhost';
  $path = isset($parts['path']) ? $parts['path'] : '/';
  if (isset($server['signature_methods'])) {
    if (is_array($server['signature_methods'])) {
      $server['signature_methods'] = strtoupper(implode(',', $server['signature_methods']));
    }
  }
  else {
    $server['signature_methods'] = '';
  }

  // When the user is an admin, then the user can update the user_id of this record
  if ($user_is_admin && array_key_exists('user_id', $server)) {
    if (is_null($server['user_id'])) {
      $update_user = ', ocr_usa_id_ref = NULL';
    }
    else {
      $update_user = ', ocr_usa_id_ref = ' . intval($server['user_id']);
    }
  }
  else {
    $update_user = '';
  }
  if (!empty($server['id'])) {

    // Check if the current user can update this server definition
    if (!$user_is_admin) {
      $ocr_usa_id_ref = $this
        ->query_one('
									SELECT ocr_usa_id_ref
									FROM oauth_consumer_registry
									WHERE ocr_id = %d
									', $server['id']);
      if ($ocr_usa_id_ref != $user_id) {
        throw new OAuthException2('The user "' . $user_id . '" is not allowed to update this server');
      }
    }

    // Update the consumer registration
    $this
      ->query('
					UPDATE oauth_consumer_registry
					SET ocr_consumer_key    	= \'%s\',
						ocr_consumer_secret 	= \'%s\',
						ocr_server_uri	    	= \'%s\',
						ocr_server_uri_host 	= \'%s\',
						ocr_server_uri_path 	= \'%s\',
						ocr_timestamp       	= NOW(),
						ocr_request_token_uri	= \'%s\',
						ocr_authorize_uri		= \'%s\',
						ocr_access_token_uri	= \'%s\',
						ocr_signature_methods	= \'%s\'
						' . $update_user . '
					WHERE ocr_id = %d
					', $server['consumer_key'], $server['consumer_secret'], $server['server_uri'], strtolower($host), $path, isset($server['request_token_uri']) ? $server['request_token_uri'] : '', isset($server['authorize_uri']) ? $server['authorize_uri'] : '', isset($server['access_token_uri']) ? $server['access_token_uri'] : '', $server['signature_methods'], $server['id']);
  }
  else {
    if (empty($update_user)) {

      // Per default the user owning the key is the user registering the key
      $update_user = ', ocr_usa_id_ref = ' . intval($user_id);
    }
    $this
      ->query('
					INSERT INTO oauth_consumer_registry
					SET ocr_consumer_key    	= \'%s\',
						ocr_consumer_secret 	= \'%s\',
						ocr_server_uri	    	= \'%s\',
						ocr_server_uri_host 	= \'%s\',
						ocr_server_uri_path 	= \'%s\',
						ocr_timestamp       	= NOW(),
						ocr_request_token_uri	= \'%s\',
						ocr_authorize_uri		= \'%s\',
						ocr_access_token_uri	= \'%s\',
						ocr_signature_methods	= \'%s\'
						' . $update_user, $server['consumer_key'], $server['consumer_secret'], $server['server_uri'], strtolower($host), $path, isset($server['request_token_uri']) ? $server['request_token_uri'] : '', isset($server['authorize_uri']) ? $server['authorize_uri'] : '', isset($server['access_token_uri']) ? $server['access_token_uri'] : '', $server['signature_methods']);
    $ocr_id = $this
      ->query_insert_id();
  }
  return $server['consumer_key'];
}