<?php # Script 9.7 - Password.php
// This page lets the user change their password.
session_start();
error_reporting(E_ALL);
require('includes/mysqli_connect.php'); // Connect to the Database.
$first_name = $_SESSION['first_name'];

$page_title = 'Change your Password';
include ('includes/header.php');


//(Below echoed out (shows error message but doesn't redirect )
if ($_SESSION['first_name'] != true)
  {
      echo("<h1>Login for access to this page</h1>");
       die();
   }
   
//If no member_ID is present, redirect the user.

if ($_SESSION['first_name'] != true) {
	//need the functions.
	require('includes/login_funtions.inc.php');
	redirect_user();
	die(); //added by Darren
}

//**ADD PHP** check if member and then get their details and put in form
if (!empty($_SESSION['first_name'])) {
		
		$query = mysqli_query($dbc, "SELECT * FROM members WHERE first_name = '".$first_name."'");
		$result = mysqli_fetch_assoc($query); // put result in an array
		if ($result) {
			$fn =  $result['first_name'];
			$ln = $result['last_name'];
			$e = $result['email'];
			$m = $result['mobile'];		
			$p = $result['password'];			
		}
}

// Check for form submission.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
	require('includes/mysqli_connect.php'); // Connect to the Database.
	
	$errors = array(); // Initialize an error array.
	
	//this isnt needed because logged in details entered 							
	
											//check for email address.
											/*if (empty($_POST['email'])) {
												$errors[] = 'You forgot to enter your email address.';
											} else {
												$e = mysqli_real_escape_string($dbc,trim($_POST['email']));
												//$e = trim($_POST['email']); try without this
											}
								
											//check for current password.
											if (empty($_POST['password'])) {
												$errors[] = 'You forgot to enter your current password.';
											} else {
												$p = mysqli_real_escape_string($dbc,trim($_POST['password']));
												//$p = trim($_POST['password']);	try without this	
											}*/
	
	//Check for new password and match with new confirmed password.
	
	if (!empty($_POST['pass1'])) {
		if ($_POST['pass1'] != $_POST['pass2']) {
		$errors[] = 'Your password did not match the confirmed password.';
	} else {
		$np = mysqli_real_escape_string($dbc,trim($_POST['pass1']));
		//$np = trim($_POST['pass1']); try without this
	}
	} else {
		$errors[] = 'You forgot to enter your new password';
	}
	
	if (empty($errors)){ // If everything is OK.
				$result = ""; //clear variable
				$query = ""; //clear variable
				
		// Check that they have entered the right email address/password combination.
    $query = "SELECT member_ID FROM members WHERE email= '$e' AND password= '$p' ";	//'".$e."' AND password='".$p."'";	try without this
	$result = @mysqli_query($dbc, $query);	
	$num = @mysqli_num_rows($result);
	
	if ($num == 1) { // Match was made.
	
		// Get the member_id.
		$row = mysqli_fetch_array($result, MYSQLI_NUM);
		
		// Make the update query.
		$query = "UPDATE members SET password = '$np' WHERE member_ID = '$row[0]' "; 				 // '".$row[0]."'"; Darren took this out.
		$result = @mysqli_query($dbc, $query);
		die();
	if (mysqli_affected_rows($dbc) == 1) {  // If it ran OK. (copied from text book)
		// Print message.
		echo '<h1>Thank You!</h1>';
		echo '<p>Your password has been updated.</p>';
	} else {
		
		// Public message.
		echo '<h1>System Error</h1>';
		echo '<p> Your password could not be changed due to a system error.  We apologize for any inconvenience.</p>';
		
		// Debugging message.
		echo'<p>' . mysqli_error($dbc) . '<br/><br/>Query: ' . $q . '</p>'; 			 // This was missing.  // <br/>Query: ' . $q . '  //
	}
		mysqli_close($dbc);  // Close the database connection.
		
		// include the footer and quit the script (to not show the form).
		include('includes/footer.php');  											// Check this Darren.
		exit();			
	} else { 
		echo '<h1>Error!</h1>';
		echo '<p class="error"> The email address and password did not match those on file.</p>';
	}
	
	} else {  // Report the errors.
	
		echo'<h1>Error!</h1>';
		echo'<p class="error">The following error(s)occurred:<br/>';
		foreach($errors as $msg){  // Print each error.
		echo " - $msg<br />\n";
	}
	
		echo'</p><p>Please try again.</p><p><br /></p>';
		}  // End of if (empty($errors)) IF.
		
		mysqli_close($dbc);  // Close the database connection.
	}   // End of the main Submit conditional.																										    
	?>  
	<br>                              
	<h1>Change Your Password</h1>
    <br>
    <hr>
    <br>
  	<form action="password.php" menthod="post" >
	<table cellspacing="10" cellpadding="5" border="0" style="width: 95%;" >
     <br>
     <tr>
             	<td>Email Address:</td>
                <td><input type="email" name="email" size="60" value="<?php echo $e ?>" /></td>
             </tr>
		
     <br>
		<td>Password</td>
                <td><input type="password" name="password" size="60" value="<?php echo $p ?>"/></td>
             </tr>
               
             <tr>
             	<td>New Password</td>
                <td><input type="password" name="pass1" size="60" /></td>
             </tr>
             
           
             	<td>Re-enter new Password</td>
                <td><input type="password" name="passd2" size="60" /></td>
             </tr>

     
    </table>  
    <br>
    	<input type="submit" name="submit" value="Change Password" />
     <br> 
  </form>
    <?php include ('includes/footer.php'); ?>
	


