$dbhost = trim($config["dbhost"]);
$dbuser = trim($config["dbuser"]);
$dbpassword = trim($config["dbpassword"]);
$dbname = trim($config["dbname"]);
$returnValue = array();
// Get user email address
if (empty($_POST["userEmail"])) {
    $returnValue["message"] = "Missing email address";
    echo json_encode($returnValue);
    return;
}
$email = htmlentities($_POST["userEmail"]);
$dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);
$dao->openConnection();
// Check if email address is found in our database
$userDetails = $dao->getUserDetails($email);
if (empty($userDetails)) {
    $returnValue["message"] = "Provided email address is not found  in our database";
    echo json_encode($returnValue);
    return;
}
// Generate a unique string token
$passwordReset = new PasswordReset();
$passwordToken = $passwordReset->generateUniqueToken(16);
// Store unique token in our database
$user_id = $userDetails["user_id"];
$dao->storePasswordToken($user_id, $passwordToken);
// Prepare email message with Subject, Message, From, To...
$messageDetails = array();
$messageDetails["message_subject"] = "Password reset requested";
$messageDetails["to_email"] = $userDetails["email"];
}
$userEmail = htmlentities($_REQUEST["userEmail"]);
$userPassword = htmlentities($_REQUEST["userPassword"]);
$userFirstName = htmlentities($_REQUEST["userFirstName"]);
$userLastName = htmlentities($_REQUEST["userLastName"]);
// Generate secure password
$salt = openssl_random_pseudo_bytes(16);
$secured_password = sha1($userPassword . $salt);
$dbhost = trim($config["dbhost"]);
$dbuser = trim($config["dbuser"]);
$dbpassword = trim($config["dbpassword"]);
$dbname = trim($config["dbname"]);
$dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);
$dao->openConnection();
// Check if user with provided username is available
$userDetails = $dao->getUserDetails($userEmail);
if (!empty($userDetails)) {
    $returnValue["status"] = "400";
    $returnValue["message"] = "Please choose a different email address";
    echo json_encode($returnValue);
    return;
}
// Register new user
$result = $dao->registerUser($userEmail, $userFirstName, $userLastName, $secured_password, $salt);
if ($result) {
    $userDetails = $dao->getUserDetails($userEmail);
    $returnValue["status"] = "200";
    $returnValue["message"] = "Successfully registered new user";
    $returnValue["userId"] = $userDetails["user_id"];
    $returnValue["userFirstName"] = $userDetails["first_name"];
    $returnValue["userLastName"] = $userDetails["last_name"];