<?php

require("functions/config.php"); 
 
	// CSRF Protection
	require 'functions/CSRF_Protect.php';
	$csrf = new CSRF_Protect(); 

	// Error Reporting Active
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);

if(isset($_POST['submit']))
{
	global $conection;
	$email = $_POST['email']; 
	$sql = mysqli_query($conection,"SELECT username FROM users where email = '$email'") 
	or die(mysqli_error());  
	$row = mysqli_fetch_array($sql);
	$rownum = mysqli_num_rows($sql);
	
    if($rownum ==1  ){
    
	// Security measures
        $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 
        $pass = hash('sha256', "admin" . $salt);
		for($round = 0; $round < 65536; $round++){ $pass = hash('sha256', $pass . $salt); }		
	
		$query = mysqli_query($conection,"update users set  password='$pass', salt='$salt', email='$email', lang='en' where email='$email';");

		$from_add = "veetadsouza@gmail.com"; //<-- put your own email address here

		$to_add = $_POST['email']; 

		$subject= "Your New Password Request"; 
		
		$message = "New Password admin";
		$status=mail("veetadsouza@gmail.com","Password Request", $message);
	}
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>FLY CMS</title>
    <meta name="description" content="">
    <meta name="author" content="">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="shortcut icon" href="assets/images/favico.ico" type="image/x-icon" >
	<script src="assets/js/jquery.min.js"></script>
	<script src="assets/js/sweetalert.min.js"></script> 
	<link rel="stylesheet" type="text/css" href="assets/css/sweetalert.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" href="assets/css/font-awesome.min.css">
	<link href="assets/css/styles.css" rel="stylesheet" media="screen">
	
</head>

<body id="bg">

<div class="container hero-unit form-signin">
    <p><i class="fa fa-paper-plane-o" aria-hidden="true"></i> Forgot your password</p>
    <form action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>' method='post'>
		<?php $csrf->echoInputField(); ?>
		<input type="email" name="email" class="form-control" placeholder="Email" required value="" />
		<br/>		
		<input class="btn btn-info btn-flat" name="submit" type="submit" value="Reset Password">
	</form>
	
</div>

</body>
<?php

if (isset($_POST['submit'])){
	
	if($status == true)
		{ 
			echo "<script type='text/javascript'>swal('AWESOME', 'You received an email with your new password!', 'success');</script>";
					echo '<meta http-equiv="refresh" content="2; index.php">'; 
					die();
		} else { 
			echo "<script type='text/javascript'>swal('Oops...', 'That email doesn\'t exist... Please try again...', 'error');</script>";
					echo '<meta http-equiv="refresh" content="2; forgot.php">'; 
					die();
		}
			
		
}

?>
</html>