You are here

public function OAuthStorePostgreSQL::updateServer in Lingotek Translation 7.6

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

Register or update a server for our site (we will be the consumer)

(This is the registry at the consumers, registering servers ;-) )

@exception OAuthException2 when fields are missing or on duplicate consumer_key

Parameters

array server:

int user_id user registering this server:

boolean user_is_admin:

Return value

consumer_key

Overrides OAuthStoreAbstract::updateServer

File

lib/oauth-php/library/store/OAuthStorePostgreSQL.php, line 757

Class

OAuthStorePostgreSQL

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 {
    $update_user_field = '';
    $update_user_value = '';
    if (empty($update_user)) {

      // Per default the user owning the key is the user registering the key
      $update_user_field = ', ocr_usa_id_ref';
      $update_user_value = ', ' . intval($user_id);
    }
    $this
      ->query('
                INSERT INTO oauth_consumer_registry (
                    ocr_consumer_key     ,
                    ocr_consumer_secret  ,
                    ocr_server_uri         ,
                    ocr_server_uri_host  ,
                    ocr_server_uri_path  ,
                    ocr_timestamp        ,
                    ocr_request_token_uri,
                    ocr_authorize_uri     ,
                    ocr_access_token_uri ,
                    ocr_signature_methods' . $update_user_field . '
                )
                VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', NOW(), \'%s\', \'%s\', \'%s\', \'%s\'' . $update_user_value . ')', $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('oauth_consumer_registry', 'ocr_id');
  }
  return $server['consumer_key'];
}