<?php
	session_start();
	if (!isset($_SESSION['view_id']))
		header("Location: index.php");

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

	include "../connect/db_connection.php";
	
	//Encrypt password using user name as key.
	$password = doEncrypt($password, $user_name);

	// Change password.
	$query = "UPDATE `users` SET `password`='$password' WHERE `UserID`='$user_name'";
	$result = mysql_query($query, $bwb_link);
	
	echo "The password for $user_name has been changed.  Click <a href=\"users.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;
}
?>
