<?php
$DB_DATABASE = "";
$prefix = "iitech_";
if(isset($_POST['database'])){
	$DB_DATABASE = $prefix.$_POST['database'];
}

// include db connect class
require_once __DIR__ . '/include/new_db_connect.php';

// connecting to db
$bdd = connect("iitech_universite");


require_once 'include/user.php';

$email = "";


if(isset($_POST['email'])){
	$email = $_POST['email'];
	try{
		$sql  = 'SELECT * FROM users_student where email = :email';
		$statement = $bdd->prepare($sql);
		$statement->bindParam(':email', 		$email, 		PDO::PARAM_STR);
		$statement->execute();
		$tab=$statement->fetch(PDO::FETCH_ASSOC);
		$username = $tab['username'];
		if($statement->rowCount() > 0) {
			$expediteur   = 'iitech@iitech-benin.net'; // L'email de l'exp&eacute;diteur.
			$message= "Hello, <br /><br />Your username is : $username. <br /><br />With best and warm regards.";
			$message    = str_replace('<br />', "\n", $message);// message trait&eacute; pour les sauts de ligne
			$to         = "$email";// e-mail du destinataire
			$subject   = 'Your username GestStudent';
			$headers   = 'From: '.$expediteur."\r\n";   // en-tête de l'e-mail
			mail($to, $subject, $message, $headers);			
		}
	}catch(PDOException  $e ){
		echo $e->getMessage( );
	}
}

if(isset($_POST['fp_email'])){
	$email = $_POST['fp_email'];
	try{
		$sql  = 'SELECT * FROM users_student where email = :email';
		$statement = $bdd->prepare($sql);
		$statement->bindParam(':email', 		$email, 		PDO::PARAM_STR);
		$statement->execute();
		$tab=$statement->fetch(PDO::FETCH_ASSOC);
		$username = $tab['username'];
		$code  =  mt_rand(); 
		if($statement->rowCount() > 0) {
			$sql  = 'DELETE FROM verification where login = :login';
			$statement = $bdd->prepare($sql);
			$statement->bindParam(':login', 		$username, 		PDO::PARAM_STR);
			$statement->execute();
			
			$sql  = 'insert into verification (login, code) values (:username, :code)';
			$statement = $bdd->prepare($sql);
			$statement->bindParam(':username', 		$username, 	PDO::PARAM_STR);
			$statement->bindParam(':code', 			$code, 		PDO::PARAM_STR);
			$statement->execute();
			
			$expediteur   = 'iitech@iitech-benin.net'; // L'email de l'exp&eacute;diteur.
			$message= "Hello, <br /><br />You request to reset the password of your account GestStudent. <br /><br />To reset, you must enter the verification code to confirm that it is a legitimate request. <br /><br />The verification code is : $code and your username is $username.<br /><br />With best and warm regards.";
			$message    = str_replace('<br />', "\n", $message);// message trait&eacute; pour les sauts de ligne
			$to         = "$email";// e-mail du destinataire
			$subject   = 'Your password reset request for GestStudent';
			$headers   = 'From: '.$expediteur."\r\n";   // en-tête de l'e-mail
			mail($to, $subject, $message, $headers);					
		}
	}catch(PDOException  $e ){
		echo $e->getMessage( );
	}
}


// Instance of a User class
$userObject = new User();

// User Login
if(!empty($email)){
    $json_array = $userObject->emailUsers($email, $bdd);
    echo json_encode($json_array);
}

?>