<?php
/////////////////////////////////////////////
// Users Management /////
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 4/9/2010	ELL
// Added more access levels.  Levels are now admin, power_user, user and restricted.
// Revised: 12/9/2010	ELL
// Added password generator.
//	Revised: 2/10/2012	ELL
//  Added table prefix handling.
// 
//
	include ("check_session.php");
			
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "users`";
		$result = mysql_query($query, $bwb_link);
		$num_results = mysql_num_rows($result);
		$j = 0;
		for ($i = 0; $i < $num_results; $i++)
		{
			$row = mysql_fetch_array($result);
			$tmp_user_name = $row["UserID"];
			if ($tmp_user_name != "admin")
			{
				$user_name[$j] = $row["UserID"];
				$password[$j] = $row["password"];
//				$AccessLevel[$j] = $row["AccessLevel"];
				$j++;
			}
		}
?>
<html>
<head>
<title>Breeze Website Builder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="manager.css" type="text/css">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<script language="javascript" type="text/javascript">

function OpenHelpWindow()
{
	window.open("http://breezewebsitebuilder.com/docs/cms/help.html#users", "Help", "scrollbars=1,resizable=1,status=0,toolbar=0,menubar=0,location=0,height=700,width=800");
}

function DeleteConfirm()
{
	document.getElementById("delete_confirm").style.display = "block";
	document.getElementById("delete_confirm").style.position = "absolute";
	document.getElementById("delete_confirm").style.top = "200px";
	document.getElementById("delete_confirm").style.left = "300px";
	document.getElementById("delete_confirm").style.width = "450px";
	document.getElementById("delete_confirm").style.backgroundColor = "#FFF";
	document.getElementById("delete_confirm").style.borderStyle = "solid";
	document.getElementById("delete_confirm").style.borderWidth = "2px";
	document.getElementById("delete_confirm").style.borderColor = "#000";
	document.getElementById("delete_text").innerHTML = document.getElementById("selectUserNameDelete").value;
}

function DeleteUser()
{
	var user_name = document.getElementById("selectUserNameDelete").value;
	document.getElementById("delete_confirm").style.display = "none";
	var url = "delete_user.php?user_name=" + user_name;
	window.location = url;
}

function HideDelete()
{
	document.getElementById("delete_confirm").style.display = "none";
}

function GeneratePassword(source)
{
	var password = "";
	var password_length = rand(6,10);
	var i;
	var assci_segment;
	
	for (i = 0; i < password_length; i++)
	{
		assci_segment = rand(1,3);
		if (assci_segment == 1)
			password = password + String.fromCharCode(rand(48,57));	// numeric
		else if (assci_segment == 2)
			password = password + String.fromCharCode(rand(65,90));	// alpha upper case
		else
			password = password + String.fromCharCode(rand(97,122));	// alpha lower case
	}
	password = password.replace('I', 'i');
	password = password.replace('O', 'o');
	password = password.replace('l', 'L');
	
	if (source == "new")
		var frm_obj = document.getElementById ('password_new_user');
	else
		var frm_obj = document.getElementById ('password_existing_user');
		
	frm_obj.value = password;
		
	return password;
}

// Equivalent to PHP rand(min, max);
function rand ( n, m )
{
  return ( n + Math.floor ( Math.random ( ) * (m - n + 1)) );
}

</script>

</head>

<body>
<br>
<div align="center">
<p class="title">Manage Users</p>
<table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
<form name="formAddUser" method="post" action="add_user.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="6" class="sub_title">Add User</td>
  </tr>
  <tr>
    <td align="right">User Name: </td>
    <td><input type="text" name="user_name"></td>
    <td align="right">Password: </td>
    <td> <input type="text" name="password" id="password_new_user"></td>
    <td align="right">Access Level: </td>
    <td><select name="selectAccessLevel" id="selectAccessLevel">
    <option value="blogger">Blogger</option>
	<option value="restricted">Restricted</option>
    <option value="user">User</option>
    <option value="power_user" selected>Power User</option>
    <option value="admin">Admin</option>
    </select></td>
  </tr>
  <tr>
  <td colspan="3">&nbsp;</td>
	<td>
	<input type="button" name="generate_password" value="Generate Password" onClick="GeneratePassword('new');">
	</td>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="6" align="center"><input type="submit" name="Submit" value="Add"></td>
  </tr>
  </table>
  </form>
  </td></tr></table>
  <br>
  <table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
  <form name="formChangePassword" method="post" action="change_password.php">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="6" class="sub_title">Change Password</td>
  </tr>
  <tr>
    <td align="right">User Name: </td>
    <td><select name="selectUserName" id="selectUserName">
<?php
	for ($i = 0; $i < ($num_results - 1); $i++)
		echo "<option value='$user_name[$i]'>$user_name[$i]</option>";
	echo "<option value='admin'>admin</option>";
?>
    </select></td>
	 <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td align="right">New Password: </td>
    <td>
    	<input type="text" name="password" id="password_existing_user">
		<input type="button" name="generate_password2" value="Generate Password"  onClick="GeneratePassword('existing');" />
	</td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>
  <tr>
    <td colspan="6" align="center"><input type="submit" name="Submit" value="Change Password"></td>
  </tr>
  </table>
  </form>
  </td></tr></table>
  <br>
  <table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>

  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="6" class="sub_title">Delete User</td>
  </tr>
  <tr>
  	<td>&nbsp;</td>
    <td>&nbsp;</td>
    <td align="right">User Name: </td>
    <td><select name="selectUserNameDelete" id="selectUserNameDelete">
<?php
	for ($i = 0; $i < ($num_results - 1); $i++)
		echo "<option value='$user_name[$i]'>$user_name[$i]";
?>
    </select></td>
	<td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>
  <tr>
    <td colspan="6" align="center"><input type="button" name="delete" value="Delete" onClick="DeleteConfirm();"></td>
  </tr>
</table>

  </td></tr></table>
  <br>
  <table width="80%" border="0" cellspacing="0" cellpadding="0">
  <tr>
  <td align="center">
  <input type="button" name="Back" value="Back to Main Menu" onClick="window.location.href='main.php?mode=admin';">
  </td>
  <td valign="top" align="right" width="10%">
      <div id="help_button" onClick="OpenHelpWindow();">
        &nbsp;
      </div>
  </td>
  </tr>
</table>
</div>

<div id="delete_confirm">
<table border="0" cellspacing="0" cellpadding="5" align="center">
  <tr>
    <td colspan="2">Are your sure you want to delete the user <b><div id="delete_text">&nbsp;</div></b>?</td>
  </tr>
  <tr>
	<td align="center"><input type="button" name="delete" id="delete" value="Yes" onClick="DeleteUser();" /></td>
	<td align="center"><input type="button" name="hide_delete" value="No" onClick="HideDelete();" />
    </td>
  </tr>
</table>
</div>

</body>
</html>
