<?php
// $Id: mysql_auth_update.module,v 1.1 2006/02/23 07:40:22 koumbit Exp $

/**
 * @file
 * Create a line in the external auth database when registering a new user.
 *
 * Also takes care of changing the password when appropriate.
 */

/**
 * Implementation of hook_help().
 */
function sql_auth_create_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Create users in related database');
  }
}

function sql_auth_create_user($op, &$edit, &$user, $category = NULL) {
  switch ($op) {
    case 'insert':
      /* recall:
       * 0 only site admins
       * 1 everyone
       * 2 approval necessary
       */
      print "insert hook";
      if (variable_get('user_register', 1) == 1) {
	$salt = '';
	if (variable_get('mysql_auth_pass_salt', 0)) {
	  $salt = ", '". user_password() . "'"; // use a random string as an initial salt
	}
	$res = _mysql_auth_query("INSERT INTO %s (%s, %s) VALUES ('%s', %s('%s'".$salt."))", array(variable_get('mysql_auth_table', 'users'), variable_get('mysql_auth_user_col', 'name'), variable_get('mysql_auth_pass_col', 'pass'), $edit['name'], _mysql_auth_current_scheme(), $edit['pass']));
      }
      break;
    case 'update':
      $salt = '';
      if (variable_get('mysql_auth_pass_salt', 0)) {
	$salt = ', '. variable_get('mysql_auth_pass_col', 'pass');
      }
      $res = _mysql_auth_query("UPDATE %s SET %s=%s('%s'%s) WHERE %s='%s'", array(variable_get('mysql_auth_table', 'users'), variable_get('mysql_auth_pass_col', 'pass'), _mysql_auth_current_scheme(), $edit['pass'], $salt, variable_get('mysql_auth_user_col', 'name'), $edit['name']));
      break;
  }
}
