<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 3/19/2019
//
include ("check_session.php");
include ("encrypt_decrypt.php");

$user_name = $_POST['user_name'];
$password = $_POST['password'];

////////////////////////////////////////////////////////////
// Insert salt into password.
$pos = rand(0, (strlen($password)-1) );
$pass1 = substr($password, 0, $pos);
$pass2 = substr($password, $pos);
$salted_pass = $pass1 . BWB_SALT . $pass2;
// Encrypt password using user name as key.
$enc_password = doEncrypt($salted_pass, $user_name);

// Create bat_login table if not exists.
$query = "SHOW TABLES LIKE '" . BWB_TABLE_PREFIX . "bat_login';";
$result = $bwb_dbh->query($query);
if ($result->rowCount() < 1)
{
	$sql = "CREATE TABLE `" . BWB_TABLE_PREFIX . "bat_login` (  `UserID` varchar(255) NOT NULL DEFAULT '',  `password` varchar(255) NOT NULL DEFAULT '') ENGINE=MyISAM DEFAULT CHARSET=latin1;";
	$cnt = $bwb_dbh->exec($sql);
	$sql = "ALTER TABLE `bwb_bat_login`  ADD PRIMARY KEY (`UserID`,`password`); COMMIT;";
	$cnt = $bwb_dbh->exec($sql);
}

// Add user to database.
$query = "REPLACE INTO `" . BWB_TABLE_PREFIX . "bat_login` SET `UserID`='$user_name', `password`='$enc_password'";
$cnt = $bwb_dbh->exec($query);
echo $cnt;
?>