<?php
// $Id: services_keyauth.install,v 1.1.2.4.2.6 2010/01/25 06:02:10 heyrocker Exp $

/**
 * @file
 *   Install, uninstall and update the module.
 */

/**
 * Implementation of hook_schema().
 */
function services_keyauth_schema() {
  $schema['services_keys'] = array(
    'description' => 'Stores all Service keys.',
    'fields' => array(
      'kid' => array(
        'description' => 'The service key ID.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''
      ),
      'title' => array(
        'description' => 'The title of the service key.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'domain' => array(
        'description' => 'The domain of the service key.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
    ),
    'primary key' => array('kid')
  );

  $schema['services_timestamp_nonce'] = array(
    'description' => 'Stores timestamp against nonce for repeat attacks.',
    'fields' => array(
      'timestamp' => array(
        'description' => 'The timestamp used with the Nonce.',
        'type'        => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'nonce' => array(
        'description' => 'The random string used on the request.',
        'type'        => 'varchar',
        'length'      => 32,
        'not null'    => TRUE,
        'default'     => ''
      ),
      'domain' => array(
        'description' => 'The domain that submitted the request.',
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => ''
      ),
    ),
    'indexes' => array(
      'timestamp' => array('timestamp'),
      'nonce'  => array('nonce'),
    ),
  );
  $schema['services_key_permissions'] = array(
    'description' => t('Stores services method\'s access rights on a per API key basis.'),
    'fields' => array(
      'kid' => array(
        'description' => t('The service key ID.'),
        'type'        => 'char',
        'length'      => 32,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'method' => array(
        'description' => t('Name of service method.'),
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
    ),
    'indexes' => array(
      'api_key'       => array('kid'),
      'method'        => array('method'),
    ),
    'unique key' => array('key_method' => array('kid', 'method')),
  );
  return $schema;
}

function _services_key_auth_permissions(&$update) {
  $schema['services_key_permissions'] = array(
    'description' => t('Stores services method\'s access rights on a per API key basis.'),
    'fields' => array(
      'kid' => array(
        'description' => t('The service key ID.'),
        'type'        => 'char',
        'length'      => 32,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'method' => array(
        'description' => t('Name of service method.'),
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
    ),
    'indexes' => array(
      'api_key'       => array('kid'),
      'method'        => array('method'),
    ),
    'unique key' => array('key_method' => array('kid', 'method')),
  );
  db_create_table($update, 'services_key_permissions', $schema['services_key_permissions']);

  return $update;
}

/**
 * Implementation of hook_install().
 */
function services_keyauth_install() {
  // Legacy tables exist so we just create the new table.
  if (!db_table_exists('services_keys')) {
    drupal_install_schema('services_keyauth');
  }
  else{
    $update = array();
    _services_key_auth_permissions($update);
  }
}

/**
 * Implementation of hook_uninstall().
 */
function services_keyauth_uninstall() {
  drupal_uninstall_schema('services_keyauth');

  variable_del('services_use_key');
  variable_del('services_use_sessid');
}

function services_keyauth_update_6001() {
  $update = array();
  _services_key_auth_permissions($update);
  return $update;
}

function services_keyauth_update_6002() {
  $update = array();
  db_drop_primary_key($update, 'services_timestamp_nonce');
  db_add_index($update, 'services_timestamp_nonce', 'nonce', array('nonce'));
  return $update;
}

function services_keyauth_update_6003() {
  $update = array();
  db_drop_index($update, 'services_timestamp_nonce', 'timestamp');
  db_change_field($update, 'services_timestamp_nonce', 'timestamp', 'timestamp', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  db_add_index($update, 'services_timestamp_nonce', 'timestamp', array('timestamp'));
  return $update;
}

/*
 * Update to ensure that new menu options are loaded.
 */
function services_keyauth_update_6004() {
  $update = array();
  return $update;
}

function services_keyauth_update_6005() {
  $update = array();

  // A table might fail to exist in certain circumstances due to an issue with the install.
  if (!db_table_exists('services_key_permissions')) {
    _services_key_auth_permissions($update);
  }
  return $update;
}