<?php
/////////////////////////////////////////////
// Users Management /////
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, 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.
// Revised: 11/16/2012	ELL
// Added checking for SSL and prompting if not SSL.
// Revised: 7/1/2013	ELL
// Implemented popup message class.
// 
//
	include ("check_session.php");
	
	$username_pass_change = $_REQUEST['user_name'];
	$password_pass_change = $_REQUEST['password'];
	$new_username = $_REQUEST['new_username'];
	$new_user_access_level = $_REQUEST['access_level'];
			
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "users`";
		$result = $bwb_dbh->query($query);
		$num_results = $result->rowCount();
		$j = 0;
		for ($i = 0; $i < $num_results; $i++)
		{
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$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">

<style type="text/css">
#generate_password_btn	{	width:150px;	}
#add_user_btn	{	width:110px;	}
#generate_password_btn2	{	width:150px;}
#change_password_btn	{	width:180px;	}
#delete_user_btn	{	width:110px;	}
</style>

<script type="text/javascript" src="js/mouse_position.js"></script>
<script type="text/javascript" src="js/popup_msg.js"></script>
<script type="text/javascript" src="js/users.js"></script>

</head>

<body>
<br>
<div align="center">
<table width="80%"><tr><td>
<div class="button_heading_sub_level_1"><div class="button_heading_text_sub_level_1">Manage Users</div><div class="button_heading_help" onClick="OpenHelpWindow();">&nbsp;</div></div>
</td></tr></table>
<?php
	if ($_SERVER["HTTPS"] != "on")
	{
		 $pageURL .= "https://";
		 if ($_SERVER["SERVER_PORT"] != "80")
		  	$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		 else
		 	$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
			
		 echo "<p class='warning_text' align='justify' style='width:80%;'><b>WARNING:</b> You are viewing this console with an unencrypted connection.  If you are adding users or changing passwords it is recommended that you use an encrypted connection.  To switch to an encrypted connection, click <b><a href='$pageURL'>here</a></b>.  <br /><span class='small_message'>Note: The SSL certificate may not be valid.  If this is the case, just accept the certificate anyway and continue to the encrypted connection.  Also, some servers do not have SSL certificates installed, so it might not be possible to view this console encrypted.  If clicking on the link does not work just click the back button.</span></p>\n";
	}
?>
<table width="80%" class="group_box_table"><tr><td>
<form name="formAddUser" id="formAddUser" method="post" action="add_user.php">
<table width="100%" class="layout_box_table" 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" width="150">Password: </td>
    <td> <input type="text" name="password" id="password_new_user"></td>
    <td>
    	 <div class='button_console' id="generate_password_btn" onClick="GeneratePassword('new');"><span class='button_console_text'>Generate Password</span></div>
    </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="7" align="center">
    <div class='button_console_add' id="add_user_btn" onClick="document.getElementById('formAddUser').submit();"><span class='button_console_text_w_icon'>Add</span></div>
    </td>
  </tr>
  </table>
  </form>
  </td></tr></table>
  <br>
  <table width="80%" class="group_box_table"><tr><td>
  <form name="formChangePassword" id="formChangePassword" method="post" action="change_password.php">
  <table width="100%" class="layout_box_table" 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 align="right">New Password: </td>
    <td width="150">
    	<input type="text" name="password" id="password_existing_user">
    </td>
    <td>
        <div class='button_console' id="generate_password_btn2" onClick="GeneratePassword('existing');"><span class='button_console_text'>Generate Password</span></div>
	</td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>
  <tr>
    <td colspan="6" align="center">
    <div class='button_console_edit' id="change_password_btn" onClick="document.getElementById('formChangePassword').submit();"><span class='button_console_text_w_icon'>Change Password</span></div>
    </td>
  </tr>
  </table>
  </form>
  </td></tr></table>
  <br>
  <table width="80%" class="group_box_table"><tr><td>

  <table width="100%" class="layout_box_table" 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">
    <div class='button_console_delete' id="delete_user_btn" onClick="DeleteConfirm();"><span class='button_console_text_w_icon'>Delete</span></div>
    </td>
  </tr>
</table>

  </td></tr></table>
</div>

<?php include("lib/popup_msg.html"); ?>

<script language="javascript" type="text/javascript">
	HidePopupMsg();
<?php	
if (isset($username_pass_change) && ($username_pass_change != ""))
{
	echo "var username_pass_change = '$username_pass_change';";
	echo "var password_pass_change = '$password_pass_change';";
?>
	popup_msg.top_offset = 100;
	popup_msg.left_offset = 320;
	popup_msg.popup_msg_type = 2;
	popup_msg.status_msg = "The password for <b>" + username_pass_change + "</b> has been changed to <b>" + password_pass_change + "</b>.";
	popup_msg.ShowPopupMsg();
<?php	}	?>
<?php	
if (isset($new_username) && ($new_username != ""))
{
	echo "var new_username = '$new_username';";
	echo "var password_pass_change = '$password_pass_change';";
	echo "var new_user_access_level = '$new_user_access_level';";
?>
	popup_msg.top_offset = 100;
	popup_msg.left_offset = 320;
	popup_msg.popup_msg_type = 2;
	popup_msg.status_msg = "New user <b>" + new_username + "</b> has been created with password <b>" + password_pass_change + "</b> and an access level of <b>" + new_user_access_level + "</b>.";
	popup_msg.ShowPopupMsg();
<?php	}	?>
</script>
</body>
</html>
