<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised:	12/9/2010	ELL
// Added password feedback in message.
// Revised: 2/7/2012	ELL
// Added table prefix handling.
// Revised: 7/2/2012	ELL
// Adapted for website user page restrictions as well.
//
	include ("check_session.php");

	$user_name = $_POST['selectUserName'];
	$password = $_POST['password'];
	$table = $_POST['table'];
	
	if ($table == "-not selected-")
	{
		echo "You must select a table first to change the password of a user.  Click <a href=\"manage_page_access.php\" title=\"here\">here</a> to select a table and try again.";
		exit;
	}
	
	//Encrypt password using user name as key.
	$encrypt_password = doEncrypt($password, $user_name);

	// Change password.
	if ((!$table) || ($table == ""))
		$query = "UPDATE `" . BWB_TABLE_PREFIX . "users` SET `password`='$encrypt_password' WHERE `UserID`='$user_name'";
	else
		$query = "UPDATE `" . $table . "` SET `password`='$encrypt_password' WHERE `UserID`='$user_name'";
		
	$result = mysql_query($query, $bwb_link);
	
	$_SESSION['last_user'] = $user_name;
	
	if ((!$table) || ($table == ""))
		echo "The password for <b>$user_name</b> has been changed to <b>$password</b>.  Click <a href=\"users.php\" title=\"here\">here</a> to return to the users management page.";
	else
		echo "The password for <b>$user_name</b> has been changed to <b>$password</b>.  Click <a href=\"manage_page_access.php\" title=\"here\">here</a> to return to the users management page.";
	
	exit;
/////////////////////////////////
function doEncrypt($Data, $Key)
{
// encrypting  a[] with key k[] into e[]
// a[] = array to be encrypted
// k[] = key of truly random gibberish
// e[] = encrypted data

	for( $i = 0; $i < strlen($Data); $i++)
	{
		$c = substr($Data, $i, 1);
		$a[$i] = ord($c);
	}
	
	for( $i = 0; $i < strlen($Data); $i++)
	{
		if ($i < strlen($Key))
		{
			$c = substr($Key, $i, 1);
			$k[$i] = ord($c);
		}
		else
			$k[$i] = 65 + $i;
	}
	
	for ( $i=0; $i < strlen($Data); $i++ )
   	{
   		$e[$i] = $k[$i] ^ $a[$i];
   	}
	
	$encrypted = "";
	for ( $i = 0; $i < strlen($Data); $i++ )
	{
		$encrypted = $encrypted. decToHex($e[$i]);
	}
	return($encrypted);
	
}

function decToHex($dec)
{
	$hexStr = "0123456789ABCDEF";
	$low = $dec % 16;
	$high = ($dec - $low)/16;
	$hex = "";
	$hex = substr($hexStr, $high, 1) . substr($hexStr, $low, 1);
	return $hex;
}
?>
