<?php
// $Id: ldapauth.install,v 1.20 2009/10/27 14:29:16 miglius Exp $

/**
 * @file
 * ldapauth module installation and upgrade code.
 */

//////////////////////////////////////////////////////////////////////////////
// Core API hooks

/**
 * Implementation of hook_install().
 */
function ldapauth_install() {
  drupal_install_schema('ldapauth');
}

/**
 * Implementation of hook_uninstall().
 */
function ldapauth_uninstall() {
  // Remove tables.
  drupal_uninstall_schema('ldapauth');

  // Remove variables
  variable_del('ldapauth_login_process');
  variable_del('ldapauth_login_conflict');
  variable_del('ldapauth_forget_passwords');
  variable_del('ldapauth_sync_passwords');
  variable_del('ldapauth_disable_pass_change');
  variable_del('ldapauth_alter_email_field');
}

//////////////////////////////////////////////////////////////////////////////
// Schema API hooks

/**
 * Implementation of hook_schema().
 */
function ldapauth_schema() {
  $schema['ldapauth'] = array(
    'description' => 'Store LDAP server and connection/authentication information.',
    'fields' => array(
      'sid' => array(
        'description' => 'The primary identifier for an LDAP server.',
        'type' => 'serial',
        'size' => 'tiny',
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'A unique name for an LDAP server configuration.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'status' => array(
        'description' => 'Whether or not an LDAP server is active.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'server' => array(
        'description' => 'The domain name or IP address of an LDAP server.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'port' => array(
        'description' => 'The TCP/IP server port which accepts LDAP connections.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 389,
      ),
      'tls' => array(
        'description' => 'Whether or not an LDAP server can use TLS.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'encrypted' => array(
        'description' => 'MD5 encrypted LDAP password.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'basedn' => array(
        'description' => 'Base DNs for users.',
        'type' => 'text',
      ),
      'user_attr' => array(
        'description' => 'The attribute that holds the login name of the users.',
        'type' => 'varchar',
        'length' => 255,
      ),
      'mail_attr' => array(
        'description' => 'The attribute that holds the email address of the users.',
        'type' => 'varchar',
        'length' => 255,
      ),
      'binddn' => array(
        'description' => 'DN for non-anonymous search.',
        'type' => 'varchar',
        'length' => 255,
      ),
      'bindpw' => array(
        'description' => 'Password for non-anonymous search.',
        'type' => 'varchar',
        'length' => 255,
      ),
      'login_php' =>  array(
        'description' => 'PHP code to transform a login name before it is sent to LDAP for authentication.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'filter_php' =>  array(
        'description' => 'PHP code to filter users which are allowed to login based on their LDAP data.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'weight' =>  array(
        'description' => 'Weight of an LDAP server.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('sid'),
    'unique keys' => array('name' => array('name')),
  );
  return $schema;
}

//////////////////////////////////////////////////////////////////////////////
// Upgrades

function ldapauth_update_6000() {
  $ret = array();
  $result = db_query("SELECT * FROM {ldapauth}");
  while ($row = db_fetch_object($result)) {
    $servers[$row->name] = $row->sid;
  }
  if (!empty($servers)) {
    $result = db_query("SELECT uid FROM {users} WHERE uid > '1'");
    while ($row = db_fetch_object($result)) {
      $account = user_load($row->uid);
      if ($account->ldap_config && in_array($account->ldap_config, array_keys($servers))) {
        user_save($account, array('ldap_config' => $servers[$account->ldap_config]));
      }
    }
  }
  return $ret;
}

function ldapauth_update_6001() {
  $ret = array();
  db_add_field($ret, 'ldapauth', 'login_php', array(
    'type' => 'text',
    'not null' => FALSE,
  ));
  db_add_field($ret, 'ldapauth', 'filter_php', array(
    'type' => 'text',
    'not null' => FALSE,
  ));
  db_add_field($ret, 'ldapauth', 'weight', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  return $ret;
}

function ldapauth_update_6002() {
  $ret = array();
  db_change_field($ret, 'ldapauth', 'basedn', 'basedn', array(
    'type' => 'text',
  ));
  return $ret;
}

function ldapauth_update_6003() {
  $ret = array();
  db_drop_primary_key($ret, 'ldapauth');
  db_change_field($ret, 'ldapauth', 'sid', 'sid', array('type' => 'serial', 'size' => 'tiny', 'not null' => TRUE), array('primary key' => array('sid')));
  db_drop_index($ret, 'ldapauth', 'sid');
  db_add_unique_key($ret, 'ldapauth', 'name', array('name'));
  return $ret;
}

